1 /******************************************************************************
2 *
3 * Copyright 1999-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #ifndef BTM_BLE_INT_TYPES_H
20 #define BTM_BLE_INT_TYPES_H
21
22 #include "osi/include/alarm.h"
23 #include "stack/btm/neighbor_inquiry.h"
24 #include "stack/include/btm_ble_api_types.h"
25 #include "types/raw_address.h"
26
27 /* scanning enable status */
28 #define BTM_BLE_SCAN_ENABLE 0x01
29 #define BTM_BLE_SCAN_DISABLE 0x00
30
31 /* advertising enable status */
32 #define BTM_BLE_ADV_ENABLE 0x01
33 #define BTM_BLE_ADV_DISABLE 0x00
34
35 #define BTM_BLE_AD_DATA_LEN 31
36
37 #define BTM_BLE_DUPLICATE_ENABLE 1
38 #define BTM_BLE_DUPLICATE_DISABLE 0
39
40 /* Interval(scan_int) = 11.25 ms= 0x0010 * 0.625 ms */
41 #define BTM_BLE_GAP_DISC_SCAN_INT 18
42 /* scan_window = 11.25 ms= 0x0010 * 0.625 ms */
43 #define BTM_BLE_GAP_DISC_SCAN_WIN 18
44 /* Tgap(gen_disc) = 1.28 s= 512 * 0.625 ms */
45 #define BTM_BLE_GAP_ADV_INT 512
46 /* Tgap(lim_timeout) = 180s max */
47 #define BTM_BLE_GAP_LIM_TIMEOUT_MS (180 * 1000)
48 /* Interval(scan_int) = 5s= 8000 * 0.625 ms */
49 #define BTM_BLE_LOW_LATENCY_SCAN_INT 8000
50 /* scan_window = 5s= 8000 * 0.625 ms */
51 #define BTM_BLE_LOW_LATENCY_SCAN_WIN 8000
52
53 /* TGAP(adv_fast_interval1) = 30(used) ~ 60 ms = 48 *0.625 */
54 #define BTM_BLE_GAP_ADV_FAST_INT_1 48
55 /* TGAP(adv_fast_interval2) = 100(used) ~ 150 ms = 160 * 0.625 ms */
56 #define BTM_BLE_GAP_ADV_FAST_INT_2 160
57 /* Tgap(adv_slow_interval) = 1.28 s= 512 * 0.625 ms */
58 #define BTM_BLE_GAP_ADV_SLOW_INT 2048
59 /* Tgap(dir_conn_adv_int_max) = 500 ms = 800 * 0.625 ms */
60 #define BTM_BLE_GAP_ADV_DIR_MAX_INT 800
61 /* Tgap(dir_conn_adv_int_min) = 250 ms = 400 * 0.625 ms */
62 #define BTM_BLE_GAP_ADV_DIR_MIN_INT 400
63
64 #define BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS (30 * 1000)
65
66 typedef enum : uint8_t {
67 BTM_BLE_SEC_REQ_ACT_NONE = 0,
68 /* encrypt the link using current key or key refresh */
69 BTM_BLE_SEC_REQ_ACT_ENCRYPT = 1,
70 BTM_BLE_SEC_REQ_ACT_PAIR = 2,
71 /* discard the sec request while encryption is started but not completed */
72 BTM_BLE_SEC_REQ_ACT_DISCARD = 3,
73 } tBTM_BLE_SEC_REQ_ACT;
74
75 #ifndef CASE_RETURN_TEXT
76 #define CASE_RETURN_TEXT(code) \
77 case code: \
78 return #code
79 #endif
80
btm_ble_sec_req_act_text(const tBTM_BLE_SEC_REQ_ACT & action)81 inline std::string btm_ble_sec_req_act_text(
82 const tBTM_BLE_SEC_REQ_ACT& action) {
83 switch (action) {
84 CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_NONE);
85 CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_ENCRYPT);
86 CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_PAIR);
87 CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_DISCARD);
88 }
89 }
90
91 #undef CASE_RETURN_TEXT
92
93 #define BTM_VSC_CHIP_CAPABILITY_L_VERSION 55
94 #define BTM_VSC_CHIP_CAPABILITY_M_VERSION 95
95 #define BTM_VSC_CHIP_CAPABILITY_S_VERSION 98
96
97 typedef struct {
98 uint16_t data_mask;
99 uint8_t* p_flags;
100 uint8_t ad_data[BTM_BLE_AD_DATA_LEN];
101 uint8_t* p_pad;
102 } tBTM_BLE_LOCAL_ADV_DATA;
103
104 #define BTM_BLE_ISVALID_PARAM(x, min, max) \
105 (((x) >= (min) && (x) <= (max)) || ((x) == BTM_BLE_CONN_PARAM_UNDEF))
106
107 typedef struct {
108 uint16_t discoverable_mode;
109 uint16_t connectable_mode;
110 uint32_t scan_window;
111 uint32_t scan_interval;
112 uint8_t scan_type; /* current scan type: active or passive */
113
114 tBTM_BLE_AFP afp; /* advertising filter policy */
115 tBTM_BLE_SFP sfp; /* scanning filter policy */
116
117 tBLE_ADDR_TYPE adv_addr_type;
118 uint8_t evt_type;
119
120 uint8_t adv_mode;
enable_advertising_mode__anon90b894ba0308121 void enable_advertising_mode() { adv_mode = BTM_BLE_ADV_ENABLE; }
disable_advertising_mode__anon90b894ba0308122 void disable_advertising_mode() { adv_mode = BTM_BLE_ADV_DISABLE; }
is_advertising_mode_enabled__anon90b894ba0308123 bool is_advertising_mode_enabled() const {
124 return (adv_mode == BTM_BLE_ADV_ENABLE);
125 }
126
127 tBLE_BD_ADDR direct_bda;
128 tBTM_BLE_EVT directed_conn;
129 bool fast_adv_on;
130 alarm_t* fast_adv_timer;
131
132 /* inquiry BD addr database */
133 tBTM_BLE_LOCAL_ADV_DATA adv_data;
134 tBTM_BLE_ADV_CHNL_MAP adv_chnl_map;
135
136 alarm_t* inquiry_timer;
137 bool scan_rsp;
138 uint8_t state; /* Current state that the inquiry process is in */
139 } tBTM_BLE_INQ_CB;
140
141 /* random address resolving complete callback */
142 typedef void(tBTM_BLE_RESOLVE_CBACK)(void* match_rec, void* p);
143
144 typedef void(tBTM_BLE_ADDR_CBACK)(const RawAddress& static_random, void* p);
145
146 /* random address management control block */
147 typedef struct {
148 tBLE_ADDR_TYPE own_addr_type; /* local device LE address type */
149 RawAddress private_addr;
150 alarm_t* refresh_raddr_timer;
151 } tBTM_LE_RANDOM_CB;
152
153 /* acceptlist using state as a bit mask */
154 constexpr uint8_t BTM_BLE_WL_IDLE = 0;
155 constexpr uint8_t BTM_BLE_ACCEPTLIST_INIT = 1;
156
157 /* resolving list using state as a bit mask */
158 enum : uint8_t {
159 BTM_BLE_RL_IDLE = 0,
160 BTM_BLE_RL_INIT = (1 << 0),
161 BTM_BLE_RL_SCAN = (1 << 1),
162 BTM_BLE_RL_ADV = (1 << 2),
163 };
164 typedef uint8_t tBTM_BLE_RL_STATE;
165
166 typedef struct { void* p_param; } tBTM_BLE_CONN_REQ;
167
168 /* LE state request */
169 #define BTM_BLE_STATE_INVALID 0
170 #define BTM_BLE_STATE_INIT 2
171 #define BTM_BLE_STATE_MAX 11
172
173 #define BTM_BLE_STATE_CONN_ADV_BIT 0x0001
174 #define BTM_BLE_STATE_INIT_BIT 0x0002
175 #define BTM_BLE_STATE_CENTRAL_BIT 0x0004
176 #define BTM_BLE_STATE_PERIPHERAL_BIT 0x0008
177 #define BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT 0x0010
178 #define BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT 0x0020
179 #define BTM_BLE_STATE_NON_CONN_ADV_BIT 0x0040
180 #define BTM_BLE_STATE_PASSIVE_SCAN_BIT 0x0080
181 #define BTM_BLE_STATE_ACTIVE_SCAN_BIT 0x0100
182 #define BTM_BLE_STATE_SCAN_ADV_BIT 0x0200
183 typedef uint16_t tBTM_BLE_STATE_MASK;
184
185 #define BTM_BLE_STATE_ALL_MASK 0x03ff
186 #define BTM_BLE_STATE_ALL_ADV_MASK \
187 (BTM_BLE_STATE_CONN_ADV_BIT | BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT | \
188 BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT | BTM_BLE_STATE_SCAN_ADV_BIT)
189 #define BTM_BLE_STATE_ALL_CONN_MASK \
190 (BTM_BLE_STATE_CENTRAL_BIT | BTM_BLE_STATE_PERIPHERAL_BIT)
191
192 typedef struct {
193 RawAddress* resolve_q_random_pseudo{nullptr};
194 uint8_t* resolve_q_action{nullptr};
195 uint8_t q_next;
196 uint8_t q_pending;
197 } tBTM_BLE_RESOLVE_Q;
198
199 /* BLE privacy mode */
200 #define BTM_PRIVACY_NONE 0 /* BLE no privacy */
201 #define BTM_PRIVACY_1_1 1 /* BLE privacy 1.1, do not support privacy 1.0 */
202 #define BTM_PRIVACY_1_2 2 /* BLE privacy 1.2 */
203 #define BTM_PRIVACY_MIXED \
204 3 /* BLE privacy mixed mode, broadcom propietary mode */
205 typedef uint8_t tBTM_PRIVACY_MODE;
206
207 /* Define BLE Device Management control structure
208 */
209 constexpr uint8_t kBTM_BLE_INQUIRY_ACTIVE = 0x10;
210 constexpr uint8_t kBTM_BLE_OBSERVE_ACTIVE = 0x80;
211 constexpr size_t kCentralAndPeripheralCount = 2;
212
213 typedef struct {
214 private:
215 uint8_t scan_activity_; /* LE scan activity mask */
216
217 public:
is_ble_inquiry_active__anon90b894ba0808218 bool is_ble_inquiry_active() const {
219 return (scan_activity_ & kBTM_BLE_INQUIRY_ACTIVE);
220 }
is_ble_observe_active__anon90b894ba0808221 bool is_ble_observe_active() const {
222 return (scan_activity_ & kBTM_BLE_OBSERVE_ACTIVE);
223 }
224
set_ble_inquiry_active__anon90b894ba0808225 void set_ble_inquiry_active() { scan_activity_ |= kBTM_BLE_INQUIRY_ACTIVE; }
set_ble_observe_active__anon90b894ba0808226 void set_ble_observe_active() { scan_activity_ |= kBTM_BLE_OBSERVE_ACTIVE; }
227
reset_ble_inquiry__anon90b894ba0808228 void reset_ble_inquiry() { scan_activity_ &= ~kBTM_BLE_INQUIRY_ACTIVE; }
reset_ble_observe__anon90b894ba0808229 void reset_ble_observe() { scan_activity_ &= ~kBTM_BLE_OBSERVE_ACTIVE; }
230
is_ble_scan_active__anon90b894ba0808231 bool is_ble_scan_active() const {
232 return (is_ble_inquiry_active() || is_ble_observe_active());
233 }
234
235 /*****************************************************
236 ** BLE Inquiry
237 *****************************************************/
238 tBTM_BLE_INQ_CB inq_var;
239
240 /* observer callback and timer */
241 tBTM_INQ_RESULTS_CB* p_obs_results_cb;
242 tBTM_CMPL_CB* p_obs_cmpl_cb;
243 alarm_t* observer_timer;
244
245 /* opportunistic observer */
246 tBTM_INQ_RESULTS_CB* p_opportunistic_obs_results_cb;
247
248 /* target announcement observer */
249 tBTM_INQ_RESULTS_CB* p_target_announcement_obs_results_cb;
250
251 /* background connection procedure cb value */
252 uint16_t scan_int;
253 uint16_t scan_win;
254
255 /* acceptlist information */
256 uint8_t wl_state;
set_acceptlist_process_in_progress__anon90b894ba0808257 void set_acceptlist_process_in_progress() {
258 wl_state |= BTM_BLE_ACCEPTLIST_INIT;
259 }
reset_acceptlist_process_in_progress__anon90b894ba0808260 void reset_acceptlist_process_in_progress() {
261 wl_state &= ~BTM_BLE_ACCEPTLIST_INIT;
262 }
is_acceptlist_in_progress__anon90b894ba0808263 bool is_acceptlist_in_progress() const {
264 return wl_state & BTM_BLE_ACCEPTLIST_INIT;
265 }
266
267 private:
268 enum : uint8_t { /* BLE connection state */
269 BLE_CONN_IDLE = 0,
270 BLE_CONNECTING = 2,
271 BLE_CONN_CANCEL = 3,
272 } conn_state_{BLE_CONN_IDLE};
273
274 public:
is_connection_state_idle__anon90b894ba0808275 bool is_connection_state_idle() const { return conn_state_ == BLE_CONN_IDLE; }
is_connection_state_connecting__anon90b894ba0808276 bool is_connection_state_connecting() const {
277 return conn_state_ == BLE_CONNECTING;
278 }
is_connection_state_cancelled__anon90b894ba0808279 bool is_connection_state_cancelled() const {
280 return conn_state_ == BLE_CONN_CANCEL;
281 }
set_connection_state_idle__anon90b894ba0808282 void set_connection_state_idle() { conn_state_ = BLE_CONN_IDLE; }
set_connection_state_connecting__anon90b894ba0808283 void set_connection_state_connecting() { conn_state_ = BLE_CONNECTING; }
set_connection_state_cancelled__anon90b894ba0808284 void set_connection_state_cancelled() { conn_state_ = BLE_CONN_CANCEL; }
285
286 /* random address management control block */
287 tBTM_LE_RANDOM_CB addr_mgnt_cb;
288
289 tBTM_PRIVACY_MODE privacy_mode; /* privacy mode */
290 uint8_t resolving_list_avail_size; /* resolving list available size */
291 tBTM_BLE_RESOLVE_Q resolving_list_pend_q; /* Resolving list queue */
292 tBTM_BLE_RL_STATE suspended_rl_state; /* Suspended resolving list state */
293 /* IRK list availability mask, up to max entry bits */
294 uint8_t* irk_list_mask{nullptr};
295 tBTM_BLE_RL_STATE rl_state; /* Resolving list state */
296
297 /* current BLE link state */
298 tBTM_BLE_STATE_MASK cur_states; /* bit mask of tBTM_BLE_STATE */
299
300 uint8_t link_count[kCentralAndPeripheralCount]; /* total link count central
301 and peripheral*/
302 } tBTM_BLE_CB;
303
304 #endif // BTM_BLE_INT_TYPES_H
305