From 6537adabc938e880fbb5a967a3260caeb1d6355c Mon Sep 17 00:00:00 2001 From: Haobo Gu Date: Thu, 7 Mar 2024 20:56:07 +0800 Subject: [PATCH] refactor(ble): clean ble code Signed-off-by: Haobo Gu --- rmk/src/ble.rs | 10 ++++++---- rmk/src/ble/bonder.rs | 8 ++++---- rmk/src/ble/constants.rs | 3 --- rmk/src/ble/hid_service.rs | 8 ++++---- rmk/src/lib.rs | 8 ++++---- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/rmk/src/ble.rs b/rmk/src/ble.rs index 5f9c283e..9134f944 100644 --- a/rmk/src/ble.rs +++ b/rmk/src/ble.rs @@ -24,6 +24,12 @@ use sequential_storage::{ map::{remove_item, store_item}, }; + +/// Flash range which used to save bonding info +pub(crate) const CONFIG_FLASH_RANGE: Range = 0x80000..0x82000; +/// Maximum number of bonded devices +pub const BONDED_DEVICE_NUM: usize = 2; + /// Create default nrf ble config pub fn nrf_ble_config(keyboard_name: &str) -> Config { Config { @@ -67,10 +73,6 @@ pub(crate) async fn softdevice_task(sd: &'static nrf_softdevice::Softdevice) -> sd.run().await } -pub(crate) const FLASH_START: u32 = 0x80000; -pub(crate) const FLASH_END: u32 = 0x82000; -pub(crate) const CONFIG_FLASH_RANGE: Range = 0x80000..0x82000; - #[embassy_executor::task] pub(crate) async fn flash_task(f: &'static mut Flash) -> ! { let mut storage_data_buffer = [0_u8; 128]; diff --git a/rmk/src/ble/bonder.rs b/rmk/src/ble/bonder.rs index e3f65c55..10c8fb5d 100644 --- a/rmk/src/ble/bonder.rs +++ b/rmk/src/ble/bonder.rs @@ -10,10 +10,11 @@ use nrf_softdevice::ble::{ }; use sequential_storage::map::StorageItem; -/// Maximum number of bonded devices -pub const BONDED_DEVICE_NUM: usize = 8; +use super::BONDED_DEVICE_NUM; + // Sync messages from server to flash -pub(crate) static FLASH_CHANNEL: Channel = Channel::new(); +pub(crate) static FLASH_CHANNEL: Channel = + Channel::new(); // Bond info which will be stored in flash #[derive(Clone, Copy, Debug, Format)] @@ -232,7 +233,6 @@ impl SecurityHandler for Bonder { let bond_info = self.bond_info.borrow_mut(); - if let Some(idx) = bond_info .iter() .position(|info| info.peer.peer_id.is_match(conn.peer_address())) diff --git a/rmk/src/ble/constants.rs b/rmk/src/ble/constants.rs index 69dd81bb..e266d53c 100644 --- a/rmk/src/ble/constants.rs +++ b/rmk/src/ble/constants.rs @@ -59,9 +59,6 @@ impl BleCharacteristics { } } -pub const KEYBOARD_ID: u8 = 0x01; -pub const MEDIA_KEYS_ID: u8 = 0x02; - pub static SCAN_DATA: LegacyAdvertisementPayload = LegacyAdvertisementBuilder::new() .services_16( ServiceList::Complete, diff --git a/rmk/src/ble/hid_service.rs b/rmk/src/ble/hid_service.rs index a1ba5dfc..02750c0a 100644 --- a/rmk/src/ble/hid_service.rs +++ b/rmk/src/ble/hid_service.rs @@ -14,7 +14,7 @@ use nrf_softdevice::{ use usbd_hid::descriptor::SerializedDescriptor as _; use super::{ - constants::{BleCharacteristics, BleDescriptor, BLE_HID_SERVICE_UUID, KEYBOARD_ID}, + constants::{BleCharacteristics, BleDescriptor, BLE_HID_SERVICE_UUID}, descriptor::BleKeyboardReport, }; @@ -82,7 +82,7 @@ impl HidService { )?; let input_keyboard_desc = input_keyboard.add_descriptor( BleDescriptor::ReportReference.uuid(), - Attribute::new([KEYBOARD_ID, 1u8]).security(SecurityMode::JustWorks), // First is ID (e.g. 1 for keyboard 2 for media keys), second is in/out + Attribute::new([1u8, 1u8]).security(SecurityMode::JustWorks), // First is report ID (e.g. 1 for keyboard 2 for media keys), second is in/out )?; let input_keyboard_handle = input_keyboard.build(); @@ -93,7 +93,7 @@ impl HidService { )?; let output_keyboard_desc = output_keyboard.add_descriptor( BleDescriptor::ReportReference.uuid(), - Attribute::new([KEYBOARD_ID, 2u8]).security(SecurityMode::JustWorks), + Attribute::new([1u8, 2u8]).security(SecurityMode::JustWorks), // First is report ID (e.g. 1 for keyboard 2 for media keys), second is in/out )?; let output_keyboard_handle = output_keyboard.build(); @@ -132,7 +132,7 @@ impl HidService { } else if handle == self.output_keyboard { // Fires if a keyboard output is changed - e.g. the caps lock LED info!("HID output keyboard: {:?}", data); - // } else if handle == self.input_media_keys_cccd { + // } else if handle == self.input_media_keys_cccd { // info!("HID input media keys: {:?}", data); } } diff --git a/rmk/src/lib.rs b/rmk/src/lib.rs index ad368647..e96fa86e 100644 --- a/rmk/src/lib.rs +++ b/rmk/src/lib.rs @@ -201,13 +201,13 @@ async fn vial_task< } } -#[cfg(feature = "ble")] -use heapless::Vec; #[cfg(feature = "ble")] use action::KeyAction; #[cfg(feature = "ble")] use embassy_executor::Spawner; #[cfg(feature = "ble")] +use heapless::Vec; +#[cfg(feature = "ble")] pub use nrf_softdevice; #[cfg(feature = "ble")] /// Initialize and run the keyboard service, with given keyboard usb config. This function never returns. @@ -245,11 +245,11 @@ pub async fn initialize_ble_keyboard_with_config_and_run< // FIXME: add auto recognition of ble/usb use crate::ble::{ advertise::create_advertisement_data, - bonder::{BondInfo, Bonder, BONDED_DEVICE_NUM}, + bonder::{BondInfo, Bonder}, constants::SCAN_DATA, flash_task, server::BleServer, - CONFIG_FLASH_RANGE, + BONDED_DEVICE_NUM, CONFIG_FLASH_RANGE, }; use nrf_softdevice::{ ble::{gatt_server, peripheral},