1use bt_hci::param::CmdMask;
7use serde::Serialize;
8
9pub mod le_feature {
11 pub const ENCRYPTION: u32 = 0;
13 pub const CONN_PARAMS_REQUEST: u32 = 1;
15 pub const PING: u32 = 4;
17 pub const DATA_LENGTH_EXTENSION: u32 = 5;
19 pub const LL_PRIVACY: u32 = 6;
21 pub const EXT_SCANNER_FILTER_POLICIES: u32 = 7;
23 pub const PHY_2M: u32 = 8;
25 pub const PHY_CODED: u32 = 11;
27 pub const EXTENDED_ADVERTISING: u32 = 12;
29 pub const PERIODIC_ADVERTISING: u32 = 13;
31 pub const CSA2: u32 = 14;
33 pub const CONN_CTE_REQUEST: u32 = 17;
35 pub const CONN_CTE_RESPONSE: u32 = 18;
37 pub const CONNLESS_CTE_TX: u32 = 19;
39 pub const CONNLESS_CTE_RX: u32 = 20;
41 pub const AOD: u32 = 21;
43 pub const AOA: u32 = 22;
45 pub const CTE_RX: u32 = 23;
47 pub const PAST_RECIPIENT: u32 = 25;
49 pub const ISO_BROADCASTER: u32 = 30;
51 pub const SYNC_RECEIVER: u32 = 31;
53 pub const POWER_CONTROL: u32 = 33;
55 pub const SUBRATING: u32 = 37;
57 pub const CHANNEL_SOUNDING: u32 = 46;
59
60 pub const NAMES: &[(u32, &str)] = &[
62 (0, "le_encryption"),
63 (1, "connection_parameters_request"),
64 (2, "extended_reject_indication"),
65 (3, "peripheral_initiated_features_exchange"),
66 (4, "le_ping"),
67 (5, "data_length_extension"),
68 (6, "ll_privacy"),
69 (7, "extended_scanner_filter_policies"),
70 (8, "le_2m_phy"),
71 (9, "stable_modulation_index_tx"),
72 (10, "stable_modulation_index_rx"),
73 (11, "le_coded_phy"),
74 (12, "extended_advertising"),
75 (13, "periodic_advertising"),
76 (14, "channel_selection_algorithm_2"),
77 (15, "power_class_1"),
78 (16, "minimum_number_of_used_channels"),
79 (17, "connection_cte_request"),
80 (18, "connection_cte_response"),
81 (19, "connectionless_cte_transmitter"),
82 (20, "connectionless_cte_receiver"),
83 (21, "antenna_switching_aod"),
84 (22, "antenna_switching_aoa"),
85 (23, "receiving_cte"),
86 (24, "periodic_advertising_sync_transfer_sender"),
87 (25, "periodic_advertising_sync_transfer_recipient"),
88 (26, "sleep_clock_accuracy_updates"),
89 (27, "remote_public_key_validation"),
90 (28, "cis_central"),
91 (29, "cis_peripheral"),
92 (30, "isochronous_broadcaster"),
93 (31, "synchronized_receiver"),
94 (32, "cis_host_support"),
95 (33, "power_control_request"),
96 (34, "power_control_request_2"),
97 (35, "path_loss_monitoring"),
98 (36, "periodic_advertising_adi"),
99 (37, "connection_subrating"),
100 (38, "connection_subrating_host_support"),
101 (39, "channel_classification"),
102 (40, "advertising_coding_selection"),
103 (41, "advertising_coding_selection_host_support"),
104 (43, "periodic_advertising_with_responses_advertiser"),
105 (44, "periodic_advertising_with_responses_scanner"),
106 (46, "channel_sounding"),
107 (47, "channel_sounding_host_support"),
108 ];
109}
110
111#[derive(Debug, Clone, Serialize)]
113pub struct Capabilities {
114 #[serde(serialize_with = "ser_addr")]
117 pub public_address: [u8; 6],
118 pub hci_version: u8,
120 pub hci_subversion: u16,
122 pub lmp_version: u8,
124 pub lmp_subversion: u16,
126 pub manufacturer: u16,
128 #[serde(skip)]
130 pub commands: [u8; 64],
131 pub lmp_features: u64,
133 pub le_features: u64,
135 pub le_states: u64,
137 pub le_acl: (u16, u16),
139 pub iso: (u16, u8),
141 pub max_adv_data: u16,
143 pub adv_sets: u8,
145 pub filter_accept_list_size: u8,
147 pub resolving_list_size: u8,
149 pub max_data_length: (u16, u16, u16, u16),
151}
152
153fn ser_addr<S: serde::Serializer>(a: &[u8; 6], s: S) -> Result<S::Ok, S::Error> {
154 s.collect_str(&crate::Address::public(*a))
155}
156
157impl Capabilities {
158 pub fn cmds(&self) -> CmdMask {
160 use bt_hci::FromHciBytes;
162 <&CmdMask>::from_hci_bytes(&self.commands)
163 .map(|(m, _)| *m)
164 .unwrap_or_default()
165 }
166
167 pub fn le(&self, bit: u32) -> bool {
169 bit < 64 && self.le_features & (1 << bit) != 0
170 }
171
172 pub fn extended_advertising(&self) -> bool {
177 self.le(le_feature::EXTENDED_ADVERTISING)
178 }
179
180 pub fn extended_scanning(&self) -> bool {
182 self.extended_advertising()
183 }
184
185 pub fn periodic_advertising(&self) -> bool {
187 self.le(le_feature::PERIODIC_ADVERTISING)
188 }
189
190 pub fn coded_phy(&self) -> bool {
192 self.le(le_feature::PHY_CODED)
193 }
194
195 pub fn phy_2m(&self) -> bool {
197 self.le(le_feature::PHY_2M)
198 }
199
200 pub fn data_length_extension(&self) -> bool {
202 self.le(le_feature::DATA_LENGTH_EXTENSION) && self.cmds().le_set_data_length()
203 }
204
205 pub fn phy_update(&self) -> bool {
207 (self.phy_2m() || self.coded_phy()) && self.cmds().le_set_phy()
208 }
209
210 pub fn le_feature_names(&self) -> Vec<&'static str> {
212 le_feature::NAMES
213 .iter()
214 .filter(|(b, _)| self.le(*b))
215 .map(|(_, n)| *n)
216 .collect()
217 }
218
219 pub fn manufacturer_name(&self) -> Option<&'static str> {
221 bluetooth_numbers::company(self.manufacturer)
222 }
223
224 pub fn version_name(&self) -> Option<&'static str> {
226 bluetooth_numbers::core_version(self.hci_version)
227 }
228}