-
Notifications
You must be signed in to change notification settings - Fork 293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support publishing and receiving large messages. #193
Changes from all commits
2ea8a0b
93a35d1
c782cc4
db003d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,22 @@ static uint8_t *stringprint(uint8_t *p, const char *s, uint16_t maxlen = 0) { | |
return p + len; | ||
} | ||
|
||
// packetAdditionalLen is a helper function used to figure out | ||
// how bigger the payload needs to be in order to account for | ||
// its variable length field. As per | ||
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.4_Size | ||
// See also readFullPacket | ||
static uint16_t packetAdditionalLen(uint32_t currLen) { | ||
/* Increase length field based on current length */ | ||
if (currLen < 128) // 7-bits | ||
return 0; | ||
if (currLen < 16384) // 14-bits | ||
return 1; | ||
if (currLen < 2097152) // 21-bits | ||
return 2; | ||
return 3; | ||
} | ||
|
||
// Adafruit_MQTT Definition //////////////////////////////////////////////////// | ||
|
||
Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid, | ||
|
@@ -233,7 +249,7 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, | |
// will read a packet and Do The Right Thing with length | ||
uint8_t *pbuff = buffer; | ||
|
||
uint8_t rlen; | ||
uint16_t rlen; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Packets larger than 255 bytes would overflow rlen. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does solve an issue I was having today with a very large binary payload, thank you.
@flavio-fernandes Is this implemented in #166? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hi @brentru . No, this fix is related but not exactly what I was trying to address in #166 . The #166 was done to avoid corrupting memory should we attempt to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @flavio-fernandes in my own build, I originally had a simpler version of the length test That said, I realized that this solution does not support packets > 65535 bytes since the lengths are all stored in uint16_t, and I'm still not sure what the right solution is there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood. Yes, you correctly point out the fact that we cannot go beyond 65535 since we are using uint16. I will defer to @brentru on how we want to proceed here, but my opinion is to go ahead with PR/166 as is, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sounds OK and I agree. - @xdylanm, see the comment below about using clang-format so we can approve this PR. |
||
|
||
// read the packet type: | ||
rlen = readPacket(pbuff, 1, timeout); | ||
|
@@ -267,7 +283,8 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, | |
DEBUG_PRINT(F("Packet Length:\t")); | ||
DEBUG_PRINTLN(value); | ||
|
||
if (value > (maxsize - (pbuff - buffer) - 1)) { | ||
// maxsize is limited to 65536 by 16-bit unsigned | ||
if (value > uint32_t(maxsize - (pbuff - buffer) - 1)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
DEBUG_PRINTLN(F("Packet too big for buffer")); | ||
rlen = readPacket(pbuff, (maxsize - (pbuff - buffer) - 1), timeout); | ||
} else { | ||
|
@@ -492,7 +509,9 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) { | |
} | ||
|
||
// Parse out length of packet. | ||
topiclen = buffer[3]; | ||
uint16_t const topicoffset = packetAdditionalLen(len); | ||
uint16_t const topicstart = topicoffset + 4; | ||
topiclen = buffer[3 + topicoffset]; | ||
DEBUG_PRINT(F("Looking for subscription len ")); | ||
DEBUG_PRINTLN(topiclen); | ||
|
||
|
@@ -505,8 +524,8 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) { | |
continue; | ||
// Stop if the subscription topic matches the received topic. Be careful | ||
// to make comparison case insensitive. | ||
if (strncasecmp((char *)buffer + 4, subscriptions[i]->topic, topiclen) == | ||
0) { | ||
if (strncasecmp((char *)buffer + topicstart, subscriptions[i]->topic, | ||
topiclen) == 0) { | ||
DEBUG_PRINT(F("Found sub #")); | ||
DEBUG_PRINTLN(i); | ||
break; | ||
|
@@ -521,21 +540,21 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) { | |
// Check if it is QoS 1, TODO: we dont support QoS 2 | ||
if ((buffer[0] & 0x6) == 0x2) { | ||
packet_id_len = 2; | ||
packetid = buffer[topiclen + 4]; | ||
packetid = buffer[topiclen + topicstart]; | ||
packetid <<= 8; | ||
packetid |= buffer[topiclen + 5]; | ||
packetid |= buffer[topiclen + topicstart + 1]; | ||
} | ||
|
||
// zero out the old data | ||
memset(subscriptions[i]->lastread, 0, SUBSCRIPTIONDATALEN); | ||
|
||
datalen = len - topiclen - packet_id_len - 4; | ||
datalen = len - topiclen - packet_id_len - topicstart; | ||
if (datalen > SUBSCRIPTIONDATALEN) { | ||
datalen = SUBSCRIPTIONDATALEN - 1; // cut it off | ||
} | ||
// extract out just the data, into the subscription object itself | ||
memmove(subscriptions[i]->lastread, buffer + 4 + topiclen + packet_id_len, | ||
datalen); | ||
memmove(subscriptions[i]->lastread, | ||
buffer + topicstart + topiclen + packet_id_len, datalen); | ||
subscriptions[i]->datalen = datalen; | ||
DEBUG_PRINT(F("Data len: ")); | ||
DEBUG_PRINTLN(datalen); | ||
|
@@ -669,21 +688,6 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) { | |
return len; | ||
} | ||
|
||
// packetAdditionalLen is a helper function used to figure out | ||
// how bigger the payload needs to be in order to account for | ||
// its variable length field. As per | ||
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.4_Size | ||
static uint16_t packetAdditionalLen(uint16_t currLen) { | ||
/* Increase length field based on current length */ | ||
if (currLen < 128) | ||
return 0; | ||
if (currLen < 16384) | ||
return 1; | ||
if (currLen < 2097151) | ||
return 2; | ||
return 3; | ||
} | ||
|
||
// as per | ||
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040 | ||
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name=Adafruit MQTT Library | ||
version=2.2.0 | ||
version=2.3.0 | ||
author=Adafruit | ||
maintainer=Adafruit <[email protected]> | ||
sentence=MQTT library that supports the FONA, ESP8266, Yun, and generic Arduino Client hardware. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the signature here to explicitly take a uint32_t so that the comparisons are consistent. This doesn't change the fact that the max packet length is limited to 65536, but that is checked in readFullPacket