max receivable MQTT packet is 16 bits (tested with 300 byte packet receive)

also fixd buffer printer to have 16 bit length
This commit is contained in:
ladyada 2016-07-06 17:20:02 -04:00
parent 121aa3865c
commit b26b4eb32c
6 changed files with 22 additions and 17 deletions

View File

@ -30,8 +30,8 @@ static char *dtostrf (double val, signed char width, unsigned char prec, char *s
} }
#endif #endif
void printBuffer(uint8_t *buffer, uint8_t len) { void printBuffer(uint8_t *buffer, uint16_t len) {
for (uint8_t i=0; i<len; i++) { for (uint16_t i=0; i<len; i++) {
if (isprint(buffer[i])) if (isprint(buffer[i]))
DEBUG_PRINTER.write(buffer[i]); DEBUG_PRINTER.write(buffer[i]);
else else
@ -189,7 +189,7 @@ int8_t Adafruit_MQTT::connect() {
return -1; return -1;
// Read connect response packet and verify it // Read connect response packet and verify it
len = readFullPacket(buffer, CONNECT_TIMEOUT_MS); len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
if (len != 4) if (len != 4)
return -1; return -1;
if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2)) if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2))
@ -230,7 +230,7 @@ int8_t Adafruit_MQTT::connect() {
uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout) { uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout) {
uint16_t len; uint16_t len;
while (len = readFullPacket(buffer, timeout)) { while (len = readFullPacket(buffer, MAXBUFFERSIZE, timeout)) {
//DEBUG_PRINT("Packet read size: "); DEBUG_PRINTLN(len); //DEBUG_PRINT("Packet read size: "); DEBUG_PRINTLN(len);
// TODO: add subscription reading & call back processing here // TODO: add subscription reading & call back processing here
@ -243,7 +243,7 @@ uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpack
return 0; return 0;
} }
uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t timeout) { uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16_t timeout) {
// will read a packet and Do The Right Thing with length // will read a packet and Do The Right Thing with length
uint8_t *pbuff = buffer; uint8_t *pbuff = buffer;
@ -277,7 +277,12 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t timeout) {
DEBUG_PRINT(F("Packet Length:\t")); DEBUG_PRINTLN(value); DEBUG_PRINT(F("Packet Length:\t")); DEBUG_PRINTLN(value);
if (value > (maxsize - (pbuff-buffer) - 1)) {
DEBUG_PRINTLN(F("Packet too big for buffer"));
rlen = readPacket(pbuff, (maxsize - (pbuff-buffer) - 1), timeout);
} else {
rlen = readPacket(pbuff, value, timeout); rlen = readPacket(pbuff, value, timeout);
}
//DEBUG_PRINT(F("Remaining packet:\t")); DEBUG_PRINTBUFFER(pbuff, rlen); //DEBUG_PRINT(F("Remaining packet:\t")); DEBUG_PRINTBUFFER(pbuff, rlen);
return ((pbuff - buffer)+rlen); return ((pbuff - buffer)+rlen);
@ -322,7 +327,7 @@ bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint8_t bLen, uint
// If QOS level is high enough verify the response packet. // If QOS level is high enough verify the response packet.
if (qos > 0) { if (qos > 0) {
len = readFullPacket(buffer, PUBLISH_TIMEOUT_MS); len = readFullPacket(buffer, MAXBUFFERSIZE, PUBLISH_TIMEOUT_MS);
DEBUG_PRINT(F("Publish QOS1+ reply:\t")); DEBUG_PRINT(F("Publish QOS1+ reply:\t"));
DEBUG_PRINTBUFFER(buffer, len); DEBUG_PRINTBUFFER(buffer, len);
if (len != 4) if (len != 4)
@ -403,7 +408,7 @@ bool Adafruit_MQTT::unsubscribe(Adafruit_MQTT_Subscribe *sub) {
if(subscriptions[i]->qos > 0 && MQTT_PROTOCOL_LEVEL > 3) { if(subscriptions[i]->qos > 0 && MQTT_PROTOCOL_LEVEL > 3) {
// wait for UNSUBACK // wait for UNSUBACK
len = readFullPacket(buffer, CONNECT_TIMEOUT_MS); len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
DEBUG_PRINT(F("UNSUBACK:\t")); DEBUG_PRINT(F("UNSUBACK:\t"));
DEBUG_PRINTBUFFER(buffer, len); DEBUG_PRINTBUFFER(buffer, len);
@ -427,7 +432,7 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
uint8_t i, topiclen, datalen; uint8_t i, topiclen, datalen;
// Check if data is available to read. // Check if data is available to read.
uint16_t len = readFullPacket(buffer, timeout); // return one full packet uint16_t len = readFullPacket(buffer, MAXBUFFERSIZE, timeout); // return one full packet
if (!len) if (!len)
return NULL; // No data available, just quit. return NULL; // No data available, just quit.
DEBUG_PRINT("Packet len: "); DEBUG_PRINTLN(len); DEBUG_PRINT("Packet len: "); DEBUG_PRINTLN(len);

View File

@ -78,7 +78,7 @@
// Largest full packet we're able to send. // Largest full packet we're able to send.
// Need to be able to store at least ~90 chars for a connect packet with full // Need to be able to store at least ~90 chars for a connect packet with full
// 23 char client ID. // 23 char client ID.
#define MAXBUFFERSIZE (125) #define MAXBUFFERSIZE (150)
#define MQTT_CONN_USERNAMEFLAG 0x80 #define MQTT_CONN_USERNAMEFLAG 0x80
#define MQTT_CONN_PASSWORDFLAG 0x40 #define MQTT_CONN_PASSWORDFLAG 0x40
@ -99,7 +99,7 @@
//and returns an int //and returns an int
typedef void (*SubscribeCallbackType)(char *); typedef void (*SubscribeCallbackType)(char *);
extern void printBuffer(uint8_t *buffer, uint8_t len); extern void printBuffer(uint8_t *buffer, uint16_t len);
class Adafruit_MQTT_Subscribe; // forward decl class Adafruit_MQTT_Subscribe; // forward decl
@ -201,10 +201,10 @@ class Adafruit_MQTT {
// Read MQTT packet from the server. Will read up to maxlen bytes and store // Read MQTT packet from the server. Will read up to maxlen bytes and store
// the data in the provided buffer. Waits up to the specified timeout (in // the data in the provided buffer. Waits up to the specified timeout (in
// milliseconds) for data to be available. // milliseconds) for data to be available.
virtual uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout) = 0; virtual uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout) = 0;
// Read a full packet, keeping note of the correct length // Read a full packet, keeping note of the correct length
uint16_t readFullPacket(uint8_t *buffer, uint16_t timeout); uint16_t readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16_t timeout);
// Properly process packets until you get to one you want // Properly process packets until you get to one you want
uint16_t processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout); uint16_t processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout);

View File

@ -100,7 +100,7 @@ class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
return mqttclient.connected(); return mqttclient.connected();
} }
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout) { uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout) {
/* Read data until either the connection is closed, or the idle timeout is reached. */ /* Read data until either the connection is closed, or the idle timeout is reached. */
uint16_t len = 0; uint16_t len = 0;
int16_t t = timeout; int16_t t = timeout;

View File

@ -47,7 +47,7 @@ bool Adafruit_MQTT_Client::connected() {
return client->connected(); return client->connected();
} }
uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint8_t maxlen, uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
int16_t timeout) { int16_t timeout) {
/* Read data until either the connection is closed, or the idle timeout is reached. */ /* Read data until either the connection is closed, or the idle timeout is reached. */
uint16_t len = 0; uint16_t len = 0;

View File

@ -50,7 +50,7 @@ class Adafruit_MQTT_Client : public Adafruit_MQTT {
bool connectServer(); bool connectServer();
bool disconnectServer(); bool disconnectServer();
bool connected(); bool connected();
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout); uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout);
bool sendPacket(uint8_t *buffer, uint8_t len); bool sendPacket(uint8_t *buffer, uint8_t len);
private: private:

View File

@ -68,7 +68,7 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT {
return fona->TCPconnected(); return fona->TCPconnected();
} }
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout) { uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout) {
uint8_t *buffp = buffer; uint8_t *buffp = buffer;
DEBUG_PRINTLN(F("Reading data..")); DEBUG_PRINTLN(F("Reading data.."));