Skip to content

Commit 1fd6b5e

Browse files
authored
fix(b01): reassemble fragmented B01/Q7 live map responses (#1048)
1 parent ad2ae31 commit 1fd6b5e

3 files changed

Lines changed: 285 additions & 11 deletions

File tree

‎roborock/devices/rpc/b01_q7_channel.py‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
ParamsType,
1919
Q7RequestMessage,
2020
create_map_key,
21+
create_map_response_assembler,
2122
decode_map_payload,
2223
decode_rpc_response,
2324
encode_mqtt_payload,
@@ -58,15 +59,12 @@ async def subscribe_map_pushes(self, callback: Callable[[bytes], None]) -> Calla
5859
...
5960

6061

61-
def _matches_map_response(response_message: RoborockMessage, *, version: bytes | None) -> bytes | None:
62-
"""Return raw map payload bytes for matching MAP_RESPONSE messages."""
63-
if (
64-
response_message.protocol == RoborockMessageProtocol.MAP_RESPONSE
65-
and response_message.payload
66-
and response_message.version == version
67-
):
68-
return response_message.payload
69-
return None
62+
def _is_map_message(response_message: RoborockMessage) -> bool:
63+
"""Check whether a message is a B01 map response part or final part."""
64+
return response_message.protocol in (
65+
RoborockMessageProtocol.MAP_RESPONSE_PART,
66+
RoborockMessageProtocol.MAP_RESPONSE,
67+
)
7068

7169

7270
async def _send_command(
@@ -172,6 +170,19 @@ class B01Q7Channel(Channel, Q7RpcChannel, Q7MapRpcChannel):
172170
def __init__(self, mqtt_channel: MqttChannel, map_key: MapKey) -> None:
173171
self._mqtt_channel = mqtt_channel
174172
self._map_key = map_key
173+
self._map_assembler = create_map_response_assembler()
174+
175+
def _match_map_response(self, response_message: RoborockMessage) -> bytes | None:
176+
"""Return the complete raw map payload for matching map messages.
177+
178+
Large maps arrive split into several messages; the assembler buffers
179+
the parts and returns the joined payload once the frame is complete.
180+
"""
181+
if not _is_map_message(response_message):
182+
return None
183+
if not response_message.payload or response_message.version != B01_VERSION:
184+
return None
185+
return self._map_assembler(response_message)
175186

176187
@property
177188
def is_connected(self) -> bool:
@@ -205,7 +216,7 @@ async def send_map_command(
205216
raw_payload = await _send_command(
206217
self._mqtt_channel,
207218
request_message,
208-
response_matcher=lambda response_message: _matches_map_response(response_message, version=B01_VERSION),
219+
response_matcher=self._match_map_response,
209220
)
210221
except TimeoutError as ex:
211222
raise RoborockException(f"B01 map command timed out after {_TIMEOUT}s ({request_message})") from ex
@@ -217,10 +228,11 @@ async def subscribe_map_pushes(self, callback: Callable[[bytes], None]) -> Calla
217228
218229
The device streams full SCMap frames on its own during cleaning; the
219230
callback receives the decoded (inflated) SCMap bytes for each frame.
231+
Frames split across several messages are reassembled first.
220232
"""
221233

222234
def on_message(message: RoborockMessage) -> None:
223-
if (raw_payload := _matches_map_response(message, version=B01_VERSION)) is None:
235+
if (raw_payload := self._match_map_response(message)) is None:
224236
return
225237
try:
226238
decoded = decode_map_payload(raw_payload, map_key=self._map_key)

‎roborock/protocols/b01_q7_protocol.py‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import logging
88
import zlib
9+
from collections.abc import Callable
910
from dataclasses import dataclass, field
1011
from typing import Any
1112

@@ -132,3 +133,70 @@ def _decode_base64_payload(raw_payload: bytes) -> bytes:
132133
return base64.b64decode(padded, validate=True)
133134
except binascii.Error as err:
134135
raise RoborockException("Failed to decode B01 map payload") from err
136+
137+
138+
# Number of incomplete multi-part map responses kept while waiting for missing parts.
139+
_MAX_PENDING_MAP_RESPONSES = 4
140+
141+
142+
def create_map_response_assembler() -> Callable[[RoborockMessage], bytes | None]:
143+
"""Create an assembler for B01 map response messages.
144+
145+
Large maps are split into multiple messages that share the same ``random``
146+
and ``timestamp`` and are numbered by ``seq``. All parts except the last
147+
one use protocol ``MAP_RESPONSE_PART`` (300), the last one uses
148+
``MAP_RESPONSE`` (301). Parts may arrive out of order, so they are buffered
149+
until complete. A lone final part whose decoded length is not a multiple of
150+
the AES block size is a trailing fragment of a split frame, not a complete
151+
frame, so it is buffered too. This ports the V1 reassembly from #1004.
152+
"""
153+
# (random, timestamp) -> seq -> (is last part, raw payload)
154+
pending: dict[tuple[int, int], dict[int, tuple[bool, bytes]]] = {}
155+
156+
def _decoded_length(raw_payload: bytes) -> int | None:
157+
try:
158+
return len(_decode_base64_payload(raw_payload))
159+
except RoborockException:
160+
return None
161+
162+
def _remember(key: tuple[int, int], parts: dict[int, tuple[bool, bytes]]) -> None:
163+
pending[key] = parts
164+
while len(pending) > _MAX_PENDING_MAP_RESPONSES:
165+
del pending[next(iter(pending))]
166+
167+
def _complete_payload(parts: dict[int, tuple[bool, bytes]]) -> bytes | None:
168+
seqs = sorted(parts)
169+
if not parts[seqs[-1]][0]:
170+
return None
171+
if seqs != list(range(seqs[0], seqs[-1] + 1)):
172+
return None
173+
joined = b"".join(parts[seq][1] for seq in seqs)
174+
length = _decoded_length(joined)
175+
if length is None or length % AES.block_size != 0:
176+
return None
177+
return joined
178+
179+
def assemble(message: RoborockMessage) -> bytes | None:
180+
"""Return the complete raw payload once all parts of a frame arrived."""
181+
if not message.payload:
182+
return None
183+
key = (message.random, message.timestamp)
184+
is_last = message.protocol == RoborockMessageProtocol.MAP_RESPONSE
185+
if message.protocol == RoborockMessageProtocol.MAP_RESPONSE_PART or (is_last and key in pending):
186+
parts = pending.pop(key, {})
187+
parts[message.seq] = (is_last, message.payload)
188+
if (joined := _complete_payload(parts)) is not None:
189+
return joined
190+
_remember(key, parts)
191+
return None
192+
if is_last:
193+
length = _decoded_length(message.payload)
194+
if length is not None and length % AES.block_size != 0:
195+
# Trailing fragment of a split frame whose earlier parts have
196+
# not arrived yet; wait for them instead of failing the decode.
197+
_remember(key, {message.seq: (True, message.payload)})
198+
return None
199+
return message.payload
200+
return None
201+
202+
return assemble

‎tests/devices/rpc/test_b01_q7_channel.py‎

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import base64
23
import json
34
import math
45
import time
@@ -62,6 +63,35 @@ def build_map_response(self, payload: bytes) -> RoborockMessage:
6263
seq=self.seq,
6364
)
6465

66+
def build_map_part(
67+
self,
68+
payload: bytes,
69+
protocol: RoborockMessageProtocol,
70+
seq: int,
71+
random: int,
72+
timestamp: int,
73+
) -> RoborockMessage:
74+
"""Build a MAP_RESPONSE_PART (300) or MAP_RESPONSE (301) message."""
75+
return RoborockMessage(
76+
protocol=protocol,
77+
payload=payload,
78+
version=b"B01",
79+
seq=seq,
80+
random=random,
81+
timestamp=timestamp,
82+
)
83+
84+
85+
def _fragmented_map_payload() -> tuple[bytes, bytes, bytes]:
86+
"""A base64 map payload split into two fragments, like issue #1043.
87+
88+
The full payload decodes to 48 bytes (a multiple of the AES block size);
89+
each fragment alone decodes to 24 bytes, so neither fragment is decodable
90+
on its own.
91+
"""
92+
full = base64.b64encode(bytes(range(48)))
93+
return full, full[:32], full[32:]
94+
6595

6696
@pytest.fixture(name="fake_channel")
6797
def fake_channel_fixture() -> FakeChannel:
@@ -350,3 +380,167 @@ async def test_subscribe_map_pushes_skips_undecodable_frames(
350380
):
351381
fake_channel.notify_subscribers(message_builder.build_map_response(b"raw-map-payload"))
352382
assert received == [b"inflated-payload"]
383+
384+
385+
# ----------------------------------------------------------------------------- fragmented map responses (#1043)
386+
387+
MAP_RANDOM = 2716216127
388+
MAP_TIMESTAMP = 1794578237
389+
390+
391+
async def test_send_map_command_reassembles_fragmented_frames(
392+
device: HomeDataDevice,
393+
product: HomeDataProduct,
394+
fake_channel: FakeChannel,
395+
message_builder: B01MessageBuilder,
396+
) -> None:
397+
"""A map split into a 300 part and a 301 part is decoded once, joined."""
398+
channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type]
399+
full, first_part, last_part = _fragmented_map_payload()
400+
401+
with patch(
402+
"roborock.devices.rpc.b01_q7_channel.decode_map_payload",
403+
return_value=b"inflated-payload",
404+
) as mock_decode:
405+
task = asyncio.create_task(channel.send_map_command("service.upload_by_mapid", {"map_id": 123}))
406+
await asyncio.sleep(0)
407+
408+
fake_channel.notify_subscribers(
409+
message_builder.build_map_part(
410+
first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP
411+
)
412+
)
413+
await asyncio.sleep(0)
414+
assert not task.done() # the first part alone is not a complete frame
415+
fake_channel.notify_subscribers(
416+
message_builder.build_map_part(
417+
last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP
418+
)
419+
)
420+
assert await task == b"inflated-payload"
421+
mock_decode.assert_called_once()
422+
assert mock_decode.call_args[0][0] == full
423+
424+
425+
async def test_send_map_command_reassembles_out_of_order_fragments(
426+
device: HomeDataDevice,
427+
product: HomeDataProduct,
428+
fake_channel: FakeChannel,
429+
message_builder: B01MessageBuilder,
430+
) -> None:
431+
"""The trailing 301 part arriving first is buffered until the 300 part arrives."""
432+
channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type]
433+
full, first_part, last_part = _fragmented_map_payload()
434+
435+
with patch(
436+
"roborock.devices.rpc.b01_q7_channel.decode_map_payload",
437+
return_value=b"inflated-payload",
438+
) as mock_decode:
439+
task = asyncio.create_task(channel.send_map_command("service.upload_by_mapid", {"map_id": 123}))
440+
await asyncio.sleep(0)
441+
442+
fake_channel.notify_subscribers(
443+
message_builder.build_map_part(
444+
last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP
445+
)
446+
)
447+
await asyncio.sleep(0)
448+
assert not task.done() # the trailing fragment alone is not decodable
449+
fake_channel.notify_subscribers(
450+
message_builder.build_map_part(
451+
first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP
452+
)
453+
)
454+
assert await task == b"inflated-payload"
455+
mock_decode.assert_called_once()
456+
assert mock_decode.call_args[0][0] == full
457+
458+
459+
async def test_subscribe_map_pushes_reassembles_fragmented_frames(
460+
device: HomeDataDevice,
461+
product: HomeDataProduct,
462+
fake_channel: FakeChannel,
463+
message_builder: B01MessageBuilder,
464+
) -> None:
465+
"""Fragmented map pushes are delivered once, as one decoded frame."""
466+
channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type]
467+
full, first_part, last_part = _fragmented_map_payload()
468+
469+
received: list[bytes] = []
470+
await channel.subscribe_map_pushes(received.append)
471+
472+
with patch(
473+
"roborock.devices.rpc.b01_q7_channel.decode_map_payload",
474+
return_value=b"inflated-payload",
475+
) as mock_decode:
476+
fake_channel.notify_subscribers(
477+
message_builder.build_map_part(
478+
first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP
479+
)
480+
)
481+
assert received == []
482+
fake_channel.notify_subscribers(
483+
message_builder.build_map_part(
484+
last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP
485+
)
486+
)
487+
assert received == [b"inflated-payload"]
488+
mock_decode.assert_called_once()
489+
assert mock_decode.call_args[0][0] == full
490+
491+
492+
async def test_subscribe_map_pushes_buffers_lone_trailing_fragment(
493+
device: HomeDataDevice,
494+
product: HomeDataProduct,
495+
fake_channel: FakeChannel,
496+
message_builder: B01MessageBuilder,
497+
) -> None:
498+
"""A lone 301 fragment is buffered, not decoded, until its 300 part arrives."""
499+
channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type]
500+
_full, first_part, last_part = _fragmented_map_payload()
501+
502+
received: list[bytes] = []
503+
await channel.subscribe_map_pushes(received.append)
504+
505+
with patch(
506+
"roborock.devices.rpc.b01_q7_channel.decode_map_payload",
507+
return_value=b"inflated-payload",
508+
):
509+
fake_channel.notify_subscribers(
510+
message_builder.build_map_part(
511+
last_part, RoborockMessageProtocol.MAP_RESPONSE, 2, MAP_RANDOM, MAP_TIMESTAMP
512+
)
513+
)
514+
assert received == []
515+
fake_channel.notify_subscribers(
516+
message_builder.build_map_part(
517+
first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP
518+
)
519+
)
520+
assert received == [b"inflated-payload"]
521+
522+
523+
async def test_subscribe_map_pushes_ignores_incomplete_frame(
524+
device: HomeDataDevice,
525+
product: HomeDataProduct,
526+
fake_channel: FakeChannel,
527+
message_builder: B01MessageBuilder,
528+
) -> None:
529+
"""A lone 300 part with no final part delivers nothing."""
530+
channel = create_b01_q7_channel(device, product, fake_channel) # type: ignore[arg-type]
531+
_full, first_part, _last_part = _fragmented_map_payload()
532+
533+
received: list[bytes] = []
534+
await channel.subscribe_map_pushes(received.append)
535+
536+
with patch(
537+
"roborock.devices.rpc.b01_q7_channel.decode_map_payload",
538+
return_value=b"inflated-payload",
539+
) as mock_decode:
540+
fake_channel.notify_subscribers(
541+
message_builder.build_map_part(
542+
first_part, RoborockMessageProtocol.MAP_RESPONSE_PART, 1, MAP_RANDOM, MAP_TIMESTAMP
543+
)
544+
)
545+
assert received == []
546+
mock_decode.assert_not_called()

0 commit comments

Comments
 (0)