Skip to content

Commit

Permalink
Merge pull request #196 from xdylanm/send_long_packet_fix
Browse files Browse the repository at this point in the history
Fixed length calculation when payload exceeds 128bytes
  • Loading branch information
brentru authored Jul 12, 2021
2 parents d466773 + a2bdbac commit 37e175d
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Adafruit_MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,19 +756,21 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
// Calculate additional bytes for length field (if any)
uint16_t additionalLen = packetAdditionalLen(len + bLen);

// Payload length. When maxPacketLen provided is 0, let's
// Payload remaining length. When maxPacketLen provided is 0, let's
// assume buffer is big enough. Fingers crossed.
if (maxPacketLen == 0 || (len + bLen + 2 + additionalLen <= maxPacketLen)) {
len += bLen + additionalLen;
} else {
// 2 + additionalLen: header byte + remaining length field (from 1 to 4 bytes)
// len = topic size field + value (string)
// bLen = buffer size
if (!(maxPacketLen == 0 ||
(len + bLen + 2 + additionalLen <= maxPacketLen))) {
// If we make it here, we got a pickle: the payload is not going
// to fit in the packet buffer. Instead of corrupting memory, let's
// do something less damaging by reducing the bLen to what we are
// able to accomodate. Alternatively, consider using a bigger
// maxPacketLen.
bLen = maxPacketLen - (len + 2 + packetAdditionalLen(maxPacketLen));
len = maxPacketLen - 4;
}
len += bLen; // remaining len excludes header byte & length field

// Now you can start generating the packet!
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
Expand Down

0 comments on commit 37e175d

Please sign in to comment.