Skip to content

Commit f63a37b

Browse files
authored
Merge pull request #170 from Fapiko/better-ping-handling
Better ping handling
2 parents c7f716c + f3f5c18 commit f63a37b

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

Adafruit_MQTT.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,12 @@ int8_t Adafruit_MQTT::connect() {
152152

153153
// Read connect response packet and verify it
154154
len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
155-
if (len != 4)
155+
if (len != 4) {
156156
return -1;
157-
if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2))
157+
}
158+
if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2)) {
158159
return -1;
160+
}
159161
if (buffer[3] != 0)
160162
return buffer[3];
161163

@@ -182,7 +184,6 @@ int8_t Adafruit_MQTT::connect() {
182184
// the Subscription before the Server sends the SUBACK Packet. (will
183185
// really need to use callbacks - ada)
184186

185-
// Serial.println("\t**looking for suback");
186187
if (processPacketsUntil(buffer, MQTT_CTRL_SUBACK, SUBACK_TIMEOUT_MS)) {
187188
success = true;
188189
break;
@@ -213,10 +214,15 @@ uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer,
213214
break;
214215
}
215216

216-
if ((buffer[0] >> 4) == waitforpackettype) {
217+
uint8_t packetType = (buffer[0] >> 4);
218+
if (packetType == waitforpackettype) {
217219
return len;
218220
} else {
219-
ERROR_PRINTLN(F("Dropped a packet"));
221+
if (packetType == MQTT_CTRL_PUBLISH) {
222+
handleSubscriptionPacket(len);
223+
} else {
224+
ERROR_PRINTLN(F("Dropped a packet"));
225+
}
220226
}
221227
}
222228
return 0;
@@ -432,30 +438,21 @@ void Adafruit_MQTT::processPackets(int16_t timeout) {
432438
while (elapsed < (uint32_t)timeout) {
433439
Adafruit_MQTT_Subscribe *sub = readSubscription(timeout - elapsed);
434440
if (sub) {
435-
// Serial.println("**** sub packet received");
436441
if (sub->callback_uint32t != NULL) {
437442
// huh lets do the callback in integer mode
438443
uint32_t data = 0;
439444
data = atoi((char *)sub->lastread);
440-
// Serial.print("*** calling int callback with : ");
441-
// Serial.println(data);
442445
sub->callback_uint32t(data);
443446
} else if (sub->callback_double != NULL) {
444447
// huh lets do the callback in doublefloat mode
445448
double data = 0;
446449
data = atof((char *)sub->lastread);
447-
// Serial.print("*** calling double callback with : ");
448-
// Serial.println(data);
449450
sub->callback_double(data);
450451
} else if (sub->callback_buffer != NULL) {
451452
// huh lets do the callback in buffer mode
452-
// Serial.print("*** calling buffer callback with : ");
453-
// Serial.println((char *)sub->lastread);
454453
sub->callback_buffer((char *)sub->lastread, sub->datalen);
455454
} else if (sub->callback_io != NULL) {
456455
// huh lets do the callback in io mode
457-
// Serial.print("*** calling io instance callback with : ");
458-
// Serial.println((char *)sub->lastread);
459456
((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread,
460457
sub->datalen);
461458
}
@@ -469,23 +466,29 @@ void Adafruit_MQTT::processPackets(int16_t timeout) {
469466
elapsed += (endtime - starttime);
470467
}
471468
}
472-
473469
Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
474-
uint16_t i, topiclen, datalen;
475-
476470
// Check if data is available to read.
477471
uint16_t len =
478472
readFullPacket(buffer, MAXBUFFERSIZE, timeout); // return one full packet
479-
if (!len)
473+
return handleSubscriptionPacket(len);
474+
}
475+
476+
Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
477+
uint16_t i, topiclen, datalen;
478+
479+
if (!len) {
480480
return NULL; // No data available, just quit.
481+
}
481482
DEBUG_PRINT("Packet len: ");
482483
DEBUG_PRINTLN(len);
483484
DEBUG_PRINTBUFFER(buffer, len);
484485

485-
if (len < 3)
486+
if (len < 3) {
486487
return NULL;
487-
if ((buffer[0] & 0xF0) != (MQTT_CTRL_PUBLISH) << 4)
488+
}
489+
if ((buffer[0] & 0xF0) != (MQTT_CTRL_PUBLISH) << 4) {
488490
return NULL;
491+
}
489492

490493
// Parse out length of packet.
491494
topiclen = buffer[3];

Adafruit_MQTT.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ class Adafruit_MQTT {
206206
// messages!
207207
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout = 0);
208208

209+
// Handle any data coming in for subscriptions and fires them off to the
210+
// appropriate callback
211+
Adafruit_MQTT_Subscribe *handleSubscriptionPacket(uint16_t len);
212+
209213
void processPackets(int16_t timeout);
210214

211215
// Ping the server to ensure the connection is still alive.

Adafruit_MQTT_Client.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
5555
uint16_t len = 0;
5656
int16_t t = timeout;
5757

58+
if (maxlen == 0) { // handle zero-length packets
59+
return 0;
60+
}
61+
5862
while (client->connected() && (timeout >= 0)) {
5963
// DEBUG_PRINT('.');
6064
while (client->available()) {
@@ -65,10 +69,6 @@ uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
6569
// DEBUG_PRINTLN((uint8_t)c, HEX);
6670
len++;
6771

68-
if (maxlen == 0) { // handle zero-length packets
69-
return 0;
70-
}
71-
7272
if (len == maxlen) { // we read all we want, bail
7373
DEBUG_PRINT(F("Read data:\t"));
7474
DEBUG_PRINTBUFFER(buffer, len);

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit MQTT Library
2-
version=2.0.0
2+
version=2.1.0
33
author=Adafruit
44
maintainer=Adafruit <info@adafruit.com>
55
sentence=MQTT library that supports the FONA, ESP8266, Yun, and generic Arduino Client hardware.

0 commit comments

Comments
 (0)