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 
26 /* scanning enable status */
27 #define BTM_BLE_SCAN_ENABLE 0x01
28 #define BTM_BLE_SCAN_DISABLE 0x00
29 
30 /* advertising enable status */
31 #define BTM_BLE_ADV_ENABLE 0x01
32 #define BTM_BLE_ADV_DISABLE 0x00
33 
34 #define BTM_BLE_AD_DATA_LEN 31
35 
36 #define BTM_BLE_DUPLICATE_ENABLE 1
37 #define BTM_BLE_DUPLICATE_DISABLE 0
38 
39 /* Interval(scan_int) = 11.25 ms= 0x0010 * 0.625 ms */
40 #define BTM_BLE_GAP_DISC_SCAN_INT 18
41 /* scan_window = 11.25 ms= 0x0010 * 0.625 ms */
42 #define BTM_BLE_GAP_DISC_SCAN_WIN 18
43 /* Tgap(gen_disc) = 1.28 s= 512 * 0.625 ms */
44 #define BTM_BLE_GAP_ADV_INT 512
45 /* Tgap(lim_timeout) = 180s max */
46 #define BTM_BLE_GAP_LIM_TIMEOUT_MS (180 * 1000)
47 /* Interval(scan_int) = 5s= 8000 * 0.625 ms */
48 #define BTM_BLE_LOW_LATENCY_SCAN_INT 8000
49 /* scan_window = 5s= 8000 * 0.625 ms */
50 #define BTM_BLE_LOW_LATENCY_SCAN_WIN 8000
51 
52 /* TGAP(adv_fast_interval1) = 30(used) ~ 60 ms  = 48 *0.625 */
53 #define BTM_BLE_GAP_ADV_FAST_INT_1 48
54 /* TGAP(adv_fast_interval2) = 100(used) ~ 150 ms = 160 * 0.625 ms */
55 #define BTM_BLE_GAP_ADV_FAST_INT_2 160
56 /* Tgap(adv_slow_interval) = 1.28 s= 512 * 0.625 ms */
57 #define BTM_BLE_GAP_ADV_SLOW_INT 2048
58 /* Tgap(dir_conn_adv_int_max) = 500 ms = 800 * 0.625 ms */
59 #define BTM_BLE_GAP_ADV_DIR_MAX_INT 800
60 /* Tgap(dir_conn_adv_int_min) = 250 ms = 400 * 0.625 ms */
61 #define BTM_BLE_GAP_ADV_DIR_MIN_INT 400
62 
63 #define BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS (30 * 1000)
64 
65 typedef enum : uint8_t {
66   BTM_BLE_SEC_REQ_ACT_NONE = 0,
67   /* encrypt the link using current key or key refresh */
68   BTM_BLE_SEC_REQ_ACT_ENCRYPT = 1,
69   BTM_BLE_SEC_REQ_ACT_PAIR = 2,
70   /* discard the sec request while encryption is started but not completed */
71   BTM_BLE_SEC_REQ_ACT_DISCARD = 3,
72 } tBTM_BLE_SEC_REQ_ACT;
73 
74 #define BTM_VSC_CHIP_CAPABILITY_L_VERSION 55
75 #define BTM_VSC_CHIP_CAPABILITY_M_VERSION 95
76 #define BTM_VSC_CHIP_CAPABILITY_S_VERSION 98
77 
78 typedef struct {
79   uint16_t data_mask;
80   uint8_t* p_flags;
81   uint8_t ad_data[BTM_BLE_AD_DATA_LEN];
82   uint8_t* p_pad;
83 } tBTM_BLE_LOCAL_ADV_DATA;
84 
85 #define BTM_BLE_ISVALID_PARAM(x, min, max) \
86   (((x) >= (min) && (x) <= (max)) || ((x) == BTM_BLE_CONN_PARAM_UNDEF))
87 
88 typedef struct {
89   uint16_t discoverable_mode;
90   uint16_t connectable_mode;
91   uint32_t scan_window;
92   uint32_t scan_interval;
93   uint8_t scan_type;             /* current scan type: active or passive */
94 
95   tBTM_BLE_AFP afp; /* advertising filter policy */
96   tBTM_BLE_SFP sfp; /* scanning filter policy */
97 
98   tBLE_ADDR_TYPE adv_addr_type;
99   uint8_t evt_type;
100 
101   uint8_t adv_mode;
enable_advertising_mode__anon5f6aadb80308102   void enable_advertising_mode() { adv_mode = BTM_BLE_ADV_ENABLE; }
disable_advertising_mode__anon5f6aadb80308103   void disable_advertising_mode() { adv_mode = BTM_BLE_ADV_DISABLE; }
is_advertising_mode_enabled__anon5f6aadb80308104   bool is_advertising_mode_enabled() const {
105     return (adv_mode == BTM_BLE_ADV_ENABLE);
106   }
107 
108   tBLE_BD_ADDR direct_bda;
109   tBTM_BLE_EVT directed_conn;
110   bool fast_adv_on;
111   alarm_t* fast_adv_timer;
112 
113   /* inquiry BD addr database */
114   tBTM_BLE_LOCAL_ADV_DATA adv_data;
115   tBTM_BLE_ADV_CHNL_MAP adv_chnl_map;
116 
117   alarm_t* inquiry_timer;
118   bool scan_rsp;
119   uint8_t state; /* Current state that the inquiry process is in */
120 } tBTM_BLE_INQ_CB;
121 
122 /* random address resolving complete callback */
123 typedef void(tBTM_BLE_RESOLVE_CBACK)(void* match_rec, void* p);
124 
125 typedef void(tBTM_BLE_ADDR_CBACK)(const RawAddress& static_random, void* p);
126 
127 /* random address management control block */
128 typedef struct {
129   tBLE_ADDR_TYPE own_addr_type; /* local device LE address type */
130   RawAddress private_addr;
131   alarm_t* refresh_raddr_timer;
132 } tBTM_LE_RANDOM_CB;
133 
134 /* acceptlist using state as a bit mask */
135 constexpr uint8_t BTM_BLE_WL_IDLE = 0;
136 constexpr uint8_t BTM_BLE_ACCEPTLIST_INIT = 1;
137 
138 /* resolving list using state as a bit mask */
139 enum : uint8_t {
140   BTM_BLE_RL_IDLE = 0,
141   BTM_BLE_RL_INIT = (1 << 0),
142   BTM_BLE_RL_SCAN = (1 << 1),
143   BTM_BLE_RL_ADV = (1 << 2),
144 };
145 typedef uint8_t tBTM_BLE_RL_STATE;
146 
147 typedef struct { void* p_param; } tBTM_BLE_CONN_REQ;
148 
149 /* LE state request */
150 #define BTM_BLE_STATE_INVALID 0
151 #define BTM_BLE_STATE_INIT 2
152 #define BTM_BLE_STATE_MAX 11
153 
154 #define BTM_BLE_STATE_CONN_ADV_BIT 0x0001
155 #define BTM_BLE_STATE_INIT_BIT 0x0002
156 #define BTM_BLE_STATE_CENTRAL_BIT 0x0004
157 #define BTM_BLE_STATE_PERIPHERAL_BIT 0x0008
158 #define BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT 0x0010
159 #define BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT 0x0020
160 #define BTM_BLE_STATE_NON_CONN_ADV_BIT 0x0040
161 #define BTM_BLE_STATE_PASSIVE_SCAN_BIT 0x0080
162 #define BTM_BLE_STATE_ACTIVE_SCAN_BIT 0x0100
163 #define BTM_BLE_STATE_SCAN_ADV_BIT 0x0200
164 typedef uint16_t tBTM_BLE_STATE_MASK;
165 
166 #define BTM_BLE_STATE_ALL_MASK 0x03ff
167 #define BTM_BLE_STATE_ALL_ADV_MASK                                  \
168   (BTM_BLE_STATE_CONN_ADV_BIT | BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT | \
169    BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT | BTM_BLE_STATE_SCAN_ADV_BIT)
170 #define BTM_BLE_STATE_ALL_CONN_MASK \
171   (BTM_BLE_STATE_CENTRAL_BIT | BTM_BLE_STATE_PERIPHERAL_BIT)
172 
173 typedef struct {
174   RawAddress* resolve_q_random_pseudo;
175   uint8_t* resolve_q_action;
176   uint8_t q_next;
177   uint8_t q_pending;
178 } tBTM_BLE_RESOLVE_Q;
179 
180 /* BLE privacy mode */
181 #define BTM_PRIVACY_NONE 0 /* BLE no privacy */
182 #define BTM_PRIVACY_1_1 1  /* BLE privacy 1.1, do not support privacy 1.0 */
183 #define BTM_PRIVACY_1_2 2  /* BLE privacy 1.2 */
184 #define BTM_PRIVACY_MIXED \
185   3 /* BLE privacy mixed mode, broadcom propietary mode */
186 typedef uint8_t tBTM_PRIVACY_MODE;
187 
188 /* Define BLE Device Management control structure
189 */
190 constexpr uint8_t kBTM_BLE_INQUIRY_ACTIVE = 0x10;
191 constexpr uint8_t kBTM_BLE_OBSERVE_ACTIVE = 0x80;
192 constexpr size_t kCentralAndPeripheralCount = 2;
193 
194 typedef struct {
195  private:
196   uint8_t scan_activity_; /* LE scan activity mask */
197 
198  public:
is_ble_inquiry_active__anon5f6aadb80808199   bool is_ble_inquiry_active() const {
200     return (scan_activity_ & kBTM_BLE_INQUIRY_ACTIVE);
201   }
is_ble_observe_active__anon5f6aadb80808202   bool is_ble_observe_active() const {
203     return (scan_activity_ & kBTM_BLE_OBSERVE_ACTIVE);
204   }
205 
set_ble_inquiry_active__anon5f6aadb80808206   void set_ble_inquiry_active() { scan_activity_ |= kBTM_BLE_INQUIRY_ACTIVE; }
set_ble_observe_active__anon5f6aadb80808207   void set_ble_observe_active() { scan_activity_ |= kBTM_BLE_OBSERVE_ACTIVE; }
208 
reset_ble_inquiry__anon5f6aadb80808209   void reset_ble_inquiry() { scan_activity_ &= ~kBTM_BLE_INQUIRY_ACTIVE; }
reset_ble_observe__anon5f6aadb80808210   void reset_ble_observe() { scan_activity_ &= ~kBTM_BLE_OBSERVE_ACTIVE; }
211 
is_ble_scan_active__anon5f6aadb80808212   bool is_ble_scan_active() const {
213     return (is_ble_inquiry_active() || is_ble_observe_active());
214   }
215 
216   /*****************************************************
217   **      BLE Inquiry
218   *****************************************************/
219   tBTM_BLE_INQ_CB inq_var;
220 
221   /* observer callback and timer */
222   tBTM_INQ_RESULTS_CB* p_obs_results_cb;
223   tBTM_CMPL_CB* p_obs_cmpl_cb;
224   alarm_t* observer_timer;
225 
226   /* background connection procedure cb value */
227   uint16_t scan_int;
228   uint16_t scan_win;
229 
230   /* acceptlist information */
231   uint8_t wl_state;
set_acceptlist_process_in_progress__anon5f6aadb80808232   void set_acceptlist_process_in_progress() {
233     wl_state |= BTM_BLE_ACCEPTLIST_INIT;
234   }
reset_acceptlist_process_in_progress__anon5f6aadb80808235   void reset_acceptlist_process_in_progress() {
236     wl_state &= ~BTM_BLE_ACCEPTLIST_INIT;
237   }
is_acceptlist_in_progress__anon5f6aadb80808238   bool is_acceptlist_in_progress() const {
239     return wl_state & BTM_BLE_ACCEPTLIST_INIT;
240   }
241 
242  private:
243   enum : uint8_t { /* BLE connection state */
244                    BLE_CONN_IDLE = 0,
245                    BLE_CONNECTING = 2,
246                    BLE_CONN_CANCEL = 3,
247   } conn_state_{BLE_CONN_IDLE};
248 
249  public:
is_connection_state_idle__anon5f6aadb80808250   bool is_connection_state_idle() const { return conn_state_ == BLE_CONN_IDLE; }
is_connection_state_connecting__anon5f6aadb80808251   bool is_connection_state_connecting() const {
252     return conn_state_ == BLE_CONNECTING;
253   }
is_connection_state_cancelled__anon5f6aadb80808254   bool is_connection_state_cancelled() const {
255     return conn_state_ == BLE_CONN_CANCEL;
256   }
set_connection_state_idle__anon5f6aadb80808257   void set_connection_state_idle() { conn_state_ = BLE_CONN_IDLE; }
set_connection_state_connecting__anon5f6aadb80808258   void set_connection_state_connecting() { conn_state_ = BLE_CONNECTING; }
set_connection_state_cancelled__anon5f6aadb80808259   void set_connection_state_cancelled() { conn_state_ = BLE_CONN_CANCEL; }
260 
261   /* random address management control block */
262   tBTM_LE_RANDOM_CB addr_mgnt_cb;
263 
264   tBTM_PRIVACY_MODE privacy_mode;    /* privacy mode */
265   uint8_t resolving_list_avail_size; /* resolving list available size */
266   tBTM_BLE_RESOLVE_Q resolving_list_pend_q; /* Resolving list queue */
267   tBTM_BLE_RL_STATE suspended_rl_state;     /* Suspended resolving list state */
268   uint8_t* irk_list_mask; /* IRK list availability mask, up to max entry bits */
269   tBTM_BLE_RL_STATE rl_state; /* Resolving list state */
270 
271   /* current BLE link state */
272   tBTM_BLE_STATE_MASK cur_states; /* bit mask of tBTM_BLE_STATE */
273 
274   uint8_t link_count[kCentralAndPeripheralCount]; /* total link count central
275                                                      and peripheral*/
276 } tBTM_BLE_CB;
277 
278 #endif  // BTM_BLE_INT_TYPES_H
279