diff options
| author | xoviat <[email protected]> | 2023-07-15 14:18:01 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-07-15 14:18:01 -0500 |
| commit | 8a146a50ecd9168b2c856532b6f167e5b39433bb (patch) | |
| tree | cd343e5a6283bd64a7fca130b0aaa9587bcc1eff /examples/stm32wb/src/bin | |
| parent | f90b170dad91848d5a0ff746d873bd8a4ce7e91f (diff) | |
| parent | 0bde4992ea1b9f662fecaf062b1f6e09dd909112 (diff) | |
Merge branch 'master' into mac-3
Diffstat (limited to 'examples/stm32wb/src/bin')
| -rw-r--r-- | examples/stm32wb/src/bin/gatt_server.rs | 397 |
1 files changed, 397 insertions, 0 deletions
diff --git a/examples/stm32wb/src/bin/gatt_server.rs b/examples/stm32wb/src/bin/gatt_server.rs new file mode 100644 index 000000000..7621efb11 --- /dev/null +++ b/examples/stm32wb/src/bin/gatt_server.rs | |||
| @@ -0,0 +1,397 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use core::time::Duration; | ||
| 6 | |||
| 7 | use defmt::*; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_stm32::bind_interrupts; | ||
| 10 | use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler}; | ||
| 11 | use embassy_stm32_wpan::hci::event::command::{CommandComplete, ReturnParameters}; | ||
| 12 | use embassy_stm32_wpan::hci::host::uart::{Packet, UartHci}; | ||
| 13 | use embassy_stm32_wpan::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType}; | ||
| 14 | use embassy_stm32_wpan::hci::types::AdvertisingType; | ||
| 15 | use embassy_stm32_wpan::hci::vendor::stm32wb::command::gap::{ | ||
| 16 | AddressType, AuthenticationRequirements, DiscoverableParameters, GapCommands, IoCapability, LocalName, Pin, Role, | ||
| 17 | SecureConnectionSupport, | ||
| 18 | }; | ||
| 19 | use embassy_stm32_wpan::hci::vendor::stm32wb::command::gatt::{ | ||
| 20 | AddCharacteristicParameters, AddServiceParameters, CharacteristicEvent, CharacteristicPermission, | ||
| 21 | CharacteristicProperty, EncryptionKeySize, GattCommands, ServiceType, UpdateCharacteristicValueParameters, Uuid, | ||
| 22 | WriteResponseParameters, | ||
| 23 | }; | ||
| 24 | use embassy_stm32_wpan::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel}; | ||
| 25 | use embassy_stm32_wpan::hci::vendor::stm32wb::event::{self, AttributeHandle, Stm32Wb5xEvent}; | ||
| 26 | use embassy_stm32_wpan::hci::{BdAddr, Event}; | ||
| 27 | use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp; | ||
| 28 | use embassy_stm32_wpan::sub::ble::Ble; | ||
| 29 | use embassy_stm32_wpan::TlMbox; | ||
| 30 | use {defmt_rtt as _, panic_probe as _}; | ||
| 31 | |||
| 32 | bind_interrupts!(struct Irqs{ | ||
| 33 | IPCC_C1_RX => ReceiveInterruptHandler; | ||
| 34 | IPCC_C1_TX => TransmitInterruptHandler; | ||
| 35 | }); | ||
| 36 | |||
| 37 | const BLE_GAP_DEVICE_NAME_LENGTH: u8 = 7; | ||
| 38 | |||
| 39 | #[embassy_executor::main] | ||
| 40 | async fn main(_spawner: Spawner) { | ||
| 41 | /* | ||
| 42 | How to make this work: | ||
| 43 | |||
| 44 | - Obtain a NUCLEO-STM32WB55 from your preferred supplier. | ||
| 45 | - Download and Install STM32CubeProgrammer. | ||
| 46 | - Download stm32wb5x_FUS_fw.bin, stm32wb5x_BLE_Stack_full_fw.bin, and Release_Notes.html from | ||
| 47 | gh:STMicroelectronics/STM32CubeWB@2234d97/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x | ||
| 48 | - Open STM32CubeProgrammer | ||
| 49 | - On the right-hand pane, click "firmware upgrade" to upgrade the st-link firmware. | ||
| 50 | - Once complete, click connect to connect to the device. | ||
| 51 | - On the left hand pane, click the RSS signal icon to open "Firmware Upgrade Services". | ||
| 52 | - In the Release_Notes.html, find the memory address that corresponds to your device for the stm32wb5x_FUS_fw.bin file | ||
| 53 | - Select that file, the memory address, "verify download", and then "Firmware Upgrade". | ||
| 54 | - Once complete, in the Release_Notes.html, find the memory address that corresponds to your device for the | ||
| 55 | stm32wb5x_BLE_Stack_full_fw.bin file. It should not be the same memory address. | ||
| 56 | - Select that file, the memory address, "verify download", and then "Firmware Upgrade". | ||
| 57 | - Select "Start Wireless Stack". | ||
| 58 | - Disconnect from the device. | ||
| 59 | - In the examples folder for stm32wb, modify the memory.x file to match your target device. | ||
| 60 | - Run this example. | ||
| 61 | |||
| 62 | Note: extended stack versions are not supported at this time. Do not attempt to install a stack with "extended" in the name. | ||
| 63 | */ | ||
| 64 | |||
| 65 | let p = embassy_stm32::init(Default::default()); | ||
| 66 | info!("Hello World!"); | ||
| 67 | |||
| 68 | let config = Config::default(); | ||
| 69 | let mut mbox = TlMbox::init(p.IPCC, Irqs, config); | ||
| 70 | |||
| 71 | let sys_event = mbox.sys_subsystem.read().await; | ||
| 72 | info!("sys event: {}", sys_event.payload()); | ||
| 73 | |||
| 74 | mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await; | ||
| 75 | |||
| 76 | info!("resetting BLE..."); | ||
| 77 | mbox.ble_subsystem.reset().await; | ||
| 78 | let response = mbox.ble_subsystem.read().await; | ||
| 79 | defmt::debug!("{}", response); | ||
| 80 | |||
| 81 | info!("config public address..."); | ||
| 82 | mbox.ble_subsystem | ||
| 83 | .write_config_data(&ConfigData::public_address(get_bd_addr()).build()) | ||
| 84 | .await; | ||
| 85 | let response = mbox.ble_subsystem.read().await; | ||
| 86 | defmt::debug!("{}", response); | ||
| 87 | |||
| 88 | info!("config random address..."); | ||
| 89 | mbox.ble_subsystem | ||
| 90 | .write_config_data(&ConfigData::random_address(get_random_addr()).build()) | ||
| 91 | .await; | ||
| 92 | let response = mbox.ble_subsystem.read().await; | ||
| 93 | defmt::debug!("{}", response); | ||
| 94 | |||
| 95 | info!("config identity root..."); | ||
| 96 | mbox.ble_subsystem | ||
| 97 | .write_config_data(&ConfigData::identity_root(&get_irk()).build()) | ||
| 98 | .await; | ||
| 99 | let response = mbox.ble_subsystem.read().await; | ||
| 100 | defmt::debug!("{}", response); | ||
| 101 | |||
| 102 | info!("config encryption root..."); | ||
| 103 | mbox.ble_subsystem | ||
| 104 | .write_config_data(&ConfigData::encryption_root(&get_erk()).build()) | ||
| 105 | .await; | ||
| 106 | let response = mbox.ble_subsystem.read().await; | ||
| 107 | defmt::debug!("{}", response); | ||
| 108 | |||
| 109 | info!("config tx power level..."); | ||
| 110 | mbox.ble_subsystem.set_tx_power_level(PowerLevel::ZerodBm).await; | ||
| 111 | let response = mbox.ble_subsystem.read().await; | ||
| 112 | defmt::debug!("{}", response); | ||
| 113 | |||
| 114 | info!("GATT init..."); | ||
| 115 | mbox.ble_subsystem.init_gatt().await; | ||
| 116 | let response = mbox.ble_subsystem.read().await; | ||
| 117 | defmt::debug!("{}", response); | ||
| 118 | |||
| 119 | info!("GAP init..."); | ||
| 120 | mbox.ble_subsystem | ||
| 121 | .init_gap(Role::PERIPHERAL, false, BLE_GAP_DEVICE_NAME_LENGTH) | ||
| 122 | .await; | ||
| 123 | let response = mbox.ble_subsystem.read().await; | ||
| 124 | defmt::debug!("{}", response); | ||
| 125 | |||
| 126 | info!("set IO capabilities..."); | ||
| 127 | mbox.ble_subsystem.set_io_capability(IoCapability::DisplayConfirm).await; | ||
| 128 | let response = mbox.ble_subsystem.read().await; | ||
| 129 | defmt::debug!("{}", response); | ||
| 130 | |||
| 131 | info!("set authentication requirements..."); | ||
| 132 | mbox.ble_subsystem | ||
| 133 | .set_authentication_requirement(&AuthenticationRequirements { | ||
| 134 | bonding_required: false, | ||
| 135 | keypress_notification_support: false, | ||
| 136 | mitm_protection_required: false, | ||
| 137 | encryption_key_size_range: (8, 16), | ||
| 138 | fixed_pin: Pin::Requested, | ||
| 139 | identity_address_type: AddressType::Public, | ||
| 140 | secure_connection_support: SecureConnectionSupport::Optional, | ||
| 141 | }) | ||
| 142 | .await | ||
| 143 | .unwrap(); | ||
| 144 | let response = mbox.ble_subsystem.read().await; | ||
| 145 | defmt::debug!("{}", response); | ||
| 146 | |||
| 147 | info!("set scan response data..."); | ||
| 148 | mbox.ble_subsystem.le_set_scan_response_data(b"TXTX").await.unwrap(); | ||
| 149 | let response = mbox.ble_subsystem.read().await; | ||
| 150 | defmt::debug!("{}", response); | ||
| 151 | |||
| 152 | info!("set scan response data..."); | ||
| 153 | mbox.ble_subsystem.le_set_scan_response_data(b"TXTX").await.unwrap(); | ||
| 154 | let response = mbox.ble_subsystem.read().await; | ||
| 155 | defmt::debug!("{}", response); | ||
| 156 | |||
| 157 | defmt::info!("initializing services and characteristics..."); | ||
| 158 | let mut ble_context = init_gatt_services(&mut mbox.ble_subsystem).await.unwrap(); | ||
| 159 | defmt::info!("{}", ble_context); | ||
| 160 | |||
| 161 | let discovery_params = DiscoverableParameters { | ||
| 162 | advertising_type: AdvertisingType::ConnectableUndirected, | ||
| 163 | advertising_interval: Some((Duration::from_millis(100), Duration::from_millis(100))), | ||
| 164 | address_type: OwnAddressType::Public, | ||
| 165 | filter_policy: AdvertisingFilterPolicy::AllowConnectionAndScan, | ||
| 166 | local_name: Some(LocalName::Complete(b"TXTX")), | ||
| 167 | advertising_data: &[], | ||
| 168 | conn_interval: (None, None), | ||
| 169 | }; | ||
| 170 | |||
| 171 | info!("set discoverable..."); | ||
| 172 | mbox.ble_subsystem.set_discoverable(&discovery_params).await.unwrap(); | ||
| 173 | let response = mbox.ble_subsystem.read().await; | ||
| 174 | defmt::debug!("{}", response); | ||
| 175 | |||
| 176 | loop { | ||
| 177 | let response = mbox.ble_subsystem.read().await; | ||
| 178 | defmt::debug!("{}", response); | ||
| 179 | |||
| 180 | if let Ok(Packet::Event(event)) = response { | ||
| 181 | match event { | ||
| 182 | Event::LeConnectionComplete(_) => { | ||
| 183 | defmt::info!("connected"); | ||
| 184 | } | ||
| 185 | Event::DisconnectionComplete(_) => { | ||
| 186 | defmt::info!("disconnected"); | ||
| 187 | ble_context.is_subscribed = false; | ||
| 188 | mbox.ble_subsystem.set_discoverable(&discovery_params).await.unwrap(); | ||
| 189 | } | ||
| 190 | Event::Vendor(vendor_event) => match vendor_event { | ||
| 191 | Stm32Wb5xEvent::AttReadPermitRequest(read_req) => { | ||
| 192 | defmt::info!("read request received {}, allowing", read_req); | ||
| 193 | mbox.ble_subsystem.allow_read(read_req.conn_handle).await | ||
| 194 | } | ||
| 195 | Stm32Wb5xEvent::AttWritePermitRequest(write_req) => { | ||
| 196 | defmt::info!("write request received {}, allowing", write_req); | ||
| 197 | mbox.ble_subsystem | ||
| 198 | .write_response(&WriteResponseParameters { | ||
| 199 | conn_handle: write_req.conn_handle, | ||
| 200 | attribute_handle: write_req.attribute_handle, | ||
| 201 | status: Ok(()), | ||
| 202 | value: write_req.value(), | ||
| 203 | }) | ||
| 204 | .await | ||
| 205 | .unwrap() | ||
| 206 | } | ||
| 207 | Stm32Wb5xEvent::GattAttributeModified(attribute) => { | ||
| 208 | defmt::info!("{}", ble_context); | ||
| 209 | if attribute.attr_handle.0 == ble_context.chars.notify.0 + 2 { | ||
| 210 | if attribute.data()[0] == 0x01 { | ||
| 211 | defmt::info!("subscribed"); | ||
| 212 | ble_context.is_subscribed = true; | ||
| 213 | } else { | ||
| 214 | defmt::info!("unsubscribed"); | ||
| 215 | ble_context.is_subscribed = false; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | } | ||
| 219 | _ => {} | ||
| 220 | }, | ||
| 221 | _ => {} | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | fn get_bd_addr() -> BdAddr { | ||
| 228 | let mut bytes = [0u8; 6]; | ||
| 229 | |||
| 230 | let lhci_info = LhciC1DeviceInformationCcrp::new(); | ||
| 231 | bytes[0] = (lhci_info.uid64 & 0xff) as u8; | ||
| 232 | bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8; | ||
| 233 | bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8; | ||
| 234 | bytes[3] = lhci_info.device_type_id; | ||
| 235 | bytes[4] = (lhci_info.st_company_id & 0xff) as u8; | ||
| 236 | bytes[5] = (lhci_info.st_company_id >> 8 & 0xff) as u8; | ||
| 237 | |||
| 238 | BdAddr(bytes) | ||
| 239 | } | ||
| 240 | |||
| 241 | fn get_random_addr() -> BdAddr { | ||
| 242 | let mut bytes = [0u8; 6]; | ||
| 243 | |||
| 244 | let lhci_info = LhciC1DeviceInformationCcrp::new(); | ||
| 245 | bytes[0] = (lhci_info.uid64 & 0xff) as u8; | ||
| 246 | bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8; | ||
| 247 | bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8; | ||
| 248 | bytes[3] = 0; | ||
| 249 | bytes[4] = 0x6E; | ||
| 250 | bytes[5] = 0xED; | ||
| 251 | |||
| 252 | BdAddr(bytes) | ||
| 253 | } | ||
| 254 | |||
| 255 | const BLE_CFG_IRK: [u8; 16] = [ | ||
| 256 | 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, | ||
| 257 | ]; | ||
| 258 | const BLE_CFG_ERK: [u8; 16] = [ | ||
| 259 | 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, | ||
| 260 | ]; | ||
| 261 | |||
| 262 | fn get_irk() -> EncryptionKey { | ||
| 263 | EncryptionKey(BLE_CFG_IRK) | ||
| 264 | } | ||
| 265 | |||
| 266 | fn get_erk() -> EncryptionKey { | ||
| 267 | EncryptionKey(BLE_CFG_ERK) | ||
| 268 | } | ||
| 269 | |||
| 270 | #[derive(defmt::Format)] | ||
| 271 | pub struct BleContext { | ||
| 272 | pub service_handle: AttributeHandle, | ||
| 273 | pub chars: CharHandles, | ||
| 274 | pub is_subscribed: bool, | ||
| 275 | } | ||
| 276 | |||
| 277 | #[derive(defmt::Format)] | ||
| 278 | pub struct CharHandles { | ||
| 279 | pub read: AttributeHandle, | ||
| 280 | pub write: AttributeHandle, | ||
| 281 | pub notify: AttributeHandle, | ||
| 282 | } | ||
| 283 | |||
| 284 | pub async fn init_gatt_services(ble_subsystem: &mut Ble) -> Result<BleContext, ()> { | ||
| 285 | let service_handle = gatt_add_service(ble_subsystem, Uuid::Uuid16(0x500)).await?; | ||
| 286 | |||
| 287 | let read = gatt_add_char( | ||
| 288 | ble_subsystem, | ||
| 289 | service_handle, | ||
| 290 | Uuid::Uuid16(0x501), | ||
| 291 | CharacteristicProperty::READ, | ||
| 292 | Some(b"Hello from embassy!"), | ||
| 293 | ) | ||
| 294 | .await?; | ||
| 295 | |||
| 296 | let write = gatt_add_char( | ||
| 297 | ble_subsystem, | ||
| 298 | service_handle, | ||
| 299 | Uuid::Uuid16(0x502), | ||
| 300 | CharacteristicProperty::WRITE_WITHOUT_RESPONSE | CharacteristicProperty::WRITE | CharacteristicProperty::READ, | ||
| 301 | None, | ||
| 302 | ) | ||
| 303 | .await?; | ||
| 304 | |||
| 305 | let notify = gatt_add_char( | ||
| 306 | ble_subsystem, | ||
| 307 | service_handle, | ||
| 308 | Uuid::Uuid16(0x503), | ||
| 309 | CharacteristicProperty::NOTIFY | CharacteristicProperty::READ, | ||
| 310 | None, | ||
| 311 | ) | ||
| 312 | .await?; | ||
| 313 | |||
| 314 | Ok(BleContext { | ||
| 315 | service_handle, | ||
| 316 | is_subscribed: false, | ||
| 317 | chars: CharHandles { read, write, notify }, | ||
| 318 | }) | ||
| 319 | } | ||
| 320 | |||
| 321 | async fn gatt_add_service(ble_subsystem: &mut Ble, uuid: Uuid) -> Result<AttributeHandle, ()> { | ||
| 322 | ble_subsystem | ||
| 323 | .add_service(&AddServiceParameters { | ||
| 324 | uuid, | ||
| 325 | service_type: ServiceType::Primary, | ||
| 326 | max_attribute_records: 8, | ||
| 327 | }) | ||
| 328 | .await; | ||
| 329 | let response = ble_subsystem.read().await; | ||
| 330 | defmt::debug!("{}", response); | ||
| 331 | |||
| 332 | if let Ok(Packet::Event(Event::CommandComplete(CommandComplete { | ||
| 333 | return_params: | ||
| 334 | ReturnParameters::Vendor(event::command::ReturnParameters::GattAddService(event::command::GattService { | ||
| 335 | service_handle, | ||
| 336 | .. | ||
| 337 | })), | ||
| 338 | .. | ||
| 339 | }))) = response | ||
| 340 | { | ||
| 341 | Ok(service_handle) | ||
| 342 | } else { | ||
| 343 | Err(()) | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | async fn gatt_add_char( | ||
| 348 | ble_subsystem: &mut Ble, | ||
| 349 | service_handle: AttributeHandle, | ||
| 350 | characteristic_uuid: Uuid, | ||
| 351 | characteristic_properties: CharacteristicProperty, | ||
| 352 | default_value: Option<&[u8]>, | ||
| 353 | ) -> Result<AttributeHandle, ()> { | ||
| 354 | ble_subsystem | ||
| 355 | .add_characteristic(&AddCharacteristicParameters { | ||
| 356 | service_handle, | ||
| 357 | characteristic_uuid, | ||
| 358 | characteristic_properties, | ||
| 359 | characteristic_value_len: 32, | ||
| 360 | security_permissions: CharacteristicPermission::empty(), | ||
| 361 | gatt_event_mask: CharacteristicEvent::all(), | ||
| 362 | encryption_key_size: EncryptionKeySize::with_value(7).unwrap(), | ||
| 363 | is_variable: true, | ||
| 364 | }) | ||
| 365 | .await; | ||
| 366 | let response = ble_subsystem.read().await; | ||
| 367 | defmt::debug!("{}", response); | ||
| 368 | |||
| 369 | if let Ok(Packet::Event(Event::CommandComplete(CommandComplete { | ||
| 370 | return_params: | ||
| 371 | ReturnParameters::Vendor(event::command::ReturnParameters::GattAddCharacteristic( | ||
| 372 | event::command::GattCharacteristic { | ||
| 373 | characteristic_handle, .. | ||
| 374 | }, | ||
| 375 | )), | ||
| 376 | .. | ||
| 377 | }))) = response | ||
| 378 | { | ||
| 379 | if let Some(value) = default_value { | ||
| 380 | ble_subsystem | ||
| 381 | .update_characteristic_value(&UpdateCharacteristicValueParameters { | ||
| 382 | service_handle, | ||
| 383 | characteristic_handle, | ||
| 384 | offset: 0, | ||
| 385 | value, | ||
| 386 | }) | ||
| 387 | .await | ||
| 388 | .unwrap(); | ||
| 389 | |||
| 390 | let response = ble_subsystem.read().await; | ||
| 391 | defmt::debug!("{}", response); | ||
| 392 | } | ||
| 393 | Ok(characteristic_handle) | ||
| 394 | } else { | ||
| 395 | Err(()) | ||
| 396 | } | ||
| 397 | } | ||
