Skip to content

Commit 1cad196

Browse files
committed
pub_hdr_fixed & pub_hdr_var simplified init
1 parent 285e0da commit 1cad196

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,11 @@ def publish(self, topic, msg, retain=False, qos=0):
460460
0 <= qos <= 1
461461
), "Quality of Service Level 2 is unsupported by this library."
462462

463-
pub_hdr_fixed = bytearray() # fixed header
464-
pub_hdr_fixed.extend(MQTT_PUB)
465-
pub_hdr_fixed[0] |= retain | qos << 1 # [3.3.1.2], [3.3.1.3]
463+
# fixed header. [3.3.1.2], [3.3.1.3]
464+
pub_hdr_fixed = bytearray([MQTT_PUB[0] | retain | qos << 1])
466465

467-
pub_hdr_var = bytearray() # variable header
468-
pub_hdr_var.append(len(topic) >> 8) # Topic length, MSB
469-
pub_hdr_var.append(len(topic) & 0xFF) # Topic length, LSB
466+
# variable header = 2-byte Topic length (big endian)
467+
pub_hdr_var = struct.pack('>H', len(topic))
470468
pub_hdr_var.extend(topic.encode("utf-8")) # Topic name
471469

472470
remaining_length = 2 + len(msg) + len(topic)

0 commit comments

Comments
 (0)