1use async_trait::async_trait;
2use bluez_async::{
3 BluetoothEvent, BluetoothSession, CharacteristicEvent, CharacteristicFlags, CharacteristicId,
4 CharacteristicInfo, DescriptorInfo, DeviceId, DeviceInfo, MacAddress, ServiceInfo,
5 WriteOptions,
6};
7use futures::future::{join_all, ready};
8use futures::stream::{Stream, StreamExt};
9#[cfg(feature = "serde")]
10use serde::{Deserialize, Serialize};
11#[cfg(feature = "serde")]
12use serde_cr as serde;
13use std::collections::{BTreeSet, HashMap};
14use std::fmt::{self, Display, Formatter};
15use std::pin::Pin;
16use std::sync::{Arc, Mutex};
17use uuid::Uuid;
18
19use crate::api::{
20 self, AddressType, BDAddr, CharPropFlags, Characteristic, Descriptor, PeripheralProperties,
21 Service, ValueNotification, WriteType,
22};
23use crate::{Error, Result};
24
25#[derive(Clone, Debug)]
26struct CharacteristicInternal {
27 info: CharacteristicInfo,
28 descriptors: HashMap<Uuid, DescriptorInfo>,
29}
30
31impl CharacteristicInternal {
32 fn new(info: CharacteristicInfo, descriptors: HashMap<Uuid, DescriptorInfo>) -> Self {
33 Self { info, descriptors }
34 }
35}
36
37#[derive(Clone, Debug)]
38struct ServiceInternal {
39 info: ServiceInfo,
40 characteristics: HashMap<Uuid, CharacteristicInternal>,
41}
42
43#[cfg_attr(
44 feature = "serde",
45 derive(Serialize, Deserialize),
46 serde(crate = "serde_cr")
47)]
48#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
49pub struct PeripheralId(pub(crate) DeviceId);
50
51impl Display for PeripheralId {
52 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
53 self.0.fmt(f)
54 }
55}
56
57#[derive(Clone, Debug)]
59pub struct Peripheral {
60 session: BluetoothSession,
61 device: DeviceId,
62 mac_address: BDAddr,
63 services: Arc<Mutex<HashMap<Uuid, ServiceInternal>>>,
64}
65
66fn get_characteristic<'a>(
67 services: &'a HashMap<Uuid, ServiceInternal>,
68 service_uuid: &Uuid,
69 characteristic_uuid: &Uuid,
70) -> Result<&'a CharacteristicInternal> {
71 services
72 .get(service_uuid)
73 .ok_or_else(|| {
74 Error::Other(format!("Service with UUID {} not found.", service_uuid).into())
75 })?
76 .characteristics
77 .get(characteristic_uuid)
78 .ok_or_else(|| {
79 Error::Other(
80 format!(
81 "Characteristic with UUID {} not found.",
82 characteristic_uuid
83 )
84 .into(),
85 )
86 })
87}
88
89impl Peripheral {
90 pub(crate) fn new(session: BluetoothSession, device: DeviceInfo) -> Self {
91 Peripheral {
92 session,
93 device: device.id,
94 mac_address: device.mac_address.into(),
95 services: Arc::new(Mutex::new(HashMap::new())),
96 }
97 }
98
99 fn characteristic_info(&self, characteristic: &Characteristic) -> Result<CharacteristicInfo> {
100 let services = self.services.lock().map_err(Into::<Error>::into)?;
101 get_characteristic(
102 &services,
103 &characteristic.service_uuid,
104 &characteristic.uuid,
105 )
106 .map(|c| &c.info)
107 .cloned()
108 }
109
110 fn descriptor_info(&self, descriptor: &Descriptor) -> Result<DescriptorInfo> {
111 let services = self.services.lock().map_err(Into::<Error>::into)?;
112 let characteristic = get_characteristic(
113 &services,
114 &descriptor.service_uuid,
115 &descriptor.characteristic_uuid,
116 )?;
117 characteristic
118 .descriptors
119 .get(&descriptor.uuid)
120 .ok_or_else(|| {
121 Error::Other(format!("Descriptor with UUID {} not found.", descriptor.uuid).into())
122 })
123 .cloned()
124 }
125
126 async fn device_info(&self) -> Result<DeviceInfo> {
127 Ok(self.session.get_device_info(&self.device).await?)
128 }
129}
130
131#[async_trait]
132impl api::Peripheral for Peripheral {
133 fn id(&self) -> PeripheralId {
134 PeripheralId(self.device.to_owned())
135 }
136
137 fn address(&self) -> BDAddr {
138 self.mac_address
139 }
140
141 async fn properties(&self) -> Result<Option<PeripheralProperties>> {
142 let device_info = self.device_info().await?;
143 Ok(Some(PeripheralProperties {
144 address: device_info.mac_address.into(),
145 address_type: Some(device_info.address_type.into()),
146 local_name: device_info.name,
147 tx_power_level: device_info.tx_power,
148 rssi: device_info.rssi,
149 manufacturer_data: device_info.manufacturer_data,
150 service_data: device_info.service_data,
151 services: device_info.services,
152 class: device_info.class,
153 }))
154 }
155
156 fn services(&self) -> BTreeSet<Service> {
157 self.services
158 .lock()
159 .unwrap()
160 .values()
161 .map(|service| service.into())
162 .collect()
163 }
164
165 async fn is_connected(&self) -> Result<bool> {
166 let device_info = self.device_info().await?;
167 Ok(device_info.connected)
168 }
169
170 async fn connect(&self) -> Result<()> {
171 self.session.connect(&self.device).await?;
172 Ok(())
173 }
174
175 async fn disconnect(&self) -> Result<()> {
176 self.session.disconnect(&self.device).await?;
177 Ok(())
178 }
179
180 async fn discover_services(&self) -> Result<()> {
181 let mut services_internal = HashMap::new();
182 let services = self.session.get_services(&self.device).await?;
183 for service in services {
184 let characteristics = self.session.get_characteristics(&service.id).await?;
185 let characteristics = join_all(
186 characteristics
187 .into_iter()
188 .fold(
189 HashMap::<Uuid, CharacteristicInfo>::new(),
192 |mut map, characteristic| {
193 if !map.contains_key(&characteristic.uuid) {
194 map.insert(characteristic.uuid, characteristic);
195 }
196 map
197 },
198 )
199 .into_iter()
200 .map(|mapped_characteristic| async {
201 let characteristic = mapped_characteristic.1;
202 let descriptors = self
203 .session
204 .get_descriptors(&characteristic.id)
205 .await
206 .unwrap_or(Vec::new())
207 .into_iter()
208 .map(|descriptor| (descriptor.uuid, descriptor))
209 .collect();
210 CharacteristicInternal::new(characteristic, descriptors)
211 }),
212 )
213 .await;
214 services_internal.insert(
215 service.uuid,
216 ServiceInternal {
217 info: service,
218 characteristics: characteristics
219 .into_iter()
220 .map(|characteristic| (characteristic.info.uuid, characteristic))
221 .collect(),
222 },
223 );
224 }
225 *(self.services.lock().map_err(Into::<Error>::into)?) = services_internal;
226 Ok(())
227 }
228
229 async fn write(
230 &self,
231 characteristic: &Characteristic,
232 data: &[u8],
233 write_type: WriteType,
234 ) -> Result<()> {
235 let characteristic_info = self.characteristic_info(characteristic)?;
236 let options = WriteOptions {
237 write_type: Some(write_type.into()),
238 ..Default::default()
239 };
240 Ok(self
241 .session
242 .write_characteristic_value_with_options(&characteristic_info.id, data, options)
243 .await?)
244 }
245
246 async fn read(&self, characteristic: &Characteristic) -> Result<Vec<u8>> {
247 let characteristic_info = self.characteristic_info(characteristic)?;
248 Ok(self
249 .session
250 .read_characteristic_value(&characteristic_info.id)
251 .await?)
252 }
253
254 async fn subscribe(&self, characteristic: &Characteristic) -> Result<()> {
255 let characteristic_info = self.characteristic_info(characteristic)?;
256 Ok(self.session.start_notify(&characteristic_info.id).await?)
257 }
258
259 async fn unsubscribe(&self, characteristic: &Characteristic) -> Result<()> {
260 let characteristic_info = self.characteristic_info(characteristic)?;
261 Ok(self.session.stop_notify(&characteristic_info.id).await?)
262 }
263
264 async fn notifications(&self) -> Result<Pin<Box<dyn Stream<Item = ValueNotification> + Send>>> {
265 let device_id = self.device.clone();
266 let events = self.session.device_event_stream(&device_id).await?;
267 let services = self.services.clone();
268 Ok(Box::pin(events.filter_map(move |event| {
269 ready(value_notification(event, &device_id, services.clone()))
270 })))
271 }
272
273 async fn write_descriptor(&self, descriptor: &Descriptor, data: &[u8]) -> Result<()> {
274 let descriptor_info = self.descriptor_info(descriptor)?;
275 Ok(self
276 .session
277 .write_descriptor_value(&descriptor_info.id, data)
278 .await?)
279 }
280
281 async fn read_descriptor(&self, descriptor: &Descriptor) -> Result<Vec<u8>> {
282 let descriptor_info = self.descriptor_info(descriptor)?;
283 Ok(self
284 .session
285 .read_descriptor_value(&descriptor_info.id)
286 .await?)
287 }
288}
289
290fn value_notification(
291 event: BluetoothEvent,
292 device_id: &DeviceId,
293 services: Arc<Mutex<HashMap<Uuid, ServiceInternal>>>,
294) -> Option<ValueNotification> {
295 match event {
296 BluetoothEvent::Characteristic {
297 id,
298 event: CharacteristicEvent::Value { value },
299 } if id.service().device() == *device_id => {
300 let services = services.lock().unwrap();
301 let uuid = find_characteristic_by_id(&services, id)?.uuid;
302 Some(ValueNotification { uuid, value })
303 }
304 _ => None,
305 }
306}
307
308fn find_characteristic_by_id(
309 services: &HashMap<Uuid, ServiceInternal>,
310 characteristic_id: CharacteristicId,
311) -> Option<&CharacteristicInfo> {
312 for service in services.values() {
313 for characteristic in service.characteristics.values() {
314 if characteristic.info.id == characteristic_id {
315 return Some(&characteristic.info);
316 }
317 }
318 }
319 None
320}
321
322impl From<WriteType> for bluez_async::WriteType {
323 fn from(write_type: WriteType) -> Self {
324 match write_type {
325 WriteType::WithoutResponse => bluez_async::WriteType::WithoutResponse,
326 WriteType::WithResponse => bluez_async::WriteType::WithResponse,
327 }
328 }
329}
330
331impl From<MacAddress> for BDAddr {
332 fn from(mac_address: MacAddress) -> Self {
333 <[u8; 6]>::into(mac_address.into())
334 }
335}
336
337impl From<DeviceId> for PeripheralId {
338 fn from(device_id: DeviceId) -> Self {
339 PeripheralId(device_id)
340 }
341}
342
343impl From<bluez_async::AddressType> for AddressType {
344 fn from(address_type: bluez_async::AddressType) -> Self {
345 match address_type {
346 bluez_async::AddressType::Public => AddressType::Public,
347 bluez_async::AddressType::Random => AddressType::Random,
348 }
349 }
350}
351
352fn make_descriptor(
353 info: &DescriptorInfo,
354 characteristic_uuid: Uuid,
355 service_uuid: Uuid,
356) -> Descriptor {
357 Descriptor {
358 uuid: info.uuid,
359 characteristic_uuid,
360 service_uuid,
361 }
362}
363
364fn make_characteristic(
365 characteristic: &CharacteristicInternal,
366 service_uuid: Uuid,
367) -> Characteristic {
368 let CharacteristicInternal { info, descriptors } = characteristic;
369 Characteristic {
370 uuid: info.uuid,
371 properties: info.flags.into(),
372 descriptors: descriptors
373 .iter()
374 .map(|(_, descriptor)| make_descriptor(descriptor, info.uuid, service_uuid))
375 .collect(),
376 service_uuid,
377 }
378}
379
380impl From<&ServiceInternal> for Service {
381 fn from(service: &ServiceInternal) -> Self {
382 Service {
383 uuid: service.info.uuid,
384 primary: service.info.primary,
385 characteristics: service
386 .characteristics
387 .values()
388 .map(|characteristic| make_characteristic(characteristic, service.info.uuid))
389 .collect(),
390 }
391 }
392}
393
394impl From<CharacteristicFlags> for CharPropFlags {
395 fn from(flags: CharacteristicFlags) -> Self {
396 let mut result = CharPropFlags::default();
397 if flags.contains(CharacteristicFlags::BROADCAST) {
398 result.insert(CharPropFlags::BROADCAST);
399 }
400 if flags.contains(CharacteristicFlags::READ) {
401 result.insert(CharPropFlags::READ);
402 }
403 if flags.contains(CharacteristicFlags::WRITE_WITHOUT_RESPONSE) {
404 result.insert(CharPropFlags::WRITE_WITHOUT_RESPONSE);
405 }
406 if flags.contains(CharacteristicFlags::WRITE) {
407 result.insert(CharPropFlags::WRITE);
408 }
409 if flags.contains(CharacteristicFlags::NOTIFY) {
410 result.insert(CharPropFlags::NOTIFY);
411 }
412 if flags.contains(CharacteristicFlags::INDICATE) {
413 result.insert(CharPropFlags::INDICATE);
414 }
415 if flags.contains(CharacteristicFlags::SIGNED_WRITE) {
416 result.insert(CharPropFlags::AUTHENTICATED_SIGNED_WRITES);
417 }
418 if flags.contains(CharacteristicFlags::EXTENDED_PROPERTIES) {
419 result.insert(CharPropFlags::EXTENDED_PROPERTIES);
420 }
421 result
422 }
423}