use crate::cb::{ManagerState, Properties};
use crate::objc::{
array_map, data_bytes, error_message, global_nsstring, nsarray, nsdata, nsdictionary, nsstring,
require_class, to_string, ClassBuilder, Retained,
};
use crate::objc::{Id, NIL};
use core::ffi::c_void;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Permissions(pub u32);
impl Permissions {
pub const READABLE: u32 = 0x01;
pub const WRITEABLE: u32 = 0x02;
pub const READ_ENCRYPTION_REQUIRED: u32 = 0x04;
pub const WRITE_ENCRYPTION_REQUIRED: u32 = 0x08;
#[inline]
pub fn has(self, bit: u32) -> bool {
self.0 & bit != 0
}
#[inline]
pub fn readable(self) -> bool {
self.has(Self::READABLE)
}
#[inline]
pub fn writeable(self) -> bool {
self.has(Self::WRITEABLE)
}
#[inline]
pub fn any_write(self) -> bool {
self.has(Self::WRITEABLE) || self.has(Self::WRITE_ENCRYPTION_REQUIRED)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(isize)]
pub enum AttError {
Success = 0x00,
InvalidHandle = 0x01,
ReadNotPermitted = 0x02,
WriteNotPermitted = 0x03,
InvalidPdu = 0x04,
InsufficientAuthentication = 0x05,
RequestNotSupported = 0x06,
InvalidOffset = 0x07,
InsufficientAuthorization = 0x08,
PrepareQueueFull = 0x09,
AttributeNotFound = 0x0A,
AttributeNotLong = 0x0B,
InsufficientEncryptionKeySize = 0x0C,
InvalidAttributeValueLength = 0x0D,
UnlikelyError = 0x0E,
InsufficientEncryption = 0x0F,
UnsupportedGroupType = 0x10,
InsufficientResources = 0x11,
}
#[derive(Debug)]
pub enum PeripheralEvent {
StateChanged(ManagerState),
AdvertisingStarted { error: Option<(i64, String)> },
ServiceAdded {
service: Retained,
error: Option<(i64, String)>,
},
Subscribed {
central: Retained,
characteristic: Retained,
},
Unsubscribed {
central: Retained,
characteristic: Retained,
},
ReadRequest { request: Retained },
WriteRequests { requests: Vec<Retained> },
ReadyToUpdateSubscribers,
L2capPublished {
psm: u16,
error: Option<(i64, String)>,
},
L2capUnpublished {
psm: u16,
error: Option<(i64, String)>,
},
L2capChannelOpened {
channel: Option<Retained>,
error: Option<(i64, String)>,
},
WillRestoreState(Box<RestoredPeripheralState>),
}
#[derive(Debug, Default)]
pub struct RestoredPeripheralState {
pub services: Vec<Retained>,
pub advertised_local_name: Option<String>,
pub advertised_services: Vec<String>,
}
pub unsafe fn parse_restored_peripheral_state(dict: Id) -> RestoredPeripheralState {
unsafe {
let services = {
let arr = crate::objc::dict_get(
dict,
global_nsstring(c"CBPeripheralManagerRestoredStateServicesKey"),
);
array_map(arr, |s| Retained::retain(s))
.into_iter()
.flatten()
.collect()
};
let advertisement = crate::objc::dict_get(
dict,
global_nsstring(c"CBPeripheralManagerRestoredStateAdvertisementDataKey"),
);
let advertised_local_name = to_string(crate::objc::dict_get(
advertisement,
global_nsstring(c"CBAdvertisementDataLocalNameKey"),
));
let advertised_services = {
let arr = crate::objc::dict_get(
advertisement,
global_nsstring(c"CBAdvertisementDataServiceUUIDsKey"),
);
array_map(arr, |u| crate::cb::uuid_string(u))
.into_iter()
.flatten()
.collect()
};
RestoredPeripheralState {
services,
advertised_local_name,
advertised_services,
}
}
}
pub trait PeripheralEventSink: Send + Sync {
fn emit(&self, event: PeripheralEvent);
}
static SINKS: OnceLock<Mutex<HashMap<usize, Arc<dyn PeripheralEventSink>>>> = OnceLock::new();
fn sinks() -> &'static Mutex<HashMap<usize, Arc<dyn PeripheralEventSink>>> {
SINKS.get_or_init(|| Mutex::new(HashMap::new()))
}
fn emit(delegate: Id, event: PeripheralEvent) {
let sink = sinks()
.lock()
.ok()
.and_then(|m| m.get(&(delegate as usize)).cloned());
if let Some(sink) = sink {
sink.emit(event);
}
}
#[derive(Debug)]
pub struct PeripheralDelegate {
object: Retained,
}
impl PeripheralDelegate {
pub fn new(sink: Arc<dyn PeripheralEventSink>) -> Self {
let class = delegate_class();
let object = unsafe {
let obj: Id = crate::msg_send![class, alloc];
let obj: Id = crate::msg_send![obj, init];
Retained::adopt(obj).expect("peripheral delegate allocation failed")
};
sinks()
.lock()
.expect("sink registry poisoned")
.insert(object.key(), sink);
Self { object }
}
#[inline]
pub fn as_ptr(&self) -> Id {
self.object.as_ptr()
}
}
impl Drop for PeripheralDelegate {
fn drop(&mut self) {
if let Ok(mut m) = sinks().lock() {
m.remove(&self.object.key());
}
}
}
static CLASS: OnceLock<usize> = OnceLock::new();
fn delegate_class() -> Id {
*CLASS.get_or_init(|| build_class() as usize) as Id
}
fn build_class() -> Id {
for attempt in 0..64u32 {
let name = if attempt == 0 {
c"WBRustCBPeripheralManagerDelegate".to_owned()
} else {
std::ffi::CString::new(format!("WBRustCBPeripheralManagerDelegate{attempt}"))
.expect("class name")
};
let Some(builder) = (unsafe { ClassBuilder::new(c"NSObject", &name) }) else {
continue;
};
return unsafe {
builder
.method(
c"peripheralManagerDidUpdateState:",
did_update_state as *const c_void,
c"v@:@",
)
.method(
c"peripheralManagerDidStartAdvertising:error:",
did_start_advertising as *const c_void,
c"v@:@@",
)
.method(
c"peripheralManager:didAddService:error:",
did_add_service as *const c_void,
c"v@:@@@",
)
.method(
c"peripheralManager:central:didSubscribeToCharacteristic:",
did_subscribe as *const c_void,
c"v@:@@@",
)
.method(
c"peripheralManager:central:didUnsubscribeFromCharacteristic:",
did_unsubscribe as *const c_void,
c"v@:@@@",
)
.method(
c"peripheralManager:didReceiveReadRequest:",
did_receive_read as *const c_void,
c"v@:@@",
)
.method(
c"peripheralManager:didReceiveWriteRequests:",
did_receive_writes as *const c_void,
c"v@:@@",
)
.method(
c"peripheralManagerIsReadyToUpdateSubscribers:",
is_ready_to_update as *const c_void,
c"v@:@",
)
.method(
c"peripheralManager:didPublishL2CAPChannel:error:",
did_publish_l2cap as *const c_void,
c"v@:@S@",
)
.method(
c"peripheralManager:didUnpublishL2CAPChannel:error:",
did_unpublish_l2cap as *const c_void,
c"v@:@S@",
)
.method(
c"peripheralManager:didOpenL2CAPChannel:error:",
did_open_l2cap as *const c_void,
c"v@:@@@",
)
.method(
c"peripheralManager:willRestoreState:",
will_restore_state as *const c_void,
c"v@:@@",
)
.conforms(c"CBPeripheralManagerDelegate")
.register()
};
}
panic!("could not register a peripheral delegate class after 64 attempts");
}
unsafe extern "C" fn did_update_state(this: Id, _cmd: *const c_void, manager: Id) {
let state = unsafe { manager_state(manager) };
emit(this, PeripheralEvent::StateChanged(state));
}
unsafe extern "C" fn did_start_advertising(this: Id, _cmd: *const c_void, _mgr: Id, error: Id) {
emit(
this,
PeripheralEvent::AdvertisingStarted {
error: unsafe { error_message(error) },
},
);
}
unsafe extern "C" fn did_add_service(
this: Id,
_cmd: *const c_void,
_mgr: Id,
service: Id,
error: Id,
) {
unsafe {
let Some(service) = Retained::retain(service) else {
return;
};
emit(
this,
PeripheralEvent::ServiceAdded {
service,
error: error_message(error),
},
);
}
}
unsafe extern "C" fn did_subscribe(
this: Id,
_cmd: *const c_void,
_mgr: Id,
central: Id,
characteristic: Id,
) {
unsafe {
let (Some(central), Some(characteristic)) =
(Retained::retain(central), Retained::retain(characteristic))
else {
return;
};
emit(
this,
PeripheralEvent::Subscribed {
central,
characteristic,
},
);
}
}
unsafe extern "C" fn did_unsubscribe(
this: Id,
_cmd: *const c_void,
_mgr: Id,
central: Id,
characteristic: Id,
) {
unsafe {
let (Some(central), Some(characteristic)) =
(Retained::retain(central), Retained::retain(characteristic))
else {
return;
};
emit(
this,
PeripheralEvent::Unsubscribed {
central,
characteristic,
},
);
}
}
unsafe extern "C" fn did_receive_read(this: Id, _cmd: *const c_void, _mgr: Id, request: Id) {
let Some(request) = (unsafe { Retained::retain(request) }) else {
return;
};
emit(this, PeripheralEvent::ReadRequest { request });
}
unsafe extern "C" fn did_receive_writes(this: Id, _cmd: *const c_void, _mgr: Id, requests: Id) {
unsafe {
let requests: Vec<Retained> = array_map(requests, |r| Retained::retain(r))
.into_iter()
.flatten()
.collect();
if requests.is_empty() {
return;
}
emit(this, PeripheralEvent::WriteRequests { requests });
}
}
unsafe extern "C" fn is_ready_to_update(this: Id, _cmd: *const c_void, _mgr: Id) {
emit(this, PeripheralEvent::ReadyToUpdateSubscribers);
}
unsafe extern "C" fn did_publish_l2cap(
this: Id,
_cmd: *const c_void,
_mgr: Id,
psm: u16,
error: Id,
) {
emit(
this,
PeripheralEvent::L2capPublished {
psm,
error: unsafe { error_message(error) },
},
);
}
unsafe extern "C" fn did_unpublish_l2cap(
this: Id,
_cmd: *const c_void,
_mgr: Id,
psm: u16,
error: Id,
) {
emit(
this,
PeripheralEvent::L2capUnpublished {
psm,
error: unsafe { error_message(error) },
},
);
}
unsafe extern "C" fn did_open_l2cap(
this: Id,
_cmd: *const c_void,
_mgr: Id,
channel: Id,
error: Id,
) {
unsafe {
emit(
this,
PeripheralEvent::L2capChannelOpened {
channel: Retained::retain(channel),
error: error_message(error),
},
);
}
}
unsafe extern "C" fn will_restore_state(this: Id, _cmd: *const c_void, _mgr: Id, dict: Id) {
let restored = unsafe { parse_restored_peripheral_state(dict) };
emit(this, PeripheralEvent::WillRestoreState(Box::new(restored)));
}
pub unsafe fn manager_state(manager: Id) -> ManagerState {
unsafe { ManagerState::from(crate::msg_send_t![isize; manager, state]) }
}
pub unsafe fn is_advertising(manager: Id) -> bool {
unsafe { crate::msg_send_t![bool; manager, isAdvertising] }
}
pub unsafe fn start_advertising(manager: Id, local_name: Option<&str>, services: &[Id]) {
unsafe {
let mut keys: Vec<Id> = Vec::new();
let mut values: Vec<Id> = Vec::new();
let mut owned: Vec<Retained> = Vec::new();
if let Some(name) = local_name {
let key = global_nsstring(c"CBAdvertisementDataLocalNameKey");
if let (false, Some(value)) = (key.is_null(), Retained::adopt(nsstring(name))) {
keys.push(key);
values.push(value.as_ptr());
owned.push(value);
}
}
if !services.is_empty() {
let key = global_nsstring(c"CBAdvertisementDataServiceUUIDsKey");
if let (false, Some(value)) = (key.is_null(), Retained::adopt(nsarray(services))) {
keys.push(key);
values.push(value.as_ptr());
owned.push(value);
}
}
let dict = if keys.is_empty() {
None
} else {
nsdictionary(&keys, &values)
};
crate::msg_send_void![
manager,
startAdvertising: dict.as_ref().map_or(NIL, |d| d.as_ptr())
];
}
}
pub unsafe fn stop_advertising(manager: Id) {
unsafe { crate::msg_send_void![manager, stopAdvertising] }
}
pub unsafe fn add_service(manager: Id, service: Id) {
unsafe { crate::msg_send_void![manager, addService: service] }
}
pub unsafe fn remove_service(manager: Id, service: Id) {
unsafe { crate::msg_send_void![manager, removeService: service] }
}
pub unsafe fn remove_all_services(manager: Id) {
unsafe { crate::msg_send_void![manager, removeAllServices] }
}
pub unsafe fn respond(manager: Id, request: Id, result: AttError) {
unsafe {
crate::msg_send_void![manager, respondToRequest: request, withResult: result as isize]
}
}
pub unsafe fn update_value(manager: Id, value: &[u8], characteristic: Id, centrals: &[Id]) -> bool {
unsafe {
let Some(data) = Retained::adopt(nsdata(value)) else {
return false;
};
let list = if centrals.is_empty() {
None
} else {
Retained::adopt(nsarray(centrals))
};
crate::msg_send_t![
bool;
manager,
updateValue: data.as_ptr(),
forCharacteristic: characteristic,
onSubscribedCentrals: list.as_ref().map_or(NIL, |l| l.as_ptr())
]
}
}
pub unsafe fn publish_l2cap_channel(manager: Id, encryption_required: bool) {
unsafe {
crate::msg_send_void![manager, publishL2CAPChannelWithEncryption: encryption_required]
}
}
pub unsafe fn unpublish_l2cap_channel(manager: Id, psm: u16) {
unsafe { crate::msg_send_void![manager, unpublishL2CAPChannel: psm] }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidAttribute {
CachedValueMustBeReadOnly,
UnsupportedDescriptor,
DescriptorValueRequired,
}
impl std::fmt::Display for InvalidAttribute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::CachedValueMustBeReadOnly => f.write_str(
"a characteristic with a fixed value must be read-only; leave the value unset \
for anything writeable or changing, and answer read requests on demand",
),
Self::UnsupportedDescriptor => f.write_str(
"only the user description (0x2901) and presentation format (0x2904) descriptors \
can be created; the client characteristic configuration descriptor is added \
automatically when a characteristic can notify",
),
Self::DescriptorValueRequired => f.write_str("a descriptor must have a value"),
}
}
}
pub fn validate_characteristic(
properties: Properties,
permissions: Permissions,
value: Option<&[u8]>,
) -> Result<(), InvalidAttribute> {
if value.is_some() {
let read_only_properties = properties.0 == Properties::READ;
let read_only_permissions = !permissions.any_write();
if !read_only_properties || !read_only_permissions {
return Err(InvalidAttribute::CachedValueMustBeReadOnly);
}
}
Ok(())
}
const USER_DESCRIPTION_UUID: u16 = 0x2901;
const PRESENTATION_FORMAT_UUID: u16 = 0x2904;
pub fn validate_descriptor(uuid16: Option<u16>, has_value: bool) -> Result<(), InvalidAttribute> {
match uuid16 {
Some(USER_DESCRIPTION_UUID) | Some(PRESENTATION_FORMAT_UUID) => {}
_ => return Err(InvalidAttribute::UnsupportedDescriptor),
}
if !has_value {
return Err(InvalidAttribute::DescriptorValueRequired);
}
Ok(())
}
pub unsafe fn mutable_service(uuid: Id, primary: bool) -> Option<Retained> {
unsafe {
let s: Id = crate::msg_send![require_class(c"CBMutableService"), alloc];
let s: Id = crate::msg_send![s, initWithType: uuid, primary: primary];
Retained::adopt(s)
}
}
pub unsafe fn service_set_characteristics(service: Id, characteristics: &[Id]) {
unsafe {
let arr = Retained::adopt(nsarray(characteristics));
crate::msg_send_void![
service,
setCharacteristics: arr.as_ref().map_or(NIL, |a| a.as_ptr())
];
}
}
pub unsafe fn service_set_included(service: Id, included: &[Id]) {
unsafe {
let arr = Retained::adopt(nsarray(included));
crate::msg_send_void![
service,
setIncludedServices: arr.as_ref().map_or(NIL, |a| a.as_ptr())
];
}
}
pub unsafe fn mutable_characteristic(
uuid: Id,
properties: Properties,
value: Option<&[u8]>,
permissions: Permissions,
) -> Option<Retained> {
unsafe {
let data = value.and_then(|v| Retained::adopt(nsdata(v)));
let c: Id = crate::msg_send![require_class(c"CBMutableCharacteristic"), alloc];
let c: Id = crate::msg_send![
c,
initWithType: uuid,
properties: properties.0 as usize,
value: data.as_ref().map_or(NIL, |d| d.as_ptr()),
permissions: permissions.0 as usize
];
Retained::adopt(c)
}
}
pub unsafe fn characteristic_set_descriptors(characteristic: Id, descriptors: &[Id]) {
unsafe {
let arr = Retained::adopt(nsarray(descriptors));
crate::msg_send_void![
characteristic,
setDescriptors: arr.as_ref().map_or(NIL, |a| a.as_ptr())
];
}
}
pub unsafe fn subscribed_centrals(characteristic: Id) -> Vec<Retained> {
unsafe {
let arr: Id = crate::msg_send![characteristic, subscribedCentrals];
array_map(arr, |c| Retained::retain(c))
.into_iter()
.flatten()
.collect()
}
}
pub unsafe fn mutable_descriptor(uuid: Id, value: &[u8], is_string: bool) -> Option<Retained> {
unsafe {
let boxed = if is_string {
Retained::adopt(nsstring(&String::from_utf8_lossy(value)))
} else {
Retained::adopt(nsdata(value))
}?;
let d: Id = crate::msg_send![require_class(c"CBMutableDescriptor"), alloc];
let d: Id = crate::msg_send![d, initWithType: uuid, value: boxed.as_ptr()];
Retained::adopt(d)
}
}
pub unsafe fn request_central(request: Id) -> Id {
unsafe { crate::msg_send![request, central] }
}
pub unsafe fn request_characteristic(request: Id) -> Id {
unsafe { crate::msg_send![request, characteristic] }
}
pub unsafe fn request_offset(request: Id) -> usize {
unsafe { crate::msg_send_t![usize; request, offset] }
}
pub unsafe fn request_value(request: Id) -> Option<Vec<u8>> {
unsafe { data_bytes(crate::msg_send![request, value]) }
}
pub unsafe fn request_set_value(request: Id, value: &[u8]) {
unsafe {
let Some(data) = Retained::adopt(nsdata(value)) else {
return;
};
crate::msg_send_void![request, setValue: data.as_ptr()];
}
}
pub unsafe fn central_identifier(central: Id) -> Option<String> {
unsafe {
to_string(crate::msg_send![
crate::msg_send![central, identifier],
UUIDString
])
}
}
pub unsafe fn central_max_update_length(central: Id) -> usize {
unsafe { crate::msg_send_t![usize; central, maximumUpdateValueLength] }
}
#[derive(Debug)]
pub struct PeripheralHost {
manager: Retained,
_delegate: PeripheralDelegate,
_queue: crate::dispatch::Queue,
}
impl PeripheralHost {
pub fn new(sink: Arc<dyn PeripheralEventSink>, show_power_alert: bool) -> Self {
Self::with_restore_identifier(sink, show_power_alert, None)
}
pub fn with_restore_identifier(
sink: Arc<dyn PeripheralEventSink>,
show_power_alert: bool,
restore_identifier: Option<&str>,
) -> Self {
let queue = crate::dispatch::Queue::serial(c"dev.webbluetooth.peripheral");
let delegate = PeripheralDelegate::new(sink);
let manager = unsafe {
let mut keys: Vec<Id> = Vec::new();
let mut values: Vec<Id> = Vec::new();
let mut owned: Vec<Retained> = Vec::new();
if show_power_alert {
let key = global_nsstring(c"CBPeripheralManagerOptionShowPowerAlertKey");
if !key.is_null() {
keys.push(key);
values.push(crate::msg_send![
require_class(c"NSNumber"),
numberWithBool: true
]);
}
}
if let Some(id) = restore_identifier {
let key = global_nsstring(c"CBPeripheralManagerOptionRestoreIdentifierKey");
if let (false, Some(value)) = (key.is_null(), Retained::adopt(nsstring(id))) {
keys.push(key);
values.push(value.as_ptr());
owned.push(value);
}
}
let options = if keys.is_empty() {
None
} else {
nsdictionary(&keys, &values)
};
let m: Id = crate::msg_send![require_class(c"CBPeripheralManager"), alloc];
let m: Id = crate::msg_send![
m,
initWithDelegate: delegate.as_ptr(),
queue: queue.as_ptr(),
options: options.as_ref().map_or(NIL, |o| o.as_ptr())
];
Retained::adopt(m).expect("CBPeripheralManager 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 { manager_state(self.as_ptr()) }
}
pub fn is_advertising(&self) -> bool {
unsafe { is_advertising(self.as_ptr()) }
}
}
unsafe impl Send for PeripheralHost {}
unsafe impl Sync for PeripheralHost {}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::mpsc;
struct Collect(mpsc::Sender<ManagerState>);
impl PeripheralEventSink for Collect {
fn emit(&self, event: PeripheralEvent) {
if let PeripheralEvent::StateChanged(s) = event {
let _ = self.0.send(s);
}
}
}
#[test]
#[cfg_attr(miri, ignore = "calls into the Objective-C runtime")]
fn peripheral_delegate_class_registers_and_receives_state() {
let (tx, rx) = mpsc::channel();
let host = PeripheralHost::new(Arc::new(Collect(tx)), false);
let state = rx
.recv_timeout(std::time::Duration::from_secs(5))
.expect("peripheralManagerDidUpdateState: never arrived");
assert_eq!(state, host.state());
assert!(!host.is_advertising());
}
#[test]
fn a_fixed_value_forces_read_only() {
let read = Properties(Properties::READ);
let readable = Permissions(Permissions::READABLE);
assert!(validate_characteristic(read, readable, Some(&[1, 2, 3])).is_ok());
assert_eq!(
validate_characteristic(
Properties(Properties::READ | Properties::WRITE),
readable,
Some(&[1])
),
Err(InvalidAttribute::CachedValueMustBeReadOnly)
);
assert_eq!(
validate_characteristic(
read,
Permissions(Permissions::READABLE | Permissions::WRITEABLE),
Some(&[1])
),
Err(InvalidAttribute::CachedValueMustBeReadOnly)
);
assert_eq!(
validate_characteristic(
Properties(Properties::READ | Properties::NOTIFY),
readable,
Some(&[1])
),
Err(InvalidAttribute::CachedValueMustBeReadOnly)
);
}
#[test]
fn a_dynamic_value_may_be_anything() {
assert!(validate_characteristic(
Properties(Properties::READ | Properties::WRITE | Properties::NOTIFY),
Permissions(Permissions::READABLE | Permissions::WRITEABLE),
None
)
.is_ok());
}
#[test]
fn only_two_descriptor_types_can_be_created() {
assert!(validate_descriptor(Some(0x2901), true).is_ok());
assert!(validate_descriptor(Some(0x2904), true).is_ok());
assert_eq!(
validate_descriptor(Some(0x2902), true),
Err(InvalidAttribute::UnsupportedDescriptor)
);
assert_eq!(
validate_descriptor(None, true),
Err(InvalidAttribute::UnsupportedDescriptor)
);
assert_eq!(
validate_descriptor(Some(0x2901), false),
Err(InvalidAttribute::DescriptorValueRequired)
);
}
}