Skip to content

_wait_for_msg() should return message type #183

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

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Changes from all 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
17 changes: 9 additions & 8 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def disconnect(self) -> None:
def ping(self) -> list[int]:
"""Pings the MQTT Broker to confirm if the broker is alive or if
there is an active network connection.
Returns response codes of any messages received while waiting for PINGRESP.
Returns packet types of any messages received while waiting for PINGRESP.
"""
self._connected()
self.logger.debug("Sending PINGREQ")
Expand Down Expand Up @@ -981,7 +981,7 @@ def reconnect(self, resub_topics: bool = True) -> int:
def loop(self, timeout: float = 0) -> Optional[list[int]]:
# pylint: disable = too-many-return-statements
"""Non-blocking message loop. Use this method to check for incoming messages.
Returns list of response codes of any messages received or None.
Returns list of packet types of any messages received or None.

:param float timeout: return after this timeout, in seconds.

Expand Down Expand Up @@ -1037,16 +1037,17 @@ def _wait_for_msg(self) -> Optional[int]:
if res in [None, b"", b"\x00"]:
# If we get here, it means that there is nothing to be received
return None
if res[0] & MQTT_PKT_TYPE_MASK == MQTT_PINGRESP:
pkt_type = res[0] & MQTT_PKT_TYPE_MASK
self.logger.debug(f"Got message type: {hex(pkt_type)} pkt: {hex(res[0])}")
if pkt_type == MQTT_PINGRESP:
self.logger.debug("Got PINGRESP")
sz = self._sock_exact_recv(1)[0]
if sz != 0x00:
raise MMQTTException(f"Unexpected PINGRESP returned from broker: {sz}.")
return MQTT_PINGRESP
return pkt_type

if res[0] & MQTT_PKT_TYPE_MASK != MQTT_PUBLISH:
self.logger.debug(f"Got message type: {hex(res[0])}")
return res[0]
if pkt_type != MQTT_PUBLISH:
return pkt_type

# Handle only the PUBLISH packet type from now on.
sz = self._recv_len()
Expand Down Expand Up @@ -1080,7 +1081,7 @@ def _wait_for_msg(self) -> Optional[int]:
elif res[0] & 6 == 4:
assert 0

return res[0]
return pkt_type

def _recv_len(self) -> int:
"""Unpack MQTT message length."""
Expand Down