diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index cbeb8f6c..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. @@ -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."""