1use std::pin::Pin;
2use std::mem;
3use std::sync::{Arc, Mutex};
4
5use tokio::sync::broadcast;
6use tokio_stream::wrappers::BroadcastStream;
7use futures::{Stream, TryStreamExt};
8
9use super::ffi;
10use crate::peripheral::InnerPeripheral;
11use crate::peripheral::Peripheral;
12use crate::types::Error;
13
14#[derive(Clone)]
15pub enum ScanEvent {
16 Start,
17 Stop,
18 Found(Peripheral),
19 Updated(Peripheral),
20}
21
22pub struct InnerAdapter {
23 internal: cxx::UniquePtr<ffi::RustyAdapter>,
24 on_scan_event: broadcast::Sender<ScanEvent>,
25}
26
27impl InnerAdapter {
28 pub fn bluetooth_enabled() -> Result<bool, Error> {
29 ffi::RustyAdapter_bluetooth_enabled().map_err(Error::from_cxx_exception)
30 }
31
32 pub fn get_adapters() -> Result<Vec<Pin<Box<InnerAdapter>>>, Error> {
33 let mut raw_adapter_list = ffi::RustyAdapter_get_adapters().map_err(Error::from_cxx_exception)?;
34
35 let mut adapters = Vec::<Pin<Box<InnerAdapter>>>::new();
36 for adapter_wrapper in raw_adapter_list.iter_mut() {
37 adapters.push(InnerAdapter::new(adapter_wrapper));
38 }
39 Ok(adapters)
40 }
41
42 fn new(wrapper: &mut ffi::RustyAdapterWrapper) -> Pin<Box<Self>> {
43 let (event_sender, _) = broadcast::channel(128);
44
45 let this = Self {
46 internal: cxx::UniquePtr::<ffi::RustyAdapter>::null(),
47 on_scan_event: event_sender
48 };
49
50 let mut this_boxed = Box::pin(this);
51 wrapper.internal.link(this_boxed.as_mut()).unwrap();
52 mem::swap(&mut this_boxed.internal, &mut wrapper.internal);
53
54 return this_boxed;
55 }
56
57 pub fn identifier(&self) -> Result<String, Error> {
58 self.internal.identifier().map_err(Error::from_cxx_exception)
59 }
60
61 pub fn address(&self) -> Result<String, Error> {
62 self.internal.address().map_err(Error::from_cxx_exception)
63 }
64
65 pub fn scan_start(&self) -> Result<(), Error> {
66 self.internal.scan_start().map_err(Error::from_cxx_exception)
67 }
68
69 pub fn scan_stop(&self) -> Result<(), Error> {
70 self.internal.scan_stop().map_err(Error::from_cxx_exception)
71 }
72
73 pub fn scan_for(&self, timeout_ms: i32) -> Result<(), Error> {
74 self.internal.scan_for(timeout_ms).map_err(Error::from_cxx_exception)
75 }
76
77 pub fn scan_is_active(&self) -> Result<bool, Error> {
78 self.internal.scan_is_active().map_err(Error::from_cxx_exception)
79 }
80
81 pub fn scan_get_results(&self) -> Result<Vec<Peripheral>, Error> {
82 let mut raw_peripheral_list = self.internal.scan_get_results().map_err(Error::from_cxx_exception)?;
83
84 let mut peripherals = Vec::<Peripheral>::new();
85 for peripheral_wrapper in raw_peripheral_list.iter_mut() {
86 peripherals.push(InnerPeripheral::new(peripheral_wrapper).into());
87 }
88
89 return Ok(peripherals);
90 }
91
92 pub fn get_paired_peripherals(&self) -> Result<Vec<Peripheral>, Error> {
93 let mut raw_peripheral_list =
94 self.internal.get_paired_peripherals().map_err(Error::from_cxx_exception)?;
95
96 let mut peripherals = Vec::<Peripheral>::new();
97 for peripheral_wrapper in raw_peripheral_list.iter_mut() {
98 peripherals.push(InnerPeripheral::new(peripheral_wrapper).into());
99 }
100
101 return Ok(peripherals);
102 }
103
104 pub fn on_callback_scan_start(&self) {
105 let _ = self.on_scan_event.send(ScanEvent::Start);
107 }
108
109 pub fn on_callback_scan_stop(&self) {
110 let _ = self.on_scan_event.send(ScanEvent::Stop);
112 }
113
114 pub fn on_callback_scan_updated(&self, peripheral: &mut ffi::RustyPeripheralWrapper) {
115 let peripheral: Peripheral = InnerPeripheral::new(peripheral).into();
117 let _ = self.on_scan_event.send(ScanEvent::Updated(peripheral));
118 }
119
120 pub fn on_callback_scan_found(&self, peripheral: &mut ffi::RustyPeripheralWrapper) {
121 let peripheral: Peripheral = InnerPeripheral::new(peripheral).into();
123 let _ = self.on_scan_event.send(ScanEvent::Found(peripheral));
124 }
125}
126
127impl Drop for InnerAdapter {
128 fn drop(&mut self) {
129 self.internal.unlink().unwrap();
130 }
131}
132
133unsafe impl Sync for InnerAdapter {}
134unsafe impl Send for InnerAdapter {}
135
136#[derive(Clone)]
137pub struct Adapter {
138 inner: Arc<Mutex<Pin<Box<InnerAdapter>>>>,
139}
140
141impl Adapter {
142
143 pub fn bluetooth_enabled() -> Result<bool, Error> {
144 ffi::RustyAdapter_bluetooth_enabled().map_err(Error::from_cxx_exception)
145 }
146
147 pub fn get_adapters() -> Result<Vec<Adapter>, Error> {
148 let mut raw_adapter_list = ffi::RustyAdapter_get_adapters().map_err(Error::from_cxx_exception)?;
149
150 let mut adapters = Vec::<Adapter>::new();
151 for adapter_wrapper in raw_adapter_list.iter_mut() {
152 adapters.push(InnerAdapter::new(adapter_wrapper).into());
153 }
154
155 return Ok(adapters);
156 }
157
158 pub fn identifier(&self) -> Result<String, Error> {
159 self.inner.lock().unwrap().identifier()
160 }
161
162 pub fn address(&self) -> Result<String, Error> {
163 self.inner.lock().unwrap().address()
164 }
165
166 pub fn scan_start(&self) -> Result<(), Error> {
167 self.inner.lock().unwrap().scan_start()
168 }
169
170 pub fn scan_stop(&self) -> Result<(), Error> {
171 self.inner.lock().unwrap().scan_stop()
172 }
173
174 pub fn scan_for(&self, timeout_ms: i32) -> Result<(), Error> {
175 self.inner.lock().unwrap().scan_for(timeout_ms)
176 }
177
178 pub fn scan_is_active(&self) -> Result<bool, Error> {
179 self.inner.lock().unwrap().scan_is_active()
180 }
181
182 pub fn scan_get_results(&self) -> Result<Vec<Peripheral>, Error> {
183 self.inner.lock().unwrap().scan_get_results()
184 }
185
186 pub fn get_paired_peripherals(&self) -> Result<Vec<Peripheral>, Error> {
187 self.inner.lock().unwrap().get_paired_peripherals()
188 }
189
190 pub fn on_scan_event(&self) -> impl Stream<Item = Result<ScanEvent, Error>> {
191 BroadcastStream::new(self.inner.lock().unwrap().on_scan_event.subscribe())
192 .map_err(|e| Error::from_string(e.to_string()))
193 }
194
195}
196
197
198impl From<Pin<Box<InnerAdapter>>> for Adapter {
199 fn from(adapter: Pin<Box<InnerAdapter>>) -> Self {
200 Self {
201 inner: Arc::new(Mutex::new(adapter)),
202 }
203 }
204}
205
206unsafe impl Send for Adapter {}
207unsafe impl Sync for Adapter {}