Skip to content

Commit 4698382

Browse files
authored
Merge pull request #183 from vladak/wait_for_msg_pkt_type
_wait_for_msg() should return message type
2 parents d1e2b7c + db9998f commit 4698382

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def disconnect(self) -> None:
702702
def ping(self) -> list[int]:
703703
"""Pings the MQTT Broker to confirm if the broker is alive or if
704704
there is an active network connection.
705-
Returns response codes of any messages received while waiting for PINGRESP.
705+
Returns packet types of any messages received while waiting for PINGRESP.
706706
"""
707707
self._connected()
708708
self.logger.debug("Sending PINGREQ")
@@ -1017,7 +1017,7 @@ def reconnect(self, resub_topics: bool = True) -> int:
10171017
def loop(self, timeout: float = 0) -> Optional[list[int]]:
10181018
# pylint: disable = too-many-return-statements
10191019
"""Non-blocking message loop. Use this method to check for incoming messages.
1020-
Returns list of response codes of any messages received or None.
1020+
Returns list of packet types of any messages received or None.
10211021
10221022
:param float timeout: return after this timeout, in seconds.
10231023
@@ -1073,16 +1073,17 @@ def _wait_for_msg(self) -> Optional[int]:
10731073
if res in [None, b"", b"\x00"]:
10741074
# If we get here, it means that there is nothing to be received
10751075
return None
1076-
if res[0] & MQTT_PKT_TYPE_MASK == MQTT_PINGRESP:
1076+
pkt_type = res[0] & MQTT_PKT_TYPE_MASK
1077+
self.logger.debug(f"Got message type: {hex(pkt_type)} pkt: {hex(res[0])}")
1078+
if pkt_type == MQTT_PINGRESP:
10771079
self.logger.debug("Got PINGRESP")
10781080
sz = self._sock_exact_recv(1)[0]
10791081
if sz != 0x00:
10801082
raise MMQTTException(f"Unexpected PINGRESP returned from broker: {sz}.")
1081-
return MQTT_PINGRESP
1083+
return pkt_type
10821084

1083-
if res[0] & MQTT_PKT_TYPE_MASK != MQTT_PUBLISH:
1084-
self.logger.debug(f"Got message type: {hex(res[0])}")
1085-
return res[0]
1085+
if pkt_type != MQTT_PUBLISH:
1086+
return pkt_type
10861087

10871088
# Handle only the PUBLISH packet type from now on.
10881089
sz = self._recv_len()
@@ -1116,7 +1117,7 @@ def _wait_for_msg(self) -> Optional[int]:
11161117
elif res[0] & 6 == 4:
11171118
assert 0
11181119

1119-
return res[0]
1120+
return pkt_type
11201121

11211122
def _recv_len(self) -> int:
11221123
"""Unpack MQTT message length."""

0 commit comments

Comments
 (0)