Skip to content

Commit 57009da

Browse files
Adafruit_MQTT (API): the payload of publish method is read-only
The payload param provided in publish is a const. This change fixes the API to ensure caller that this is the case for all variations of the publish method.
1 parent 785b5a4 commit 57009da

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

Adafruit_MQTT.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,11 @@ bool Adafruit_MQTT::disconnect() {
311311
}
312312

313313
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
314-
return publish(topic, (uint8_t *)(data), strlen(data), qos);
314+
return publish(topic, (const uint8_t *)(data), strlen(data), qos);
315315
}
316316

317-
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
318-
uint8_t qos) {
317+
bool Adafruit_MQTT::publish(const char *topic, const uint8_t *data,
318+
uint16_t bLen, uint8_t qos) {
319319
// Construct and send publish packet.
320320
uint16_t len =
321321
publishPacket(buffer, topic, data, bLen, qos, (uint16_t)sizeof(buffer));
@@ -680,8 +680,8 @@ uint16_t Adafruit_MQTT::packetAdditionalLen(uint16_t currLen) {
680680
// as per
681681
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
682682
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
683-
uint8_t *data, uint16_t bLen, uint8_t qos,
684-
uint16_t maxPacketLen) {
683+
const uint8_t *data, uint16_t bLen,
684+
uint8_t qos, uint16_t maxPacketLen) {
685685
uint8_t *p = packet;
686686
uint16_t len = 0;
687687

@@ -857,7 +857,7 @@ bool Adafruit_MQTT_Publish::publish(const char *payload) {
857857
}
858858

859859
// publish buffer of arbitrary length
860-
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) {
860+
bool Adafruit_MQTT_Publish::publish(const uint8_t *payload, uint16_t bLen) {
861861

862862
return mqtt->publish(topic, payload, bLen, qos);
863863
}

Adafruit_MQTT.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class Adafruit_MQTT {
187187
// Publish a message to a topic using the specified QoS level. Returns true
188188
// if the message was published, false otherwise.
189189
bool publish(const char *topic, const char *payload, uint8_t qos = 0);
190-
bool publish(const char *topic, uint8_t *payload, uint16_t bLen,
190+
bool publish(const char *topic, const uint8_t *payload, uint16_t bLen,
191191
uint8_t qos = 0);
192192

193193
// Add a subscription to receive messages for a topic. Returns true if the
@@ -258,8 +258,9 @@ class Adafruit_MQTT {
258258
uint8_t connectPacket(uint8_t *packet);
259259
uint8_t disconnectPacket(uint8_t *packet);
260260
static uint16_t packetAdditionalLen(uint16_t currLen);
261-
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
262-
uint16_t bLen, uint8_t qos, uint16_t maxPacketLen);
261+
uint16_t publishPacket(uint8_t *packet, const char *topic,
262+
const uint8_t *payload, uint16_t bLen, uint8_t qos,
263+
uint16_t maxPacketLen);
263264
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
264265
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
265266
uint8_t pingPacket(uint8_t *packet);
@@ -279,7 +280,7 @@ class Adafruit_MQTT_Publish {
279280
// This might be ignored and a higher precision value sent.
280281
bool publish(int32_t i);
281282
bool publish(uint32_t i);
282-
bool publish(uint8_t *b, uint16_t bLen);
283+
bool publish(const uint8_t *b, uint16_t bLen);
283284

284285
private:
285286
Adafruit_MQTT *mqtt;

0 commit comments

Comments
 (0)