use crate::{
cb, peripheral as sys, ManagerState, PeripheralEvent, PeripheralEventSink, PeripheralHost,
Retained,
};
use futures_channel::oneshot;
use futures_core::Stream;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::{Arc, Mutex, Weak};
use std::task::{Context, Poll};
use webbluetooth_core::error::{Availability, Error, Result};
use webbluetooth_core::peripheral::Advertising;
use webbluetooth_core::peripheral::{AttError, RestoredPeripheral, Service, Write};
use webbluetooth_core::uuid::BluetoothUuid;
use webbluetooth_core::uuid::IntoUuid;
fn to_sys(error: AttError) -> sys::AttError {
use sys::AttError as S;
match error {
AttError::Success => S::Success,
AttError::InvalidHandle => S::InvalidHandle,
AttError::ReadNotPermitted => S::ReadNotPermitted,
AttError::WriteNotPermitted => S::WriteNotPermitted,
AttError::InvalidPdu => S::InvalidPdu,
AttError::InsufficientAuthentication => S::InsufficientAuthentication,
AttError::RequestNotSupported => S::RequestNotSupported,
AttError::InvalidOffset => S::InvalidOffset,
AttError::InsufficientAuthorization => S::InsufficientAuthorization,
AttError::PrepareQueueFull => S::PrepareQueueFull,
AttError::AttributeNotFound => S::AttributeNotFound,
AttError::AttributeNotLong => S::AttributeNotLong,
AttError::InsufficientEncryptionKeySize => S::InsufficientEncryptionKeySize,
AttError::InvalidAttributeValueLength => S::InvalidAttributeValueLength,
AttError::UnlikelyError => S::UnlikelyError,
AttError::InsufficientEncryption => S::InsufficientEncryption,
AttError::UnsupportedGroupType => S::UnsupportedGroupType,
AttError::InsufficientResources => S::InsufficientResources,
}
}
#[derive(Debug, Clone)]
pub struct RemoteCentral {
handle: Retained,
id: String,
}
impl RemoteCentral {
pub fn id(&self) -> &str {
&self.id
}
pub fn max_notification_length(&self) -> usize {
unsafe { sys::central_max_update_length(self.handle.as_ptr()) }
}
}
pub struct ReadRequest {
inner: Arc<Inner>,
request: Retained,
characteristic: BluetoothUuid,
central: RemoteCentral,
offset: usize,
answered: bool,
}
impl ReadRequest {
pub fn characteristic(&self) -> &BluetoothUuid {
&self.characteristic
}
pub fn central(&self) -> &RemoteCentral {
&self.central
}
pub fn offset(&self) -> usize {
self.offset
}
pub fn respond(mut self, value: &[u8]) -> Result<()> {
if self.offset > value.len() {
self.answer(AttError::InvalidOffset);
return Err(Error::InvalidModification(format!(
"read offset {} is past the end of a {}-byte value",
self.offset,
value.len()
)));
}
unsafe { sys::request_set_value(self.request.as_ptr(), &value[self.offset..]) };
self.answer(AttError::Success);
Ok(())
}
pub fn reject(mut self, error: AttError) {
self.answer(error);
}
fn answer(&mut self, result: AttError) {
if self.answered {
return;
}
self.answered = true;
unsafe {
sys::respond(
self.inner.host.as_ptr(),
self.request.as_ptr(),
to_sys(result),
)
};
}
}
impl Drop for ReadRequest {
fn drop(&mut self) {
self.answer(AttError::RequestNotSupported);
}
}
impl std::fmt::Debug for ReadRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ReadRequest")
.field("characteristic", &self.characteristic.as_str())
.field("offset", &self.offset)
.finish()
}
}
pub struct WriteRequest {
inner: Arc<Inner>,
first: Retained,
writes: Vec<Write>,
central: RemoteCentral,
answered: bool,
}
impl WriteRequest {
pub fn writes(&self) -> &[Write] {
&self.writes
}
pub fn central(&self) -> &RemoteCentral {
&self.central
}
pub fn accept(mut self) {
self.answer(AttError::Success);
}
pub fn reject_all(mut self, error: AttError) {
self.answer(error);
}
fn answer(&mut self, result: AttError) {
if self.answered {
return;
}
self.answered = true;
unsafe {
sys::respond(
self.inner.host.as_ptr(),
self.first.as_ptr(),
to_sys(result),
)
};
}
}
impl Drop for WriteRequest {
fn drop(&mut self) {
self.answer(AttError::RequestNotSupported);
}
}
impl std::fmt::Debug for WriteRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WriteRequest")
.field("writes", &self.writes.len())
.finish()
}
}
const REQUEST_BACKLOG: usize = 1024;
pub struct Requests {
rx: webbluetooth_core::backlog::Receiver<Request>,
}
impl Requests {
pub fn lost(&self) -> u64 {
self.rx.lost()
}
}
impl Stream for Requests {
type Item = Request;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Request>> {
Pin::new(&mut self.rx).poll_next(cx)
}
}
#[derive(Debug)]
pub enum Request {
Read(ReadRequest),
Write(WriteRequest),
Subscribed {
central: RemoteCentral,
characteristic: BluetoothUuid,
},
Unsubscribed {
central: RemoteCentral,
characteristic: BluetoothUuid,
},
ReadyToNotify,
ChannelOpened(crate::l2cap::L2capChannel),
}
struct Inner {
host: PeripheralHost,
state: Mutex<ManagerState>,
state_waiters: Mutex<Vec<oneshot::Sender<ManagerState>>>,
advertising_waiters: Mutex<Vec<oneshot::Sender<Result<()>>>>,
service_waiters: Mutex<HashMap<usize, Vec<oneshot::Sender<Result<()>>>>>,
ready_waiters: Mutex<Vec<oneshot::Sender<()>>>,
published: Mutex<HashMap<usize, BluetoothUuid>>,
requests: Mutex<Option<webbluetooth_core::backlog::Sender<Request>>>,
l2cap_publish_waiters: Mutex<Vec<oneshot::Sender<Result<u16>>>>,
l2cap_unpublish_waiters: Mutex<Vec<oneshot::Sender<Result<()>>>>,
restored: Mutex<Option<RestoredPeripheral>>,
}
struct Sink(Weak<Inner>);
impl PeripheralEventSink for Sink {
fn emit(&self, event: PeripheralEvent) {
if let Some(inner) = self.0.upgrade() {
inner.handle(event);
}
}
}
impl Inner {
fn handle(self: &Arc<Self>, event: PeripheralEvent) {
match event {
PeripheralEvent::StateChanged(state) => {
*self.state.lock().unwrap() = state;
for tx in self.state_waiters.lock().unwrap().drain(..) {
let _ = tx.send(state);
}
}
PeripheralEvent::AdvertisingStarted { error } => {
let result = match error {
Some((code, message)) => Err(Error::Network(format!(
"could not start advertising: {message} (CBError {code})"
))),
None => Ok(()),
};
for tx in self.advertising_waiters.lock().unwrap().drain(..) {
let _ = tx.send(result.clone());
}
}
PeripheralEvent::ServiceAdded { service, error } => {
let result = match error {
Some((code, message)) => Err(Error::Network(format!(
"could not publish service: {message} (CBError {code})"
))),
None => Ok(()),
};
let waiters = self.service_waiters.lock().unwrap().remove(&service.key());
for tx in waiters.into_iter().flatten() {
let _ = tx.send(result.clone());
}
}
PeripheralEvent::Subscribed {
central,
characteristic,
} => {
let uuid = self.name_of(&characteristic);
self.send(Request::Subscribed {
central: self.wrap_central(central),
characteristic: uuid,
});
}
PeripheralEvent::Unsubscribed {
central,
characteristic,
} => {
let uuid = self.name_of(&characteristic);
self.send(Request::Unsubscribed {
central: self.wrap_central(central),
characteristic: uuid,
});
}
PeripheralEvent::ReadRequest { request } => {
let (characteristic, central, offset) = unsafe {
let c = sys::request_characteristic(request.as_ptr());
let uuid = Retained::retain(c)
.map(|r| self.name_of(&r))
.unwrap_or_else(|| BluetoothUuid::from_u16(0));
let central = Retained::retain(sys::request_central(request.as_ptr()));
(uuid, central, sys::request_offset(request.as_ptr()))
};
let Some(central) = central.map(|c| self.wrap_central(c)) else {
return;
};
let read = ReadRequest {
inner: self.clone(),
request,
characteristic,
central,
offset,
answered: false,
};
self.send(Request::Read(read));
}
PeripheralEvent::WriteRequests { requests } => {
let Some(first) = requests.first().cloned() else {
return;
};
let central = unsafe { Retained::retain(sys::request_central(first.as_ptr())) };
let Some(central) = central.map(|c| self.wrap_central(c)) else {
return;
};
let writes = requests
.iter()
.map(|r| unsafe {
let c = Retained::retain(sys::request_characteristic(r.as_ptr()));
Write {
characteristic: c
.map(|c| self.name_of(&c))
.unwrap_or_else(|| BluetoothUuid::from_u16(0)),
offset: sys::request_offset(r.as_ptr()),
value: sys::request_value(r.as_ptr()).unwrap_or_default(),
}
})
.collect();
self.send(Request::Write(WriteRequest {
inner: self.clone(),
first,
writes,
central,
answered: false,
}));
}
PeripheralEvent::L2capPublished { psm, error } => {
let result = match error {
Some((code, message)) => Err(Error::Network(format!(
"could not publish an L2CAP channel: {message} (CBError {code})"
))),
None => Ok(psm),
};
for tx in self.l2cap_publish_waiters.lock().unwrap().drain(..) {
let _ = tx.send(result.clone());
}
}
PeripheralEvent::L2capUnpublished { psm, error } => {
let result = match error {
Some((code, message)) => Err(Error::Network(format!(
"could not unpublish PSM {psm}: {message} (CBError {code})"
))),
None => Ok(()),
};
for tx in self.l2cap_unpublish_waiters.lock().unwrap().drain(..) {
let _ = tx.send(result.clone());
}
}
PeripheralEvent::L2capChannelOpened { channel, error } => {
if error.is_some() {
return;
}
let Some(handle) = channel else { return };
if let Ok(channel) = unsafe { crate::l2cap::adopt(handle) } {
self.send(Request::ChannelOpened(channel));
}
}
PeripheralEvent::WillRestoreState(restored) => {
*self.restored.lock().unwrap() = Some(RestoredPeripheral {
advertised_local_name: restored.advertised_local_name.clone(),
advertised_services: restored
.advertised_services
.iter()
.filter_map(|s| BluetoothUuid::parse(s).ok())
.collect(),
service_count: restored.services.len(),
});
}
PeripheralEvent::ReadyToUpdateSubscribers => {
for tx in self.ready_waiters.lock().unwrap().drain(..) {
let _ = tx.send(());
}
self.send(Request::ReadyToNotify);
}
}
}
fn send(&self, request: Request) {
let tx = self.requests.lock().unwrap().clone();
if let Some(tx) = tx {
if tx.send(request).is_err() {
*self.requests.lock().unwrap() = None;
}
}
}
fn name_of(&self, characteristic: &Retained) -> BluetoothUuid {
if let Some(uuid) = self.published.lock().unwrap().get(&characteristic.key()) {
return *uuid;
}
unsafe { cb::uuid_string(cb::attribute_uuid(characteristic.as_ptr())) }
.and_then(|s| BluetoothUuid::parse(&s).ok())
.unwrap_or_else(|| BluetoothUuid::from_u16(0))
}
fn wrap_central(&self, handle: Retained) -> RemoteCentral {
let id = unsafe { sys::central_identifier(handle.as_ptr()) }.unwrap_or_default();
RemoteCentral { handle, id }
}
async fn settled_state(&self) -> ManagerState {
let mut state = *self.state.lock().unwrap();
if state != ManagerState::Unknown {
return state;
}
let rx = {
let (tx, rx) = oneshot::channel();
let current = *self.state.lock().unwrap();
if current != ManagerState::Unknown {
return current;
}
self.state_waiters.lock().unwrap().push(tx);
rx
};
if let Ok(Ok(s)) =
webbluetooth_core::timer::timeout(std::time::Duration::from_secs(5), rx).await
{
state = s;
}
state
}
async fn require_powered_on(&self) -> Result<()> {
crate::backend::manager_state(self.settled_state().await).require_powered_on()
}
}
#[derive(Clone)]
pub struct Peripheral {
inner: Arc<Inner>,
}
impl Peripheral {
pub fn new() -> (Self, Requests) {
Self::build(None)
}
pub fn with_restoration(restoration: webbluetooth_core::Restoration) -> (Self, Requests) {
Self::build(Some(restoration))
}
fn build(restoration: Option<webbluetooth_core::Restoration>) -> (Self, Requests) {
let identifier = restoration.map(|r| r.identifier);
let (tx, rx) = webbluetooth_core::backlog::bounded(
REQUEST_BACKLOG,
webbluetooth_core::backlog::Overflow::KeepOldest,
);
let inner = Arc::new_cyclic(|weak: &Weak<Inner>| {
let sink = Arc::new(Sink(weak.clone()));
Inner {
host: PeripheralHost::with_restore_identifier(sink, false, identifier.as_deref()),
state: Mutex::new(ManagerState::Unknown),
state_waiters: Mutex::new(Vec::new()),
advertising_waiters: Mutex::new(Vec::new()),
service_waiters: Mutex::new(HashMap::new()),
ready_waiters: Mutex::new(Vec::new()),
published: Mutex::new(HashMap::new()),
requests: Mutex::new(Some(tx)),
l2cap_publish_waiters: Mutex::new(Vec::new()),
l2cap_unpublish_waiters: Mutex::new(Vec::new()),
restored: Mutex::new(None),
}
});
let actual = inner.host.state();
if actual != ManagerState::Unknown {
*inner.state.lock().unwrap() = actual;
}
(Self { inner }, Requests { rx })
}
pub async fn availability(&self) -> std::result::Result<(), Availability> {
match self.inner.require_powered_on().await {
Ok(()) => Ok(()),
Err(Error::NotAvailable(a)) => Err(a),
Err(_) => Err(Availability::Unknown),
}
}
pub async fn publish(&self, service: Service) -> Result<PublishedService> {
service.validate()?;
for c in &service.characteristics {
sys::validate_characteristic(
crate::Properties(c.properties),
crate::Permissions(c.permissions),
c.value.as_deref(),
)
.map_err(|e| Error::NotSupported(format!("characteristic {}: {e}", c.uuid)))?;
}
self.inner.require_powered_on().await?;
let service_uuid = service.uuid;
let mut characteristic_handles: Vec<(BluetoothUuid, Retained)> = Vec::new();
let mut descriptor_keepalive: Vec<Retained> = Vec::new();
let cb_service = unsafe {
let uuid = cb::uuid_from_string(service_uuid.as_str())
.ok_or_else(|| Error::Security(format!("invalid service UUID {service_uuid}")))?;
sys::mutable_service(uuid.as_ptr(), service.primary)
.ok_or_else(|| Error::Aborted("could not allocate CBMutableService".into()))?
};
for definition in &service.characteristics {
let handle = unsafe {
let uuid = cb::uuid_from_string(definition.uuid.as_str()).ok_or_else(|| {
Error::Security(format!("invalid characteristic UUID {}", definition.uuid))
})?;
let c = sys::mutable_characteristic(
uuid.as_ptr(),
crate::Properties(definition.properties),
definition.value.as_deref(),
crate::Permissions(definition.permissions),
)
.ok_or_else(|| {
Error::Aborted("could not allocate CBMutableCharacteristic".into())
})?;
if !definition.descriptors.is_empty() {
let mut built = Vec::new();
for d in &definition.descriptors {
let duuid = cb::uuid_from_string(d.uuid.as_str()).ok_or_else(|| {
Error::Security(format!("invalid descriptor UUID {}", d.uuid))
})?;
let handle = sys::mutable_descriptor(duuid.as_ptr(), &d.value, d.is_string)
.ok_or_else(|| {
Error::Aborted("could not allocate CBMutableDescriptor".into())
})?;
built.push(handle);
}
let ptrs: Vec<_> = built.iter().map(|d| d.as_ptr()).collect();
sys::characteristic_set_descriptors(c.as_ptr(), &ptrs);
descriptor_keepalive.extend(built);
}
c
};
characteristic_handles.push((definition.uuid, handle));
}
let ptrs: Vec<_> = characteristic_handles
.iter()
.map(|(_, h)| h.as_ptr())
.collect();
unsafe { sys::service_set_characteristics(cb_service.as_ptr(), &ptrs) };
let rx = {
let (tx, rx) = oneshot::channel();
self.inner
.service_waiters
.lock()
.unwrap()
.entry(cb_service.key())
.or_default()
.push(tx);
rx
};
unsafe { sys::add_service(self.inner.host.as_ptr(), cb_service.as_ptr()) };
rx.await
.map_err(|_| Error::Aborted("publishing was cancelled".into()))??;
{
let mut published = self.inner.published.lock().unwrap();
for (uuid, handle) in &characteristic_handles {
published.insert(handle.key(), *uuid);
}
}
Ok(PublishedService {
inner: self.inner.clone(),
uuid: service_uuid,
handle: cb_service,
characteristics: characteristic_handles
.into_iter()
.map(|(uuid, handle)| PublishedCharacteristic {
inner: self.inner.clone(),
uuid,
handle,
})
.collect(),
_descriptors: descriptor_keepalive,
})
}
pub fn unpublish_all(&self) {
unsafe { sys::remove_all_services(self.inner.host.as_ptr()) };
self.inner.published.lock().unwrap().clear();
}
pub async fn start_advertising(&self, advertising: Advertising) -> Result<()> {
self.inner.require_powered_on().await?;
let uuids: Vec<Retained> = advertising
.services
.iter()
.filter_map(|u| unsafe { cb::uuid_from_string(u.as_str()) })
.collect();
let ptrs: Vec<_> = uuids.iter().map(|u| u.as_ptr()).collect();
let rx = {
let (tx, rx) = oneshot::channel();
self.inner.advertising_waiters.lock().unwrap().push(tx);
rx
};
unsafe {
sys::start_advertising(
self.inner.host.as_ptr(),
advertising.local_name.as_deref(),
&ptrs,
)
};
rx.await
.map_err(|_| Error::Aborted("advertising was cancelled".into()))?
}
pub async fn publish_l2cap_channel(
&self,
encryption_required: bool,
) -> Result<webbluetooth_core::Psm> {
self.inner.require_powered_on().await?;
let rx = {
let (tx, rx) = oneshot::channel();
self.inner.l2cap_publish_waiters.lock().unwrap().push(tx);
rx
};
unsafe { sys::publish_l2cap_channel(self.inner.host.as_ptr(), encryption_required) };
rx.await
.map_err(|_| Error::Aborted("publishing the L2CAP channel was cancelled".into()))?
}
pub async fn unpublish_l2cap_channel(&self, psm: webbluetooth_core::Psm) -> Result<()> {
let rx = {
let (tx, rx) = oneshot::channel();
self.inner.l2cap_unpublish_waiters.lock().unwrap().push(tx);
rx
};
unsafe { sys::unpublish_l2cap_channel(self.inner.host.as_ptr(), psm) };
rx.await
.map_err(|_| Error::Aborted("unpublishing was cancelled".into()))?
}
pub async fn restored_state(&self) -> Option<RestoredPeripheral> {
let _ = self.inner.settled_state().await;
self.inner.restored.lock().unwrap().clone()
}
pub fn stop_advertising(&self) {
unsafe { sys::stop_advertising(self.inner.host.as_ptr()) };
}
pub fn is_advertising(&self) -> bool {
self.inner.host.is_advertising()
}
}
impl std::fmt::Debug for Peripheral {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Peripheral")
.field("state", &*self.inner.state.lock().unwrap())
.field("advertising", &self.is_advertising())
.finish()
}
}
pub struct PublishedService {
inner: Arc<Inner>,
uuid: BluetoothUuid,
handle: Retained,
characteristics: Vec<PublishedCharacteristic>,
_descriptors: Vec<Retained>,
}
impl PublishedService {
pub fn uuid(&self) -> &BluetoothUuid {
&self.uuid
}
pub fn characteristics(&self) -> Vec<PublishedCharacteristic> {
self.characteristics.to_vec()
}
pub fn characteristic(&self, uuid: impl IntoUuid) -> Option<PublishedCharacteristic> {
let uuid = uuid.into_uuid().ok()?;
self.characteristics
.iter()
.find(|c| c.uuid == uuid)
.cloned()
}
pub fn unpublish(self) {
unsafe { sys::remove_service(self.inner.host.as_ptr(), self.handle.as_ptr()) };
let mut published = self.inner.published.lock().unwrap();
for c in &self.characteristics {
published.remove(&c.handle.key());
}
}
}
impl std::fmt::Debug for PublishedService {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PublishedService")
.field("uuid", &self.uuid.as_str())
.field("characteristics", &self.characteristics.len())
.finish()
}
}
#[derive(Clone)]
pub struct PublishedCharacteristic {
inner: Arc<Inner>,
uuid: BluetoothUuid,
handle: Retained,
}
impl PublishedCharacteristic {
pub fn uuid(&self) -> &BluetoothUuid {
&self.uuid
}
pub fn subscribers(&self) -> Vec<RemoteCentral> {
unsafe { sys::subscribed_centrals(self.handle.as_ptr()) }
.into_iter()
.map(|handle| {
let id = unsafe { sys::central_identifier(handle.as_ptr()) }.unwrap_or_default();
RemoteCentral { handle, id }
})
.collect()
}
pub fn try_notify(&self, value: &[u8]) -> bool {
unsafe { sys::update_value(self.inner.host.as_ptr(), value, self.handle.as_ptr(), &[]) }
}
pub fn try_notify_centrals(&self, value: &[u8], centrals: &[RemoteCentral]) -> bool {
let ptrs: Vec<_> = centrals.iter().map(|c| c.handle.as_ptr()).collect();
unsafe { sys::update_value(self.inner.host.as_ptr(), value, self.handle.as_ptr(), &ptrs) }
}
pub async fn notify(&self, value: &[u8]) -> Result<()> {
if self.subscribers().is_empty() {
return Ok(());
}
loop {
if self.try_notify(value) {
return Ok(());
}
let rx = {
let (tx, rx) = oneshot::channel();
self.inner.ready_waiters.lock().unwrap().push(tx);
rx
};
rx.await
.map_err(|_| Error::Aborted("notification was cancelled".into()))?;
}
}
}
impl std::fmt::Debug for PublishedCharacteristic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PublishedCharacteristic")
.field("uuid", &self.uuid.as_str())
.field("subscribers", &self.subscribers().len())
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use futures_executor::block_on;
use webbluetooth_core::gatt::CharacteristicProperties as Properties;
use webbluetooth_core::peripheral::{Characteristic, Descriptor, Permissions, Service};
use webbluetooth_core::uuid::{characteristics, services};
#[test]
#[cfg_attr(miri, ignore = "calls into the Objective-C runtime")]
fn a_fixed_value_cannot_also_be_writeable() {
let service = Service::new(services::BATTERY_SERVICE)
.unwrap()
.characteristic(
Characteristic::new(characteristics::BATTERY_LEVEL)
.unwrap()
.read()
.write()
.value([50]),
);
let (peripheral, _events) = Peripheral::new();
let err = block_on(peripheral.publish(service)).unwrap_err();
assert!(matches!(err, Error::NotSupported(_)), "got {err:?}");
assert!(
err.to_string().contains("read-only"),
"unhelpful message: {err}"
);
}
#[test]
#[cfg_attr(miri, ignore = "calls into the Objective-C runtime")]
fn a_fixed_value_cannot_also_notify() {
let service = Service::new(services::BATTERY_SERVICE)
.unwrap()
.characteristic(
Characteristic::new(characteristics::BATTERY_LEVEL)
.unwrap()
.read()
.notify()
.value([50]),
);
let (peripheral, _events) = Peripheral::new();
assert!(block_on(peripheral.publish(service)).is_err());
}
#[test]
#[cfg_attr(miri, ignore = "calls into the Objective-C runtime")]
fn a_characteristic_needs_at_least_one_property() {
let service = Service::new(services::BATTERY_SERVICE)
.unwrap()
.characteristic(Characteristic::new(characteristics::BATTERY_LEVEL).unwrap());
let (peripheral, _events) = Peripheral::new();
let err = block_on(peripheral.publish(service)).unwrap_err();
assert!(err.to_string().contains("no properties"), "got {err}");
}
#[test]
#[cfg_attr(miri, ignore = "calls into the Objective-C runtime")]
fn a_service_needs_at_least_one_characteristic() {
let (peripheral, _events) = Peripheral::new();
let err = block_on(peripheral.publish(Service::new(services::BATTERY_SERVICE).unwrap()))
.unwrap_err();
assert!(err.to_string().contains("no characteristics"), "got {err}");
}
#[test]
#[cfg_attr(miri, ignore = "calls into the Objective-C runtime")]
fn the_cccd_cannot_be_declared_by_hand() {
let service = Service::new(services::BATTERY_SERVICE)
.unwrap()
.characteristic(
Characteristic::new(characteristics::BATTERY_LEVEL)
.unwrap()
.read()
.notify()
.descriptor(Descriptor {
uuid: BluetoothUuid::from_u16(0x2902),
value: vec![0, 0],
is_string: false,
}),
);
let (peripheral, _events) = Peripheral::new();
let err = block_on(peripheral.publish(service)).unwrap_err();
assert!(err.to_string().contains("automatically"), "got {err}");
}
#[test]
fn a_user_description_descriptor_is_accepted() {
let d = Descriptor::user_description("Battery level");
assert!(d.validate().is_ok());
assert!(d.is_string);
}
#[test]
fn definitions_build_the_expected_bits() {
let c = Characteristic::new(characteristics::BATTERY_LEVEL)
.unwrap()
.read()
.notify();
assert!(Properties(c.properties).read());
assert!(Properties(c.properties).notify());
assert!(!Properties(c.properties).write());
assert!(Permissions(c.permissions).readable());
assert!(!Permissions(c.permissions).any_write());
let w = Characteristic::new(characteristics::HEART_RATE_CONTROL_POINT)
.unwrap()
.write()
.require_encryption();
assert!(Permissions(w.permissions).has(Permissions::WRITE_ENCRYPTION_REQUIRED));
}
#[test]
#[cfg_attr(miri, ignore = "calls into the Objective-C runtime")]
fn the_manager_reports_a_state() {
let (peripheral, _events) = Peripheral::new();
let availability = block_on(peripheral.availability());
assert!(!matches!(availability, Err(Availability::Unknown)));
assert!(!peripheral.is_advertising());
}
}