From c099e96641ce909bedc32c59a47ae0be3d5ff39c Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sun, 15 Nov 2026 00:14:25 +0000 Subject: [PATCH] fix(b01): isolate Q7 map response assemblers per subscription --- roborock/devices/rpc/b01_q7_channel.py | 29 +---- roborock/protocols/b01_q7_protocol.py | 4 +- tests/devices/rpc/test_b01_q7_channel.py | 130 ++++++++++------------- tests/protocols/test_b01_q07_protocol.py | 117 +++++++++++++++++++- 4 files changed, 179 insertions(+), 101 deletions(-) diff --git a/roborock/devices/rpc/b01_q7_channel.py b/roborock/devices/rpc/b01_q7_channel.py index d91f10f2d..9ac8a923c 100644 --- a/roborock/devices/rpc/b01_q7_channel.py +++ b/roborock/devices/rpc/b01_q7_channel.py @@ -12,7 +12,6 @@ from roborock.exceptions import RoborockException from roborock.protocols.b01_q7_protocol import ( B01_Q7_DPS, - B01_VERSION, CommandType, MapKey, ParamsType, @@ -23,7 +22,7 @@ decode_rpc_response, encode_mqtt_payload, ) -from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol +from roborock.roborock_message import RoborockMessage _LOGGER = logging.getLogger(__name__) _TIMEOUT = 10.0 @@ -59,14 +58,6 @@ async def subscribe_map_pushes(self, callback: Callable[[bytes], None]) -> Calla ... -def _is_map_message(response_message: RoborockMessage) -> bool: - """Check whether a message is a B01 map response part or final part.""" - return response_message.protocol in ( - RoborockMessageProtocol.MAP_RESPONSE_PART, - RoborockMessageProtocol.MAP_RESPONSE, - ) - - async def _send_command( mqtt_channel: MqttChannel, request_message: Q7RequestMessage, @@ -170,19 +161,6 @@ class B01Q7Channel(Channel, Q7RpcChannel, Q7MapRpcChannel): def __init__(self, mqtt_channel: MqttChannel, map_key: MapKey) -> None: self._mqtt_channel = mqtt_channel self._map_key = map_key - self._map_assembler = create_map_response_assembler() - - def _match_map_response(self, response_message: RoborockMessage) -> bytes | None: - """Return the complete raw map payload for matching map messages. - - Large maps arrive split into several messages; the assembler buffers - the parts and returns the joined payload once the frame is complete. - """ - if not _is_map_message(response_message): - return None - if not response_message.payload or response_message.version != B01_VERSION: - return None - return self._map_assembler(response_message) @property def is_connected(self) -> bool: @@ -216,7 +194,7 @@ async def send_map_command( raw_payload = await _send_command( self._mqtt_channel, request_message, - response_matcher=self._match_map_response, + response_matcher=create_map_response_assembler(), ) except TimeoutError as ex: raise RoborockException(f"B01 map command timed out after {_TIMEOUT}s ({request_message})") from ex @@ -230,9 +208,10 @@ async def subscribe_map_pushes(self, callback: Callable[[bytes], None]) -> Calla callback receives the decoded (inflated) SCMap bytes for each frame. Frames split across several messages are reassembled first. """ + assembler = create_map_response_assembler() def on_message(message: RoborockMessage) -> None: - if (raw_payload := self._match_map_response(message)) is None: + if (raw_payload := assembler(message)) is None: return try: decoded = decode_map_payload(raw_payload, map_key=self._map_key) diff --git a/roborock/protocols/b01_q7_protocol.py b/roborock/protocols/b01_q7_protocol.py index e9fb32b07..912249db4 100644 --- a/roborock/protocols/b01_q7_protocol.py +++ b/roborock/protocols/b01_q7_protocol.py @@ -168,7 +168,7 @@ def _complete_payload(parts: dict[int, tuple[bool, bytes]]) -> bytes | None: seqs = sorted(parts) if not parts[seqs[-1]][0]: return None - if seqs != list(range(seqs[0], seqs[-1] + 1)): + if seqs != list(range(1, seqs[-1] + 1)): return None joined = b"".join(parts[seq][1] for seq in seqs) length = _decoded_length(joined) @@ -178,7 +178,7 @@ def _complete_payload(parts: dict[int, tuple[bool, bytes]]) -> bytes | None: def assemble(message: RoborockMessage) -> bytes | None: """Return the complete raw payload once all parts of a frame arrived.""" - if not message.payload: + if not message.payload or message.version != B01_VERSION: return None key = (message.random, message.timestamp) is_last = message.protocol == RoborockMessageProtocol.MAP_RESPONSE diff --git a/tests/devices/rpc/test_b01_q7_channel.py b/tests/devices/rpc/test_b01_q7_channel.py index 206c7947b..90c0c75a6 100644 --- a/tests/devices/rpc/test_b01_q7_channel.py +++ b/tests/devices/rpc/test_b01_q7_channel.py @@ -388,15 +388,25 @@ async def test_subscribe_map_pushes_skips_undecodable_frames( MAP_TIMESTAMP = 1794578237 +@pytest.mark.parametrize("out_of_order", [False, True], ids=["in_order", "out_of_order"]) async def test_send_map_command_reassembles_fragmented_frames( device: HomeDataDevice, product: HomeDataProduct, fake_channel: FakeChannel, message_builder: B01MessageBuilder, + out_of_order: bool, ) -> None: - """A map split into a 300 part and a 301 part is decoded once, joined.""" + """A map split across 300 and 301 parts is joined before decoding regardless of arrival order.""" channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type] full, first_part, last_part = _fragmented_map_payload() + parts = [ + message_builder.build_map_part( + first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP + ), + message_builder.build_map_part(last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP), + ] + if out_of_order: + parts.reverse() with patch( "roborock.devices.rpc.b01_q7_channel.decode_map_payload", @@ -405,66 +415,34 @@ async def test_send_map_command_reassembles_fragmented_frames( task = asyncio.create_task(channel.send_map_command("service.upload_by_mapid", {"map_id": 123})) await asyncio.sleep(0) - fake_channel.notify_subscribers( - message_builder.build_map_part( - first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP - ) - ) + fake_channel.notify_subscribers(parts[0]) await asyncio.sleep(0) - assert not task.done() # the first part alone is not a complete frame - fake_channel.notify_subscribers( - message_builder.build_map_part( - last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP - ) - ) - assert await task == b"inflated-payload" - mock_decode.assert_called_once() - assert mock_decode.call_args[0][0] == full - - -async def test_send_map_command_reassembles_out_of_order_fragments( - device: HomeDataDevice, - product: HomeDataProduct, - fake_channel: FakeChannel, - message_builder: B01MessageBuilder, -) -> None: - """The trailing 301 part arriving first is buffered until the 300 part arrives.""" - channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type] - full, first_part, last_part = _fragmented_map_payload() - - with patch( - "roborock.devices.rpc.b01_q7_channel.decode_map_payload", - return_value=b"inflated-payload", - ) as mock_decode: - task = asyncio.create_task(channel.send_map_command("service.upload_by_mapid", {"map_id": 123})) - await asyncio.sleep(0) - - fake_channel.notify_subscribers( - message_builder.build_map_part( - last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP - ) - ) - await asyncio.sleep(0) - assert not task.done() # the trailing fragment alone is not decodable - fake_channel.notify_subscribers( - message_builder.build_map_part( - first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP - ) - ) + assert not task.done() + fake_channel.notify_subscribers(parts[1]) assert await task == b"inflated-payload" mock_decode.assert_called_once() assert mock_decode.call_args[0][0] == full +@pytest.mark.parametrize("out_of_order", [False, True], ids=["in_order", "out_of_order"]) async def test_subscribe_map_pushes_reassembles_fragmented_frames( device: HomeDataDevice, product: HomeDataProduct, fake_channel: FakeChannel, message_builder: B01MessageBuilder, + out_of_order: bool, ) -> None: - """Fragmented map pushes are delivered once, as one decoded frame.""" + """Fragmented map pushes are buffered until all parts arrive and delivered once.""" channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type] full, first_part, last_part = _fragmented_map_payload() + parts = [ + message_builder.build_map_part( + first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP + ), + message_builder.build_map_part(last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP), + ] + if out_of_order: + parts.reverse() received: list[bytes] = [] await channel.subscribe_map_pushes(received.append) @@ -473,51 +451,57 @@ async def test_subscribe_map_pushes_reassembles_fragmented_frames( "roborock.devices.rpc.b01_q7_channel.decode_map_payload", return_value=b"inflated-payload", ) as mock_decode: - fake_channel.notify_subscribers( - message_builder.build_map_part( - first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP - ) - ) + fake_channel.notify_subscribers(parts[0]) assert received == [] - fake_channel.notify_subscribers( - message_builder.build_map_part( - last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP - ) - ) + fake_channel.notify_subscribers(parts[1]) assert received == [b"inflated-payload"] mock_decode.assert_called_once() assert mock_decode.call_args[0][0] == full -async def test_subscribe_map_pushes_buffers_lone_trailing_fragment( +@pytest.mark.parametrize("out_of_order", [False, True], ids=["in_order", "out_of_order"]) +async def test_send_map_command_with_active_map_push_subscription( device: HomeDataDevice, product: HomeDataProduct, fake_channel: FakeChannel, message_builder: B01MessageBuilder, + out_of_order: bool, ) -> None: - """A lone 301 fragment is buffered, not decoded, until its 300 part arrives.""" + """Active push subscriptions must not drain fragments needed by send_map_command.""" channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type] - _full, first_part, last_part = _fragmented_map_payload() + full, first_part, last_part = _fragmented_map_payload() + parts = [ + message_builder.build_map_part( + first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP + ), + message_builder.build_map_part(last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP), + ] + if out_of_order: + parts.reverse() received: list[bytes] = [] await channel.subscribe_map_pushes(received.append) - with patch( - "roborock.devices.rpc.b01_q7_channel.decode_map_payload", - return_value=b"inflated-payload", + with ( + patch( + "roborock.devices.rpc.b01_q7_channel.decode_map_payload", + return_value=b"inflated-payload", + ) as mock_decode, + patch("roborock.devices.rpc.b01_q7_channel._TIMEOUT", 0.05), ): - fake_channel.notify_subscribers( - message_builder.build_map_part( - last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP - ) - ) + task = asyncio.create_task(channel.send_map_command("service.upload_by_mapid", {"map_id": 123})) + await asyncio.sleep(0) + + fake_channel.notify_subscribers(parts[0]) + await asyncio.sleep(0) + assert not task.done() assert received == [] - fake_channel.notify_subscribers( - message_builder.build_map_part( - first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP - ) - ) + + fake_channel.notify_subscribers(parts[1]) + assert await task == b"inflated-payload" assert received == [b"inflated-payload"] + assert mock_decode.call_count == 2 + assert [call.args[0] for call in mock_decode.call_args_list] == [full, full] async def test_subscribe_map_pushes_ignores_incomplete_frame( diff --git a/tests/protocols/test_b01_q07_protocol.py b/tests/protocols/test_b01_q07_protocol.py index a507fa41a..5dc3a2bcf 100644 --- a/tests/protocols/test_b01_q07_protocol.py +++ b/tests/protocols/test_b01_q07_protocol.py @@ -1,5 +1,6 @@ """Tests for the B01 protocol message encoding and decoding.""" +import base64 import json import pathlib from collections.abc import Generator @@ -10,7 +11,12 @@ from freezegun import freeze_time from syrupy import SnapshotAssertion -from roborock.protocols.b01_q7_protocol import Q7RequestMessage, decode_rpc_response, encode_mqtt_payload +from roborock.protocols.b01_q7_protocol import ( + Q7RequestMessage, + create_map_response_assembler, + decode_rpc_response, + encode_mqtt_payload, +) from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol TESTDATA_PATH = pathlib.Path("tests/protocols/testdata/b01_q7_protocol") @@ -69,3 +75,112 @@ def test_encode_mqtt_payload(dps: int, command: str, params: dict[str, list[str] assert decoded_json["dps"][str(dps)]["method"] == command assert decoded_json["dps"][str(dps)]["msgId"] == str(msg_id) assert decoded_json["dps"][str(dps)]["params"] == params + + +def _build_map_msg( + payload: bytes | None, + protocol: RoborockMessageProtocol = RoborockMessageProtocol.MAP_RESPONSE, + *, + seq: int = 1, + random: int = 2716216127, + timestamp: int = 1794578237, + version: bytes = b"B01", +) -> RoborockMessage: + return RoborockMessage( + protocol=protocol, + payload=payload, + seq=seq, + version=version, + random=random, + timestamp=timestamp, + ) + + +def test_map_response_assembler_single_part() -> None: + """A complete single-part MAP_RESPONSE passes through regardless of MQTT seq.""" + assembler = create_map_response_assembler() + payload = base64.b64encode(bytes(range(32))) + + assert assembler(_build_map_msg(payload, seq=12750)) == payload + + +@pytest.mark.parametrize("order", [(0, 1), (1, 0)], ids=["in_order", "out_of_order"]) +def test_map_response_assembler_two_parts(order: tuple[int, int]) -> None: + """Two-part fragmented map frames reassemble in-order and out-of-order.""" + assembler = create_map_response_assembler() + full = base64.b64encode(bytes(range(48))) + messages = [ + _build_map_msg(full[:32], RoborockMessageProtocol.MAP_RESPONSE_PART, seq=1), + _build_map_msg(full[32:], RoborockMessageProtocol.MAP_RESPONSE, seq=2), + ] + + assert assembler(messages[order[0]]) is None + assert assembler(messages[order[1]]) == full + + +@pytest.mark.parametrize( + "order", + [(0, 1, 2), (2, 1, 0), (1, 2, 0)], + ids=["in_order", "reverse", "suffix_first"], +) +def test_map_response_assembler_three_parts(order: tuple[int, int, int]) -> None: + """Three-part frames wait for seq=1 even when trailing parts form a block-aligned suffix.""" + assembler = create_map_response_assembler() + # 96 raw bytes -> 128 base64 chars; parts 2+3 alone decode to 48 bytes (a multiple of 16). + full = base64.b64encode(bytes(range(96))) + messages = [ + _build_map_msg(full[:64], RoborockMessageProtocol.MAP_RESPONSE_PART, seq=1), + _build_map_msg(full[64:96], RoborockMessageProtocol.MAP_RESPONSE_PART, seq=2), + _build_map_msg(full[96:], RoborockMessageProtocol.MAP_RESPONSE, seq=3), + ] + + assert assembler(messages[order[0]]) is None + assert assembler(messages[order[1]]) is None + assert assembler(messages[order[2]]) == full + + +def test_map_response_assembler_evicts_stale_incomplete_frames() -> None: + """Incomplete frames beyond the pending buffer cap are evicted FIFO.""" + assembler = create_map_response_assembler() + full = base64.b64encode(bytes(range(48))) + first_part, last_part = full[:32], full[32:] + + for idx in range(5): + assert ( + assembler( + _build_map_msg( + first_part, + RoborockMessageProtocol.MAP_RESPONSE_PART, + seq=1, + random=idx, + ) + ) + is None + ) + + # random=0 was evicted when random=4 arrived; sending its trailing 301 fragment + # buffers random=0 anew (evicting random=1) and returns None rather than joining. + assert assembler(_build_map_msg(last_part, RoborockMessageProtocol.MAP_RESPONSE, seq=2, random=0)) is None + # random=2 is still retained in the buffer and completes cleanly. + assert assembler(_build_map_msg(last_part, RoborockMessageProtocol.MAP_RESPONSE, seq=2, random=2)) == full + + +@pytest.mark.parametrize( + "message", + [ + _build_map_msg(None), + _build_map_msg(b""), + _build_map_msg(base64.b64encode(bytes(range(32))), RoborockMessageProtocol.RPC_RESPONSE), + _build_map_msg(base64.b64encode(bytes(range(32))), version=b"1.0"), + _build_map_msg( + base64.b64encode(bytes(range(48)))[:32], + RoborockMessageProtocol.MAP_RESPONSE_PART, + version=b"1.0", + ), + ], + ids=["none_payload", "empty_payload", "wrong_protocol", "wrong_version_301", "wrong_version_300"], +) +def test_map_response_assembler_ignores_non_matching_messages(message: RoborockMessage) -> None: + """Non-B01, non-map, or empty messages are ignored.""" + assembler = create_map_response_assembler() + assert assembler(message) is None