Skip to content

Commit db003d3

Browse files
author
xdylanm
committed
Addressing comments from PR #193 with merge from PR#166. Clang-format, version ticked to 2.3.0, replaced duplicate topicOffsetFromLength with packetAdditionalLen.
1 parent c782cc4 commit db003d3

File tree

3 files changed

+26
-40
lines changed

3 files changed

+26
-40
lines changed

Adafruit_MQTT.cpp

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ static uint8_t *stringprint(uint8_t *p, const char *s, uint16_t maxlen = 0) {
9696
return p + len;
9797
}
9898

99+
// packetAdditionalLen is a helper function used to figure out
100+
// how bigger the payload needs to be in order to account for
101+
// its variable length field. As per
102+
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.4_Size
103+
// See also readFullPacket
104+
static uint16_t packetAdditionalLen(uint32_t currLen) {
105+
/* Increase length field based on current length */
106+
if (currLen < 128) // 7-bits
107+
return 0;
108+
if (currLen < 16384) // 14-bits
109+
return 1;
110+
if (currLen < 2097152) // 21-bits
111+
return 2;
112+
return 3;
113+
}
114+
99115
// Adafruit_MQTT Definition ////////////////////////////////////////////////////
100116

101117
Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
@@ -267,7 +283,8 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
267283
DEBUG_PRINT(F("Packet Length:\t"));
268284
DEBUG_PRINTLN(value);
269285

270-
if (value > (maxsize - (pbuff - buffer) - 1)) {
286+
// maxsize is limited to 65536 by 16-bit unsigned
287+
if (value > uint32_t(maxsize - (pbuff - buffer) - 1)) {
271288
DEBUG_PRINTLN(F("Packet too big for buffer"));
272289
rlen = readPacket(pbuff, (maxsize - (pbuff - buffer) - 1), timeout);
273290
} else {
@@ -474,22 +491,6 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
474491
return handleSubscriptionPacket(len);
475492
}
476493

477-
namespace {
478-
479-
uint16_t topicOffsetFromLength(uint16_t const len)
480-
{
481-
if (len < 128) { // 7 bits (+1 continuation bit)
482-
return 0;
483-
} else if (len < 16384) { // 14 bits (+2 continuation bits)
484-
return 1;
485-
} else if (len < 2097152) { // 21 bits
486-
return 2;
487-
}
488-
return 3;
489-
}
490-
491-
} // anon
492-
493494
Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
494495
uint16_t i, topiclen, datalen;
495496

@@ -508,9 +509,9 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
508509
}
509510

510511
// Parse out length of packet.
511-
uint16_t const topicoffset = topicOffsetFromLength(len);
512+
uint16_t const topicoffset = packetAdditionalLen(len);
512513
uint16_t const topicstart = topicoffset + 4;
513-
topiclen = buffer[3+topicoffset];
514+
topiclen = buffer[3 + topicoffset];
514515
DEBUG_PRINT(F("Looking for subscription len "));
515516
DEBUG_PRINTLN(topiclen);
516517

@@ -523,8 +524,8 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
523524
continue;
524525
// Stop if the subscription topic matches the received topic. Be careful
525526
// to make comparison case insensitive.
526-
if (strncasecmp((char *)buffer + topicstart, subscriptions[i]->topic, topiclen) ==
527-
0) {
527+
if (strncasecmp((char *)buffer + topicstart, subscriptions[i]->topic,
528+
topiclen) == 0) {
528529
DEBUG_PRINT(F("Found sub #"));
529530
DEBUG_PRINTLN(i);
530531
break;
@@ -552,8 +553,8 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
552553
datalen = SUBSCRIPTIONDATALEN - 1; // cut it off
553554
}
554555
// extract out just the data, into the subscription object itself
555-
memmove(subscriptions[i]->lastread, buffer + topicstart + topiclen + packet_id_len,
556-
datalen);
556+
memmove(subscriptions[i]->lastread,
557+
buffer + topicstart + topiclen + packet_id_len, datalen);
557558
subscriptions[i]->datalen = datalen;
558559
DEBUG_PRINT(F("Data len: "));
559560
DEBUG_PRINTLN(datalen);
@@ -687,21 +688,6 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
687688
return len;
688689
}
689690

690-
// packetAdditionalLen is a helper function used to figure out
691-
// how bigger the payload needs to be in order to account for
692-
// its variable length field. As per
693-
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.4_Size
694-
static uint16_t packetAdditionalLen(uint16_t currLen) {
695-
/* Increase length field based on current length */
696-
if (currLen < 128)
697-
return 0;
698-
if (currLen < 16384)
699-
return 1;
700-
if (currLen < 2097151)
701-
return 2;
702-
return 3;
703-
}
704-
705691
// as per
706692
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
707693
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,

Adafruit_MQTT_Client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len) {
9595
DEBUG_PRINTLN(ret);
9696
len -= ret;
9797
offset += ret;
98-
98+
9999
if (ret != sendlen) {
100100
DEBUG_PRINTLN("Failed to send packet.");
101101
return false;

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.2.0
2+
version=2.3.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)