use crate::objc::{
self, array_map, data_bytes, dict_get, global_nsstring, nsarray, nsdata, nsstring, number_bool,
number_i64, require_class, to_string, Retained,
};
use crate::objc::{Id, NIL};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ManagerState {
Unknown = 0,
Resetting = 1,
Unsupported = 2,
Unauthorized = 3,
PoweredOff = 4,
PoweredOn = 5,
}
impl From<isize> for ManagerState {
fn from(v: isize) -> Self {
match v {
1 => Self::Resetting,
2 => Self::Unsupported,
3 => Self::Unauthorized,
4 => Self::PoweredOff,
5 => Self::PoweredOn,
_ => Self::Unknown,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Authorization {
NotDetermined = 0,
Restricted = 1,
Denied = 2,
Allowed = 3,
}
impl From<isize> for Authorization {
fn from(v: isize) -> Self {
match v {
1 => Self::Restricted,
2 => Self::Denied,
3 => Self::Allowed,
_ => Self::NotDetermined,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PeripheralState {
Disconnected = 0,
Connecting = 1,
Connected = 2,
Disconnecting = 3,
}
impl From<isize> for PeripheralState {
fn from(v: isize) -> Self {
match v {
1 => Self::Connecting,
2 => Self::Connected,
3 => Self::Disconnecting,
_ => Self::Disconnected,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Properties(pub u32);
impl Properties {
pub const BROADCAST: u32 = 0x01;
pub const READ: u32 = 0x02;
pub const WRITE_WITHOUT_RESPONSE: u32 = 0x04;
pub const WRITE: u32 = 0x08;
pub const NOTIFY: u32 = 0x10;
pub const INDICATE: u32 = 0x20;
pub const AUTHENTICATED_SIGNED_WRITES: u32 = 0x40;
pub const EXTENDED_PROPERTIES: u32 = 0x80;
pub const NOTIFY_ENCRYPTION_REQUIRED: u32 = 0x100;
pub const INDICATE_ENCRYPTION_REQUIRED: u32 = 0x200;
#[inline]
pub fn has(self, bit: u32) -> bool {
self.0 & bit != 0
}
#[inline]
pub fn broadcast(self) -> bool {
self.has(Self::BROADCAST)
}
#[inline]
pub fn read(self) -> bool {
self.has(Self::READ)
}
#[inline]
pub fn write_without_response(self) -> bool {
self.has(Self::WRITE_WITHOUT_RESPONSE)
}
#[inline]
pub fn write(self) -> bool {
self.has(Self::WRITE)
}
#[inline]
pub fn notify(self) -> bool {
self.has(Self::NOTIFY)
}
#[inline]
pub fn indicate(self) -> bool {
self.has(Self::INDICATE)
}
#[inline]
pub fn authenticated_signed_writes(self) -> bool {
self.has(Self::AUTHENTICATED_SIGNED_WRITES)
}
#[inline]
pub fn reliable_write(self) -> bool {
self.has(Self::EXTENDED_PROPERTIES)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WriteType {
WithResponse = 0,
WithoutResponse = 1,
}
pub fn authorization() -> Authorization {
unsafe { Authorization::from(msg_send_t![isize; require_class(c"CBManager"), authorization]) }
}
pub unsafe fn uuid_from_string(s: &str) -> Option<Retained> {
unsafe {
let ns = Retained::adopt(nsstring(s))?;
let u: Id = msg_send![require_class(c"CBUUID"), UUIDWithString: ns.as_ptr()];
Retained::retain(u)
}
}
pub unsafe fn uuid_string(uuid: Id) -> Option<String> {
unsafe { to_string(msg_send![uuid, UUIDString]) }
}
pub unsafe fn central_new(delegate: Id, queue: Id, options: Id) -> Option<Retained> {
unsafe {
let m: Id = msg_send![require_class(c"CBCentralManager"), alloc];
let m: Id = msg_send![m, initWithDelegate: delegate, queue: queue, options: options];
Retained::adopt(m)
}
}
pub unsafe fn central_options(
show_power_alert: bool,
restore_identifier: Option<&str>,
) -> Option<Retained> {
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"CBCentralManagerOptionShowPowerAlertKey");
if !key.is_null() {
keys.push(key);
values.push(msg_send![require_class(c"NSNumber"), numberWithBool: true]);
}
}
if let Some(id) = restore_identifier {
let key = global_nsstring(c"CBCentralManagerOptionRestoreIdentifierKey");
if let (false, Some(value)) = (key.is_null(), Retained::adopt(nsstring(id))) {
keys.push(key);
values.push(value.as_ptr());
owned.push(value);
}
}
if keys.is_empty() {
return None;
}
objc::nsdictionary(&keys, &values)
}
}
pub unsafe fn peripheral_open_l2cap(peripheral: Id, psm: u16) {
unsafe { msg_send_void![peripheral, openL2CAPChannel: psm] }
}
#[derive(Debug, Default)]
pub struct RestoredCentralState {
pub peripherals: Vec<Retained>,
pub scan_services: Vec<String>,
pub scan_allow_duplicates: bool,
}
pub unsafe fn parse_restored_central_state(dict: Id) -> RestoredCentralState {
unsafe {
let peripherals = {
let arr = dict_get(
dict,
global_nsstring(c"CBCentralManagerRestoredStatePeripheralsKey"),
);
array_map(arr, |p| Retained::retain(p))
.into_iter()
.flatten()
.collect()
};
let scan_services = {
let arr = dict_get(
dict,
global_nsstring(c"CBCentralManagerRestoredStateScanServicesKey"),
);
array_map(arr, |u| uuid_string(u))
.into_iter()
.flatten()
.collect()
};
let scan_allow_duplicates = {
let opts = dict_get(
dict,
global_nsstring(c"CBCentralManagerRestoredStateScanOptionsKey"),
);
let key = global_nsstring(c"CBCentralManagerScanOptionAllowDuplicatesKey");
number_bool(dict_get(opts, key)).unwrap_or(false)
};
RestoredCentralState {
peripherals,
scan_services,
scan_allow_duplicates,
}
}
}
pub unsafe fn central_state(central: Id) -> ManagerState {
unsafe { ManagerState::from(msg_send_t![isize; central, state]) }
}
pub unsafe fn central_is_scanning(central: Id) -> bool {
unsafe { msg_send_t![bool; central, isScanning] }
}
pub unsafe fn central_scan(central: Id, services: &[Id], allow_duplicates: bool) {
unsafe {
let arr = if services.is_empty() {
None
} else {
Retained::adopt(nsarray(services))
};
let opts = if allow_duplicates {
let key = global_nsstring(c"CBCentralManagerScanOptionAllowDuplicatesKey");
if key.is_null() {
None
} else {
let val: Id = msg_send![require_class(c"NSNumber"), numberWithBool: true];
let keys = Retained::adopt(nsarray(&[key]));
let vals = Retained::adopt(nsarray(&[val]));
match (keys, vals) {
(Some(k), Some(v)) => {
let d: Id = msg_send![
require_class(c"NSDictionary"),
dictionaryWithObjects: v.as_ptr(),
forKeys: k.as_ptr()
];
Retained::retain(d)
}
_ => None,
}
}
} else {
None
};
msg_send_void![
central,
scanForPeripheralsWithServices: arr.as_ref().map_or(NIL, |a| a.as_ptr()),
options: opts.as_ref().map_or(NIL, |o| o.as_ptr())
];
}
}
pub unsafe fn central_stop_scan(central: Id) {
unsafe { msg_send_void![central, stopScan] }
}
pub unsafe fn central_connect(central: Id, peripheral: Id) {
unsafe { msg_send_void![central, connectPeripheral: peripheral, options: NIL] }
}
pub unsafe fn central_disconnect(central: Id, peripheral: Id) {
unsafe { msg_send_void![central, cancelPeripheralConnection: peripheral] }
}
pub unsafe fn central_retrieve_connected(central: Id, services: &[Id]) -> Vec<Retained> {
unsafe {
let Some(arr) = Retained::adopt(nsarray(services)) else {
return Vec::new();
};
let res: Id = msg_send![central, retrieveConnectedPeripheralsWithServices: arr.as_ptr()];
array_map(res, |p| Retained::retain(p))
.into_iter()
.flatten()
.collect()
}
}
pub unsafe fn central_retrieve_by_identifier(central: Id, identifiers: &[&str]) -> Vec<Retained> {
unsafe {
let mut nsuuids = Vec::with_capacity(identifiers.len());
for id in identifiers {
let Some(s) = Retained::adopt(nsstring(id)) else {
continue;
};
let u: Id = msg_send![require_class(c"NSUUID"), alloc];
let u: Id = msg_send![u, initWithUUIDString: s.as_ptr()];
if let Some(r) = Retained::adopt(u) {
nsuuids.push(r);
}
}
let ptrs: Vec<Id> = nsuuids.iter().map(|u| u.as_ptr()).collect();
let Some(arr) = Retained::adopt(nsarray(&ptrs)) else {
return Vec::new();
};
let res: Id = msg_send![central, retrievePeripheralsWithIdentifiers: arr.as_ptr()];
array_map(res, |p| Retained::retain(p))
.into_iter()
.flatten()
.collect()
}
}
pub unsafe fn peripheral_name(peripheral: Id) -> Option<String> {
unsafe { to_string(msg_send![peripheral, name]) }
}
pub unsafe fn peripheral_identifier(peripheral: Id) -> Option<String> {
unsafe { to_string(msg_send![msg_send![peripheral, identifier], UUIDString]) }
}
pub unsafe fn peripheral_state(peripheral: Id) -> PeripheralState {
unsafe { PeripheralState::from(msg_send_t![isize; peripheral, state]) }
}
pub unsafe fn peripheral_set_delegate(peripheral: Id, delegate: Id) {
unsafe { msg_send_void![peripheral, setDelegate: delegate] }
}
pub unsafe fn peripheral_discover_services(peripheral: Id, services: &[Id]) {
unsafe {
let arr = if services.is_empty() {
None
} else {
Retained::adopt(nsarray(services))
};
msg_send_void![peripheral, discoverServices: arr.as_ref().map_or(NIL, |a| a.as_ptr())];
}
}
pub unsafe fn peripheral_services(peripheral: Id) -> Vec<Retained> {
unsafe {
let arr: Id = msg_send![peripheral, services];
array_map(arr, |s| Retained::retain(s))
.into_iter()
.flatten()
.collect()
}
}
pub unsafe fn peripheral_discover_characteristics(peripheral: Id, uuids: &[Id], service: Id) {
unsafe {
let arr = if uuids.is_empty() {
None
} else {
Retained::adopt(nsarray(uuids))
};
msg_send_void![
peripheral,
discoverCharacteristics: arr.as_ref().map_or(NIL, |a| a.as_ptr()),
forService: service
];
}
}
pub unsafe fn peripheral_discover_descriptors(peripheral: Id, characteristic: Id) {
unsafe { msg_send_void![peripheral, discoverDescriptorsForCharacteristic: characteristic] }
}
pub unsafe fn peripheral_read_characteristic(peripheral: Id, characteristic: Id) {
unsafe { msg_send_void![peripheral, readValueForCharacteristic: characteristic] }
}
pub unsafe fn peripheral_write_characteristic(
peripheral: Id,
characteristic: Id,
value: &[u8],
write_type: WriteType,
) {
unsafe {
let Some(data) = Retained::adopt(nsdata(value)) else {
return;
};
msg_send_void![
peripheral,
writeValue: data.as_ptr(),
forCharacteristic: characteristic,
type: write_type as isize
];
}
}
pub unsafe fn peripheral_set_notify(peripheral: Id, characteristic: Id, enabled: bool) {
unsafe {
msg_send_void![peripheral, setNotifyValue: enabled, forCharacteristic: characteristic]
}
}
pub unsafe fn peripheral_read_descriptor(peripheral: Id, descriptor: Id) {
unsafe { msg_send_void![peripheral, readValueForDescriptor: descriptor] }
}
pub unsafe fn peripheral_write_descriptor(peripheral: Id, descriptor: Id, value: &[u8]) {
unsafe {
let Some(data) = Retained::adopt(nsdata(value)) else {
return;
};
msg_send_void![peripheral, writeValue: data.as_ptr(), forDescriptor: descriptor];
}
}
pub unsafe fn peripheral_read_rssi(peripheral: Id) {
unsafe { msg_send_void![peripheral, readRSSI] }
}
pub unsafe fn peripheral_max_write_len(peripheral: Id, write_type: WriteType) -> usize {
unsafe { msg_send_t![usize; peripheral, maximumWriteValueLengthForType: write_type as isize] }
}
pub unsafe fn peripheral_can_write_without_response(peripheral: Id) -> bool {
unsafe { msg_send_t![bool; peripheral, canSendWriteWithoutResponse] }
}
pub unsafe fn attribute_uuid(attribute: Id) -> Id {
unsafe { msg_send![attribute, UUID] }
}
pub unsafe fn service_is_primary(service: Id) -> bool {
unsafe { msg_send_t![bool; service, isPrimary] }
}
pub unsafe fn service_characteristics(service: Id) -> Vec<Retained> {
unsafe {
let arr: Id = msg_send![service, characteristics];
array_map(arr, |c| Retained::retain(c))
.into_iter()
.flatten()
.collect()
}
}
pub unsafe fn peripheral_discover_included_services(peripheral: Id, services: &[Id], service: Id) {
unsafe {
let filter = if services.is_empty() {
NIL
} else {
nsarray(services)
};
msg_send_void![
peripheral,
discoverIncludedServices: filter,
forService: service
];
}
}
pub unsafe fn service_included(service: Id) -> Vec<Retained> {
unsafe {
let arr: Id = msg_send![service, includedServices];
array_map(arr, |s| Retained::retain(s))
.into_iter()
.flatten()
.collect()
}
}
pub unsafe fn characteristic_properties(characteristic: Id) -> Properties {
unsafe { Properties(msg_send_t![usize; characteristic, properties] as u32) }
}
pub unsafe fn characteristic_value(characteristic: Id) -> Option<Vec<u8>> {
unsafe { data_bytes(msg_send![characteristic, value]) }
}
pub unsafe fn characteristic_is_notifying(characteristic: Id) -> bool {
unsafe { msg_send_t![bool; characteristic, isNotifying] }
}
pub unsafe fn characteristic_service(characteristic: Id) -> Id {
unsafe { msg_send![characteristic, service] }
}
pub unsafe fn characteristic_descriptors(characteristic: Id) -> Vec<Retained> {
unsafe {
let arr: Id = msg_send![characteristic, descriptors];
array_map(arr, |d| Retained::retain(d))
.into_iter()
.flatten()
.collect()
}
}
pub unsafe fn descriptor_value(descriptor: Id) -> Option<Vec<u8>> {
unsafe {
let v: Id = msg_send![descriptor, value];
if v.is_null() {
return None;
}
if msg_send_t![bool; v, isKindOfClass: require_class(c"NSData")] {
return data_bytes(v);
}
if msg_send_t![bool; v, isKindOfClass: require_class(c"NSNumber")] {
let n = number_i64(v).unwrap_or(0);
return Some((n as u16).to_le_bytes().to_vec());
}
if msg_send_t![bool; v, isKindOfClass: require_class(c"NSString")] {
return to_string(v).map(String::into_bytes);
}
None
}
}
pub unsafe fn descriptor_characteristic(descriptor: Id) -> Id {
unsafe { msg_send![descriptor, characteristic] }
}
#[derive(Debug, Clone, Default)]
pub struct Advertisement {
pub local_name: Option<String>,
pub tx_power: Option<i16>,
pub is_connectable: Option<bool>,
pub service_uuids: Vec<String>,
pub overflow_service_uuids: Vec<String>,
pub solicited_service_uuids: Vec<String>,
pub manufacturer_data: Option<Vec<u8>>,
pub service_data: Vec<(String, Vec<u8>)>,
}
pub unsafe fn parse_advertisement(dict: Id) -> Advertisement {
unsafe {
let uuids_at = |sym: &core::ffi::CStr| -> Vec<String> {
let arr = dict_get(dict, global_nsstring(sym));
array_map(arr, |u| uuid_string(u))
.into_iter()
.flatten()
.collect()
};
let service_data = {
let sd = dict_get(dict, global_nsstring(c"CBAdvertisementDataServiceDataKey"));
let keys = objc::dict_keys(sd);
array_map(keys, |k| {
let uuid = uuid_string(k)?;
let bytes = data_bytes(dict_get(sd, k))?;
Some((uuid, bytes))
})
.into_iter()
.flatten()
.collect()
};
Advertisement {
local_name: to_string(dict_get(
dict,
global_nsstring(c"CBAdvertisementDataLocalNameKey"),
)),
tx_power: number_i64(dict_get(
dict,
global_nsstring(c"CBAdvertisementDataTxPowerLevelKey"),
))
.map(|v| v as i16),
is_connectable: number_bool(dict_get(
dict,
global_nsstring(c"CBAdvertisementDataIsConnectable"),
)),
service_uuids: uuids_at(c"CBAdvertisementDataServiceUUIDsKey"),
overflow_service_uuids: uuids_at(c"CBAdvertisementDataOverflowServiceUUIDsKey"),
solicited_service_uuids: uuids_at(c"CBAdvertisementDataSolicitedServiceUUIDsKey"),
manufacturer_data: data_bytes(dict_get(
dict,
global_nsstring(c"CBAdvertisementDataManufacturerDataKey"),
)),
service_data,
}
}
}