Fixed length calculation when payload exceeds 128bytes

This commit is contained in:
xdylanm 2021-05-30 14:13:57 -07:00
parent e524b5f3ae
commit b007806ac1

View File

@ -728,19 +728,20 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
// Calculate additional bytes for length field (if any) // Calculate additional bytes for length field (if any)
uint16_t additionalLen = packetAdditionalLen(len + bLen); 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. // assume buffer is big enough. Fingers crossed.
if (maxPacketLen == 0 || (len + bLen + 2 + additionalLen <= maxPacketLen)) { // 2 + additionalLen: header byte + remaining length field (from 1 to 4 bytes)
len += bLen + additionalLen; // len = topic size field + value (string)
} else { // 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 // 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 // to fit in the packet buffer. Instead of corrupting memory, let's
// do something less damaging by reducing the bLen to what we are // do something less damaging by reducing the bLen to what we are
// able to accomodate. Alternatively, consider using a bigger // able to accomodate. Alternatively, consider using a bigger
// maxPacketLen. // maxPacketLen.
bLen = maxPacketLen - (len + 2 + packetAdditionalLen(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! // Now you can start generating the packet!
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1; p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;