<commit_before>#ifndef MQTT_CLIENT_H
#define MQTT_CLIENT_H

#include <Arduino.h>
#include <Client.h>
#include <Stream.h>

#include "system.h"

class MQTTClient;

typedef void (*MQTTClientCallbackSimple)(String &topic, String &payload);
typedef void (*MQTTClientCallbackAdvanced)(MQTTClient * client, String &topic, String &payload, char bytes[], unsigned int length);

typedef struct {
  MQTTClient * client = NULL;
  bool use_advanced = false;
  MQTTClientCallbackSimple simple = NULL;
  MQTTClientCallbackAdvanced advanced = NULL;
} MQTTClientCallback;

static void MQTTClient_callback(lwmqtt_client_t *client, void *ref, lwmqtt_string_t *topic, lwmqtt_message_t *message) {
  // get callback
  MQTTClientCallback *cb = (MQTTClientCallback *)ref;

  // null terminate topic to create String object
  char terminated_topic[topic->len + 1];
  memcpy(terminated_topic, topic->data, (size_t)topic->len);
  terminated_topic[topic->len] = '\0';

  // get payload
  char *payload = (char *)message->payload;

  // null terminate payload
  payload[message->payload_len] = '\0';

  // create arduino strings
  String str_topic = String(terminated_topic);
  String str_payload = String(payload);

  // call the user callback
  if (cb->use_advanced) {
    cb->advanced(cb->client, str_topic, str_payload, (char *)message->payload, (unsigned int)message->payload_len);
  } else {
    cb->simple(str_topic, str_payload);
  }
}

class MQTTClient {
 private:
  int bufSize;
  unsigned char *readBuf;
  unsigned char *writeBuf;

  unsigned int timeout = 1000;

  Client *netClient;
  const char *hostname;
  int port;
  lwmqtt_will_t will;
  bool hasWill;
  MQTTClientCallback callback;

  lwmqtt_arduino_network_t network;<commit_msg>added note<commit_after>#ifndef MQTT_CLIENT_H
#define MQTT_CLIENT_H

#include <Arduino.h>
#include <Client.h>
#include <Stream.h>

#include "system.h"

class MQTTClient;

typedef void (*MQTTClientCallbackSimple)(String &topic, String &payload);
typedef void (*MQTTClientCallbackAdvanced)(MQTTClient * client, String &topic, String &payload, char bytes[], unsigned int length);

typedef struct {
  MQTTClient * client = NULL;
  bool use_advanced = false;
  MQTTClientCallbackSimple simple = NULL;
  MQTTClientCallbackAdvanced advanced = NULL;
} MQTTClientCallback;

static void MQTTClient_callback(lwmqtt_client_t *client, void *ref, lwmqtt_string_t *topic, lwmqtt_message_t *message) {
  // get callback
  MQTTClientCallback *cb = (MQTTClientCallback *)ref;

  // null terminate topic to create String object
  char terminated_topic[topic->len + 1];
  memcpy(terminated_topic, topic->data, (size_t)topic->len);
  terminated_topic[topic->len] = '\0';

  // get payload
  char *payload = (char *)message->payload;

  // null terminate payload
  payload[message->payload_len] = '\0';

  // create arduino strings
  // TODO: Is there no way to create the strings without causing the data to be copied?
  String str_topic = String(terminated_topic);
  String str_payload = String(payload);

  // call the user callback
  if (cb->use_advanced) {
    cb->advanced(cb->client, str_topic, str_payload, (char *)message->payload, (unsigned int)message->payload_len);
  } else {
    cb->simple(str_topic, str_payload);
  }
}

class MQTTClient {
 private:
  int bufSize;
  unsigned char *readBuf;
  unsigned char *writeBuf;

  unsigned int timeout = 1000;

  Client *netClient;
  const char *hostname;
  int port;
  lwmqtt_will_t will;
  bool hasWill;
  MQTTClientCallback callback;

  lwmqtt_arduino_network_t network;
{'fraction_non_alphanumeric': 0.10887439750496172, 'fraction_numerical': 0.00396937907570173, 'mean_word_length': 4.831404958677686, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2799531', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1137, 'n_words': 306}