From 79c86d311d8b8e42e02d17b09f3b001ae33abafa Mon Sep 17 00:00:00 2001 From: Josh Wu Date: Thu, 26 Dec 2024 15:16:25 +0800 Subject: [PATCH] CS commands and events --- bumble/device.py | 74 +++++ bumble/hci.py | 535 ++++++++++++++++++++++++++++++++++- bumble/host.py | 18 ++ examples/run_cs_initiator.py | 68 +++++ examples/run_cs_reflector.py | 64 +++++ 5 files changed, 756 insertions(+), 3 deletions(-) create mode 100644 examples/run_cs_initiator.py create mode 100644 examples/run_cs_reflector.py diff --git a/bumble/device.py b/bumble/device.py index 54fd92c6..1bbd5cd3 100644 --- a/bumble/device.py +++ b/bumble/device.py @@ -1116,6 +1116,31 @@ async def terminate(self) -> None: await terminated.wait() +# ----------------------------------------------------------------------------- +@dataclass +class ChannelSoundingCapabilities: + num_config_supported: int + max_consecutive_procedures_supported: int + num_antennas_supported: int + max_antenna_paths_supported: int + roles_supported: int + modes_supported: int + rtt_capability: int + rtt_aa_only_n: int + rtt_sounding_n: int + rtt_random_payload_n: int + nadm_sounding_capability: int + nadm_random_capability: int + cs_sync_phys_supported: int + subfeatures_supported: int + t_ip1_times_supported: int + t_ip2_times_supported: int + t_fcs_times_supported: int + t_pm_times_supported: int + t_sw_time_supported: int + tx_snr_capability: int + + # ----------------------------------------------------------------------------- class LePhyOptions: # Coded PHY preference @@ -4481,6 +4506,55 @@ def on_failure(handle: int, status: int): ) return await read_feature_future + async def get_remote_cs_capabilities( + self, connection: Connection + ) -> ChannelSoundingCapabilities: + with closing(EventWatcher()) as watcher: + complete_future: asyncio.Future[ChannelSoundingCapabilities] = ( + asyncio.get_running_loop().create_future() + ) + + def on_event( + event: hci.HCI_LE_CS_Read_Remote_Supported_Capabilities_Complete_Event, + ): + if event.connection_handle != connection.handle: + return + if event.status != hci.HCI_SUCCESS: + complete_future.set_exception(hci.HCI_Error(event.status)) + complete_future.set_result( + ChannelSoundingCapabilities( + num_config_supported=event.num_config_supported, + max_consecutive_procedures_supported=event.max_consecutive_procedures_supported, + num_antennas_supported=event.num_antennas_supported, + max_antenna_paths_supported=event.max_antenna_paths_supported, + roles_supported=event.roles_supported, + modes_supported=event.modes_supported, + rtt_capability=event.rtt_capability, + rtt_aa_only_n=event.rtt_aa_only_n, + rtt_sounding_n=event.rtt_sounding_n, + rtt_random_payload_n=event.rtt_random_payload_n, + nadm_sounding_capability=event.nadm_sounding_capability, + nadm_random_capability=event.nadm_random_capability, + cs_sync_phys_supported=event.cs_sync_phys_supported, + subfeatures_supported=event.subfeatures_supported, + t_ip1_times_supported=event.t_ip1_times_supported, + t_ip2_times_supported=event.t_ip2_times_supported, + t_fcs_times_supported=event.t_fcs_times_supported, + t_pm_times_supported=event.t_pm_times_supported, + t_sw_time_supported=event.t_sw_time_supported, + tx_snr_capability=event.tx_snr_capability, + ) + ) + + watcher.on(self.host, 'cs_read_remote_supported_capabilities', on_event) + await self.send_command( + hci.HCI_LE_CS_Read_Remote_Supported_Capabilities_Command( + connection_handle=connection.handle + ), + check_result=True, + ) + return await complete_future + @host_event_handler def on_flush(self): self.emit('flush') diff --git a/bumble/hci.py b/bumble/hci.py index cb4ee8c0..4efdcb1f 100644 --- a/bumble/hci.py +++ b/bumble/hci.py @@ -275,7 +275,7 @@ def phy_list_to_bits(phys: Optional[Iterable[int]]) -> int: HCI_LE_CS_READ_REMOTE_FAE_TABLE_COMPLETE_EVENT = 0x2D HCI_LE_CS_SECURITY_ENABLE_COMPLETE_EVENT = 0x2E HCI_LE_CS_CONFIG_COMPLETE_EVENT = 0x2F -HCI_LE_CS_PROCEDURE_ENABLE_EVENT = 0x30 +HCI_LE_CS_PROCEDURE_ENABLE_COMPLETE_EVENT = 0x30 HCI_LE_CS_SUBEVENT_RESULT_EVENT = 0x31 HCI_LE_CS_SUBEVENT_RESULT_CONTINUE_EVENT = 0x32 HCI_LE_CS_TEST_END_COMPLETE_EVENT = 0x33 @@ -599,7 +599,7 @@ def phy_list_to_bits(phys: Optional[Iterable[int]]) -> int: HCI_LE_READ_ALL_REMOTE_FEATURES_COMMAND = hci_command_op_code(0x08, 0x0088) HCI_LE_CS_READ_LOCAL_SUPPORTED_CAPABILITIES_COMMAND = hci_command_op_code(0x08, 0x0089) HCI_LE_CS_READ_REMOTE_SUPPORTED_CAPABILITIES_COMMAND = hci_command_op_code(0x08, 0x008A) -HCI_LE_CS_WRITE_CACHED_REMOTE_SUPPORTED_CAPABILITIES = hci_command_op_code(0x08, 0x008B) +HCI_LE_CS_WRITE_CACHED_REMOTE_SUPPORTED_CAPABILITIES_COMMAND = hci_command_op_code(0x08, 0x008B) HCI_LE_CS_SECURITY_ENABLE_COMMAND = hci_command_op_code(0x08, 0x008C) HCI_LE_CS_SET_DEFAULT_SETTINGS_COMMAND = hci_command_op_code(0x08, 0x008D) HCI_LE_CS_READ_REMOTE_FAE_TABLE_COMMAND = hci_command_op_code(0x08, 0x008E) @@ -971,7 +971,7 @@ class PhyBit(enum.IntFlag): HCI_READ_ENCRYPTION_KEY_SIZE_COMMAND : 1 << (20*8+4), HCI_LE_CS_READ_LOCAL_SUPPORTED_CAPABILITIES_COMMAND : 1 << (20*8+5), HCI_LE_CS_READ_REMOTE_SUPPORTED_CAPABILITIES_COMMAND : 1 << (20*8+6), - HCI_LE_CS_WRITE_CACHED_REMOTE_SUPPORTED_CAPABILITIES : 1 << (20*8+7), + HCI_LE_CS_WRITE_CACHED_REMOTE_SUPPORTED_CAPABILITIES_COMMAND : 1 << (20*8+7), HCI_SET_EVENT_MASK_PAGE_2_COMMAND : 1 << (22*8+2), HCI_READ_FLOW_CONTROL_MODE_COMMAND : 1 << (23*8+0), HCI_WRITE_FLOW_CONTROL_MODE_COMMAND : 1 << (23*8+1), @@ -5059,6 +5059,264 @@ class HCI_LE_Set_Host_Feature_Command(HCI_Command): ''' +# ----------------------------------------------------------------------------- +@HCI_Command.command( + return_parameters_fields=[ + ('status', STATUS_SPEC), + ('num_config_supported', 1), + ('max_consecutive_procedures_supported', 2), + ('num_antennas_supported', 1), + ('max_antenna_paths_supported', 1), + ('roles_supported', 1), + ('modes_supported', 1), + ('rtt_capability', 1), + ('rtt_aa_only_n', 1), + ('rtt_sounding_n', 1), + ('rtt_random_payload_n', 1), + ('nadm_sounding_capability', 2), + ('nadm_random_capability', 2), + ('cs_sync_phys_supported', 1), + ('subfeatures_supported', 2), + ('t_ip1_times_supported', 2), + ('t_ip2_times_supported', 2), + ('t_fcs_times_supported', 2), + ('t_pm_times_supported', 2), + ('t_sw_time_supported', 1), + ('tx_snr_capability', 1), + ] +) +class HCI_LE_CS_Read_Local_Supported_Capabilities_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.130 LE CS Read Local Supported Capabilities command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command([('connection_handle', 2)]) +class HCI_LE_CS_Read_Remote_Supported_Capabilities_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.131 LE CS Read Remote Supported Capabilities command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command( + [ + ('connection_handle', 2), + ('num_config_supported', 1), + ('max_consecutive_procedures_supported', 2), + ('num_antennas_supported', 1), + ('max_antenna_paths_supported', 1), + ('roles_supported', 1), + ('modes_supported', 1), + ('rtt_capability', 1), + ('rtt_aa_only_n', 1), + ('rtt_sounding_n', 1), + ('rtt_random_payload_n', 1), + ('nadm_sounding_capability', 2), + ('nadm_random_capability', 2), + ('cs_sync_phys_supported', 1), + ('subfeatures_supported', 2), + ('t_ip1_times_supported', 2), + ('t_ip2_times_supported', 2), + ('t_fcs_times_supported', 2), + ('t_pm_times_supported', 2), + ('t_sw_time_supported', 1), + ('tx_snr_capability', 1), + ], + return_parameters_fields=[ + ('status', STATUS_SPEC), + ('connection_handle', 2), + ], +) +class HCI_LE_CS_Write_Cached_Remote_Supported_Capabilities_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.132 LE CS Write Cached Remote Supported Capabilities command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command([('connection_handle', 2)]) +class HCI_LE_CS_Security_Enable_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.133 LE CS Security Enable command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command( + [ + ('connection_handle', 2), + ('role_enable', 1), + ('cs_sync_antenna_selection', 1), + ('max_tx_power', 1), + ], + return_parameters_fields=[('status', STATUS_SPEC), ('connection_handle', 2)], +) +class HCI_LE_CS_Set_Default_Settings_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.134 LE CS Security Enable command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command([('connection_handle', 2)]) +class HCI_LE_CS_Read_Remote_FAE_Table_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.135 LE CS Read Remote FAE Table command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command( + [ + ('connection_handle', 2), + ('remote_fae_table', 72), + ], + return_parameters_fields=[('status', STATUS_SPEC), ('connection_handle', 2)], +) +class HCI_LE_CS_Write_Cached_Remote_FAE_Table_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.136 LE CS Write Cached Remote FAE Table command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command( + [ + ('connection_handle', 2), + ('config_id', 1), + ('create_context', 1), + ('main_mode_type', 1), + ('sub_mode_type', 1), + ('min_main_mode_steps', 1), + ('max_main_mode_steps', 1), + ('main_mode_repetition', 1), + ('mode_0_steps', 1), + ('role', 1), + ('rtt_type', 1), + ('cs_sync_phy', 1), + ('channel_map', 10), + ('channel_map_repetition', 1), + ('channel_selection_type', 1), + ('ch3c_shape', 1), + ('ch3c_jump', 1), + ('reserved', 1), + ], +) +class HCI_LE_CS_Create_Config_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.137 LE CS Create Config command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command( + [ + ('connection_handle', 2), + ('config_id', 1), + ], +) +class HCI_LE_CS_Remove_Config_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.138 LE CS Remove Config command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command( + [('channel_classification', 10)], return_parameters_fields=[('status', STATUS_SPEC)] +) +class HCI_LE_CS_Set_Channel_Classification_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.139 LE CS Set Channel Classification command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command( + [ + ('connection_handle', 2), + ('config_id', 1), + ('max_procedure_len', 2), + ('min_procedure_interval', 2), + ('max_procedure_interval', 2), + ('max_procedure_count', 2), + ('min_subevent_len', 3), + ('max_subevent_len', 3), + ('tone_antenna_config_selection', 1), + ('phy', 1), + ('tx_power_delta', 1), + ('preferred_peer_antenna', 1), + ('snr_control_initiator', 1), + ('snr_control_reflector', 1), + ], + return_parameters_fields=[('status', STATUS_SPEC), ('connection_handle', 2)], +) +class HCI_LE_CS_Set_Procedure_Parameters_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.140 LE CS Set Procedure Parameters command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command( + [ + ('connection_handle', 2), + ('config_id', 1), + ('enable', 1), + ], +) +class HCI_LE_CS_Procedure_Enable_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.141 LE CS Procedure Enable command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command( + [ + ('main_mode_type', 1), + ('sub_mode_type', 1), + ('main_mode_repetition', 1), + ('mode_0_steps', 1), + ('role', 1), + ('rtt_type', 1), + ('cs_sync_phy', 1), + ('cs_sync_antenna_selection', 1), + ('subevent_len', 3), + ('subevent_interval', 2), + ('max_num_subevents', 1), + ('transmit_power_level', 1), + ('t_ip1_time', 1), + ('t_ip2_time', 1), + ('t_fcs_time', 1), + ('t_pm_time', 1), + ('t_sw_time', 1), + ('tone_antenna_config_selection', 1), + ('reserved', 1), + ('snr_control_initiator', 1), + ('snr_control_reflector', 1), + ('drbg_nonce', 2), + ('channel_map_repetition', 1), + ('override_config', 2), + ('override_parameters_data', 'v'), + ], +) +class HCI_LE_CS_Test_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.142 LE CS Test command + ''' + + +# ----------------------------------------------------------------------------- +@HCI_Command.command() +class HCI_LE_CS_Test_End_Command(HCI_Command): + ''' + See Bluetooth spec @ 7.8.143 LE CS Test End command + ''' + + # ----------------------------------------------------------------------------- # HCI Events # ----------------------------------------------------------------------------- @@ -5996,6 +6254,277 @@ class HCI_LE_BIGInfo_Advertising_Report_Event(HCI_LE_Meta_Event): ''' +# ----------------------------------------------------------------------------- +@HCI_LE_Meta_Event.event( + [ + ('status', STATUS_SPEC), + ('connection_handle', 2), + ('num_config_supported', 1), + ('max_consecutive_procedures_supported', 2), + ('num_antennas_supported', 1), + ('max_antenna_paths_supported', 1), + ('roles_supported', 1), + ('modes_supported', 1), + ('rtt_capability', 1), + ('rtt_aa_only_n', 1), + ('rtt_sounding_n', 1), + ('rtt_random_payload_n', 1), + ('nadm_sounding_capability', 2), + ('nadm_random_capability', 2), + ('cs_sync_phys_supported', 1), + ('subfeatures_supported', 2), + ('t_ip1_times_supported', 2), + ('t_ip2_times_supported', 2), + ('t_fcs_times_supported', 2), + ('t_pm_times_supported', 2), + ('t_sw_time_supported', 1), + ('tx_snr_capability', 1), + ] +) +class HCI_LE_CS_Read_Remote_Supported_Capabilities_Complete_Event(HCI_LE_Meta_Event): + ''' + See Bluetooth spec @ 7.7.65.39 LE CS Read Remote Supported Capabilities Complete event + ''' + + status: int + connection_handle: int + num_config_supported: int + max_consecutive_procedures_supported: int + num_antennas_supported: int + max_antenna_paths_supported: int + roles_supported: int + modes_supported: int + rtt_capability: int + rtt_aa_only_n: int + rtt_sounding_n: int + rtt_random_payload_n: int + nadm_sounding_capability: int + nadm_random_capability: int + cs_sync_phys_supported: int + subfeatures_supported: int + t_ip1_times_supported: int + t_ip2_times_supported: int + t_fcs_times_supported: int + t_pm_times_supported: int + t_sw_time_supported: int + tx_snr_capability: int + + +# ----------------------------------------------------------------------------- +@HCI_LE_Meta_Event.event( + [ + ('status', STATUS_SPEC), + ('connection_handle', 2), + ('remote_fae_table', 72), + ] +) +class HCI_LE_CS_Read_Remote_FAE_Table_Complete_Event(HCI_LE_Meta_Event): + ''' + See Bluetooth spec @ 7.7.65.40 LE CS Read Remote FAE Table Complete event + ''' + + status: int + connection_handle: int + remote_fae_table: int + + +# ----------------------------------------------------------------------------- +@HCI_LE_Meta_Event.event( + [ + ('status', STATUS_SPEC), + ('connection_handle', 2), + ] +) +class HCI_LE_CS_Security_Enable_Complete_Event(HCI_LE_Meta_Event): + ''' + See Bluetooth spec @ 7.7.65.41 LE CS Security Enable Complete event + ''' + + status: int + connection_handle: int + + +# ----------------------------------------------------------------------------- +@HCI_LE_Meta_Event.event( + [ + ('status', STATUS_SPEC), + ('connection_handle', 2), + ('config_id', 1), + ('action', 1), + ('main_mode_type', 1), + ('sub_mode_type', 1), + ('min_main_mode_steps', 1), + ('max_main_mode_steps', 1), + ('main_mode_repetition', 1), + ('mode_0_steps', 1), + ('role', 1), + ('rtt_type', 1), + ('cs_sync_phy', 1), + ('channel_map', 10), + ('channel_map_repetition', 1), + ('channel_selection_type', 1), + ('ch3c_shape', 1), + ('ch3c_jump', 1), + ('reserved', 1), + ('t_ip1_time', 1), + ('t_ip2_time', 1), + ('t_fcs_time', 1), + ('t_pm_time', 1), + ] +) +class HCI_LE_CS_Config_Complete_Event(HCI_LE_Meta_Event): + ''' + See Bluetooth spec @ 7.7.65.42 LE CS Config Complete event + ''' + + status: int + connection_handle: int + config_id: int + action: int + main_mode_type: int + sub_mode_type: int + min_main_mode_steps: int + max_main_mode_steps: int + main_mode_repetition: int + mode_0_steps: int + role: int + rtt_type: int + cs_sync_phy: int + channel_map: bytes + channel_map_repetition: int + channel_selection_type: int + ch3c_shape: int + ch3c_jump: int + reserved: int + t_ip1_time: int + t_ip2_time: int + t_fcs_time: int + t_pm_time: int + + +# ----------------------------------------------------------------------------- +@HCI_LE_Meta_Event.event( + [ + ('status', STATUS_SPEC), + ('connection_handle', 2), + ('config_id', 1), + ('state', 1), + ('tone_antenna_config_selection', 1), + ('selected_tx_power', 1), + ('subevent_len', 3), + ('subevents_per_event', 1), + ('subevent_interval', 2), + ('event_interval', 2), + ('procedure_interval', 2), + ('procedure_count', 2), + ('max_procedure_len', 2), + ] +) +class HCI_LE_CS_Procedure_Enable_Complete_Event(HCI_LE_Meta_Event): + ''' + See Bluetooth spec @ 7.7.65.43 LE CS Procedure Enable Complete event + ''' + + status: int + connection_handle: int + config_id: int + state: int + tone_antenna_config_selection: int + selected_tx_power: int + subevent_len: int + subevents_per_event: int + subevent_interval: int + event_interval: int + procedure_interval: int + procedure_count: int + max_procedure_len: int + + +# ----------------------------------------------------------------------------- +@HCI_LE_Meta_Event.event( + [ + ('connection_handle', 2), + ('config_id', 1), + ('start_acl_conn_event_counter', 2), + ('procedure_counter', 2), + ('frequency_compensation', 2), + ('reference_power_level', 1), + ('procedure_done_status', 1), + ('subevent_done_status', 1), + ('abort_reason', 1), + ('num_antenna_paths', 1), + [ + ('step_mode', 1), + ('step_channel', 1), + ('step_data', 'v'), + ], + ] +) +class HCI_LE_CS_Subevent_Result_Event(HCI_LE_Meta_Event): + ''' + See Bluetooth spec @ 7.7.65.44 LE CS Subevent Result event + ''' + + status: int + config_id: int + start_acl_conn_event_counter: int + procedure_counter: int + frequency_compensation: int + reference_power_level: int + procedure_done_status: int + subevent_done_status: int + abort_reason: int + num_antenna_paths: int + step_mode: list[int] + step_channel: list[int] + step_data: list[bytes] + + +# ----------------------------------------------------------------------------- +@HCI_LE_Meta_Event.event( + [ + ('connection_handle', 2), + ('config_id', 1), + ('procedure_done_status', 1), + ('subevent_done_status', 1), + ('abort_reason', 1), + ('num_antenna_paths', 1), + [ + ('step_mode', 1), + ('step_channel', 1), + ('step_data', 'v'), + ], + ] +) +class HCI_LE_CS_Subevent_Result_Continue_Event(HCI_LE_Meta_Event): + ''' + See Bluetooth spec @ 7.7.65.45 LE CS Subevent Result Continue event + ''' + + status: int + config_id: int + procedure_done_status: int + subevent_done_status: int + abort_reason: int + num_antenna_paths: int + step_mode: list[int] + step_channel: list[int] + step_data: list[bytes] + + +# ----------------------------------------------------------------------------- +@HCI_LE_Meta_Event.event( + [ + ('connection_handle', 2), + ('status', STATUS_SPEC), + ] +) +class HCI_LE_CS_Test_End_Complete_Event(HCI_LE_Meta_Event): + ''' + See Bluetooth spec @ 7.7.65.46 LE CS Test End Complete event + ''' + + # ----------------------------------------------------------------------------- @HCI_Event.event([('status', STATUS_SPEC)]) class HCI_Inquiry_Complete_Event(HCI_Event): diff --git a/bumble/host.py b/bumble/host.py index 1ce4263a..8310245a 100644 --- a/bumble/host.py +++ b/bumble/host.py @@ -1296,5 +1296,23 @@ def on_hci_le_read_remote_features_complete_event(self, event): int.from_bytes(event.le_features, 'little'), ) + def on_hci_le_cs_read_remote_supported_capabilities_complete_event(self, event): + self.emit('cs_read_remote_supported_capabilities', event) + + def on_hci_le_cs_security_enable_complete_event(self, event): + self.emit('cs_security_enablement', event) + + def on_hci_le_cs_config_complete_event(self, event): + self.emit('cs_config_complete', event) + + def on_hci_le_cs_procedure_enablement_event(self, event): + self.emit('cs_procedure_enablement', event) + + def on_hci_le_cs_subevent_result_event(self, event): + self.emit('cs_subevent_result', event) + + def on_hci_le_cs_subevent_result_continue_event(self, event): + self.emit('cs_subevent_result_continue', event) + def on_hci_vendor_event(self, event): self.emit('vendor_event', event) diff --git a/examples/run_cs_initiator.py b/examples/run_cs_initiator.py new file mode 100644 index 00000000..3376c5b1 --- /dev/null +++ b/examples/run_cs_initiator.py @@ -0,0 +1,68 @@ +# Copyright 2024 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. + +# ----------------------------------------------------------------------------- +# Imports +# ----------------------------------------------------------------------------- +import asyncio +import logging +import sys +import os + +from bumble import core +from bumble import hci +from bumble.device import Device +from bumble.transport import open_transport_or_link + + +# ----------------------------------------------------------------------------- +async def main() -> None: + if len(sys.argv) < 4: + print( + 'Usage: run_cs_initiator.py ' + '' + ) + print('example: run_cs_initiator.py device1.json usb:0 F0:F1:F2:F3:F4:F5') + return + + print('<<< connecting to HCI...') + async with await open_transport_or_link(sys.argv[2]) as hci_transport: + print('<<< connected') + + device = Device.from_config_file_with_hci( + sys.argv[1], hci_transport.source, hci_transport.sink + ) + target_address = hci.Address(sys.argv[3]) + + await device.power_on() + await device.send_command( + hci.HCI_LE_CS_Read_Local_Supported_Capabilities_Command(), check_result=True + ) + await device.send_command( + hci.HCI_LE_Set_Host_Feature_Command( + bit_number=hci.LeFeature.CHANNEL_SOUNDING_HOST_SUPPORT, bit_value=1 + ) + ) + + connection = await device.connect( + target_address, transport=core.BT_LE_TRANSPORT + ) + await device.get_remote_cs_capabilities(connection) + + await hci_transport.source.terminated + + +# ----------------------------------------------------------------------------- +logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper()) +asyncio.run(main()) diff --git a/examples/run_cs_reflector.py b/examples/run_cs_reflector.py new file mode 100644 index 00000000..38222552 --- /dev/null +++ b/examples/run_cs_reflector.py @@ -0,0 +1,64 @@ +# Copyright 2024 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. + +# ----------------------------------------------------------------------------- +# Imports +# ----------------------------------------------------------------------------- +import asyncio +import logging +import sys +import os + +from bumble import hci +from bumble.device import Device, AdvertisingParameters +from bumble.transport import open_transport_or_link + + +# ----------------------------------------------------------------------------- +async def main() -> None: + if len(sys.argv) < 3: + print('Usage: run_cs_reflector.py ') + print('example: run_cs_reflector.py device1.json usb:0') + return + + print('<<< connecting to HCI...') + async with await open_transport_or_link(sys.argv[2]) as hci_transport: + print('<<< connected') + + device = Device.from_config_file_with_hci( + sys.argv[1], hci_transport.source, hci_transport.sink + ) + + await device.power_on() + await device.send_command( + hci.HCI_LE_Set_Host_Feature_Command( + bit_number=hci.LeFeature.CHANNEL_SOUNDING_HOST_SUPPORT, bit_value=1 + ) + ) + + await device.create_advertising_set( + auto_restart=True, + advertising_parameters=AdvertisingParameters( + primary_advertising_interval_min=100, + primary_advertising_interval_max=100, + own_address_type=hci.OwnAddressType.RANDOM, + ), + ) + + await hci_transport.source.terminated + + +# ----------------------------------------------------------------------------- +logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper()) +asyncio.run(main())