Adds retain support for publishing messages

This commit is contained in:
Lucas Jandrew 2020-02-19 19:15:27 -05:00
parent 3693eb8800
commit 259cba0837
2 changed files with 9 additions and 9 deletions

View File

@ -297,13 +297,13 @@ bool Adafruit_MQTT::disconnect() {
} }
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) { bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos, bool retain) {
return publish(topic, (uint8_t*)(data), strlen(data), qos); return publish(topic, (uint8_t*)(data), strlen(data), qos, retain);
} }
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos) { bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos, bool retain) {
// Construct and send publish packet. // Construct and send publish packet.
uint16_t len = publishPacket(buffer, topic, data, bLen, qos); uint16_t len = publishPacket(buffer, topic, data, bLen, qos, retain);
if (!sendPacket(buffer, len)) if (!sendPacket(buffer, len))
return false; return false;
@ -640,7 +640,7 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
// as per http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040 // as per http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic, uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
uint8_t *data, uint16_t bLen, uint8_t qos) { uint8_t *data, uint16_t bLen, uint8_t qos, bool retain) {
uint8_t *p = packet; uint8_t *p = packet;
uint16_t len=0; uint16_t len=0;
@ -653,7 +653,7 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
len += bLen; // payload length len += bLen; // payload length
// Now you can start generating the packet! // Now you can start generating the packet!
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1; p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1 | retain;
p++; p++;
// fill in packet[1] last // fill in packet[1] last

View File

@ -177,8 +177,8 @@ class Adafruit_MQTT {
// Publish a message to a topic using the specified QoS level. Returns true // Publish a message to a topic using the specified QoS level. Returns true
// if the message was published, false otherwise. // if the message was published, false otherwise.
bool publish(const char *topic, const char *payload, uint8_t qos = 0); bool publish(const char *topic, const char *payload, uint8_t qos = 0, bool retain = false);
bool publish(const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos = 0); bool publish(const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos = 0, bool retain = false);
// Add a subscription to receive messages for a topic. Returns true if the // Add a subscription to receive messages for a topic. Returns true if the
// subscription could be added or was already present, false otherwise. // subscription could be added or was already present, false otherwise.
@ -243,7 +243,7 @@ class Adafruit_MQTT {
// Functions to generate MQTT packets. // Functions to generate MQTT packets.
uint8_t connectPacket(uint8_t *packet); uint8_t connectPacket(uint8_t *packet);
uint8_t disconnectPacket(uint8_t *packet); uint8_t disconnectPacket(uint8_t *packet);
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos); uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos, bool retain);
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos); uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
uint8_t unsubscribePacket(uint8_t *packet, const char *topic); uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
uint8_t pingPacket(uint8_t *packet); uint8_t pingPacket(uint8_t *packet);