Skip to content

Commit da50f5c

Browse files
authored
Merge pull request adafruit#91 from Eason010212/fix-remaining-length
Fix Issue adafruit#90: Remaining length issues
2 parents 179490a + de73ed4 commit da50f5c

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def __init__(
181181
randint(0, int(time.monotonic() * 100) % 1000), randint(0, 99)
182182
)
183183
# generated client_id's enforce spec.'s length rules
184-
if len(self.client_id) > 23 or not self.client_id:
184+
if len(self.client_id.encode("utf-8")) > 23 or not self.client_id:
185185
raise ValueError("MQTT Client ID must be between 1 and 23 bytes")
186186

187187
# LWT
@@ -448,16 +448,16 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
448448
var_header[6] = clean_session << 1
449449

450450
# Set up variable header and remaining_length
451-
remaining_length = 12 + len(self.client_id)
451+
remaining_length = 12 + len(self.client_id.encode("utf-8"))
452452
if self._username:
453-
remaining_length += 2 + len(self._username) + 2 + len(self._password)
453+
remaining_length += 2 + len(self._username.encode("utf-8")) + 2 + len(self._password.encode("utf-8"))
454454
var_header[6] |= 0xC0
455455
if self.keep_alive:
456456
assert self.keep_alive < MQTT_TOPIC_LENGTH_LIMIT
457457
var_header[7] |= self.keep_alive >> 8
458458
var_header[8] |= self.keep_alive & 0x00FF
459459
if self._lw_topic:
460-
remaining_length += 2 + len(self._lw_topic) + 2 + len(self._lw_msg)
460+
remaining_length += 2 + len(self._lw_topic.encode("utf-8")) + 2 + len(self._lw_msg)
461461
var_header[6] |= 0x4 | (self._lw_qos & 0x1) << 3 | (self._lw_qos & 0x2) << 3
462462
var_header[6] |= self._lw_retain << 5
463463

@@ -584,10 +584,10 @@ def publish(self, topic, msg, retain=False, qos=0):
584584
pub_hdr_fixed = bytearray([0x30 | retain | qos << 1])
585585

586586
# variable header = 2-byte Topic length (big endian)
587-
pub_hdr_var = bytearray(struct.pack(">H", len(topic)))
587+
pub_hdr_var = bytearray(struct.pack(">H", len(topic.encode("utf-8"))))
588588
pub_hdr_var.extend(topic.encode("utf-8")) # Topic name
589589

590-
remaining_length = 2 + len(msg) + len(topic)
590+
remaining_length = 2 + len(msg) + len(topic.encode("utf-8"))
591591
if qos > 0:
592592
# packet identifier where QoS level is 1 or 2. [3.3.2.2]
593593
remaining_length += 2
@@ -666,15 +666,15 @@ def subscribe(self, topic, qos=0):
666666
topics.append((t, q))
667667
# Assemble packet
668668
packet_length = 2 + (2 * len(topics)) + (1 * len(topics))
669-
packet_length += sum(len(topic) for topic, qos in topics)
669+
packet_length += sum(len(topic.encode("utf-8")) for topic, qos in topics)
670670
packet_length_byte = packet_length.to_bytes(1, "big")
671671
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
675675
# attaching topic and QOS level to the packet
676676
for t, q in topics:
677-
topic_size = len(t).to_bytes(2, "big")
677+
topic_size = len(t.encode("utf-8")).to_bytes(2, "big")
678678
qos_byte = q.to_bytes(1, "big")
679679
packet += topic_size + t.encode() + qos_byte
680680
if self.logger:
@@ -715,13 +715,13 @@ def unsubscribe(self, topic):
715715
)
716716
# Assemble packet
717717
packet_length = 2 + (2 * len(topics))
718-
packet_length += sum(len(topic) for topic in topics)
718+
packet_length += sum(len(topic.encode("utf-8")) for topic in topics)
719719
packet_length_byte = packet_length.to_bytes(1, "big")
720720
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:
724-
topic_size = len(t).to_bytes(2, "big")
724+
topic_size = len(t.encode("utf-8")).to_bytes(2, "big")
725725
packet += topic_size + t.encode()
726726
if self.logger:
727727
for t in topics:
@@ -912,10 +912,11 @@ def _send_str(self, string):
912912
:param str string: String to write to the socket.
913913
914914
"""
915-
self._sock.send(struct.pack("!H", len(string)))
916915
if isinstance(string, str):
916+
self._sock.send(struct.pack("!H", len(string.encode("utf-8"))))
917917
self._sock.send(str.encode(string, "utf-8"))
918918
else:
919+
self._sock.send(struct.pack("!H", len(string)))
919920
self._sock.send(string)
920921

921922
@staticmethod

0 commit comments

Comments
 (0)