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
9 changes: 5 additions & 4 deletions py/pica/pica/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ async def _recv_data(self) -> bytes:
return await self.data_queue.get()

def send_control(self, packet: uci.ControlPacket):
# TODO packet fragmentation.
packet = bytearray(packet.serialize())
packet[3] = len(packet) - 4
self.writer.write(packet)
raw = packet.serialize()
chunks = [raw[i : i + 255] for i in range(4, len(raw), 255)] or [b""]
Comment on lines +100 to +101

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add defensive checks to ensure that the input packet is not None and that the serialized packet is at least 4 bytes long (the minimum size of a valid UCI control packet header). This prevents potential AttributeError and IndexError crashes during serialization and fragmentation.

Suggested change
raw = packet.serialize()
chunks = [raw[i : i + 255] for i in range(4, len(raw), 255)] or [b""]
if packet is None:
raise ValueError("Packet cannot be None")
raw = packet.serialize()
if len(raw) < 4:
raise ValueError(f"Control packet must be at least 4 bytes, got {len(raw)}")
chunks = [raw[i : i + 255] for i in range(4, len(raw), 255)] or [b""]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test client methods receive typed PDL packet instances that serialize to valid UCI headers; defensive type checks aren't used elsewhere in Host and would add unreached branches to test code.

for i, chunk in enumerate(chunks):
pbf = 0x10 if i < len(chunks) - 1 else 0
self.writer.write(bytes([raw[0] | pbf, raw[1], raw[2], len(chunk)]) + chunk)

def send_data(self, packet: uci.DataPacket):
packet = bytearray(packet.serialize())
Expand Down
72 changes: 72 additions & 0 deletions tests/ranging.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,78 @@ async def multicast_list_ntf(host: Host, peer: Host):
)
)

host.send_control(
uci.SessionUpdateControllerMulticastListCmd(
session_token=0,
action=uci.UpdateMulticastListAction.REMOVE_CONTROLEE,
payload=uci.SessionUpdateControllerMulticastListCmdPayload(
controlees=[
uci.Controlee(
short_address=list(peer.mac_address),
subsession_id=0,
)
]
).serialize(),
)
)
await host.expect_control(
uci.SessionUpdateControllerMulticastListRsp(
status=uci.Status.OK,
controlee_status=[
uci.ControleeStatus(
mac_address=list(peer.mac_address),
status=uci.MulticastUpdateStatus.OK_MULTICAST_LIST_UPDATE,
)
],
)
)

host.send_control(
uci.SessionSetAppConfigCmd(
session_token=0,
tlvs=[
uci.AppConfigTlv(
cfg_id=uci.AppConfigTlvType.STS_CONFIG,
v=bytes([uci.StsConfig.PROVISIONED_FOR_RESPONDER_SUB_SESSION_KEY]),
)
],
)
)
await host.expect_control(
uci.SessionSetAppConfigRsp(status=uci.Status.OK, cfg_status=[])
)

# 8 * 38-byte controlees -> 310-byte payload, placing the 8th MAC address in the 2nd PBF fragment.
controlees = [
uci.Controlee_V2_0_32_Byte_Version(
short_address=bytearray([0, i + 3]),
subsession_id=i + 1,
subsession_key=bytearray(32),
)
for i in range(8)
]
host.send_control(
uci.SessionUpdateControllerMulticastListCmd(
session_token=0,
action=uci.UpdateMulticastListAction.ADD_CONTROLEE_WITH_EXTENDED_SUB_SESSION_KEY,
payload=uci.SessionUpdateControllerMulticastListCmd_2_0_32_Byte_Payload(
controlees=controlees
).serialize(),
)
)
await host.expect_control(
uci.SessionUpdateControllerMulticastListRsp(
status=uci.Status.OK,
controlee_status=[
uci.ControleeStatus(
mac_address=[0, i + 3],
status=uci.MulticastUpdateStatus.OK_MULTICAST_LIST_UPDATE,
)
for i in range(8)
],
)
)

host.send_control(uci.SessionDeinitCmd(session_token=0))

await host.expect_control(uci.SessionDeinitRsp(status=uci.Status.OK))
Expand Down
Loading