Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust fixed_header init; (-) loop_forever() #46

Merged
merged 5 commits into from
Aug 6, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ def connect(self, clean_session=True):
raise MMQTTException("Invalid broker address defined.", e)

# Fixed Header
fixed_header = bytearray()
fixed_header.append(0x10)
fixed_header = bytearray([0x10])
2bndy5 marked this conversation as resolved.
Show resolved Hide resolved

# NOTE: Variable header is
# MQTT_HDR_CONNECT = bytearray(b"\x04MQTT\x04\x02\0\0")
Expand Down Expand Up @@ -461,13 +460,11 @@ def publish(self, topic, msg, retain=False, qos=0):
0 <= qos <= 1
), "Quality of Service Level 2 is unsupported by this library."

pub_hdr_fixed = bytearray() # fixed header
pub_hdr_fixed.extend(MQTT_PUB)
pub_hdr_fixed[0] |= retain | qos << 1 # [3.3.1.2], [3.3.1.3]
# fixed header. [3.3.1.2], [3.3.1.3]
pub_hdr_fixed = bytearray([MQTT_PUB[0] | retain | qos << 1])

pub_hdr_var = bytearray() # variable header
pub_hdr_var.append(len(topic) >> 8) # Topic length, MSB
pub_hdr_var.append(len(topic) & 0xFF) # Topic length, LSB
# variable header = 2-byte Topic length (big endian)
pub_hdr_var = struct.pack(">H", len(topic))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running the example simpletest on Adafruit CircuitPython 5.3.1 on 2020-07-13; Adafruit PyPortal with samd51j20 throws the following error in the REPL:

Subscribed to test/topic with QOS level 0
Publishing to test/topic
Traceback (most recent call last):
  File "code.py", line 116, in <module>
  File "/lib/adafruit_minimqtt/adafruit_minimqtt.py", line 468, in publish
AttributeError: 'bytes' object has no attribute 'extend'

Instead of extend on L468, we may want to append

Copy link
Contributor Author

@2bndy5 2bndy5 Jul 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is a borderline rookie mistake. I tried append() instead, but I still got the same error AttributeError: 'bytes' object has no attribute 'append' However pub_hdr_var += topic.encode() does what we want. The real problem comes from the change to using struct.pack() which returns an immutable bytes object, where methods like append() or extend(bytes([0x00])) (notice the need for conversion from int to bytes when using extend()) can only apply to a mutable bytearray object. So, I've wrapped the struct.pack() into a bytearray(struct.pack()) which allows us to keep using the pub_hdr_var.append() calls on L474 & L475.

pub_hdr_var.extend(topic.encode("utf-8")) # Topic name

remaining_length = 2 + len(msg) + len(topic)
Expand Down Expand Up @@ -688,21 +685,6 @@ def reconnect(self, resub_topics=True):
feed = subscribed_topics.pop()
self.subscribe(feed)

def loop_forever(self):
2bndy5 marked this conversation as resolved.
Show resolved Hide resolved
"""Starts a blocking message loop. Use this
method if you want to run a program forever.
Code below a call to this method will NOT execute.

.. note:: This method is depreciated and will be removed in the
next major release. Please see
`examples/minimqtt_pub_sub_blocking.py <examples.html#basic-forever-loop>`_
for an example of creating a blocking loop which can handle wireless
network events.
"""
while True:
if self._sock.connected:
self.loop()

def loop(self):
"""Non-blocking message loop. Use this method to
check incoming subscription messages.
Expand Down