Skip to content

Commit 55d85a6

Browse files
author
brentru
committed
QoS level 2 unhandled by library so throw more verbose error exception
1 parent 8d1b6a1 commit 55d85a6

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@
6767

6868
# Variable CONNECT header [MQTT 3.1.2]
6969
MQTT_HDR_CONNECT = bytearray(b"\x04MQTT\x04\x02\0\0")
70-
# Variable PUBLISH header [MQTT 3.3.2]
71-
MQTT_HDR_PUBLISH = bytearray(b"\x00\x01\x61\x2F\x62\x00\x0a")
7270

7371

7472
CONNACK_ERRORS = {
@@ -444,32 +442,33 @@ def publish(self, topic, msg, retain=False, qos=0):
444442
raise MMQTTException("Invalid message data type.")
445443
if len(msg) > MQTT_MSG_MAX_SZ:
446444
raise MMQTTException("Message size larger than %d bytes." % MQTT_MSG_MAX_SZ)
447-
self._check_qos(qos)
445+
assert (
446+
0 <= qos <= 1
447+
), "Quality of Service Level 2 is unsupported by this library."
448448

449449
pub_hdr_fixed = bytearray() # fixed header
450450
pub_hdr_fixed.extend(MQTT_PUB)
451-
pub_hdr_fixed[0] |= retain | qos << 1
451+
pub_hdr_fixed[0] |= retain | qos << 1 # [3.3.1.2], [3.3.1.3]
452452

453453
pub_hdr_var = bytearray() # variable header
454-
pub_hdr_var.append(len(topic) >> 8) # Topic len MSB
455-
pub_hdr_var.append(len(topic) & 0xFF) # Topic len LSB
456-
pub_hdr_var.extend(topic.encode("utf-8")) # Topic structure
454+
pub_hdr_var.append(len(topic) >> 8) # Topic length, MSB
455+
pub_hdr_var.append(len(topic) & 0xFF) # Topic length, LSB
456+
pub_hdr_var.extend(topic.encode("utf-8")) # Topic name
457457

458458
remaining_length = 2 + len(msg) + len(topic)
459459
if qos > 0:
460+
# packet identifier where QoS level is 1 or 2. [3.3.2.2]
460461
pid = self._pid
461462
remaining_length += 2
462463
pub_hdr_var.append(0x00)
463464
pub_hdr_var.append(self._pid)
464465
self._pid += 1
465466

466-
# Remaining length calculation
467+
# Calculate remaining length [2.2.3]
467468
if remaining_length > 0x7F:
468-
# Calculate Remaining Length [2.2.3]
469469
while remaining_length > 0:
470470
encoded_byte = remaining_length % 0x80
471471
remaining_length = remaining_length // 0x80
472-
# if there is more data to encode, set the top bit of the byte
473472
if remaining_length > 0:
474473
encoded_byte |= 0x80
475474
pub_hdr_fixed.append(encoded_byte)
@@ -500,10 +499,6 @@ def publish(self, topic, msg, retain=False, qos=0):
500499
if self.on_publish is not None:
501500
self.on_publish(self, self.user_data, topic, rcv_pid)
502501
return
503-
elif qos == 2:
504-
assert 0
505-
if self.on_publish is not None:
506-
self.on_publish(self, self.user_data, topic, rcv_pid)
507502

508503
def subscribe(self, topic, qos=0):
509504
"""Subscribes to a topic on the MQTT Broker.

0 commit comments

Comments
 (0)