Skip to content

Commit

Permalink
Fix ISO packet issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zxzxwu committed Dec 4, 2023
1 parent 3adcc8b commit d6afbc6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions bumble/hci.py
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,9 @@ def from_bytes(packet: bytes) -> HCI_Packet:
if packet_type == HCI_EVENT_PACKET:
return HCI_Event.from_bytes(packet)

if packet_type == HCI_ISO_DATA_PACKET:
return HCI_IsoDataPacket.from_bytes(packet)

return HCI_CustomPacket(packet)

def __init__(self, name):
Expand Down Expand Up @@ -6098,7 +6101,7 @@ def from_bytes(packet: bytes) -> HCI_IsoDataPacket:
if ts_flag:
if not should_include_sdu_info:
logger.warn(f'Timestamp included when pb_flag={bin(pb_flag)}')
time_stamp, _ = struct.unpack_from('<I', packet, pos)
time_stamp, *_ = struct.unpack_from('<I', packet, pos)
pos += 4

if should_include_sdu_info:
Expand Down Expand Up @@ -6165,7 +6168,7 @@ def to_bytes(self) -> bytes:
self.packet_sequence_number,
self.iso_sdu_length | self.packet_status_flag << 14,
]
return struct.pack(fmt, args) + self.iso_sdu_fragment
return struct.pack(fmt, *args) + self.iso_sdu_fragment

def __str__(self) -> str:
return (
Expand Down
1 change: 1 addition & 0 deletions bumble/transport/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
hci.HCI_ACL_DATA_PACKET: (2, 2, 'H'),
hci.HCI_SYNCHRONOUS_DATA_PACKET: (1, 2, 'B'),
hci.HCI_EVENT_PACKET: (1, 1, 'B'),
hci.HCI_ISO_DATA_PACKET: (2, 2, 'H'),
}


Expand Down
26 changes: 26 additions & 0 deletions tests/hci_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
HCI_CustomPacket,
HCI_Disconnect_Command,
HCI_Event,
HCI_IsoDataPacket,
HCI_LE_Add_Device_To_Filter_Accept_List_Command,
HCI_LE_Advertising_Report_Event,
HCI_LE_Channel_Selection_Algorithm_Event,
Expand Down Expand Up @@ -486,6 +487,29 @@ def test_custom():
assert packet.payload == data


# -----------------------------------------------------------------------------
def test_iso_data_packet():
data = bytes.fromhex(
'05616044002ac9f0a193003c00e83b477b00eba8d41dc018bf1a980f0290afe1e7c37652096697'
'52b6a535a8df61e22931ef5a36281bc77ed6a3206d984bcdabee6be831c699cb50e2'
)
packet = HCI_IsoDataPacket.from_bytes(data)
assert packet.connection_handle == 0x0061
assert packet.packet_status_flag == 0
assert packet.pb_flag == 0x02
assert packet.ts_flag == 0x01
assert packet.data_total_length == 68
assert packet.time_stamp == 2716911914
assert packet.packet_sequence_number == 147
assert packet.iso_sdu_length == 60
assert packet.iso_sdu_fragment == bytes.fromhex(
'e83b477b00eba8d41dc018bf1a980f0290afe1e7c3765209669752b6a535a8df61e22931ef5a3'
'6281bc77ed6a3206d984bcdabee6be831c699cb50e2'
)

assert packet.to_bytes() == data


# -----------------------------------------------------------------------------
def run_test_events():
test_HCI_Event()
Expand Down Expand Up @@ -524,6 +548,7 @@ def run_test_commands():
test_HCI_LE_Set_Default_PHY_Command()
test_HCI_LE_Set_Extended_Scan_Parameters_Command()
test_HCI_LE_Set_Extended_Advertising_Enable_Command()
test_HCI_LE_Setup_ISO_Data_Path_Command()


# -----------------------------------------------------------------------------
Expand All @@ -532,3 +557,4 @@ def run_test_commands():
run_test_commands()
test_address()
test_custom()
test_iso_data_packet()

0 comments on commit d6afbc6

Please sign in to comment.