From 62848056a259ced761bb8eb62b0d1ecee60ac812 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:01:14 +0200 Subject: [PATCH 01/18] test --- .github/workflows/superlinter.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/superlinter.yml b/.github/workflows/superlinter.yml index 96ce0cd..ce74c58 100644 --- a/.github/workflows/superlinter.yml +++ b/.github/workflows/superlinter.yml @@ -1,6 +1,7 @@ name: Super-Linter # Run this workflow every time a new commit pushed to your repository +# on: push jobs: From 505cacc2df4a8c5a779b548d90c69d16aee161c6 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:09:10 +0200 Subject: [PATCH 02/18] Lint fixes --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d124f8e..6a2f241 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # TinyMqtt -![](https://img.shields.io/github/v/release/hsaturn/TinyMqtt) -![](https://img.shields.io/github/issues/hsaturn/TinyMqtt) -![](https://img.shields.io/badge/platform-ESP8266-green) -![](https://img.shields.io/github/license/hsaturn/TinyMqtt) -![](https://img.shields.io/badge/Mqtt-%203.1.1-yellow) +![Release](https://img.shields.io/github/v/release/hsaturn/TinyMqtt) +![Issues](https://img.shields.io/github/issues/hsaturn/TinyMqtt) +![Esp8266](https://img.shields.io/badge/platform-ESP8266-green) +![Gpl 3.0](https://img.shields.io/github/license/hsaturn/TinyMqtt) +![Mqtt 3.1.1](https://img.shields.io/badge/Mqtt-%203.1.1-yellow) ESP 8266 is a small, fast and capable Mqtt Broker and Client From 96d8018960fb77d960121e2ac066356efd35f86f Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:16:07 +0200 Subject: [PATCH 03/18] Test aunit --- .github/workflows/aunit.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/aunit.yml diff --git a/.github/workflows/aunit.yml b/.github/workflows/aunit.yml new file mode 100644 index 0000000..60a532d --- /dev/null +++ b/.github/workflows/aunit.yml @@ -0,0 +1,30 @@ +# See https://docs.github.com/en/actions/guides for documentation about GitHub +# Actions. + +name: AUnit Tests + +# Run on all branches. +on: [push] + +jobs: + build: + + runs-on: ubuntu-18.04 + + steps: + - uses: actions/checkout@v2 + + - name: Setup + run: | + cd .. + git clone https://github.com/bxparks/EpoxyDuino + git clone https://github.com/bxparks/AceRoutine + git clone https://github.com/bxparks/AUnit + git clone https://github.com/bxparks/AceCommon + - name: Verify examples + run: | + make -C examples + - name: Verify tests + run: | + make -C tests + make -C tests runtests From 253bc9b3f5079a9f80d193e486669c651b0268f9 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:26:53 +0200 Subject: [PATCH 04/18] Added makefile for aunit --- tests/Makefile | 20 +++++++++++++++++++ tests/local-tests/Makfile | 6 ++++++ .../local-tests.ino} | 0 3 files changed, 26 insertions(+) create mode 100644 tests/Makefile create mode 100644 tests/local-tests/Makfile rename tests/{tinymqtt-tests/tinymqtt-tests.ino => local-tests/local-tests.ino} (100%) diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..bfea400 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,20 @@ +tests: + set -e; \ + for i in *-tests/Makefile; do \ + echo '==== Making:' $$(dirname $$i); \ + $(MAKE) -C $$(dirname $$i) -j; \ + done + +runtests: + set -e; \ + for i in *-tests/Makefile; do \ + echo '==== Running:' $$(dirname $$i); \ + $$(dirname $$i)/$$(dirname $$i).out; \ + done + +clean: + set -e; \ + for i in *-tests/Makefile; do \ + echo '==== Cleaning:' $$(dirname $$i); \ + $(MAKE) -C $$(dirname $$i) clean; \ + done diff --git a/tests/local-tests/Makfile b/tests/local-tests/Makfile new file mode 100644 index 0000000..799dc2e --- /dev/null +++ b/tests/local-tests/Makfile @@ -0,0 +1,6 @@ +# See https://github.com/bxparks/EpoxyDuino for documentation about this +# Makefile to compile and run Arduino programs natively on Linux or MacOS. + +APP_NAME := local-tests +ARDUINO_LIBS := AUnit AceCommon AceTime +include ../../../EpoxyDuino/EpoxyDuino.mk diff --git a/tests/tinymqtt-tests/tinymqtt-tests.ino b/tests/local-tests/local-tests.ino similarity index 100% rename from tests/tinymqtt-tests/tinymqtt-tests.ino rename to tests/local-tests/local-tests.ino From e550197d0a99cf9ceafed041c96562efda0427bf Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:28:48 +0200 Subject: [PATCH 05/18] Fixed make target --- .github/workflows/aunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/aunit.yml b/.github/workflows/aunit.yml index 60a532d..1d6c1ea 100644 --- a/.github/workflows/aunit.yml +++ b/.github/workflows/aunit.yml @@ -21,7 +21,7 @@ jobs: git clone https://github.com/bxparks/AceRoutine git clone https://github.com/bxparks/AUnit git clone https://github.com/bxparks/AceCommon - - name: Verify examples + - name: Verify tests run: | make -C examples - name: Verify tests From 3f2c1c57e1d4991e4b33a32ac6e98cb68fbebc94 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:30:19 +0200 Subject: [PATCH 06/18] Fixed make target --- .github/workflows/aunit.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/aunit.yml b/.github/workflows/aunit.yml index 1d6c1ea..117bccd 100644 --- a/.github/workflows/aunit.yml +++ b/.github/workflows/aunit.yml @@ -21,9 +21,6 @@ jobs: git clone https://github.com/bxparks/AceRoutine git clone https://github.com/bxparks/AUnit git clone https://github.com/bxparks/AceCommon - - name: Verify tests - run: | - make -C examples - name: Verify tests run: | make -C tests From 60d385189b436fa1d1a578c6aa46348a389a39f9 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:30:19 +0200 Subject: [PATCH 07/18] Fixed make target --- .github/workflows/aunit.yml | 3 --- tests/local-tests/{Makfile => Makefile} | 0 2 files changed, 3 deletions(-) rename tests/local-tests/{Makfile => Makefile} (100%) diff --git a/.github/workflows/aunit.yml b/.github/workflows/aunit.yml index 1d6c1ea..117bccd 100644 --- a/.github/workflows/aunit.yml +++ b/.github/workflows/aunit.yml @@ -21,9 +21,6 @@ jobs: git clone https://github.com/bxparks/AceRoutine git clone https://github.com/bxparks/AUnit git clone https://github.com/bxparks/AceCommon - - name: Verify tests - run: | - make -C examples - name: Verify tests run: | make -C tests diff --git a/tests/local-tests/Makfile b/tests/local-tests/Makefile similarity index 100% rename from tests/local-tests/Makfile rename to tests/local-tests/Makefile From d01f46dbc1b7e4ed82f74497ec19f0bda1dd4949 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:42:57 +0200 Subject: [PATCH 08/18] Fix aunit --- .github/workflows/aunit.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/aunit.yml b/.github/workflows/aunit.yml index 117bccd..b354a4e 100644 --- a/.github/workflows/aunit.yml +++ b/.github/workflows/aunit.yml @@ -21,6 +21,7 @@ jobs: git clone https://github.com/bxparks/AceRoutine git clone https://github.com/bxparks/AUnit git clone https://github.com/bxparks/AceCommon + git clone https://github.com/hsaturn/TinyMqtt - name: Verify tests run: | make -C tests From 3083bcf071e4c6c4718f0cb6c0dd3fb699b370e7 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:44:11 +0200 Subject: [PATCH 09/18] Revert auint --- .github/workflows/aunit.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/aunit.yml b/.github/workflows/aunit.yml index b354a4e..117bccd 100644 --- a/.github/workflows/aunit.yml +++ b/.github/workflows/aunit.yml @@ -21,7 +21,6 @@ jobs: git clone https://github.com/bxparks/AceRoutine git clone https://github.com/bxparks/AUnit git clone https://github.com/bxparks/AceCommon - git clone https://github.com/hsaturn/TinyMqtt - name: Verify tests run: | make -C tests From f2a805f724a452d67bf63c7b549e9368caee97df Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:50:50 +0200 Subject: [PATCH 10/18] Fix makefile --- tests/local-tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/local-tests/Makefile b/tests/local-tests/Makefile index 799dc2e..ed81629 100644 --- a/tests/local-tests/Makefile +++ b/tests/local-tests/Makefile @@ -2,5 +2,5 @@ # Makefile to compile and run Arduino programs natively on Linux or MacOS. APP_NAME := local-tests -ARDUINO_LIBS := AUnit AceCommon AceTime +ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt include ../../../EpoxyDuino/EpoxyDuino.mk From ed4091c53ee1b4e94baa806e9ed00fac567a1f50 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 28 Mar 2021 23:57:06 +0200 Subject: [PATCH 11/18] ESP8266WiFi lib added for aunit --- .github/workflows/aunit.yml | 1 + tests/local-tests/Makefile | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/aunit.yml b/.github/workflows/aunit.yml index 117bccd..7d896ba 100644 --- a/.github/workflows/aunit.yml +++ b/.github/workflows/aunit.yml @@ -21,6 +21,7 @@ jobs: git clone https://github.com/bxparks/AceRoutine git clone https://github.com/bxparks/AUnit git clone https://github.com/bxparks/AceCommon + git clone https://github.com/ekstrand/ESP8266wifi - name: Verify tests run: | make -C tests diff --git a/tests/local-tests/Makefile b/tests/local-tests/Makefile index ed81629..0a232dc 100644 --- a/tests/local-tests/Makefile +++ b/tests/local-tests/Makefile @@ -2,5 +2,5 @@ # Makefile to compile and run Arduino programs natively on Linux or MacOS. APP_NAME := local-tests -ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt +ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt ESP8266wifi include ../../../EpoxyDuino/EpoxyDuino.mk From 1a70c90af24b3329a033b71a0aefab3c054ed483 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Mon, 29 Mar 2021 00:01:32 +0200 Subject: [PATCH 12/18] refix EspWifi --- .github/workflows/aunit.yml | 1 - tests/local-tests/Makefile | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/aunit.yml b/.github/workflows/aunit.yml index 7d896ba..117bccd 100644 --- a/.github/workflows/aunit.yml +++ b/.github/workflows/aunit.yml @@ -21,7 +21,6 @@ jobs: git clone https://github.com/bxparks/AceRoutine git clone https://github.com/bxparks/AUnit git clone https://github.com/bxparks/AceCommon - git clone https://github.com/ekstrand/ESP8266wifi - name: Verify tests run: | make -C tests diff --git a/tests/local-tests/Makefile b/tests/local-tests/Makefile index 0a232dc..df1abdb 100644 --- a/tests/local-tests/Makefile +++ b/tests/local-tests/Makefile @@ -2,5 +2,5 @@ # Makefile to compile and run Arduino programs natively on Linux or MacOS. APP_NAME := local-tests -ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt ESP8266wifi +ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt ESP8266WiFi include ../../../EpoxyDuino/EpoxyDuino.mk From 602050f309d2009f0fc8f58a369752a40fb86837 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Mon, 29 Mar 2021 11:45:51 +0200 Subject: [PATCH 13/18] Update readme.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6a2f241..e555a4e 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ ESP 8266 is a small, fast and capable Mqtt Broker and Client * Implement zeroconf mode (needs async) * Add a max_clients in MqttBroker. Used with zeroconf, there will be no need for having tons of clients (also RAM is the problem with many clients) +* Why not a 'global' TinyMqtt::loop() instead of having to call loop for all broker/clients instances * Test what is the real max number of clients for broker. As far as I saw, 1k is needed per client which would make more than 30 clients critical. * ~~MqttMessage uses a buffer 256 bytes which is usually far than needed.~~ * ~~MqttClient does not support more than one subscription at time~~ From 9407193454ae6beaba98564489ee64a2bf42c876 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Mon, 29 Mar 2021 20:35:55 +0200 Subject: [PATCH 14/18] MqttClient::unsubscribe implemented --- library.json | 2 +- library.properties | 2 +- src/TinyMqtt.cpp | 44 ++++++++++++++++++++++--------- src/TinyMqtt.h | 3 ++- tests/local-tests/local-tests.ino | 4 +-- 5 files changed, 37 insertions(+), 18 deletions(-) diff --git a/library.json b/library.json index 135c534..a2cc462 100644 --- a/library.json +++ b/library.json @@ -6,7 +6,7 @@ "type": "git", "url": "https://github.com/hsaturn/TinyMqtt.git" }, - "version": "0.6.0", + "version": "0.7.1", "exclude": "", "examples": "examples/*/*.ino", "frameworks": "arduino", diff --git a/library.properties b/library.properties index 4e46fdb..1a093b4 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TinyMqtt -version=0.6.0 +version=0.7.1 author=Francois BIOT, HSaturn, maintainer=Francois BIOT, HSaturn, sentence=A tiny broker and client library for MQTT messaging. diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 9c37ac1..bd2bce7 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -269,24 +269,42 @@ MqttError MqttClient::subscribe(Topic topic, uint8_t qos) subscriptions.insert(topic); - if (parent==nullptr) // remote broker ? + if (parent==nullptr) // remote broker { - debug("remote subscribe"); - MqttMessage msg(MqttMessage::Type::Subscribe, 2); - - // TODO manage packet identifier - msg.add(0); - msg.add(0); - - msg.add(topic); - msg.add(qos); - ret = msg.sendTo(this); - - // TODO we should wait (state machine) for SUBACK + return sendTopic(topic, MqttMessage::Type::Subscribe, qos); } return ret; } +MqttError MqttClient::unsubscribe(Topic topic) +{ + auto it=subscriptions.find(topic); + if (it != subscriptions.end()) + { + subscriptions.erase(it); + if (parent==nullptr) // remote broker + { + return sendTopic(topic, MqttMessage::Type::UnSubscribe, 0); + } + } + return MqttOk; +} + +MqttError MqttClient::sendTopic(const Topic& topic, MqttMessage::Type type, uint8_t qos) +{ + MqttMessage msg(type, 2); + + // TODO manage packet identifier + msg.add(0); + msg.add(0); + + msg.add(topic); + msg.add(qos); + + // TODO instead we should wait (state machine) for SUBACK / UNSUBACK ? + return msg.sendTo(this); +} + long MqttClient::counter=0; void MqttClient::processMessage() diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index d40e959..3246e63 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -149,7 +149,7 @@ class MqttClient MqttError publish(const Topic& t) { return publish(t, nullptr, 0);}; MqttError subscribe(Topic topic, uint8_t qos=0); - MqttError unsubscribe(Topic& topic); + MqttError unsubscribe(Topic topic); // connected to local broker // TODO seems to be useless @@ -178,6 +178,7 @@ class MqttClient static long counter; // Number of messages sent private: + MqttError sendTopic(const Topic& topic, MqttMessage::Type type, uint8_t qos); void resubscribe(); friend class MqttBroker; diff --git a/tests/local-tests/local-tests.ino b/tests/local-tests/local-tests.ino index a8c6dad..6dc0233 100644 --- a/tests/local-tests/local-tests.ino +++ b/tests/local-tests/local-tests.ino @@ -96,8 +96,9 @@ test(local_unsubscribe) MqttClient publisher(&broker); publisher.publish("a/b"); - // subscriber.unsubscribe("a/b"); TODO not yet implemented + subscriber.unsubscribe("a/b"); + publisher.publish("a/b"); publisher.publish("a/b"); assertTrue(published[""]["a/b"] == 1); // Only one publish has been received @@ -120,7 +121,6 @@ test(local_nocallback_when_destroyed) assertEqual(published.size(), (size_t)0); // Only one publish has been received } - //---------------------------------------------------------------------------- // setup() and loop() void setup() { From 792a28e831293f78f6726dbaddd892136db31b76 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Mon, 29 Mar 2021 20:45:33 +0200 Subject: [PATCH 15/18] deleted --- src/TinyMqtt.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index bd2bce7..3a95f39 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -41,7 +41,6 @@ MqttClient::~MqttClient() { close(); delete client; - Serial << "Client deleted" << endl; } void MqttClient::close(bool bSendDisconnect) From 533ab0c70dac8fd16c4db0226dbfebb93cdeae0e Mon Sep 17 00:00:00 2001 From: hsaturn Date: Mon, 29 Mar 2021 21:47:14 +0200 Subject: [PATCH 16/18] Try to mock Esp --- .github/workflows/aunit.yml | 1 + tests/local-tests/Makefile | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/aunit.yml b/.github/workflows/aunit.yml index 117bccd..a23ac7f 100644 --- a/.github/workflows/aunit.yml +++ b/.github/workflows/aunit.yml @@ -21,6 +21,7 @@ jobs: git clone https://github.com/bxparks/AceRoutine git clone https://github.com/bxparks/AUnit git clone https://github.com/bxparks/AceCommon + git clone https://github.com/hsaturn/EspMock - name: Verify tests run: | make -C tests diff --git a/tests/local-tests/Makefile b/tests/local-tests/Makefile index df1abdb..d6ab7a3 100644 --- a/tests/local-tests/Makefile +++ b/tests/local-tests/Makefile @@ -2,5 +2,5 @@ # Makefile to compile and run Arduino programs natively on Linux or MacOS. APP_NAME := local-tests -ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt ESP8266WiFi +ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock include ../../../EpoxyDuino/EpoxyDuino.mk From a6596ffc89bfb058bf5867884e5b5e78a6975ca1 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Mon, 29 Mar 2021 23:06:40 +0200 Subject: [PATCH 17/18] Fix ptr --- src/TinyMqtt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 3246e63..2c98161 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -158,7 +158,7 @@ class MqttClient void dump() { uint32_t ms=millis(); - Serial << "MqttClient (" << clientId.c_str() << ") p=" << (int32_t) parent + Serial << "MqttClient (" << clientId.c_str() << ") p=" << (uint64_t) parent << " c=" << (int32_t)client << (connected() ? " ON " : " OFF"); Serial << ", alive=" << (uint32_t)alive << '/' << ms << ", ka=" << keep_alive; Serial << (client && client->connected() ? "" : "dis") << "connected"; From 6fcfc9dfc0a1fca8d98a6bddba48e87b309b6b6e Mon Sep 17 00:00:00 2001 From: hsaturn Date: Mon, 29 Mar 2021 23:20:49 +0200 Subject: [PATCH 18/18] ptr --- src/TinyMqtt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 2c98161..59a8a88 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -159,7 +159,7 @@ class MqttClient { uint32_t ms=millis(); Serial << "MqttClient (" << clientId.c_str() << ") p=" << (uint64_t) parent - << " c=" << (int32_t)client << (connected() ? " ON " : " OFF"); + << " c=" << (uint64_t)client << (connected() ? " ON " : " OFF"); Serial << ", alive=" << (uint32_t)alive << '/' << ms << ", ka=" << keep_alive; Serial << (client && client->connected() ? "" : "dis") << "connected"; message.hexdump("entrant msg");