Skip to content

fix: corebluetooth fulfill characteristics futures on peripheral disconnection #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2024
Merged
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
36 changes: 32 additions & 4 deletions src/corebluetooth/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,32 @@ impl PeripheralInternal {
if let Some(future) = self.disconnected_future_state.take() {
future.lock().unwrap().set_reply(CoreBluetoothReply::Ok)
}

// Fulfill all pending futures
let error = CoreBluetoothReply::Err(String::from("Device disconnected"));
self.services.iter().for_each(|(_, service)| {
service
.characteristics
.iter()
.for_each(|(_, characteristic)| {
let CharacteristicInternal {
read_future_state,
write_future_state,
subscribe_future_state,
unsubscribe_future_state,
..
} = characteristic;

let futures = read_future_state
.into_iter()
.chain(write_future_state.into_iter())
.chain(subscribe_future_state.into_iter())
.chain(unsubscribe_future_state.into_iter());
for state in futures {
state.lock().unwrap().set_reply(error.clone());
}
});
});
}
}

Expand Down Expand Up @@ -734,8 +760,9 @@ impl CoreBluetoothInternal {
self.get_characteristic(peripheral_uuid, service_uuid, characteristic_uuid)
{
trace!("Got subscribed event!");
let state = characteristic.subscribe_future_state.pop_back().unwrap();
state.lock().unwrap().set_reply(CoreBluetoothReply::Ok);
if let Some(state) = characteristic.subscribe_future_state.pop_back() {
state.lock().unwrap().set_reply(CoreBluetoothReply::Ok);
}
}
}

Expand All @@ -749,8 +776,9 @@ impl CoreBluetoothInternal {
self.get_characteristic(peripheral_uuid, service_uuid, characteristic_uuid)
{
trace!("Got unsubscribed event!");
let state = characteristic.unsubscribe_future_state.pop_back().unwrap();
state.lock().unwrap().set_reply(CoreBluetoothReply::Ok);
if let Some(state) = characteristic.unsubscribe_future_state.pop_back() {
state.lock().unwrap().set_reply(CoreBluetoothReply::Ok);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/corebluetooth/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ impl api::Peripheral for Peripheral {
.await?;
match fut.await {
CoreBluetoothReply::Ok => {}
CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)),
reply => panic!("Unexpected reply: {:?}", reply),
}
Ok(())
Expand All @@ -333,6 +334,7 @@ impl api::Peripheral for Peripheral {
.await?;
match fut.await {
CoreBluetoothReply::ReadResult(chars) => Ok(chars),
CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)),
_ => {
panic!("Shouldn't get anything but read result!");
}
Expand All @@ -353,6 +355,7 @@ impl api::Peripheral for Peripheral {
.await?;
match fut.await {
CoreBluetoothReply::Ok => trace!("subscribed!"),
CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)),
_ => panic!("Didn't subscribe!"),
}
Ok(())
Expand All @@ -372,6 +375,7 @@ impl api::Peripheral for Peripheral {
.await?;
match fut.await {
CoreBluetoothReply::Ok => {}
CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)),
_ => panic!("Didn't unsubscribe!"),
}
Ok(())
Expand Down
Loading