Skip to content

Commit 11ed20f

Browse files
authored
Merge pull request adafruit#79 from dlizotte-uwo/master
MQTT QOS=1,2 rules state that pid must be > 0.
2 parents 64c80b9 + 1769040 commit 11ed20f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,9 @@ def publish(self, topic, msg, retain=False, qos=0):
591591
if qos > 0:
592592
# packet identifier where QoS level is 1 or 2. [3.3.2.2]
593593
remaining_length += 2
594-
pub_hdr_var.append(0x00)
595-
pub_hdr_var.append(self._pid)
596-
self._pid += 1
594+
self._pid = self._pid + 1 if self._pid < 0xFFFF else 1
595+
pub_hdr_var.append(self._pid >> 8)
596+
pub_hdr_var.append(self._pid & 0xFF)
597597

598598
# Calculate remaining length [2.2.3]
599599
if remaining_length > 0x7F:
@@ -668,7 +668,7 @@ def subscribe(self, topic, qos=0):
668668
packet_length = 2 + (2 * len(topics)) + (1 * len(topics))
669669
packet_length += sum(len(topic) for topic, qos in topics)
670670
packet_length_byte = packet_length.to_bytes(1, "big")
671-
self._pid += 1
671+
self._pid = self._pid + 1 if self._pid < 0xFFFF else 1
672672
packet_id_bytes = self._pid.to_bytes(2, "big")
673673
# Packet with variable and fixed headers
674674
packet = MQTT_SUB + packet_length_byte + packet_id_bytes
@@ -717,7 +717,7 @@ def unsubscribe(self, topic):
717717
packet_length = 2 + (2 * len(topics))
718718
packet_length += sum(len(topic) for topic in topics)
719719
packet_length_byte = packet_length.to_bytes(1, "big")
720-
self._pid += 1
720+
self._pid = self._pid + 1 if self._pid < 0xFFFF else 1
721721
packet_id_bytes = self._pid.to_bytes(2, "big")
722722
packet = MQTT_UNSUB + packet_length_byte + packet_id_bytes
723723
for t in topics:

0 commit comments

Comments
 (0)