Skip to content

Commit 3a7cf33

Browse files
authored
Merge pull request #190 from dlizotte-uwo/master
MQTT with QOS 1 or 2 forbids a packet ID of 0.
2 parents 3468daa + 5b93947 commit 3a7cf33

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

Adafruit_MQTT.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
132132
will_qos = 0;
133133
will_retain = 0;
134134

135+
packet_id_counter = 1; // MQTT spec forbids packet id of 0 if QOS=1
135136
keepAliveInterval = MQTT_CONN_KEEPALIVE;
136-
137-
packet_id_counter = 0;
138137
}
139138

140139
Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port,
@@ -155,9 +154,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port,
155154
will_qos = 0;
156155
will_retain = 0;
157156

157+
packet_id_counter = 1; // MQTT spec forbids packet id of 0 if QOS=1
158158
keepAliveInterval = MQTT_CONN_KEEPALIVE;
159-
160-
packet_id_counter = 0;
161159
}
162160

163161
int8_t Adafruit_MQTT::connect() {
@@ -394,7 +392,7 @@ bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
394392

395393
// we increment the packet_id_counter right after publishing so inc here too
396394
// to match
397-
packnum++;
395+
packnum = packnum + 1 + (packnum + 1 == 0); // Skip zero
398396
if (packnum != packet_id_counter)
399397
return false;
400398
}
@@ -808,8 +806,8 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
808806
p[1] = packet_id_counter & 0xFF;
809807
p += 2;
810808

811-
// increment the packet id
812-
packet_id_counter++;
809+
// increment the packet id, skipping 0
810+
packet_id_counter = packet_id_counter + 1 + (packet_id_counter + 1 == 0);
813811
}
814812

815813
memmove(p, data, bLen);
@@ -834,8 +832,8 @@ uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
834832
p[1] = packet_id_counter & 0xFF;
835833
p += 2;
836834

837-
// increment the packet id
838-
packet_id_counter++;
835+
// increment the packet id, skipping 0
836+
packet_id_counter = packet_id_counter + 1 + (packet_id_counter + 1 == 0);
839837

840838
p = stringprint(p, topic);
841839

@@ -863,8 +861,8 @@ uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
863861
p[1] = packet_id_counter & 0xFF;
864862
p += 2;
865863

866-
// increment the packet id
867-
packet_id_counter++;
864+
// increment the packet id, skipping 0
865+
packet_id_counter = packet_id_counter + 1 + (packet_id_counter + 1 == 0);
868866

869867
p = stringprint(p, topic);
870868

@@ -979,4 +977,4 @@ void Adafruit_MQTT_Subscribe::removeCallback(void) {
979977
callback_double = 0;
980978
callback_io = 0;
981979
io_mqtt = 0;
982-
}
980+
}

0 commit comments

Comments
 (0)