esp32_nimble/client/
ble_advertised_device.rs

1use crate::BLEAddress;
2use crate::enums::*;
3use esp_idf_svc::sys;
4
5#[cfg(esp_idf_bt_nimble_ext_adv)]
6#[repr(transparent)]
7#[derive(Clone, Copy)]
8pub struct BLEAdvertisedDevice(sys::ble_gap_ext_disc_desc);
9
10#[cfg(not(esp_idf_bt_nimble_ext_adv))]
11#[repr(transparent)]
12#[derive(Clone, Copy)]
13pub struct BLEAdvertisedDevice(sys::ble_gap_disc_desc);
14
15impl BLEAdvertisedDevice {
16    /// Get the address of the advertising device.
17    pub fn addr(&self) -> BLEAddress {
18        self.0.addr.into()
19    }
20
21    /// Get the advertisement type.
22    pub fn adv_type(&self) -> AdvType {
23        #[cfg(esp_idf_bt_nimble_ext_adv)]
24        {
25            if (self.0.props & (sys::BLE_HCI_ADV_LEGACY_MASK as u8)) != 0 {
26                AdvType::from_event_type(self.0.legacy_event_type)
27            } else {
28                AdvType::Extended(self.0.props)
29            }
30        }
31
32        #[cfg(not(esp_idf_bt_nimble_ext_adv))]
33        {
34            AdvType::from_event_type(self.0.event_type)
35        }
36    }
37
38    pub fn rssi(&self) -> i8 {
39        self.0.rssi
40    }
41
42    #[cfg(esp_idf_bt_nimble_ext_adv)]
43    /// Get the set ID of the extended advertisement.
44    pub fn sid(&self) -> u8 {
45        self.0.sid
46    }
47
48    #[cfg(esp_idf_bt_nimble_ext_adv)]
49    /// Get the primary PHY used by this advertisement.
50    pub fn prim_phy(&self) -> PrimPhy {
51        PrimPhy::try_from(self.0.prim_phy).unwrap()
52    }
53
54    #[cfg(esp_idf_bt_nimble_ext_adv)]
55    /// Get the secondary PHY used by this advertisement.
56    pub fn sec_phy(&self) -> Option<SecPhy> {
57        SecPhy::try_from(self.0.sec_phy).ok()
58    }
59
60    #[cfg(esp_idf_bt_nimble_ext_adv)]
61    /// Get the periodic interval of the advertisement.
62    pub fn periodic_itvl(&self) -> u16 {
63        self.0.periodic_adv_itvl
64    }
65}
66
67impl core::fmt::Debug for BLEAdvertisedDevice {
68    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
69        #[cfg(esp_idf_bt_nimble_ext_adv)]
70        {
71            f.debug_struct("BLEAdvertisedDevice")
72                .field("addr", &self.addr())
73                .field("adv_type", &self.adv_type())
74                .field("rssi", &self.rssi())
75                .field("sid", &self.sid())
76                .field("prim_phy", &self.prim_phy())
77                .field("sec_phy", &self.sec_phy())
78                .field("periodic_itvl", &self.periodic_itvl())
79                .finish()
80        }
81        #[cfg(not(esp_idf_bt_nimble_ext_adv))]
82        {
83            f.debug_struct("BLEAdvertisedDevice")
84                .field("addr", &self.addr())
85                .field("adv_type", &self.adv_type())
86                .field("rssi", &self.rssi())
87                .finish()
88        }
89    }
90}