work in progress on switching to direct mode

This commit is contained in:
2024-08-24 20:53:20 -04:00
parent a53584ab82
commit 802285d0ec
5 changed files with 99 additions and 8 deletions

View File

@ -34,6 +34,9 @@ impl BleServer {
Commands::NotifyMotorDown { data: 0 },
Commands::NotifyMotorStop { data: 0 },
Commands::NotifyMotorUp { data: 0 },
Commands::PairTimerExpired,
Commands::AllowPairing,
Commands::EraseBleBonds,
];
let r = dp.get_callback_channel(&cmds);
let s = dp.get_cmd_channel();
@ -55,7 +58,7 @@ impl BleServer {
let ble_device = BLEDevice::take();
set_device_security(ble_device);
let server = ble_device.get_server();
set_server_callbacks(server);
set_server_callbacks(server, self.send_q.clone());
let _pairing_service = server.create_service(UUID_SERVICE_PAIR);
let lift_service = server.create_service(UUID_SERVICE_LIFT);
@ -94,7 +97,7 @@ impl BleServer {
let ble_advertiser = ble_device.get_advertising();
// TODO: we will need to enable / disable the ability to pair!
advertise_pairing(ble_advertiser)?;
advertise(ble_advertiser)?;
loop {
debug!("Waiting for updates that should be notified via bluetooth");
@ -111,6 +114,16 @@ impl BleServer {
Commands::NotifyMotorStop{data} => {
button_up.lock().set_value(&[data]).notify();
}
Commands::PairTimerExpired => {
advertise_to_direct(ble_advertiser).expect("Failed to exit pairing mode");
self.send_q.send(Commands::PairTimerClear).await?;
}
Commands::AllowPairing => {
advertise_to_undirected(ble_advertiser).expect("Failed to enter pairing mode");
}
Commands::EraseBleBonds => {
ble_device.delete_all_bonds().expect("Failed trying to erase bluetooth bonding information");
}
_ => {
error!("Invalid command received by bluetooth handler {:?}", cmd);
// No need to reboot as state is recoverable.
@ -150,7 +163,7 @@ fn on_bluetooth_cmd(sender: &SendQ, args: &mut OnWriteArgs, cmd: Commands) {
fn set_device_security(dev: &mut BLEDevice) {
dev.security()
// Enable all security protections
// Enable all security protections (including bond, so that bond info is saved)
.set_auth(AuthReq::all())
// Options we support for putting in pairing info.
// "NoInputOutput" means that we will have "just works" pairing
@ -159,8 +172,8 @@ fn set_device_security(dev: &mut BLEDevice) {
.resolve_rpa();
}
fn set_server_callbacks(server: &mut BLEServer) {
server.on_connect(|server, clntdesc| {
fn set_server_callbacks(server: &mut BLEServer, sender: SendQ) {
server.on_connect(move |server, clntdesc| {
// Print connected client data
info!("client connected: {:?}", clntdesc);
// Update connection parameters
@ -172,24 +185,40 @@ fn set_server_callbacks(server: &mut BLEServer) {
BLE_LATENCY,
BLE_TIMEOUT,
).unwrap();
sender.send_blocking(Commands::PairTimerClear).unwrap();
// TODO: Cancel pairing mode timeout
});
server.on_disconnect(|_desc, _reason| {
info!("Disconnected, back to advertising");
});
}
fn advertise_pairing(advertiser: &Mutex<BLEAdvertising>) -> Result<()> {
fn advertise(advertiser: &Mutex<BLEAdvertising>) -> Result<()> {
trace!("Setting up advertiser");
advertiser
.lock()
.advertisement_type(ConnMode::Und)
.set_data(
BLEAdvertisementData::new()
.name(DEVICE_NAME)
.add_service_uuid(UUID_SERVICE_PAIR)
)?;
// TODO: this appears to run in its own thread; verify.
// TODO: isn't there a restart? We'll need to switch between pairing and not.
info!("Staring Bluetooth Server");
advertiser.lock().start()?;
Ok(())
}
fn advertise_to_direct(advertiser: &Mutex<BLEAdvertising>) -> Result<()> {
advertiser.lock().stop()?;
advertiser.lock().advertisement_type(ConnMode::Dir).start()?;
Ok(())
}
fn advertise_to_undirected(advertiser: &Mutex<BLEAdvertising>) -> Result<()> {
advertiser.lock().stop()?;
advertiser.lock().advertisement_type(ConnMode::Und).start()?;
Ok(())
}
//TODO set maximum pairs to remember?
//TODO after disconnect, it returns to scanning - will it return to directed scanning? Find out when directed is working.