diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 8679a76..5768387 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -106,7 +106,7 @@ void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka) #ifdef TINY_MQTT_ASYNC client->onData(onData, this); client->onConnect(onConnect, this); - client->connect(broker.c_str(), port); + client->connect(broker.c_str(), port, ka); #else if (client->connect(broker.c_str(), port)) { @@ -259,7 +259,7 @@ bool MqttBroker::compareString( void MqttMessage::getString(const char* &buff, uint16_t& len) { - len = (buff[0]<<8)|(buff[1]); + len = getSize(buff); buff+=2; } @@ -321,8 +321,8 @@ void MqttClient::onConnect(void *mqttclient_ptr, TcpClient*) msg.add(0x4); // Mqtt protocol version 3.1.1 msg.add(0x0); // Connect flags TODO user / name - msg.add(0x00); // keep_alive - msg.add((char)mqtt->keep_alive); + msg.add((char)(mqtt->keep_alive >> 8)); // keep_alive + msg.add((char)(mqtt->keep_alive & 0xFF)); msg.add(mqtt->clientId); debug("cnx: mqtt connecting"); msg.sendTo(mqtt); @@ -443,7 +443,7 @@ void MqttClient::processMessage(MqttMessage* mesg) } payload = header+10; mqtt_flags = header[7]; - keep_alive = (header[8]<<8)|(header[9]); + keep_alive = MqttMessage::getSize(header+8); if (strncmp("MQTT", header+2,4)) { debug("bad mqtt header"); diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 0887751..275eedd 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -104,6 +104,11 @@ class MqttMessage Create=6 }; + static inline uint32_t getSize(const char* buffer) + { + const unsigned char* bun = (const unsigned char*)buffer; + return (*bun << 8) | bun[1]; } + MqttMessage() { reset(); } MqttMessage(Type t, uint8_t bits_d3_d0=0) { create(t); buffer[0] |= bits_d3_d0; } void incoming(char byte); @@ -250,6 +255,7 @@ class MqttClient #ifdef EPOXY_DUINO static std::map counters; // Number of processed messages #endif + uint32_t keepAlive() const { return keep_alive; } private: @@ -312,6 +318,8 @@ class MqttBroker client->dump(indent); } + const std::vector getClients() const { return clients; } + private: friend class MqttClient; diff --git a/tests/network-tests/network-tests.ino b/tests/network-tests/network-tests.ino index 7080414..8f9b0c4 100644 --- a/tests/network-tests/network-tests.ino +++ b/tests/network-tests/network-tests.ino @@ -6,6 +6,7 @@ #include #include #include +#include /** * TinyMqtt network unit tests. @@ -140,6 +141,41 @@ test(suback) assertEqual(MqttClient::counters[MqttMessage::Type::SubAck], 1); } +test(network_client_keep_alive_high) +{ + const uint32_t keep_alive=1000; + start_servers(2, true); + assertEqual(WiFi.status(), WL_CONNECTED); + + MqttBroker broker(1883); + broker.begin(); + IPAddress broker_ip = WiFi.localIP(); + + ESP8266WiFiClass::selectInstance(2); + MqttClient client; + client.connect(broker_ip.toString().c_str(), 1883, keep_alive); + broker.loop(); + + assertTrue(broker.clientsCount() == 1); + assertTrue(client.connected()); + + MqttClient::counters[MqttMessage::Type::SubAck] = 0; + client.subscribe("a/b"); + + // TODO how to avoid these loops ??? + broker.loop(); + client.loop(); + + assertEqual(MqttClient::counters[MqttMessage::Type::SubAck], 1); + + uint32_t sz = broker.getClients().size(); + assertEqual(sz , (uint32_t)1); + + uint32_t ka = broker.getClients()[0]->keepAlive(); + assertEqual(ka, keep_alive); + +} + test(network_client_to_broker_connexion) { start_servers(2, true); diff --git a/tests/nowifi-tests/Makefile b/tests/nowifi-tests/Makefile index 3d297d7..7a0ccfb 100644 --- a/tests/nowifi-tests/Makefile +++ b/tests/nowifi-tests/Makefile @@ -7,7 +7,7 @@ EXTRA_CXXFLAGS=-g3 -O0 CXXFLAGS = -Wextra -Wall -std=gnu++11 -fno-exceptions -fno-threadsafe-statics APP_NAME := nowifi-tests -ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsyncTCP +ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsyncTCP TinyConsole ARDUINO_LIB_DIRS := ../../../EspMock/libraries EPOXY_CORE := EPOXY_CORE_ESP8266 include ../../../EpoxyDuino/EpoxyDuino.mk diff --git a/tests/string-indexer-tests/Makefile b/tests/string-indexer-tests/Makefile index dd82877..a6c4dc0 100644 --- a/tests/string-indexer-tests/Makefile +++ b/tests/string-indexer-tests/Makefile @@ -4,7 +4,7 @@ EXTRA_CXXFLAGS=-g3 -O0 APP_NAME := string-indexer-tests -ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsync +ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsync TinyConsole ARDUINO_LIB_DIRS := ../../../EspMock/libraries EPOXY_CORE := EPOXY_CORE_ESP8266 include ../../../EpoxyDuino/EpoxyDuino.mk diff --git a/tests/topic-tests/Makefile b/tests/topic-tests/Makefile index faa3b40..a15c2e6 100644 --- a/tests/topic-tests/Makefile +++ b/tests/topic-tests/Makefile @@ -4,7 +4,7 @@ EXTRA_CXXFLAGS=-g3 -O0 APP_NAME := topic-tests -ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsync +ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsync TinyConsole ARDUINO_LIB_DIRS := ../../../EspMock/libraries EPOXY_CORE := EPOXY_CORE_ESP8266 include ../../../EpoxyDuino/EpoxyDuino.mk