Skip to content
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

Change readFullPacket #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions Adafruit_MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
DEBUG_PRINTBUFFER(pbuff, rlen);
pbuff++;

uint32_t value = 0;
uint32_t remainingLength = 0;
uint32_t multiplier = 1;
uint8_t encodedByte;

Expand All @@ -256,7 +256,7 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
pbuff++; // get ready for reading the next byte
uint32_t intermediate = encodedByte & 0x7F;
intermediate *= multiplier;
value += intermediate;
remainingLength += intermediate;
multiplier *= 128;
if (multiplier > (128UL * 128UL * 128UL)) {
DEBUG_PRINT(F("Malformed packet len\n"));
Expand All @@ -265,17 +265,19 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
} while (encodedByte & 0x80);

DEBUG_PRINT(F("Packet Length:\t"));
DEBUG_PRINTLN(value);
DEBUG_PRINTLN(fixedHeaderLength + remainingLength);
brentru marked this conversation as resolved.
Show resolved Hide resolved

if (value > (maxsize - (pbuff - buffer) - 1)) {
uint16_t fixedHeaderLength = pbuff - buffer;
mpoettgen marked this conversation as resolved.
Show resolved Hide resolved

if (fixedHeaderLength + remainingLength + 1 > maxsize) {
DEBUG_PRINTLN(F("Packet too big for buffer"));
rlen = readPacket(pbuff, (maxsize - (pbuff - buffer) - 1), timeout);
rlen = readPacket(pbuff, (maxsize - fixedHeaderLength - 1), timeout);
} else {
rlen = readPacket(pbuff, value, timeout);
rlen = readPacket(pbuff, remainingLength, timeout);
}
// DEBUG_PRINT(F("Remaining packet:\t")); DEBUG_PRINTBUFFER(pbuff, rlen);

return ((pbuff - buffer) + rlen);
return (fixedHeaderLength + rlen);
}

const __FlashStringHelper *Adafruit_MQTT::connectErrorString(int8_t code) {
Expand Down