Skip to content

Commit

Permalink
[issue #84] Fix and unit test added
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaturn committed May 10, 2023
1 parent ac391a4 commit 90435b1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/TinyMqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ void MqttClient::processMessage(MqttMessage* mesg)
if (mqtt_connected() or tcp_client == nullptr)
{
uint8_t qos = mesg->flags();
qos = (qos / 2) & 3;
payload = header;
mesg->getString(payload, len);
Topic published(payload, len);
Expand All @@ -654,8 +655,19 @@ void MqttClient::processMessage(MqttMessage* mesg)
Console << "Received Publish (" << published.str().c_str() << ") size=" << (int)len << endl;
#endif
// << '(' << string(payload, len).c_str() << ')' << " msglen=" << mesg->length() << endl;
if (qos) payload+=2; // ignore packet identifier if any
const char* ID; // remove PublishID() to avoid misuse
if (qos) {
ID = payload;
payload+=2; // ignore packet identifier if any
}
len=mesg->end()-payload;
if (qos == 1)
{
MqttMessage msg(MqttMessage::Type::PubAck);
msg.add(ID[0]); // MessageID high
msg.add(ID[1]); // MessageID low
msg.sendTo(this);
}
// TODO reset DUP
// TODO reset RETAIN

Expand Down
33 changes: 33 additions & 0 deletions tests/network-tests/network-tests.ino
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,39 @@ test(retained_message)
assertEqual(published.size(), (size_t)2);
}

test(retained_payload) // # issue #84
{
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, "sender");

// 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("receiver");
remote_client.connect(broker_ip, 1883);
remote_client.setCallback(onPublish);
remote_client.subscribe("#");

assertTrue(remote_client.connected());
for(int i=0; i<10; i++) { broker.loop(); local_client.loop(); remote_client.loop(); };

// Check that the retained message is published
assertEqual(lastPayload, "retained");
}

test(remote_client_disconnect_reconnect)
{
published.clear();
Expand Down

0 comments on commit 90435b1

Please sign in to comment.