From 687c1e5acc5fa825d16c09b527d1f581548b3a96 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Mon, 6 Nov 2023 22:30:09 +0100 Subject: [PATCH 1/2] _wait_for_msg() should return message type --- adafruit_minimqtt/adafruit_minimqtt.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index cbeb8f6c..eb62154e 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -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() @@ -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.""" From db9998f5abca6e0a1c0a44ee84138d21b201e12e Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Mon, 6 Nov 2023 22:37:46 +0100 Subject: [PATCH 2/2] adjust docs --- adafruit_minimqtt/adafruit_minimqtt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index eb62154e..37b3cfb5 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -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") @@ -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.