Skip to content

Commit aed7ffc

Browse files
UHSL-Marcusmarvinroger
authored andcommitted
✨ Add ability to publish DUP packets (marvinroger#61)
* Added ability to publish DUP packets * Added DUP retry feature to subscibe messages * Reverted previous commit * Revert "Reverted previous commit" This reverts commit 337314b. * Revert "Added DUP retry feature to subscibe messages" This reverts commit 3f7170c. * Updated docs
1 parent 6bb7099 commit aed7ffc

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

docs/2.-API-reference.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Return the packet ID or 0 if failed.
145145

146146
* **`topic`**: Topic
147147

148-
#### uint16_t publish(const char\* `topic`, uint8_t `qos`, bool `retain`, const char\* `payload` = nullptr, size_t `length` = 0)
148+
#### uint16_t publish(const char\* `topic`, uint8_t `qos`, bool `retain`, const char\* `payload` = nullptr, size_t `length` = 0, bool dup = false, uint16_t message_id = 0)
149149

150150
Publish a packet.
151151

@@ -156,3 +156,5 @@ Return the packet ID (or 1 if QoS 0) or 0 if failed.
156156
* **`retain`**: Retain flag
157157
* **`payload`**: Payload. If unset, the payload will be empty
158158
* **`length`**: Payload length. If unset or set to 0, the payload will be considered as a string and its size will be calculated using `strlen(payload)`
159+
* **`dup`**: Duplicate flag. If set or set to 1, the payload will be flagged as a duplicate
160+
* **`message_id`**: The message ID. If unset or set to 0, the message ID will be automtaically assigned. Use this with the DUP flag to identify which message is being duplicated

src/AsyncMqttClient.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -786,13 +786,13 @@ uint16_t AsyncMqttClient::unsubscribe(const char* topic) {
786786
return packetId;
787787
}
788788

789-
uint16_t AsyncMqttClient::publish(const char* topic, uint8_t qos, bool retain, const char* payload, size_t length) {
789+
uint16_t AsyncMqttClient::publish(const char* topic, uint8_t qos, bool retain, const char* payload, size_t length, bool dup, uint16_t message_id) {
790790
if (!_connected) return 0;
791791

792792
char fixedHeader[5];
793793
fixedHeader[0] = AsyncMqttClientInternals::PacketType.PUBLISH;
794794
fixedHeader[0] = fixedHeader[0] << 4;
795-
fixedHeader[0] = fixedHeader[0] | AsyncMqttClientInternals::HeaderFlag.CONNECT_RESERVED;
795+
fixedHeader[0] = fixedHeader[0] | (dup ? 0x01 : 0x00);
796796
if (retain) fixedHeader[0] |= AsyncMqttClientInternals::HeaderFlag.PUBLISH_RETAIN;
797797
switch (qos) {
798798
case 0:
@@ -829,7 +829,11 @@ uint16_t AsyncMqttClient::publish(const char* topic, uint8_t qos, bool retain, c
829829
uint16_t packetId = 0;
830830
char packetIdBytes[2];
831831
if (qos != 0) {
832-
packetId = _getNextPacketId();
832+
if (dup && message_id > 0) {
833+
packetId = message_id;
834+
} else {
835+
packetId = _getNextPacketId();
836+
}
833837
packetIdBytes[0] = packetId >> 8;
834838
packetIdBytes[1] = packetId & 0xFF;
835839
}

src/AsyncMqttClient.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class AsyncMqttClient {
6767
void disconnect(bool force = false);
6868
uint16_t subscribe(const char* topic, uint8_t qos);
6969
uint16_t unsubscribe(const char* topic);
70-
uint16_t publish(const char* topic, uint8_t qos, bool retain, const char* payload = nullptr, size_t length = 0);
70+
uint16_t publish(const char* topic, uint8_t qos, bool retain, const char* payload = nullptr, size_t length = 0, bool dup = false, uint16_t message_id = 0);
7171

7272
private:
7373
AsyncClient _client;

0 commit comments

Comments
 (0)