#![cfg(target_vendor = "apple")]
#[macro_use]
pub mod macros;
#[doc(hidden)]
pub mod backend;
#[cfg(peripheral_role)]
pub mod peripheral_backend;
pub mod cb;
pub mod delegate;
pub mod dispatch;
pub mod l2cap;
pub mod objc;
#[cfg(peripheral_role)]
pub mod peripheral;
use std::sync::Arc;
pub use cb::RestoredCentralState;
pub use cb::{Advertisement, Authorization, ManagerState, PeripheralState, Properties, WriteType};
pub use delegate::{Event, EventSink};
pub use l2cap::{Channel as L2capChannel, Closed as L2capClosed, Psm};
pub use objc::{AutoreleasePool, Retained};
pub use objc::{Id, NIL};
#[cfg(peripheral_role)]
pub use peripheral::{
AttError, InvalidAttribute, PeripheralEvent, PeripheralEventSink, PeripheralHost, Permissions,
RestoredPeripheralState,
};
#[derive(Debug)]
pub struct Central {
manager: Retained,
_delegate: delegate::Delegate,
_queue: dispatch::Queue,
}
impl Central {
pub fn new(sink: Arc<dyn EventSink>, show_power_alert: bool) -> Self {
Self::with_restore_identifier(sink, show_power_alert, None)
}
pub fn with_restore_identifier(
sink: Arc<dyn EventSink>,
show_power_alert: bool,
restore_identifier: Option<&str>,
) -> Self {
let queue = dispatch::Queue::serial(c"dev.webbluetooth.central");
let delegate = delegate::Delegate::new(sink);
let manager = unsafe {
let options = cb::central_options(show_power_alert, restore_identifier);
cb::central_new(
delegate.as_ptr(),
queue.as_ptr(),
options.as_ref().map_or(NIL, |o| o.as_ptr()),
)
.expect("CBCentralManager allocation failed")
};
Self {
manager,
_delegate: delegate,
_queue: queue,
}
}
#[inline]
pub fn as_ptr(&self) -> Id {
self.manager.as_ptr()
}
pub fn state(&self) -> ManagerState {
unsafe { cb::central_state(self.as_ptr()) }
}
pub fn is_scanning(&self) -> bool {
unsafe { cb::central_is_scanning(self.as_ptr()) }
}
pub unsafe fn adopt_peripheral(&self, peripheral: Id) {
unsafe { cb::peripheral_set_delegate(peripheral, self._delegate.as_ptr()) }
}
}
unsafe impl Send for Central {}
unsafe impl Sync for Central {}
pub fn authorization() -> Authorization {
cb::authorization()
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::mpsc;
struct Collect(mpsc::Sender<ManagerState>);
impl EventSink for Collect {
fn emit(&self, event: Event) {
if let Event::StateChanged(s) = event {
let _ = self.0.send(s);
}
}
}
#[test]
#[cfg_attr(miri, ignore = "calls into the Objective-C runtime")]
fn delegate_class_registers_and_receives_state() {
let (tx, rx) = mpsc::channel();
let central = Central::new(Arc::new(Collect(tx)), false);
let state = rx
.recv_timeout(std::time::Duration::from_secs(5))
.expect("centralManagerDidUpdateState: never arrived");
assert_eq!(state, central.state());
}
#[test]
#[cfg_attr(miri, ignore = "calls into the Objective-C runtime")]
fn two_centrals_get_independent_sinks() {
let (tx1, rx1) = mpsc::channel();
let (tx2, rx2) = mpsc::channel();
let _a = Central::new(Arc::new(Collect(tx1)), false);
let _b = Central::new(Arc::new(Collect(tx2)), false);
let t = std::time::Duration::from_secs(5);
assert!(rx1.recv_timeout(t).is_ok(), "first central got no state");
assert!(rx2.recv_timeout(t).is_ok(), "second central got no state");
}
#[test]
#[cfg_attr(miri, ignore = "calls into the Objective-C runtime")]
fn uuid_string_is_not_canonical() {
let short = unsafe { cb::uuid_from_string("180F") }.expect("short CBUUID");
assert_eq!(
unsafe { cb::uuid_string(short.as_ptr()) }.as_deref(),
Some("180F")
);
let long = unsafe { cb::uuid_from_string("0000180f-0000-1000-8000-00805f9b34fb") }
.expect("128-bit CBUUID");
assert_eq!(
unsafe { cb::uuid_string(long.as_ptr()) }.as_deref(),
Some("0000180F-0000-1000-8000-00805F9B34FB"),
"same UUID, different string — and uppercased"
);
}
}