From 0ba089d1e818202539381d190802adb9d029b970 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 28 Oct 2022 12:27:33 -0400 Subject: [PATCH 1/6] fixes processUntil --- Adafruit_MQTT.cpp | 32 +++++++++++++++++++++++++++++++- Adafruit_MQTT.h | 4 ++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index 9d5128e..d4ddd49 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -226,6 +226,9 @@ uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout) { uint16_t len; + DEBUG_PRINTLN("call: processPacketsUntil()"); + DEBUG_PRINT("Looking for packetType: "); + DEBUG_PRINTLN(waitforpackettype); while (true) { len = readFullPacket(buffer, MAXBUFFERSIZE, timeout); @@ -239,7 +242,31 @@ uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, return len; } else { if (packetType == MQTT_CTRL_PUBLISH) { - handleSubscriptionPacket(len); + + Adafruit_MQTT_Subscribe *sub = handleSubscriptionPacket(len); + if (sub) { + DEBUG_PRINTLN("processPacketsUntil got subscription!"); + if (sub->callback_uint32t != NULL) { + // huh lets do the callback in integer mode + uint32_t data = 0; + data = atoi((char *)sub->lastread); + sub->callback_uint32t(data); + } else if (sub->callback_double != NULL) { + // huh lets do the callback in doublefloat mode + double data = 0; + data = atof((char *)sub->lastread); + sub->callback_double(data); + } else if (sub->callback_buffer != NULL) { + // huh lets do the callback in buffer mode + DEBUG_PRINTLN("processPacketsUntil called the callback_buffer!"); + sub->callback_buffer((char *)sub->lastread, sub->datalen); + } else if (sub->callback_io != NULL) { + // huh lets do the callback in io mode + ((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread, + sub->datalen); + } + } + } else { ERROR_PRINTLN(F("Dropped a packet")); } @@ -477,8 +504,10 @@ void Adafruit_MQTT::processPackets(int16_t timeout) { uint32_t elapsed = 0, endtime, starttime = millis(); while (elapsed < (uint32_t)timeout) { + DEBUG_PRINTLN("L480: readSubscription() called by processPackets()"); Adafruit_MQTT_Subscribe *sub = readSubscription(timeout - elapsed); if (sub) { + DEBUG_PRINTLN("processPackets got subscription!"); if (sub->callback_uint32t != NULL) { // huh lets do the callback in integer mode uint32_t data = 0; @@ -491,6 +520,7 @@ void Adafruit_MQTT::processPackets(int16_t timeout) { sub->callback_double(data); } else if (sub->callback_buffer != NULL) { // huh lets do the callback in buffer mode + DEBUG_PRINTLN("processPackets callback_buffer!"); sub->callback_buffer((char *)sub->lastread, sub->datalen); } else if (sub->callback_io != NULL) { // huh lets do the callback in io mode diff --git a/Adafruit_MQTT.h b/Adafruit_MQTT.h index 7249aa1..c7ec436 100644 --- a/Adafruit_MQTT.h +++ b/Adafruit_MQTT.h @@ -34,7 +34,7 @@ #define ADAFRUIT_MQTT_VERSION_PATCH 0 // Uncomment/comment to turn on/off debug output messages. -//#define MQTT_DEBUG +#define MQTT_DEBUG // Uncomment/comment to turn on/off error output messages. #define MQTT_ERROR @@ -107,7 +107,7 @@ // Largest full packet we're able to send. // Need to be able to store at least ~90 chars for a connect packet with full // 23 char client ID. -#define MAXBUFFERSIZE (150) +#define MAXBUFFERSIZE (512) #define MQTT_CONN_USERNAMEFLAG 0x80 #define MQTT_CONN_PASSWORDFLAG 0x40 From 5c00e84d085ae38574362a93fed9d5332dd71559 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 28 Oct 2022 13:01:06 -0400 Subject: [PATCH 2/6] add new processSubscriptionPacket func --- Adafruit_MQTT.cpp | 74 +++++++++++++++++------------------------------ Adafruit_MQTT.h | 6 ++-- 2 files changed, 31 insertions(+), 49 deletions(-) diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index d4ddd49..e4bd2cc 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -222,6 +222,29 @@ int8_t Adafruit_MQTT::connect(const char *user, const char *pass) { return connect(); } +void Adafruit_MQTT::processSubscriptionPacket(Adafruit_MQTT_Subscribe *sub) { + if (sub->callback_uint32t != NULL) { + // execute callback in integer mode + uint32_t data = 0; + data = atoi((char *)sub->lastread); + sub->callback_uint32t(data); + } else if (sub->callback_double != NULL) { + // execute callback in doublefloat mode + double data = 0; + data = atof((char *)sub->lastread); + sub->callback_double(data); + } else if (sub->callback_buffer != NULL) { + // execute callback in buffer mode + DEBUG_PRINTLN("processPacketsUntil called the callback_buffer!"); + sub->callback_buffer((char *)sub->lastread, sub->datalen); + } else if (sub->callback_io != NULL) { + // execute callback in io mode + ((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread, sub->datalen); + } + // mark subscription message as "read"" + sub->new_message = false; +} + uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout) { @@ -242,31 +265,9 @@ uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, return len; } else { if (packetType == MQTT_CTRL_PUBLISH) { - Adafruit_MQTT_Subscribe *sub = handleSubscriptionPacket(len); - if (sub) { - DEBUG_PRINTLN("processPacketsUntil got subscription!"); - if (sub->callback_uint32t != NULL) { - // huh lets do the callback in integer mode - uint32_t data = 0; - data = atoi((char *)sub->lastread); - sub->callback_uint32t(data); - } else if (sub->callback_double != NULL) { - // huh lets do the callback in doublefloat mode - double data = 0; - data = atof((char *)sub->lastread); - sub->callback_double(data); - } else if (sub->callback_buffer != NULL) { - // huh lets do the callback in buffer mode - DEBUG_PRINTLN("processPacketsUntil called the callback_buffer!"); - sub->callback_buffer((char *)sub->lastread, sub->datalen); - } else if (sub->callback_io != NULL) { - // huh lets do the callback in io mode - ((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread, - sub->datalen); - } - } - + if (sub) + processSubscriptionPacket(sub); } else { ERROR_PRINTLN(F("Dropped a packet")); } @@ -506,29 +507,8 @@ void Adafruit_MQTT::processPackets(int16_t timeout) { while (elapsed < (uint32_t)timeout) { DEBUG_PRINTLN("L480: readSubscription() called by processPackets()"); Adafruit_MQTT_Subscribe *sub = readSubscription(timeout - elapsed); - if (sub) { - DEBUG_PRINTLN("processPackets got subscription!"); - if (sub->callback_uint32t != NULL) { - // huh lets do the callback in integer mode - uint32_t data = 0; - data = atoi((char *)sub->lastread); - sub->callback_uint32t(data); - } else if (sub->callback_double != NULL) { - // huh lets do the callback in doublefloat mode - double data = 0; - data = atof((char *)sub->lastread); - sub->callback_double(data); - } else if (sub->callback_buffer != NULL) { - // huh lets do the callback in buffer mode - DEBUG_PRINTLN("processPackets callback_buffer!"); - sub->callback_buffer((char *)sub->lastread, sub->datalen); - } else if (sub->callback_io != NULL) { - // huh lets do the callback in io mode - ((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread, - sub->datalen); - } - } - + if (sub) + processSubscriptionPacket(sub); // keep track over elapsed time endtime = millis(); if (endtime < starttime) { diff --git a/Adafruit_MQTT.h b/Adafruit_MQTT.h index c7ec436..e2609b1 100644 --- a/Adafruit_MQTT.h +++ b/Adafruit_MQTT.h @@ -209,10 +209,12 @@ public: // messages! Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout = 0); - // Handle any data coming in for subscriptions and fires them off to the - // appropriate callback + // Handle any data coming in for subscriptions Adafruit_MQTT_Subscribe *handleSubscriptionPacket(uint16_t len); + // Execute a subscription packet's associated callback and mark as "read" + void processSubscriptionPacket(Adafruit_MQTT_Subscribe *sub); + void processPackets(int16_t timeout); // Ping the server to ensure the connection is still alive. From 30d4af81799b4351c4fc4bf6a08b28ea03578283 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 28 Oct 2022 14:53:32 -0400 Subject: [PATCH 3/6] remove debug --- Adafruit_MQTT.cpp | 5 ----- Adafruit_MQTT.h | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index e4bd2cc..1da9be2 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -235,7 +235,6 @@ void Adafruit_MQTT::processSubscriptionPacket(Adafruit_MQTT_Subscribe *sub) { sub->callback_double(data); } else if (sub->callback_buffer != NULL) { // execute callback in buffer mode - DEBUG_PRINTLN("processPacketsUntil called the callback_buffer!"); sub->callback_buffer((char *)sub->lastread, sub->datalen); } else if (sub->callback_io != NULL) { // execute callback in io mode @@ -249,9 +248,6 @@ uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout) { uint16_t len; - DEBUG_PRINTLN("call: processPacketsUntil()"); - DEBUG_PRINT("Looking for packetType: "); - DEBUG_PRINTLN(waitforpackettype); while (true) { len = readFullPacket(buffer, MAXBUFFERSIZE, timeout); @@ -505,7 +501,6 @@ void Adafruit_MQTT::processPackets(int16_t timeout) { uint32_t elapsed = 0, endtime, starttime = millis(); while (elapsed < (uint32_t)timeout) { - DEBUG_PRINTLN("L480: readSubscription() called by processPackets()"); Adafruit_MQTT_Subscribe *sub = readSubscription(timeout - elapsed); if (sub) processSubscriptionPacket(sub); diff --git a/Adafruit_MQTT.h b/Adafruit_MQTT.h index e2609b1..00657b4 100644 --- a/Adafruit_MQTT.h +++ b/Adafruit_MQTT.h @@ -34,7 +34,7 @@ #define ADAFRUIT_MQTT_VERSION_PATCH 0 // Uncomment/comment to turn on/off debug output messages. -#define MQTT_DEBUG +// #define MQTT_DEBUG // Uncomment/comment to turn on/off error output messages. #define MQTT_ERROR @@ -107,7 +107,7 @@ // Largest full packet we're able to send. // Need to be able to store at least ~90 chars for a connect packet with full // 23 char client ID. -#define MAXBUFFERSIZE (512) +#define MAXBUFFERSIZE (150) #define MQTT_CONN_USERNAMEFLAG 0x80 #define MQTT_CONN_PASSWORDFLAG 0x40 From a80db209cd9f76172269a435a5cc6014850370aa Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 28 Oct 2022 14:53:54 -0400 Subject: [PATCH 4/6] bump lib prop --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 8baa280..5231595 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit MQTT Library -version=2.4.3 +version=2.4.4 author=Adafruit maintainer=Adafruit sentence=MQTT library that supports the FONA, ESP8266, ESP32, Yun, and generic Arduino Client hardware. From 52e04bebe98b2f03116d58b24d6fa5dde83aa537 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 28 Oct 2022 16:32:51 -0400 Subject: [PATCH 5/6] loren review --- Adafruit_MQTT.cpp | 3 +++ library.properties | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index 1da9be2..85f91d8 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -239,6 +239,9 @@ void Adafruit_MQTT::processSubscriptionPacket(Adafruit_MQTT_Subscribe *sub) { } else if (sub->callback_io != NULL) { // execute callback in io mode ((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread, sub->datalen); + } else { + DEBUG_PRINTLN("ERROR: Subscription packet did not have an associated callback"); + return; } // mark subscription message as "read"" sub->new_message = false; diff --git a/library.properties b/library.properties index 5231595..f2aca1f 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit MQTT Library -version=2.4.4 +version=2.5.0 author=Adafruit maintainer=Adafruit sentence=MQTT library that supports the FONA, ESP8266, ESP32, Yun, and generic Arduino Client hardware. From 1eb56066eff1f1e3e6f23ef17c5a36cad4b76146 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 28 Oct 2022 16:44:07 -0400 Subject: [PATCH 6/6] clang --- Adafruit_MQTT.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index 85f91d8..eaaff81 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -240,7 +240,8 @@ void Adafruit_MQTT::processSubscriptionPacket(Adafruit_MQTT_Subscribe *sub) { // execute callback in io mode ((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread, sub->datalen); } else { - DEBUG_PRINTLN("ERROR: Subscription packet did not have an associated callback"); + DEBUG_PRINTLN( + "ERROR: Subscription packet did not have an associated callback"); return; } // mark subscription message as "read""