From 294657f2ca73482230e33324b98d84f535b7c222 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Wed, 22 Mar 2023 09:42:58 +0100 Subject: [PATCH 1/8] [bump_version] Added a commit message for bump version --- bump_version.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bump_version.sh b/bump_version.sh index 26c4fa2..268acde 100755 --- a/bump_version.sh +++ b/bump_version.sh @@ -8,7 +8,7 @@ else fi if [ "$1" == "" ]; then echo - echo "Syntax: $0 [-d] {new_version}" + echo "Syntax: $0 [-d] {new_version} [commit message]" echo echo " -d : dry run, generate json and update properties but do not run git commands" echo "" @@ -34,7 +34,6 @@ else depends=$(echo "$value" | sed "s/,/ /g") echo " Depends=$depends" fi - echo " " sed -i "s@#$name@$value@g" library.json sed -i "s@#$name@$value@g" library.json done < library.properties deps="" @@ -47,10 +46,11 @@ else sed -i "s@#dependencies@$deps@g" library.json sed -i "s/'/\"/g" library.json if [ "$do" == "1" ]; then + echo "Pushing all" git tag $1 git add library.properties git add library.json - git commit -m "Release $1" + git commit -m "Release $1 $2" git push git push --tags fi From 245e74666e1b33bb6b3b2044d205167bf56eec25 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Wed, 22 Mar 2023 20:28:58 +0100 Subject: [PATCH 2/8] [examples] Little modification for RETAIN --- examples/simple-broker/simple-broker.ino | 8 ++++++-- examples/simple-client/simple-client.ino | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/simple-broker/simple-broker.ino b/examples/simple-broker/simple-broker.ino index 1b054bd..3abf151 100644 --- a/examples/simple-broker/simple-broker.ino +++ b/examples/simple-broker/simple-broker.ino @@ -1,7 +1,9 @@ #include "TinyMqtt.h" // https://github.com/hsaturn/TinyMqtt -#define PORT 1883 -MqttBroker broker(PORT); +const uint16_t PORT 1883; +const uint8_t RETAIN = 10; // Max retained messages + +MqttBroker broker(PORT, RETAIN); /** Basic Mqtt Broker * @@ -16,6 +18,8 @@ MqttBroker broker(PORT); * Your ESP will become a MqttBroker. * You can test it with any client such as mqtt-spy for example * + * Messages are retained *only* if retain > 0 + * */ const char* ssid = ""; diff --git a/examples/simple-client/simple-client.ino b/examples/simple-client/simple-client.ino index 0ba8179..fada697 100644 --- a/examples/simple-client/simple-client.ino +++ b/examples/simple-client/simple-client.ino @@ -1,7 +1,7 @@ #include "TinyMqtt.h" // https://github.com/hsaturn/TinyMqtt #include "TinyStreaming.h" // https://github.com/hsaturn/TinyConsole -/** Simple Client (The simplest configuration) +/** Simple Client (The simplest configuration, client only sends topics) * * * +--------+ From 37fb46ec3b281c14fc839affb84a87ca47d0a63a Mon Sep 17 00:00:00 2001 From: hsaturn Date: Wed, 22 Mar 2023 20:29:25 +0100 Subject: [PATCH 3/8] [TinyMqtt.cpp] Removed obsolete comment --- src/TinyMqtt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 8bf772d..b795676 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -638,7 +638,7 @@ void MqttClient::processMessage(MqttMessage* mesg) #endif if (callback and isSubscribedTo(published)) { - callback(this, published, payload, len); // TODO send the real payload + callback(this, published, payload, len); } } else if (local_broker) // from outside to inside From e4ad27c805910182388297095d48b6f8dc474a6d Mon Sep 17 00:00:00 2001 From: hsaturn Date: Wed, 22 Mar 2023 20:29:52 +0100 Subject: [PATCH 4/8] [MqttClient] Added connect(IPAddress) --- src/TinyMqtt.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 9119de4..86085ce 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -210,6 +210,8 @@ class MqttClient void connect(MqttBroker* local_broker); void connect(string broker, uint16_t port, uint16_t keep_alive = 10); + void connect(const IPAddress& ip, uint16_t port, uint16_t keep_alive = 10) + { connect(ip.toString().c_str(), port, keep_alive); } // TODO it seems that connected returns true in tcp mode even if // no negociation occurred From 02496bef732a8d2f555396bbc384290ff93d1472 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Wed, 22 Mar 2023 20:31:05 +0100 Subject: [PATCH 5/8] [test] Test added for retain --- tests/Makefile.opts | 2 +- tests/network-tests/network-tests.ino | 153 ++++++++++++++++++++++---- 2 files changed, 131 insertions(+), 24 deletions(-) diff --git a/tests/Makefile.opts b/tests/Makefile.opts index 473f0e3..dec0f6d 100644 --- a/tests/Makefile.opts +++ b/tests/Makefile.opts @@ -1,7 +1,7 @@ # GCC # CXXFLAGS = -Wextra -Wall -std=gnu++11 -fno-exceptions -fno-threadsafe-statics -EXTRA_CXXFLAGS=-g3 -O0 +EXTRA_CXXFLAGS=-g3 -O0 -std=c++17 CXXFLAGS=-D_GNU_SOURCE -Werror=return-type -std=gnu++17 -Wall -g3 -O0 diff --git a/tests/network-tests/network-tests.ino b/tests/network-tests/network-tests.ino index 5e67f6b..bb2285e 100644 --- a/tests/network-tests/network-tests.ino +++ b/tests/network-tests/network-tests.ino @@ -85,7 +85,7 @@ std::map> published; // map[client_id] => map[t char* lastPayload = nullptr; size_t lastLength; -void start_servers(int n, bool early_accept = true) +void start_many_wifi_esp(int n, bool early_accept = true) { ESP8266WiFiClass::resetInstances(); ESP8266WiFiClass::earlyAccept = early_accept; @@ -107,7 +107,7 @@ void onPublish(const MqttClient* srce, const Topic& topic, const char* payload, lastLength = length; } -test(network_single_broker_begin) +test(single_broker_begin) { assertEqual(WiFi.status(), WL_CONNECTED); @@ -119,7 +119,7 @@ test(network_single_broker_begin) test(suback) { - start_servers(2, true); + start_many_wifi_esp(2, true); assertEqual(WiFi.status(), WL_CONNECTED); MqttBroker broker(1883); @@ -144,10 +144,10 @@ test(suback) assertEqual(MqttClient::counters[MqttMessage::Type::SubAck], 1); } -test(network_client_keep_alive_high) +test(client_keep_alive_high) { const uint32_t keep_alive=1000; - start_servers(2, true); + start_many_wifi_esp(2, true); assertEqual(WiFi.status(), WL_CONNECTED); MqttBroker broker(1883); @@ -179,9 +179,116 @@ test(network_client_keep_alive_high) } -test(network_client_to_broker_connexion) +test(retained_message) { - start_servers(2, true); + published.clear(); + + start_many_wifi_esp(2, true); + assertEqual(WiFi.status(), WL_CONNECTED); + + MqttBroker broker(1883); + broker.begin(); + broker.retain(10); + IPAddress broker_ip = WiFi.localIP(); + + MqttClient local_client(&broker); + + // Send a retained message + // No remote client connected + local_client.publish("topic", "retained", true); + + for(int i=0; i<2; i++) + { + broker.loop(); + local_client.loop(); + }; + + // No connect a client from 2nd Esp + ESP8266WiFiClass::selectInstance(2); + MqttClient remote_client; + remote_client.connect(broker_ip, 1883); + remote_client.setCallback(onPublish); + + assertTrue(remote_client.connected()); + for(int i=0; i<4; i++) { broker.loop(); local_client.loop(); remote_client.loop(); }; + assertEqual(broker.clientsCount(), (size_t) 2); + + // Should not have received anything yet + assertEqual(published.size(), (size_t)0); + + // Now, remote client subscribes to topic + remote_client.subscribe("topic"); + for(int i=0; i<4; i++) { broker.loop(); local_client.loop(); remote_client.loop(); }; + + // Check that the retained message is published + assertEqual(published.size(), (size_t)1); + + // FIXME we should check that + // 1 - Retained message has the retain flag set + // 2 - Published retained messages that are send normally have their retain flag off + + // The next part of this test does not pass yet (due to remote_client.close() + // that does not work well. + return; + + // Now remove the retained message + remote_client.close(); + for(int i=0; i<4; i++) { broker.loop(); local_client.loop(); remote_client.loop(); }; + assertFalse(remote_client.connected()); + assertEqual(broker.clientsCount(), (size_t) 1); + + // Disconnect / reconnect the remote clien that should receive again the message + remote_client.connect(broker_ip, 1883); + remote_client.subscribe("topic"); + assertTrue(remote_client.connected()); + for(int i=0; i<4; i++) { broker.loop(); local_client.loop(); remote_client.loop(); }; + assertEqual(broker.clientsCount(), (size_t) 2); + assertEqual(published.size(), (size_t)2); + + // Remove the retained message now + local_client.publish("topic", "", true); + assertEqual(published.size(), (size_t)1); + + // And reconnect the remote client + remote_client.connect(broker_ip, 1883); + for(int i=0; i<4; i++) { broker.loop(); local_client.loop(); remote_client.loop(); }; + assertEqual(broker.clientsCount(), (size_t) 2); + + // check that the message was received + assertEqual(published.size(), (size_t)2); +} + +test(remote_client_disconnect_reconnect) +{ + published.clear(); + start_many_wifi_esp(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, 1883); + + for(int i=0; i<4; i++) { broker.loop(); client.loop(); }; + assertEqual(broker.clientsCount(), (size_t) 1); + + // Disconnect the client + client.close(); + for(int i=0; i<4; i++) { broker.loop(); client.loop(); }; + assertEqual(broker.clientsCount(), (size_t) 0); + + // Reconnect the client + client.connect(broker_ip, 1883); + for(int i=0; i<4; i++) { broker.loop(); client.loop(); }; + assertEqual(broker.clientsCount(), (size_t) 1); +} + +test(client_to_broker_connexion) +{ + start_many_wifi_esp(2, true); assertEqual(WiFi.status(), WL_CONNECTED); MqttBroker broker(1883); @@ -197,9 +304,9 @@ test(network_client_to_broker_connexion) assertTrue(client.connected()); } -test(network_one_client_one_broker_publish_and_subscribe_through_network) +test(one_client_one_broker_publish_and_subscribe) { - start_servers(2, true); + start_many_wifi_esp(2, true); published.clear(); assertEqual(WiFi.status(), WL_CONNECTED); @@ -228,9 +335,9 @@ test(network_one_client_one_broker_publish_and_subscribe_through_network) assertEqual((int)lastLength, (int)2); // sizeof(ab) } -test(network_one_client_one_broker_hudge_publish_and_subscribe_through_network) +test(one_client_one_broker_hudge_payload) { - start_servers(2, true); + start_many_wifi_esp(2, true); published.clear(); assertEqual(WiFi.status(), WL_CONNECTED); @@ -247,8 +354,8 @@ test(network_one_client_one_broker_hudge_publish_and_subscribe_through_network) std::string sent; - for(int i=0; i<200; i++) - sent += char('0'+i%10); + for(int i=0; i<400; i++) + sent += char('a'+i%26); client.setCallback(onPublish); client.subscribe("a/b"); @@ -264,7 +371,7 @@ test(network_one_client_one_broker_hudge_publish_and_subscribe_through_network) assertEqual((unsigned int)lastLength, (unsigned int)sent.size()); } -test(network_client_should_unregister_when_destroyed) +test(client_should_unregister_when_destroyed) { assertEqual(broker.clientsCount(), (size_t)0); { @@ -278,7 +385,7 @@ test(network_client_should_unregister_when_destroyed) // THESE TESTS ARE IN LOCAL MODE // WE HAVE TO CONVERT THEM TO WIFI MODE (pass through virtual TCP link) -test(network_connect) +test(connect) { assertEqual(broker.clientsCount(), (size_t)0); @@ -287,7 +394,7 @@ test(network_connect) assertEqual(broker.clientsCount(), (size_t)1); } -test(network_publish_should_be_dispatched) +test(publish_should_be_dispatched) { published.clear(); assertEqual(broker.clientsCount(), (size_t)0); @@ -307,7 +414,7 @@ test(network_publish_should_be_dispatched) assertEqual(published[TINY_MQTT_DEFAULT_CLIENT_ID]["a/c"], 2); } -test(network_publish_should_be_dispatched_to_clients) +test(publish_should_be_dispatched_to_clients) { published.clear(); assertEqual(broker.clientsCount(), (size_t)0); @@ -332,7 +439,7 @@ test(network_publish_should_be_dispatched_to_clients) assertEqual(published["B"]["a/c"], 0); } -test(network_unsubscribe) +test(unsubscribe) { published.clear(); assertEqual(broker.clientsCount(), (size_t)0); @@ -352,7 +459,7 @@ test(network_unsubscribe) assertEqual(published[TINY_MQTT_DEFAULT_CLIENT_ID]["a/b"], 1); // Only one publish has been received } -test(network_nocallback_when_destroyed) +test(nocallback_when_destroyed) { published.clear(); assertEqual(broker.clientsCount(), (size_t)0); @@ -371,7 +478,7 @@ test(network_nocallback_when_destroyed) assertEqual(published.size(), (size_t)1); // Only one publish has been received } -test(network_small_payload) +test(small_payload) { published.clear(); @@ -389,7 +496,7 @@ test(network_small_payload) assertEqual(lastLength, (size_t)4); } -test(network_hudge_payload) +test(hudge_payload) { const char* payload="This payload is hudge, just because its length exceeds 127. Thus when encoding length, we have to encode it on two bytes at min. This should not prevent the message from being encoded and decoded successfully !"; @@ -436,7 +543,7 @@ test(connack) } ); - start_servers(2, true); + start_many_wifi_esp(2, true); assertEqual(WiFi.status(), WL_CONNECTED); MqttBroker broker(1883); @@ -469,7 +576,7 @@ void setup() { while(!Serial); */ - Serial.println("=============[ FAKE NETWORK TinyMqtt TESTS ]========================"); + Serial.println("=============[ NETWORK TinyMqtt TESTS ]========================"); WiFi.mode(WIFI_STA); WiFi.begin("network", "password"); From 43dbea1f171478d53e66bcc173f483a41ff7ea13 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Wed, 22 Mar 2023 20:31:41 +0100 Subject: [PATCH 6/8] [test] Fix payload bug in test + moved huge paylod from network to here --- tests/local-tests/local-tests.ino | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/local-tests/local-tests.ino b/tests/local-tests/local-tests.ino index 1379a76..bed2d00 100644 --- a/tests/local-tests/local-tests.ino +++ b/tests/local-tests/local-tests.ino @@ -18,14 +18,14 @@ MqttBroker broker(1883); std::map> published; // map[client_id] => map[topic] = count -const char* lastPayload; +std::string lastPayload; size_t lastLength; void onPublish(const MqttClient* srce, const Topic& topic, const char* payload, size_t length) { if (srce) published[srce->id()][topic]++; - lastPayload = payload; + lastPayload = std::string(payload, length); lastLength = length; } @@ -51,6 +51,7 @@ test(local_client_should_unregister_when_destroyed) test(local_client_do_not_disconnect_after_publishing_and_long_inactivity) { + published.clear(); EpoxyTest::set_millis(0); MqttBroker broker(1883); MqttClient client(&broker, "client"); @@ -121,6 +122,25 @@ test(local_publish_should_be_dispatched) assertEqual(published[""]["a/c"], 2); } +test(hudge_payload) +{ + published.clear(); + const char* payload="This payload is hudge, just because its length exceeds 127. Thus when encoding length, we have to encode it on two bytes at min. This should not prevent the message from being encoded and decoded successfully !"; + + MqttClient subscriber(&broker); + assertEqual(broker.clientsCount(), (size_t)1); + subscriber.setCallback(onPublish); + subscriber.subscribe("a/b"); // Note -> this does not send any byte .... (nowhere to send) + + MqttClient publisher(&broker); + publisher.publish("a/b", payload); // This publish is received + + // onPublish should have filled lastPayload and lastLength + assertEqual(payload, lastPayload.c_str()); + assertEqual(lastLength, strlen(payload)); + assertEqual(strncmp(payload, lastPayload.c_str(), lastLength), 0); +} + test(local_publish_should_be_dispatched_to_local_clients) { published.clear(); From f8a2e35dd92392f9531411f1787d25dc6a617f2f Mon Sep 17 00:00:00 2001 From: hsaturn Date: Wed, 22 Mar 2023 20:34:15 +0100 Subject: [PATCH 7/8] [readme] Words about retain --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 759147f..7de70f8 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ TinyMqtt is a small, fast and capable Mqtt Broker and Client for Esp8266 / Esp32 ## Features +- Supports retained messages (not activated by default) - Async Wifi compatible (me-no-dev/ESPAsyncTCP@^1.2.2) - Very fast broker I saw it re-sent 1000 topics per second for two clients that had subscribed (payload ~15 bytes ESP8266). No topic lost. From 088071d17f01daae770fe1eab8b24ac1cb0b9da0 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Wed, 22 Mar 2023 20:34:15 +0100 Subject: [PATCH 8/8] [readme] Words about retain --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 7de70f8..13dc9d0 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,13 @@ TinyMqtt is a small, fast and capable Mqtt Broker and Client for Esp8266 / Esp32 - tinymqtt-test : This is a complex sketch with a terminal console that allows to add clients publish, connect etc with interpreted commands. +## Retained messages + +Qos 1 is not supported, but retained messages are. So a new subscription is able to send old messages. +This feature is disabled by default. +The default retain parameter of MqttBroker::MqttBroker takes an optional (0 by default) number of retained messages. +MqttBroker::retain(n) will also make the broker store n messages at max. + ## Standalone mode (zeroconf) -> The zeroconf mode is not yet implemented zeroconf clients to connect to broker on local network.