diff --git a/tests/api/test_request.py b/tests/api/test_request.py index 88c2083..9cd23fb 100644 --- a/tests/api/test_request.py +++ b/tests/api/test_request.py @@ -6,7 +6,7 @@ import zigpy_znp.commands as c import zigpy_znp.config as conf from zigpy_znp.exceptions import CommandNotRecognized, InvalidCommandResponse -from zigpy_znp.frames import GeneralFrame +from zigpy_znp.frames import GeneralFrame, TransportFrame import zigpy_znp.types as t @@ -327,3 +327,73 @@ async def test_send_failure_when_disconnected(connected_znp): await znp.request(c.SYS.Ping.Req()) assert "Coordinator is disconnected" in str(e.value) + + +def wire_bytes(znp_server) -> bytes: + return b"".join(m.args[0] for m in znp_server._uart.data_received.mock_calls) + + +async def test_srsp_timeout_resyncs_uart(connected_znp, mocker): + znp, znp_server = connected_znp + znp._config[conf.CONF_ZNP_CONFIG][conf.CONF_SREQ_TIMEOUT] = 0.1 + mocker.patch.object(znp, "connection_lost") + + znp_server.reply_to( + c.SYS.Ping.Req(), responses=[c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS)] + ) + + with pytest.raises(asyncio.TimeoutError): + await znp.request(c.UTIL.TimeAlive.Req()) + + # The request is followed by the flush and a ping, in that order + assert wire_bytes(znp_server) == ( + TransportFrame(c.UTIL.TimeAlive.Req().to_frame()).serialize() + + b"\x01" + + b"\x00" * 257 + + TransportFrame(c.SYS.Ping.Req().to_frame()).serialize() + ) + + # The radio answered the ping, so we stay connected + assert len(znp.connection_lost.mock_calls) == 0 + assert not any(znp._listeners.values()) + + +async def test_srsp_timeout_resync_failure_disconnects(connected_znp, mocker): + znp, znp_server = connected_znp + znp._config[conf.CONF_ZNP_CONFIG][conf.CONF_SREQ_TIMEOUT] = 0.1 + mocker.patch.object(znp, "connection_lost") + + with pytest.raises(asyncio.TimeoutError): + await znp.request(c.UTIL.TimeAlive.Req()) + + assert len(znp.connection_lost.mock_calls) == 1 + assert isinstance(znp.connection_lost.mock_calls[0].args[0], TimeoutError) + assert not any(znp._listeners.values()) + + +async def test_srsp_resync_blocks_other_requests(connected_znp): + znp, znp_server = connected_znp + znp._config[conf.CONF_ZNP_CONFIG][conf.CONF_SREQ_TIMEOUT] = 0.1 + + znp_server.reply_to( + c.SYS.Ping.Req(), responses=[c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS)] + ) + + lost = asyncio.create_task(znp.request(c.UTIL.TimeAlive.Req())) + await asyncio.sleep(0) + ping = asyncio.create_task(znp.request(c.SYS.Ping.Req())) + + with pytest.raises(asyncio.TimeoutError): + await lost + + await ping + + # The queued ping is sent only after the resync ping + ping_frame = TransportFrame(c.SYS.Ping.Req().to_frame()).serialize() + assert wire_bytes(znp_server) == ( + TransportFrame(c.UTIL.TimeAlive.Req().to_frame()).serialize() + + b"\x01" + + b"\x00" * 257 + + ping_frame + + ping_frame + ) diff --git a/zigpy_znp/api.py b/zigpy_znp/api.py index e55d5a7..86b249b 100644 --- a/zigpy_znp/api.py +++ b/zigpy_znp/api.py @@ -45,6 +45,8 @@ STARTUP_TIMEOUT = 15 AFTER_BOOTLOADER_SKIP_BYTE_DELAY = 2.5 NETWORK_COMMISSIONING_TIMEOUT = 60 +# The firmware starts the network before it sends the SRSP +NETWORK_START_SREQ_TIMEOUT = 15 BOOTLOADER_PIN_TOGGLE_DELAY = 0.15 CONNECT_PING_TIMEOUT = 0.50 CONNECT_PROBE_TIMEOUT = 10 @@ -270,6 +272,7 @@ async def start_network(self): RemainingModes=c.app_config.BDBCommissioningMode.NONE, ), timeout=NETWORK_COMMISSIONING_TIMEOUT, + sreq_timeout=NETWORK_START_SREQ_TIMEOUT, ) # This is the correct startup sequence according to the forums, @@ -284,7 +287,10 @@ async def start_network(self): ) else: # In Z-Stack 1.2.2, StartupFromApp actually does what it says - rsp = await self.request(c.ZDO.StartupFromApp.Req(StartDelay=100)) + rsp = await self.request( + c.ZDO.StartupFromApp.Req(StartDelay=100), + timeout=NETWORK_START_SREQ_TIMEOUT, + ) if rsp.State not in ( c.zdo.StartupState.NewNetworkState, @@ -755,6 +761,38 @@ async def connect(self, *, test_port=True) -> None: LOGGER.debug("Connected to %s", self._uart.url) + def flood_uart(self) -> None: + """ + Pushes the firmware's MT frame parser out of a frame with lost bytes. The worst + case is a parser locked onto a SoF and 0xFF length inside a frame's payload, + which needs 258 more bytes. The filler XORs to 0x01 at every length, so the + broken frame passes the FCS check with only a ~1/256 chance. It also contains + no SoF, so the parser cannot lock onto a new frame in the middle of the filler. + """ + + LOGGER.debug("Flooding radio UART to reset the MT frame parser") + self._uart.write(b"\x01" + b"\x00" * 257) + + async def _resync_uart(self) -> None: + """ + Recovers from a lost SRSP. Must be called with the request lock held, so that + no other frame is eaten by a stuck parser. Reconnects if the radio stays silent. + """ + + ping_rsp = self.wait_for_response(c.SYS.Ping.Rsp(partial=True)) + + self.flood_uart() + self._uart.send(c.SYS.Ping.Req().to_frame(align=self.nvram.align_structs)) + + try: + async with asyncio.timeout(self._znp_config[conf.CONF_SREQ_TIMEOUT]): + await ping_rsp + except TimeoutError as exc: + LOGGER.warning("Radio did not respond after UART resync") + self.connection_lost(exc) + else: + LOGGER.debug("Radio responded after UART resync") + def connection_made(self) -> None: """ Called by the UART object to indicate that the port was opened. @@ -1064,11 +1102,20 @@ async def request( self._uart.send(frame) # We should get a SRSP in a reasonable amount of time - async with asyncio.timeout( - timeout or self._znp_config[conf.CONF_SREQ_TIMEOUT] - ): - # We lock until either a sync response is seen or an error occurs - response = await response_future + try: + async with asyncio.timeout( + timeout or self._znp_config[conf.CONF_SREQ_TIMEOUT] + ): + # We lock until either a sync response is seen or an error occurs + response = await response_future + except TimeoutError: + # Z-Stack's MT frame parser has no timeout. If the radio drops bytes of + # our frame (e.g. a silent UART RX overrun), the parser waits for the + # rest and eats the start of the next frames. A lost SRSP is the only + # sign of this, so resync before any other request is sent. + LOGGER.warning("No SRSP for %s, resynchronizing UART", request) + await self._resync_uart() + raise if isinstance(response, c.RPCError.CommandNotRecognized.Rsp): raise CommandNotRecognized( @@ -1084,7 +1131,14 @@ async def request( return response async def request_callback_rsp( - self, *, request, callback, timeout=None, background=False, **response_params + self, + *, + request, + callback, + timeout=None, + sreq_timeout=None, + background=False, + **response_params, ): """ Sends an SREQ, gets its SRSP confirmation, and waits for its real AREQ response. @@ -1103,11 +1157,14 @@ async def request_callback_rsp( callback_rsp, listener = self.wait_for_responses([callback], context=True) + # The SRSP gets its own timeout: reusing the callback timeout would hold the + # request lock for that long if the SRSP is lost + # Typical request/response/callbacks are not backgrounded if not background: try: async with asyncio.timeout(timeout): - await self.request(request, timeout=timeout, **response_params) + await self.request(request, timeout=sreq_timeout, **response_params) return await callback_rsp finally: @@ -1118,7 +1175,9 @@ async def request_callback_rsp( try: async with asyncio.timeout(timeout): - request_rsp = await self.request(request, **response_params) + request_rsp = await self.request( + request, timeout=sreq_timeout, **response_params + ) except Exception: # If the SREQ/SRSP pair fails, we must cancel the AREQ listener self.remove_listener(listener) diff --git a/zigpy_znp/config.py b/zigpy_znp/config.py index ceed731..9f3ac2d 100644 --- a/zigpy_znp/config.py +++ b/zigpy_znp/config.py @@ -95,7 +95,10 @@ def validator(value: typing.Any) -> None: vol.Optional(CONF_TX_POWER, default=None): vol.Any( None, vol.All(int, vol.Range(min=-22, max=22)) ), - vol.Optional(CONF_SREQ_TIMEOUT, default=15): VolPositiveNumber, + # SRSPs from user logs take under 150 ms on healthy setups and + # under 2.4 s on stalled hosts. A lost SRSP holds the request lock + # until this timeout, so it should not be much longer than that. + vol.Optional(CONF_SREQ_TIMEOUT, default=3): VolPositiveNumber, vol.Optional(CONF_ARSP_TIMEOUT, default=30): VolPositiveNumber, vol.Optional( CONF_AUTO_RECONNECT_RETRY_DELAY, default=5