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 "btm_api_types.h"
22 #include "btm_ble_api_types.h"
23 #include "btm_ble_int_types.h"
24 #include "hcidefs.h"
25 #include "osi/include/alarm.h"
26 #include "osi/include/list.h"
27 #include "rfcdefs.h"
28 
29 typedef char tBTM_LOC_BD_NAME[BTM_MAX_LOC_BD_NAME_LEN + 1];
30 
31 #define BTM_ACL_IS_CONNECTED(bda) \
32   (btm_bda_to_acl(bda, BT_TRANSPORT_BR_EDR) != NULL)
33 
34 /* Definitions for Server Channel Number (SCN) management
35 */
36 #define BTM_MAX_SCN PORT_MAX_RFC_PORTS
37 
38 /* Define masks for supported and exception 2.0 ACL packet types
39 */
40 #define BTM_ACL_SUPPORTED_PKTS_MASK                                           \
41   (HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH1 | HCI_PKT_TYPES_MASK_DM3 | \
42    HCI_PKT_TYPES_MASK_DH3 | HCI_PKT_TYPES_MASK_DM5 | HCI_PKT_TYPES_MASK_DH5)
43 
44 #define BTM_ACL_EXCEPTION_PKTS_MASK                            \
45   (HCI_PKT_TYPES_MASK_NO_2_DH1 | HCI_PKT_TYPES_MASK_NO_3_DH1 | \
46    HCI_PKT_TYPES_MASK_NO_2_DH3 | HCI_PKT_TYPES_MASK_NO_3_DH3 | \
47    HCI_PKT_TYPES_MASK_NO_2_DH5 | HCI_PKT_TYPES_MASK_NO_3_DH5)
48 
49 #define BTM_EPR_AVAILABLE(p)                                            \
50   ((HCI_ATOMIC_ENCRYPT_SUPPORTED((p)->peer_lmp_feature_pages[0]) &&     \
51     HCI_ATOMIC_ENCRYPT_SUPPORTED(                                       \
52         controller_get_interface()->get_features_classic(0)->as_array)) \
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   BT_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   uint32_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_LE_OBSERVE, 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_INACTIVE 0
247 #define BTM_RMT_NAME_EXT 0x1 /* Initiated through API */
248 #define BTM_RMT_NAME_SEC 0x2 /* Initiated internally by security manager */
249 #define BTM_RMT_NAME_INQ 0x4 /* Remote name initiated internally by inquiry */
250   bool remname_active; /* State of a remote name request by external API */
251 
252   tBTM_CMPL_CB* p_inq_cmpl_cb;
253   tBTM_INQ_RESULTS_CB* p_inq_results_cb;
254   tBTM_CMPL_CB*
255       p_inq_ble_cmpl_cb; /*completion callback exclusively for LE Observe*/
256   tBTM_INQ_RESULTS_CB*
257       p_inq_ble_results_cb; /*results callback exclusively for LE observe*/
258   tBTM_CMPL_CB* p_inqfilter_cmpl_cb; /* Called (if not NULL) after inquiry
259                                         filter completed */
260   uint32_t inq_counter; /* Counter incremented each time an inquiry completes */
261   /* Used for determining whether or not duplicate devices */
262   /* have responded to the same inquiry */
263   tINQ_BDADDR* p_bd_db;    /* Pointer to memory that holds bdaddrs */
264   uint16_t num_bd_entries; /* Number of entries in database */
265   uint16_t max_bd_entries; /* Maximum number of entries that can be stored */
266   tINQ_DB_ENT inq_db[BTM_INQ_DB_SIZE];
267   tBTM_INQ_PARMS inqparms; /* Contains the parameters for the current inquiry */
268   tBTM_INQUIRY_CMPL
269       inq_cmpl_info; /* Status and number of responses from the last inquiry */
270 
271   uint16_t per_min_delay; /* Current periodic minimum delay */
272   uint16_t per_max_delay; /* Current periodic maximum delay */
273   bool inqfilt_active;
274   uint8_t pending_filt_complete_event; /* to take care of
275                                           btm_event_filter_complete
276                                           corresponding to */
277   /* inquiry that has been cancelled*/
278   uint8_t inqfilt_type; /* Contains the inquiry filter type (BD ADDR, COD, or
279                            Clear) */
280 
281 #define BTM_INQ_INACTIVE_STATE 0
282 #define BTM_INQ_CLR_FILT_STATE \
283   1 /* Currently clearing the inquiry filter preceeding the inquiry request */
284     /* (bypassed if filtering is not used)                                  */
285 #define BTM_INQ_SET_FILT_STATE \
286   2 /* Sets the new filter (or turns off filtering) in this state */
287 #define BTM_INQ_ACTIVE_STATE \
288   3 /* Actual inquiry or periodic inquiry is in progress */
289 #define BTM_INQ_REMNAME_STATE 4 /* Remote name requests are active  */
290 
291   uint8_t state;      /* Current state that the inquiry process is in */
292   uint8_t inq_active; /* Bit Mask indicating type of inquiry is active */
293   bool no_inc_ssp;    /* true, to stop inquiry on incoming SSP */
294 } tBTM_INQUIRY_VAR_ST;
295 
296 /* The MSB of the clock offset field indicates whether the offset is valid. */
297 #define BTM_CLOCK_OFFSET_VALID 0x8000
298 
299 /* Define the structures needed by security management
300 */
301 
302 #define BTM_SEC_INVALID_HANDLE 0xFFFF
303 
304 typedef uint8_t* BTM_BD_NAME_PTR; /* Pointer to Device name */
305 
306 /* Security callback is called by this unit when security
307  *   procedures are completed.  Parameters are
308  *              BD Address of remote
309  *              Result of the operation
310 */
311 typedef tBTM_SEC_CBACK tBTM_SEC_CALLBACK;
312 
313 typedef void(tBTM_SCO_IND_CBACK)(uint16_t sco_inx);
314 
315 /* MACROs to convert from SCO packet types mask to ESCO and back */
316 #define BTM_SCO_PKT_TYPE_MASK \
317   (HCI_PKT_TYPES_MASK_HV1 | HCI_PKT_TYPES_MASK_HV2 | HCI_PKT_TYPES_MASK_HV3)
318 
319 /* Mask defining only the SCO types of an esco packet type */
320 #define BTM_ESCO_PKT_TYPE_MASK \
321   (ESCO_PKT_TYPES_MASK_HV1 | ESCO_PKT_TYPES_MASK_HV2 | ESCO_PKT_TYPES_MASK_HV3)
322 
323 #define BTM_SCO_2_ESCO(scotype) \
324   ((uint16_t)(((scotype)&BTM_SCO_PKT_TYPE_MASK) >> 5))
325 #define BTM_ESCO_2_SCO(escotype) \
326   ((uint16_t)(((escotype)&BTM_ESCO_PKT_TYPE_MASK) << 5))
327 
328 /* Define masks for supported and exception 2.0 SCO packet types
329 */
330 #define BTM_SCO_SUPPORTED_PKTS_MASK                    \
331   (ESCO_PKT_TYPES_MASK_HV1 | ESCO_PKT_TYPES_MASK_HV2 | \
332    ESCO_PKT_TYPES_MASK_HV3 | ESCO_PKT_TYPES_MASK_EV3 | \
333    ESCO_PKT_TYPES_MASK_EV4 | ESCO_PKT_TYPES_MASK_EV5)
334 
335 #define BTM_SCO_EXCEPTION_PKTS_MASK                              \
336   (ESCO_PKT_TYPES_MASK_NO_2_EV3 | ESCO_PKT_TYPES_MASK_NO_3_EV3 | \
337    ESCO_PKT_TYPES_MASK_NO_2_EV5 | ESCO_PKT_TYPES_MASK_NO_3_EV5)
338 
339 #define BTM_SCO_ROUTE_UNKNOWN 0xff
340 
341 /* Define the structure that contains (e)SCO data */
342 typedef struct {
343   tBTM_ESCO_CBACK* p_esco_cback; /* Callback for eSCO events     */
344   enh_esco_params_t setup;
345   tBTM_ESCO_DATA data; /* Connection complete information */
346   uint8_t hci_status;
347 } tBTM_ESCO_INFO;
348 
349 /* Define the structure used for SCO Management
350 */
351 typedef struct {
352   tBTM_ESCO_INFO esco; /* Current settings             */
353   tBTM_SCO_CB* p_conn_cb; /* Callback for when connected  */
354   tBTM_SCO_CB* p_disc_cb; /* Callback for when disconnect */
355   uint16_t state;         /* The state of the SCO link    */
356   uint16_t hci_handle;    /* HCI Handle                   */
357   bool is_orig;           /* true if the originator       */
358   bool rem_bd_known;      /* true if remote BD addr known */
359 
360 } tSCO_CONN;
361 
362 /* SCO Management control block */
363 typedef struct {
364   tBTM_SCO_IND_CBACK* app_sco_ind_cb;
365   tSCO_CONN sco_db[BTM_MAX_SCO_LINKS];
366   enh_esco_params_t def_esco_parms;
367   uint16_t sco_disc_reason;
368   bool esco_supported;        /* true if 1.2 cntlr AND supports eSCO links */
369   esco_data_path_t sco_route; /* HCI, PCM, or TEST */
370 } tSCO_CB;
371 
372 #if (BTM_SCO_INCLUDED == TRUE)
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 #else
381 #define btm_accept_sco_link(sco_inx, p_setup, p_conn_cb, p_disc_cb)
382 #define btm_reject_sco_link(sco_inx)
383 #define btm_set_sco_ind_cback(sco_ind_cb)
384 #define btm_sco_chk_pend_rolechange(hci_handle)
385 #endif /* BTM_SCO_INCLUDED */
386 
387 /*
388  * Define structure for Security Service Record.
389  * A record exists for each service registered with the Security Manager
390 */
391 #define BTM_SEC_OUT_FLAGS \
392   (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHORIZE)
393 #define BTM_SEC_IN_FLAGS \
394   (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHORIZE)
395 
396 #define BTM_SEC_OUT_LEVEL4_FLAGS                                       \
397   (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_MITM | \
398    BTM_SEC_MODE4_LEVEL4)
399 
400 #define BTM_SEC_IN_LEVEL4_FLAGS                                     \
401   (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_MITM | \
402    BTM_SEC_MODE4_LEVEL4)
403 typedef struct {
404   uint32_t mx_proto_id;     /* Service runs over this multiplexer protocol */
405   uint32_t orig_mx_chan_id; /* Channel on the multiplexer protocol    */
406   uint32_t term_mx_chan_id; /* Channel on the multiplexer protocol    */
407   uint16_t psm;             /* L2CAP PSM value */
408   uint16_t security_flags;  /* Bitmap of required security features */
409   uint8_t service_id;       /* Passed in authorization callback */
410 #if BTM_SEC_SERVICE_NAME_LEN > 0
411   uint8_t orig_service_name[BTM_SEC_SERVICE_NAME_LEN + 1];
412   uint8_t term_service_name[BTM_SEC_SERVICE_NAME_LEN + 1];
413 #endif
414 } tBTM_SEC_SERV_REC;
415 
416 /* LE Security information of device in Slave Role */
417 typedef struct {
418   BT_OCTET16 irk;   /* peer diverified identity root */
419   BT_OCTET16 pltk;  /* peer long term key */
420   BT_OCTET16 pcsrk; /* peer SRK peer device used to secured sign local data  */
421 
422   BT_OCTET16 lltk;  /* local long term key */
423   BT_OCTET16 lcsrk; /* local SRK peer device used to secured sign local data  */
424 
425   BT_OCTET8 rand;        /* random vector for LTK generation */
426   uint16_t ediv;         /* LTK diversifier of this slave device */
427   uint16_t div;          /* local DIV  to generate local LTK=d1(ER,DIV,0) and
428                             CSRK=d1(ER,DIV,1)  */
429   uint8_t sec_level;     /* local pairing security level */
430   uint8_t key_size;      /* key size of the LTK delivered to peer device */
431   uint8_t srk_sec_level; /* security property of peer SRK for this device */
432   uint8_t local_csrk_sec_level; /* security property of local CSRK for this
433                                    device */
434 
435   uint32_t counter;       /* peer sign counter for verifying rcv signed cmd */
436   uint32_t local_counter; /* local sign counter for sending signed write cmd*/
437 } tBTM_SEC_BLE_KEYS;
438 
439 typedef struct {
440   RawAddress pseudo_addr; /* LE pseudo address of the device if different from
441                           device address  */
442   tBLE_ADDR_TYPE ble_addr_type; /* LE device type: public or random address */
443   tBLE_ADDR_TYPE static_addr_type; /* static address type */
444   RawAddress static_addr;          /* static address */
445 
446 #define BTM_WHITE_LIST_BIT 0x01
447 #define BTM_RESOLVING_LIST_BIT 0x02
448   uint8_t in_controller_list; /* in controller resolving list or not */
449   uint8_t resolving_list_index;
450 #if (BLE_PRIVACY_SPT == TRUE)
451   RawAddress cur_rand_addr; /* current random address */
452 
453 #define BTM_BLE_ADDR_PSEUDO 0 /* address index device record */
454 #define BTM_BLE_ADDR_RRA 1    /* cur_rand_addr */
455 #define BTM_BLE_ADDR_STATIC 2 /* static_addr  */
456   uint8_t active_addr_type;
457 #endif
458 
459   tBTM_LE_KEY_TYPE key_type; /* bit mask of valid key types in record */
460   tBTM_SEC_BLE_KEYS keys;    /* LE device security info in slave rode */
461 } tBTM_SEC_BLE;
462 
463 /* Peering bond type */
464 enum { BOND_TYPE_UNKNOWN, BOND_TYPE_PERSISTENT, BOND_TYPE_TEMPORARY };
465 typedef uint8_t tBTM_BOND_TYPE;
466 
467 /*
468  * Define structure for Security Device Record.
469  * A record exists for each device authenticated with this device
470 */
471 typedef struct {
472   tBTM_SEC_SERV_REC* p_cur_service;
473   tBTM_SEC_CALLBACK* p_callback;
474   void* p_ref_data;
475   uint32_t timestamp; /* Timestamp of the last connection   */
476   uint32_t trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE]; /* Bitwise OR of trusted
477                                                         services     */
478   uint16_t hci_handle;     /* Handle to connection when exists   */
479   uint16_t clock_offset;   /* Latest known clock offset          */
480   RawAddress bd_addr;      /* BD_ADDR of the device              */
481   DEV_CLASS dev_class;     /* DEV_CLASS of the device            */
482   LINK_KEY link_key;       /* Device link key                    */
483   uint8_t pin_code_length; /* Length of the pin_code used for paring */
484 
485 #define BTM_SEC_AUTHORIZED BTM_SEC_FLAG_AUTHORIZED       /* 0x01 */
486 #define BTM_SEC_AUTHENTICATED BTM_SEC_FLAG_AUTHENTICATED /* 0x02 */
487 #define BTM_SEC_ENCRYPTED BTM_SEC_FLAG_ENCRYPTED         /* 0x04 */
488 #define BTM_SEC_NAME_KNOWN 0x08
489 #define BTM_SEC_LINK_KEY_KNOWN BTM_SEC_FLAG_LKEY_KNOWN   /* 0x10 */
490 #define BTM_SEC_LINK_KEY_AUTHED BTM_SEC_FLAG_LKEY_AUTHED /* 0x20 */
491 #define BTM_SEC_ROLE_SWITCHED 0x40
492 #define BTM_SEC_IN_USE 0x80
493 /* LE link security flag */
494 #define BTM_SEC_LE_AUTHENTICATED \
495   0x0200 /* LE link is encrypted after pairing with MITM */
496 #define BTM_SEC_LE_ENCRYPTED 0x0400  /* LE link is encrypted */
497 #define BTM_SEC_LE_NAME_KNOWN 0x0800 /* not used */
498 #define BTM_SEC_LE_LINK_KEY_KNOWN \
499   0x1000 /* bonded with peer (peer LTK and/or SRK is saved) */
500 #define BTM_SEC_LE_LINK_KEY_AUTHED 0x2000 /* pairing is done with MITM */
501 #define BTM_SEC_16_DIGIT_PIN_AUTHED \
502   0x4000 /* pairing is done with 16 digit pin */
503 
504   uint16_t sec_flags; /* Current device security state      */
505 
506   tBTM_BD_NAME sec_bd_name; /* User friendly name of the device. (may be
507                                truncated to save space in dev_rec table) */
508   BD_FEATURES feature_pages[HCI_EXT_FEATURES_PAGE_MAX +
509                             1]; /* Features supported by the device */
510   uint8_t num_read_pages;
511 
512 #define BTM_SEC_STATE_IDLE 0
513 #define BTM_SEC_STATE_AUTHENTICATING 1
514 #define BTM_SEC_STATE_ENCRYPTING 2
515 #define BTM_SEC_STATE_GETTING_NAME 3
516 #define BTM_SEC_STATE_AUTHORIZING 4
517 #define BTM_SEC_STATE_SWITCHING_ROLE 5
518 #define BTM_SEC_STATE_DISCONNECTING 6 /* disconnecting BR/EDR */
519 #define BTM_SEC_STATE_DELAY_FOR_ENC \
520   7 /* delay to check for encryption to work around */
521     /* controller problems */
522 #define BTM_SEC_STATE_DISCONNECTING_BLE 8  /* disconnecting BLE */
523 #define BTM_SEC_STATE_DISCONNECTING_BOTH 9 /* disconnecting BR/EDR and BLE */
524 
525   uint8_t sec_state;  /* Operating state                    */
526   bool is_originator; /* true if device is originating connection */
527   bool role_master;           /* true if current mode is master     */
528   uint16_t security_required; /* Security required for connection   */
529   bool link_key_not_sent; /* link key notification has not been sent waiting for
530                              name */
531   uint8_t link_key_type;  /* Type of key used in pairing   */
532   bool link_key_changed;  /* Changed link key during current connection */
533 
534 #define BTM_MAX_PRE_SM4_LKEY_TYPE \
535   BTM_LKEY_TYPE_REMOTE_UNIT /* the link key type used by legacy pairing */
536 
537 #define BTM_SM4_UNKNOWN 0x00
538 #define BTM_SM4_KNOWN 0x10
539 #define BTM_SM4_TRUE 0x11
540 #define BTM_SM4_REQ_PEND 0x08 /* set this bit when getting remote features */
541 #define BTM_SM4_UPGRADE 0x04  /* set this bit when upgrading link key */
542 #define BTM_SM4_RETRY                                     \
543   0x02 /* set this bit to retry on HCI_ERR_KEY_MISSING or \
544           HCI_ERR_LMP_ERR_TRANS_COLLISION */
545 #define BTM_SM4_DD_ACP \
546   0x20 /* set this bit to indicate peer initiated dedicated bonding */
547 #define BTM_SM4_CONN_PEND                                               \
548   0x40 /* set this bit to indicate accepting acl conn; to be cleared on \
549           btm_acl_created */
550   uint8_t sm4;                /* BTM_SM4_TRUE, if the peer supports SM4 */
551   tBTM_IO_CAP rmt_io_caps;    /* IO capability of the peer device */
552   tBTM_AUTH_REQ rmt_auth_req; /* the auth_req flag as in the IO caps rsp evt */
553   bool remote_supports_secure_connections;
554   bool remote_features_needed; /* set to true if the local device is in */
555   /* "Secure Connections Only" mode and it receives */
556   /* HCI_IO_CAPABILITY_REQUEST_EVT from the peer before */
557   /* it knows peer's support for Secure Connections */
558 
559   uint16_t ble_hci_handle; /* use in DUMO connection */
560   uint8_t enc_key_size;    /* current link encryption key size */
561   tBT_DEVICE_TYPE device_type;
562   bool new_encryption_key_is_p256; /* Set to true when the newly generated LK
563                                    ** is generated from P-256.
564                                    ** Link encrypted with such LK can be used
565                                    ** for SM over BR/EDR.
566                                    */
567   bool no_smp_on_br;        /* if set to true then SMP on BR/EDR doesn't */
568                             /* work, i.e. link keys crosspairing */
569                             /* SC BR/EDR->SC LE doesn't happen */
570   tBTM_BOND_TYPE bond_type; /* peering bond type */
571 
572   tBTM_SEC_BLE ble;
573   tBTM_LE_CONN_PRAMS conn_params;
574 
575 #if (BTM_DISC_DURING_RS == TRUE)
576 #define BTM_SEC_RS_NOT_PENDING 0 /* Role Switch not in progress */
577 #define BTM_SEC_RS_PENDING 1     /* Role Switch in progress */
578 #define BTM_SEC_DISC_PENDING 2   /* Disconnect is pending */
579   uint8_t rs_disc_pending;
580 #endif
581 #define BTM_SEC_NO_LAST_SERVICE_ID 0
582   uint8_t last_author_service_id; /* ID of last serviced authorized: Reset after
583                                      each l2cap connection */
584 
585 } tBTM_SEC_DEV_REC;
586 
587 #define BTM_SEC_IS_SM4(sm) ((bool)(BTM_SM4_TRUE == ((sm)&BTM_SM4_TRUE)))
588 #define BTM_SEC_IS_SM4_LEGACY(sm) ((bool)(BTM_SM4_KNOWN == ((sm)&BTM_SM4_TRUE)))
589 #define BTM_SEC_IS_SM4_UNKNOWN(sm) \
590   ((bool)(BTM_SM4_UNKNOWN == ((sm)&BTM_SM4_TRUE)))
591 
592 #define BTM_SEC_LE_MASK                              \
593   (BTM_SEC_LE_AUTHENTICATED | BTM_SEC_LE_ENCRYPTED | \
594    BTM_SEC_LE_LINK_KEY_KNOWN | BTM_SEC_LE_LINK_KEY_AUTHED)
595 
596 /*
597  * Define device configuration structure
598 */
599 typedef struct {
600   tBTM_LOC_BD_NAME bd_name;  /* local Bluetooth device name */
601   bool pin_type;             /* true if PIN type is fixed */
602   uint8_t pin_code_len;      /* Bonding information */
603   PIN_CODE pin_code;         /* PIN CODE if pin type is fixed */
604   bool connectable;          /* If true page scan should be enabled */
605   uint8_t def_inq_scan_mode; /* ??? limited/general/none */
606 } tBTM_CFG;
607 
608 enum {
609   BTM_PM_ST_ACTIVE = BTM_PM_STS_ACTIVE,
610   BTM_PM_ST_HOLD = BTM_PM_STS_HOLD,
611   BTM_PM_ST_SNIFF = BTM_PM_STS_SNIFF,
612   BTM_PM_ST_PARK = BTM_PM_STS_PARK,
613   BTM_PM_ST_PENDING = BTM_PM_STS_PENDING,
614   BTM_PM_ST_INVALID = 0xFF
615 };
616 typedef uint8_t tBTM_PM_STATE;
617 
618 enum {
619   BTM_PM_SET_MODE_EVT, /* Set power mode API is called. */
620   BTM_PM_UPDATE_EVT,
621   BTM_PM_RD_MODE_EVT /* Read power mode API is called. */
622 };
623 typedef uint8_t tBTM_PM_EVENT;
624 
625 typedef struct {
626   uint16_t event;
627   uint16_t len;
628   uint8_t link_ind;
629 } tBTM_PM_MSG_DATA;
630 
631 typedef struct {
632   uint8_t hci_status;
633   uint8_t mode;
634   uint16_t interval;
635 } tBTM_PM_MD_CHG_DATA;
636 
637 typedef struct {
638   uint8_t pm_id; /* the entity that calls SetPowerMode API */
639   tBTM_PM_PWR_MD* p_pmd;
640 } tBTM_PM_SET_MD_DATA;
641 
642 typedef struct {
643   void* p_data;
644   uint8_t link_ind;
645 } tBTM_PM_SM_DATA;
646 
647 typedef struct {
648   tBTM_PM_PWR_MD req_mode[BTM_MAX_PM_RECORDS + 1]; /* the desired mode and
649                                                       parameters of the
650                                                       connection*/
651   tBTM_PM_PWR_MD
652       set_mode; /* the mode and parameters sent down to the host controller. */
653   uint16_t interval; /* the interval from last mode change event. */
654 #if (BTM_SSR_INCLUDED == TRUE)
655   uint16_t max_lat;    /* stored SSR maximum latency */
656   uint16_t min_rmt_to; /* stored SSR minimum remote timeout */
657   uint16_t min_loc_to; /* stored SSR minimum local timeout */
658 #endif
659   tBTM_PM_STATE state; /* contains the current mode of the connection */
660   bool chg_ind;        /* a request change indication */
661 } tBTM_PM_MCB;
662 
663 #define BTM_PM_REC_NOT_USED 0
664 typedef struct {
665   tBTM_PM_STATUS_CBACK*
666       cback;    /* to notify the registered party of mode change event */
667   uint8_t mask; /* registered request mask. 0, if this entry is not used */
668 } tBTM_PM_RCB;
669 
670 enum {
671   BTM_BLI_ACL_UP_EVT,
672   BTM_BLI_ACL_DOWN_EVT,
673   BTM_BLI_PAGE_EVT,
674   BTM_BLI_PAGE_DONE_EVT,
675   BTM_BLI_INQ_EVT,
676   BTM_BLI_INQ_CANCEL_EVT,
677   BTM_BLI_INQ_DONE_EVT
678 };
679 typedef uint8_t tBTM_BLI_EVENT;
680 
681 /* Pairing State */
682 enum {
683   BTM_PAIR_STATE_IDLE, /* Idle                                         */
684   BTM_PAIR_STATE_GET_REM_NAME, /* Getting the remote name (to check for SM4) */
685   BTM_PAIR_STATE_WAIT_PIN_REQ, /* Started authentication, waiting for PIN req
686                                   (PIN is pre-fetched) */
687   BTM_PAIR_STATE_WAIT_LOCAL_PIN,       /* Waiting for local PIN code */
688   BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM, /* Waiting user 'yes' to numeric
689                                           confirmation   */
690   BTM_PAIR_STATE_KEY_ENTRY, /* Key entry state (we are a keyboard)          */
691   BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP, /* Waiting for local response to peer OOB
692                                         data  */
693   BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS, /* Waiting for local IO capabilities and OOB
694                                        data */
695   BTM_PAIR_STATE_INCOMING_SSP, /* Incoming SSP (got peer IO caps when idle) */
696   BTM_PAIR_STATE_WAIT_AUTH_COMPLETE, /* All done, waiting authentication
697                                         cpmplete    */
698   BTM_PAIR_STATE_WAIT_DISCONNECT     /* Waiting to disconnect the ACL */
699 };
700 typedef uint8_t tBTM_PAIRING_STATE;
701 
702 #define BTM_PAIR_FLAGS_WE_STARTED_DD \
703   0x01 /* We want to do dedicated bonding              */
704 #define BTM_PAIR_FLAGS_PEER_STARTED_DD \
705   0x02 /* Peer initiated dedicated bonding             */
706 #define BTM_PAIR_FLAGS_DISC_WHEN_DONE 0x04 /* Disconnect when done     */
707 #define BTM_PAIR_FLAGS_PIN_REQD \
708   0x08 /* set this bit when pin_callback is called     */
709 #define BTM_PAIR_FLAGS_PRE_FETCH_PIN \
710   0x10 /* set this bit when pre-fetch pin     */
711 #define BTM_PAIR_FLAGS_REJECTED_CONNECT \
712   0x20 /* set this bit when rejected incoming connection  */
713 #define BTM_PAIR_FLAGS_WE_CANCEL_DD \
714   0x40 /* set this bit when cancelling a bonding procedure */
715 #define BTM_PAIR_FLAGS_LE_ACTIVE \
716   0x80 /* use this bit when SMP pairing is active */
717 
718 typedef struct {
719   bool is_mux;
720   RawAddress bd_addr;
721   uint16_t psm;
722   bool is_orig;
723   tBTM_SEC_CALLBACK* p_callback;
724   void* p_ref_data;
725   uint32_t mx_proto_id;
726   uint32_t mx_chan_id;
727   tBT_TRANSPORT transport;
728   tBTM_BLE_SEC_ACT sec_act;
729 } tBTM_SEC_QUEUE_ENTRY;
730 
731 #define CONN_ORIENT_TERM false
732 #define CONN_ORIENT_ORIG true
733 typedef bool CONNECTION_TYPE;
734 
735 /* Define a structure to hold all the BTM data
736 */
737 
738 #define BTM_STATE_BUFFER_SIZE 5 /* size of state buffer */
739 
740 typedef struct {
741   tBTM_CFG cfg; /* Device configuration */
742 
743   /****************************************************
744   **      ACL Management
745   ****************************************************/
746   tACL_CONN acl_db[MAX_L2CAP_LINKS];
747   uint8_t btm_scn[BTM_MAX_SCN]; /* current SCNs: true if SCN is in use */
748   uint16_t btm_def_link_policy;
749   uint16_t btm_def_link_super_tout;
750 
751   tBTM_BL_EVENT_MASK bl_evt_mask;
752   tBTM_BL_CHANGE_CB* p_bl_changed_cb; /* Callback for when Busy Level changed */
753 
754   /****************************************************
755   **      Power Management
756   ****************************************************/
757   tBTM_PM_MCB pm_mode_db[MAX_L2CAP_LINKS];       /* per ACL link */
758   tBTM_PM_RCB pm_reg_db[BTM_MAX_PM_RECORDS + 1]; /* per application/module */
759   uint8_t pm_pend_link; /* the index of acl_db, which has a pending PM cmd */
760   uint8_t pm_pend_id;   /* the id pf the module, which has a pending PM cmd */
761 
762   /*****************************************************
763   **      Device control
764   *****************************************************/
765   tBTM_DEVCB devcb;
766 
767   /*****************************************************
768   **      BLE Device controllers
769   *****************************************************/
770   tBTM_BLE_CB ble_ctr_cb;
771 
772   uint16_t enc_handle;
773   BT_OCTET8 enc_rand; /* received rand value from LTK request*/
774   uint16_t ediv;      /* received ediv value from LTK request */
775   uint8_t key_size;
776   tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
777 
778   /* Packet types supported by the local device */
779   uint16_t btm_acl_pkt_types_supported;
780   uint16_t btm_sco_pkt_types_supported;
781 
782   /*****************************************************
783   **      Inquiry
784   *****************************************************/
785   tBTM_INQUIRY_VAR_ST btm_inq_vars;
786 
787 /*****************************************************
788 **      SCO Management
789 *****************************************************/
790 #if (BTM_SCO_INCLUDED == TRUE)
791   tSCO_CB sco_cb;
792 #endif
793 
794   /*****************************************************
795   **      Security Management
796   *****************************************************/
797   tBTM_APPL_INFO api;
798 
799 #define BTM_SEC_MAX_RMT_NAME_CALLBACKS 2
800   tBTM_RMT_NAME_CALLBACK* p_rmt_name_callback[BTM_SEC_MAX_RMT_NAME_CALLBACKS];
801 
802   tBTM_SEC_DEV_REC* p_collided_dev_rec;
803   alarm_t* sec_collision_timer;
804   uint32_t collision_start_time;
805   uint32_t max_collision_delay;
806   uint32_t dev_rec_count; /* Counter used for device record timestamp */
807   uint8_t security_mode;
808   bool pairing_disabled;
809   bool connect_only_paired;
810   bool security_mode_changed; /* mode changed during bonding */
811   bool pin_type_changed;      /* pin type changed during bonding */
812   bool sec_req_pending;       /*   true if a request is pending */
813 
814   uint8_t pin_code_len;             /* for legacy devices */
815   PIN_CODE pin_code;                /* for legacy devices */
816   tBTM_PAIRING_STATE pairing_state; /* The current pairing state    */
817   uint8_t pairing_flags;            /* The current pairing flags    */
818   RawAddress pairing_bda;           /* The device currently pairing */
819   alarm_t* pairing_timer;           /* Timer for pairing process    */
820   uint16_t disc_handle;             /* for legacy devices */
821   uint8_t disc_reason;              /* for legacy devices */
822   tBTM_SEC_SERV_REC sec_serv_rec[BTM_SEC_MAX_SERVICE_RECORDS];
823   list_t* sec_dev_rec; /* list of tBTM_SEC_DEV_REC */
824   tBTM_SEC_SERV_REC* p_out_serv;
825   tBTM_MKEY_CALLBACK* mkey_cback;
826 
827   RawAddress connecting_bda;
828   DEV_CLASS connecting_dc;
829 
830   uint8_t acl_disc_reason;
831   uint8_t trace_level;
832   uint8_t busy_level; /* the current busy level */
833   bool is_paging;     /* true, if paging is in progess */
834   bool is_inquiry;    /* true, if inquiry is in progess */
835   fixed_queue_t* page_queue;
836   bool paging;
837   bool discing;
838   fixed_queue_t* sec_pending_q; /* pending sequrity requests in
839                                    tBTM_SEC_QUEUE_ENTRY format */
840 
841   char state_temp_buffer[BTM_STATE_BUFFER_SIZE];
842 } tBTM_CB;
843 
844 /* security action for L2CAP COC channels */
845 #define BTM_SEC_OK 1
846 #define BTM_SEC_ENCRYPT 2         /* encrypt the link with current key */
847 #define BTM_SEC_ENCRYPT_NO_MITM 3 /* unauthenticated encryption or better */
848 #define BTM_SEC_ENCRYPT_MITM 4    /* authenticated encryption */
849 #define BTM_SEC_ENC_PENDING 5     /* wait for link encryption pending */
850 
851 typedef uint8_t tBTM_SEC_ACTION;
852 
853 #endif  // BTM_INT_TYPES_H
854