Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions roborock/devices/rpc/b01_q7_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from roborock.exceptions import RoborockException
from roborock.protocols.b01_q7_protocol import (
B01_Q7_DPS,
B01_VERSION,
CommandType,
MapKey,
ParamsType,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions roborock/protocols/b01_q7_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
130 changes: 57 additions & 73 deletions tests/devices/rpc/test_b01_q7_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -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(
Expand Down
Loading
Loading