Skip to content

Commit

Permalink
refactor(ble): clean ble code
Browse files Browse the repository at this point in the history
Signed-off-by: Haobo Gu <[email protected]>
  • Loading branch information
HaoboGu committed Mar 7, 2024
1 parent 5607e1b commit 6537ada
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
10 changes: 6 additions & 4 deletions rmk/src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = 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 {
Expand Down Expand Up @@ -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<u32> = 0x80000..0x82000;

#[embassy_executor::task]
pub(crate) async fn flash_task(f: &'static mut Flash) -> ! {
let mut storage_data_buffer = [0_u8; 128];
Expand Down
8 changes: 4 additions & 4 deletions rmk/src/ble/bonder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ThreadModeRawMutex, FlashOperationMessage, 2> = Channel::new();
pub(crate) static FLASH_CHANNEL: Channel<ThreadModeRawMutex, FlashOperationMessage, 2> =
Channel::new();

// Bond info which will be stored in flash
#[derive(Clone, Copy, Debug, Format)]
Expand Down Expand Up @@ -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()))
Expand Down
3 changes: 0 additions & 3 deletions rmk/src/ble/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions rmk/src/ble/hid_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions rmk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit 6537ada

Please sign in to comment.