Skip to content

Commit

Permalink
Merge branch 'main' into timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaturn committed Mar 23, 2023
2 parents e41452e + 088071d commit 5d07294
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 33 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -64,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.
Expand Down
6 changes: 3 additions & 3 deletions bump_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand All @@ -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=""
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions examples/simple-broker/simple-broker.ino
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -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 = "";
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-client/simple-client.ino
Original file line number Diff line number Diff line change
@@ -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)
*
*
* +--------+
Expand Down
2 changes: 1 addition & 1 deletion src/TinyMqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,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
Expand Down
2 changes: 2 additions & 0 deletions src/TinyMqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile.opts
Original file line number Diff line number Diff line change
@@ -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

Expand Down
24 changes: 22 additions & 2 deletions tests/local-tests/local-tests.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ MqttBroker broker(1883);

std::map<string, std::map<Topic, int>> 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;
}

Expand All @@ -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");
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 5d07294

Please sign in to comment.