Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a0435b2cfb | ||
|
|
bda041417d | ||
|
|
baffda8a6d | ||
|
|
09e3a3e45f | ||
|
|
f17ece3376 | ||
|
|
0db07df27b | ||
|
|
292592c3dd | ||
|
|
1f267c135b |
4
.github/workflows/aunit.yml
vendored
4
.github/workflows/aunit.yml
vendored
@@ -9,7 +9,7 @@ on: [push]
|
|||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
|
||||||
runs-on: ubuntu-18.04
|
runs-on: ubuntu-20.04
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
@@ -18,7 +18,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
cd ..
|
cd ..
|
||||||
git clone https://github.com/hsaturn/TinyConsole
|
git clone https://github.com/hsaturn/TinyConsole
|
||||||
git clone https://github.com/bxparks/EpoxyDuino
|
git clone https://github.com/hsaturn/EpoxyDuino
|
||||||
git clone https://github.com/bxparks/AceRoutine
|
git clone https://github.com/bxparks/AceRoutine
|
||||||
git clone https://github.com/bxparks/AUnit
|
git clone https://github.com/bxparks/AUnit
|
||||||
git clone https://github.com/bxparks/AceCommon
|
git clone https://github.com/bxparks/AceCommon
|
||||||
|
|||||||
@@ -30,7 +30,7 @@
|
|||||||
* - accepts external clients
|
* - accepts external clients
|
||||||
* - MqttClassBinder allows to mix together many mqtt sources
|
* - MqttClassBinder allows to mix together many mqtt sources
|
||||||
*
|
*
|
||||||
* cons - Takes more memory (48 more bytes for the one MqttClassBinder<Class>
|
* cons - Takes more memory (24 more bytes for the one MqttClassBinder<Class>
|
||||||
* - a bit hard to understand
|
* - a bit hard to understand
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
name=TinyMqtt
|
name=TinyMqtt
|
||||||
version=0.9.11
|
version=0.9.12
|
||||||
author=Francois BIOT, HSaturn, <hsaturn@gmail.com>
|
author=Francois BIOT, HSaturn, <hsaturn@gmail.com>
|
||||||
maintainer=Francois BIOT, HSaturn, <hsaturn@gmail.com>
|
maintainer=Francois BIOT, HSaturn, <hsaturn@gmail.com>
|
||||||
sentence=A tiny broker and client library for MQTT messaging.
|
sentence=A tiny broker and client library for MQTT messaging.
|
||||||
|
|||||||
@@ -38,12 +38,12 @@ MqttClient::MqttClient(MqttBroker* local_broker, TcpClient* new_client)
|
|||||||
{
|
{
|
||||||
debug("MqttClient private with broker");
|
debug("MqttClient private with broker");
|
||||||
#ifdef TINY_MQTT_ASYNC
|
#ifdef TINY_MQTT_ASYNC
|
||||||
client = new_client;
|
tcp_client = new_client;
|
||||||
client->onData(onData, this);
|
tcp_client->onData(onData, this);
|
||||||
// client->onConnect() TODO
|
// client->onConnect() TODO
|
||||||
// client->onDisconnect() TODO
|
// client->onDisconnect() TODO
|
||||||
#else
|
#else
|
||||||
client = new WiFiClient(*new_client);
|
tcp_client = new WiFiClient(*new_client);
|
||||||
#endif
|
#endif
|
||||||
#ifdef EPOXY_DUINO
|
#ifdef EPOXY_DUINO
|
||||||
alive = millis()+500000;
|
alive = millis()+500000;
|
||||||
@@ -55,30 +55,32 @@ MqttClient::MqttClient(MqttBroker* local_broker, TcpClient* new_client)
|
|||||||
MqttClient::MqttClient(MqttBroker* local_broker, const std::string& id)
|
MqttClient::MqttClient(MqttBroker* local_broker, const std::string& id)
|
||||||
: local_broker(local_broker), clientId(id)
|
: local_broker(local_broker), clientId(id)
|
||||||
{
|
{
|
||||||
client = nullptr;
|
alive = 0;
|
||||||
|
keep_alive = 0;
|
||||||
|
|
||||||
if (local_broker) local_broker->addClient(this);
|
if (local_broker) local_broker->addClient(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
MqttClient::~MqttClient()
|
MqttClient::~MqttClient()
|
||||||
{
|
{
|
||||||
close();
|
close();
|
||||||
delete client;
|
delete tcp_client;
|
||||||
|
debug("*** MqttClient delete()");
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttClient::close(bool bSendDisconnect)
|
void MqttClient::close(bool bSendDisconnect)
|
||||||
{
|
{
|
||||||
debug("close " << id().c_str());
|
debug("close " << id().c_str());
|
||||||
mqtt_connected = false;
|
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.create(MqttMessage::Type::Disconnect);
|
||||||
message.hexdump("close");
|
message.hexdump("close");
|
||||||
message.sendTo(this);
|
message.sendTo(this);
|
||||||
}
|
}
|
||||||
client->stop();
|
tcp_client->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (local_broker)
|
if (local_broker)
|
||||||
@@ -100,18 +102,18 @@ void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka)
|
|||||||
debug("MqttClient::connect_to_host " << broker << ':' << port);
|
debug("MqttClient::connect_to_host " << broker << ':' << port);
|
||||||
keep_alive = ka;
|
keep_alive = ka;
|
||||||
close();
|
close();
|
||||||
if (client) delete client;
|
if (tcp_client) delete tcp_client;
|
||||||
client = new TcpClient;
|
tcp_client = new TcpClient;
|
||||||
|
|
||||||
#ifdef TINY_MQTT_ASYNC
|
#ifdef TINY_MQTT_ASYNC
|
||||||
client->onData(onData, this);
|
tcp_client->onData(onData, this);
|
||||||
client->onConnect(onConnect, this);
|
tcp_client->onConnect(onConnect, this);
|
||||||
client->connect(broker.c_str(), port, ka);
|
tcp_client->connect(broker.c_str(), port, ka);
|
||||||
#else
|
#else
|
||||||
if (client->connect(broker.c_str(), port))
|
if (tcp_client->connect(broker.c_str(), port))
|
||||||
{
|
{
|
||||||
debug("link established");
|
debug("link established");
|
||||||
onConnect(this, client);
|
onConnect(this, tcp_client);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -129,9 +131,9 @@ void MqttBroker::addClient(MqttClient* client)
|
|||||||
void MqttBroker::connect(const std::string& host, uint16_t port)
|
void MqttBroker::connect(const std::string& host, uint16_t port)
|
||||||
{
|
{
|
||||||
debug("MqttBroker::connect");
|
debug("MqttBroker::connect");
|
||||||
if (broker == nullptr) broker = new MqttClient;
|
if (remote_broker == nullptr) remote_broker = new MqttClient;
|
||||||
broker->connect(host, port);
|
remote_broker->connect(host, port);
|
||||||
broker->local_broker = this; // Because connect removed the link
|
remote_broker->local_broker = this; // Because connect removed the link
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttBroker::removeClient(MqttClient* remove)
|
void MqttBroker::removeClient(MqttClient* remove)
|
||||||
@@ -175,19 +177,16 @@ void MqttBroker::loop()
|
|||||||
onClient(this, &client);
|
onClient(this, &client);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (broker)
|
if (remote_broker)
|
||||||
{
|
{
|
||||||
// TODO should monitor broker's activity.
|
// TODO should monitor broker's activity.
|
||||||
// 1 When broker disconnect and reconnect we have to re-subscribe
|
// 1 When broker disconnect and reconnect we have to re-subscribe
|
||||||
broker->loop();
|
remote_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<clients.size(); i++)
|
for(size_t i=0; i<clients.size(); i++)
|
||||||
{
|
{
|
||||||
auto client = clients[i];
|
MqttClient* client = clients[i];
|
||||||
if (client->connected())
|
if (client->connected())
|
||||||
{
|
{
|
||||||
client->loop();
|
client->loop();
|
||||||
@@ -205,9 +204,9 @@ void MqttBroker::loop()
|
|||||||
MqttError MqttBroker::subscribe(const Topic& topic, uint8_t qos)
|
MqttError MqttBroker::subscribe(const Topic& topic, uint8_t qos)
|
||||||
{
|
{
|
||||||
debug("MqttBroker::subscribe");
|
debug("MqttBroker::subscribe");
|
||||||
if (broker && broker->connected())
|
if (remote_broker && remote_broker->connected())
|
||||||
{
|
{
|
||||||
return broker->subscribe(topic, qos);
|
return remote_broker->subscribe(topic, qos);
|
||||||
}
|
}
|
||||||
return MqttNowhereToSend;
|
return MqttNowhereToSend;
|
||||||
}
|
}
|
||||||
@@ -222,19 +221,19 @@ MqttError MqttBroker::publish(const MqttClient* source, const Topic& topic, Mqtt
|
|||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
#if TINY_MQTT_DEBUG
|
#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;
|
" srce=" << (source->isLocal() ? "loc" : "rem") << " clt#" << i << ", local=" << client->isLocal() << ", con=" << client->connected() << endl;
|
||||||
#endif
|
#endif
|
||||||
bool doit = false;
|
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
|
// ext_broker -> clients or clients -> ext_broker
|
||||||
if (source == broker) // external broker -> internal clients
|
if (source == remote_broker) // external broker -> internal clients
|
||||||
doit = true;
|
doit = true;
|
||||||
else // external clients -> this broker
|
else // external clients -> this broker
|
||||||
{
|
{
|
||||||
// As this broker is connected to another broker, simply forward the msg
|
// 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;
|
if (ret != MqttOk) retval = ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -285,7 +284,7 @@ void MqttClient::clientAlive(uint32_t more_seconds)
|
|||||||
|
|
||||||
void MqttClient::loop()
|
void MqttClient::loop()
|
||||||
{
|
{
|
||||||
if (alive && (millis() > alive))
|
if (keep_alive && (millis() >= alive))
|
||||||
{
|
{
|
||||||
if (local_broker)
|
if (local_broker)
|
||||||
{
|
{
|
||||||
@@ -293,11 +292,11 @@ void MqttClient::loop()
|
|||||||
close();
|
close();
|
||||||
debug(red << "closed");
|
debug(red << "closed");
|
||||||
}
|
}
|
||||||
else if (client && client->connected())
|
else if (tcp_client && tcp_client->connected())
|
||||||
{
|
{
|
||||||
debug("pingreq");
|
debug("pingreq");
|
||||||
uint16_t pingreq = MqttMessage::Type::PingReq;
|
uint16_t pingreq = MqttMessage::Type::PingReq;
|
||||||
client->write((const char*)(&pingreq), 2);
|
tcp_client->write((const char*)(&pingreq), 2);
|
||||||
clientAlive(0);
|
clientAlive(0);
|
||||||
|
|
||||||
// TODO when many MqttClient passes through a local broker
|
// TODO when many MqttClient passes through a local broker
|
||||||
@@ -305,9 +304,9 @@ void MqttClient::loop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifndef TINY_MQTT_ASYNC
|
#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())
|
if (message.type())
|
||||||
{
|
{
|
||||||
processMessage(&message);
|
processMessage(&message);
|
||||||
@@ -520,11 +519,11 @@ void MqttClient::processMessage(MqttMessage* mesg)
|
|||||||
|
|
||||||
case MqttMessage::Type::PingReq:
|
case MqttMessage::Type::PingReq:
|
||||||
if (!mqtt_connected) break;
|
if (!mqtt_connected) break;
|
||||||
if (client)
|
if (tcp_client)
|
||||||
{
|
{
|
||||||
uint16_t pingreq = MqttMessage::Type::PingResp;
|
uint16_t pingreq = MqttMessage::Type::PingResp;
|
||||||
debug(cyan << "Ping response to client ");
|
debug(cyan << "Ping response to client ");
|
||||||
client->write((const char*)(&pingreq), 2);
|
tcp_client->write((const char*)(&pingreq), 2);
|
||||||
bclose = false;
|
bclose = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -586,9 +585,9 @@ void MqttClient::processMessage(MqttMessage* mesg)
|
|||||||
|
|
||||||
case MqttMessage::Type::Publish:
|
case MqttMessage::Type::Publish:
|
||||||
#if TINY_MQTT_DEBUG
|
#if TINY_MQTT_DEBUG
|
||||||
Console << "publish " << mqtt_connected << '/' << (long) client << endl;
|
Console << "publish " << mqtt_connected << '/' << (long) tcp_client << endl;
|
||||||
#endif
|
#endif
|
||||||
if (mqtt_connected or client == nullptr)
|
if (mqtt_connected or tcp_client == nullptr)
|
||||||
{
|
{
|
||||||
uint8_t qos = mesg->flags();
|
uint8_t qos = mesg->flags();
|
||||||
payload = header;
|
payload = header;
|
||||||
@@ -604,7 +603,7 @@ void MqttClient::processMessage(MqttMessage* mesg)
|
|||||||
// TODO reset DUP
|
// TODO reset DUP
|
||||||
// TODO reset RETAIN
|
// 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 TINY_MQTT_DEBUG
|
||||||
if (TinyMqtt::debug >= 2)
|
if (TinyMqtt::debug >= 2)
|
||||||
@@ -730,7 +729,7 @@ MqttError MqttClient::publish(const Topic& topic, const char* payload, size_t pa
|
|||||||
{
|
{
|
||||||
return local_broker->publish(this, topic, msg);
|
return local_broker->publish(this, topic, msg);
|
||||||
}
|
}
|
||||||
else if (client)
|
else if (tcp_client)
|
||||||
return msg.sendTo(this);
|
return msg.sendTo(this);
|
||||||
else
|
else
|
||||||
return MqttNowhereToSend;
|
return MqttNowhereToSend;
|
||||||
@@ -744,7 +743,7 @@ MqttError MqttClient::publishIfSubscribed(const Topic& topic, MqttMessage& msg)
|
|||||||
debug("mqttclient publishIfSubscribed " << topic.c_str() << ' ' << subscriptions.size());
|
debug("mqttclient publishIfSubscribed " << topic.c_str() << ' ' << subscriptions.size());
|
||||||
if (isSubscribedTo(topic))
|
if (isSubscribedTo(topic))
|
||||||
{
|
{
|
||||||
if (client)
|
if (tcp_client)
|
||||||
retval = msg.sendTo(this);
|
retval = msg.sendTo(this);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -193,12 +193,13 @@ class MqttClient
|
|||||||
// no negociation occurred
|
// no negociation occurred
|
||||||
bool connected()
|
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)
|
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; }
|
const std::string& id() const { return clientId; }
|
||||||
@@ -229,7 +230,7 @@ class MqttClient
|
|||||||
|
|
||||||
// connected to local broker
|
// connected to local broker
|
||||||
// TODO seems to be useless
|
// TODO seems to be useless
|
||||||
bool isLocal() const { return client == nullptr; }
|
bool isLocal() const { return tcp_client == nullptr; }
|
||||||
|
|
||||||
void dump(std::string indent="")
|
void dump(std::string indent="")
|
||||||
{
|
{
|
||||||
@@ -238,9 +239,9 @@ class MqttClient
|
|||||||
uint32_t ms=millis();
|
uint32_t ms=millis();
|
||||||
Console << indent << "+-- " << '\'' << clientId.c_str() << "' " << (connected() ? " ON " : " OFF");
|
Console << indent << "+-- " << '\'' << clientId.c_str() << "' " << (connected() ? " ON " : " OFF");
|
||||||
Console << ", alive=" << alive << '/' << ms << ", ka=" << keep_alive << ' ';
|
Console << ", alive=" << alive << '/' << ms << ", ka=" << keep_alive << ' ';
|
||||||
if (client)
|
if (tcp_client)
|
||||||
{
|
{
|
||||||
if (client->connected())
|
if (tcp_client->connected())
|
||||||
Console << TinyConsole::green << "connected";
|
Console << TinyConsole::green << "connected";
|
||||||
else
|
else
|
||||||
Console << TinyConsole::red << "disconnected";
|
Console << TinyConsole::red << "disconnected";
|
||||||
@@ -295,7 +296,7 @@ class MqttClient
|
|||||||
// when MqttBroker uses MqttClient for each external connexion
|
// when MqttBroker uses MqttClient for each external connexion
|
||||||
MqttBroker* local_broker=nullptr;
|
MqttBroker* local_broker=nullptr;
|
||||||
|
|
||||||
TcpClient* client=nullptr; // connection to remote broker
|
TcpClient* tcp_client=nullptr; // connection to remote broker
|
||||||
std::set<Topic> subscriptions;
|
std::set<Topic> subscriptions;
|
||||||
std::string clientId;
|
std::string clientId;
|
||||||
CallBack callback = nullptr;
|
CallBack callback = nullptr;
|
||||||
@@ -357,7 +358,7 @@ class MqttBroker
|
|||||||
|
|
||||||
const char* auth_user = "guest";
|
const char* auth_user = "guest";
|
||||||
const char* auth_password = "guest";
|
const char* auth_password = "guest";
|
||||||
MqttClient* broker = nullptr;
|
MqttClient* remote_broker = nullptr;
|
||||||
|
|
||||||
State state = Disconnected;
|
State state = Disconnected;
|
||||||
};
|
};
|
||||||
|
|||||||
13
tests/classbind-tests/Makefile
Normal file
13
tests/classbind-tests/Makefile
Normal file
@@ -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 <optimized out>)
|
||||||
|
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
|
||||||
@@ -40,6 +40,34 @@ test(local_client_should_unregister_when_destroyed)
|
|||||||
assertEqual(broker.clientsCount(), (size_t)0);
|
assertEqual(broker.clientsCount(), (size_t)0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test(local_client_do_not_disconnect_after_publishing)
|
||||||
|
{
|
||||||
|
set_millis(0);
|
||||||
|
MqttBroker broker(1883);
|
||||||
|
MqttClient client(&broker, "client");
|
||||||
|
MqttClient sender(&broker, "sender");
|
||||||
|
broker.loop();
|
||||||
|
|
||||||
|
client.subscribe("#");
|
||||||
|
client.subscribe("test");
|
||||||
|
client.setCallback(onPublish);
|
||||||
|
assertEqual(broker.clientsCount(), (size_t)2);
|
||||||
|
|
||||||
|
sender.publish("test", "value");
|
||||||
|
broker.loop();
|
||||||
|
|
||||||
|
add_seconds(60);
|
||||||
|
client.loop();
|
||||||
|
sender.loop();
|
||||||
|
broker.loop();
|
||||||
|
|
||||||
|
assertEqual(broker.clientsCount(), (size_t)2);
|
||||||
|
assertEqual(sender.connected(), true);
|
||||||
|
assertEqual(client.connected(), true);
|
||||||
|
|
||||||
|
assertEqual(published.size(), (size_t)1); // client has received something
|
||||||
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
test(local_connect)
|
test(local_connect)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user