aboutsummaryrefslogtreecommitdiff
path: root/tests/stm32/src/bin/wpan_ble.rs
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-07-15 14:28:42 -0500
committerxoviat <[email protected]>2023-07-15 14:28:42 -0500
commitd11a94e2a7c030dac7a7c4d6967f614104111d5a (patch)
tree0db43017247fa2445bc1fc8431c36f073df6e3ef /tests/stm32/src/bin/wpan_ble.rs
parentd6dd5ea5d3ab7309bd5b4bec28afaee68d20b4ae (diff)
wpan: add mac test
Diffstat (limited to 'tests/stm32/src/bin/wpan_ble.rs')
-rw-r--r--tests/stm32/src/bin/wpan_ble.rs251
1 files changed, 251 insertions, 0 deletions
diff --git a/tests/stm32/src/bin/wpan_ble.rs b/tests/stm32/src/bin/wpan_ble.rs
new file mode 100644
index 000000000..3ad8aca4e
--- /dev/null
+++ b/tests/stm32/src/bin/wpan_ble.rs
@@ -0,0 +1,251 @@
1// required-features: ble
2
3#![no_std]
4#![no_main]
5#![feature(type_alias_impl_trait)]
6#[path = "../common.rs"]
7mod common;
8
9use core::time::Duration;
10
11use common::*;
12use embassy_executor::Spawner;
13use embassy_stm32::bind_interrupts;
14use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler};
15use embassy_stm32_wpan::hci::host::uart::UartHci;
16use embassy_stm32_wpan::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType};
17use embassy_stm32_wpan::hci::types::AdvertisingType;
18use embassy_stm32_wpan::hci::vendor::stm32wb::command::gap::{
19 AdvertisingDataType, DiscoverableParameters, GapCommands, Role,
20};
21use embassy_stm32_wpan::hci::vendor::stm32wb::command::gatt::GattCommands;
22use embassy_stm32_wpan::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel};
23use embassy_stm32_wpan::hci::BdAddr;
24use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp;
25use embassy_stm32_wpan::sub::mm;
26use embassy_stm32_wpan::TlMbox;
27use {defmt_rtt as _, panic_probe as _};
28
29bind_interrupts!(struct Irqs{
30 IPCC_C1_RX => ReceiveInterruptHandler;
31 IPCC_C1_TX => TransmitInterruptHandler;
32});
33
34const BLE_GAP_DEVICE_NAME_LENGTH: u8 = 7;
35
36#[embassy_executor::task]
37async fn run_mm_queue(memory_manager: mm::MemoryManager) {
38 memory_manager.run_queue().await;
39}
40
41#[embassy_executor::main]
42async fn main(spawner: Spawner) {
43 let p = embassy_stm32::init(config());
44 info!("Hello World!");
45
46 let config = Config::default();
47 let mut mbox = TlMbox::init(p.IPCC, Irqs, config);
48
49 spawner.spawn(run_mm_queue(mbox.mm_subsystem)).unwrap();
50
51 let sys_event = mbox.sys_subsystem.read().await;
52 info!("sys event: {}", sys_event.payload());
53
54 let fw_info = mbox.sys_subsystem.wireless_fw_info().unwrap();
55 let version_major = fw_info.version_major();
56 let version_minor = fw_info.version_minor();
57 let subversion = fw_info.subversion();
58
59 let sram2a_size = fw_info.sram2a_size();
60 let sram2b_size = fw_info.sram2b_size();
61
62 info!(
63 "version {}.{}.{} - SRAM2a {} - SRAM2b {}",
64 version_major, version_minor, subversion, sram2a_size, sram2b_size
65 );
66
67 let _ = mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await;
68
69 info!("resetting BLE...");
70 mbox.ble_subsystem.reset().await;
71 let response = mbox.ble_subsystem.read().await.unwrap();
72 info!("{}", response);
73
74 info!("config public address...");
75 mbox.ble_subsystem
76 .write_config_data(&ConfigData::public_address(get_bd_addr()).build())
77 .await;
78 let response = mbox.ble_subsystem.read().await.unwrap();
79 info!("{}", response);
80
81 info!("config random address...");
82 mbox.ble_subsystem
83 .write_config_data(&ConfigData::random_address(get_random_addr()).build())
84 .await;
85 let response = mbox.ble_subsystem.read().await.unwrap();
86 info!("{}", response);
87
88 info!("config identity root...");
89 mbox.ble_subsystem
90 .write_config_data(&ConfigData::identity_root(&get_irk()).build())
91 .await;
92 let response = mbox.ble_subsystem.read().await.unwrap();
93 info!("{}", response);
94
95 info!("config encryption root...");
96 mbox.ble_subsystem
97 .write_config_data(&ConfigData::encryption_root(&get_erk()).build())
98 .await;
99 let response = mbox.ble_subsystem.read().await.unwrap();
100 info!("{}", response);
101
102 info!("config tx power level...");
103 mbox.ble_subsystem.set_tx_power_level(PowerLevel::ZerodBm).await;
104 let response = mbox.ble_subsystem.read().await.unwrap();
105 info!("{}", response);
106
107 info!("GATT init...");
108 mbox.ble_subsystem.init_gatt().await;
109 let response = mbox.ble_subsystem.read().await.unwrap();
110 info!("{}", response);
111
112 info!("GAP init...");
113 mbox.ble_subsystem
114 .init_gap(Role::PERIPHERAL, false, BLE_GAP_DEVICE_NAME_LENGTH)
115 .await;
116 let response = mbox.ble_subsystem.read().await.unwrap();
117 info!("{}", response);
118
119 // info!("set scan response...");
120 // mbox.ble_subsystem.le_set_scan_response_data(&[]).await.unwrap();
121 // let response = mbox.ble_subsystem.read().await.unwrap();
122 // info!("{}", response);
123
124 info!("set discoverable...");
125 mbox.ble_subsystem
126 .set_discoverable(&DiscoverableParameters {
127 advertising_type: AdvertisingType::NonConnectableUndirected,
128 advertising_interval: Some((Duration::from_millis(250), Duration::from_millis(250))),
129 address_type: OwnAddressType::Public,
130 filter_policy: AdvertisingFilterPolicy::AllowConnectionAndScan,
131 local_name: None,
132 advertising_data: &[],
133 conn_interval: (None, None),
134 })
135 .await
136 .unwrap();
137
138 let response = mbox.ble_subsystem.read().await;
139 info!("{}", response);
140
141 // remove some advertisement to decrease the packet size
142 info!("delete tx power ad type...");
143 mbox.ble_subsystem
144 .delete_ad_type(AdvertisingDataType::TxPowerLevel)
145 .await;
146 let response = mbox.ble_subsystem.read().await.unwrap();
147 info!("{}", response);
148
149 info!("delete conn interval ad type...");
150 mbox.ble_subsystem
151 .delete_ad_type(AdvertisingDataType::PeripheralConnectionInterval)
152 .await;
153 let response = mbox.ble_subsystem.read().await.unwrap();
154 info!("{}", response);
155
156 info!("update advertising data...");
157 mbox.ble_subsystem
158 .update_advertising_data(&eddystone_advertising_data())
159 .await
160 .unwrap();
161 let response = mbox.ble_subsystem.read().await.unwrap();
162 info!("{}", response);
163
164 info!("update advertising data type...");
165 mbox.ble_subsystem
166 .update_advertising_data(&[3, AdvertisingDataType::UuidCompleteList16 as u8, 0xaa, 0xfe])
167 .await
168 .unwrap();
169 let response = mbox.ble_subsystem.read().await.unwrap();
170 info!("{}", response);
171
172 info!("update advertising data flags...");
173 mbox.ble_subsystem
174 .update_advertising_data(&[
175 2,
176 AdvertisingDataType::Flags as u8,
177 (0x02 | 0x04) as u8, // BLE general discoverable, without BR/EDR support
178 ])
179 .await
180 .unwrap();
181 let response = mbox.ble_subsystem.read().await.unwrap();
182 info!("{}", response);
183
184 info!("Test OK");
185 cortex_m::asm::bkpt();
186}
187
188fn get_bd_addr() -> BdAddr {
189 let mut bytes = [0u8; 6];
190
191 let lhci_info = LhciC1DeviceInformationCcrp::new();
192 bytes[0] = (lhci_info.uid64 & 0xff) as u8;
193 bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8;
194 bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8;
195 bytes[3] = lhci_info.device_type_id;
196 bytes[4] = (lhci_info.st_company_id & 0xff) as u8;
197 bytes[5] = (lhci_info.st_company_id >> 8 & 0xff) as u8;
198
199 BdAddr(bytes)
200}
201
202fn get_random_addr() -> BdAddr {
203 let mut bytes = [0u8; 6];
204
205 let lhci_info = LhciC1DeviceInformationCcrp::new();
206 bytes[0] = (lhci_info.uid64 & 0xff) as u8;
207 bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8;
208 bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8;
209 bytes[3] = 0;
210 bytes[4] = 0x6E;
211 bytes[5] = 0xED;
212
213 BdAddr(bytes)
214}
215
216const BLE_CFG_IRK: [u8; 16] = [
217 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
218];
219const BLE_CFG_ERK: [u8; 16] = [
220 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21,
221];
222
223fn get_irk() -> EncryptionKey {
224 EncryptionKey(BLE_CFG_IRK)
225}
226
227fn get_erk() -> EncryptionKey {
228 EncryptionKey(BLE_CFG_ERK)
229}
230
231fn eddystone_advertising_data() -> [u8; 24] {
232 const EDDYSTONE_URL: &[u8] = b"www.rust-lang.com";
233
234 let mut service_data = [0u8; 24];
235 let url_len = EDDYSTONE_URL.len();
236
237 service_data[0] = 6 + url_len as u8;
238 service_data[1] = AdvertisingDataType::ServiceData as u8;
239
240 // 16-bit eddystone uuid
241 service_data[2] = 0xaa;
242 service_data[3] = 0xFE;
243
244 service_data[4] = 0x10; // URL frame type
245 service_data[5] = 22_i8 as u8; // calibrated TX power at 0m
246 service_data[6] = 0x03; // eddystone url prefix = https
247
248 service_data[7..(7 + url_len)].copy_from_slice(EDDYSTONE_URL);
249
250 service_data
251}