• 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 #ifndef BTM_INT_TYPES_H
19 #define BTM_INT_TYPES_H
20 
21 #include "btif/include/btif_bqr.h"
22 #include "btm_api_types.h"
23 #include "btm_ble_api_types.h"
24 #include "btm_ble_int_types.h"
25 #include "hcidefs.h"
26 #include "osi/include/alarm.h"
27 #include "osi/include/list.h"
28 #include "rfcdefs.h"
29 
30 typedef char tBTM_LOC_BD_NAME[BTM_MAX_LOC_BD_NAME_LEN + 1];
31 
32 #define BTM_ACL_IS_CONNECTED(bda) \
33   (btm_bda_to_acl(bda, BT_TRANSPORT_BR_EDR) != NULL)
34 
35 /* Definitions for Server Channel Number (SCN) management
36 */
37 #define BTM_MAX_SCN PORT_MAX_RFC_PORTS
38 
39 /* Define masks for supported and exception 2.0 ACL packet types
40 */
41 #define BTM_ACL_SUPPORTED_PKTS_MASK                                           \
42   (HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH1 | HCI_PKT_TYPES_MASK_DM3 | \
43    HCI_PKT_TYPES_MASK_DH3 | HCI_PKT_TYPES_MASK_DM5 | HCI_PKT_TYPES_MASK_DH5)
44 
45 #define BTM_ACL_EXCEPTION_PKTS_MASK                            \
46   (HCI_PKT_TYPES_MASK_NO_2_DH1 | HCI_PKT_TYPES_MASK_NO_3_DH1 | \
47    HCI_PKT_TYPES_MASK_NO_2_DH3 | HCI_PKT_TYPES_MASK_NO_3_DH3 | \
48    HCI_PKT_TYPES_MASK_NO_2_DH5 | HCI_PKT_TYPES_MASK_NO_3_DH5)
49 
50 #define BTM_EPR_AVAILABLE(p)                                            \
51   ((HCI_ATOMIC_ENCRYPT_SUPPORTED((p)->peer_lmp_feature_pages[0]) &&     \
52     HCI_ATOMIC_ENCRYPT_SUPPORTED(                                       \
53         controller_get_interface()->get_features_classic(0)->as_array)) \
54        ? true                                                           \
55        : false)
56 
57 #define BTM_IS_BRCM_CONTROLLER()                                 \
58   (controller_get_interface()->get_bt_version()->manufacturer == \
59    LMP_COMPID_BROADCOM)
60 
61 /* Define the ACL Management control structure
62 */
63 typedef struct {
64   uint16_t hci_handle;
65   uint16_t pkt_types_mask;
66   uint16_t clock_offset;
67   RawAddress remote_addr;
68   DEV_CLASS remote_dc;
69   BD_NAME remote_name;
70 
71   uint16_t manufacturer;
72   uint16_t lmp_subversion;
73   uint16_t link_super_tout;
74   BD_FEATURES
75   peer_lmp_feature_pages[HCI_EXT_FEATURES_PAGE_MAX + 1]; /* Peer LMP Extended
76                                                             features mask table
77                                                             for the device */
78   uint8_t num_read_pages;
79   uint8_t lmp_version;
80 
81   bool in_use;
82   uint8_t link_role;
83   bool link_up_issued; /* True if busy_level link up has been issued */
84 
85 #define BTM_ACL_SWKEY_STATE_IDLE 0
86 #define BTM_ACL_SWKEY_STATE_MODE_CHANGE 1
87 #define BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF 2
88 #define BTM_ACL_SWKEY_STATE_SWITCHING 3
89 #define BTM_ACL_SWKEY_STATE_ENCRYPTION_ON 4
90 #define BTM_ACL_SWKEY_STATE_IN_PROGRESS 5
91   uint8_t switch_role_state;
92 
93 #define BTM_MAX_SW_ROLE_FAILED_ATTEMPTS 3
94   uint8_t switch_role_failed_attempts;
95 
96 #define BTM_ACL_ENCRYPT_STATE_IDLE 0
97 #define BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF 1 /* encryption turning off */
98 #define BTM_ACL_ENCRYPT_STATE_TEMP_FUNC \
99   2 /* temporarily off for change link key or role switch */
100 #define BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON 3 /* encryption turning on */
101   uint8_t encrypt_state;                   /* overall BTM encryption state */
102 
103   tBT_TRANSPORT transport;
104   RawAddress conn_addr;   /* local device address used for this connection */
105   uint8_t conn_addr_type; /* local device address type for this connection */
106   RawAddress active_remote_addr;   /* remote address used on this connection */
107   uint8_t active_remote_addr_type; /* local device address type for this
108                                       connection */
109   BD_FEATURES peer_le_features; /* Peer LE Used features mask for the device */
110 
111 } tACL_CONN;
112 
113 /* Define the Device Management control structure
114 */
115 typedef struct {
116   tBTM_DEV_STATUS_CB* p_dev_status_cb; /* Device status change callback */
117   tBTM_VS_EVT_CB* p_vend_spec_cb
118       [BTM_MAX_VSE_CALLBACKS]; /* Register for vendor specific events  */
119 
120   tBTM_CMPL_CB*
121       p_stored_link_key_cmpl_cb; /* Read/Write/Delete stored link key    */
122 
123   alarm_t* read_local_name_timer; /* Read local name timer */
124   tBTM_CMPL_CB* p_rln_cmpl_cb;    /* Callback function to be called when  */
125                                   /* read local name function complete    */
126 
127   alarm_t* read_rssi_timer;       /* Read RSSI timer */
128   tBTM_CMPL_CB* p_rssi_cmpl_cb;   /* Callback function to be called when  */
129                                   /* read RSSI function completes */
130 
131   alarm_t* read_failed_contact_counter_timer; /* Read Failed Contact Counter */
132                                               /* timer */
133   tBTM_CMPL_CB* p_failed_contact_counter_cmpl_cb; /* Callback function to be */
134   /* called when read Failed Contact Counter function completes */
135 
136   alarm_t*
137       read_automatic_flush_timeout_timer; /* Read Automatic Flush Timeout */
138                                           /* timer */
139   tBTM_CMPL_CB* p_automatic_flush_timeout_cmpl_cb; /* Callback function to be */
140   /* called when read Automatic Flush Timeout function completes */
141 
142   alarm_t* read_link_quality_timer;
143   tBTM_CMPL_CB* p_link_qual_cmpl_cb; /* Callback function to be called when  */
144                                      /* read link quality function completes */
145 
146   alarm_t* read_inq_tx_power_timer;
147   tBTM_CMPL_CB*
148       p_inq_tx_power_cmpl_cb; /* Callback function to be called when  */
149                               /* read inq tx power function completes  */
150 
151   alarm_t* qos_setup_timer;          /* QoS setup timer */
152   tBTM_CMPL_CB* p_qos_setup_cmpl_cb; /* Callback function to be called when  */
153                                      /* qos setup function completes         */
154 
155   tBTM_ROLE_SWITCH_CMPL switch_role_ref_data;
156   tBTM_CMPL_CB* p_switch_role_cb; /* Callback function to be called when  */
157                                   /* requested switch role is completed   */
158 
159   alarm_t* read_tx_power_timer;     /* Read tx power timer */
160   tBTM_CMPL_CB* p_tx_power_cmpl_cb; /* Callback function to be called       */
161 
162   DEV_CLASS dev_class; /* Local device class                   */
163 
164   tBTM_CMPL_CB*
165       p_le_test_cmd_cmpl_cb; /* Callback function to be called when
166                              LE test mode command has been sent successfully */
167 
168   RawAddress read_tx_pwr_addr; /* read TX power target address     */
169 
170 #define BTM_LE_SUPPORT_STATE_SIZE 8
171   uint8_t le_supported_states[BTM_LE_SUPPORT_STATE_SIZE];
172 
173   tBTM_BLE_LOCAL_ID_KEYS id_keys;      /* local BLE ID keys */
174   Octet16 ble_encryption_key_value;    /* BLE encryption key */
175 
176 #if (BTM_BLE_CONFORMANCE_TESTING == TRUE)
177   bool no_disc_if_pair_fail;
178   bool enable_test_mac_val;
179   BT_OCTET8 test_mac;
180   bool enable_test_local_sign_cntr;
181   uint32_t test_local_sign_cntr;
182 #endif
183 
184   tBTM_IO_CAP loc_io_caps;      /* IO capability of the local device */
185   tBTM_AUTH_REQ loc_auth_req;   /* the auth_req flag  */
186   bool secure_connections_only; /* Rejects service level 0 connections if */
187                                 /* itself or peer device doesn't support */
188                                 /* secure connections */
189 } tBTM_DEVCB;
190 
191 /* Define the structures and constants used for inquiry
192 */
193 
194 /* Definitions of limits for inquiries */
195 #define BTM_PER_INQ_MIN_MAX_PERIOD HCI_PER_INQ_MIN_MAX_PERIOD
196 #define BTM_PER_INQ_MAX_MAX_PERIOD HCI_PER_INQ_MAX_MAX_PERIOD
197 #define BTM_PER_INQ_MIN_MIN_PERIOD HCI_PER_INQ_MIN_MIN_PERIOD
198 #define BTM_PER_INQ_MAX_MIN_PERIOD HCI_PER_INQ_MAX_MIN_PERIOD
199 #define BTM_MAX_INQUIRY_LENGTH HCI_MAX_INQUIRY_LENGTH
200 #define BTM_MIN_INQUIRY_LEN 0x01
201 
202 #define BTM_MIN_INQ_TX_POWER (-70)
203 #define BTM_MAX_INQ_TX_POWER 20
204 
205 typedef struct {
206   uint32_t inq_count; /* Used for determining if a response has already been */
207   /* received for the current inquiry operation. (We do not   */
208   /* want to flood the caller with multiple responses from    */
209   /* the same device.                                         */
210   RawAddress bd_addr;
211 } tINQ_BDADDR;
212 
213 typedef struct {
214   uint64_t time_of_resp;
215   uint32_t
216       inq_count; /* "timestamps" the entry with a particular inquiry count   */
217                  /* Used for determining if a response has already been      */
218                  /* received for the current inquiry operation. (We do not   */
219                  /* want to flood the caller with multiple responses from    */
220                  /* the same device.                                         */
221   tBTM_INQ_INFO inq_info;
222   bool in_use;
223   bool scan_rsp;
224 } tINQ_DB_ENT;
225 
226 enum { INQ_NONE, INQ_LE_OBSERVE, INQ_GENERAL };
227 typedef uint8_t tBTM_INQ_TYPE;
228 
229 typedef struct {
230   tBTM_CMPL_CB* p_remname_cmpl_cb;
231 
232 #define BTM_EXT_RMT_NAME_TIMEOUT_MS (40 * 1000) /* 40 seconds */
233 
234   alarm_t* remote_name_timer;
235 
236   uint16_t discoverable_mode;
237   uint16_t connectable_mode;
238   uint16_t page_scan_window;
239   uint16_t page_scan_period;
240   uint16_t inq_scan_window;
241   uint16_t inq_scan_period;
242   uint16_t inq_scan_type;
243   uint16_t page_scan_type; /* current page scan type */
244   tBTM_INQ_TYPE scan_type;
245 
246   RawAddress remname_bda; /* Name of bd addr for active remote name request */
247 #define BTM_RMT_NAME_INACTIVE 0
248 #define BTM_RMT_NAME_EXT 0x1 /* Initiated through API */
249 #define BTM_RMT_NAME_SEC 0x2 /* Initiated internally by security manager */
250 #define BTM_RMT_NAME_INQ 0x4 /* Remote name initiated internally by inquiry */
251   bool remname_active; /* State of a remote name request by external API */
252 
253   tBTM_CMPL_CB* p_inq_cmpl_cb;
254   tBTM_INQ_RESULTS_CB* p_inq_results_cb;
255   tBTM_CMPL_CB*
256       p_inq_ble_cmpl_cb; /*completion callback exclusively for LE Observe*/
257   tBTM_INQ_RESULTS_CB*
258       p_inq_ble_results_cb; /*results callback exclusively for LE observe*/
259   tBTM_CMPL_CB* p_inqfilter_cmpl_cb; /* Called (if not NULL) after inquiry
260                                         filter completed */
261   uint32_t inq_counter; /* Counter incremented each time an inquiry completes */
262   /* Used for determining whether or not duplicate devices */
263   /* have responded to the same inquiry */
264   tINQ_BDADDR* p_bd_db;    /* Pointer to memory that holds bdaddrs */
265   uint16_t num_bd_entries; /* Number of entries in database */
266   uint16_t max_bd_entries; /* Maximum number of entries that can be stored */
267   tINQ_DB_ENT inq_db[BTM_INQ_DB_SIZE];
268   tBTM_INQ_PARMS inqparms; /* Contains the parameters for the current inquiry */
269   tBTM_INQUIRY_CMPL
270       inq_cmpl_info; /* Status and number of responses from the last inquiry */
271 
272   uint16_t per_min_delay; /* Current periodic minimum delay */
273   uint16_t per_max_delay; /* Current periodic maximum delay */
274   bool inqfilt_active;
275   uint8_t pending_filt_complete_event; /* to take care of
276                                           btm_event_filter_complete
277                                           corresponding to */
278   /* inquiry that has been cancelled*/
279   uint8_t inqfilt_type; /* Contains the inquiry filter type (BD ADDR, COD, or
280                            Clear) */
281 
282 #define BTM_INQ_INACTIVE_STATE 0
283 #define BTM_INQ_CLR_FILT_STATE \
284   1 /* Currently clearing the inquiry filter preceeding the inquiry request */
285     /* (bypassed if filtering is not used)                                  */
286 #define BTM_INQ_SET_FILT_STATE \
287   2 /* Sets the new filter (or turns off filtering) in this state */
288 #define BTM_INQ_ACTIVE_STATE \
289   3 /* Actual inquiry or periodic inquiry is in progress */
290 #define BTM_INQ_REMNAME_STATE 4 /* Remote name requests are active  */
291 
292   uint8_t state;      /* Current state that the inquiry process is in */
293   uint8_t inq_active; /* Bit Mask indicating type of inquiry is active */
294   bool no_inc_ssp;    /* true, to stop inquiry on incoming SSP */
295 } tBTM_INQUIRY_VAR_ST;
296 
297 /* The MSB of the clock offset field indicates whether the offset is valid. */
298 #define BTM_CLOCK_OFFSET_VALID 0x8000
299 
300 /* Define the structures needed by security management
301 */
302 
303 #define BTM_SEC_INVALID_HANDLE 0xFFFF
304 
305 typedef uint8_t* BTM_BD_NAME_PTR; /* Pointer to Device name */
306 
307 /* Security callback is called by this unit when security
308  *   procedures are completed.  Parameters are
309  *              BD Address of remote
310  *              Result of the operation
311 */
312 typedef tBTM_SEC_CBACK tBTM_SEC_CALLBACK;
313 
314 typedef void(tBTM_SCO_IND_CBACK)(uint16_t sco_inx);
315 
316 /* MACROs to convert from SCO packet types mask to ESCO and back */
317 #define BTM_SCO_PKT_TYPE_MASK \
318   (HCI_PKT_TYPES_MASK_HV1 | HCI_PKT_TYPES_MASK_HV2 | HCI_PKT_TYPES_MASK_HV3)
319 
320 /* Mask defining only the SCO types of an esco packet type */
321 #define BTM_ESCO_PKT_TYPE_MASK \
322   (ESCO_PKT_TYPES_MASK_HV1 | ESCO_PKT_TYPES_MASK_HV2 | ESCO_PKT_TYPES_MASK_HV3)
323 
324 #define BTM_SCO_2_ESCO(scotype) \
325   ((uint16_t)(((scotype)&BTM_SCO_PKT_TYPE_MASK) >> 5))
326 #define BTM_ESCO_2_SCO(escotype) \
327   ((uint16_t)(((escotype)&BTM_ESCO_PKT_TYPE_MASK) << 5))
328 
329 /* Define masks for supported and exception 2.0 SCO packet types
330 */
331 #define BTM_SCO_SUPPORTED_PKTS_MASK                    \
332   (ESCO_PKT_TYPES_MASK_HV1 | ESCO_PKT_TYPES_MASK_HV2 | \
333    ESCO_PKT_TYPES_MASK_HV3 | ESCO_PKT_TYPES_MASK_EV3 | \
334    ESCO_PKT_TYPES_MASK_EV4 | ESCO_PKT_TYPES_MASK_EV5)
335 
336 #define BTM_SCO_EXCEPTION_PKTS_MASK                              \
337   (ESCO_PKT_TYPES_MASK_NO_2_EV3 | ESCO_PKT_TYPES_MASK_NO_3_EV3 | \
338    ESCO_PKT_TYPES_MASK_NO_2_EV5 | ESCO_PKT_TYPES_MASK_NO_3_EV5)
339 
340 #define BTM_SCO_ROUTE_UNKNOWN 0xff
341 
342 /* Define the structure that contains (e)SCO data */
343 typedef struct {
344   tBTM_ESCO_CBACK* p_esco_cback; /* Callback for eSCO events     */
345   enh_esco_params_t setup;
346   tBTM_ESCO_DATA data; /* Connection complete information */
347   uint8_t hci_status;
348 } tBTM_ESCO_INFO;
349 
350 /* Define the structure used for SCO Management
351 */
352 typedef struct {
353   tBTM_ESCO_INFO esco; /* Current settings             */
354   tBTM_SCO_CB* p_conn_cb; /* Callback for when connected  */
355   tBTM_SCO_CB* p_disc_cb; /* Callback for when disconnect */
356   uint16_t state;         /* The state of the SCO link    */
357   uint16_t hci_handle;    /* HCI Handle                   */
358   bool is_orig;           /* true if the originator       */
359   bool rem_bd_known;      /* true if remote BD addr known */
360 
361 } tSCO_CONN;
362 
363 /* SCO Management control block */
364 typedef struct {
365   tBTM_SCO_IND_CBACK* app_sco_ind_cb;
366   tSCO_CONN sco_db[BTM_MAX_SCO_LINKS];
367   enh_esco_params_t def_esco_parms;
368   uint16_t sco_disc_reason;
369   bool esco_supported;        /* true if 1.2 cntlr AND supports eSCO links */
370   esco_data_path_t sco_route; /* HCI, PCM, or TEST */
371 } tSCO_CB;
372 
373 extern void btm_set_sco_ind_cback(tBTM_SCO_IND_CBACK* sco_ind_cb);
374 extern void btm_accept_sco_link(uint16_t sco_inx, enh_esco_params_t* p_setup,
375                                 tBTM_SCO_CB* p_conn_cb, tBTM_SCO_CB* p_disc_cb);
376 extern void btm_reject_sco_link(uint16_t sco_inx);
377 extern void btm_sco_chk_pend_rolechange(uint16_t hci_handle);
378 extern void btm_sco_disc_chk_pend_for_modechange(uint16_t hci_handle);
379 
380 /*
381  * Define structure for Security Service Record.
382  * A record exists for each service registered with the Security Manager
383 */
384 #define BTM_SEC_OUT_FLAGS \
385   (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHORIZE)
386 #define BTM_SEC_IN_FLAGS \
387   (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHORIZE)
388 
389 #define BTM_SEC_OUT_LEVEL4_FLAGS                                       \
390   (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_MITM | \
391    BTM_SEC_MODE4_LEVEL4)
392 
393 #define BTM_SEC_IN_LEVEL4_FLAGS                                     \
394   (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_MITM | \
395    BTM_SEC_MODE4_LEVEL4)
396 typedef struct {
397   uint32_t mx_proto_id;     /* Service runs over this multiplexer protocol */
398   uint32_t orig_mx_chan_id; /* Channel on the multiplexer protocol    */
399   uint32_t term_mx_chan_id; /* Channel on the multiplexer protocol    */
400   uint16_t psm;             /* L2CAP PSM value */
401   uint16_t security_flags;  /* Bitmap of required security features */
402   uint8_t service_id;       /* Passed in authorization callback */
403 #if BTM_SEC_SERVICE_NAME_LEN > 0
404   uint8_t orig_service_name[BTM_SEC_SERVICE_NAME_LEN + 1];
405   uint8_t term_service_name[BTM_SEC_SERVICE_NAME_LEN + 1];
406 #endif
407 } tBTM_SEC_SERV_REC;
408 
409 /* LE Security information of device in Slave Role */
410 typedef struct {
411   Octet16 irk;   /* peer diverified identity root */
412   Octet16 pltk;  /* peer long term key */
413   Octet16 pcsrk; /* peer SRK peer device used to secured sign local data  */
414 
415   Octet16 lltk;  /* local long term key */
416   Octet16 lcsrk; /* local SRK peer device used to secured sign local data  */
417 
418   BT_OCTET8 rand;        /* random vector for LTK generation */
419   uint16_t ediv;         /* LTK diversifier of this slave device */
420   uint16_t div;          /* local DIV  to generate local LTK=d1(ER,DIV,0) and
421                             CSRK=d1(ER,DIV,1)  */
422   uint8_t sec_level;     /* local pairing security level */
423   uint8_t key_size;      /* key size of the LTK delivered to peer device */
424   uint8_t srk_sec_level; /* security property of peer SRK for this device */
425   uint8_t local_csrk_sec_level; /* security property of local CSRK for this
426                                    device */
427 
428   uint32_t counter;       /* peer sign counter for verifying rcv signed cmd */
429   uint32_t local_counter; /* local sign counter for sending signed write cmd*/
430 } tBTM_SEC_BLE_KEYS;
431 
432 typedef struct {
433   RawAddress pseudo_addr; /* LE pseudo address of the device if different from
434                           device address  */
435   tBLE_ADDR_TYPE ble_addr_type; /* LE device type: public or random address */
436   tBLE_ADDR_TYPE identity_addr_type; /* identity address type */
437   RawAddress identity_addr;          /* identity address */
438 
439 #define BTM_WHITE_LIST_BIT 0x01
440 #define BTM_RESOLVING_LIST_BIT 0x02
441   uint8_t in_controller_list; /* in controller resolving list or not */
442   uint8_t resolving_list_index;
443 #if (BLE_PRIVACY_SPT == TRUE)
444   RawAddress cur_rand_addr; /* current random address */
445 
446 #define BTM_BLE_ADDR_PSEUDO 0 /* address index device record */
447 #define BTM_BLE_ADDR_RRA 1    /* cur_rand_addr */
448 #define BTM_BLE_ADDR_STATIC 2 /* static_addr  */
449   uint8_t active_addr_type;
450 #endif
451 
452   tBTM_LE_KEY_TYPE key_type; /* bit mask of valid key types in record */
453   tBTM_SEC_BLE_KEYS keys;    /* LE device security info in slave rode */
454 } tBTM_SEC_BLE;
455 
456 /* Peering bond type */
457 enum { BOND_TYPE_UNKNOWN, BOND_TYPE_PERSISTENT, BOND_TYPE_TEMPORARY };
458 typedef uint8_t tBTM_BOND_TYPE;
459 
460 /*
461  * Define structure for Security Device Record.
462  * A record exists for each device authenticated with this device
463 */
464 typedef struct {
465   tBTM_SEC_SERV_REC* p_cur_service;
466   tBTM_SEC_CALLBACK* p_callback;
467   void* p_ref_data;
468   uint32_t timestamp; /* Timestamp of the last connection   */
469   uint32_t trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE]; /* Bitwise OR of trusted
470                                                         services     */
471   uint16_t hci_handle;     /* Handle to connection when exists   */
472   uint16_t clock_offset;   /* Latest known clock offset          */
473   RawAddress bd_addr;      /* BD_ADDR of the device              */
474   DEV_CLASS dev_class;     /* DEV_CLASS of the device            */
475   LinkKey link_key;        /* Device link key                    */
476   uint8_t pin_code_length; /* Length of the pin_code used for paring */
477 
478 #define BTM_SEC_AUTHORIZED BTM_SEC_FLAG_AUTHORIZED       /* 0x01 */
479 #define BTM_SEC_AUTHENTICATED BTM_SEC_FLAG_AUTHENTICATED /* 0x02 */
480 #define BTM_SEC_ENCRYPTED BTM_SEC_FLAG_ENCRYPTED         /* 0x04 */
481 #define BTM_SEC_NAME_KNOWN 0x08
482 #define BTM_SEC_LINK_KEY_KNOWN BTM_SEC_FLAG_LKEY_KNOWN   /* 0x10 */
483 #define BTM_SEC_LINK_KEY_AUTHED BTM_SEC_FLAG_LKEY_AUTHED /* 0x20 */
484 #define BTM_SEC_ROLE_SWITCHED 0x40
485 #define BTM_SEC_IN_USE 0x80
486 /* LE link security flag */
487 #define BTM_SEC_LE_AUTHENTICATED \
488   0x0200 /* LE link is encrypted after pairing with MITM */
489 #define BTM_SEC_LE_ENCRYPTED 0x0400  /* LE link is encrypted */
490 #define BTM_SEC_LE_NAME_KNOWN 0x0800 /* not used */
491 #define BTM_SEC_LE_LINK_KEY_KNOWN \
492   0x1000 /* bonded with peer (peer LTK and/or SRK is saved) */
493 #define BTM_SEC_LE_LINK_KEY_AUTHED 0x2000 /* pairing is done with MITM */
494 #define BTM_SEC_16_DIGIT_PIN_AUTHED \
495   0x4000 /* pairing is done with 16 digit pin */
496 
497   uint16_t sec_flags; /* Current device security state      */
498 
499   tBTM_BD_NAME sec_bd_name; /* User friendly name of the device. (may be
500                                truncated to save space in dev_rec table) */
501   BD_FEATURES feature_pages[HCI_EXT_FEATURES_PAGE_MAX +
502                             1]; /* Features supported by the device */
503   uint8_t num_read_pages;
504 
505 #define BTM_SEC_STATE_IDLE 0
506 #define BTM_SEC_STATE_AUTHENTICATING 1
507 #define BTM_SEC_STATE_ENCRYPTING 2
508 #define BTM_SEC_STATE_GETTING_NAME 3
509 #define BTM_SEC_STATE_AUTHORIZING 4
510 #define BTM_SEC_STATE_SWITCHING_ROLE 5
511 #define BTM_SEC_STATE_DISCONNECTING 6 /* disconnecting BR/EDR */
512 #define BTM_SEC_STATE_DELAY_FOR_ENC \
513   7 /* delay to check for encryption to work around */
514     /* controller problems */
515 #define BTM_SEC_STATE_DISCONNECTING_BLE 8  /* disconnecting BLE */
516 #define BTM_SEC_STATE_DISCONNECTING_BOTH 9 /* disconnecting BR/EDR and BLE */
517 
518   uint8_t sec_state;  /* Operating state                    */
519   bool is_originator; /* true if device is originating connection */
520   bool role_master;           /* true if current mode is master     */
521   uint16_t security_required; /* Security required for connection   */
522   bool link_key_not_sent; /* link key notification has not been sent waiting for
523                              name */
524   uint8_t link_key_type;  /* Type of key used in pairing   */
525   bool link_key_changed;  /* Changed link key during current connection */
526 
527 #define BTM_MAX_PRE_SM4_LKEY_TYPE \
528   BTM_LKEY_TYPE_REMOTE_UNIT /* the link key type used by legacy pairing */
529 
530 #define BTM_SM4_UNKNOWN 0x00
531 #define BTM_SM4_KNOWN 0x10
532 #define BTM_SM4_TRUE 0x11
533 #define BTM_SM4_REQ_PEND 0x08 /* set this bit when getting remote features */
534 #define BTM_SM4_UPGRADE 0x04  /* set this bit when upgrading link key */
535 #define BTM_SM4_RETRY                                     \
536   0x02 /* set this bit to retry on HCI_ERR_KEY_MISSING or \
537           HCI_ERR_LMP_ERR_TRANS_COLLISION */
538 #define BTM_SM4_DD_ACP \
539   0x20 /* set this bit to indicate peer initiated dedicated bonding */
540 #define BTM_SM4_CONN_PEND                                               \
541   0x40 /* set this bit to indicate accepting acl conn; to be cleared on \
542           btm_acl_created */
543   uint8_t sm4;                /* BTM_SM4_TRUE, if the peer supports SM4 */
544   tBTM_IO_CAP rmt_io_caps;    /* IO capability of the peer device */
545   tBTM_AUTH_REQ rmt_auth_req; /* the auth_req flag as in the IO caps rsp evt */
546   bool remote_supports_secure_connections;
547   bool remote_features_needed; /* set to true if the local device is in */
548   /* "Secure Connections Only" mode and it receives */
549   /* HCI_IO_CAPABILITY_REQUEST_EVT from the peer before */
550   /* it knows peer's support for Secure Connections */
551 
552   uint16_t ble_hci_handle; /* use in DUMO connection */
553   uint8_t enc_key_size;    /* current link encryption key size */
554   tBT_DEVICE_TYPE device_type;
555   bool new_encryption_key_is_p256; /* Set to true when the newly generated LK
556                                    ** is generated from P-256.
557                                    ** Link encrypted with such LK can be used
558                                    ** for SM over BR/EDR.
559                                    */
560   bool no_smp_on_br;        /* if set to true then SMP on BR/EDR doesn't */
561                             /* work, i.e. link keys crosspairing */
562                             /* SC BR/EDR->SC LE doesn't happen */
563   tBTM_BOND_TYPE bond_type; /* peering bond type */
564 
565   tBTM_SEC_BLE ble;
566   tBTM_LE_CONN_PRAMS conn_params;
567 
568 #if (BTM_DISC_DURING_RS == TRUE)
569 #define BTM_SEC_RS_NOT_PENDING 0 /* Role Switch not in progress */
570 #define BTM_SEC_RS_PENDING 1     /* Role Switch in progress */
571 #define BTM_SEC_DISC_PENDING 2   /* Disconnect is pending */
572   uint8_t rs_disc_pending;
573 #endif
574 #define BTM_SEC_NO_LAST_SERVICE_ID 0
575   uint8_t last_author_service_id; /* ID of last serviced authorized: Reset after
576                                      each l2cap connection */
577 
578 } tBTM_SEC_DEV_REC;
579 
580 #define BTM_SEC_IS_SM4(sm) ((bool)(BTM_SM4_TRUE == ((sm)&BTM_SM4_TRUE)))
581 #define BTM_SEC_IS_SM4_LEGACY(sm) ((bool)(BTM_SM4_KNOWN == ((sm)&BTM_SM4_TRUE)))
582 #define BTM_SEC_IS_SM4_UNKNOWN(sm) \
583   ((bool)(BTM_SM4_UNKNOWN == ((sm)&BTM_SM4_TRUE)))
584 
585 #define BTM_SEC_LE_MASK                              \
586   (BTM_SEC_LE_AUTHENTICATED | BTM_SEC_LE_ENCRYPTED | \
587    BTM_SEC_LE_LINK_KEY_KNOWN | BTM_SEC_LE_LINK_KEY_AUTHED)
588 
589 /*
590  * Define device configuration structure
591 */
592 typedef struct {
593   tBTM_LOC_BD_NAME bd_name;  /* local Bluetooth device name */
594   bool pin_type;             /* true if PIN type is fixed */
595   uint8_t pin_code_len;      /* Bonding information */
596   PIN_CODE pin_code;         /* PIN CODE if pin type is fixed */
597   bool connectable;          /* If true page scan should be enabled */
598   uint8_t def_inq_scan_mode; /* ??? limited/general/none */
599 } tBTM_CFG;
600 
601 enum {
602   BTM_PM_ST_ACTIVE = BTM_PM_STS_ACTIVE,
603   BTM_PM_ST_HOLD = BTM_PM_STS_HOLD,
604   BTM_PM_ST_SNIFF = BTM_PM_STS_SNIFF,
605   BTM_PM_ST_PARK = BTM_PM_STS_PARK,
606   BTM_PM_ST_PENDING = BTM_PM_STS_PENDING,
607   BTM_PM_ST_INVALID = 0xFF
608 };
609 typedef uint8_t tBTM_PM_STATE;
610 
611 enum {
612   BTM_PM_SET_MODE_EVT, /* Set power mode API is called. */
613   BTM_PM_UPDATE_EVT,
614   BTM_PM_RD_MODE_EVT /* Read power mode API is called. */
615 };
616 typedef uint8_t tBTM_PM_EVENT;
617 
618 typedef struct {
619   uint16_t event;
620   uint16_t len;
621   uint8_t link_ind;
622 } tBTM_PM_MSG_DATA;
623 
624 typedef struct {
625   uint8_t hci_status;
626   uint8_t mode;
627   uint16_t interval;
628 } tBTM_PM_MD_CHG_DATA;
629 
630 typedef struct {
631   uint8_t pm_id; /* the entity that calls SetPowerMode API */
632   tBTM_PM_PWR_MD* p_pmd;
633 } tBTM_PM_SET_MD_DATA;
634 
635 typedef struct {
636   void* p_data;
637   uint8_t link_ind;
638 } tBTM_PM_SM_DATA;
639 
640 typedef struct {
641   tBTM_PM_PWR_MD req_mode[BTM_MAX_PM_RECORDS + 1]; /* the desired mode and
642                                                       parameters of the
643                                                       connection*/
644   tBTM_PM_PWR_MD
645       set_mode; /* the mode and parameters sent down to the host controller. */
646   uint16_t interval; /* the interval from last mode change event. */
647 #if (BTM_SSR_INCLUDED == TRUE)
648   uint16_t max_lat;    /* stored SSR maximum latency */
649   uint16_t min_rmt_to; /* stored SSR minimum remote timeout */
650   uint16_t min_loc_to; /* stored SSR minimum local timeout */
651 #endif
652   tBTM_PM_STATE state; /* contains the current mode of the connection */
653   bool chg_ind;        /* a request change indication */
654 } tBTM_PM_MCB;
655 
656 #define BTM_PM_REC_NOT_USED 0
657 typedef struct {
658   tBTM_PM_STATUS_CBACK*
659       cback;    /* to notify the registered party of mode change event */
660   uint8_t mask; /* registered request mask. 0, if this entry is not used */
661 } tBTM_PM_RCB;
662 
663 enum {
664   BTM_BLI_ACL_UP_EVT,
665   BTM_BLI_ACL_DOWN_EVT,
666   BTM_BLI_PAGE_EVT,
667   BTM_BLI_PAGE_DONE_EVT,
668   BTM_BLI_INQ_EVT,
669   BTM_BLI_INQ_CANCEL_EVT,
670   BTM_BLI_INQ_DONE_EVT
671 };
672 typedef uint8_t tBTM_BLI_EVENT;
673 
674 /* Pairing State */
675 enum {
676   BTM_PAIR_STATE_IDLE, /* Idle                                         */
677   BTM_PAIR_STATE_GET_REM_NAME, /* Getting the remote name (to check for SM4) */
678   BTM_PAIR_STATE_WAIT_PIN_REQ, /* Started authentication, waiting for PIN req
679                                   (PIN is pre-fetched) */
680   BTM_PAIR_STATE_WAIT_LOCAL_PIN,       /* Waiting for local PIN code */
681   BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM, /* Waiting user 'yes' to numeric
682                                           confirmation   */
683   BTM_PAIR_STATE_KEY_ENTRY, /* Key entry state (we are a keyboard)          */
684   BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP, /* Waiting for local response to peer OOB
685                                         data  */
686   BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS, /* Waiting for local IO capabilities and OOB
687                                        data */
688   BTM_PAIR_STATE_INCOMING_SSP, /* Incoming SSP (got peer IO caps when idle) */
689   BTM_PAIR_STATE_WAIT_AUTH_COMPLETE, /* All done, waiting authentication
690                                         cpmplete    */
691   BTM_PAIR_STATE_WAIT_DISCONNECT     /* Waiting to disconnect the ACL */
692 };
693 typedef uint8_t tBTM_PAIRING_STATE;
694 
695 #define BTM_PAIR_FLAGS_WE_STARTED_DD \
696   0x01 /* We want to do dedicated bonding              */
697 #define BTM_PAIR_FLAGS_PEER_STARTED_DD \
698   0x02 /* Peer initiated dedicated bonding             */
699 #define BTM_PAIR_FLAGS_DISC_WHEN_DONE 0x04 /* Disconnect when done     */
700 #define BTM_PAIR_FLAGS_PIN_REQD \
701   0x08 /* set this bit when pin_callback is called     */
702 #define BTM_PAIR_FLAGS_PRE_FETCH_PIN \
703   0x10 /* set this bit when pre-fetch pin     */
704 #define BTM_PAIR_FLAGS_REJECTED_CONNECT \
705   0x20 /* set this bit when rejected incoming connection  */
706 #define BTM_PAIR_FLAGS_WE_CANCEL_DD \
707   0x40 /* set this bit when cancelling a bonding procedure */
708 #define BTM_PAIR_FLAGS_LE_ACTIVE \
709   0x80 /* use this bit when SMP pairing is active */
710 
711 typedef struct {
712   bool is_mux;
713   RawAddress bd_addr;
714   uint16_t psm;
715   bool is_orig;
716   tBTM_SEC_CALLBACK* p_callback;
717   void* p_ref_data;
718   uint32_t mx_proto_id;
719   uint32_t mx_chan_id;
720   tBT_TRANSPORT transport;
721   tBTM_BLE_SEC_ACT sec_act;
722 } tBTM_SEC_QUEUE_ENTRY;
723 
724 #define CONN_ORIENT_TERM false
725 #define CONN_ORIENT_ORIG true
726 typedef bool CONNECTION_TYPE;
727 
728 // Bluetooth Quality Report - Report receiver
729 typedef void(tBTM_BT_QUALITY_REPORT_RECEIVER)(uint8_t len, uint8_t* p_stream);
730 
731 /* Define a structure to hold all the BTM data
732 */
733 
734 #define BTM_STATE_BUFFER_SIZE 5 /* size of state buffer */
735 
736 typedef struct {
737   tBTM_CFG cfg; /* Device configuration */
738 
739   /****************************************************
740   **      ACL Management
741   ****************************************************/
742   tACL_CONN acl_db[MAX_L2CAP_LINKS];
743   uint8_t btm_scn[BTM_MAX_SCN]; /* current SCNs: true if SCN is in use */
744   uint16_t btm_def_link_policy;
745   uint16_t btm_def_link_super_tout;
746 
747   tBTM_BL_EVENT_MASK bl_evt_mask;
748   tBTM_BL_CHANGE_CB* p_bl_changed_cb; /* Callback for when Busy Level changed */
749 
750   /****************************************************
751   **      Power Management
752   ****************************************************/
753   tBTM_PM_MCB pm_mode_db[MAX_L2CAP_LINKS];       /* per ACL link */
754   tBTM_PM_RCB pm_reg_db[BTM_MAX_PM_RECORDS + 1]; /* per application/module */
755   uint8_t pm_pend_link; /* the index of acl_db, which has a pending PM cmd */
756   uint8_t pm_pend_id;   /* the id pf the module, which has a pending PM cmd */
757 
758   /*****************************************************
759   **      Device control
760   *****************************************************/
761   tBTM_DEVCB devcb;
762 
763   /*****************************************************
764   **      BLE Device controllers
765   *****************************************************/
766   tBTM_BLE_CB ble_ctr_cb;
767 
768   uint16_t enc_handle;
769   BT_OCTET8 enc_rand; /* received rand value from LTK request*/
770   uint16_t ediv;      /* received ediv value from LTK request */
771   uint8_t key_size;
772   tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
773 
774   /* Packet types supported by the local device */
775   uint16_t btm_acl_pkt_types_supported;
776   uint16_t btm_sco_pkt_types_supported;
777 
778   /*****************************************************
779   **      Inquiry
780   *****************************************************/
781   tBTM_INQUIRY_VAR_ST btm_inq_vars;
782 
783   /*****************************************************
784   **      SCO Management
785   *****************************************************/
786   tSCO_CB sco_cb;
787 
788   /*****************************************************
789   **      Security Management
790   *****************************************************/
791   tBTM_APPL_INFO api;
792 
793 #define BTM_SEC_MAX_RMT_NAME_CALLBACKS 2
794   tBTM_RMT_NAME_CALLBACK* p_rmt_name_callback[BTM_SEC_MAX_RMT_NAME_CALLBACKS];
795 
796   tBTM_SEC_DEV_REC* p_collided_dev_rec;
797   alarm_t* sec_collision_timer;
798   uint64_t collision_start_time;
799   uint32_t dev_rec_count; /* Counter used for device record timestamp */
800   uint8_t security_mode;
801   bool pairing_disabled;
802   bool connect_only_paired;
803   bool security_mode_changed; /* mode changed during bonding */
804   bool pin_type_changed;      /* pin type changed during bonding */
805   bool sec_req_pending;       /*   true if a request is pending */
806 
807   uint8_t pin_code_len;             /* for legacy devices */
808   PIN_CODE pin_code;                /* for legacy devices */
809   tBTM_PAIRING_STATE pairing_state; /* The current pairing state    */
810   uint8_t pairing_flags;            /* The current pairing flags    */
811   RawAddress pairing_bda;           /* The device currently pairing */
812   alarm_t* pairing_timer;           /* Timer for pairing process    */
813   uint16_t disc_handle;             /* for legacy devices */
814   uint8_t disc_reason;              /* for legacy devices */
815   tBTM_SEC_SERV_REC sec_serv_rec[BTM_SEC_MAX_SERVICE_RECORDS];
816   list_t* sec_dev_rec; /* list of tBTM_SEC_DEV_REC */
817   tBTM_SEC_SERV_REC* p_out_serv;
818   tBTM_MKEY_CALLBACK* mkey_cback;
819 
820   RawAddress connecting_bda;
821   DEV_CLASS connecting_dc;
822 
823   uint8_t acl_disc_reason;
824   uint8_t trace_level;
825   uint8_t busy_level; /* the current busy level */
826   bool is_paging;     /* true, if paging is in progess */
827   bool is_inquiry;    /* true, if inquiry is in progess */
828   fixed_queue_t* page_queue;
829   bool paging;
830   bool discing;
831   fixed_queue_t* sec_pending_q; /* pending sequrity requests in
832                                    tBTM_SEC_QUEUE_ENTRY format */
833 
834   char state_temp_buffer[BTM_STATE_BUFFER_SIZE];
835   // BQR Receiver
836   tBTM_BT_QUALITY_REPORT_RECEIVER* p_bqr_report_receiver;
837 } tBTM_CB;
838 
839 /* security action for L2CAP COC channels */
840 #define BTM_SEC_OK 1
841 #define BTM_SEC_ENCRYPT 2         /* encrypt the link with current key */
842 #define BTM_SEC_ENCRYPT_NO_MITM 3 /* unauthenticated encryption or better */
843 #define BTM_SEC_ENCRYPT_MITM 4    /* authenticated encryption */
844 #define BTM_SEC_ENC_PENDING 5     /* wait for link encryption pending */
845 
846 typedef uint8_t tBTM_SEC_ACTION;
847 
848 #endif  // BTM_INT_TYPES_H
849