• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define BTM_BLE_1M_PHY_MASK 1
73 #define BTM_BLE_CODED_PHY_MASK (1 << 2)
74 
75 typedef struct {
76   uint16_t data_mask;
77   uint8_t* p_flags;
78   uint8_t ad_data[BTM_BLE_AD_DATA_LEN];
79   uint8_t* p_pad;
80 } tBTM_BLE_LOCAL_ADV_DATA;
81 
82 #define BTM_BLE_ISVALID_PARAM(x, min, max) \
83   (((x) >= (min) && (x) <= (max)) || ((x) == BTM_BLE_CONN_PARAM_UNDEF))
84 
85 typedef struct {
86   uint16_t discoverable_mode;
87   uint16_t connectable_mode;
88   uint16_t scan_window_1m;
89   uint16_t scan_interval_1m;
90   uint16_t scan_window_coded;
91   uint16_t scan_interval_coded;
92   uint8_t scan_type; /* current scan type: active or passive */
93   uint8_t scan_phy;
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__anon9d10d6040208102   void enable_advertising_mode() { adv_mode = BTM_BLE_ADV_ENABLE; }
disable_advertising_mode__anon9d10d6040208103   void disable_advertising_mode() { adv_mode = BTM_BLE_ADV_DISABLE; }
is_advertising_mode_enabled__anon9d10d6040208104   bool is_advertising_mode_enabled() const { return adv_mode == BTM_BLE_ADV_ENABLE; }
is_1m_phy_configured__anon9d10d6040208105   bool is_1m_phy_configured() const { return (scan_phy & BTM_BLE_1M_PHY_MASK) != 0; }
is_coded_phy_configured__anon9d10d6040208106   bool is_coded_phy_configured() const { return (scan_phy & BTM_BLE_CODED_PHY_MASK) != 0; }
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 {
148   void* p_param;
149 } tBTM_BLE_CONN_REQ;
150 
151 /* LE state request */
152 #define BTM_BLE_STATE_INVALID 0
153 #define BTM_BLE_STATE_INIT 2
154 #define BTM_BLE_STATE_MAX 11
155 
156 #define BTM_BLE_STATE_CONN_ADV_BIT 0x0001
157 #define BTM_BLE_STATE_INIT_BIT 0x0002
158 #define BTM_BLE_STATE_CENTRAL_BIT 0x0004
159 #define BTM_BLE_STATE_PERIPHERAL_BIT 0x0008
160 #define BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT 0x0010
161 #define BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT 0x0020
162 #define BTM_BLE_STATE_NON_CONN_ADV_BIT 0x0040
163 #define BTM_BLE_STATE_PASSIVE_SCAN_BIT 0x0080
164 #define BTM_BLE_STATE_ACTIVE_SCAN_BIT 0x0100
165 #define BTM_BLE_STATE_SCAN_ADV_BIT 0x0200
166 typedef uint16_t tBTM_BLE_STATE_MASK;
167 
168 #define BTM_BLE_STATE_ALL_MASK 0x03ff
169 #define BTM_BLE_STATE_ALL_ADV_MASK                                  \
170   (BTM_BLE_STATE_CONN_ADV_BIT | BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT | \
171    BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT | BTM_BLE_STATE_SCAN_ADV_BIT)
172 #define BTM_BLE_STATE_ALL_CONN_MASK (BTM_BLE_STATE_CENTRAL_BIT | BTM_BLE_STATE_PERIPHERAL_BIT)
173 
174 typedef struct {
175   RawAddress* resolve_q_random_pseudo{nullptr};
176   uint8_t* resolve_q_action{nullptr};
177   uint8_t q_next;
178   uint8_t q_pending;
179 } tBTM_BLE_RESOLVE_Q;
180 
181 /* BLE privacy mode */
182 #define BTM_PRIVACY_NONE 0  /* BLE no privacy */
183 #define BTM_PRIVACY_1_1 1   /* BLE privacy 1.1, do not support privacy 1.0 */
184 #define BTM_PRIVACY_1_2 2   /* BLE privacy 1.2 */
185 #define BTM_PRIVACY_MIXED 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__anon9d10d6040708199   bool is_ble_inquiry_active() const { return scan_activity_ & kBTM_BLE_INQUIRY_ACTIVE; }
is_ble_observe_active__anon9d10d6040708200   bool is_ble_observe_active() const { return scan_activity_ & kBTM_BLE_OBSERVE_ACTIVE; }
201 
set_ble_inquiry_active__anon9d10d6040708202   void set_ble_inquiry_active() { scan_activity_ |= kBTM_BLE_INQUIRY_ACTIVE; }
set_ble_observe_active__anon9d10d6040708203   void set_ble_observe_active() { scan_activity_ |= kBTM_BLE_OBSERVE_ACTIVE; }
204 
reset_ble_inquiry__anon9d10d6040708205   void reset_ble_inquiry() { scan_activity_ &= ~kBTM_BLE_INQUIRY_ACTIVE; }
reset_ble_observe__anon9d10d6040708206   void reset_ble_observe() { scan_activity_ &= ~kBTM_BLE_OBSERVE_ACTIVE; }
207 
is_ble_scan_active__anon9d10d6040708208   bool is_ble_scan_active() const { return is_ble_inquiry_active() || is_ble_observe_active(); }
209 
210   /*****************************************************
211   **      BLE Inquiry
212   *****************************************************/
213   tBTM_BLE_INQ_CB inq_var;
214 
215   /* observer callback and timer */
216   tBTM_INQ_RESULTS_CB* p_obs_results_cb;
217   tBTM_CMPL_CB* p_obs_cmpl_cb;
218   alarm_t* observer_timer;
219 
220   /* opportunistic observer */
221   tBTM_INQ_RESULTS_CB* p_opportunistic_obs_results_cb;
222 
223   /* target announcement observer */
224   tBTM_INQ_RESULTS_CB* p_target_announcement_obs_results_cb;
225 
226 private:
227   enum : uint8_t { /* BLE connection state */
228                    BLE_CONN_IDLE = 0,
229                    BLE_CONNECTING = 2,
230                    BLE_CONN_CANCEL = 3,
231   } conn_state_{BLE_CONN_IDLE};
232 
233 public:
is_connection_state_idle__anon9d10d6040708234   bool is_connection_state_idle() const { return conn_state_ == BLE_CONN_IDLE; }
is_connection_state_connecting__anon9d10d6040708235   bool is_connection_state_connecting() const { return conn_state_ == BLE_CONNECTING; }
is_connection_state_cancelled__anon9d10d6040708236   bool is_connection_state_cancelled() const { return conn_state_ == BLE_CONN_CANCEL; }
set_connection_state_idle__anon9d10d6040708237   void set_connection_state_idle() { conn_state_ = BLE_CONN_IDLE; }
set_connection_state_connecting__anon9d10d6040708238   void set_connection_state_connecting() { conn_state_ = BLE_CONNECTING; }
set_connection_state_cancelled__anon9d10d6040708239   void set_connection_state_cancelled() { conn_state_ = BLE_CONN_CANCEL; }
240 
241   /* random address management control block */
242   tBTM_LE_RANDOM_CB addr_mgnt_cb;
243 
244   tBTM_PRIVACY_MODE privacy_mode;           /* privacy mode */
245   uint8_t resolving_list_avail_size;        /* resolving list available size */
246   tBTM_BLE_RESOLVE_Q resolving_list_pend_q; /* Resolving list queue */
247   tBTM_BLE_RL_STATE suspended_rl_state;     /* Suspended resolving list state */
248   /* IRK list availability mask, up to max entry bits */
249   uint8_t* irk_list_mask{nullptr};
250   tBTM_BLE_RL_STATE rl_state; /* Resolving list state */
251 
252   /* current BLE link state */
253   tBTM_BLE_STATE_MASK cur_states; /* bit mask of tBTM_BLE_STATE */
254 
255   uint8_t link_count[kCentralAndPeripheralCount]; /* total link count central
256                                                      and peripheral*/
257 } tBTM_BLE_CB;
258 
259 #endif  // BTM_BLE_INT_TYPES_H
260