Reformatted with clang-format

This commit is contained in:
Lucas Jandrew 2020-11-06 10:20:52 -05:00
parent c952333c40
commit f3f5c18c6d
3 changed files with 53 additions and 44 deletions

View File

@ -152,10 +152,12 @@ int8_t Adafruit_MQTT::connect() {
// Read connect response packet and verify it // Read connect response packet and verify it
len = readFullPacket(buffer, MAXBUFFERSIZE, 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)) {
return -1; return -1;
}
if (buffer[3] != 0) if (buffer[3] != 0)
return buffer[3]; return buffer[3];
@ -166,7 +168,8 @@ int8_t Adafruit_MQTT::connect() {
continue; continue;
boolean success = false; boolean success = false;
for (uint8_t retry=0; (retry<3) && !success; retry++) { // retry until we get a suback for (uint8_t retry = 0; (retry < 3) && !success;
retry++) { // retry until we get a suback
// Construct and send subscription packet. // Construct and send subscription packet.
uint8_t len = subscribePacket(buffer, subscriptions[i]->topic, uint8_t len = subscribePacket(buffer, subscriptions[i]->topic,
subscriptions[i]->qos); subscriptions[i]->qos);
@ -213,13 +216,13 @@ uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer,
uint8_t packetType = (buffer[0] >> 4); uint8_t packetType = (buffer[0] >> 4);
if (packetType == waitforpackettype) { if (packetType == waitforpackettype) {
return len; return len;
} else { } else {
if (packetType == MQTT_CTRL_PUBLISH) { if (packetType == MQTT_CTRL_PUBLISH) {
handleSubscriptionPacket(len); handleSubscriptionPacket(len);
} else { } else {
ERROR_PRINTLN(F("Dropped a packet")); ERROR_PRINTLN(F("Dropped a packet"));
} }
} }
} }
return 0; return 0;
@ -261,11 +264,12 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
} }
} while (encodedByte & 0x80); } while (encodedByte & 0x80);
DEBUG_PRINT(F("Packet Length:\t")); DEBUG_PRINTLN(value); DEBUG_PRINT(F("Packet Length:\t"));
DEBUG_PRINTLN(value);
if (value > (maxsize - (pbuff-buffer) - 1)) { if (value > (maxsize - (pbuff - buffer) - 1)) {
DEBUG_PRINTLN(F("Packet too big for buffer")); DEBUG_PRINTLN(F("Packet too big for buffer"));
rlen = readPacket(pbuff, (maxsize - (pbuff-buffer) - 1), timeout); rlen = readPacket(pbuff, (maxsize - (pbuff - buffer) - 1), timeout);
} else { } else {
rlen = readPacket(pbuff, value, timeout); rlen = readPacket(pbuff, value, timeout);
} }
@ -435,22 +439,19 @@ void Adafruit_MQTT::processPackets(int16_t timeout) {
Adafruit_MQTT_Subscribe *sub = readSubscription(timeout - elapsed); Adafruit_MQTT_Subscribe *sub = readSubscription(timeout - elapsed);
if (sub) { if (sub) {
if (sub->callback_uint32t != NULL) { if (sub->callback_uint32t != NULL) {
// huh lets do the callback in integer mode // huh lets do the callback in integer mode
uint32_t data = 0; uint32_t data = 0;
data = atoi((char *)sub->lastread); data = atoi((char *)sub->lastread);
sub->callback_uint32t(data); sub->callback_uint32t(data);
} } else if (sub->callback_double != NULL) {
else if (sub->callback_double != NULL) { // huh lets do the callback in doublefloat mode
// huh lets do the callback in doublefloat mode double data = 0;
double data = 0; data = atof((char *)sub->lastread);
data = atof((char *)sub->lastread); sub->callback_double(data);
sub->callback_double(data); } else if (sub->callback_buffer != NULL) {
} // huh lets do the callback in buffer mode
else if (sub->callback_buffer != NULL) { sub->callback_buffer((char *)sub->lastread, sub->datalen);
// huh lets do the callback in buffer mode } else if (sub->callback_io != NULL) {
sub->callback_buffer((char *)sub->lastread, sub->datalen);
}
else if (sub->callback_io != NULL) {
// huh lets do the callback in io mode // huh lets do the callback in io mode
((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread, ((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread,
sub->datalen); sub->datalen);
@ -466,21 +467,28 @@ void Adafruit_MQTT::processPackets(int16_t timeout) {
} }
} }
Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) { Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
// Check if data is available to read. // Check if data is available to read.
uint16_t len = readFullPacket(buffer, MAXBUFFERSIZE, timeout); // return one full packet uint16_t len =
return handleSubscriptionPacket(len); readFullPacket(buffer, MAXBUFFERSIZE, timeout); // return one full packet
return handleSubscriptionPacket(len);
} }
Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) { Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
uint16_t i, topiclen, datalen; uint16_t i, topiclen, datalen;
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);
DEBUG_PRINTBUFFER(buffer, len); DEBUG_PRINTBUFFER(buffer, len);
if (len<3) return NULL; if (len < 3) {
if ((buffer[0] & 0xF0) != (MQTT_CTRL_PUBLISH) << 4) return NULL; return NULL;
}
if ((buffer[0] & 0xF0) != (MQTT_CTRL_PUBLISH) << 4) {
return NULL;
}
// Parse out length of packet. // Parse out length of packet.
topiclen = buffer[3]; topiclen = buffer[3];
@ -671,7 +679,7 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
// calc length of non-header data // calc length of non-header data
len += 2; // two bytes to set the topic size len += 2; // two bytes to set the topic size
len += strlen(topic); // topic length len += strlen(topic); // topic length
if(qos > 0) { if (qos > 0) {
len += 2; // qos packet id len += 2; // qos packet id
} }
len += bLen; // payload length len += bLen; // payload length

View File

@ -206,7 +206,8 @@ public:
// messages! // messages!
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout = 0); Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout = 0);
// Handle any data coming in for subscriptions and fires them off to the appropriate callback // Handle any data coming in for subscriptions and fires them off to the
// appropriate callback
Adafruit_MQTT_Subscribe *handleSubscriptionPacket(uint16_t len); Adafruit_MQTT_Subscribe *handleSubscriptionPacket(uint16_t len);
void processPackets(int16_t timeout); void processPackets(int16_t timeout);

View File

@ -55,9 +55,9 @@ uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
uint16_t len = 0; uint16_t len = 0;
int16_t t = timeout; int16_t t = timeout;
if (maxlen == 0) { // handle zero-length packets if (maxlen == 0) { // handle zero-length packets
return 0; return 0;
} }
while (client->connected() && (timeout >= 0)) { while (client->connected() && (timeout >= 0)) {
// DEBUG_PRINT('.'); // DEBUG_PRINT('.');
@ -69,7 +69,7 @@ uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
// DEBUG_PRINTLN((uint8_t)c, HEX); // DEBUG_PRINTLN((uint8_t)c, HEX);
len++; len++;
if (len == maxlen) { // we read all we want, bail if (len == maxlen) { // we read all we want, bail
DEBUG_PRINT(F("Read data:\t")); DEBUG_PRINT(F("Read data:\t"));
DEBUG_PRINTBUFFER(buffer, len); DEBUG_PRINTBUFFER(buffer, len);
return len; return len;