@@ -132,9 +132,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
132
132
will_qos = 0 ;
133
133
will_retain = 0 ;
134
134
135
+ packet_id_counter = 1 ; // MQTT spec forbids packet id of 0 if QOS=1
135
136
keepAliveInterval = MQTT_CONN_KEEPALIVE;
136
-
137
- packet_id_counter = 0 ;
138
137
}
139
138
140
139
Adafruit_MQTT::Adafruit_MQTT (const char *server, uint16_t port,
@@ -155,9 +154,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port,
155
154
will_qos = 0 ;
156
155
will_retain = 0 ;
157
156
157
+ packet_id_counter = 1 ; // MQTT spec forbids packet id of 0 if QOS=1
158
158
keepAliveInterval = MQTT_CONN_KEEPALIVE;
159
-
160
- packet_id_counter = 0 ;
161
159
}
162
160
163
161
int8_t Adafruit_MQTT::connect () {
@@ -394,7 +392,7 @@ bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
394
392
395
393
// we increment the packet_id_counter right after publishing so inc here too
396
394
// to match
397
- packnum++;
395
+ packnum = packnum + 1 + (packnum + 1 == 0 ); // Skip zero
398
396
if (packnum != packet_id_counter)
399
397
return false ;
400
398
}
@@ -808,8 +806,8 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
808
806
p[1 ] = packet_id_counter & 0xFF ;
809
807
p += 2 ;
810
808
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 ) ;
813
811
}
814
812
815
813
memmove (p, data, bLen);
@@ -834,8 +832,8 @@ uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
834
832
p[1 ] = packet_id_counter & 0xFF ;
835
833
p += 2 ;
836
834
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 ) ;
839
837
840
838
p = stringprint (p, topic);
841
839
@@ -863,8 +861,8 @@ uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
863
861
p[1 ] = packet_id_counter & 0xFF ;
864
862
p += 2 ;
865
863
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 ) ;
868
866
869
867
p = stringprint (p, topic);
870
868
@@ -979,4 +977,4 @@ void Adafruit_MQTT_Subscribe::removeCallback(void) {
979
977
callback_double = 0 ;
980
978
callback_io = 0 ;
981
979
io_mqtt = 0 ;
982
- }
980
+ }
0 commit comments