1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use bumble::wrapper::{
16 controller::Controller,
17 device::Device,
18 hci::{
19 packets::{
20 AddressType, Enable, ErrorCode, LeScanType, LeScanningFilterPolicy,
21 LeSetScanEnableBuilder, LeSetScanEnableComplete, LeSetScanParametersBuilder,
22 LeSetScanParametersComplete, OwnAddressType,
23 },
24 Address, Error,
25 },
26 host::Host,
27 link::Link,
28 };
29 use pyo3::{
30 exceptions::PyException,
31 {PyErr, PyResult},
32 };
33
34 #[pyo3_asyncio::tokio::test]
test_hci_roundtrip_success_and_failure() -> PyResult<()>35 async fn test_hci_roundtrip_success_and_failure() -> PyResult<()> {
36 let address = Address::new("F0:F1:F2:F3:F4:F5", AddressType::RandomDeviceAddress)?;
37 let device = create_local_device(address).await?;
38
39 device.power_on().await?;
40
41 // BLE Spec Core v5.3
42 // 7.8.9 LE Set Scan Parameters command
43 // ...
44 // The Host shall not issue this command when scanning is enabled in the
45 // Controller; if it is the Command Disallowed error code shall be used.
46 // ...
47
48 let command = LeSetScanEnableBuilder {
49 filter_duplicates: Enable::Disabled,
50 // will cause failure later
51 le_scan_enable: Enable::Enabled,
52 };
53
54 let event: LeSetScanEnableComplete = device
55 .send_command(command.into(), false)
56 .await?
57 .try_into()
58 .map_err(|e: Error| PyErr::new::<PyException, _>(e.to_string()))?;
59
60 assert_eq!(ErrorCode::Success, event.get_status());
61
62 let command = LeSetScanParametersBuilder {
63 le_scan_type: LeScanType::Passive,
64 le_scan_interval: 0,
65 le_scan_window: 0,
66 own_address_type: OwnAddressType::RandomDeviceAddress,
67 scanning_filter_policy: LeScanningFilterPolicy::AcceptAll,
68 };
69
70 let event: LeSetScanParametersComplete = device
71 .send_command(command.into(), false)
72 .await?
73 .try_into()
74 .map_err(|e: Error| PyErr::new::<PyException, _>(e.to_string()))?;
75
76 assert_eq!(ErrorCode::CommandDisallowed, event.get_status());
77
78 Ok(())
79 }
80
create_local_device(address: Address) -> PyResult<Device>81 async fn create_local_device(address: Address) -> PyResult<Device> {
82 let link = Link::new_local_link()?;
83 let controller = Controller::new("C1", None, None, Some(link), Some(address.clone())).await?;
84 let host = Host::new(controller.clone().into(), controller.into()).await?;
85 Device::new(None, Some(address), None, Some(host), None)
86 }
87