Commit 2b8aa199 authored by Eric Duminil's avatar Eric Duminil
Browse files

Trying to remove warnings

parent 88944cef
......@@ -3,7 +3,7 @@
PubSubClient.cpp - A simple client for MQTT.
Nick O'Leary
http://knolleary.net
*/
*/
#include "PubSubClient.h"
#include "Arduino.h"
......@@ -19,7 +19,7 @@ PubSubClient::PubSubClient() {
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(Client& client) {
PubSubClient::PubSubClient(Client &client) {
this->_state = MQTT_DISCONNECTED;
setClient(client);
this->stream = NULL;
......@@ -29,7 +29,7 @@ PubSubClient::PubSubClient(Client& client) {
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client) {
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client &client) {
this->_state = MQTT_DISCONNECTED;
setServer(addr, port);
setClient(client);
......@@ -39,9 +39,9 @@ PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client) {
setKeepAlive(MQTT_KEEPALIVE);
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream) {
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client &client, Stream &stream) {
this->_state = MQTT_DISCONNECTED;
setServer(addr,port);
setServer(addr, port);
setClient(client);
setStream(stream);
this->bufferSize = 0;
......@@ -49,7 +49,7 @@ PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream
setKeepAlive(MQTT_KEEPALIVE);
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client) {
this->_state = MQTT_DISCONNECTED;
setServer(addr, port);
setCallback(callback);
......@@ -60,9 +60,9 @@ PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATUR
setKeepAlive(MQTT_KEEPALIVE);
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client, Stream &stream) {
this->_state = MQTT_DISCONNECTED;
setServer(addr,port);
setServer(addr, port);
setCallback(callback);
setClient(client);
setStream(stream);
......@@ -72,7 +72,7 @@ PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATUR
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client) {
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client &client) {
this->_state = MQTT_DISCONNECTED;
setServer(ip, port);
setClient(client);
......@@ -82,9 +82,9 @@ PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client) {
setKeepAlive(MQTT_KEEPALIVE);
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client, Stream& stream) {
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client &client, Stream &stream) {
this->_state = MQTT_DISCONNECTED;
setServer(ip,port);
setServer(ip, port);
setClient(client);
setStream(stream);
this->bufferSize = 0;
......@@ -92,7 +92,7 @@ PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client, Stream& s
setKeepAlive(MQTT_KEEPALIVE);
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client) {
this->_state = MQTT_DISCONNECTED;
setServer(ip, port);
setCallback(callback);
......@@ -103,9 +103,9 @@ PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE,
setKeepAlive(MQTT_KEEPALIVE);
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client, Stream &stream) {
this->_state = MQTT_DISCONNECTED;
setServer(ip,port);
setServer(ip, port);
setCallback(callback);
setClient(client);
setStream(stream);
......@@ -115,9 +115,9 @@ PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE,
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client) {
PubSubClient::PubSubClient(const char *domain, uint16_t port, Client &client) {
this->_state = MQTT_DISCONNECTED;
setServer(domain,port);
setServer(domain, port);
setClient(client);
this->stream = NULL;
this->bufferSize = 0;
......@@ -125,9 +125,9 @@ PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client) {
setKeepAlive(MQTT_KEEPALIVE);
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client, Stream& stream) {
PubSubClient::PubSubClient(const char *domain, uint16_t port, Client &client, Stream &stream) {
this->_state = MQTT_DISCONNECTED;
setServer(domain,port);
setServer(domain, port);
setClient(client);
setStream(stream);
this->bufferSize = 0;
......@@ -135,9 +135,9 @@ PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client, St
setKeepAlive(MQTT_KEEPALIVE);
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
PubSubClient::PubSubClient(const char *domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client) {
this->_state = MQTT_DISCONNECTED;
setServer(domain,port);
setServer(domain, port);
setCallback(callback);
setClient(client);
this->stream = NULL;
......@@ -146,9 +146,9 @@ PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGN
setKeepAlive(MQTT_KEEPALIVE);
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}
PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
PubSubClient::PubSubClient(const char *domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client, Stream &stream) {
this->_state = MQTT_DISCONNECTED;
setServer(domain,port);
setServer(domain, port);
setCallback(callback);
setClient(client);
setStream(stream);
......@@ -163,27 +163,29 @@ PubSubClient::~PubSubClient() {
}
boolean PubSubClient::connect(const char *id) {
return connect(id,NULL,NULL,0,0,0,0,1);
return connect(id, NULL, NULL, 0, 0, 0, 0, 1);
}
boolean PubSubClient::connect(const char *id, const char *user, const char *pass) {
return connect(id,user,pass,0,0,0,0,1);
return connect(id, user, pass, 0, 0, 0, 0, 1);
}
boolean PubSubClient::connect(const char *id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) {
return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage,1);
boolean PubSubClient::connect(const char *id, const char *willTopic, uint8_t willQos, boolean willRetain,
const char *willMessage) {
return connect(id, NULL, NULL, willTopic, willQos, willRetain, willMessage, 1);
}
boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) {
return connect(id,user,pass,willTopic,willQos,willRetain,willMessage,1);
boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char *willTopic,
uint8_t willQos, boolean willRetain, const char *willMessage) {
return connect(id, user, pass, willTopic, willQos, willRetain, willMessage, 1);
}
boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage, boolean cleanSession) {
boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char *willTopic,
uint8_t willQos, boolean willRetain, const char *willMessage, boolean cleanSession) {
if (!connected()) {
int result = 0;
if(_client->connected()) {
if (_client->connected()) {
result = 1;
} else {
if (domain != NULL) {
......@@ -203,28 +205,28 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p', MQTT_VERSION};
#define MQTT_HEADER_VERSION_LENGTH 9
#elif MQTT_VERSION == MQTT_VERSION_3_1_1
uint8_t d[7] = {0x00,0x04,'M','Q','T','T',MQTT_VERSION};
uint8_t d[7] = { 0x00, 0x04, 'M', 'Q', 'T', 'T', MQTT_VERSION };
#define MQTT_HEADER_VERSION_LENGTH 7
#endif
for (j = 0;j<MQTT_HEADER_VERSION_LENGTH;j++) {
for (j = 0; j < MQTT_HEADER_VERSION_LENGTH; j++) {
this->buffer[length++] = d[j];
}
uint8_t v;
if (willTopic) {
v = 0x04|(willQos<<3)|(willRetain<<5);
v = 0x04 | (willQos << 3) | (willRetain << 5);
} else {
v = 0x00;
}
if (cleanSession) {
v = v|0x02;
v = v | 0x02;
}
if(user != NULL) {
v = v|0x80;
if (user != NULL) {
v = v | 0x80;
if(pass != NULL) {
v = v|(0x80>>1);
if (pass != NULL) {
v = v | (0x80 >> 1);
}
}
this->buffer[length++] = v;
......@@ -232,31 +234,31 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
this->buffer[length++] = ((this->keepAlive) >> 8);
this->buffer[length++] = ((this->keepAlive) & 0xFF);
CHECK_STRING_LENGTH(length,id)
length = writeString(id,this->buffer,length);
CHECK_STRING_LENGTH(length, id)
length = writeString(id, this->buffer, length);
if (willTopic) {
CHECK_STRING_LENGTH(length,willTopic)
length = writeString(willTopic,this->buffer,length);
CHECK_STRING_LENGTH(length,willMessage)
length = writeString(willMessage,this->buffer,length);
CHECK_STRING_LENGTH(length, willTopic)
length = writeString(willTopic, this->buffer, length);
CHECK_STRING_LENGTH(length, willMessage)
length = writeString(willMessage, this->buffer, length);
}
if(user != NULL) {
CHECK_STRING_LENGTH(length,user)
length = writeString(user,this->buffer,length);
if(pass != NULL) {
CHECK_STRING_LENGTH(length,pass)
length = writeString(pass,this->buffer,length);
if (user != NULL) {
CHECK_STRING_LENGTH(length, user)
length = writeString(user, this->buffer, length);
if (pass != NULL) {
CHECK_STRING_LENGTH(length, pass)
length = writeString(pass, this->buffer, length);
}
}
write(MQTTCONNECT,this->buffer,length-MQTT_MAX_HEADER_SIZE);
write(MQTTCONNECT, this->buffer, length - MQTT_MAX_HEADER_SIZE);
lastInActivity = lastOutActivity = millis();
while (!_client->available()) {
unsigned long t = millis();
if (t-lastInActivity >= ((int32_t) this->socketTimeout*1000UL)) {
if (t - lastInActivity >= ((int32_t) this->socketTimeout * 1000UL)) {
_state = MQTT_CONNECTION_TIMEOUT;
_client->stop();
return false;
......@@ -285,12 +287,12 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
}
// reads a byte into result
boolean PubSubClient::readByte(uint8_t * result) {
boolean PubSubClient::readByte(uint8_t *result) {
uint32_t previousMillis = millis();
while(!_client->available()) {
while (!_client->available()) {
yield();
uint32_t currentMillis = millis();
if(currentMillis - previousMillis >= ((int32_t) this->socketTimeout * 1000)){
if (currentMillis - previousMillis >= ((int32_t) this->socketTimeout * 1000)) {
return false;
}
}
......@@ -299,20 +301,21 @@ boolean PubSubClient::readByte(uint8_t * result) {
}
// reads a byte into result[*index] and increments index
boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){
boolean PubSubClient::readByte(uint8_t *result, uint16_t *index) {
uint16_t current_index = *index;
uint8_t * write_address = &(result[current_index]);
if(readByte(write_address)){
uint8_t *write_address = &(result[current_index]);
if (readByte(write_address)) {
*index = current_index + 1;
return true;
}
return false;
}
uint32_t PubSubClient::readPacket(uint8_t* lengthLength) {
uint32_t PubSubClient::readPacket(uint8_t *lengthLength) {
uint16_t len = 0;
if(!readByte(this->buffer, &len)) return 0;
bool isPublish = (this->buffer[0]&0xF0) == MQTTPUBLISH;
if (!readByte(this->buffer, &len))
return 0;
bool isPublish = (this->buffer[0] & 0xF0) == MQTTPUBLISH;
uint32_t multiplier = 1;
uint32_t length = 0;
uint8_t digit = 0;
......@@ -326,30 +329,34 @@ uint32_t PubSubClient::readPacket(uint8_t* lengthLength) {
_client->stop();
return 0;
}
if(!readByte(&digit)) return 0;
if (!readByte(&digit))
return 0;
this->buffer[len++] = digit;
length += (digit & 127) * multiplier;
multiplier <<=7; //multiplier *= 128
multiplier <<= 7; //multiplier *= 128
} while ((digit & 128) != 0);
*lengthLength = len-1;
*lengthLength = len - 1;
if (isPublish) {
// Read in topic length to calculate bytes to skip over for Stream writing
if(!readByte(this->buffer, &len)) return 0;
if(!readByte(this->buffer, &len)) return 0;
skip = (this->buffer[*lengthLength+1]<<8)+this->buffer[*lengthLength+2];
if (!readByte(this->buffer, &len))
return 0;
if (!readByte(this->buffer, &len))
return 0;
skip = (this->buffer[*lengthLength + 1] << 8) + this->buffer[*lengthLength + 2];
start = 2;
if (this->buffer[0]&MQTTQOS1) {
if (this->buffer[0] & MQTTQOS1) {
// skip message id
skip += 2;
}
}
uint32_t idx = len;
for (uint32_t i = start;i<length;i++) {
if(!readByte(&digit)) return 0;
for (uint32_t i = start; i < length; i++) {
if (!readByte(&digit))
return 0;
if (this->stream) {
if (isPublish && idx-*lengthLength-2>skip) {
if (isPublish && idx - *lengthLength - 2 > skip) {
this->stream->write(digit);
}
}
......@@ -370,7 +377,7 @@ uint32_t PubSubClient::readPacket(uint8_t* lengthLength) {
boolean PubSubClient::loop() {
if (connected()) {
unsigned long t = millis();
if ((t - lastInActivity > this->keepAlive*1000UL) || (t - lastOutActivity > this->keepAlive*1000UL)) {
if ((t - lastInActivity > this->keepAlive * 1000UL) || (t - lastOutActivity > this->keepAlive * 1000UL)) {
if (pingOutstanding) {
this->_state = MQTT_CONNECTION_TIMEOUT;
_client->stop();
......@@ -378,7 +385,7 @@ boolean PubSubClient::loop() {
} else {
this->buffer[0] = MQTTPINGREQ;
this->buffer[1] = 0;
_client->write(this->buffer,2);
_client->write(this->buffer, 2);
lastOutActivity = t;
lastInActivity = t;
pingOutstanding = true;
......@@ -391,35 +398,35 @@ boolean PubSubClient::loop() {
uint8_t *payload;
if (len > 0) {
lastInActivity = t;
uint8_t type = this->buffer[0]&0xF0;
uint8_t type = this->buffer[0] & 0xF0;
if (type == MQTTPUBLISH) {
if (callback) {
uint16_t tl = (this->buffer[llen+1]<<8)+this->buffer[llen+2]; /* topic length in bytes */
memmove(this->buffer+llen+2,this->buffer+llen+3,tl); /* move topic inside buffer 1 byte to front */
this->buffer[llen+2+tl] = 0; /* end the topic as a 'C' string with \x00 */
char *topic = (char*) this->buffer+llen+2;
uint16_t tl = (this->buffer[llen + 1] << 8) + this->buffer[llen + 2]; /* topic length in bytes */
memmove(this->buffer + llen + 2, this->buffer + llen + 3, tl); /* move topic inside buffer 1 byte to front */
this->buffer[llen + 2 + tl] = 0; /* end the topic as a 'C' string with \x00 */
char *topic = (char*) this->buffer + llen + 2;
// msgId only present for QOS>0
if ((this->buffer[0]&0x06) == MQTTQOS1) {
msgId = (this->buffer[llen+3+tl]<<8)+this->buffer[llen+3+tl+1];
payload = this->buffer+llen+3+tl+2;
callback(topic,payload,len-llen-3-tl-2);
if ((this->buffer[0] & 0x06) == MQTTQOS1) {
msgId = (this->buffer[llen + 3 + tl] << 8) + this->buffer[llen + 3 + tl + 1];
payload = this->buffer + llen + 3 + tl + 2;
callback(topic, payload, len - llen - 3 - tl - 2);
this->buffer[0] = MQTTPUBACK;
this->buffer[1] = 2;
this->buffer[2] = (msgId >> 8);
this->buffer[3] = (msgId & 0xFF);
_client->write(this->buffer,4);
_client->write(this->buffer, 4);
lastOutActivity = t;
} else {
payload = this->buffer+llen+3+tl;
callback(topic,payload,len-llen-3-tl);
payload = this->buffer + llen + 3 + tl;
callback(topic, payload, len - llen - 3 - tl);
}
}
} else if (type == MQTTPINGREQ) {
this->buffer[0] = MQTTPINGRESP;
this->buffer[1] = 0;
_client->write(this->buffer,2);
_client->write(this->buffer, 2);
} else if (type == MQTTPINGRESP) {
pingOutstanding = false;
}
......@@ -433,31 +440,31 @@ boolean PubSubClient::loop() {
return false;
}
boolean PubSubClient::publish(const char* topic, const char* payload) {
return publish(topic,(const uint8_t*)payload, payload ? strnlen(payload, this->bufferSize) : 0,false);
boolean PubSubClient::publish(const char *topic, const char *payload) {
return publish(topic, (const uint8_t*) payload, payload ? strnlen(payload, this->bufferSize) : 0, false);
}
boolean PubSubClient::publish(const char* topic, const char* payload, boolean retained) {
return publish(topic,(const uint8_t*)payload, payload ? strnlen(payload, this->bufferSize) : 0,retained);
boolean PubSubClient::publish(const char *topic, const char *payload, boolean retained) {
return publish(topic, (const uint8_t*) payload, payload ? strnlen(payload, this->bufferSize) : 0, retained);
}
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
boolean PubSubClient::publish(const char *topic, const uint8_t *payload, unsigned int plength) {
return publish(topic, payload, plength, false);
}
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) {
boolean PubSubClient::publish(const char *topic, const uint8_t *payload, unsigned int plength, boolean retained) {
if (connected()) {
if (this->bufferSize < MQTT_MAX_HEADER_SIZE + 2+strnlen(topic, this->bufferSize) + plength) {
if (this->bufferSize < MQTT_MAX_HEADER_SIZE + 2 + strnlen(topic, this->bufferSize) + plength) {
// Too long
return false;
}
// Leave room in the buffer for header and variable length field
uint16_t length = MQTT_MAX_HEADER_SIZE;
length = writeString(topic,this->buffer,length);
length = writeString(topic, this->buffer, length);
// Add payload
uint16_t i;
for (i=0;i<plength;i++) {
for (i = 0; i < plength; i++) {
this->buffer[length++] = payload[i];
}
......@@ -466,16 +473,16 @@ boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigne
if (retained) {
header |= 1;
}
return write(header,this->buffer,length-MQTT_MAX_HEADER_SIZE);
return write(header, this->buffer, length - MQTT_MAX_HEADER_SIZE);
}
return false;
}
boolean PubSubClient::publish_P(const char* topic, const char* payload, boolean retained) {
return publish_P(topic, (const uint8_t*)payload, payload ? strnlen(payload, this->bufferSize) : 0, retained);
boolean PubSubClient::publish_P(const char *topic, const char *payload, boolean retained) {
return publish_P(topic, (const uint8_t*) payload, payload ? strnlen(payload, this->bufferSize) : 0, retained);
}
boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) {
boolean PubSubClient::publish_P(const char *topic, const uint8_t *payload, unsigned int plength, boolean retained) {
uint8_t llen = 0;
uint8_t digit;
unsigned int rc = 0;
......@@ -484,7 +491,7 @@ boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsig
unsigned int i;
uint8_t header;
unsigned int len;
int expectedLength;
uint32_t expectedLength;
if (!connected()) {
return false;
......@@ -506,14 +513,14 @@ boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsig
}
this->buffer[pos++] = digit;
llen++;
} while(len>0);
} while (len > 0);
pos = writeString(topic,this->buffer,pos);
pos = writeString(topic, this->buffer, pos);
rc += _client->write(this->buffer,pos);
rc += _client->write(this->buffer, pos);
for (i=0;i<plength;i++) {
rc += _client->write((char)pgm_read_byte_near(payload + i));
for (i = 0; i < plength; i++) {
rc += _client->write((char) pgm_read_byte_near(payload + i));
}
lastOutActivity = millis();
......@@ -523,19 +530,19 @@ boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsig
return (rc == expectedLength);
}
boolean PubSubClient::beginPublish(const char* topic, unsigned int plength, boolean retained) {
boolean PubSubClient::beginPublish(const char *topic, unsigned int plength, boolean retained) {
if (connected()) {
// Send the header and variable length field
uint16_t length = MQTT_MAX_HEADER_SIZE;
length = writeString(topic,this->buffer,length);
length = writeString(topic, this->buffer, length);
uint8_t header = MQTTPUBLISH;
if (retained) {
header |= 1;
}
size_t hlen = buildHeader(header, this->buffer, plength+length-MQTT_MAX_HEADER_SIZE);
uint16_t rc = _client->write(this->buffer+(MQTT_MAX_HEADER_SIZE-hlen),length-(MQTT_MAX_HEADER_SIZE-hlen));
size_t hlen = buildHeader(header, this->buffer, plength + length - MQTT_MAX_HEADER_SIZE);
uint16_t rc = _client->write(this->buffer + (MQTT_MAX_HEADER_SIZE - hlen), length - (MQTT_MAX_HEADER_SIZE - hlen));
lastOutActivity = millis();
return (rc == (length-(MQTT_MAX_HEADER_SIZE-hlen)));
return (rc == (length - (MQTT_MAX_HEADER_SIZE - hlen)));
}
return false;
}
......@@ -551,10 +558,10 @@ size_t PubSubClient::write(uint8_t data) {
size_t PubSubClient::write(const uint8_t *buffer, size_t size) {
lastOutActivity = millis();
return _client->write(buffer,size);
return _client->write(buffer, size);
}
size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, uint16_t length) {
size_t PubSubClient::buildHeader(uint8_t header, uint8_t *buf, uint16_t length) {
uint8_t lenBuf[4];
uint8_t llen = 0;
uint8_t digit;
......@@ -569,16 +576,16 @@ size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, uint16_t length)
}
lenBuf[pos++] = digit;
llen++;
} while(len>0);
} while (len > 0);
buf[4-llen] = header;
for (int i=0;i<llen;i++) {
buf[MQTT_MAX_HEADER_SIZE-llen+i] = lenBuf[i];
buf[4 - llen] = header;
for (int i = 0; i < llen; i++) {
buf[MQTT_MAX_HEADER_SIZE - llen + i] = lenBuf[i];
}
return llen+1; // Full header size is variable length bit plus the 1-byte fixed header
return llen + 1; // Full header size is variable length bit plus the 1-byte fixed header
}
boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
boolean PubSubClient::write(uint8_t header, uint8_t *buf, uint16_t length) {
uint16_t rc;
uint8_t hlen = buildHeader(header, buf, length);
......@@ -596,17 +603,17 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
}
return result;
#else
rc = _client->write(buf+(MQTT_MAX_HEADER_SIZE-hlen),length+hlen);
rc = _client->write(buf + (MQTT_MAX_HEADER_SIZE - hlen), length + hlen);
lastOutActivity = millis();
return (rc == hlen+length);
return (rc == hlen + length);
#endif
}
boolean PubSubClient::subscribe(const char* topic) {
boolean PubSubClient::subscribe(const char *topic) {
return subscribe(topic, 0);
}
boolean PubSubClient::subscribe(const char* topic, uint8_t qos) {
boolean PubSubClient::subscribe(const char *topic, uint8_t qos) {
size_t topicLength = strnlen(topic, this->bufferSize);
if (topic == 0) {
return false;
......@@ -627,14 +634,14 @@ boolean PubSubClient::subscribe(const char* topic, uint8_t qos) {
}
this->buffer[length++] = (nextMsgId >> 8);
this->buffer[length++] = (nextMsgId & 0xFF);
length = writeString((char*)topic, this->buffer,length);
length = writeString((char*) topic, this->buffer, length);
this->buffer[length++] = qos;
return write(MQTTSUBSCRIBE|MQTTQOS1,this->buffer,length-MQTT_MAX_HEADER_SIZE);
return write(MQTTSUBSCRIBE | MQTTQOS1, this->buffer, length - MQTT_MAX_HEADER_SIZE);
}
return false;
}
boolean PubSubClient::unsubscribe(const char* topic) {
boolean PubSubClient::unsubscribe(const char *topic) {
size_t topicLength = strnlen(topic, this->bufferSize);
if (topic == 0) {
return false;
......@@ -651,8 +658,8 @@ boolean PubSubClient::unsubscribe(const char* topic) {
}
this->buffer[length++] = (nextMsgId >> 8);
this->buffer[length++] = (nextMsgId & 0xFF);
length = writeString(topic, this->buffer,length);
return write(MQTTUNSUBSCRIBE|MQTTQOS1,this->buffer,length-MQTT_MAX_HEADER_SIZE);
length = writeString(topic, this->buffer, length);
return write(MQTTUNSUBSCRIBE | MQTTQOS1, this->buffer, length - MQTT_MAX_HEADER_SIZE);
}
return false;
}
......@@ -660,33 +667,32 @@ boolean PubSubClient::unsubscribe(const char* topic) {
void PubSubClient::disconnect() {
this->buffer[0] = MQTTDISCONNECT;
this->buffer[1] = 0;
_client->write(this->buffer,2);
_client->write(this->buffer, 2);
_state = MQTT_DISCONNECTED;
_client->flush();
_client->stop();
lastInActivity = lastOutActivity = millis();
}
uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) {
const char* idp = string;
uint16_t PubSubClient::writeString(const char *string, uint8_t *buf, uint16_t pos) {
const char *idp = string;
uint16_t i = 0;
pos += 2;
while (*idp) {
buf[pos++] = *idp++;
i++;
}
buf[pos-i-2] = (i >> 8);
buf[pos-i-1] = (i & 0xFF);
buf[pos - i - 2] = (i >> 8);
buf[pos - i - 1] = (i & 0xFF);
return pos;
}
boolean PubSubClient::connected() {
boolean rc;
if (_client == NULL ) {
if (_client == NULL) {
rc = false;
} else {
rc = (int)_client->connected();
rc = (int) _client->connected();
if (!rc) {
if (this->_state == MQTT_CONNECTED) {
this->_state = MQTT_CONNECTION_LOST;
......@@ -700,9 +706,9 @@ boolean PubSubClient::connected() {
return rc;
}
PubSubClient& PubSubClient::setServer(uint8_t * ip, uint16_t port) {
IPAddress addr(ip[0],ip[1],ip[2],ip[3]);
return setServer(addr,port);
PubSubClient& PubSubClient::setServer(uint8_t *ip, uint16_t port) {
IPAddress addr(ip[0], ip[1], ip[2], ip[3]);
return setServer(addr, port);
}
PubSubClient& PubSubClient::setServer(IPAddress ip, uint16_t port) {
......@@ -712,7 +718,7 @@ PubSubClient& PubSubClient::setServer(IPAddress ip, uint16_t port) {
return *this;
}
PubSubClient& PubSubClient::setServer(const char * domain, uint16_t port) {
PubSubClient& PubSubClient::setServer(const char *domain, uint16_t port) {
this->domain = domain;
this->port = port;
return *this;
......@@ -723,12 +729,12 @@ PubSubClient& PubSubClient::setCallback(MQTT_CALLBACK_SIGNATURE) {
return *this;
}
PubSubClient& PubSubClient::setClient(Client& client){
PubSubClient& PubSubClient::setClient(Client &client) {
this->_client = &client;
return *this;
}
PubSubClient& PubSubClient::setStream(Stream& stream){
PubSubClient& PubSubClient::setStream(Stream &stream) {
this->stream = &stream;
return *this;
}
......@@ -743,9 +749,9 @@ boolean PubSubClient::setBufferSize(uint16_t size) {
return false;
}
if (this->bufferSize == 0) {
this->buffer = (uint8_t*)malloc(size);
this->buffer = (uint8_t*) malloc(size);
} else {
uint8_t* newBuffer = (uint8_t*)realloc(this->buffer, size);
uint8_t *newBuffer = (uint8_t*) realloc(this->buffer, size);
if (newBuffer != NULL) {
this->buffer = newBuffer;
} else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment