Callback length field wrong for >128 byte packets

This commit is contained in:
Nicholas O'Leary 2013-01-11 17:09:39 +00:00
parent ae44d21503
commit 875061615f
2 changed files with 11 additions and 9 deletions

View File

@ -102,7 +102,8 @@ boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic,
return false; return false;
} }
} }
uint16_t len = readPacket(); uint8_t llen;
uint16_t len = readPacket(&llen);
if (len == 4 && buffer[3] == 0) { if (len == 4 && buffer[3] == 0) {
lastInActivity = millis(); lastInActivity = millis();
@ -120,7 +121,7 @@ uint8_t PubSubClient::readByte() {
return _client->read(); return _client->read();
} }
uint16_t PubSubClient::readPacket() { uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
uint16_t len = 0; uint16_t len = 0;
buffer[len++] = readByte(); buffer[len++] = readByte();
uint8_t multiplier = 1; uint8_t multiplier = 1;
@ -132,7 +133,7 @@ uint16_t PubSubClient::readPacket() {
length += (digit & 127) * multiplier; length += (digit & 127) * multiplier;
multiplier *= 128; multiplier *= 128;
} while ((digit & 128) != 0); } while ((digit & 128) != 0);
*lengthLength = len-1;
for (uint16_t i = 0;i<length;i++) for (uint16_t i = 0;i<length;i++)
{ {
if (len < MQTT_MAX_PACKET_SIZE) { if (len < MQTT_MAX_PACKET_SIZE) {
@ -163,21 +164,22 @@ boolean PubSubClient::loop() {
} }
} }
if (_client->available()) { if (_client->available()) {
uint16_t len = readPacket(); uint8_t llen;
uint16_t len = readPacket(&llen);
if (len > 0) { if (len > 0) {
lastInActivity = t; lastInActivity = t;
uint8_t type = buffer[0]&0xF0; uint8_t type = buffer[0]&0xF0;
if (type == MQTTPUBLISH) { if (type == MQTTPUBLISH) {
if (callback) { if (callback) {
uint16_t tl = (buffer[2]<<8)+buffer[3]; uint16_t tl = (buffer[llen+1]<<8)+buffer[llen+2];
char topic[tl+1]; char topic[tl+1];
for (uint16_t i=0;i<tl;i++) { for (uint16_t i=0;i<tl;i++) {
topic[i] = buffer[4+i]; topic[i] = buffer[llen+3+i];
} }
topic[tl] = 0; topic[tl] = 0;
// ignore msgID - only support QoS 0 subs // ignore msgID - only support QoS 0 subs
uint8_t *payload = buffer+4+tl; uint8_t *payload = buffer+llen+3+tl;
callback(topic,payload,len-4-tl); callback(topic,payload,len-llen-3-tl);
} }
} else if (type == MQTTPINGREQ) { } else if (type == MQTTPINGREQ) {
buffer[0] = MQTTPINGRESP; buffer[0] = MQTTPINGRESP;

View File

@ -46,7 +46,7 @@ private:
unsigned long lastInActivity; unsigned long lastInActivity;
bool pingOutstanding; bool pingOutstanding;
void (*callback)(char*,uint8_t*,unsigned int); void (*callback)(char*,uint8_t*,unsigned int);
uint16_t readPacket(); uint16_t readPacket(uint8_t*);
uint8_t readByte(); uint8_t readByte();
boolean write(uint8_t header, uint8_t* buf, uint16_t length); boolean write(uint8_t header, uint8_t* buf, uint16_t length);
uint16_t writeString(char* string, uint8_t* buf, uint16_t pos); uint16_t writeString(char* string, uint8_t* buf, uint16_t pos);