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