Local fixes
Diff: MQTTClient.cpp
- Revision:
- 7:0f12a3d0bd10
- Parent:
- 6:1ce536bf461b
diff -r 1ce536bf461b -r 0f12a3d0bd10 MQTTClient.cpp --- a/MQTTClient.cpp Fri Jun 05 15:46:10 2020 +0000 +++ b/MQTTClient.cpp Thu Sep 24 21:19:41 2020 +0000 @@ -9,6 +9,8 @@ #include <string.h> #include <time.h> +#define UIPETHERNET_DEBUG 1 + /** * @brief * @note @@ -133,6 +135,9 @@ ) { if (!connected()) { +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::connect \r\n"); +#endif int result = 0; if (_domain != NULL) { @@ -195,6 +200,9 @@ unsigned long t = time(NULL); if (t - _lastInActivity > MQTT_KEEPALIVE) { _client.stop(); +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::connect available \r\n"); +#endif return false; } } @@ -204,12 +212,18 @@ if (_readPacket(&len) == 4 && _buffer[3] == 0) { _lastInActivity = time(NULL); _pingOutstanding = false; +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::connect 4 \r\n"); +#endif return true; } } _client.stop(); } +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::connect End \r\n"); +#endif return false; } @@ -235,6 +249,11 @@ */ uint16_t MQTTClient::_readPacket(uint8_t* length) { +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::_readPacket \r\n"); +#endif + + uint16_t len = 0; _buffer[len++] = _readByte(); @@ -284,6 +303,10 @@ len = 0; // This will cause the packet to be ignored. } +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::_readPacket End \r\n"); +#endif + return len; } @@ -295,6 +318,11 @@ */ bool MQTTClient::poll() { + +#ifdef UIPETHERNET_DEBUG +// printf("bool MQTTClient::poll() \r\n"); +#endif + if (connected()) { time_t now = time(NULL); if ((now - _lastInActivity > MQTT_KEEPALIVE) || (now - _lastOutActivity > MQTT_KEEPALIVE)) { @@ -366,6 +394,10 @@ return true; } +#ifdef UIPETHERNET_DEBUG + printf("bool MQTTClient::poll() End \r\n"); +#endif + return false; } @@ -399,6 +431,11 @@ */ bool MQTTClient::publish(const char* topic, uint8_t* payload, uint16_t plength, bool retained) { + +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::publish \r\n"); +#endif + if (connected()) { // Leave room in the buffer for header and variable length field uint16_t length = 5; @@ -417,6 +454,10 @@ return _write(header, _buffer, length - 5); } +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::publish End \r\n"); +#endif + return false; } @@ -482,6 +523,10 @@ */ bool MQTTClient::subscribe(const char* topic, uint8_t qos) { +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::subscribe \r\n"); +#endif + if (qos > 1) return false; @@ -500,6 +545,10 @@ return _write(MQTTSUBSCRIBE | MQTTQOS1, _buffer, length - 5); } +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::subscribe End \r\n"); +#endif + return false; } @@ -511,6 +560,10 @@ */ bool MQTTClient::unsubscribe(const char* topic) { +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::unsubscribe \r\n"); +#endif + if (connected()) { uint16_t length = 5; _nextMsgId++; @@ -524,6 +577,10 @@ return _write(MQTTUNSUBSCRIBE | MQTTQOS1, _buffer, length - 5); } +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::unsubscribe End \r\n"); +#endif + return false; } @@ -535,11 +592,21 @@ */ void MQTTClient::disconnect() { +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::disconnect() \r\n"); +#endif + _buffer[0] = MQTTDISCONNECT; _buffer[1] = 0; _client.send(_buffer, 2); + _client.close(); _client.stop(); _lastInActivity = _lastOutActivity = time(NULL); + +#ifdef UIPETHERNET_DEBUG + printf("MQTTClient::disconnect() End \r\n"); +#endif + } /**