Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python scripts: add helper.py to gather common functionnalities #52

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 7 additions & 16 deletions scripts/data_transfer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,12 @@
import argparse
from pica import Host
from pica.packets import uci
from helper import expect_correct_startup

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 expect_correct_startup(host)

host.send_control(
uci.SessionInitCmd(
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down
30 changes: 30 additions & 0 deletions scripts/helper.py
Original file line number Diff line number Diff line change
@@ -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 expect_correct_startup(host: Host):
SilverBzH marked this conversation as resolved.
Show resolved Hide resolved
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)
)
36 changes: 9 additions & 27 deletions scripts/ranging_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,13 @@

import asyncio
import argparse

from pica import Host
from pica.packets import uci
from helper import expect_correct_startup

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 expect_correct_startup(host)

host.send_control(
uci.SessionInitCmd(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
Loading