Removing remainder of whitespace differences

This commit is contained in:
Lucas Jandrew 2020-02-24 01:28:48 -05:00
parent 3ba30fe64b
commit adb717f7dd

View File

@ -48,15 +48,13 @@ int strncasecmp(const char * str1, const char * str2, int len) {
void printBuffer(uint8_t *buffer, uint16_t len) {
DEBUG_PRINTER.print('\t');
for (uint16_t i=0; i<len; i++) {
if (isprint(buffer[i])) {
if (isprint(buffer[i]))
DEBUG_PRINTER.write(buffer[i]);
} else {
else
DEBUG_PRINTER.print(" ");
}
DEBUG_PRINTER.print(F(" [0x"));
if (buffer[i] < 0x10) {
if (buffer[i] < 0x10)
DEBUG_PRINTER.print("0");
}
DEBUG_PRINTER.print(buffer[i],HEX);
DEBUG_PRINTER.print("], ");
if (i % 8 == 7) {
@ -89,10 +87,8 @@ static uint8_t *stringprint(uint8_t *p, const char *s, uint16_t maxlen = 0) {
Serial.write(pgm_read_byte(s+i));
}
*/
p[0] = len >> 8;
p++;
p[0] = len & 0xFF;
p++;
p[0] = len >> 8; p++;
p[0] = len & 0xFF; p++;
strncpy((char *)p, s, len);
return p+len;
}
@ -152,21 +148,18 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server,
int8_t Adafruit_MQTT::connect() {
// Connect to the server.
if (!connectServer()) {
if (!connectServer())
return -1;
}
// Construct and send connect packet.
uint8_t len = connectPacket(buffer);
if (!sendPacket(buffer, len)) {
if (!sendPacket(buffer, len))
return -1;
}
// Read connect response packet and verify it
len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
if (len != 4) {
if (len != 4)
return -1;
}
if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2))
return -1;
if (buffer[3] != 0)
@ -175,21 +168,17 @@ int8_t Adafruit_MQTT::connect() {
// Setup subscriptions once connected.
for (uint8_t i=0; i<MAXSUBSCRIPTIONS; i++) {
// Ignore subscriptions that aren't defined.
if (subscriptions[i] == 0) {
continue;
}
if (subscriptions[i] == 0) continue;
boolean success = false;
for (uint8_t retry=0; (retry<3) && !success; retry++) { // retry until we get a suback
// Construct and send subscription packet.
uint8_t len = subscribePacket(buffer, subscriptions[i]->topic, subscriptions[i]->qos);
if (!sendPacket(buffer, len)) {
if (!sendPacket(buffer, len))
return -1;
}
if (MQTT_PROTOCOL_LEVEL < 3) { // older versions didn't suback
if(MQTT_PROTOCOL_LEVEL < 3) // older versions didn't suback
break;
}
// Check for SUBACK if using MQTT 3.1.1 or higher
// TODO: The Server is permitted to start sending PUBLISH packets matching the
@ -207,7 +196,8 @@ int8_t Adafruit_MQTT::connect() {
return 0;
}
int8_t Adafruit_MQTT::connect(const char *user, const char *pass) {
int8_t Adafruit_MQTT::connect(const char *user, const char *pass)
{
username = user;
password = pass;
return connect();
@ -247,8 +237,7 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16
rlen = readPacket(pbuff, 1, timeout);
if (rlen != 1) return 0;
DEBUG_PRINT(F("Packet Type:\t"));
DEBUG_PRINTBUFFER(pbuff, rlen);
DEBUG_PRINT(F("Packet Type:\t")); DEBUG_PRINTBUFFER(pbuff, rlen);
pbuff++;
uint32_t value = 0;
@ -270,8 +259,7 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16
}
} 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)) {
DEBUG_PRINTLN(F("Packet too big for buffer"));
@ -286,27 +274,16 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16
const __FlashStringHelper* Adafruit_MQTT::connectErrorString(int8_t code) {
switch (code) {
case 1:
return F("The Server does not support the level of the MQTT protocol requested");
case 2:
return F("The Client identifier is correct UTF-8 but not allowed by the Server");
case 3:
return F("The MQTT service is unavailable");
case 4:
return F("The data in the user name or password is malformed");
case 5:
return F("Not authorized to connect");
case 6:
return F("Exceeded reconnect rate limit. Please try again later.");
case 7:
return F(
"You have been banned from connecting. Please contact the MQTT server administrator for more details.");
case -1:
return F("Connection failed");
case -2:
return F("Failed to subscribe");
default:
return F("Unknown error");
case 1: return F("The Server does not support the level of the MQTT protocol requested");
case 2: return F("The Client identifier is correct UTF-8 but not allowed by the Server");
case 3: return F("The MQTT service is unavailable");
case 4: return F("The data in the user name or password is malformed");
case 5: return F("Not authorized to connect");
case 6: return F("Exceeded reconnect rate limit. Please try again later.");
case 7: return F("You have been banned from connecting. Please contact the MQTT server administrator for more details.");
case -1: return F("Connection failed");
case -2: return F("Failed to subscribe");
default: return F("Unknown error");
}
}
@ -314,20 +291,21 @@ bool Adafruit_MQTT::disconnect() {
// Construct and send disconnect packet.
uint8_t len = disconnectPacket(buffer);
if (!sendPacket(buffer, len)) DEBUG_PRINTLN(F("Unable to send disconnect packet"));
if (! sendPacket(buffer, len))
DEBUG_PRINTLN(F("Unable to send disconnect packet"));
return disconnectServer();
}
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos, bool retain) {
return publish(topic, (uint8_t *) (data), strlen(data), qos, retain);
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
return publish(topic, (uint8_t*)(data), strlen(data), qos);
}
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos, bool retain) {
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos) {
// Construct and send publish packet.
uint16_t len = publishPacket(buffer, topic, data, bLen, qos, retain);
uint16_t len = publishPacket(buffer, topic, data, bLen, qos);
if (!sendPacket(buffer, len))
return false;
@ -381,8 +359,7 @@ bool Adafruit_MQTT::subscribe(Adafruit_MQTT_Subscribe *sub) {
if (i==MAXSUBSCRIPTIONS) { // add to subscriptionlist
for (i=0; i<MAXSUBSCRIPTIONS; i++) {
if (subscriptions[i] == 0) {
DEBUG_PRINT(F("Added sub "));
DEBUG_PRINTLN(i);
DEBUG_PRINT(F("Added sub ")); DEBUG_PRINTLN(i);
subscriptions[i] = sub;
return true;
}
@ -449,17 +426,20 @@ void Adafruit_MQTT::processPackets(int16_t timeout) {
data = atoi((char *)sub->lastread);
//Serial.print("*** calling int callback with : "); Serial.println(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
double data = 0;
data = atof((char *)sub->lastread);
//Serial.print("*** calling double callback with : "); Serial.println(data);
sub->callback_double(data);
} else if (sub->callback_buffer != NULL) {
}
else if (sub->callback_buffer != NULL) {
// huh lets do the callback in buffer mode
//Serial.print("*** calling buffer callback with : "); Serial.println((char *)sub->lastread);
sub->callback_buffer((char *)sub->lastread, sub->datalen);
} else if (sub->callback_io != NULL) {
}
else if (sub->callback_io != NULL) {
// huh lets do the callback in io mode
//Serial.print("*** calling io instance callback with : "); Serial.println((char *)sub->lastread);
((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread, sub->datalen);
@ -476,32 +456,21 @@ void Adafruit_MQTT::processPackets(int16_t timeout) {
}
Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
// Check if data is available to read.
uint16_t len = readFullPacket(buffer, MAXBUFFERSIZE, timeout); // return one full packet
return handleSubscriptionPacket(len);
}
Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
uint16_t i, topiclen, datalen;
if (!len) {
// Check if data is available to read.
uint16_t len = readFullPacket(buffer, MAXBUFFERSIZE, timeout); // return one full packet
if (!len)
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);
if (len < 3) {
return NULL;
}
if ((buffer[0] & 0xF0) != (MQTT_CTRL_PUBLISH) << 4) {
return NULL;
}
if (len<3) return NULL;
if ((buffer[0] & 0xF0) != (MQTT_CTRL_PUBLISH) << 4) return NULL;
// Parse out length of packet.
topiclen = buffer[3];
DEBUG_PRINT(F("Looking for subscription len "));
DEBUG_PRINTLN(topiclen);
DEBUG_PRINT(F("Looking for subscription len ")); DEBUG_PRINTLN(topiclen);
// Find subscription associated with this packet.
for (i=0; i<MAXSUBSCRIPTIONS; i++) {
@ -513,15 +482,12 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
// Stop if the subscription topic matches the received topic. Be careful
// to make comparison case insensitive.
if (strncasecmp((char*)buffer+4, subscriptions[i]->topic, topiclen) == 0) {
DEBUG_PRINT(F("Found sub #"));
DEBUG_PRINTLN(i);
DEBUG_PRINT(F("Found sub #")); DEBUG_PRINTLN(i);
break;
}
}
}
if (i == MAXSUBSCRIPTIONS) {
return NULL; // matching sub not found ???
}
if (i==MAXSUBSCRIPTIONS) return NULL; // matching sub not found ???
uint8_t packet_id_len = 0;
uint16_t packetid = 0;
@ -543,17 +509,16 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
// extract out just the data, into the subscription object itself
memmove(subscriptions[i]->lastread, buffer+4+topiclen+packet_id_len, datalen);
subscriptions[i]->datalen = datalen;
DEBUG_PRINT(F("Data len: "));
DEBUG_PRINTLN(datalen);
DEBUG_PRINT(F("Data: "));
DEBUG_PRINTLN((char *) subscriptions[i]->lastread);
DEBUG_PRINT(F("Data len: ")); DEBUG_PRINTLN(datalen);
DEBUG_PRINT(F("Data: ")); DEBUG_PRINTLN((char *)subscriptions[i]->lastread);
if ((MQTT_PROTOCOL_LEVEL > 3) &&(buffer[0] & 0x6) == 0x2) {
uint8_t ackpacket[4];
// Construct and send puback packet.
uint8_t len = pubackPacket(ackpacket, packetid);
if (!sendPacket(ackpacket, len)) DEBUG_PRINT(F("Failed"));
if (!sendPacket(ackpacket, len))
DEBUG_PRINT(F("Failed"));
}
// return the valid matching subscription
@ -677,7 +642,7 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
// as per http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
uint8_t *data, uint16_t bLen, uint8_t qos, bool retain) {
uint8_t *data, uint16_t bLen, uint8_t qos) {
uint8_t *p = packet;
uint16_t len=0;
@ -690,7 +655,7 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
len += bLen; // payload length
// Now you can start generating the packet!
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1 | retain;
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
p++;
// fill in packet[1] last
@ -756,6 +721,7 @@ uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
}
uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
uint8_t *p = packet;
@ -812,13 +778,11 @@ uint8_t Adafruit_MQTT::disconnectPacket(uint8_t *packet) {
// Adafruit_MQTT_Publish Definition ////////////////////////////////////////////
Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,
const char *feed, uint8_t
q) {
const char *feed, uint8_t q) {
mqtt = mqttserver;
topic = feed;
qos = q;
}
bool Adafruit_MQTT_Publish::publish(int32_t i) {
char payload[12];
ltoa(i, payload, 10);
@ -842,8 +806,7 @@ bool Adafruit_MQTT_Publish::publish(const char *payload) {
}
//publish buffer of arbitrary length
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t
bLen) {
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) {
return mqtt->publish(topic, payload, bLen, qos);
}
@ -852,8 +815,7 @@ bLen) {
// Adafruit_MQTT_Subscribe Definition //////////////////////////////////////////
Adafruit_MQTT_Subscribe::Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver,
const char *feed, uint8_t
q) {
const char *feed, uint8_t q) {
mqtt = mqttserver;
topic = feed;
qos = q;