Skip to content

Commit

Permalink
fix(ble): do not save bond info of same device
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 6537ada commit b2237f4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
26 changes: 22 additions & 4 deletions rmk/src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ use embedded_storage::nor_flash::NorFlash;
use nrf_softdevice::{raw, Config, Flash};
use sequential_storage::{
cache::NoCache,
map::{remove_item, store_item},
map::{fetch_item, 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;
pub const BONDED_DEVICE_NUM: usize = 8;

/// Create default nrf ble config
pub fn nrf_ble_config(keyboard_name: &str) -> Config {
Expand Down Expand Up @@ -91,8 +90,27 @@ pub(crate) async fn flash_task(f: &'static mut Flash) -> ! {
.await
.unwrap();
}
FlashOperationMessage::BondInfo(b) => {
FlashOperationMessage::BondInfo(mut b) => {
info!("Saving item: {}", info);
for key in 0..BONDED_DEVICE_NUM {
if let Ok(Some(info)) = fetch_item::<BondInfo, _>(
f,
CONFIG_FLASH_RANGE,
NoCache::new(),
&mut storage_data_buffer,
key as u8,
)
.await
{
// The device has been stored in flash, update
if b.peer.peer_id.addr == info.peer.peer_id.addr {
info!("Peer exists in flash, replace it");
b.slot_num = info.slot_num;
break;
}
}
}

store_item::<BondInfo, _>(
f,
CONFIG_FLASH_RANGE,
Expand Down
4 changes: 2 additions & 2 deletions rmk/src/ble/bonder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub(crate) static FLASH_CHANNEL: Channel<ThreadModeRawMutex, FlashOperationMessa
// Bond info which will be stored in flash
#[derive(Clone, Copy, Debug, Format)]
pub(crate) struct BondInfo {
slot_num: u8,
peer: Peer,
pub(crate) slot_num: u8,
pub(crate) peer: Peer,
sys_attr: SystemAttribute,
}

Expand Down
30 changes: 26 additions & 4 deletions rmk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ pub async fn initialize_ble_keyboard_with_config_and_run<
use defmt::*;
use embassy_futures::select::{select, Either};
use nrf_softdevice::Softdevice;
use sequential_storage::{cache::NoCache, map::fetch_item};
use sequential_storage::{
cache::NoCache,
map::{fetch_item, remove_item},
};
use static_cell::StaticCell;
// FIXME: add auto recognition of ble/usb
use crate::ble::{
Expand Down Expand Up @@ -280,9 +283,28 @@ pub async fn initialize_ble_keyboard_with_config_and_run<
fetch_item::<BondInfo, _>(f, CONFIG_FLASH_RANGE, NoCache::new(), &mut buf, key as u8)
.await
{
match bond_info.push(info) {
Ok(_) => (),
Err(_) => error!("Add bond info error"),
// Iterate through bond_info, remove same devices by comparing peer address
if bond_info
.iter()
.filter(|i| i.peer.peer_id.addr == info.peer.peer_id.addr)
.count()
> 0
{
info!("Peer exists, removing current");
remove_item::<BondInfo, _>(
f,
CONFIG_FLASH_RANGE,
NoCache::new(),
&mut buf,
key as u8,
)
.await
.unwrap();
} else {
match bond_info.push(info) {
Ok(_) => (),
Err(_) => error!("Add bond info error"),
}
}
}
}
Expand Down

0 comments on commit b2237f4

Please sign in to comment.