Skip to content

Commit

Permalink
chore: optimized device discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhman-sukh committed Aug 17, 2024
1 parent c07181b commit 34056d4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 41 deletions.
77 changes: 45 additions & 32 deletions rust/ble_module/src/ble/ble_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ impl IdleBleService {
control_handle: msg_chara_handle,
..Default::default()
};
// let adp = self.adapter.clone();
// let cmd_tx2 = cmd_tx.clone();
let adp = self.adapter.clone();
let cmd_tx2 = cmd_tx.clone();
let main_characterstic = Characteristic {
uuid: read_char(),
read: Some(CharacteristicRead {
Expand All @@ -167,29 +167,42 @@ impl IdleBleService {
);
match utils::find_ignore_device_by_mac(req.device_address) {
Some(_) => {
utils::update_last_found(req.device_address);
// utils::update_last_found(req.device_address);
}
None => {
// let adp2 = adp.clone();
// let cmd_tx2 = cmd_tx2.clone();
// async_std::task::spawn(async move {
// match adp2.device(req.device_address) {
// Ok(device) => {
// match cmd_tx2.send(BleMainLoopEvent::DeviceDiscovered(device)).await {
// Ok(_) => {
// log::info!("Device discovered event sent");
// }
// Err(err) => {
// log::error!("Error sending device discovered event: {:#?}", err);
// }
// };
// // self.on_device_discovered( &device, &mut internal_sender);
// },
// Err(e) => {
// log::error!("Error: {:#?}", e);
// }
// };
// });
let adp2 = adp.clone();
let cmd_tx2 = cmd_tx2.clone();
async_std::task::spawn(async move {
match adp2.device(req.device_address) {
Ok(device) => {
match cmd_tx2.send(BleMainLoopEvent::DeviceDiscovered(device.clone())).await {
Ok(_) => {

// Add a null ble_device to stop event from being sent again
// Will be update in func: on_device_discovered.
let ble_device = utils::BleScanDevice {
qaul_id: vec![],
rssi: 0,
mac_address: device.address(),
name: device.name().await.unwrap().unwrap_or_default(),
device: device,
last_found_time: utils::current_time_millis(),
is_connected: false,
};
utils::add_ignore_device(ble_device);
log::info!("Device discovered event sent");
}
Err(err) => {
log::error!("Error sending device discovered event: {:#?}", err);
}
};
// self.on_device_discovered( &device, &mut internal_sender);
},
Err(e) => {
log::error!("Error: {:#?}", e);
}
};
});
}
}
let value = qaul_id.clone();
Expand Down Expand Up @@ -595,9 +608,9 @@ impl IdleBleService {
let mut read_char_uuid_found = false;
let mut msg_receivers: Vec<CharacteristicReader> = vec![];
let stringified_addr = utils::mac_to_string(&device.address());
let device_name = device.name().await.unwrap().unwrap_or_default();
let device_name = device.name().await.unwrap_or_default().unwrap_or_default();
if !(device_name.len() == 5 || device_name == "qaul") {
return Err("Not a Qaul device".into());
return Err("Not a Qaul device".into());
}
log::info!(
"Discovered device {} with name {:?}",
Expand Down Expand Up @@ -772,19 +785,19 @@ impl IdleBleService {
let mac_address = write.device_address();
match find_device_by_mac(mac_address) {
Some(_) => {
utils::update_last_found(mac_address);
// utils::update_last_found(mac_address);
}
None => {
device_known = false;
log::warn!("Device not found in known devices");
// // match self.adapter.device(mac_address) {
// match self.adapter.device(mac_address) {
// Ok(device) => {
// // self.
// self.
// self.on_device_discovered(&device, &mut internal_sender.clone()).await.unwrap();
// },
// Err(_) => {
// // log::error!("Error:");
// }
// },
// Err(_) => {
// // log::error!("Error:");
// }
// };

}
Expand Down
24 changes: 15 additions & 9 deletions rust/ble_module/src/ble/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ pub struct BleScanDevice {
pub is_connected: bool,
}

impl BleScanDevice {
pub fn update_last_found(&mut self) {
self.last_found_time = current_time_millis();
}
}

#[derive(Serialize, Deserialize)]
pub struct Message {
#[serde(rename = "qaul_id")]
Expand Down Expand Up @@ -60,7 +54,11 @@ mod tests {
/// Add a new device to list of all previously discovered devices.
pub fn add_device(device: BleScanDevice) {
let mut devices = DEVICE_LIST.lock().unwrap();
devices.insert(device.mac_address, device);
if devices.contains_key(&device.mac_address) {
devices.get_mut(&device.mac_address).map(|val| { *val = device; });
}else {
devices.insert(device.mac_address, device);
}
}

/// Key value lookup for previously discovered.
Expand Down Expand Up @@ -90,7 +88,11 @@ pub fn remove_device_by_mac(mac_address: Address) {
/// Add a new message to the message map maintained by ble listner.
pub fn add_msg_map(stringified_addr: String, hex_msg: String) {
let mut msg_map = MSG_MAP.lock().unwrap();
msg_map.insert(stringified_addr, hex_msg);
if msg_map.contains_key(stringified_addr.as_str()) {
msg_map.get_mut(&stringified_addr).map(|val| { *val = hex_msg; });
}else {
msg_map.insert(stringified_addr, hex_msg);
}
}

/// Key value lookup for message map maintained by ble listner.
Expand All @@ -108,7 +110,11 @@ pub fn remove_msg_map_by_mac(stringified_addr: String) {
/// Add a new device to list of devices present nearby and maintain their last found time.
pub fn add_ignore_device(device: BleScanDevice) {
let mut ignore_devices = IGNORE_LIST.lock().unwrap();
ignore_devices.insert(device.mac_address, device);
if ignore_devices.contains_key(&device.mac_address) {
ignore_devices.get_mut(&device.mac_address).map(|val| { *val = device; });
}else {
ignore_devices.insert(device.mac_address, device);
}
}

/// Key value lookup for devices present nearby.
Expand Down

0 comments on commit 34056d4

Please sign in to comment.