Skip to main content

android_ble/
types.rs

1#[allow(unused)]
2use crate::Adapter;
3use crate::Uuid;
4use std::collections::HashMap;
5
6/// A platform-specific device identifier.
7/// On Android it contains the Bluetooth address in the format `AB:CD:EF:01:23:45`.
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct DeviceId(pub(crate) String);
11
12impl std::fmt::Display for DeviceId {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        std::fmt::Display::fmt(&self.0, f)
15    }
16}
17
18/// Events generated by [`Adapter::events`].
19#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
20pub enum AdapterEvent {
21    /// The adapter has become available (powered on and ready to use)
22    Available,
23    /// The adapter has become unavailable (powered off or otherwise disabled)
24    Unavailable,
25}
26
27/// Events generated by [`Adapter::device_connection_events`].
28#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
29pub enum ConnectionEvent {
30    /// The device has disconnected from the host system
31    Disconnected,
32    /// The device has connected to the host system
33    Connected,
34}
35
36/// Represents a device discovered during a scan operation.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct AdvertisingDevice {
39    /// The source of the advertisement
40    pub device: crate::Device,
41    /// The advertisment data
42    pub adv_data: AdvertisementData,
43    /// The signal strength in dBm of the received advertisement packet
44    pub rssi: Option<i16>,
45}
46
47/// Data included in a Bluetooth advertisement or scan reponse.
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct AdvertisementData {
50    /// The (possibly shortened) local name of the device (CSS §A.1.2)
51    pub local_name: Option<String>,
52    /// Manufacturer specific data (CSS §A.1.4)
53    pub manufacturer_data: Option<ManufacturerData>,
54    /// Advertised GATT service UUIDs (CSS §A.1.1)
55    pub services: Vec<Uuid>,
56    /// Service associated data (CSS §A.1.11)
57    pub service_data: HashMap<Uuid, Vec<u8>>,
58    /// Transmitted power level (CSS §A.1.5)
59    pub tx_power_level: Option<i16>,
60    /// Set to true for connectable advertising packets
61    pub is_connectable: bool,
62}
63
64/// Manufacturer specific data included in Bluetooth advertisements.
65///
66/// See the Bluetooth Core Specification Supplement §A.1.4 for details.
67#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
68pub struct ManufacturerData {
69    /// Company identifier (defined [here](https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers/))
70    pub company_id: u16,
71    /// Manufacturer specific data
72    pub data: Vec<u8>,
73}
74
75/// GATT characteristic properties as defined in the Bluetooth Core Specification, Vol 3, Part G, §3.3.1.1.
76///
77/// Extended properties are also included as defined in §3.3.3.1.
78#[allow(missing_docs)]
79#[non_exhaustive]
80#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
81pub struct CharacteristicProperties {
82    pub broadcast: bool,
83    pub read: bool,
84    pub write_without_response: bool,
85    pub write: bool,
86    pub notify: bool,
87    pub indicate: bool,
88    pub authenticated_signed_writes: bool,
89    pub extended_properties: bool,
90    pub reliable_write: bool,
91    pub writable_auxiliaries: bool,
92}
93
94impl CharacteristicProperties {
95    /// Raw transmutation from [`u32`].
96    ///
97    /// Extended properties are in the upper bits.
98    pub fn from_bits(bits: u32) -> Self {
99        CharacteristicProperties {
100            broadcast: (bits & (1 << 0)) != 0,
101            read: (bits & (1 << 1)) != 0,
102            write_without_response: (bits & (1 << 2)) != 0,
103            write: (bits & (1 << 3)) != 0,
104            notify: (bits & (1 << 4)) != 0,
105            indicate: (bits & (1 << 5)) != 0,
106            authenticated_signed_writes: (bits & (1 << 6)) != 0,
107            extended_properties: (bits & (1 << 7)) != 0,
108            reliable_write: (bits & (1 << 8)) != 0,
109            writable_auxiliaries: (bits & (1 << 9)) != 0,
110        }
111    }
112
113    /// Raw transmutation to [`u32`].
114    ///
115    /// Extended properties are in the upper bits.
116    pub fn to_bits(self) -> u32 {
117        u32::from(self.broadcast)
118            | (u32::from(self.read) << 1)
119            | (u32::from(self.write_without_response) << 2)
120            | (u32::from(self.write) << 3)
121            | (u32::from(self.notify) << 4)
122            | (u32::from(self.indicate) << 5)
123            | (u32::from(self.authenticated_signed_writes) << 6)
124            | (u32::from(self.extended_properties) << 7)
125            | (u32::from(self.reliable_write) << 8)
126            | (u32::from(self.writable_auxiliaries) << 9)
127    }
128}