Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adafruit_MQTT (API): the payload of publish method is read-only #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions Adafruit_MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,14 @@ 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 len =
publishPacket(buffer, topic, data, bLen, qos, (uint16_t)sizeof(buffer));
if (!sendPacket(buffer, len))
return false;

Expand Down Expand Up @@ -665,11 +666,22 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
return len;
}

uint16_t Adafruit_MQTT::packetAdditionalLen(uint16_t currLen) {
/* Increase length field based on current length */
if (currLen < 128)
return 0;
if (currLen < 16384)
return 1;
if (currLen < 2097151)
return 2;
return 3;
}

// 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) {
const uint8_t *data, uint16_t bLen,
uint8_t qos, uint16_t maxPacketLen) {
uint8_t *p = packet;
uint16_t len = 0;

Expand All @@ -679,7 +691,21 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
if (qos > 0) {
len += 2; // qos packet id
}
len += bLen; // payload length
// calculate additional bytes for length field (if any)
uint16_t additionalLen = packetAdditionalLen(len + bLen);

// payload length
if (len + bLen + 2 + additionalLen <= maxPacketLen) {
len += bLen + additionalLen;
} else {
// If we make it here, we got a pickle: the payload is not going
// to fit in the packet buffer. Instead of corrupting memory, let's
// do something less damaging by reducing the bLen to what we are
// able to accomodate. Alternatively, consider using a bigger
// maxPacketLen.
bLen = maxPacketLen - (len + 2 + packetAdditionalLen(maxPacketLen));
len = maxPacketLen - 4;
}

// Now you can start generating the packet!
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
Expand Down Expand Up @@ -831,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
10 changes: 6 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 @@ -257,8 +257,10 @@ class Adafruit_MQTT {
// Functions to generate MQTT packets.
uint8_t connectPacket(uint8_t *packet);
uint8_t disconnectPacket(uint8_t *packet);
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
uint16_t bLen, uint8_t qos);
static uint16_t packetAdditionalLen(uint16_t currLen);
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 @@ -278,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