mirror of
https://github.com/google/bumble.git
synced 2026-07-14 14:10:50 +00:00
Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d26b99865 | |||
| c75402d29a | |||
| c8360b7a06 | |||
| 13c6090f8c | |||
| 6fd08b709a | |||
| 55d8171ad8 | |||
| 2111c49a2c | |||
| adef270195 | |||
| 8015e528f2 | |||
| 9f2fdf5309 | |||
| 7d816b0c6c | |||
| 47fcd8e9ec | |||
| fe991f67da | |||
| 82a5acc290 | |||
| 022ccf3d70 | |||
| 8a0992c00e | |||
| d7b007f3af | |||
| 7b0eb93fd8 | |||
| a8851b4759 | |||
| 7a72b225d4 | |||
| e5e51fd3f2 | |||
| a889dbae31 | |||
| 8a3c8c735e | |||
| 3a7dcd1377 | |||
| 36c7694c44 | |||
| 16dd5ae63d | |||
| 3c8fe5637d | |||
| 3266d16cf1 | |||
| 17bc5566aa | |||
| 7a14ebdabe | |||
| 17a202bc13 |
@@ -12,7 +12,7 @@
|
||||
|
||||
// Use 'postCreateCommand' to run commands after the container is created.
|
||||
"postCreateCommand":
|
||||
"python -m pip install '.[build,test,development,documentation]'",
|
||||
"python -m pip install '.[all]' --group dev",
|
||||
|
||||
// Configure tool-specific properties.
|
||||
"customizations": {
|
||||
|
||||
@@ -35,7 +35,7 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install ".[build,test,development]"
|
||||
python -m pip install ".[all]" --group dev
|
||||
- name: Check
|
||||
run: |
|
||||
invoke project.pre-commit
|
||||
|
||||
@@ -34,7 +34,7 @@ jobs:
|
||||
- name: Install
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install .[avatar]
|
||||
python -m pip install .[all,avatar]
|
||||
- name: Rootcanal
|
||||
run: nohup python -m rootcanal > rootcanal.log &
|
||||
- name: Test
|
||||
|
||||
@@ -35,7 +35,7 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install ".[build,test,development,documentation]"
|
||||
python -m pip install ".[all]" --group dev
|
||||
- name: Test
|
||||
run: |
|
||||
invoke test
|
||||
@@ -62,7 +62,7 @@ jobs:
|
||||
- name: Install Python dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install ".[build,test,development,documentation]"
|
||||
python -m pip install ".[all]" --group dev
|
||||
- name: Install Rust toolchain
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
|
||||
@@ -33,7 +33,7 @@ To install package dependencies needed to run the bumble examples, execute the f
|
||||
|
||||
```
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install ".[test,development,documentation]"
|
||||
python -m pip install ".[all]" --group dev
|
||||
```
|
||||
|
||||
### Examples
|
||||
|
||||
+1
-3
@@ -40,9 +40,7 @@ else:
|
||||
try:
|
||||
import lc3 # type: ignore # pylint: disable=E0401
|
||||
except ImportError as e:
|
||||
raise ImportError(
|
||||
"Try `python -m pip install \"git+https://github.com/google/liblc3.git\"`."
|
||||
) from e
|
||||
raise ImportError("Try `python -m pip install '.[auracast]'`.") from e
|
||||
|
||||
import bumble.device
|
||||
import bumble.logging
|
||||
|
||||
+2
-2
@@ -133,8 +133,8 @@ def print_connection(connection: Connection) -> None:
|
||||
if connection.transport == PhysicalTransport.LE:
|
||||
params.append(
|
||||
'DL=('
|
||||
f'TX:{connection.data_length[0]}/{connection.data_length[1]},'
|
||||
f'RX:{connection.data_length[2]}/{connection.data_length[3]}'
|
||||
f'TX:{connection.data_length.max_tx_octets}/{connection.data_length.max_tx_time},'
|
||||
f'RX:{connection.data_length.max_rx_octets}/{connection.data_length.max_rx_time}'
|
||||
')'
|
||||
)
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ from importlib import resources
|
||||
try:
|
||||
import lc3 # type: ignore # pylint: disable=E0401
|
||||
except ImportError as e:
|
||||
raise ImportError("Try `python -m pip install \".[lc3]\"`.") from e
|
||||
raise ImportError("Try `python -m pip install \".[auracast]\"`.") from e
|
||||
|
||||
import aiohttp.web
|
||||
import click
|
||||
|
||||
+28
-8
@@ -913,7 +913,8 @@ class PeriodicAdvertisingSync(utils.EventEmitter):
|
||||
)
|
||||
)
|
||||
|
||||
self.state = self.State.PENDING
|
||||
if self.state == self.State.INIT:
|
||||
self.state = self.State.PENDING
|
||||
|
||||
async def terminate(self) -> None:
|
||||
if self.state in (self.State.INIT, self.State.CANCELLED, self.State.TERMINATED):
|
||||
@@ -1575,6 +1576,18 @@ class CigParameters:
|
||||
rtn_c_to_p: int = DEVICE_DEFAULT_ISO_CIS_RTN # Number of C->P retransmissions
|
||||
rtn_p_to_c: int = DEVICE_DEFAULT_ISO_CIS_RTN # Number of P->C retransmissions
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
# For unidirectional CIS (e.g., Central-to-Peripheral only), the unused direction's
|
||||
# SDU size is 0. If SDU size is 0, the corresponding retransmission count and PHY
|
||||
# must also be set to 0. Otherwise, some controllers will reject the parameters
|
||||
# with error 0x30 (Parameter Out Of Mandatory Range).
|
||||
if self.max_sdu_c_to_p == 0:
|
||||
self.rtn_c_to_p = 0
|
||||
self.phy_c_to_p = hci.PhyBit(0)
|
||||
if self.max_sdu_p_to_c == 0:
|
||||
self.rtn_p_to_c = 0
|
||||
self.phy_p_to_c = hci.PhyBit(0)
|
||||
|
||||
cig_id: int
|
||||
cis_parameters: list[CisParameters]
|
||||
sdu_interval_c_to_p: int # C->P SDU interval, in microseconds
|
||||
@@ -1804,6 +1817,13 @@ class Connection(utils.CompositeEventEmitter):
|
||||
subrate_factor: int = 1
|
||||
continuation_number: int = 0
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class LeDataLength:
|
||||
max_tx_octets: int
|
||||
max_tx_time: int
|
||||
max_rx_octets: int
|
||||
max_rx_time: int
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
device: Device,
|
||||
@@ -1832,7 +1852,7 @@ class Connection(utils.CompositeEventEmitter):
|
||||
self.authenticated = False
|
||||
self.sc = False
|
||||
self.att_mtu = att.ATT_DEFAULT_MTU
|
||||
self.data_length: tuple[int, int, int, int] = DEVICE_DEFAULT_DATA_LENGTH
|
||||
self.data_length = self.LeDataLength(*DEVICE_DEFAULT_DATA_LENGTH)
|
||||
self.gatt_client = gatt_client.Client(self) # Per-connection client
|
||||
self.gatt_server = (
|
||||
device.gatt_server
|
||||
@@ -5423,7 +5443,7 @@ class Device(utils.CompositeEventEmitter):
|
||||
role: int = hci.CsRole.INITIATOR,
|
||||
rtt_type: int = hci.RttType.AA_ONLY,
|
||||
cs_sync_phy: int = hci.CsSyncPhy.LE_1M,
|
||||
channel_map: bytes = b'\x54\x55\x55\x54\x55\x55\x55\x55\x55\x15',
|
||||
channel_map: bytes = b'\x54\x55\x55\x54\x55\x55\x55\x55\x55\x05',
|
||||
channel_map_repetition: int = 0x01,
|
||||
channel_selection_type: int = hci.HCI_LE_CS_Create_Config_Command.ChannelSelectionType.ALGO_3B,
|
||||
ch3c_shape: int = hci.HCI_LE_CS_Create_Config_Command.Ch3cShape.HAT,
|
||||
@@ -6794,11 +6814,11 @@ class Device(utils.CompositeEventEmitter):
|
||||
f'*** Connection Data Length Change: [0x{connection.handle:04X}] '
|
||||
f'{connection.peer_address} as {connection.role_name}'
|
||||
)
|
||||
connection.data_length = (
|
||||
max_tx_octets,
|
||||
max_tx_time,
|
||||
max_rx_octets,
|
||||
max_rx_time,
|
||||
connection.data_length = Connection.LeDataLength(
|
||||
max_tx_octets=max_tx_octets,
|
||||
max_tx_time=max_tx_time,
|
||||
max_rx_octets=max_rx_octets,
|
||||
max_rx_time=max_rx_time,
|
||||
)
|
||||
connection.emit(connection.EVENT_CONNECTION_DATA_LENGTH_CHANGE)
|
||||
|
||||
|
||||
@@ -247,6 +247,7 @@ class Host(utils.EventEmitter):
|
||||
bis_links: dict[int, IsoLink]
|
||||
sco_links: dict[int, ScoLink]
|
||||
bigs: dict[int, set[int]]
|
||||
link_ts_flags: dict[int, int]
|
||||
acl_packet_queue: DataPacketQueue | None = None
|
||||
le_acl_packet_queue: DataPacketQueue | None = None
|
||||
iso_packet_queue: DataPacketQueue | None = None
|
||||
@@ -269,6 +270,7 @@ class Host(utils.EventEmitter):
|
||||
self.bis_links = {} # BIS links, by connection handle
|
||||
self.sco_links = {} # SCO links, by connection handle
|
||||
self.bigs = {} # BIG Handle to BIS Handles
|
||||
self.link_ts_flags = {} # TS_Flag for ISO links, by handle
|
||||
self.pending_command: hci.HCI_SyncCommand | hci.HCI_AsyncCommand | None = None
|
||||
self.pending_response: (
|
||||
asyncio.Future[
|
||||
@@ -416,6 +418,7 @@ class Host(utils.EventEmitter):
|
||||
hci.HCI_HARDWARE_ERROR_EVENT,
|
||||
hci.HCI_FLUSH_OCCURRED_EVENT,
|
||||
hci.HCI_ROLE_CHANGE_EVENT,
|
||||
hci.HCI_NUMBER_OF_COMPLETED_PACKETS_EVENT,
|
||||
hci.HCI_MODE_CHANGE_EVENT,
|
||||
hci.HCI_RETURN_LINK_KEYS_EVENT,
|
||||
hci.HCI_PIN_CODE_REQUEST_EVENT,
|
||||
@@ -486,6 +489,7 @@ class Host(utils.EventEmitter):
|
||||
hci.HCI_LE_PHY_UPDATE_COMPLETE_EVENT,
|
||||
hci.HCI_LE_EXTENDED_ADVERTISING_REPORT_EVENT,
|
||||
hci.HCI_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHED_EVENT,
|
||||
hci.HCI_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHED_V2_EVENT,
|
||||
hci.HCI_LE_PERIODIC_ADVERTISING_REPORT_EVENT,
|
||||
hci.HCI_LE_PERIODIC_ADVERTISING_SYNC_LOST_EVENT,
|
||||
hci.HCI_LE_SCAN_TIMEOUT_EVENT,
|
||||
@@ -1028,6 +1032,82 @@ class Host(utils.EventEmitter):
|
||||
# Look for the connection to which this data belongs
|
||||
if connection := self.connections.get(packet.connection_handle):
|
||||
connection.on_hci_acl_data_packet(packet)
|
||||
return
|
||||
|
||||
# WORKAROUND: Some controllers (e.g. Intel BE200) send ISO data wrapped in ACL packets
|
||||
# using the CIS handle.
|
||||
is_cis = packet.connection_handle in self.cis_links
|
||||
is_bis = packet.connection_handle in self.bis_links
|
||||
|
||||
if is_cis or is_bis:
|
||||
logger.debug(
|
||||
f"Received ISO data wrapped in ACL packet for handle 0x{packet.connection_handle:04X}"
|
||||
)
|
||||
payload = packet.data
|
||||
|
||||
ts_flag = self.link_ts_flags.get(packet.connection_handle)
|
||||
if ts_flag is None:
|
||||
# Learn TS flag from the first packet on this link
|
||||
if is_bis:
|
||||
# BIS packets always have Timestamp according to spec
|
||||
ts_flag = 1
|
||||
elif len(payload) < 8:
|
||||
# Too short to have 8-byte header (TS), must be No TS
|
||||
ts_flag = 0
|
||||
else:
|
||||
psn_no_ts = int.from_bytes(payload[0:2], 'little')
|
||||
psn_has_ts = int.from_bytes(payload[4:6], 'little')
|
||||
if psn_has_ts == 0:
|
||||
ts_flag = 1
|
||||
elif psn_no_ts == 0:
|
||||
ts_flag = 0
|
||||
else:
|
||||
# Fallback heuristic
|
||||
ts_flag = 1 if psn_has_ts < psn_no_ts else 0
|
||||
self.link_ts_flags[packet.connection_handle] = ts_flag
|
||||
logger.info(
|
||||
f"Learned TS_Flag = {ts_flag} for handle 0x{packet.connection_handle:04X}"
|
||||
)
|
||||
|
||||
if ts_flag:
|
||||
header_size = 8
|
||||
sdu_length_offset = 6
|
||||
else:
|
||||
header_size = 4
|
||||
sdu_length_offset = 2
|
||||
|
||||
pb_flag = 0b10
|
||||
if len(payload) >= header_size:
|
||||
sdu_length = int.from_bytes(
|
||||
payload[sdu_length_offset : sdu_length_offset + 2], 'little'
|
||||
)
|
||||
if sdu_length == len(payload) - header_size:
|
||||
pb_flag = 0b10 # Complete SDU
|
||||
else:
|
||||
pb_flag = 0b00 # First fragment
|
||||
else:
|
||||
pb_flag = 0b01 # Continuation
|
||||
ts_flag = 0
|
||||
|
||||
# Reconstruct the raw ISO packet (excluding packet indicator 0x05)
|
||||
pdu_info = packet.connection_handle | (pb_flag << 12) | (ts_flag << 14)
|
||||
header = bytes(
|
||||
[
|
||||
pdu_info & 0xFF,
|
||||
(pdu_info >> 8) & 0xFF,
|
||||
len(payload) & 0xFF,
|
||||
(len(payload) >> 8) & 0xFF,
|
||||
]
|
||||
)
|
||||
raw_iso_packet = header + payload
|
||||
|
||||
try:
|
||||
iso_packet = hci.HCI_IsoDataPacket.from_bytes(
|
||||
bytes([hci.HCI_ISO_DATA_PACKET]) + raw_iso_packet
|
||||
)
|
||||
self.on_hci_iso_data_packet(iso_packet)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to reconstruct ISO packet from ACL: {e}")
|
||||
|
||||
def on_hci_sco_data_packet(self, packet: hci.HCI_SynchronousDataPacket) -> None:
|
||||
# Experimental
|
||||
@@ -1251,6 +1331,7 @@ class Host(utils.EventEmitter):
|
||||
self.emit('disconnection', handle, event.reason)
|
||||
|
||||
# Remove the handle reference
|
||||
self.link_ts_flags.pop(handle, None)
|
||||
_ = (
|
||||
self.connections.pop(handle, 0)
|
||||
or self.cis_links.pop(handle, 0)
|
||||
@@ -1371,6 +1452,20 @@ class Host(utils.EventEmitter):
|
||||
event.advertiser_clock_accuracy,
|
||||
)
|
||||
|
||||
def on_hci_le_periodic_advertising_sync_established_v2_event(
|
||||
self, event: hci.HCI_LE_Periodic_Advertising_Sync_Established_V2_Event
|
||||
):
|
||||
self.emit(
|
||||
'periodic_advertising_sync_establishment',
|
||||
event.status,
|
||||
event.sync_handle,
|
||||
event.advertising_sid,
|
||||
event.advertiser_address,
|
||||
event.advertiser_phy,
|
||||
event.periodic_advertising_interval,
|
||||
event.advertiser_clock_accuracy,
|
||||
)
|
||||
|
||||
def on_hci_le_periodic_advertising_sync_lost_event(
|
||||
self, event: hci.HCI_LE_Periodic_Advertising_Sync_Lost_Event
|
||||
):
|
||||
|
||||
+32
-18
@@ -1683,6 +1683,10 @@ class LeCreditBasedChannel(utils.EventEmitter):
|
||||
if identifier in self.manager.le_coc_requests:
|
||||
raise InvalidStateError('too many concurrent connection requests')
|
||||
|
||||
# Create a future to wait for the response
|
||||
connection_result = asyncio.get_running_loop().create_future()
|
||||
self.connection_result = connection_result
|
||||
|
||||
self._change_state(self.State.CONNECTING)
|
||||
request = L2CAP_LE_Credit_Based_Connection_Request(
|
||||
identifier=identifier,
|
||||
@@ -1695,17 +1699,19 @@ class LeCreditBasedChannel(utils.EventEmitter):
|
||||
self.manager.le_coc_requests[identifier] = request
|
||||
self.send_control_frame(request)
|
||||
|
||||
# Create a future to wait for the response
|
||||
self.connection_result = asyncio.get_running_loop().create_future()
|
||||
|
||||
# Wait for the connection to succeed or fail
|
||||
return await self.connection_result
|
||||
return await connection_result
|
||||
|
||||
async def disconnect(self) -> None:
|
||||
# Check that we're connected
|
||||
if self.state != self.State.CONNECTED:
|
||||
raise InvalidStateError('not connected')
|
||||
|
||||
# Create a future to wait for the state machine to get to a success or error
|
||||
# state
|
||||
disconnection_result = asyncio.get_running_loop().create_future()
|
||||
self.disconnection_result = disconnection_result
|
||||
|
||||
self._change_state(self.State.DISCONNECTING)
|
||||
self.flush_output()
|
||||
self.send_control_frame(
|
||||
@@ -1716,17 +1722,18 @@ class LeCreditBasedChannel(utils.EventEmitter):
|
||||
)
|
||||
)
|
||||
|
||||
# Create a future to wait for the state machine to get to a success or error
|
||||
# state
|
||||
self.disconnection_result = asyncio.get_running_loop().create_future()
|
||||
return await self.disconnection_result
|
||||
return await disconnection_result
|
||||
|
||||
def abort(self) -> None:
|
||||
if self.state == self.State.CONNECTED:
|
||||
if self.state in (self.State.CONNECTED, self.State.DISCONNECTING):
|
||||
self._change_state(self.State.DISCONNECTED)
|
||||
if self.state == self.State.CONNECTING:
|
||||
if self.connection_result is not None:
|
||||
self.connection_result.cancel()
|
||||
self.manager.on_channel_closed(self)
|
||||
if self.connection_result is not None:
|
||||
self.connection_result.cancel()
|
||||
self.connection_result = None
|
||||
if self.disconnection_result is not None:
|
||||
self.disconnection_result.set_result(None)
|
||||
self.disconnection_result = None
|
||||
|
||||
def on_pdu(self, pdu: bytes) -> None:
|
||||
if self.sink is None:
|
||||
@@ -1861,6 +1868,10 @@ class LeCreditBasedChannel(utils.EventEmitter):
|
||||
)
|
||||
)
|
||||
self._change_state(self.State.DISCONNECTED)
|
||||
self.manager.on_channel_closed(self)
|
||||
if self.disconnection_result is not None:
|
||||
self.disconnection_result.set_result(None)
|
||||
self.disconnection_result = None
|
||||
self.flush_output()
|
||||
|
||||
def on_disconnection_response(self, response: L2CAP_Disconnection_Response) -> None:
|
||||
@@ -1876,6 +1887,7 @@ class LeCreditBasedChannel(utils.EventEmitter):
|
||||
return
|
||||
|
||||
self._change_state(self.State.DISCONNECTED)
|
||||
self.manager.on_channel_closed(self)
|
||||
if self.disconnection_result:
|
||||
self.disconnection_result.set_result(None)
|
||||
self.disconnection_result = None
|
||||
@@ -2461,7 +2473,7 @@ class ChannelManager:
|
||||
if (
|
||||
channel := self.find_channel(connection.handle, response.source_cid)
|
||||
) is None:
|
||||
logger.warning(
|
||||
logger.debug(
|
||||
color(
|
||||
f'channel {response.source_cid} not found for '
|
||||
f'0x{connection.handle:04X}:{cid}',
|
||||
@@ -2879,11 +2891,13 @@ class ChannelManager:
|
||||
|
||||
channel.on_credits(credit.credits)
|
||||
|
||||
def on_channel_closed(self, channel: ClassicChannel) -> None:
|
||||
connection_channels = self.channels.get(channel.connection.handle)
|
||||
if connection_channels:
|
||||
if channel.source_cid in connection_channels:
|
||||
del connection_channels[channel.source_cid]
|
||||
def on_channel_closed(self, channel: ClassicChannel | LeCreditBasedChannel) -> None:
|
||||
if classic_connection_channels := self.channels.get(channel.connection.handle):
|
||||
classic_connection_channels.pop(channel.source_cid, None)
|
||||
elif le_connection_channels := self.le_coc_channels.get(
|
||||
channel.connection.handle
|
||||
):
|
||||
le_connection_channels.pop(channel.destination_cid, None)
|
||||
|
||||
async def create_le_credit_based_channel(
|
||||
self,
|
||||
|
||||
@@ -28,13 +28,18 @@ from bumble.transport.common import (
|
||||
)
|
||||
|
||||
# pylint: disable=no-name-in-module
|
||||
from bumble.transport.grpc_protobuf.emulated_bluetooth_packets_pb2 import HCIPacket
|
||||
from bumble.transport.grpc_protobuf.emulated_bluetooth_pb2_grpc import (
|
||||
EmulatedBluetoothServiceStub,
|
||||
)
|
||||
from bumble.transport.grpc_protobuf.emulated_bluetooth_vhci_pb2_grpc import (
|
||||
VhciForwardingServiceStub,
|
||||
)
|
||||
try:
|
||||
from bumble.transport.grpc_protobuf.emulated_bluetooth_packets_pb2 import HCIPacket
|
||||
from bumble.transport.grpc_protobuf.emulated_bluetooth_pb2_grpc import (
|
||||
EmulatedBluetoothServiceStub,
|
||||
)
|
||||
from bumble.transport.grpc_protobuf.emulated_bluetooth_vhci_pb2_grpc import (
|
||||
VhciForwardingServiceStub,
|
||||
)
|
||||
except ImportError as e:
|
||||
raise ImportError(
|
||||
'The bumble[android] extra is required to use the Android emulator transport'
|
||||
) from e
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Logging
|
||||
|
||||
@@ -37,18 +37,27 @@ from bumble.transport.common import (
|
||||
)
|
||||
|
||||
# pylint: disable=no-name-in-module
|
||||
from bumble.transport.grpc_protobuf.netsim.common_pb2 import ChipKind
|
||||
from bumble.transport.grpc_protobuf.netsim.hci_packet_pb2 import HCIPacket
|
||||
from bumble.transport.grpc_protobuf.netsim.packet_streamer_pb2 import (
|
||||
PacketRequest,
|
||||
PacketResponse,
|
||||
)
|
||||
from bumble.transport.grpc_protobuf.netsim.packet_streamer_pb2_grpc import (
|
||||
PacketStreamerServicer,
|
||||
PacketStreamerStub,
|
||||
add_PacketStreamerServicer_to_server,
|
||||
)
|
||||
from bumble.transport.grpc_protobuf.netsim.startup_pb2 import Chip, ChipInfo, DeviceInfo
|
||||
try:
|
||||
from bumble.transport.grpc_protobuf.netsim.common_pb2 import ChipKind
|
||||
from bumble.transport.grpc_protobuf.netsim.hci_packet_pb2 import HCIPacket
|
||||
from bumble.transport.grpc_protobuf.netsim.packet_streamer_pb2 import (
|
||||
PacketRequest,
|
||||
PacketResponse,
|
||||
)
|
||||
from bumble.transport.grpc_protobuf.netsim.packet_streamer_pb2_grpc import (
|
||||
PacketStreamerServicer,
|
||||
PacketStreamerStub,
|
||||
add_PacketStreamerServicer_to_server,
|
||||
)
|
||||
from bumble.transport.grpc_protobuf.netsim.startup_pb2 import (
|
||||
Chip,
|
||||
ChipInfo,
|
||||
DeviceInfo,
|
||||
)
|
||||
except ImportError as e:
|
||||
raise ImportError(
|
||||
'The bumble[android] extra is required to use the Android netsim transport'
|
||||
) from e
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Logging
|
||||
|
||||
@@ -104,6 +104,9 @@ async def open_pyusb_transport(spec: str) -> Transport:
|
||||
0,
|
||||
packet[1:],
|
||||
)
|
||||
elif packet_type == hci.HCI_ISO_DATA_PACKET:
|
||||
# Workaround: Send ISO packets over Bulk Out
|
||||
self.device.write(USB_ENDPOINT_ACL_OUT, packet[1:])
|
||||
else:
|
||||
logger.warning(
|
||||
color(f'unsupported packet type {packet_type}', 'red')
|
||||
|
||||
+45
-9
@@ -37,6 +37,20 @@ from bumble.transport.common import BaseSource, Transport, TransportInitError
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def _safe_call_soon(loop: asyncio.AbstractEventLoop, callback, *args) -> None:
|
||||
"""Schedule `callback` on `loop` from the libusb event thread, tolerating the
|
||||
case where the loop has already been closed during process/transport
|
||||
teardown. Without this guard, a libusb transfer callback that fires after the
|
||||
asyncio loop is closed raises 'Event loop is closed' on the C callback
|
||||
thread, which can escalate to a libusb mutex assertion and crash the process
|
||||
(SIGABRT). This makes an unclean shutdown a no-op instead of a core dump."""
|
||||
try:
|
||||
loop.call_soon_threadsafe(callback, *args)
|
||||
except RuntimeError:
|
||||
pass # loop already closed; nothing left to schedule
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Constants
|
||||
# -----------------------------------------------------------------------------
|
||||
@@ -266,7 +280,7 @@ class UsbPacketSink:
|
||||
self.packets.put_nowait(packet)
|
||||
|
||||
def transfer_callback(self, transfer):
|
||||
self.loop.call_soon_threadsafe(self.out_transfer_ready.release)
|
||||
_safe_call_soon(self.loop, self.out_transfer_ready.release)
|
||||
status = transfer.getStatus()
|
||||
|
||||
logger.debug(f"OUT CALLBACK: {status}")
|
||||
@@ -338,6 +352,25 @@ class UsbPacketSink:
|
||||
)
|
||||
self.isochronous_out_transfer.submit()
|
||||
submitted = True
|
||||
elif packet_type == hci.HCI_ISO_DATA_PACKET:
|
||||
if self.isochronous_out_transfer is None:
|
||||
# Workaround: Send ISO packets over Bulk Out when Isochronous endpoints are not enabled
|
||||
self.bulk_or_control_out_transfer.setBulk(
|
||||
self.bulk_out.getAddress(),
|
||||
packet_payload,
|
||||
callback=self.transfer_callback,
|
||||
)
|
||||
self.bulk_or_control_out_transfer.submit()
|
||||
submitted = True
|
||||
else:
|
||||
logger.warning(
|
||||
color(
|
||||
'ISO packets over Isochronous endpoints not supported yet',
|
||||
'red',
|
||||
)
|
||||
)
|
||||
self.out_transfer_ready.release()
|
||||
continue
|
||||
else:
|
||||
logger.warning(
|
||||
color(f'unsupported packet type {packet_type}', 'red')
|
||||
@@ -516,8 +549,8 @@ class UsbPacketSource(asyncio.Protocol, BaseSource):
|
||||
self.dequeue_task = self.loop.create_task(self.dequeue())
|
||||
|
||||
def queue_packet(self, packet_type: int, packet_data: bytes) -> None:
|
||||
self.loop.call_soon_threadsafe(
|
||||
self.queue.put_nowait, bytes([packet_type]) + packet_data
|
||||
_safe_call_soon(
|
||||
self.loop, self.queue.put_nowait, bytes([packet_type]) + packet_data
|
||||
)
|
||||
|
||||
def transfer_callback(self, transfer):
|
||||
@@ -563,16 +596,16 @@ class UsbPacketSource(asyncio.Protocol, BaseSource):
|
||||
transfer.submit()
|
||||
except usb1.USBError as error:
|
||||
logger.warning(f"Failed to re-submit transfer: {error}")
|
||||
self.loop.call_soon_threadsafe(self.on_transport_lost)
|
||||
_safe_call_soon(self.loop, self.on_transport_lost)
|
||||
elif status == usb1.TRANSFER_CANCELLED:
|
||||
logger.debug(f"IN[{packet_type}] transfer canceled")
|
||||
self.loop.call_soon_threadsafe(self.done[transfer].set)
|
||||
_safe_call_soon(self.loop, self.done[transfer].set)
|
||||
else:
|
||||
logger.warning(
|
||||
color(f'!!! IN[{packet_type}] transfer not completed', 'red')
|
||||
)
|
||||
self.loop.call_soon_threadsafe(self.done[transfer].set)
|
||||
self.loop.call_soon_threadsafe(self.on_transport_lost)
|
||||
_safe_call_soon(self.loop, self.done[transfer].set)
|
||||
_safe_call_soon(self.loop, self.on_transport_lost)
|
||||
|
||||
async def dequeue(self):
|
||||
while not self.closed:
|
||||
@@ -685,7 +718,7 @@ class UsbTransport(Transport):
|
||||
logger.warning(f'!!! Exception while handling events: {error}')
|
||||
|
||||
logger.debug('ending USB event loop')
|
||||
self.loop.call_soon_threadsafe(self.event_loop_done.set_result, None)
|
||||
_safe_call_soon(self.loop, self.event_loop_done.set_result, None)
|
||||
|
||||
async def close(self):
|
||||
self.source.close()
|
||||
@@ -696,7 +729,10 @@ class UsbTransport(Transport):
|
||||
# We no longer need the event loop to run
|
||||
with self.lock:
|
||||
self.event_loop_should_exit = True
|
||||
self.context.interruptEventHandler()
|
||||
try:
|
||||
self.context.interruptEventHandler()
|
||||
except (AttributeError, usb1.USBError) as error:
|
||||
logger.warning(f"Failed to interrupt event handler: {error}")
|
||||
|
||||
self.device.releaseInterface(self.acl_interface.getNumber())
|
||||
if self.sco_interface:
|
||||
|
||||
@@ -29,10 +29,10 @@ Try installing the optional `[auracast]` dependencies:
|
||||
|
||||
## LC3
|
||||
The `auracast` app depends on the `lc3` python module, which is available
|
||||
either as PyPI module (currently only available for Linux x86_64).
|
||||
as a PyPI module `lc3py` (currently available for Linux x86_64 and macOS arm64).
|
||||
When installing Bumble with the optional `auracast` dependency, the `lc3`
|
||||
module will be installed from the `lc3py` PyPI package if available.
|
||||
If not, you will need to install it separately. This can be done with:
|
||||
module will be installed automatically if your platform is supported.
|
||||
If not, you will need to install it separately. This can be done by building it from source:
|
||||
```bash
|
||||
$ python3 -m pip install "git+https://github.com/google/liblc3.git"
|
||||
```
|
||||
|
||||
@@ -71,6 +71,12 @@ $ python3 -m pip install git+https://github.com/google/bumble.git@27c0551
|
||||
When you work on the Bumble code itself, and run some of the tests or example apps, or import the
|
||||
module in your own code, you typically either install the package from source in "development mode" as described above, or you may choose to skip the install phase.
|
||||
|
||||
!!! tip "Installing Development Dependencies"
|
||||
To install all optional dependencies and development tools (like `pytest`, `black`, `invoke`, etc.), run:
|
||||
```bash
|
||||
python3 -m pip install -e ".[all]" --group dev
|
||||
```
|
||||
|
||||
If you plan on contributing to the project, please read the [contributing](development/contributing.md) section.
|
||||
|
||||
## Without Installing
|
||||
|
||||
@@ -24,6 +24,20 @@ stack via a virtual HCI interface.
|
||||
Both ways are controlled via gRPC requests to the Android emulator controller and/or
|
||||
from the Android emulator.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Using the Android Emulator or Netsim transports requires the `android` optional dependencies (which include `grpcio` and `protobuf`).
|
||||
|
||||
If you installed Bumble from PyPI:
|
||||
```bash
|
||||
python3 -m pip install "bumble[android]"
|
||||
```
|
||||
|
||||
If you are running from source:
|
||||
```bash
|
||||
python3 -m pip install ".[android]"
|
||||
```
|
||||
|
||||
## Launching the Emulator
|
||||
|
||||
If the version of the emulator you are running does not yet support enabling
|
||||
|
||||
@@ -8,6 +8,9 @@ ANDROID EMULATOR TRANSPORT
|
||||
Use the `android-netsim` transport name instead.
|
||||
|
||||
|
||||
!!! note
|
||||
This transport requires the `android` extra. See the [Android Platform](../platforms/android.md) page for installation instructions.
|
||||
|
||||
The Android "netsim" transport either connects, as a host, to a **Netsim** virtual controller
|
||||
("host" mode), or acts as a virtual controller itself ("controller" mode) accepting host
|
||||
connections.
|
||||
|
||||
+39
-25
@@ -24,31 +24,58 @@ dependencies = [
|
||||
# updated. Relax the version requirement since it's better than being completely unable
|
||||
# to import the package in case of version mismatch.
|
||||
"cryptography >= 42.0.8; platform_system=='Android'",
|
||||
"grpcio >= 1.62.1; platform_system!='Emscripten'",
|
||||
"humanize >= 4.6.0; platform_system!='Emscripten'",
|
||||
"libusb1 >= 2.0.1; platform_system!='Emscripten'",
|
||||
"libusb-package == 1.0.26.1; platform_system!='Emscripten' and platform_system!='Android'",
|
||||
"platformdirs >= 3.10.0; platform_system!='Emscripten'",
|
||||
"prompt_toolkit >= 3.0.16; platform_system!='Emscripten'",
|
||||
"prettytable >= 3.6.0; platform_system!='Emscripten'",
|
||||
"protobuf >= 3.12.4; platform_system!='Emscripten'",
|
||||
"pyee >= 13.0.0",
|
||||
"pyserial-asyncio >= 0.5; platform_system!='Emscripten'",
|
||||
"pyserial >= 3.5; platform_system!='Emscripten'",
|
||||
"pyusb >= 1.2; platform_system!='Emscripten'",
|
||||
"tomli ~= 2.2.1; platform_system!='Emscripten' and python_version<'3.11'",
|
||||
"websockets >= 15.0.1; platform_system!='Emscripten'",
|
||||
# Serial
|
||||
"pyserial-asyncio >= 0.5; platform_system!='Emscripten'",
|
||||
"pyserial >= 3.5; platform_system!='Emscripten'",
|
||||
# USB
|
||||
"libusb1 >= 2.0.1; platform_system!='Emscripten'",
|
||||
"libusb-package == 1.0.26.4; platform_system!='Emscripten' and platform_system!='Android'",
|
||||
"pyusb >= 1.2; platform_system!='Emscripten'",
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
android = [
|
||||
"grpcio >= 1.62.1; platform_system!='Emscripten'",
|
||||
"protobuf >= 3.12.4; platform_system!='Emscripten'",
|
||||
]
|
||||
avatar = [
|
||||
"pandora-avatar == 0.0.10",
|
||||
"rootcanal == 1.11.1 ; python_version>='3.10'",
|
||||
]
|
||||
pandora = ["bt-test-interfaces >= 0.0.6"]
|
||||
auracast = [
|
||||
"lc3py >= 1.1.3; python_version>='3.10' and ((platform_system=='Linux' and platform_machine=='x86_64') or (platform_system=='Darwin' and platform_machine=='arm64'))",
|
||||
"sounddevice >= 0.5.1",
|
||||
]
|
||||
all = [
|
||||
"bumble[android]",
|
||||
"bumble[auracast]",
|
||||
]
|
||||
|
||||
[dependency-groups]
|
||||
build = ["build >= 0.7"]
|
||||
test = [
|
||||
"pytest >= 8.2",
|
||||
"pytest-asyncio >= 0.23.5",
|
||||
"pytest-html >= 3.2.0",
|
||||
"pytest >= 9.0",
|
||||
"pytest-asyncio >= 1.4",
|
||||
"pytest-html >= 4.2",
|
||||
"coverage >= 6.4",
|
||||
]
|
||||
development = [
|
||||
docs = [
|
||||
"mkdocs >= 1.6.0",
|
||||
"mkdocs-material >= 9.6",
|
||||
"mkdocstrings[python] >= 0.27.0",
|
||||
]
|
||||
dev = [
|
||||
{include-group = "build"},
|
||||
{include-group = "test"},
|
||||
{include-group = "docs"},
|
||||
"black ~= 25.1",
|
||||
"bt-test-interfaces >= 0.0.6",
|
||||
"grpcio-tools >= 1.62.1",
|
||||
@@ -63,20 +90,6 @@ development = [
|
||||
"types-invoke >= 1.7.3",
|
||||
"types-protobuf >= 4.21.0",
|
||||
]
|
||||
avatar = [
|
||||
"pandora-avatar == 0.0.10",
|
||||
"rootcanal == 1.11.1 ; python_version>='3.10'",
|
||||
]
|
||||
pandora = ["bt-test-interfaces >= 0.0.6"]
|
||||
documentation = [
|
||||
"mkdocs >= 1.6.0",
|
||||
"mkdocs-material >= 9.6",
|
||||
"mkdocstrings[python] >= 0.27.0",
|
||||
]
|
||||
auracast = [
|
||||
"lc3py >= 1.1.3; python_version>='3.10' and ((platform_system=='Linux' and platform_machine=='x86_64') or (platform_system=='Darwin' and platform_machine=='arm64'))",
|
||||
"sounddevice >= 0.5.1",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
bumble-auracast = "bumble.apps.auracast:main"
|
||||
@@ -132,6 +145,7 @@ write_to = "bumble/_version.py"
|
||||
[tool.pytest.ini_options]
|
||||
pythonpath = "."
|
||||
testpaths = ["tests"]
|
||||
asyncio_mode = "auto"
|
||||
|
||||
[tool.pylint.master]
|
||||
init-hook = 'import sys; sys.path.append(".")'
|
||||
|
||||
@@ -47,7 +47,7 @@ def build(ctx, install=False):
|
||||
if install:
|
||||
ctx.run('python -m pip install .[build]')
|
||||
|
||||
ctx.run("python -m build")
|
||||
ctx.run("python -I -m build")
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
+3
-3
@@ -46,7 +46,7 @@ class TwoDevices(test_utils.TwoDevices):
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"command,",
|
||||
"command",
|
||||
[
|
||||
avrcp.GetPlayStatusCommand(),
|
||||
avrcp.GetCapabilitiesCommand(
|
||||
@@ -132,7 +132,7 @@ def test_command(command: avrcp.Command):
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"event,",
|
||||
"event",
|
||||
[
|
||||
avrcp.UidsChangedEvent(uid_counter=7),
|
||||
avrcp.TrackChangedEvent(uid=12356),
|
||||
@@ -159,7 +159,7 @@ def test_event(event: avrcp.Event):
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"response,",
|
||||
"response",
|
||||
[
|
||||
avrcp.GetPlayStatusResponse(
|
||||
song_length=1010, song_position=13, play_status=avrcp.PlayStatus.PAUSED
|
||||
|
||||
+1
-1
@@ -72,7 +72,7 @@ def test_sef():
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
'sirk_type,', [(csip.SirkType.ENCRYPTED), (csip.SirkType.PLAINTEXT)]
|
||||
'sirk_type', [(csip.SirkType.ENCRYPTED), (csip.SirkType.PLAINTEXT)]
|
||||
)
|
||||
async def test_csis(sirk_type):
|
||||
SIRK = bytes.fromhex('2f62c8ae41867d1bb619e788a2605faa')
|
||||
|
||||
+42
-3
@@ -17,6 +17,7 @@
|
||||
# -----------------------------------------------------------------------------
|
||||
import asyncio
|
||||
import functools
|
||||
import inspect
|
||||
import logging
|
||||
import os
|
||||
from unittest import mock
|
||||
@@ -278,7 +279,7 @@ async def test_legacy_advertising():
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.parametrize(
|
||||
'auto_restart,',
|
||||
'auto_restart',
|
||||
(True, False),
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
@@ -357,7 +358,7 @@ async def test_advertising_and_scanning():
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.parametrize(
|
||||
'own_address_type,',
|
||||
'own_address_type',
|
||||
(OwnAddressType.PUBLIC, OwnAddressType.RANDOM),
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
@@ -395,7 +396,7 @@ async def test_extended_advertising_connection(own_address_type):
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.parametrize(
|
||||
'own_address_type,',
|
||||
'own_address_type',
|
||||
(OwnAddressType.PUBLIC, OwnAddressType.RANDOM),
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
@@ -595,6 +596,25 @@ async def test_cis_setup_failure():
|
||||
await asyncio.wait_for(cis_create_task, _TIMEOUT)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def test_cis_parameters_unidirectional():
|
||||
# Test C2P unidirectional (P to C not used)
|
||||
cis_c2p = CigParameters.CisParameters(cis_id=1, max_sdu_p_to_c=0)
|
||||
assert cis_c2p.max_sdu_c_to_p != 0
|
||||
assert cis_c2p.rtn_c_to_p != 0
|
||||
assert cis_c2p.phy_c_to_p != hci.PhyBit(0)
|
||||
assert cis_c2p.rtn_p_to_c == 0
|
||||
assert cis_c2p.phy_p_to_c == hci.PhyBit(0)
|
||||
|
||||
# Test P2C unidirectional (C to P not used)
|
||||
cis_p2c = CigParameters.CisParameters(cis_id=2, max_sdu_c_to_p=0)
|
||||
assert cis_p2c.max_sdu_p_to_c != 0
|
||||
assert cis_p2c.rtn_p_to_c != 0
|
||||
assert cis_p2c.phy_p_to_c != hci.PhyBit(0)
|
||||
assert cis_p2c.rtn_c_to_p == 0
|
||||
assert cis_p2c.phy_c_to_p == hci.PhyBit(0)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.asyncio
|
||||
async def test_enter_and_exit_sniff_mode():
|
||||
@@ -674,6 +694,25 @@ async def test_power_on_default_static_address_should_not_be_any():
|
||||
assert devices[0].static_address != Address.ANY_RANDOM
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def test_cs_channel_map_excludes_forbidden_channels():
|
||||
forbidden = {0, 1, 23, 24, 25, 76, 77, 78, 79}
|
||||
default_map = (
|
||||
inspect.signature(Device.create_cs_config).parameters['channel_map'].default
|
||||
)
|
||||
|
||||
enabled = {
|
||||
byte_idx * 8 + bit
|
||||
for byte_idx, byte in enumerate(default_map)
|
||||
for bit in range(8)
|
||||
if byte & (1 << bit)
|
||||
}
|
||||
|
||||
assert enabled.isdisjoint(
|
||||
forbidden
|
||||
), f"Default channel_map enables forbidden CS channels: {enabled & forbidden}"
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def test_gatt_services_with_gas_and_gatt():
|
||||
device = Device(host=Host(None, None))
|
||||
|
||||
+3
-3
@@ -297,7 +297,7 @@ def test_custom_le_meta_event():
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.parametrize(
|
||||
"clazz,",
|
||||
"clazz",
|
||||
[
|
||||
clazz[1]
|
||||
for clazz in inspect.getmembers(hci)
|
||||
@@ -313,7 +313,7 @@ def test_hci_command_subclasses_op_code(clazz: type[hci.HCI_Command]):
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.parametrize(
|
||||
"clazz,",
|
||||
"clazz",
|
||||
[
|
||||
clazz[1]
|
||||
for clazz in inspect.getmembers(hci)
|
||||
@@ -330,7 +330,7 @@ def test_hci_event_subclasses_event_code(clazz: type[hci.HCI_Event]):
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.parametrize(
|
||||
"clazz,",
|
||||
"clazz",
|
||||
[
|
||||
clazz[1]
|
||||
for clazz in inspect.getmembers(hci)
|
||||
|
||||
+2
-2
@@ -333,7 +333,7 @@ async def test_query_calls_with_calls(
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"operation,",
|
||||
"operation",
|
||||
(
|
||||
hfp.CallHoldOperation.RELEASE_ALL_HELD_CALLS,
|
||||
hfp.CallHoldOperation.RELEASE_ALL_ACTIVE_CALLS,
|
||||
@@ -358,7 +358,7 @@ async def test_hold_call_without_call_index(
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"operation,",
|
||||
"operation",
|
||||
(
|
||||
hfp.CallHoldOperation.RELEASE_SPECIFIC_CALL,
|
||||
hfp.CallHoldOperation.HOLD_ALL_CALLS_EXCEPT,
|
||||
|
||||
@@ -74,6 +74,20 @@ async def test_reset(supported_commands: set[int], max_lmp_features_page_number:
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reset_enables_number_of_completed_packets_event() -> None:
|
||||
controller = Controller('C')
|
||||
controller.total_num_le_acl_data_packets = 3
|
||||
host = Host(controller, AsyncPipeSink(controller))
|
||||
|
||||
await host.reset()
|
||||
|
||||
completed_packets_event_bit = 1 << (hci.HCI_NUMBER_OF_COMPLETED_PACKETS_EVENT - 1)
|
||||
assert controller.event_mask & completed_packets_event_bit
|
||||
assert host.le_acl_packet_queue is not None
|
||||
assert host.le_acl_packet_queue.max_in_flight == 3
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def test_data_packet_queue():
|
||||
controller = unittest.mock.Mock()
|
||||
|
||||
+80
-5
@@ -49,19 +49,19 @@ def test_helpers():
|
||||
psm = l2cap.L2CAP_Connection_Request.serialize_psm(0x242311)
|
||||
assert psm == bytes([0x11, 0x23, 0x24])
|
||||
|
||||
(offset, psm) = l2cap.L2CAP_Connection_Request.parse_psm(
|
||||
offset, psm = l2cap.L2CAP_Connection_Request.parse_psm(
|
||||
bytes([0x00, 0x01, 0x00, 0x44]), 1
|
||||
)
|
||||
assert offset == 3
|
||||
assert psm == 0x01
|
||||
|
||||
(offset, psm) = l2cap.L2CAP_Connection_Request.parse_psm(
|
||||
offset, psm = l2cap.L2CAP_Connection_Request.parse_psm(
|
||||
bytes([0x00, 0x23, 0x10, 0x44]), 1
|
||||
)
|
||||
assert offset == 3
|
||||
assert psm == 0x1023
|
||||
|
||||
(offset, psm) = l2cap.L2CAP_Connection_Request.parse_psm(
|
||||
offset, psm = l2cap.L2CAP_Connection_Request.parse_psm(
|
||||
bytes([0x00, 0x11, 0x23, 0x24, 0x44]), 1
|
||||
)
|
||||
assert offset == 4
|
||||
@@ -197,7 +197,7 @@ async def test_basic_connection():
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.parametrize("info_type,", list(l2cap.L2CAP_Information_Request.InfoType))
|
||||
@pytest.mark.parametrize("info_type", list(l2cap.L2CAP_Information_Request.InfoType))
|
||||
async def test_l2cap_information_request(monkeypatch, info_type):
|
||||
# TODO: Replace handlers with API when implemented
|
||||
devices = await TwoDevices.create_with_connection()
|
||||
@@ -321,7 +321,7 @@ async def test_mtu():
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("mtu,", (50, 255, 256, 1000))
|
||||
@pytest.mark.parametrize("mtu", (50, 255, 256, 1000))
|
||||
async def test_enhanced_retransmission_mode(mtu: int):
|
||||
devices = TwoDevices()
|
||||
await devices.setup_connection()
|
||||
@@ -457,6 +457,81 @@ def test_fcs(cid: int, payload: str, expected: str):
|
||||
assert pdu.to_bytes(with_fcs=True) == bytes.fromhex(expected)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.asyncio
|
||||
async def test_abort_while_disconnecting():
|
||||
# Setup client and server channels
|
||||
devices = TwoDevices()
|
||||
await devices.setup_connection()
|
||||
psm = 1234
|
||||
|
||||
server_channels = asyncio.Queue[l2cap.LeCreditBasedChannel]()
|
||||
devices.devices[1].create_l2cap_server(
|
||||
spec=l2cap.LeCreditBasedChannelSpec(psm=psm),
|
||||
handler=server_channels.put_nowait,
|
||||
)
|
||||
client_channel = await devices.connections[0].create_l2cap_channel(
|
||||
spec=l2cap.LeCreditBasedChannelSpec(psm)
|
||||
)
|
||||
server_channel = await server_channels.get()
|
||||
|
||||
# Stub the server's request handler to ignore the disconnection request.
|
||||
# This keeps the client channel in the DISCONNECTING state, waiting for a response,
|
||||
# so we can simulate calling abort() during an active disconnection.
|
||||
server_channel.on_disconnection_request = lambda request: None
|
||||
|
||||
# Intercept state change to DISCONNECTING and call abort()
|
||||
original_change_state = client_channel._change_state
|
||||
abort_called = False
|
||||
|
||||
def intercept_change_state_and_abort(new_state):
|
||||
nonlocal abort_called
|
||||
original_change_state(new_state)
|
||||
if (
|
||||
new_state == l2cap.LeCreditBasedChannel.State.DISCONNECTING
|
||||
and not abort_called
|
||||
):
|
||||
abort_called = True
|
||||
client_channel.abort()
|
||||
|
||||
client_channel._change_state = intercept_change_state_and_abort
|
||||
|
||||
# Start disconnection and wait with a timeout. It should resolve immediately due to the abort.
|
||||
await asyncio.wait_for(client_channel.disconnect(), timeout=1.0)
|
||||
assert client_channel.state == l2cap.LeCreditBasedChannel.State.DISCONNECTED
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.asyncio
|
||||
async def test_disconnection_collision():
|
||||
# Setup client and server channels
|
||||
devices = TwoDevices()
|
||||
await devices.setup_connection()
|
||||
psm = 1234
|
||||
|
||||
server_channels = asyncio.Queue[l2cap.LeCreditBasedChannel]()
|
||||
devices.devices[1].create_l2cap_server(
|
||||
spec=l2cap.LeCreditBasedChannelSpec(psm=psm),
|
||||
handler=server_channels.put_nowait,
|
||||
)
|
||||
client_channel = await devices.connections[0].create_l2cap_channel(
|
||||
spec=l2cap.LeCreditBasedChannelSpec(psm)
|
||||
)
|
||||
server_channel = await server_channels.get()
|
||||
|
||||
# Trigger disconnection from both sides concurrently to cause a collision.
|
||||
# Both channels will transition to DISCONNECTING and send DISCONNECTION_REQUESTs.
|
||||
# When each side receives the peer's request, it will handle it and resolve the
|
||||
# disconnection_result future.
|
||||
await asyncio.gather(
|
||||
client_channel.disconnect(),
|
||||
server_channel.disconnect(),
|
||||
)
|
||||
|
||||
assert client_channel.state == l2cap.LeCreditBasedChannel.State.DISCONNECTED
|
||||
assert server_channel.state == l2cap.LeCreditBasedChannel.State.DISCONNECTED
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
async def run():
|
||||
test_helpers()
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ async def test_self_disconnection():
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
'responder_role,',
|
||||
'responder_role',
|
||||
(Role.CENTRAL, Role.PERIPHERAL),
|
||||
)
|
||||
async def test_self_classic_connection(responder_role):
|
||||
|
||||
@@ -102,7 +102,7 @@ def test_parser_extensions():
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.parametrize(
|
||||
"address,",
|
||||
"address",
|
||||
("127.0.0.1", "::1"),
|
||||
)
|
||||
async def test_tcp_connection(address):
|
||||
@@ -205,7 +205,7 @@ async def test_unix_connection_abstract():
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.parametrize(
|
||||
"address,",
|
||||
"address",
|
||||
("127.0.0.1", "[::1]"),
|
||||
)
|
||||
async def test_android_netsim_connection(address):
|
||||
@@ -228,7 +228,7 @@ async def test_android_netsim_connection(address):
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
@pytest.mark.parametrize(
|
||||
"spec,",
|
||||
"spec",
|
||||
(
|
||||
"android-netsim:[::1]:{port},mode=host[a=b,c=d]",
|
||||
"android-netsim:localhost:{port},mode=host[a=b,c=d]",
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
# Copyright 2026 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.
|
||||
|
||||
import asyncio
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from bumble import hci
|
||||
from bumble.transport import usb
|
||||
|
||||
|
||||
def test_safe_call_soon_ignores_closed_loop():
|
||||
closed_loop = asyncio.new_event_loop()
|
||||
closed_loop.close()
|
||||
callback = mock.Mock()
|
||||
|
||||
usb._safe_call_soon(closed_loop, callback)
|
||||
|
||||
callback.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_safe_call_soon_schedules_callback_on_open_loop():
|
||||
called = asyncio.Event()
|
||||
args = []
|
||||
|
||||
def callback(value):
|
||||
args.append(value)
|
||||
called.set()
|
||||
|
||||
usb._safe_call_soon(asyncio.get_running_loop(), callback, 'scheduled')
|
||||
|
||||
await asyncio.wait_for(called.wait(), timeout=1.0)
|
||||
assert args == ['scheduled']
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_usb_packet_sink_iso_routing():
|
||||
# Mock usb1 device and endpoints
|
||||
mock_device = mock.Mock()
|
||||
mock_bulk_out = mock.Mock()
|
||||
mock_bulk_out.getAddress.return_value = 0x02
|
||||
|
||||
# Scenario 1: Isochronous endpoints are not enabled (isochronous_out is None)
|
||||
mock_transfer = mock.Mock()
|
||||
mock_device.getTransfer.return_value = mock_transfer
|
||||
|
||||
sink = usb.UsbPacketSink(mock_device, mock_bulk_out, isochronous_out=None)
|
||||
sink.start()
|
||||
|
||||
# Send HCI_ISO_DATA_PACKET
|
||||
iso_packet = bytes([hci.HCI_ISO_DATA_PACKET, 0x01, 0x02, 0x03])
|
||||
sink.on_packet(iso_packet)
|
||||
|
||||
# Yield control to let the queue processor run
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
# Verify it was sent via bulk transfer
|
||||
mock_transfer.setBulk.assert_called_once_with(
|
||||
0x02,
|
||||
bytes([0x01, 0x02, 0x03]),
|
||||
callback=sink.transfer_callback,
|
||||
)
|
||||
mock_transfer.submit.assert_called_once()
|
||||
|
||||
if sink.queue_task:
|
||||
sink.queue_task.cancel()
|
||||
try:
|
||||
await sink.queue_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_usb_packet_sink_iso_routing_with_iso_endpoint():
|
||||
# Mock usb1 device and endpoints
|
||||
mock_device = mock.Mock()
|
||||
mock_bulk_out = mock.Mock()
|
||||
mock_bulk_out.getAddress.return_value = 0x02
|
||||
mock_iso_out = mock.Mock()
|
||||
mock_iso_out.getMaxPacketSize.return_value = 64
|
||||
|
||||
# Scenario 2: Isochronous endpoints are enabled
|
||||
mock_transfer_bulk = mock.Mock()
|
||||
mock_transfer_iso = mock.Mock()
|
||||
|
||||
# getTransfer is called twice: once for bulk_or_control and once for isochronous
|
||||
mock_device.getTransfer.side_effect = [mock_transfer_bulk, mock_transfer_iso]
|
||||
|
||||
sink = usb.UsbPacketSink(mock_device, mock_bulk_out, isochronous_out=mock_iso_out)
|
||||
sink.start()
|
||||
|
||||
# Send HCI_ISO_DATA_PACKET
|
||||
iso_packet = bytes([hci.HCI_ISO_DATA_PACKET, 0x01, 0x02, 0x03])
|
||||
sink.on_packet(iso_packet)
|
||||
|
||||
# Yield control to let the queue processor run
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
# Verify it was NOT sent via bulk transfer
|
||||
mock_transfer_bulk.setBulk.assert_not_called()
|
||||
|
||||
if sink.queue_task:
|
||||
sink.queue_task.cancel()
|
||||
try:
|
||||
await sink.queue_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
Reference in New Issue
Block a user