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 Aug 29, 2020
1 parent 785b5a4 commit 57009da
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
12 changes: 6 additions & 6 deletions Adafruit_MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,11 @@ 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, topic, data, bLen, qos, (uint16_t)sizeof(buffer));
Expand Down Expand Up @@ -680,8 +680,8 @@ 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, const char *topic,
uint8_t *data, uint16_t bLen, uint8_t qos,
uint16_t maxPacketLen) {
const uint8_t *data, uint16_t bLen,
uint8_t qos, uint16_t maxPacketLen) {
uint8_t *p = packet;
uint16_t len = 0;

Expand Down Expand Up @@ -857,7 +857,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
9 changes: 5 additions & 4 deletions Adafruit_MQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,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,
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
Expand Down Expand Up @@ -258,8 +258,9 @@ class Adafruit_MQTT {
uint8_t connectPacket(uint8_t *packet);
uint8_t disconnectPacket(uint8_t *packet);
static uint16_t packetAdditionalLen(uint16_t currLen);
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
uint16_t bLen, uint8_t qos, uint16_t maxPacketLen);
uint16_t publishPacket(uint8_t *packet, const char *topic,
const uint8_t *payload, uint16_t bLen, uint8_t qos,
uint16_t maxPacketLen);
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 @@ -279,7 +280,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:
Adafruit_MQTT *mqtt;
Expand Down

0 comments on commit 57009da

Please sign in to comment.