From ace8491d59530a236fc91394d1cbd0517297202d Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 11 Oct 2018 18:19:52 +0200 Subject: [PATCH] Update arduino-mqtt library Update arduino-mqtt library to 2.4.0 --- lib/arduino-mqtt-2.3.3.02/src/system.cpp | 48 ------ lib/arduino-mqtt-2.3.3.02/src/system.h | 26 --- .../.editorconfig | 0 .../.gitignore | 0 .../.travis.yml | 2 +- .../CMakeLists.txt | 8 +- .../LICENSE.md | 0 .../Makefile | 2 +- .../README.md | 7 +- .../AdafruitHuzzahESP8266.ino | 0 .../AdafruitHuzzahESP8266Secure.ino | 0 .../ArduinoEthernetShield.ino | 0 .../ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino | 0 .../ArduinoMKRGSM1400Secure.ino | 0 .../ArduinoWiFi101/ArduinoWiFi101.ino | 0 .../ArduinoWiFi101Secure.ino | 0 .../ArduinoWiFiShield/ArduinoWiFiShield.ino | 0 .../examples/ArduinoYun/ArduinoYun.ino | 0 .../ArduinoYunSecure/ArduinoYunSecure.ino | 0 .../ESP32DevelopmentBoard.ino | 0 .../ESP32DevelopmentBoardSecure.ino | 0 .../library.properties | 2 +- .../src/MQTT.h} | 0 .../src/MQTTClient.h | 157 +++++++++++++++--- .../src/lwmqtt/client.c | 0 .../src/lwmqtt/helpers.c | 0 .../src/lwmqtt/helpers.h | 0 .../src/lwmqtt/lwmqtt.h | 0 .../src/lwmqtt/packet.c | 0 .../src/lwmqtt/packet.h | 0 .../src/lwmqtt/string.c | 0 31 files changed, 141 insertions(+), 111 deletions(-) delete mode 100644 lib/arduino-mqtt-2.3.3.02/src/system.cpp delete mode 100644 lib/arduino-mqtt-2.3.3.02/src/system.h rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/.editorconfig (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/.gitignore (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/.travis.yml (98%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/CMakeLists.txt (86%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/LICENSE.md (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/Makefile (79%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/README.md (95%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/AdafruitHuzzahESP8266Secure/AdafruitHuzzahESP8266Secure.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/ArduinoMKRGSM1400Secure/ArduinoMKRGSM1400Secure.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/ArduinoWiFi101/ArduinoWiFi101.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/ArduinoWiFi101Secure/ArduinoWiFi101Secure.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/ArduinoYun/ArduinoYun.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/ArduinoYunSecure/ArduinoYunSecure.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/examples/ESP32DevelopmentBoardSecure/ESP32DevelopmentBoardSecure.ino (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/library.properties (95%) rename lib/{arduino-mqtt-2.3.3.02/src/MQTTlw.h => arduino-mqtt-2.4.0/src/MQTT.h} (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/src/MQTTClient.h (74%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/src/lwmqtt/client.c (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/src/lwmqtt/helpers.c (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/src/lwmqtt/helpers.h (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/src/lwmqtt/lwmqtt.h (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/src/lwmqtt/packet.c (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/src/lwmqtt/packet.h (100%) rename lib/{arduino-mqtt-2.3.3.02 => arduino-mqtt-2.4.0}/src/lwmqtt/string.c (100%) diff --git a/lib/arduino-mqtt-2.3.3.02/src/system.cpp b/lib/arduino-mqtt-2.3.3.02/src/system.cpp deleted file mode 100644 index cfd8c4740..000000000 --- a/lib/arduino-mqtt-2.3.3.02/src/system.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include - -#include "system.h" - -void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout) { - // cast timer reference - auto t = (lwmqtt_arduino_timer_t *)ref; - - // set future end time - t->end = (uint32_t)(millis() + timeout); -} - -int32_t lwmqtt_arduino_timer_get(void *ref) { - // cast timer reference - auto t = (lwmqtt_arduino_timer_t *)ref; - - // get difference to end time - return (int32_t)t->end - (int32_t)millis(); -} - -lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buffer, size_t len, size_t *read, uint32_t timeout) { - // cast network reference - auto n = (lwmqtt_arduino_network_t *)ref; - - // set timeout - n->client->setTimeout(timeout); - - // read bytes - *read = n->client->readBytes(buffer, len); - if (*read <= 0) { - return LWMQTT_NETWORK_FAILED_READ; - } - - return LWMQTT_SUCCESS; -} - -lwmqtt_err_t lwmqtt_arduino_network_write(void *ref, uint8_t *buffer, size_t len, size_t *sent, uint32_t /*timeout*/) { - // cast network reference - auto n = (lwmqtt_arduino_network_t *)ref; - - // write bytes - *sent = n->client->write(buffer, len); - if (*sent <= 0) { - return LWMQTT_NETWORK_FAILED_WRITE; - }; - - return LWMQTT_SUCCESS; -} diff --git a/lib/arduino-mqtt-2.3.3.02/src/system.h b/lib/arduino-mqtt-2.3.3.02/src/system.h deleted file mode 100644 index e51739fb8..000000000 --- a/lib/arduino-mqtt-2.3.3.02/src/system.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef LWMQTT_ARDUINO_H -#define LWMQTT_ARDUINO_H - -#include -#include - -extern "C" { -#include "lwmqtt/lwmqtt.h" -}; - -typedef struct { - uint32_t end; -} lwmqtt_arduino_timer_t; - -void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout); - -int32_t lwmqtt_arduino_timer_get(void *ref); - -typedef struct { - Client *client; -} lwmqtt_arduino_network_t; - -lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buf, size_t len, size_t *read, uint32_t timeout); -lwmqtt_err_t lwmqtt_arduino_network_write(void *ref, uint8_t *buf, size_t len, size_t *sent, uint32_t timeout); - -#endif // LWMQTT_ARDUINO_H diff --git a/lib/arduino-mqtt-2.3.3.02/.editorconfig b/lib/arduino-mqtt-2.4.0/.editorconfig similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/.editorconfig rename to lib/arduino-mqtt-2.4.0/.editorconfig diff --git a/lib/arduino-mqtt-2.3.3.02/.gitignore b/lib/arduino-mqtt-2.4.0/.gitignore similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/.gitignore rename to lib/arduino-mqtt-2.4.0/.gitignore diff --git a/lib/arduino-mqtt-2.3.3.02/.travis.yml b/lib/arduino-mqtt-2.4.0/.travis.yml similarity index 98% rename from lib/arduino-mqtt-2.3.3.02/.travis.yml rename to lib/arduino-mqtt-2.4.0/.travis.yml index b59599cef..6971b9fba 100644 --- a/lib/arduino-mqtt-2.3.3.02/.travis.yml +++ b/lib/arduino-mqtt-2.4.0/.travis.yml @@ -1,7 +1,7 @@ language: generic env: global: - - IDE_VERSION=1.8.5 + - IDE_VERSION=1.8.7 matrix: - EXAMPLE="AdafruitHuzzahESP8266" BOARD="esp8266:esp8266:huzzah:FlashSize=4M3M,CpuFrequency=80" - EXAMPLE="AdafruitHuzzahESP8266Secure" BOARD="esp8266:esp8266:huzzah:FlashSize=4M3M,CpuFrequency=80" diff --git a/lib/arduino-mqtt-2.3.3.02/CMakeLists.txt b/lib/arduino-mqtt-2.4.0/CMakeLists.txt similarity index 86% rename from lib/arduino-mqtt-2.3.3.02/CMakeLists.txt rename to lib/arduino-mqtt-2.4.0/CMakeLists.txt index 58b0e31c9..328ce0c87 100644 --- a/lib/arduino-mqtt-2.3.3.02/CMakeLists.txt +++ b/lib/arduino-mqtt-2.4.0/CMakeLists.txt @@ -10,8 +10,8 @@ include_directories( /Users/256dpi/Development/Arduino/libraries/MKRGSM/src /Applications/Arduino.app/Contents/Java/libraries/Bridge/src /Users/256dpi/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WiFi/src - /Users/256dpi/Development/Arduino/hardware/espressif/esp32/libraries/WiFi/src - /Users/256dpi/Development/Arduino/hardware/espressif/esp32/libraries/WiFiClientSecure/src + /Users/256dpi/Library/Arduino15/packages/esp32/libraries/WiFi/src + /Users/256dpi/Library/Arduino15/packages/esp32/libraries/WiFiClientSecure/src src/) include_directories(src/) @@ -33,8 +33,6 @@ set(SOURCE_FILES examples/ESP32DevelopmentBoardSecure/ESP32DevelopmentBoardSecure.ino src/lwmqtt src/MQTT.h - src/MQTTClient.h - src/system.cpp - src/system.h) + src/MQTTClient.h) add_executable(arduino-mqtt ${SOURCE_FILES}) diff --git a/lib/arduino-mqtt-2.3.3.02/LICENSE.md b/lib/arduino-mqtt-2.4.0/LICENSE.md similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/LICENSE.md rename to lib/arduino-mqtt-2.4.0/LICENSE.md diff --git a/lib/arduino-mqtt-2.3.3.02/Makefile b/lib/arduino-mqtt-2.4.0/Makefile similarity index 79% rename from lib/arduino-mqtt-2.3.3.02/Makefile rename to lib/arduino-mqtt-2.4.0/Makefile index cfd69e49c..9e734fcc3 100644 --- a/lib/arduino-mqtt-2.3.3.02/Makefile +++ b/lib/arduino-mqtt-2.4.0/Makefile @@ -1,7 +1,7 @@ all: fmt fmt: - clang-format -i src/*.cpp src/*.h -style="{BasedOnStyle: Google, ColumnLimit: 120}" + clang-format -i src/*.h -style="{BasedOnStyle: Google, ColumnLimit: 120}" update: rm -rf ./lwmqtt diff --git a/lib/arduino-mqtt-2.3.3.02/README.md b/lib/arduino-mqtt-2.4.0/README.md similarity index 95% rename from lib/arduino-mqtt-2.3.3.02/README.md rename to lib/arduino-mqtt-2.4.0/README.md index 470c4a58c..92e61ed6e 100644 --- a/lib/arduino-mqtt-2.3.3.02/README.md +++ b/lib/arduino-mqtt-2.4.0/README.md @@ -151,11 +151,12 @@ void setOptions(int keepAlive, bool cleanSession, int timeout); Connect to broker using the supplied client id and an optional username and password: ```c++ -bool connect(const char clientId[]); -bool connect(const char clientId[], const char username[]); -bool connect(const char clientId[], const char username[], const char password[]); +bool connect(const char clientId[], bool skip = false); +bool connect(const char clientId[], const char username[], bool skip = false); +bool connect(const char clientId[], const char username[], const char password[], bool skip = false); ``` +- If the `skip` option is set to true, the client will skip the network level connection and jump to the MQTT level connection. This option can be used in order to establish and verify TLS connections manually before giving control to the MQTT client. - This functions returns a boolean that indicates if the connection has been established successfully. Publishes a message to the broker with an optional payload: diff --git a/lib/arduino-mqtt-2.3.3.02/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino b/lib/arduino-mqtt-2.4.0/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino rename to lib/arduino-mqtt-2.4.0/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/AdafruitHuzzahESP8266Secure/AdafruitHuzzahESP8266Secure.ino b/lib/arduino-mqtt-2.4.0/examples/AdafruitHuzzahESP8266Secure/AdafruitHuzzahESP8266Secure.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/AdafruitHuzzahESP8266Secure/AdafruitHuzzahESP8266Secure.ino rename to lib/arduino-mqtt-2.4.0/examples/AdafruitHuzzahESP8266Secure/AdafruitHuzzahESP8266Secure.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino b/lib/arduino-mqtt-2.4.0/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino rename to lib/arduino-mqtt-2.4.0/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino b/lib/arduino-mqtt-2.4.0/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino rename to lib/arduino-mqtt-2.4.0/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/ArduinoMKRGSM1400Secure/ArduinoMKRGSM1400Secure.ino b/lib/arduino-mqtt-2.4.0/examples/ArduinoMKRGSM1400Secure/ArduinoMKRGSM1400Secure.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/ArduinoMKRGSM1400Secure/ArduinoMKRGSM1400Secure.ino rename to lib/arduino-mqtt-2.4.0/examples/ArduinoMKRGSM1400Secure/ArduinoMKRGSM1400Secure.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/ArduinoWiFi101/ArduinoWiFi101.ino b/lib/arduino-mqtt-2.4.0/examples/ArduinoWiFi101/ArduinoWiFi101.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/ArduinoWiFi101/ArduinoWiFi101.ino rename to lib/arduino-mqtt-2.4.0/examples/ArduinoWiFi101/ArduinoWiFi101.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/ArduinoWiFi101Secure/ArduinoWiFi101Secure.ino b/lib/arduino-mqtt-2.4.0/examples/ArduinoWiFi101Secure/ArduinoWiFi101Secure.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/ArduinoWiFi101Secure/ArduinoWiFi101Secure.ino rename to lib/arduino-mqtt-2.4.0/examples/ArduinoWiFi101Secure/ArduinoWiFi101Secure.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino b/lib/arduino-mqtt-2.4.0/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino rename to lib/arduino-mqtt-2.4.0/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/ArduinoYun/ArduinoYun.ino b/lib/arduino-mqtt-2.4.0/examples/ArduinoYun/ArduinoYun.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/ArduinoYun/ArduinoYun.ino rename to lib/arduino-mqtt-2.4.0/examples/ArduinoYun/ArduinoYun.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/ArduinoYunSecure/ArduinoYunSecure.ino b/lib/arduino-mqtt-2.4.0/examples/ArduinoYunSecure/ArduinoYunSecure.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/ArduinoYunSecure/ArduinoYunSecure.ino rename to lib/arduino-mqtt-2.4.0/examples/ArduinoYunSecure/ArduinoYunSecure.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino b/lib/arduino-mqtt-2.4.0/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino rename to lib/arduino-mqtt-2.4.0/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino diff --git a/lib/arduino-mqtt-2.3.3.02/examples/ESP32DevelopmentBoardSecure/ESP32DevelopmentBoardSecure.ino b/lib/arduino-mqtt-2.4.0/examples/ESP32DevelopmentBoardSecure/ESP32DevelopmentBoardSecure.ino similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/examples/ESP32DevelopmentBoardSecure/ESP32DevelopmentBoardSecure.ino rename to lib/arduino-mqtt-2.4.0/examples/ESP32DevelopmentBoardSecure/ESP32DevelopmentBoardSecure.ino diff --git a/lib/arduino-mqtt-2.3.3.02/library.properties b/lib/arduino-mqtt-2.4.0/library.properties similarity index 95% rename from lib/arduino-mqtt-2.3.3.02/library.properties rename to lib/arduino-mqtt-2.4.0/library.properties index a5ce4138a..0cf498e9b 100644 --- a/lib/arduino-mqtt-2.3.3.02/library.properties +++ b/lib/arduino-mqtt-2.4.0/library.properties @@ -1,5 +1,5 @@ name=MQTT -version=2.3.3 +version=2.4.0 author=Joel Gaehwiler maintainer=Joel Gaehwiler sentence=MQTT library for Arduino diff --git a/lib/arduino-mqtt-2.3.3.02/src/MQTTlw.h b/lib/arduino-mqtt-2.4.0/src/MQTT.h similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/src/MQTTlw.h rename to lib/arduino-mqtt-2.4.0/src/MQTT.h diff --git a/lib/arduino-mqtt-2.3.3.02/src/MQTTClient.h b/lib/arduino-mqtt-2.4.0/src/MQTTClient.h similarity index 74% rename from lib/arduino-mqtt-2.3.3.02/src/MQTTClient.h rename to lib/arduino-mqtt-2.4.0/src/MQTTClient.h index ecb69ec1d..d87f62cd5 100644 --- a/lib/arduino-mqtt-2.3.3.02/src/MQTTClient.h +++ b/lib/arduino-mqtt-2.4.0/src/MQTTClient.h @@ -5,7 +5,66 @@ #include #include -#include "system.h" +extern "C" { +#include "lwmqtt/lwmqtt.h" +}; + +typedef struct { + uint32_t end; +} lwmqtt_arduino_timer_t; + +void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout); + +int32_t lwmqtt_arduino_timer_get(void *ref); + +typedef struct { + Client *client; +} lwmqtt_arduino_network_t; + +void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout) { + // cast timer reference + auto t = (lwmqtt_arduino_timer_t *)ref; + + // set future end time + t->end = (uint32_t)(millis() + timeout); +} + +int32_t lwmqtt_arduino_timer_get(void *ref) { + // cast timer reference + auto t = (lwmqtt_arduino_timer_t *)ref; + + // get difference to end time + return (int32_t)t->end - (int32_t)millis(); +} + +lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buffer, size_t len, size_t *read, uint32_t timeout) { + // cast network reference + auto n = (lwmqtt_arduino_network_t *)ref; + + // set timeout + n->client->setTimeout(timeout); + + // read bytes + *read = n->client->readBytes(buffer, len); + if (*read <= 0) { + return LWMQTT_NETWORK_FAILED_READ; + } + + return LWMQTT_SUCCESS; +} + +lwmqtt_err_t lwmqtt_arduino_network_write(void *ref, uint8_t *buffer, size_t len, size_t *sent, uint32_t /*timeout*/) { + // cast network reference + auto n = (lwmqtt_arduino_network_t *)ref; + + // write bytes + *sent = n->client->write(buffer, len); + if (*sent <= 0) { + return LWMQTT_NETWORK_FAILED_WRITE; + }; + + return LWMQTT_SUCCESS; +} class MQTTClient; @@ -70,14 +129,13 @@ class MQTTClient { Client *netClient = nullptr; const char *hostname = nullptr; int port = 0; - lwmqtt_will_t will = lwmqtt_default_will; - bool hasWill = false; + lwmqtt_will_t *will = nullptr; MQTTClientCallback callback; lwmqtt_arduino_network_t network = {nullptr}; lwmqtt_arduino_timer_t timer1 = {0}; lwmqtt_arduino_timer_t timer2 = {0}; - lwmqtt_client_t client; + lwmqtt_client_t client = {0}; bool _connected = false; lwmqtt_return_code_t _returnCode = (lwmqtt_return_code_t)0; @@ -85,13 +143,25 @@ class MQTTClient { public: explicit MQTTClient(int bufSize = 128) { - memset(&client, 0, sizeof(client)); + // reset client + memset(&this->client, 0, sizeof(lwmqtt_client_t)); + + // allocate buffers this->bufSize = (size_t)bufSize; this->readBuf = (uint8_t *)malloc((size_t)bufSize + 1); this->writeBuf = (uint8_t *)malloc((size_t)bufSize); } ~MQTTClient() { + // free will + this->clearWill(); + + // free hostname + if (this->hostname != nullptr) { + free((void *)this->hostname); + } + + // free buffers free(this->readBuf); free(this->writeBuf); } @@ -136,7 +206,7 @@ class MQTTClient { void setHost(const char hostname[], int port) { // free hostname if set - if(this->hostname != nullptr) { + if (this->hostname != nullptr) { free((void *)this->hostname); } @@ -150,37 +220,78 @@ class MQTTClient { void setWill(const char topic[], const char payload[]) { this->setWill(topic, payload, false, 0); } void setWill(const char topic[], const char payload[], bool retained, int qos) { - this->hasWill = true; - this->will.topic = lwmqtt_string(topic); - this->will.payload = lwmqtt_string(payload); - this->will.retained = retained; - this->will.qos = (lwmqtt_qos_t)qos; + // return if topic is missing + if (topic == nullptr || strlen(topic) == 0) { + return; + } + + // clear existing will + this->clearWill(); + + // allocate will + this->will = (lwmqtt_will_t *)malloc(sizeof(lwmqtt_will_t)); + memset(this->will, 0, sizeof(lwmqtt_will_t)); + + // set topic + this->will->topic = lwmqtt_string(strdup(topic)); + + // set payload if available + if (payload != nullptr && strlen(payload) > 0) { + this->will->payload = lwmqtt_string(strdup(payload)); + } + + // set flags + this->will->retained = retained; + this->will->qos = (lwmqtt_qos_t)qos; } - void clearWill() { this->hasWill = false; } + void clearWill() { + // return if not set + if (this->will == nullptr) { + return; + } + + // free payload if set + if (this->will->payload.len > 0) { + free(this->will->payload.data); + } + + // free topic if set + if (this->will->topic.len > 0) { + free(this->will->topic.data); + } + + // free will + free(this->will); + this->will = nullptr; + } void setOptions(int keepAlive, bool cleanSession, int timeout) { + // set new options this->keepAlive = (uint16_t)keepAlive; this->cleanSession = cleanSession; this->timeout = (uint32_t)timeout; } - bool connect(const char clientId[]) { return this->connect(clientId, nullptr, nullptr); } + bool connect(const char clientId[], bool skip = false) { return this->connect(clientId, nullptr, nullptr); } - bool connect(const char clientId[], const char username[]) { return this->connect(clientId, username, nullptr); } + bool connect(const char clientId[], const char username[], bool skip = false) { return this->connect(clientId, username, nullptr); } - bool connect(const char clientId[], const char username[], const char password[]) { + bool connect(const char clientId[], const char username[], const char password[], bool skip = false) { // close left open connection if still connected - if (this->connected()) { + if (!skip && this->connected()) { this->close(); } // save client this->network.client = this->netClient; - // connect to host - if (this->netClient->connect(this->hostname, (uint16_t)this->port) < 0) { - return false; + // connect to hostg + if(!skip) { + int ret = this->netClient->connect(this->hostname, (uint16_t)this->port); + if (ret <= 0) { + return false; + } } // prepare options @@ -198,14 +309,8 @@ class MQTTClient { } } - // prepare will reference - lwmqtt_will_t *will = nullptr; - if (this->hasWill) { - will = &this->will; - } - // connect to broker - this->_lastError = lwmqtt_connect(&this->client, options, will, &this->_returnCode, this->timeout); + this->_lastError = lwmqtt_connect(&this->client, options, this->will, &this->_returnCode, this->timeout); if (this->_lastError != LWMQTT_SUCCESS) { // close connection this->close(); diff --git a/lib/arduino-mqtt-2.3.3.02/src/lwmqtt/client.c b/lib/arduino-mqtt-2.4.0/src/lwmqtt/client.c similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/src/lwmqtt/client.c rename to lib/arduino-mqtt-2.4.0/src/lwmqtt/client.c diff --git a/lib/arduino-mqtt-2.3.3.02/src/lwmqtt/helpers.c b/lib/arduino-mqtt-2.4.0/src/lwmqtt/helpers.c similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/src/lwmqtt/helpers.c rename to lib/arduino-mqtt-2.4.0/src/lwmqtt/helpers.c diff --git a/lib/arduino-mqtt-2.3.3.02/src/lwmqtt/helpers.h b/lib/arduino-mqtt-2.4.0/src/lwmqtt/helpers.h similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/src/lwmqtt/helpers.h rename to lib/arduino-mqtt-2.4.0/src/lwmqtt/helpers.h diff --git a/lib/arduino-mqtt-2.3.3.02/src/lwmqtt/lwmqtt.h b/lib/arduino-mqtt-2.4.0/src/lwmqtt/lwmqtt.h similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/src/lwmqtt/lwmqtt.h rename to lib/arduino-mqtt-2.4.0/src/lwmqtt/lwmqtt.h diff --git a/lib/arduino-mqtt-2.3.3.02/src/lwmqtt/packet.c b/lib/arduino-mqtt-2.4.0/src/lwmqtt/packet.c similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/src/lwmqtt/packet.c rename to lib/arduino-mqtt-2.4.0/src/lwmqtt/packet.c diff --git a/lib/arduino-mqtt-2.3.3.02/src/lwmqtt/packet.h b/lib/arduino-mqtt-2.4.0/src/lwmqtt/packet.h similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/src/lwmqtt/packet.h rename to lib/arduino-mqtt-2.4.0/src/lwmqtt/packet.h diff --git a/lib/arduino-mqtt-2.3.3.02/src/lwmqtt/string.c b/lib/arduino-mqtt-2.4.0/src/lwmqtt/string.c similarity index 100% rename from lib/arduino-mqtt-2.3.3.02/src/lwmqtt/string.c rename to lib/arduino-mqtt-2.4.0/src/lwmqtt/string.c