Added retain flag for publishing

This commit is contained in:
Assaf Inbal 2017-01-02 09:11:56 +02:00 committed by Mathieu Maret
parent 7413e92e77
commit af01dcb7da
2 changed files with 18 additions and 16 deletions

View File

@ -337,15 +337,15 @@ bool Adafruit_MQTT::disconnect() {
return disconnectServer(); return disconnectServer();
} }
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, uint8_t 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, bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
uint8_t qos) { uint8_t qos, uint8_t retain) {
// Construct and send publish packet. // Construct and send publish packet.
uint16_t len = uint16_t len =
publishPacket(buffer, topic, data, bLen, qos, (uint16_t)sizeof(buffer)); publishPacket(buffer, topic, data, bLen, qos, (uint16_t)sizeof(buffer), retain);
if (!sendPacket(buffer, len)) if (!sendPacket(buffer, len))
return false; return false;
@ -743,7 +743,7 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040 // 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,
uint16_t maxPacketLen) { uint16_t maxPacketLen, uint8_t retain) {
uint8_t *p = packet; uint8_t *p = packet;
uint16_t len = 0; uint16_t len = 0;
@ -773,7 +773,7 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
len += bLen; // remaining len excludes header byte & length field len += bLen; // remaining len excludes header byte & length field
// 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 ? 1 : 0);
p++; p++;
// fill in packet[1] last // fill in packet[1] last
@ -893,38 +893,39 @@ uint8_t Adafruit_MQTT::disconnectPacket(uint8_t *packet) {
// Adafruit_MQTT_Publish Definition //////////////////////////////////////////// // Adafruit_MQTT_Publish Definition ////////////////////////////////////////////
Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,
const char *feed, uint8_t q) { const char *feed, uint8_t q, uint8_t r) {
mqtt = mqttserver; mqtt = mqttserver;
topic = feed; topic = feed;
qos = q; qos = q;
retain = r;
} }
bool Adafruit_MQTT_Publish::publish(int32_t i) { bool Adafruit_MQTT_Publish::publish(int32_t i) {
char payload[12]; char payload[12];
ltoa(i, payload, 10); ltoa(i, payload, 10);
return mqtt->publish(topic, payload, qos); return mqtt->publish(topic, payload, qos, retain);
} }
bool Adafruit_MQTT_Publish::publish(uint32_t i) { bool Adafruit_MQTT_Publish::publish(uint32_t i) {
char payload[11]; char payload[11];
ultoa(i, payload, 10); ultoa(i, payload, 10);
return mqtt->publish(topic, payload, qos); return mqtt->publish(topic, payload, qos, retain);
} }
bool Adafruit_MQTT_Publish::publish(double f, uint8_t precision) { bool Adafruit_MQTT_Publish::publish(double f, uint8_t precision) {
char payload[41]; // Need to technically hold float max, 39 digits and minus char payload[41]; // Need to technically hold float max, 39 digits and minus
// sign. // sign.
dtostrf(f, 0, precision, payload); dtostrf(f, 0, precision, payload);
return mqtt->publish(topic, payload, qos); return mqtt->publish(topic, payload, qos, retain);
} }
bool Adafruit_MQTT_Publish::publish(const char *payload) { bool Adafruit_MQTT_Publish::publish(const char *payload) {
return mqtt->publish(topic, payload, qos); return mqtt->publish(topic, payload, qos, retain);
} }
// publish buffer of arbitrary length // publish buffer of arbitrary length
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) { bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) {
return mqtt->publish(topic, payload, bLen, qos); return mqtt->publish(topic, payload, bLen, qos, retain);
} }
// Adafruit_MQTT_Subscribe Definition ////////////////////////////////////////// // Adafruit_MQTT_Subscribe Definition //////////////////////////////////////////

View File

@ -189,9 +189,9 @@ public:
// 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, uint8_t retain = 0);
bool publish(const char *topic, uint8_t *payload, uint16_t bLen, bool publish(const char *topic, uint8_t *payload, uint16_t bLen,
uint8_t qos = 0); uint8_t qos = 0, uint8_t retain = 0);
// 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.
@ -266,7 +266,7 @@ private:
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 publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
uint16_t bLen, uint8_t qos, uint16_t maxPacketLen = 0); uint16_t bLen, uint8_t qos, uint16_t maxPacketLen = 0, uint8_t retain = 0);
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);
@ -276,7 +276,7 @@ private:
class Adafruit_MQTT_Publish { class Adafruit_MQTT_Publish {
public: public:
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed,
uint8_t qos = 0); uint8_t qos = 0, uint8_t retain = 0);
bool publish(const char *s); bool publish(const char *s);
bool publish( bool publish(
@ -292,6 +292,7 @@ private:
Adafruit_MQTT *mqtt; Adafruit_MQTT *mqtt;
const char *topic; const char *topic;
uint8_t qos; uint8_t qos;
uint8_t retain;
}; };
class Adafruit_MQTT_Subscribe { class Adafruit_MQTT_Subscribe {