diff options
| author | goueslati <[email protected]> | 2023-06-22 15:21:14 +0100 |
|---|---|---|
| committer | goueslati <[email protected]> | 2023-06-22 15:21:14 +0100 |
| commit | cd4f8f13a2c533f4e752c2e446661a6d86b19fe6 (patch) | |
| tree | 8bd29c956fa5a57a5ec6fbf7ade3799b9370b8f3 | |
| parent | 1f2be2dac5eeed739d2866b9b63ca06fdd84c276 (diff) | |
wpan: add BLE HCI
| -rw-r--r-- | embassy-stm32-wpan/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/ble.rs | 19 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/cmd.rs | 2 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/evt.rs | 23 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/lhci.rs | 111 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/lib.rs | 2 | ||||
| -rw-r--r-- | examples/stm32wb/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | examples/stm32wb/Cargo.toml | 6 | ||||
| -rw-r--r-- | examples/stm32wb/src/bin/eddystone_beacon.rs | 249 |
9 files changed, 412 insertions, 6 deletions
diff --git a/embassy-stm32-wpan/Cargo.toml b/embassy-stm32-wpan/Cargo.toml index f9023ed79..fda4189ca 100644 --- a/embassy-stm32-wpan/Cargo.toml +++ b/embassy-stm32-wpan/Cargo.toml | |||
| @@ -23,11 +23,13 @@ cortex-m = "0.7.6" | |||
| 23 | heapless = "0.7.16" | 23 | heapless = "0.7.16" |
| 24 | 24 | ||
| 25 | bit_field = "0.10.2" | 25 | bit_field = "0.10.2" |
| 26 | stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] } | ||
| 27 | bluetooth-hci-async = { version = "*", git = "https://github.com/OueslatiGhaith/bluetooth-hci", optional = true } | ||
| 26 | 28 | ||
| 27 | [features] | 29 | [features] |
| 28 | defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt"] | 30 | defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt"] |
| 29 | 31 | ||
| 30 | ble = [] | 32 | ble = ["dep:bluetooth-hci-async"] |
| 31 | mac = [] | 33 | mac = [] |
| 32 | 34 | ||
| 33 | stm32wb10cc = [ "embassy-stm32/stm32wb10cc" ] | 35 | stm32wb10cc = [ "embassy-stm32/stm32wb10cc" ] |
diff --git a/embassy-stm32-wpan/src/ble.rs b/embassy-stm32-wpan/src/ble.rs index 60e0cbdf8..297ee4cdf 100644 --- a/embassy-stm32-wpan/src/ble.rs +++ b/embassy-stm32-wpan/src/ble.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | use core::marker::PhantomData; | 1 | use core::marker::PhantomData; |
| 2 | 2 | ||
| 3 | use embassy_stm32::ipcc::Ipcc; | 3 | use embassy_stm32::ipcc::Ipcc; |
| 4 | use hci::Opcode; | ||
| 4 | 5 | ||
| 5 | use crate::channels; | 6 | use crate::channels; |
| 6 | use crate::cmd::CmdPacket; | 7 | use crate::cmd::CmdPacket; |
| @@ -29,7 +30,7 @@ impl Ble { | |||
| 29 | Self { phantom: PhantomData } | 30 | Self { phantom: PhantomData } |
| 30 | } | 31 | } |
| 31 | /// `HW_IPCC_BLE_EvtNot` | 32 | /// `HW_IPCC_BLE_EvtNot` |
| 32 | pub async fn read(&self) -> EvtBox { | 33 | pub async fn tl_read(&self) -> EvtBox { |
| 33 | Ipcc::receive(channels::cpu2::IPCC_BLE_EVENT_CHANNEL, || unsafe { | 34 | Ipcc::receive(channels::cpu2::IPCC_BLE_EVENT_CHANNEL, || unsafe { |
| 34 | if let Some(node_ptr) = LinkedListNode::remove_head(EVT_QUEUE.as_mut_ptr()) { | 35 | if let Some(node_ptr) = LinkedListNode::remove_head(EVT_QUEUE.as_mut_ptr()) { |
| 35 | Some(EvtBox::new(node_ptr.cast())) | 36 | Some(EvtBox::new(node_ptr.cast())) |
| @@ -41,7 +42,7 @@ impl Ble { | |||
| 41 | } | 42 | } |
| 42 | 43 | ||
| 43 | /// `TL_BLE_SendCmd` | 44 | /// `TL_BLE_SendCmd` |
| 44 | pub async fn write(&self, opcode: u16, payload: &[u8]) { | 45 | pub async fn tl_write(&self, opcode: u16, payload: &[u8]) { |
| 45 | Ipcc::send(channels::cpu1::IPCC_BLE_CMD_CHANNEL, || unsafe { | 46 | Ipcc::send(channels::cpu1::IPCC_BLE_CMD_CHANNEL, || unsafe { |
| 46 | CmdPacket::write_into(BLE_CMD_BUFFER.as_mut_ptr(), TlPacketType::BleCmd, opcode, payload); | 47 | CmdPacket::write_into(BLE_CMD_BUFFER.as_mut_ptr(), TlPacketType::BleCmd, opcode, payload); |
| 47 | }) | 48 | }) |
| @@ -61,3 +62,17 @@ impl Ble { | |||
| 61 | .await; | 62 | .await; |
| 62 | } | 63 | } |
| 63 | } | 64 | } |
| 65 | |||
| 66 | pub extern crate bluetooth_hci_async as hci; | ||
| 67 | |||
| 68 | impl hci::Controller for Ble { | ||
| 69 | async fn controller_write(&mut self, opcode: Opcode, payload: &[u8]) { | ||
| 70 | self.tl_write(opcode.0, payload).await; | ||
| 71 | } | ||
| 72 | |||
| 73 | async fn controller_read(&self) -> &[u8] { | ||
| 74 | let evt_box = self.tl_read().await; | ||
| 75 | |||
| 76 | evt_box.serial() | ||
| 77 | } | ||
| 78 | } | ||
diff --git a/embassy-stm32-wpan/src/cmd.rs b/embassy-stm32-wpan/src/cmd.rs index c8056aaa7..8428b6ffc 100644 --- a/embassy-stm32-wpan/src/cmd.rs +++ b/embassy-stm32-wpan/src/cmd.rs | |||
| @@ -52,7 +52,7 @@ impl CmdPacket { | |||
| 52 | p_cmd_serial, | 52 | p_cmd_serial, |
| 53 | CmdSerialStub { | 53 | CmdSerialStub { |
| 54 | ty: packet_type as u8, | 54 | ty: packet_type as u8, |
| 55 | cmd_code: cmd_code, | 55 | cmd_code, |
| 56 | payload_len: payload.len() as u8, | 56 | payload_len: payload.len() as u8, |
| 57 | }, | 57 | }, |
| 58 | ); | 58 | ); |
diff --git a/embassy-stm32-wpan/src/evt.rs b/embassy-stm32-wpan/src/evt.rs index 47bdc49bf..7a4738b7a 100644 --- a/embassy-stm32-wpan/src/evt.rs +++ b/embassy-stm32-wpan/src/evt.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | use core::{ptr, slice}; | 1 | use core::{ptr, slice}; |
| 2 | 2 | ||
| 3 | use super::PacketHeader; | 3 | use super::PacketHeader; |
| 4 | use crate::consts::TL_EVT_HEADER_SIZE; | ||
| 4 | 5 | ||
| 5 | /** | 6 | /** |
| 6 | * The payload of `Evt` for a command status event | 7 | * The payload of `Evt` for a command status event |
| @@ -105,6 +106,14 @@ impl EvtBox { | |||
| 105 | Self { ptr } | 106 | Self { ptr } |
| 106 | } | 107 | } |
| 107 | 108 | ||
| 109 | pub fn evt<'a>(&self) -> &'a [u8] { | ||
| 110 | unsafe { | ||
| 111 | let evt_packet = &(*self.ptr); | ||
| 112 | |||
| 113 | core::slice::from_raw_parts(evt_packet as *const _ as *const u8, core::mem::size_of::<EvtPacket>()) | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 108 | /// Returns information about the event | 117 | /// Returns information about the event |
| 109 | pub fn stub(&self) -> EvtStub { | 118 | pub fn stub(&self) -> EvtStub { |
| 110 | unsafe { | 119 | unsafe { |
| @@ -124,6 +133,20 @@ impl EvtBox { | |||
| 124 | slice::from_raw_parts(p_payload, payload_len as usize) | 133 | slice::from_raw_parts(p_payload, payload_len as usize) |
| 125 | } | 134 | } |
| 126 | } | 135 | } |
| 136 | |||
| 137 | /// writes an underlying [`EvtPacket`] into the provided buffer. | ||
| 138 | /// Returns the number of bytes that were written. | ||
| 139 | /// Returns an error if event kind is unknown or if provided buffer size is not enough. | ||
| 140 | pub fn serial<'a>(&self) -> &'a [u8] { | ||
| 141 | unsafe { | ||
| 142 | let evt_serial: *const EvtSerial = &(*self.ptr).evt_serial; | ||
| 143 | let evt_serial_buf: *const u8 = evt_serial.cast(); | ||
| 144 | |||
| 145 | let len = (*evt_serial).evt.payload_len as usize + TL_EVT_HEADER_SIZE; | ||
| 146 | |||
| 147 | slice::from_raw_parts(evt_serial_buf, len) | ||
| 148 | } | ||
| 149 | } | ||
| 127 | } | 150 | } |
| 128 | 151 | ||
| 129 | impl Drop for EvtBox { | 152 | impl Drop for EvtBox { |
diff --git a/embassy-stm32-wpan/src/lhci.rs b/embassy-stm32-wpan/src/lhci.rs new file mode 100644 index 000000000..62116a695 --- /dev/null +++ b/embassy-stm32-wpan/src/lhci.rs | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | use crate::cmd::CmdPacket; | ||
| 2 | use crate::consts::{TlPacketType, TL_EVT_HEADER_SIZE}; | ||
| 3 | use crate::evt::{CcEvt, EvtPacket, EvtSerial}; | ||
| 4 | use crate::tables::{DeviceInfoTable, RssInfoTable, SafeBootInfoTable, WirelessFwInfoTable}; | ||
| 5 | use crate::TL_REF_TABLE; | ||
| 6 | |||
| 7 | const TL_BLEEVT_CC_OPCODE: u8 = 0x0e; | ||
| 8 | const LHCI_OPCODE_C1_DEVICE_INF: u16 = 0xfd62; | ||
| 9 | |||
| 10 | const PACKAGE_DATA_PTR: *const u8 = 0x1FFF_7500 as _; | ||
| 11 | const UID64_PTR: *const u32 = 0x1FFF_7580 as _; | ||
| 12 | |||
| 13 | #[derive(Debug, Copy, Clone)] | ||
| 14 | #[repr(C, packed)] | ||
| 15 | pub struct LhciC1DeviceInformationCcrp { | ||
| 16 | pub status: u8, | ||
| 17 | pub rev_id: u16, | ||
| 18 | pub dev_code_id: u16, | ||
| 19 | pub package_type: u8, | ||
| 20 | pub device_type_id: u8, | ||
| 21 | pub st_company_id: u32, | ||
| 22 | pub uid64: u32, | ||
| 23 | |||
| 24 | pub uid96_0: u32, | ||
| 25 | pub uid96_1: u32, | ||
| 26 | pub uid96_2: u32, | ||
| 27 | |||
| 28 | pub safe_boot_info_table: SafeBootInfoTable, | ||
| 29 | pub rss_info_table: RssInfoTable, | ||
| 30 | pub wireless_fw_info_table: WirelessFwInfoTable, | ||
| 31 | |||
| 32 | pub app_fw_inf: u32, | ||
| 33 | } | ||
| 34 | |||
| 35 | impl Default for LhciC1DeviceInformationCcrp { | ||
| 36 | fn default() -> Self { | ||
| 37 | let DeviceInfoTable { | ||
| 38 | safe_boot_info_table, | ||
| 39 | rss_info_table, | ||
| 40 | wireless_fw_info_table, | ||
| 41 | } = unsafe { &*(*TL_REF_TABLE.as_ptr()).device_info_table }.clone(); | ||
| 42 | |||
| 43 | let device_id = stm32_device_signature::device_id(); | ||
| 44 | let uid96_0 = (device_id[3] as u32) << 24 | ||
| 45 | | (device_id[2] as u32) << 16 | ||
| 46 | | (device_id[1] as u32) << 8 | ||
| 47 | | device_id[0] as u32; | ||
| 48 | let uid96_1 = (device_id[7] as u32) << 24 | ||
| 49 | | (device_id[6] as u32) << 16 | ||
| 50 | | (device_id[5] as u32) << 8 | ||
| 51 | | device_id[4] as u32; | ||
| 52 | let uid96_2 = (device_id[11] as u32) << 24 | ||
| 53 | | (device_id[10] as u32) << 16 | ||
| 54 | | (device_id[9] as u32) << 8 | ||
| 55 | | device_id[8] as u32; | ||
| 56 | |||
| 57 | let package_type = unsafe { *PACKAGE_DATA_PTR }; | ||
| 58 | let uid64 = unsafe { *UID64_PTR }; | ||
| 59 | let st_company_id = unsafe { *UID64_PTR.offset(1) } >> 8 & 0x00FF_FFFF; | ||
| 60 | let device_type_id = (unsafe { *UID64_PTR.offset(1) } & 0x000000FF) as u8; | ||
| 61 | |||
| 62 | LhciC1DeviceInformationCcrp { | ||
| 63 | status: 0, | ||
| 64 | rev_id: 0, | ||
| 65 | dev_code_id: 0, | ||
| 66 | package_type, | ||
| 67 | device_type_id, | ||
| 68 | st_company_id, | ||
| 69 | uid64, | ||
| 70 | uid96_0, | ||
| 71 | uid96_1, | ||
| 72 | uid96_2, | ||
| 73 | safe_boot_info_table, | ||
| 74 | rss_info_table, | ||
| 75 | wireless_fw_info_table, | ||
| 76 | app_fw_inf: (1 << 8), // 0.0.1 | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | impl LhciC1DeviceInformationCcrp { | ||
| 82 | pub fn new() -> Self { | ||
| 83 | Self::default() | ||
| 84 | } | ||
| 85 | |||
| 86 | pub fn write(&self, cmd_packet: &mut CmdPacket) { | ||
| 87 | let self_size = core::mem::size_of::<LhciC1DeviceInformationCcrp>(); | ||
| 88 | |||
| 89 | unsafe { | ||
| 90 | let cmd_packet_ptr: *mut CmdPacket = cmd_packet; | ||
| 91 | let evet_packet_ptr: *mut EvtPacket = cmd_packet_ptr.cast(); | ||
| 92 | |||
| 93 | let evt_serial: *mut EvtSerial = &mut (*evet_packet_ptr).evt_serial; | ||
| 94 | let evt_payload = (*evt_serial).evt.payload.as_mut_ptr(); | ||
| 95 | let evt_cc: *mut CcEvt = evt_payload.cast(); | ||
| 96 | let evt_cc_payload_buf = (*evt_cc).payload.as_mut_ptr(); | ||
| 97 | |||
| 98 | (*evt_serial).kind = TlPacketType::LocRsp as u8; | ||
| 99 | (*evt_serial).evt.evt_code = TL_BLEEVT_CC_OPCODE; | ||
| 100 | (*evt_serial).evt.payload_len = TL_EVT_HEADER_SIZE as u8 + self_size as u8; | ||
| 101 | |||
| 102 | (*evt_cc).cmd_code = LHCI_OPCODE_C1_DEVICE_INF; | ||
| 103 | (*evt_cc).num_cmd = 1; | ||
| 104 | |||
| 105 | let self_ptr: *const LhciC1DeviceInformationCcrp = self; | ||
| 106 | let self_buf = self_ptr.cast(); | ||
| 107 | |||
| 108 | core::ptr::copy(self_buf, evt_cc_payload_buf, self_size); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
diff --git a/embassy-stm32-wpan/src/lib.rs b/embassy-stm32-wpan/src/lib.rs index 5aec9933c..bf0f0466e 100644 --- a/embassy-stm32-wpan/src/lib.rs +++ b/embassy-stm32-wpan/src/lib.rs | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | #![no_std] | 1 | #![no_std] |
| 2 | #![cfg_attr(feature = "ble", feature(async_fn_in_trait))] | ||
| 2 | 3 | ||
| 3 | // This must go FIRST so that all the other modules see its macros. | 4 | // This must go FIRST so that all the other modules see its macros. |
| 4 | pub mod fmt; | 5 | pub mod fmt; |
| @@ -21,6 +22,7 @@ pub mod channels; | |||
| 21 | pub mod cmd; | 22 | pub mod cmd; |
| 22 | pub mod consts; | 23 | pub mod consts; |
| 23 | pub mod evt; | 24 | pub mod evt; |
| 25 | pub mod lhci; | ||
| 24 | #[cfg(feature = "mac")] | 26 | #[cfg(feature = "mac")] |
| 25 | pub mod mac; | 27 | pub mod mac; |
| 26 | pub mod mm; | 28 | pub mod mm; |
diff --git a/examples/stm32wb/.cargo/config.toml b/examples/stm32wb/.cargo/config.toml index d23fdc513..35317a297 100644 --- a/examples/stm32wb/.cargo/config.toml +++ b/examples/stm32wb/.cargo/config.toml | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] |
| 2 | # replace STM32WB55CCUx with your chip as listed in `probe-rs-cli chip list` | 2 | # replace STM32WB55CCUx with your chip as listed in `probe-rs-cli chip list` |
| 3 | # runner = "probe-rs-cli run --chip STM32WB55CCUx --speed 1000 --connect-under-reset" | 3 | # runner = "probe-rs-cli run --chip STM32WB55RGVx --speed 1000 --connect-under-reset" |
| 4 | runner = "teleprobe local run --chip STM32WB55RG --elf" | 4 | runner = "teleprobe local run --chip STM32WB55RG --elf" |
| 5 | 5 | ||
| 6 | [build] | 6 | [build] |
diff --git a/examples/stm32wb/Cargo.toml b/examples/stm32wb/Cargo.toml index e41424aad..726cd10d4 100644 --- a/examples/stm32wb/Cargo.toml +++ b/examples/stm32wb/Cargo.toml | |||
| @@ -33,4 +33,8 @@ required-features = ["ble"] | |||
| 33 | 33 | ||
| 34 | [[bin]] | 34 | [[bin]] |
| 35 | name = "tl_mbox_mac" | 35 | name = "tl_mbox_mac" |
| 36 | required-features = ["mac"] \ No newline at end of file | 36 | required-features = ["mac"] |
| 37 | |||
| 38 | [[bin]] | ||
| 39 | name = "eddystone_beacon" | ||
| 40 | required-features = ["ble"] \ No newline at end of file | ||
diff --git a/examples/stm32wb/src/bin/eddystone_beacon.rs b/examples/stm32wb/src/bin/eddystone_beacon.rs new file mode 100644 index 000000000..fdd5be4a2 --- /dev/null +++ b/examples/stm32wb/src/bin/eddystone_beacon.rs | |||
| @@ -0,0 +1,249 @@ | |||
| 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::ble::hci::host::uart::UartHci; | ||
| 12 | use embassy_stm32_wpan::ble::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType}; | ||
| 13 | use embassy_stm32_wpan::ble::hci::types::AdvertisingType; | ||
| 14 | use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::gap::{ | ||
| 15 | AdvertisingDataType, DiscoverableParameters, GapCommands, Role, | ||
| 16 | }; | ||
| 17 | use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::gatt::GattCommands; | ||
| 18 | use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel}; | ||
| 19 | use embassy_stm32_wpan::ble::hci::BdAddr; | ||
| 20 | use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp; | ||
| 21 | use embassy_stm32_wpan::TlMbox; | ||
| 22 | use {defmt_rtt as _, panic_probe as _}; | ||
| 23 | |||
| 24 | bind_interrupts!(struct Irqs{ | ||
| 25 | IPCC_C1_RX => ReceiveInterruptHandler; | ||
| 26 | IPCC_C1_TX => TransmitInterruptHandler; | ||
| 27 | }); | ||
| 28 | |||
| 29 | const BLE_GAP_DEVICE_NAME_LENGTH: u8 = 7; | ||
| 30 | |||
| 31 | #[embassy_executor::main] | ||
| 32 | async fn main(_spawner: Spawner) { | ||
| 33 | /* | ||
| 34 | How to make this work: | ||
| 35 | |||
| 36 | - Obtain a NUCLEO-STM32WB55 from your preferred supplier. | ||
| 37 | - Download and Install STM32CubeProgrammer. | ||
| 38 | - Download stm32wb5x_FUS_fw.bin, stm32wb5x_BLE_Stack_full_fw.bin, and Release_Notes.html from | ||
| 39 | gh:STMicroelectronics/STM32CubeWB@2234d97/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x | ||
| 40 | - Open STM32CubeProgrammer | ||
| 41 | - On the right-hand pane, click "firmware upgrade" to upgrade the st-link firmware. | ||
| 42 | - Once complete, click connect to connect to the device. | ||
| 43 | - On the left hand pane, click the RSS signal icon to open "Firmware Upgrade Services". | ||
| 44 | - In the Release_Notes.html, find the memory address that corresponds to your device for the stm32wb5x_FUS_fw.bin file | ||
| 45 | - Select that file, the memory address, "verify download", and then "Firmware Upgrade". | ||
| 46 | - Once complete, in the Release_Notes.html, find the memory address that corresponds to your device for the | ||
| 47 | stm32wb5x_BLE_Stack_full_fw.bin file. It should not be the same memory address. | ||
| 48 | - Select that file, the memory address, "verify download", and then "Firmware Upgrade". | ||
| 49 | - Select "Start Wireless Stack". | ||
| 50 | - Disconnect from the device. | ||
| 51 | - In the examples folder for stm32wb, modify the memory.x file to match your target device. | ||
| 52 | - Run this example. | ||
| 53 | |||
| 54 | Note: extended stack versions are not supported at this time. Do not attempt to install a stack with "extended" in the name. | ||
| 55 | */ | ||
| 56 | |||
| 57 | let p = embassy_stm32::init(Default::default()); | ||
| 58 | info!("Hello World!"); | ||
| 59 | |||
| 60 | let config = Config::default(); | ||
| 61 | let mut mbox = TlMbox::init(p.IPCC, Irqs, config); | ||
| 62 | |||
| 63 | let sys_event = mbox.sys_subsystem.read().await; | ||
| 64 | info!("sys event: {}", sys_event.payload()); | ||
| 65 | |||
| 66 | mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await; | ||
| 67 | |||
| 68 | info!("resetting BLE..."); | ||
| 69 | mbox.ble_subsystem.reset().await; | ||
| 70 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 71 | defmt::info!("{}", response); | ||
| 72 | |||
| 73 | info!("config public address..."); | ||
| 74 | mbox.ble_subsystem | ||
| 75 | .write_config_data(&ConfigData::public_address(get_bd_addr()).build()) | ||
| 76 | .await; | ||
| 77 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 78 | defmt::info!("{}", response); | ||
| 79 | |||
| 80 | info!("config random address..."); | ||
| 81 | mbox.ble_subsystem | ||
| 82 | .write_config_data(&ConfigData::random_address(get_random_addr()).build()) | ||
| 83 | .await; | ||
| 84 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 85 | defmt::info!("{}", response); | ||
| 86 | |||
| 87 | info!("config identity root..."); | ||
| 88 | mbox.ble_subsystem | ||
| 89 | .write_config_data(&ConfigData::identity_root(&get_irk()).build()) | ||
| 90 | .await; | ||
| 91 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 92 | defmt::info!("{}", response); | ||
| 93 | |||
| 94 | info!("config encryption root..."); | ||
| 95 | mbox.ble_subsystem | ||
| 96 | .write_config_data(&ConfigData::encryption_root(&get_erk()).build()) | ||
| 97 | .await; | ||
| 98 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 99 | defmt::info!("{}", response); | ||
| 100 | |||
| 101 | info!("config tx power level..."); | ||
| 102 | mbox.ble_subsystem.set_tx_power_level(PowerLevel::ZerodBm).await; | ||
| 103 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 104 | defmt::info!("{}", response); | ||
| 105 | |||
| 106 | info!("GATT init..."); | ||
| 107 | mbox.ble_subsystem.init_gatt().await; | ||
| 108 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 109 | defmt::info!("{}", response); | ||
| 110 | |||
| 111 | info!("GAP init..."); | ||
| 112 | mbox.ble_subsystem | ||
| 113 | .init_gap(Role::PERIPHERAL, false, BLE_GAP_DEVICE_NAME_LENGTH) | ||
| 114 | .await; | ||
| 115 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 116 | defmt::info!("{}", response); | ||
| 117 | |||
| 118 | // info!("set scan response..."); | ||
| 119 | // mbox.ble_subsystem.le_set_scan_response_data(&[]).await.unwrap(); | ||
| 120 | // let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 121 | // defmt::info!("{}", response); | ||
| 122 | |||
| 123 | info!("set discoverable..."); | ||
| 124 | mbox.ble_subsystem | ||
| 125 | .set_discoverable(&DiscoverableParameters { | ||
| 126 | advertising_type: AdvertisingType::NonConnectableUndirected, | ||
| 127 | advertising_interval: Some((Duration::from_millis(250), Duration::from_millis(250))), | ||
| 128 | address_type: OwnAddressType::Public, | ||
| 129 | filter_policy: AdvertisingFilterPolicy::AllowConnectionAndScan, | ||
| 130 | local_name: None, | ||
| 131 | advertising_data: &[], | ||
| 132 | conn_interval: (None, None), | ||
| 133 | }) | ||
| 134 | .await | ||
| 135 | .unwrap(); | ||
| 136 | |||
| 137 | let response = mbox.ble_subsystem.read().await; | ||
| 138 | defmt::info!("{}", response); | ||
| 139 | |||
| 140 | // remove some advertisement to decrease the packet size | ||
| 141 | info!("delete tx power ad type..."); | ||
| 142 | mbox.ble_subsystem | ||
| 143 | .delete_ad_type(AdvertisingDataType::TxPowerLevel) | ||
| 144 | .await; | ||
| 145 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 146 | defmt::info!("{}", response); | ||
| 147 | |||
| 148 | info!("delete conn interval ad type..."); | ||
| 149 | mbox.ble_subsystem | ||
| 150 | .delete_ad_type(AdvertisingDataType::PeripheralConnectionInterval) | ||
| 151 | .await; | ||
| 152 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 153 | defmt::info!("{}", response); | ||
| 154 | |||
| 155 | info!("update advertising data..."); | ||
| 156 | mbox.ble_subsystem | ||
| 157 | .update_advertising_data(&eddystone_advertising_data()) | ||
| 158 | .await | ||
| 159 | .unwrap(); | ||
| 160 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 161 | defmt::info!("{}", response); | ||
| 162 | |||
| 163 | info!("update advertising data type..."); | ||
| 164 | mbox.ble_subsystem | ||
| 165 | .update_advertising_data(&[3, AdvertisingDataType::UuidCompleteList16 as u8, 0xaa, 0xfe]) | ||
| 166 | .await | ||
| 167 | .unwrap(); | ||
| 168 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 169 | defmt::info!("{}", response); | ||
| 170 | |||
| 171 | info!("update advertising data flags..."); | ||
| 172 | mbox.ble_subsystem | ||
| 173 | .update_advertising_data(&[ | ||
| 174 | 2, | ||
| 175 | AdvertisingDataType::Flags as u8, | ||
| 176 | (0x02 | 0x04) as u8, // BLE general discoverable, without BR/EDR support | ||
| 177 | ]) | ||
| 178 | .await | ||
| 179 | .unwrap(); | ||
| 180 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 181 | defmt::info!("{}", response); | ||
| 182 | |||
| 183 | cortex_m::asm::wfi(); | ||
| 184 | } | ||
| 185 | |||
| 186 | fn get_bd_addr() -> BdAddr { | ||
| 187 | let mut bytes = [0u8; 6]; | ||
| 188 | |||
| 189 | let lhci_info = LhciC1DeviceInformationCcrp::new(); | ||
| 190 | bytes[0] = (lhci_info.uid64 & 0xff) as u8; | ||
| 191 | bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8; | ||
| 192 | bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8; | ||
| 193 | bytes[3] = lhci_info.device_type_id; | ||
| 194 | bytes[4] = (lhci_info.st_company_id & 0xff) as u8; | ||
| 195 | bytes[5] = (lhci_info.st_company_id >> 8 & 0xff) as u8; | ||
| 196 | |||
| 197 | BdAddr(bytes) | ||
| 198 | } | ||
| 199 | |||
| 200 | fn get_random_addr() -> BdAddr { | ||
| 201 | let mut bytes = [0u8; 6]; | ||
| 202 | |||
| 203 | let lhci_info = LhciC1DeviceInformationCcrp::new(); | ||
| 204 | bytes[0] = (lhci_info.uid64 & 0xff) as u8; | ||
| 205 | bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8; | ||
| 206 | bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8; | ||
| 207 | bytes[3] = 0; | ||
| 208 | bytes[4] = 0x6E; | ||
| 209 | bytes[5] = 0xED; | ||
| 210 | |||
| 211 | BdAddr(bytes) | ||
| 212 | } | ||
| 213 | |||
| 214 | const BLE_CFG_IRK: [u8; 16] = [ | ||
| 215 | 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, | ||
| 216 | ]; | ||
| 217 | const BLE_CFG_ERK: [u8; 16] = [ | ||
| 218 | 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, | ||
| 219 | ]; | ||
| 220 | |||
| 221 | fn get_irk() -> EncryptionKey { | ||
| 222 | EncryptionKey(BLE_CFG_IRK) | ||
| 223 | } | ||
| 224 | |||
| 225 | fn get_erk() -> EncryptionKey { | ||
| 226 | EncryptionKey(BLE_CFG_ERK) | ||
| 227 | } | ||
| 228 | |||
| 229 | fn eddystone_advertising_data() -> [u8; 24] { | ||
| 230 | const EDDYSTONE_URL: &[u8] = b"www.rust-lang.com"; | ||
| 231 | |||
| 232 | let mut service_data = [0u8; 24]; | ||
| 233 | let url_len = EDDYSTONE_URL.len(); | ||
| 234 | |||
| 235 | service_data[0] = 6 + url_len as u8; | ||
| 236 | service_data[1] = AdvertisingDataType::ServiceData as u8; | ||
| 237 | |||
| 238 | // 16-bit eddystone uuid | ||
| 239 | service_data[2] = 0xaa; | ||
| 240 | service_data[3] = 0xFE; | ||
| 241 | |||
| 242 | service_data[4] = 0x10; // URL frame type | ||
| 243 | service_data[5] = 22_i8 as u8; // calibrated TX power at 0m | ||
| 244 | service_data[6] = 0x03; // eddystone url prefix = https | ||
| 245 | |||
| 246 | service_data[7..(7 + url_len)].copy_from_slice(EDDYSTONE_URL); | ||
| 247 | |||
| 248 | service_data | ||
| 249 | } | ||
