esp32_nimble/server/
ble_conn_desc.rs

1use crate::{BLEAddress, BLEError, ble};
2use esp_idf_svc::sys as esp_idf_sys;
3use esp_idf_sys::ble_gap_conn_desc;
4
5#[repr(transparent)]
6pub struct BLEConnDesc(pub(crate) ble_gap_conn_desc);
7
8impl BLEConnDesc {
9    /// Gets the over-the-air address of the connected peer
10    #[inline]
11    pub fn address(&self) -> BLEAddress {
12        BLEAddress::from(self.0.peer_ota_addr)
13    }
14
15    /// Gets the ID address of the connected peer
16    #[inline]
17    pub fn id_address(&self) -> BLEAddress {
18        BLEAddress::from(self.0.peer_id_addr)
19    }
20
21    /// Gets the connection handle of the connected peer
22    #[inline]
23    pub fn conn_handle(&self) -> u16 {
24        self.0.conn_handle
25    }
26
27    /// Gets the connection interval for this connection (in 1.25ms units)
28    #[inline]
29    pub fn interval(&self) -> u16 {
30        self.0.conn_itvl
31    }
32
33    /// Gets the supervision timeout for this connection (in 10ms units)
34    #[inline]
35    pub fn timeout(&self) -> u16 {
36        self.0.supervision_timeout
37    }
38
39    /// Gets the allowable latency for this connection (unit = number of intervals)
40    #[inline]
41    pub fn latency(&self) -> u16 {
42        self.0.conn_latency
43    }
44
45    /// Gets the maximum transmission unit size for this connection (in bytes)
46    #[inline]
47    pub fn mtu(&self) -> u16 {
48        unsafe { esp_idf_sys::ble_att_mtu(self.0.conn_handle) }
49    }
50
51    /// Check if we are connected to a bonded peer
52    #[inline]
53    pub fn bonded(&self) -> bool {
54        self.0.sec_state.bonded() != 0
55    }
56
57    /// Check if the connection in encrypted
58    #[inline]
59    pub fn encrypted(&self) -> bool {
60        self.0.sec_state.encrypted() != 0
61    }
62
63    /// Check if the the connection has been authenticated
64    #[inline]
65    pub fn authenticated(&self) -> bool {
66        self.0.sec_state.authenticated() != 0
67    }
68
69    /// Gets the key size used to encrypt the connection
70    #[inline]
71    pub fn sec_key_size(&self) -> u32 {
72        self.0.sec_state.key_size()
73    }
74
75    /// Retrieves the most-recently measured RSSI.
76    /// A connection’s RSSI is updated whenever a data channel PDU is received.
77    pub fn get_rssi(&self) -> Result<i8, BLEError> {
78        let mut rssi: i8 = 0;
79        unsafe {
80            ble!(esp_idf_sys::ble_gap_conn_rssi(
81                self.0.conn_handle,
82                &mut rssi
83            ))?;
84        }
85        Ok(rssi)
86    }
87}
88
89impl core::fmt::Debug for BLEConnDesc {
90    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
91        f.debug_struct("BLEConnDesc")
92            .field("address", &self.address())
93            .field("bonded", &self.bonded())
94            .field("encrypted", &self.encrypted())
95            .field("authenticated", &self.authenticated())
96            .field("mtu", &self.mtu())
97            .finish()
98    }
99}