adafruit / Adafruit_MQTT_Library

Arduino library for MQTT support
MIT License
566 stars 292 forks source link

Start packet id counter from 1 instead of 0 #207

Closed Mollayo closed 2 years ago

Mollayo commented 2 years ago

@brentru : this is a pull request to address this issue: https://github.com/adafruit/Adafruit_MQTT_Library/issues/203

Packets whose index is 0 are considered as malformed packets by the MQTT broker. If such thing happens, the client is disconnected by the broker. The value of the packet_id_counter variable is now calculated in a way that it is always different from 0 to avoid unwanted disconnections.

mikedoug commented 2 years ago

I agree with this patch too -- it was causing a malformed packet when subscribing immediately after a connection; which is a pretty common thing to do.

mikedoug commented 2 years ago

Though a more compact way to do this might just be:

packet_id_counter = packet_id_counter == 0xffff ? 1 : packet_id_counter + 1;

Also, in publish, in the qos if block:

packnum = packnum == 0xffff ? 1 : packnum + 1;