Skip to content

Commit d3fdbb2

Browse files
committed
Use function overloading instead of optional arguments
1 parent de5059e commit d3fdbb2

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

Adafruit_MQTT.cpp

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,24 @@ bool Adafruit_MQTT::disconnect() {
366366
}
367367

368368
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
369-
return publish(topic, (uint8_t *)(data), strlen(data), qos);
369+
return publish(topic, (uint8_t*)(data), strlen(data), false, qos);
370370
}
371371

372372
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
373373
uint8_t qos) {
374+
return publish(topic, data, bLen, false, qos);
375+
}
376+
377+
bool Adafruit_MQTT::publish(const char *topic, const char *data,
378+
bool retain, uint8_t qos) {
379+
return publish(topic, (uint8_t*)(data), strlen(data), retain, qos);
380+
}
381+
382+
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
383+
bool retain, uint8_t qos) {
374384
// Construct and send publish packet.
375-
uint16_t len =
376-
publishPacket(buffer, topic, data, bLen, qos, (uint16_t)sizeof(buffer));
385+
uint16_t len = publishPacket(buffer, topic, data, bLen, retain, qos, (uint16_t)sizeof(buffer));
386+
377387
if (!sendPacket(buffer, len))
378388
return false;
379389

@@ -753,6 +763,12 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
753763
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
754764
uint8_t *data, uint16_t bLen, uint8_t qos,
755765
uint16_t maxPacketLen) {
766+
return publishPacket(packet, topic, data, bLen, false, qos, maxPacketLen);
767+
}
768+
769+
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
770+
uint8_t *data, uint16_t bLen, bool retain,
771+
uint8_t qos, uint16_t maxPacketLen) {
756772
uint8_t *p = packet;
757773
uint16_t len = 0;
758774

@@ -907,33 +923,54 @@ Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,
907923
topic = feed;
908924
qos = q;
909925
}
926+
910927
bool Adafruit_MQTT_Publish::publish(int32_t i) {
928+
return publish(i, false);
929+
}
930+
931+
bool Adafruit_MQTT_Publish::publish(int32_t i, bool retain) {
911932
char payload[12];
912933
ltoa(i, payload, 10);
913-
return mqtt->publish(topic, payload, qos);
934+
return mqtt->publish(topic, payload, retain, qos);
914935
}
915936

916937
bool Adafruit_MQTT_Publish::publish(uint32_t i) {
938+
return publish(i, false);
939+
}
940+
941+
bool Adafruit_MQTT_Publish::publish(uint32_t i, bool retain) {
917942
char payload[11];
918943
ultoa(i, payload, 10);
919-
return mqtt->publish(topic, payload, qos);
944+
return mqtt->publish(topic, payload, retain, qos);
920945
}
921946

922947
bool Adafruit_MQTT_Publish::publish(double f, uint8_t precision) {
923-
char payload[41]; // Need to technically hold float max, 39 digits and minus
924-
// sign.
948+
return publish(f, false, precision);
949+
}
950+
951+
bool Adafruit_MQTT_Publish::publish(double f, bool retain, uint8_t precision) {
952+
char payload[41]; // Need to technically hold float max, 39 digits and minus
953+
// sign.
925954
dtostrf(f, 0, precision, payload);
926-
return mqtt->publish(topic, payload, qos);
955+
return mqtt->publish(topic, payload, retain, qos);
927956
}
928957

929958
bool Adafruit_MQTT_Publish::publish(const char *payload) {
930-
return mqtt->publish(topic, payload, qos);
959+
return publish(payload, false);
960+
}
961+
962+
bool Adafruit_MQTT_Publish::publish(const char *payload, bool retain) {
963+
return mqtt->publish(topic, payload, retain, qos);
931964
}
932965

933966
// publish buffer of arbitrary length
934967
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) {
968+
return publish(payload, bLen, false);
969+
}
970+
971+
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen, bool retain) {
935972

936-
return mqtt->publish(topic, payload, bLen, qos);
973+
return mqtt->publish(topic, payload, bLen, retain, qos);
937974
}
938975

939976
// Adafruit_MQTT_Subscribe Definition //////////////////////////////////////////

Adafruit_MQTT.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ class Adafruit_MQTT {
192192
bool publish(const char *topic, const char *payload, uint8_t qos = 0);
193193
bool publish(const char *topic, uint8_t *payload, uint16_t bLen,
194194
uint8_t qos = 0);
195+
bool publish(const char *topic, const char *payload, bool retain, uint8_t qos = 0);
196+
bool publish(const char *topic, uint8_t *payload, uint16_t bLen, bool retain, uint8_t qos = 0);
195197

196198
// Add a subscription to receive messages for a topic. Returns true if the
197199
// subscription could be added or was already present, false otherwise.
@@ -268,8 +270,10 @@ class Adafruit_MQTT {
268270
uint8_t connectPacket(uint8_t *packet);
269271
uint8_t disconnectPacket(uint8_t *packet);
270272
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
271-
uint16_t bLen, uint8_t qos, uint16_t maxPacketLen = 0,
272-
bool retain = false);
273+
uint16_t bLen, uint8_t qos, uint16_t maxPacketLen = 0);
274+
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
275+
uint16_t bLen, bool retain, uint8_t qos, uint16_t maxPacketLen = 0);
276+
273277
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
274278
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
275279
uint8_t pingPacket(uint8_t *packet);
@@ -282,14 +286,19 @@ class Adafruit_MQTT_Publish {
282286
uint8_t qos = 0);
283287

284288
bool publish(const char *s);
289+
bool publish(const char *s, bool retain);
285290
bool publish(
286291
double f,
287292
uint8_t precision =
288293
2); // Precision controls the minimum number of digits after decimal.
289294
// This might be ignored and a higher precision value sent.
295+
bool publish(double f, bool retain, uint8_t precision = 2);
290296
bool publish(int32_t i);
297+
bool publish(int32_t i, bool retain);
291298
bool publish(uint32_t i);
299+
bool publish(uint32_t i, bool retain);
292300
bool publish(uint8_t *b, uint16_t bLen);
301+
bool publish(uint8_t *b, uint16_t bLen, bool retain);
293302

294303
private:
295304
Adafruit_MQTT *mqtt;

0 commit comments

Comments
 (0)