ble_peripheral_rust/peripheral/corebluetooth/
mod.rs

1mod characteristic_utils;
2pub mod error;
3mod mac_extensions;
4mod mac_utils;
5pub mod peripheral_delegate;
6mod peripheral_manager;
7
8use crate::{
9    error::{Error, ErrorType},
10    gatt::{peripheral_event::PeripheralEvent, service::Service},
11};
12use async_trait::async_trait;
13use peripheral_manager::{is_authorized, run_peripheral_thread, ManagerEvent};
14use tokio::sync::{mpsc::Sender, oneshot};
15use uuid::Uuid;
16
17use super::PeripheralImpl;
18
19pub struct Peripheral {
20    manager_tx: Sender<ManagerEvent>,
21}
22
23#[async_trait]
24impl PeripheralImpl for Peripheral {
25    type Peripheral = Self;
26
27    async fn new(sender_tx: Sender<PeripheralEvent>) -> Result<Self, Error> {
28        if !is_authorized() {
29            return Err(Error::from_type(ErrorType::PermissionDenied));
30        }
31        let (manager_tx, manager_rx) = tokio::sync::mpsc::channel(256);
32        run_peripheral_thread(sender_tx, manager_rx);
33        Ok(Peripheral { manager_tx })
34    }
35
36    async fn is_powered(&mut self) -> Result<bool, Error> {
37        let (responder, responder_rx) = oneshot::channel();
38        self.manager_tx
39            .send(ManagerEvent::IsPowered { responder })
40            .await?;
41        return responder_rx.await?;
42    }
43
44    async fn is_advertising(&mut self) -> Result<bool, Error> {
45        let (responder, responder_rx) = oneshot::channel();
46        self.manager_tx
47            .send(ManagerEvent::IsAdvertising { responder })
48            .await?;
49        return responder_rx.await?;
50    }
51
52    async fn start_advertising(&mut self, name: &str, uuids: &[Uuid]) -> Result<(), Error> {
53        let (responder, responder_rx) = oneshot::channel();
54        self.manager_tx
55            .send(ManagerEvent::StartAdvertising {
56                name: name.to_string(),
57                uuids: uuids.to_vec(),
58                responder,
59            })
60            .await?;
61        return responder_rx.await?;
62    }
63
64    async fn stop_advertising(&mut self) -> Result<(), Error> {
65        let (responder, responder_rx) = oneshot::channel();
66        self.manager_tx
67            .send(ManagerEvent::StopAdvertising { responder })
68            .await?;
69        return responder_rx.await?;
70    }
71
72    async fn add_service(&mut self, service: &Service) -> Result<(), Error> {
73        let (responder, responder_rx) = oneshot::channel();
74        self.manager_tx
75            .send(ManagerEvent::AddService {
76                service: service.clone(),
77                responder,
78            })
79            .await?;
80        return responder_rx.await?;
81    }
82
83    async fn update_characteristic(
84        &mut self,
85        characteristic: Uuid,
86        value: Vec<u8>,
87    ) -> Result<(), Error> {
88        let (responder, responder_rx) = oneshot::channel();
89        self.manager_tx
90            .send(ManagerEvent::UpdateCharacteristic {
91                characteristic,
92                value,
93                responder,
94            })
95            .await?;
96        return responder_rx.await?;
97    }
98}