Skip to content

Commit

Permalink
Handle failure in Peripheral::Connect by returning an error (#353)
Browse files Browse the repository at this point in the history
Co-authored-by: Ted Mielczarek <[email protected]>
  • Loading branch information
luser and tedmielczarek-fastly authored Dec 7, 2023
1 parent 41bc777 commit dc6e1a4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
16 changes: 13 additions & 3 deletions src/corebluetooth/central_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub enum CentralDelegateEvent {
},
ConnectionFailed {
peripheral_uuid: Uuid,
error_description: Option<String>,
},
DisconnectedDevice {
peripheral_uuid: Uuid,
Expand Down Expand Up @@ -176,9 +177,13 @@ impl Debug for CentralDelegateEvent {
.debug_struct("ConnectedDevice")
.field("peripheral_uuid", peripheral_uuid)
.finish(),
CentralDelegateEvent::ConnectionFailed { peripheral_uuid } => f
CentralDelegateEvent::ConnectionFailed {
peripheral_uuid,
error_description,
} => f
.debug_struct("ConnectionFailed")
.field("peripheral_uuid", peripheral_uuid)
.field("error_description", error_description)
.finish(),
CentralDelegateEvent::DisconnectedDevice { peripheral_uuid } => f
.debug_struct("DisconnectedDevice")
Expand Down Expand Up @@ -486,13 +491,18 @@ pub mod CentralDelegate {
_cmd: Sel,
_central: id,
peripheral: id,
_error: id,
error: id,
) {
trace!("delegate_centralmanager_didfailtoconnectperipheral_error");
let peripheral_uuid = nsuuid_to_uuid(cb::peer_identifier(peripheral));
let error_description_ns = ns::error_localizeddescription(error);
let error_description = nsstring_to_string(error_description_ns);
send_delegate_event(
delegate,
CentralDelegateEvent::ConnectionFailed { peripheral_uuid },
CentralDelegateEvent::ConnectionFailed {
peripheral_uuid,
error_description,
},
);
}

Expand Down
9 changes: 9 additions & 0 deletions src/corebluetooth/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ pub mod ns {
uuidstring
}
}

// NSError

pub fn error_localizeddescription(nserror: id) -> id /* NSString* */ {
unsafe {
let description: id = msg_send![nserror, localizedDescription];
description
}
}
}

pub mod cb {
Expand Down
13 changes: 9 additions & 4 deletions src/corebluetooth/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,13 @@ impl CoreBluetoothInternal {
// itself when it receives all of its service/characteristic info.
}

fn on_peripheral_connection_failed(&mut self, peripheral_uuid: Uuid) {
fn on_peripheral_connection_failed(
&mut self,
peripheral_uuid: Uuid,
error_description: Option<String>,
) {
trace!("Got connection fail event!");
let error = error_description.unwrap_or(String::from("Connection failed"));
if self.peripherals.contains_key(&peripheral_uuid) {
let peripheral = self
.peripherals
Expand All @@ -619,7 +624,7 @@ impl CoreBluetoothInternal {
.unwrap()
.lock()
.unwrap()
.set_reply(CoreBluetoothReply::Err(String::from("Connection failed")));
.set_reply(CoreBluetoothReply::Err(error));
}
}

Expand Down Expand Up @@ -1022,8 +1027,8 @@ impl CoreBluetoothInternal {
CentralDelegateEvent::ConnectedDevice{peripheral_uuid} => {
self.on_peripheral_connect(peripheral_uuid)
},
CentralDelegateEvent::ConnectionFailed{peripheral_uuid} => {
self.on_peripheral_connection_failed(peripheral_uuid)
CentralDelegateEvent::ConnectionFailed{peripheral_uuid, error_description} => {
self.on_peripheral_connection_failed(peripheral_uuid, error_description)
},
CentralDelegateEvent::DisconnectedDevice{peripheral_uuid} => {
self.on_peripheral_disconnect(peripheral_uuid).await
Expand Down
3 changes: 2 additions & 1 deletion src/corebluetooth/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ impl api::Peripheral for Peripheral {
self.shared
.emit_event(CentralEvent::DeviceConnected(self.shared.uuid.into()));
}
_ => panic!("Shouldn't get anything but connected!"),
CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)),
_ => panic!("Shouldn't get anything but connected or err!"),
}
trace!("Device connected!");
Ok(())
Expand Down

0 comments on commit dc6e1a4

Please sign in to comment.