Skip to content
Open
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
18 changes: 9 additions & 9 deletions src/PubSubClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,11 @@ bool PubSubClient::handlePacket(uint8_t hdrLen, size_t length) {
DEBUG_PSC_PRINTF("handlePacket(): Packet too short to contain topic length field\n");
return false;
}
uint16_t topicLen = (_buffer[hdrLen + 1] << 8) + _buffer[hdrLen + 2]; // topic length in bytes
char* topic = (char*)(_buffer + hdrLen + 3 - 1); // set the topic in the LSB of the topic lenght, as we move it there
uint16_t payloadOffset = hdrLen + 3 + topicLen; // payload starts after header and topic (if there is no packet identifier)
size_t payloadLen = length - payloadOffset; // this might change by 2 if we have a QoS 1 or 2 message
uint8_t* payload = _buffer + payloadOffset;
const uint16_t topicLen = (_buffer[hdrLen + 1] << 8) + _buffer[hdrLen + 2]; // topic length in bytes
char* const topic = (char*)(_buffer + hdrLen + 3 - 1); // set the topic in the LSB of the topic lenght, as we move it there
const uint16_t payloadOffset = hdrLen + 3 + topicLen; // payload starts after header and topic (if there is no packet identifier)
const size_t payloadLen = length - payloadOffset;
uint8_t* const payload = _buffer + payloadOffset;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always have to think really deep about this order of const declaration, as it is about the content being const vs. the pointer address being const. (which is what is done here)
Like const uint8_t* vs uint8_t* const.
And a real beauty when using const uint8_t* const...


// Guard 2: ensure topic fits in buffer
if (length < payloadOffset) {
Expand All @@ -406,16 +406,16 @@ bool PubSubClient::handlePacket(uint8_t hdrLen, size_t length) {
DEBUG_PSC_PRINTF("handlePacket(): Missing msgId in QoS 1/2 message\n");
return false;
}
uint8_t publishQos = MQTT_HDR_GET_QOS(_buffer[0]); // save QoS before _buffer[0] is overwritten
uint16_t msgId = (_buffer[payloadOffset] << 8) + _buffer[payloadOffset + 1];
const uint8_t publishQos = MQTT_HDR_GET_QOS(_buffer[0]); // save QoS before _buffer[0] is overwritten
const uint16_t msgId = (_buffer[payloadOffset] << 8) + _buffer[payloadOffset + 1];
callback(topic, payload + 2, payloadLen - 2); // remove the msgId from the callback payload

// QoS 1: respond with PUBACK
// QoS 2: respond with PUBREC (first step of the QoS 2 subscriber handshake)
_buffer[0] = (publishQos == MQTT_QOS1) ? MQTTPUBACK : MQTTPUBREC;
_buffer[1] = 2;
_buffer[2] = (msgId >> 8);
_buffer[3] = (msgId & 0xFF);
_buffer[2] = (uint8_t)(msgId >> 8);
_buffer[3] = (uint8_t)(msgId & 0xFF);
if (_client->write(_buffer, 4) == 4) {
_lastOutActivity = millis();
}
Expand Down