Exemplo n.º 1
0
 def send_responses():
     api._uart.data_received(
         TransportFrame(
             c.AF.DataRequest.Rsp(
                 Status=t.Status.SUCCESS).to_frame()).serialize() +
         TransportFrame(
             c.AF.DataConfirm.Callback(
                 Endpoint=56, TSN=1,
                 Status=t.Status.SUCCESS).to_frame()).serialize())
Exemplo n.º 2
0
def test_uart_rx_byte_by_byte_garbage():
    api = mock.Mock()
    transport = mock.Mock()

    uart = znp_uart.ZnpMtProtocol(api)
    uart.connection_made(transport)

    test_command = c.SYS.ResetInd.Callback(
        Reason=t.ResetReason.PowerUp,
        TransportRev=0x00,
        ProductId=0x45,
        MajorRel=0x01,
        MinorRel=0x02,
        MaintRel=0x03,
    )
    test_frame = test_command.to_frame()
    test_frame_bytes = TransportFrame(test_frame).serialize()

    data = b""
    data += bytes.fromhex("58 4a 72 35 51 da 60 ed 1f")
    data += bytes.fromhex("03 6d b6")
    data += bytes.fromhex("ee 90")
    data += test_frame_bytes
    data += bytes.fromhex("00 00")
    data += bytes.fromhex("e4 4f 51 b2 39 4b 8d e3 ca 61")
    data += bytes.fromhex("8c 56 8a 2c d8 22 64 9e 9d 7b")

    # The frame should be parsed identically regardless of framing
    for byte in data:
        uart.data_received(bytes([byte]))

    api.frame_received.assert_called_once_with(test_frame)
Exemplo n.º 3
0
def test_uart_frame_received_error():
    transport = mock.Mock()

    api = mock.Mock()
    api.frame_received = mock.Mock(side_effect=RuntimeError("An error"))

    with pytest.raises(RuntimeError):
        api.frame_received(None)

    uart = znp_uart.ZnpMtProtocol(api)
    uart.connection_made(transport)

    test_command = c.SYS.ResetInd.Callback(
        Reason=t.ResetReason.PowerUp,
        TransportRev=0x00,
        ProductId=0x45,
        MajorRel=0x01,
        MinorRel=0x02,
        MaintRel=0x03,
    )
    test_frame = test_command.to_frame()
    test_frame_bytes = TransportFrame(test_frame).serialize()

    # Errors thrown by api.frame_received should not impact how many frames are handled
    uart.data_received(test_frame_bytes * 3)

    # We should have received all three frames
    api.frame_received.call_count == 3
Exemplo n.º 4
0
def test_uart_rx_sof_stress():
    api = mock.Mock()
    transport = mock.Mock()

    uart = znp_uart.ZnpMtProtocol(api)
    uart.connection_made(transport)

    test_command = c.SYS.ResetInd.Callback(
        Reason=t.ResetReason.PowerUp,
        TransportRev=0x00,
        ProductId=0x45,
        MajorRel=0x01,
        MinorRel=0x02,
        MaintRel=0x03,
    )
    test_frame = test_command.to_frame()
    test_frame_bytes = TransportFrame(test_frame).serialize()

    # We include an almost-valid frame and many stray SoF markers
    uart.data_received(b"\xFE" + b"\xFE" + b"\xFE" + test_frame_bytes[:-1] + b"\x00")
    uart.data_received(b"\xFE\xFE\x00\xFE\x01")
    uart.data_received(b"\xFE" + b"\xFE" + b"\xFE" + test_frame_bytes + b"\x00\x00")

    # We should see the valid frame exactly once
    api.frame_received.assert_called_once_with(test_frame)
Exemplo n.º 5
0
async def test_znp_uart(znp, event_loop):
    ping_rsp = c.SysCommands.Ping.Rsp(Capabilities=t.MTCapabilities.CAP_SYS)

    event_loop.call_soon(znp.frame_received, ping_rsp.to_frame())
    response = await znp.request(c.SysCommands.Ping.Req())

    assert ping_rsp == response

    frame, _ = TransportFrame.deserialize(bytes.fromhex("FE   00   21 01   20"))

    znp._uart.send.assert_called_once_with(frame.payload)
Exemplo n.º 6
0
def test_uart_rx_basic():
    api = mock.Mock()
    transport = mock.Mock()

    uart = znp_uart.ZnpMtProtocol(api)
    uart.connection_made(transport)

    test_command = c.SYS.ResetInd.Callback(
        Reason=t.ResetReason.PowerUp,
        TransportRev=0x00,
        ProductId=0x45,
        MajorRel=0x01,
        MinorRel=0x02,
        MaintRel=0x03,
    )
    test_frame = test_command.to_frame()
    test_frame_bytes = TransportFrame(test_frame).serialize()

    uart.data_received(test_frame_bytes)

    api.frame_received.assert_called_once_with(test_frame)
Exemplo n.º 7
0
def test_uart_rx_corrupted_fcs():
    api = mock.Mock()
    transport = mock.Mock()

    uart = znp_uart.ZnpMtProtocol(api)
    uart.connection_made(transport)

    test_command = c.SYS.ResetInd.Callback(
        Reason=t.ResetReason.PowerUp,
        TransportRev=0x00,
        ProductId=0x45,
        MajorRel=0x01,
        MinorRel=0x02,
        MaintRel=0x03,
    )
    test_frame = test_command.to_frame()
    test_frame_bytes = TransportFrame(test_frame).serialize()

    # Almost, but not quite
    uart.data_received(test_frame_bytes[:-1])
    uart.data_received(b"\x00")

    assert not api.frame_received.called