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