diff --git a/scripts/data_transfer_example.py b/scripts/data_transfer_example.py index 145fa4f..1710b5a 100755 --- a/scripts/data_transfer_example.py +++ b/scripts/data_transfer_example.py @@ -18,21 +18,12 @@ import argparse from pica import Host from pica.packets import uci +from helper import init MAX_DATA_PACKET_PAYLOAD_SIZE = 1024 async def data_message_send(host: Host, peer: Host, file: str): - await host.expect_control( - uci.DeviceStatusNtf(device_state=uci.DeviceState.DEVICE_STATE_READY)) - - host.send_control( - uci.DeviceResetCmd(reset_config=uci.ResetConfig.UWBS_RESET)) - - await host.expect_control( - uci.DeviceResetRsp(status=uci.StatusCode.UCI_STATUS_OK)) - - await host.expect_control( - uci.DeviceStatusNtf(device_state=uci.DeviceState.DEVICE_STATE_READY)) + await init(host) host.send_control( uci.SessionInitCmd( @@ -109,19 +100,19 @@ async def data_transfer(host: Host, dst_mac_address: bytes, file: str, session_i if len(b) > MAX_DATA_PACKET_PAYLOAD_SIZE: for i in range(0, len(b), MAX_DATA_PACKET_PAYLOAD_SIZE): - section = b[i:i+MAX_DATA_PACKET_PAYLOAD_SIZE] + chunk = b[i:i+MAX_DATA_PACKET_PAYLOAD_SIZE] if i + MAX_DATA_PACKET_PAYLOAD_SIZE >= len(b): host.send_data(uci.DataMessageSnd(session_handle=int(session_id), destination_address=int.from_bytes(dst_mac_address), data_sequence_number=seq_num, - application_data=section)) + application_data=chunk)) else: host.send_data(uci.DataMessageSnd(session_handle=int(session_id), pbf = uci.PacketBoundaryFlag.NOT_COMPLETE, destination_address=int.from_bytes(dst_mac_address), data_sequence_number=seq_num, - application_data=section)) + application_data=chunk)) seq_num += 1 if seq_num >= 65535: @@ -148,14 +139,14 @@ async def run(address: str, uci_port: int, http_port: int, file: str): try: host0 = await Host.connect(address, uci_port, bytes([0, 1])) host1 = await Host.connect(address, uci_port, bytes([0, 2])) - except Exception as exn: + except Exception: print( f'Failed to connect to Pica server at address {address}:{uci_port}\n' + 'Make sure the server is running') exit(1) async with asyncio.TaskGroup() as tg: - task0 = tg.create_task(data_message_send(host0, host1, file)) + tg.create_task(data_message_send(host0, host1, file)) host0.disconnect() host1.disconnect() diff --git a/scripts/helper.py b/scripts/helper.py new file mode 100644 index 0000000..bbce7ad --- /dev/null +++ b/scripts/helper.py @@ -0,0 +1,30 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from pica import Host +from pica.packets import uci + + +async def init(host: Host): + await host.expect_control( + uci.DeviceStatusNtf(device_state=uci.DeviceState.DEVICE_STATE_READY) + ) + + host.send_control(uci.DeviceResetCmd(reset_config=uci.ResetConfig.UWBS_RESET)) + + await host.expect_control(uci.DeviceResetRsp(status=uci.StatusCode.UCI_STATUS_OK)) + + await host.expect_control( + uci.DeviceStatusNtf(device_state=uci.DeviceState.DEVICE_STATE_READY) + ) diff --git a/scripts/ranging_example.py b/scripts/ranging_example.py index b932cfd..54bc35a 100755 --- a/scripts/ranging_example.py +++ b/scripts/ranging_example.py @@ -16,21 +16,13 @@ import asyncio import argparse + from pica import Host from pica.packets import uci +from helper import init async def controller(host: Host, peer: Host): - await host.expect_control( - uci.DeviceStatusNtf(device_state=uci.DeviceState.DEVICE_STATE_READY)) - - host.send_control( - uci.DeviceResetCmd(reset_config=uci.ResetConfig.UWBS_RESET)) - - await host.expect_control( - uci.DeviceResetRsp(status=uci.StatusCode.UCI_STATUS_OK)) - - await host.expect_control( - uci.DeviceStatusNtf(device_state=uci.DeviceState.DEVICE_STATE_READY)) + await init(host) host.send_control( uci.SessionInitCmd( @@ -107,7 +99,7 @@ async def controller(host: Host, peer: Host): uci.DeviceStatusNtf( device_state=uci.DeviceState.DEVICE_STATE_ACTIVE)) - for n in range(1, 3): + for _ in range(1, 3): event = await host.expect_control( uci.ShortMacTwoWaySessionInfoNtf, timeout=2.0) @@ -141,17 +133,7 @@ async def controller(host: Host, peer: Host): async def controlee(host: Host, peer: Host): - await host.expect_control( - uci.DeviceStatusNtf(device_state=uci.DeviceState.DEVICE_STATE_READY)) - - host.send_control( - uci.DeviceResetCmd(reset_config=uci.ResetConfig.UWBS_RESET)) - - await host.expect_control( - uci.DeviceResetRsp(status=uci.StatusCode.UCI_STATUS_OK)) - - await host.expect_control( - uci.DeviceStatusNtf(device_state=uci.DeviceState.DEVICE_STATE_READY)) + await expect_correct_startup(host) host.send_control( uci.SessionInitCmd( @@ -228,7 +210,7 @@ async def controlee(host: Host, peer: Host): uci.DeviceStatusNtf( device_state=uci.DeviceState.DEVICE_STATE_ACTIVE)) - for n in range(1, 3): + for _ in range(1, 3): event = await host.expect_control( uci.ShortMacTwoWaySessionInfoNtf, timeout=2.0) @@ -265,15 +247,15 @@ async def run(address: str, uci_port: int, http_port: int): try: host0 = await Host.connect(address, uci_port, bytes([0, 1])) host1 = await Host.connect(address, uci_port, bytes([0, 2])) - except Exception as exn: + except Exception: print( f'Failed to connect to Pica server at address {address}:{uci_port}\n' + 'Make sure the server is running') exit(1) async with asyncio.TaskGroup() as tg: - task0 = tg.create_task(controller(host0, host1)) - task1 = tg.create_task(controlee(host1, host0)) + tg.create_task(controller(host0, host1)) + tg.create_task(controlee(host1, host0)) host0.disconnect() host1.disconnect()