Skip to content

Commit

Permalink
Adafruit_MQTT (API): the payload of publish method is read-only
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
flavio-fernandes committed Dec 25, 2019
1 parent 4204e1d commit a780728
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Adafruit_MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ bool Adafruit_MQTT::disconnect() {


bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
return publish(topic, (uint8_t*)(data), strlen(data), qos);
return publish(topic, (const uint8_t*)(data), strlen(data), qos);
}

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

// 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, uint16_t maxPacketLen, const char *topic,
uint8_t *data, uint16_t bLen, uint8_t qos) {
const uint8_t *data, uint16_t bLen, uint8_t qos) {
uint8_t *p = packet;
uint16_t len=2; // control + length

Expand Down Expand Up @@ -825,7 +825,7 @@ bool Adafruit_MQTT_Publish::publish(const char *payload) {
}

//publish buffer of arbitrary length
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) {
bool Adafruit_MQTT_Publish::publish(const uint8_t *payload, uint16_t bLen) {

return mqtt->publish(topic, payload, bLen, qos);
}
Expand Down
6 changes: 3 additions & 3 deletions Adafruit_MQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Adafruit_MQTT {
// Publish a message to a topic using the specified QoS level. Returns true
// if the message was published, false otherwise.
bool publish(const char *topic, const char *payload, uint8_t qos = 0);
bool publish(const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos = 0);
bool publish(const char *topic, const uint8_t *payload, uint16_t bLen, uint8_t qos = 0);

// Add a subscription to receive messages for a topic. Returns true if the
// subscription could be added or was already present, false otherwise.
Expand Down Expand Up @@ -244,7 +244,7 @@ class Adafruit_MQTT {
uint8_t connectPacket(uint8_t *packet);
uint8_t disconnectPacket(uint8_t *packet);
uint16_t publishPacket(uint8_t *packet, uint16_t maxPacketLen,
const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos);
const char *topic, const uint8_t *payload, uint16_t bLen, 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 pingPacket(uint8_t *packet);
Expand All @@ -262,7 +262,7 @@ class Adafruit_MQTT_Publish {
// This might be ignored and a higher precision value sent.
bool publish(int32_t i);
bool publish(uint32_t i);
bool publish(uint8_t *b, uint16_t bLen);
bool publish(const uint8_t *b, uint16_t bLen);


private:
Expand Down

0 comments on commit a780728

Please sign in to comment.