-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manage lifecycle of CIS and SCO links in host
- Loading branch information
Showing
3 changed files
with
272 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
# Copyright 2021-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. | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Imports | ||
# ----------------------------------------------------------------------------- | ||
import asyncio | ||
import logging | ||
import os | ||
import pytest | ||
from unittest import mock | ||
|
||
|
||
from bumble.core import BT_BR_EDR_TRANSPORT | ||
from bumble.controller import Controller | ||
from bumble.link import LocalLink | ||
from bumble.host import Host | ||
from bumble.transport import AsyncPipeSink | ||
from bumble.hci import ( | ||
Address, | ||
HCI_Connection_Request_Event, | ||
HCI_Connection_Complete_Event, | ||
HCI_Disconnection_Complete_Event, | ||
HCI_Synchronous_Connection_Complete_Event, | ||
HCI_LE_CIS_Request_Event, | ||
HCI_LE_CIS_Established_Event, | ||
HCI_SUCCESS, | ||
HCI_REMOTE_USER_TERMINATED_CONNECTION_ERROR, | ||
) | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Logging | ||
# ----------------------------------------------------------------------------- | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
def make_host(): | ||
link = LocalLink() | ||
controller = Controller('C1', link=link) | ||
return Host(controller, AsyncPipeSink(controller)) | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
@pytest.mark.asyncio | ||
async def test_acl_connection_disconnection(): | ||
host = make_host() | ||
|
||
m = mock.Mock() | ||
host.on('connection_request', m) | ||
host.on_hci_packet( | ||
HCI_Connection_Request_Event( | ||
bd_addr=Address('00:11:22:33:44:55'), | ||
class_of_device=0, | ||
link_type=HCI_Connection_Complete_Event.ACL_LINK_TYPE, | ||
) | ||
) | ||
m.assert_called_with( | ||
Address('00:11:22:33:44:55'), 0, HCI_Connection_Complete_Event.ACL_LINK_TYPE | ||
) | ||
|
||
host.on('connection', m) | ||
host.on_hci_packet( | ||
HCI_Connection_Complete_Event( | ||
status=HCI_SUCCESS, | ||
bd_addr=Address('00:11:22:33:44:55'), | ||
connection_handle=0x0001, | ||
link_type=HCI_Connection_Complete_Event.ACL_LINK_TYPE, | ||
encryption_enabled=0, | ||
) | ||
) | ||
assert host.connections[0x0001].handle == 0x0001 | ||
assert host.connections[0x0001].peer_address == Address('00:11:22:33:44:55') | ||
m.assert_called_with( | ||
0x0001, BT_BR_EDR_TRANSPORT, Address('00:11:22:33:44:55'), None, None | ||
) | ||
|
||
host.on('disconnection', m) | ||
host.on_hci_packet( | ||
HCI_Disconnection_Complete_Event( | ||
status=HCI_SUCCESS, | ||
connection_handle=0x0001, | ||
reason=HCI_REMOTE_USER_TERMINATED_CONNECTION_ERROR, | ||
) | ||
) | ||
m.assert_called_with(0x0001, HCI_REMOTE_USER_TERMINATED_CONNECTION_ERROR) | ||
assert 0x0001 not in host.connections | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
@pytest.mark.asyncio | ||
async def test_sco_connection_disconnection(): | ||
host = make_host() | ||
|
||
m = mock.Mock() | ||
host.on('connection_request', m) | ||
host.on_hci_packet( | ||
HCI_Connection_Request_Event( | ||
bd_addr=Address('00:11:22:33:44:55'), | ||
class_of_device=0, | ||
link_type=HCI_Connection_Complete_Event.SCO_LINK_TYPE, | ||
) | ||
) | ||
m.assert_called_with( | ||
Address('00:11:22:33:44:55'), 0, HCI_Connection_Complete_Event.SCO_LINK_TYPE | ||
) | ||
|
||
host.on('sco_connection', m) | ||
host.on_hci_packet( | ||
HCI_Synchronous_Connection_Complete_Event( | ||
status=HCI_SUCCESS, | ||
bd_addr=Address('00:11:22:33:44:55'), | ||
link_type=HCI_Connection_Complete_Event.SCO_LINK_TYPE, | ||
connection_handle=0x0001, | ||
transmission_interval=0, | ||
retransmission_window=0, | ||
rx_packet_length=0, | ||
tx_packet_length=0, | ||
air_mode=0, | ||
) | ||
) | ||
assert host.sco_links[0x0001].handle == 0x0001 | ||
assert host.sco_links[0x0001].peer_address == Address('00:11:22:33:44:55') | ||
m.assert_called_with( | ||
Address('00:11:22:33:44:55'), | ||
0x0001, | ||
HCI_Connection_Complete_Event.SCO_LINK_TYPE, | ||
) | ||
|
||
host.on('disconnection', m) | ||
host.on_hci_packet( | ||
HCI_Disconnection_Complete_Event( | ||
status=HCI_SUCCESS, | ||
connection_handle=0x0001, | ||
reason=HCI_REMOTE_USER_TERMINATED_CONNECTION_ERROR, | ||
) | ||
) | ||
assert 0x0001 not in host.sco_links | ||
m.assert_called_with(0x0001, HCI_REMOTE_USER_TERMINATED_CONNECTION_ERROR) | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
@pytest.mark.asyncio | ||
async def test_cis_connection_disconnection(): | ||
host = make_host() | ||
|
||
m = mock.Mock() | ||
host.on('cis_request', m) | ||
host.on_hci_packet( | ||
HCI_LE_CIS_Request_Event( | ||
acl_connection_handle=0x0001, | ||
cis_connection_handle=0x0002, | ||
cig_id=0x0001, | ||
cis_id=0x0001, | ||
) | ||
) | ||
m.assert_called_with(0x0001, 0x0002, 0x0001, 0x0001) | ||
|
||
host.on('cis_establishment', m) | ||
host.on_hci_packet( | ||
HCI_LE_CIS_Established_Event( | ||
status=HCI_SUCCESS, | ||
connection_handle=0x0002, | ||
cig_sync_delay=0, | ||
cis_sync_delay=0, | ||
transport_latency_c_to_p=0, | ||
transport_latency_p_to_c=0, | ||
phy_c_to_p=0, | ||
phy_p_to_c=0, | ||
nse=0, | ||
bn_c_to_p=0, | ||
bn_p_to_c=0, | ||
ft_c_to_p=0, | ||
ft_p_to_c=0, | ||
max_pdu_c_to_p=0, | ||
max_pdu_p_to_c=0, | ||
iso_interval=0, | ||
) | ||
) | ||
assert host.cis_links[0x0002].handle == 0x0002 | ||
m.assert_called_with(0x0002) | ||
|
||
host.on('disconnection', m) | ||
host.on_hci_packet( | ||
HCI_Disconnection_Complete_Event( | ||
status=HCI_SUCCESS, | ||
connection_handle=0x0002, | ||
reason=HCI_REMOTE_USER_TERMINATED_CONNECTION_ERROR, | ||
) | ||
) | ||
assert 0x0002 not in host.cis_links | ||
m.assert_called_with(0x0002, HCI_REMOTE_USER_TERMINATED_CONNECTION_ERROR) | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
async def run_test_host(): | ||
await test_acl_connection_disconnection() | ||
await test_sco_connection_disconnection() | ||
await test_cis_connection_disconnection() | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
if __name__ == '__main__': | ||
logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'INFO').upper()) | ||
asyncio.run(run_test_host()) |