Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions server/src/bonding_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,35 @@ use bluer::{
agent::{Agent, ReqResult, RequestConfirmation},
Address, Session,
};
use log::info;
use log::{info, warn};
use tokio::{sync::mpsc, time::timeout};

const BT_NAME_PREFIX: &str = "cedar-";

pub struct BluetoothDevice {
pub name: String,
pub address: String,
}

pub async fn get_adapter_alias() -> Result<String, Box<dyn Error + 'static>> {
pub async fn get_adapter_alias(
serial: &str,
) -> Result<String, Box<dyn Error + 'static>> {
let session = Session::new().await?;
let adapter = session.default_adapter().await?;
let alias = adapter.alias().await?;
let mut alias = adapter.alias().await?;
let expected_alias = generate_bluetooth_name(serial);
if alias != expected_alias {
info!("Updating Bluetooth alias");
match adapter.set_alias(expected_alias.clone()).await {
Ok(_) => {
alias = expected_alias.to_string();
}
Err(e) => {
warn!("Unable to update alias: {:?}", e);
}
}
}

info!("Current device alias: {}", alias);
Ok(alias)
}
Expand Down Expand Up @@ -114,4 +131,12 @@ pub async fn get_bonded_devices(
});
}
Ok(result)
}
}

fn generate_bluetooth_name(serial: &str) -> String {
if serial.len() < 3 {
warn!("Unexpected length for serial number: {}", serial);
return "cedar".to_string();
}
format!("{}{}", BT_NAME_PREFIX, &serial[serial.len() - 3..])
}
2 changes: 1 addition & 1 deletion server/src/cedar_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ impl Cedar for MyCedar {
&self,
_request: tonic::Request<EmptyMessage>,
) -> Result<tonic::Response<GetBluetoothNameResponse>, tonic::Status> {
let bt_name = match get_adapter_alias().await {
let bt_name = match get_adapter_alias(&self.serial_number).await {
Ok(name) => name,
Err(e) => {
warn!("Unable to get Bluetooth name: {}", e);
Expand Down