From 1f267c135bbce967abc0d285a985a463427c53b4 Mon Sep 17 00:00:00 2001 From: Francois BIOT Date: Thu, 29 Dec 2022 02:15:18 +0100 Subject: [PATCH 1/5] fix erroneous sizeof multimap comment --- examples/advanced/mqtt_class_binder/mqtt_class_binder.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/advanced/mqtt_class_binder/mqtt_class_binder.ino b/examples/advanced/mqtt_class_binder/mqtt_class_binder.ino index 46eb6ca..f9f8081 100644 --- a/examples/advanced/mqtt_class_binder/mqtt_class_binder.ino +++ b/examples/advanced/mqtt_class_binder/mqtt_class_binder.ino @@ -30,7 +30,7 @@ * - accepts external clients * - MqttClassBinder allows to mix together many mqtt sources * - * cons - Takes more memory (48 more bytes for the one MqttClassBinder + * cons - Takes more memory (24 more bytes for the one MqttClassBinder * - a bit hard to understand * */ From 292592c3ddf745457464cb48ba21ca215fe4da61 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Thu, 29 Dec 2022 02:17:43 +0100 Subject: [PATCH 2/5] Added missing Makefile for unit test of MqttClassBinder --- tests/classbind-tests/Makefile | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/classbind-tests/Makefile diff --git a/tests/classbind-tests/Makefile b/tests/classbind-tests/Makefile new file mode 100644 index 0000000..7f0f8b5 --- /dev/null +++ b/tests/classbind-tests/Makefile @@ -0,0 +1,13 @@ +# See https://github.com/bxparks/EpoxyDuino for documentation about this +# Makefile to compile and run Arduino programs natively on Linux or MacOS. + +EXTRA_CXXFLAGS=-g3 -O0 -DTINY_MQTT_TESTS + +# Remove flto flag from EpoxyDuino (too many ) +CXXFLAGS = -Wextra -Wall -std=gnu++11 -fno-exceptions -fno-threadsafe-statics + +APP_NAME := classbind-tests +ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsyncTCP TinyConsole +ARDUINO_LIB_DIRS := ../../../EspMock/libraries +EPOXY_CORE := EPOXY_CORE_ESP8266 +include ../../../EpoxyDuino/EpoxyDuino.mk From 0db07df27b5e3561f4ceaf358c39c63340cdbfc1 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Thu, 29 Dec 2022 12:54:58 +0100 Subject: [PATCH 3/5] Remove useless comment --- src/TinyMqtt.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 51b654c..5624f76 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -182,9 +182,6 @@ void MqttBroker::loop() broker->loop(); } - - // for(auto it=clients.begin(); it!=clients.end(); it++) - // use index because size can change during the loop for(size_t i=0; i Date: Thu, 29 Dec 2022 12:52:29 +0100 Subject: [PATCH 4/5] MqttClient::client renamed to tcp_client --- src/TinyMqtt.cpp | 55 ++++++++++++++++++++++++------------------------ src/TinyMqtt.h | 13 ++++++------ 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 5624f76..fc774cf 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -38,12 +38,12 @@ MqttClient::MqttClient(MqttBroker* local_broker, TcpClient* new_client) { debug("MqttClient private with broker"); #ifdef TINY_MQTT_ASYNC - client = new_client; - client->onData(onData, this); + tcp_client = new_client; + tcp_client->onData(onData, this); // client->onConnect() TODO // client->onDisconnect() TODO #else - client = new WiFiClient(*new_client); + tcp_client = new WiFiClient(*new_client); #endif #ifdef EPOXY_DUINO alive = millis()+500000; @@ -55,30 +55,29 @@ MqttClient::MqttClient(MqttBroker* local_broker, TcpClient* new_client) MqttClient::MqttClient(MqttBroker* local_broker, const std::string& id) : local_broker(local_broker), clientId(id) { - client = nullptr; - if (local_broker) local_broker->addClient(this); } MqttClient::~MqttClient() { close(); - delete client; + delete tcp_client; + debug("*** MqttClient delete()"); } void MqttClient::close(bool bSendDisconnect) { debug("close " << id().c_str()); mqtt_connected = false; - if (client) // connected to a remote broker + if (tcp_client) // connected to a remote broker { - if (bSendDisconnect and client->connected()) + if (bSendDisconnect and tcp_client->connected()) { message.create(MqttMessage::Type::Disconnect); message.hexdump("close"); message.sendTo(this); } - client->stop(); + tcp_client->stop(); } if (local_broker) @@ -100,18 +99,18 @@ void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka) debug("MqttClient::connect_to_host " << broker << ':' << port); keep_alive = ka; close(); - if (client) delete client; - client = new TcpClient; + if (tcp_client) delete tcp_client; + tcp_client = new TcpClient; #ifdef TINY_MQTT_ASYNC - client->onData(onData, this); - client->onConnect(onConnect, this); - client->connect(broker.c_str(), port, ka); + tcp_client->onData(onData, this); + tcp_client->onConnect(onConnect, this); + tcp_client->connect(broker.c_str(), port, ka); #else - if (client->connect(broker.c_str(), port)) + if (tcp_client->connect(broker.c_str(), port)) { debug("link established"); - onConnect(this, client); + onConnect(this, tcp_client); } else { @@ -184,7 +183,7 @@ void MqttBroker::loop() for(size_t i=0; iconnected()) { client->loop(); @@ -290,11 +289,11 @@ void MqttClient::loop() close(); debug(red << "closed"); } - else if (client && client->connected()) + else if (tcp_client && tcp_client->connected()) { debug("pingreq"); uint16_t pingreq = MqttMessage::Type::PingReq; - client->write((const char*)(&pingreq), 2); + tcp_client->write((const char*)(&pingreq), 2); clientAlive(0); // TODO when many MqttClient passes through a local broker @@ -302,9 +301,9 @@ void MqttClient::loop() } } #ifndef TINY_MQTT_ASYNC - while(client && client->available()>0) + while(tcp_client && tcp_client->available()>0) { - message.incoming(client->read()); + message.incoming(tcp_client->read()); if (message.type()) { processMessage(&message); @@ -517,11 +516,11 @@ void MqttClient::processMessage(MqttMessage* mesg) case MqttMessage::Type::PingReq: if (!mqtt_connected) break; - if (client) + if (tcp_client) { uint16_t pingreq = MqttMessage::Type::PingResp; debug(cyan << "Ping response to client "); - client->write((const char*)(&pingreq), 2); + tcp_client->write((const char*)(&pingreq), 2); bclose = false; } else @@ -583,9 +582,9 @@ void MqttClient::processMessage(MqttMessage* mesg) case MqttMessage::Type::Publish: #if TINY_MQTT_DEBUG - Console << "publish " << mqtt_connected << '/' << (long) client << endl; + Console << "publish " << mqtt_connected << '/' << (long) tcp_client << endl; #endif - if (mqtt_connected or client == nullptr) + if (mqtt_connected or tcp_client == nullptr) { uint8_t qos = mesg->flags(); payload = header; @@ -601,7 +600,7 @@ void MqttClient::processMessage(MqttMessage* mesg) // TODO reset DUP // TODO reset RETAIN - if (local_broker==nullptr or client==nullptr) // internal MqttClient receives publish + if (local_broker==nullptr or tcp_client==nullptr) // internal MqttClient receives publish { #if TINY_MQTT_DEBUG if (TinyMqtt::debug >= 2) @@ -727,7 +726,7 @@ MqttError MqttClient::publish(const Topic& topic, const char* payload, size_t pa { return local_broker->publish(this, topic, msg); } - else if (client) + else if (tcp_client) return msg.sendTo(this); else return MqttNowhereToSend; @@ -741,7 +740,7 @@ MqttError MqttClient::publishIfSubscribed(const Topic& topic, MqttMessage& msg) debug("mqttclient publishIfSubscribed " << topic.c_str() << ' ' << subscriptions.size()); if (isSubscribedTo(topic)) { - if (client) + if (tcp_client) retval = msg.sendTo(this); else { diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 8980d9b..a035990 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -193,12 +193,13 @@ class MqttClient // no negociation occurred bool connected() { - return (local_broker!=nullptr and client==nullptr) or (client and client->connected()); + return (local_broker!=nullptr and tcp_client==nullptr) + or (tcp_client and tcp_client->connected()); } void write(const char* buf, size_t length) { - if (client) client->write(buf, length); + if (tcp_client) tcp_client->write(buf, length); } const std::string& id() const { return clientId; } @@ -229,7 +230,7 @@ class MqttClient // connected to local broker // TODO seems to be useless - bool isLocal() const { return client == nullptr; } + bool isLocal() const { return tcp_client == nullptr; } void dump(std::string indent="") { @@ -238,9 +239,9 @@ class MqttClient uint32_t ms=millis(); Console << indent << "+-- " << '\'' << clientId.c_str() << "' " << (connected() ? " ON " : " OFF"); Console << ", alive=" << alive << '/' << ms << ", ka=" << keep_alive << ' '; - if (client) + if (tcp_client) { - if (client->connected()) + if (tcp_client->connected()) Console << TinyConsole::green << "connected"; else Console << TinyConsole::red << "disconnected"; @@ -295,7 +296,7 @@ class MqttClient // when MqttBroker uses MqttClient for each external connexion MqttBroker* local_broker=nullptr; - TcpClient* client=nullptr; // connection to remote broker + TcpClient* tcp_client=nullptr; // connection to remote broker std::set subscriptions; std::string clientId; CallBack callback = nullptr; From 09e3a3e45f875f32e8737615e2faa86d9ad941de Mon Sep 17 00:00:00 2001 From: Francois BIOT Date: Thu, 29 Dec 2022 13:39:34 +0100 Subject: [PATCH 5/5] Rename MqttBroker to remote_broker --- src/TinyMqtt.cpp | 22 +++++++++++----------- src/TinyMqtt.h | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index fc774cf..e357d3a 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -128,9 +128,9 @@ void MqttBroker::addClient(MqttClient* client) void MqttBroker::connect(const std::string& host, uint16_t port) { debug("MqttBroker::connect"); - if (broker == nullptr) broker = new MqttClient; - broker->connect(host, port); - broker->local_broker = this; // Because connect removed the link + if (remote_broker == nullptr) remote_broker = new MqttClient; + remote_broker->connect(host, port); + remote_broker->local_broker = this; // Because connect removed the link } void MqttBroker::removeClient(MqttClient* remove) @@ -174,11 +174,11 @@ void MqttBroker::loop() onClient(this, &client); } #endif - if (broker) + if (remote_broker) { // TODO should monitor broker's activity. // 1 When broker disconnect and reconnect we have to re-subscribe - broker->loop(); + remote_broker->loop(); } for(size_t i=0; iconnected()) + if (remote_broker && remote_broker->connected()) { - return broker->subscribe(topic, qos); + return remote_broker->subscribe(topic, qos); } return MqttNowhereToSend; } @@ -218,19 +218,19 @@ MqttError MqttBroker::publish(const MqttClient* source, const Topic& topic, Mqtt { i++; #if TINY_MQTT_DEBUG - Console << __LINE__ << " broker:" << (broker && broker->connected() ? "linked" : "alone") << + Console << __LINE__ << " broker:" << (remote_broker && remote_broker->connected() ? "linked" : "alone") << " srce=" << (source->isLocal() ? "loc" : "rem") << " clt#" << i << ", local=" << client->isLocal() << ", con=" << client->connected() << endl; #endif bool doit = false; - if (broker && broker->connected()) // this (MqttBroker) is connected (to a external broker) + if (remote_broker && remote_broker->connected()) // this (MqttBroker) is connected (to a external broker) { // ext_broker -> clients or clients -> ext_broker - if (source == broker) // external broker -> internal clients + if (source == remote_broker) // external broker -> internal clients doit = true; else // external clients -> this broker { // As this broker is connected to another broker, simply forward the msg - MqttError ret = broker->publishIfSubscribed(topic, msg); + MqttError ret = remote_broker->publishIfSubscribed(topic, msg); if (ret != MqttOk) retval = ret; } } diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index a035990..c67f236 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -358,7 +358,7 @@ class MqttBroker const char* auth_user = "guest"; const char* auth_password = "guest"; - MqttClient* broker = nullptr; + MqttClient* remote_broker = nullptr; State state = Disconnected; };