Skip to content

Commit a780728

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 4204e1d commit a780728

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

Adafruit_MQTT.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,10 @@ bool Adafruit_MQTT::disconnect() {
298298

299299

300300
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
301-
return publish(topic, (uint8_t*)(data), strlen(data), qos);
301+
return publish(topic, (const uint8_t*)(data), strlen(data), qos);
302302
}
303303

304-
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos) {
304+
bool Adafruit_MQTT::publish(const char *topic, const uint8_t *data, uint16_t bLen, uint8_t qos) {
305305
// Construct and send publish packet.
306306
uint16_t len = publishPacket(buffer, (uint16_t) sizeof(buffer), topic, data, bLen, qos);
307307
if (!sendPacket(buffer, len))
@@ -648,7 +648,7 @@ uint16_t Adafruit_MQTT::packetAdditionalLen(uint16_t currLen)
648648

649649
// as per http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
650650
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, uint16_t maxPacketLen, const char *topic,
651-
uint8_t *data, uint16_t bLen, uint8_t qos) {
651+
const uint8_t *data, uint16_t bLen, uint8_t qos) {
652652
uint8_t *p = packet;
653653
uint16_t len=2; // control + length
654654

@@ -825,7 +825,7 @@ bool Adafruit_MQTT_Publish::publish(const char *payload) {
825825
}
826826

827827
//publish buffer of arbitrary length
828-
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) {
828+
bool Adafruit_MQTT_Publish::publish(const uint8_t *payload, uint16_t bLen) {
829829

830830
return mqtt->publish(topic, payload, bLen, qos);
831831
}

Adafruit_MQTT.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class Adafruit_MQTT {
178178
// Publish a message to a topic using the specified QoS level. Returns true
179179
// if the message was published, false otherwise.
180180
bool publish(const char *topic, const char *payload, uint8_t qos = 0);
181-
bool publish(const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos = 0);
181+
bool publish(const char *topic, const uint8_t *payload, uint16_t bLen, uint8_t qos = 0);
182182

183183
// Add a subscription to receive messages for a topic. Returns true if the
184184
// subscription could be added or was already present, false otherwise.
@@ -244,7 +244,7 @@ class Adafruit_MQTT {
244244
uint8_t connectPacket(uint8_t *packet);
245245
uint8_t disconnectPacket(uint8_t *packet);
246246
uint16_t publishPacket(uint8_t *packet, uint16_t maxPacketLen,
247-
const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos);
247+
const char *topic, const uint8_t *payload, uint16_t bLen, uint8_t qos);
248248
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
249249
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
250250
uint8_t pingPacket(uint8_t *packet);
@@ -262,7 +262,7 @@ class Adafruit_MQTT_Publish {
262262
// This might be ignored and a higher precision value sent.
263263
bool publish(int32_t i);
264264
bool publish(uint32_t i);
265-
bool publish(uint8_t *b, uint16_t bLen);
265+
bool publish(const uint8_t *b, uint16_t bLen);
266266

267267

268268
private:

0 commit comments

Comments
 (0)