1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  this file contains the main Bluetooth Manager (BTM) internal
22  *  definitions.
23  *
24  ******************************************************************************/
25 #ifndef BTM_INT_H
26 #define BTM_INT_H
27 
28 typedef struct tBTM_SEC_DEV_REC tBTM_SEC_DEV_REC;
29 
30 #include "common/bt_defs.h"
31 #include "common/bt_target.h"
32 #include "stack/hcidefs.h"
33 
34 #include "stack/rfcdefs.h"
35 
36 #include "stack/btm_api.h"
37 #include "osi/fixed_queue.h"
38 
39 #if (BLE_INCLUDED == TRUE)
40 #include "btm_ble_int.h"
41 #endif
42 #if (SMP_INCLUDED == TRUE)
43 #include "stack/smp_api.h"
44 #endif
45 
46 #define ESP_VS_REM_LEGACY_AUTH_CMP 0x03
47 
48 #if BTM_MAX_LOC_BD_NAME_LEN > 0
49 typedef char tBTM_LOC_BD_NAME[BTM_MAX_LOC_BD_NAME_LEN + 1];
50 #endif
51 
52 #define  BTM_ACL_IS_CONNECTED(bda)   (btm_bda_to_acl (bda, BT_TRANSPORT_BR_EDR) != NULL)
53 
54 /* Definitions for Server Channel Number (SCN) management
55 */
56 #define BTM_MAX_SCN      PORT_MAX_RFC_PORTS
57 
58 /* Define masks for supported and exception 2.0 ACL packet types
59 */
60 #define BTM_ACL_SUPPORTED_PKTS_MASK      (HCI_PKT_TYPES_MASK_DM1        | \
61                                           HCI_PKT_TYPES_MASK_DH1        | \
62                                           HCI_PKT_TYPES_MASK_DM3        | \
63                                           HCI_PKT_TYPES_MASK_DH3        | \
64                                           HCI_PKT_TYPES_MASK_DM5        | \
65                                           HCI_PKT_TYPES_MASK_DH5)
66 
67 #define BTM_ACL_EXCEPTION_PKTS_MASK      (HCI_PKT_TYPES_MASK_NO_2_DH1   | \
68                                           HCI_PKT_TYPES_MASK_NO_3_DH1   | \
69                                           HCI_PKT_TYPES_MASK_NO_2_DH3   | \
70                                           HCI_PKT_TYPES_MASK_NO_3_DH3   | \
71                                           HCI_PKT_TYPES_MASK_NO_2_DH5   | \
72                                           HCI_PKT_TYPES_MASK_NO_3_DH5)
73 
74 #define BTM_EPR_AVAILABLE(p) ((HCI_ATOMIC_ENCRYPT_SUPPORTED((p)->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]) && \
75                                HCI_ATOMIC_ENCRYPT_SUPPORTED(controller_get_interface()->get_features_classic(0)->as_array)) \
76                                ? TRUE : FALSE)
77 
78 #define BTM_IS_BRCM_CONTROLLER() (controller_get_interface()->get_bt_version()->manufacturer == LMP_COMPID_BROADCOM)
79 
80 typedef struct t_acl_db_param{
81 #define ACL_DB_HANDLE 0x00
82 #define ACL_DB_BDA    0x01
83     UINT8 type;
84     void *p_data1;
85     void *p_data2;
86 }tACL_DB_PARAM;
87 
88 enum {
89     BTM_PM_ST_ACTIVE  = BTM_PM_STS_ACTIVE,
90     BTM_PM_ST_HOLD    = BTM_PM_STS_HOLD,
91     BTM_PM_ST_SNIFF   = BTM_PM_STS_SNIFF,
92     BTM_PM_ST_PARK    = BTM_PM_STS_PARK,
93     BTM_PM_ST_PENDING = BTM_PM_STS_PENDING
94 };
95 typedef UINT8 tBTM_PM_STATE;
96 
97 typedef struct {
98     tBTM_PM_PWR_MD req_mode[BTM_MAX_PM_RECORDS + 1]; /* the desired mode and parameters of the connection*/
99     tBTM_PM_PWR_MD set_mode;  /* the mode and parameters sent down to the host controller. */
100     UINT16         interval;  /* the interval from last mode change event. */
101 #if (BTM_SSR_INCLUDED == TRUE)
102     UINT16         max_lat;   /* stored SSR maximum latency */
103     UINT16         min_rmt_to;/* stored SSR minimum remote timeout */
104     UINT16         min_loc_to;/* stored SSR minimum local timeout */
105 #endif
106     tBTM_PM_STATE  state;     /* contains the current mode of the connection */
107     BOOLEAN        chg_ind;   /* a request change indication */
108 } tBTM_PM_MCB;
109 
110 /* Define the ACL Management control structure
111 */
112 typedef struct {
113 UINT16          hci_handle;
114 UINT16          pkt_types_mask;
115 UINT16          clock_offset;
116 BD_ADDR         remote_addr;
117 DEV_CLASS       remote_dc;
118 BD_NAME         remote_name;
119 
120 UINT16          manufacturer;
121 UINT16          lmp_subversion;
122 UINT16          link_super_tout;
123 BD_FEATURES     peer_lmp_features[HCI_EXT_FEATURES_PAGE_MAX + 1];    /* Peer LMP Extended features mask table for the device */
124 UINT8           num_read_pages;
125 UINT8           lmp_version;
126 
127 BOOLEAN         in_use;
128 UINT8           link_role;
129 BOOLEAN         link_up_issued;     /* True if busy_level link up has been issued */
130 BOOLEAN         sc_downgrade;       /* Store if security is downgraded or not. */
131 
132 #define BTM_ACL_LEGACY_AUTH_NONE                (0)
133 #define BTM_ACL_LEGACY_AUTH_SELF                (1<<0)
134 #define BTM_ACL_LEGACY_AUTH_REMOTE              (1<<1)
135 #define BTM_ACL_LEGACY_AUTH_MUTUAL              (1<<2)
136 UINT8           legacy_auth_state;
137 
138 #define BTM_ACL_SWKEY_STATE_IDLE                0
139 #define BTM_ACL_SWKEY_STATE_MODE_CHANGE         1
140 #define BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF      2
141 #define BTM_ACL_SWKEY_STATE_SWITCHING           3
142 #define BTM_ACL_SWKEY_STATE_ENCRYPTION_ON       4
143 #define BTM_ACL_SWKEY_STATE_IN_PROGRESS         5
144 UINT8           switch_role_state;
145 
146 #define BTM_ACL_ENCRYPT_STATE_IDLE              0
147 #define BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF       1   /* encryption turning off */
148 #define BTM_ACL_ENCRYPT_STATE_TEMP_FUNC         2   /* temporarily off for change link key or role switch */
149 #define BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON        3   /* encryption turning on */
150 UINT8           encrypt_state;                  /* overall BTM encryption state */
151 
152 #if BLE_INCLUDED == TRUE
153 tBT_TRANSPORT   transport;
154 BD_ADDR         conn_addr;              /* local device address used for this connection */
155 UINT8           conn_addr_type;         /* local device address type for this connection */
156 BD_ADDR         active_remote_addr;     /* remote address used on this connection */
157 UINT8           active_remote_addr_type;         /* local device address type for this connection */
158 BD_FEATURES     peer_le_features;       /* Peer LE Used features mask for the device */
159 tBTM_SET_PKT_DATA_LENGTH_CBACK *p_set_pkt_data_cback;
160 tBTM_LE_SET_PKT_DATA_LENGTH_PARAMS data_length_params;
161 BOOLEAN   data_len_updating;
162 // data len update cmd cache
163 BOOLEAN   data_len_waiting;
164 tBTM_SET_PKT_DATA_LENGTH_CBACK *p_set_data_len_cback_waiting;
165 UINT16 tx_len_waiting;
166 #endif
167 tBTM_PM_MCB     *p_pm_mode_db;          /* Pointer to PM mode control block per ACL link */
168 
169 } tACL_CONN;
170 
171 /*****************************************************
172 ** TIMER Definitions
173 ******************************************************/
174 #define TT_DEV_RESET     1
175 #define TT_DEV_RLN       2
176 #define TT_DEV_RLNKP     4              /* Read Link Policy Settings */
177 
178 /* Define the Device Management control structure
179 */
180 typedef struct {
181 tBTM_DEV_STATUS_CB  *p_dev_status_cb;   /* Device status change callback        */
182 tBTM_VS_EVT_CB      *p_vend_spec_cb[BTM_MAX_VSE_CALLBACKS];     /* Register for vendor specific events  */
183 
184 tBTM_CMPL_CB        *p_stored_link_key_cmpl_cb;   /* Read/Write/Delete stored link key    */
185 
186 TIMER_LIST_ENT       reset_timer;
187 tBTM_CMPL_CB         *p_reset_cmpl_cb;
188 
189 TIMER_LIST_ENT       rln_timer;
190 tBTM_CMPL_CB        *p_rln_cmpl_cb;     /* Callback function to be called when  */
191 /* read local name function complete    */
192 TIMER_LIST_ENT       rssi_timer;
193 tBTM_CMPL_CB        *p_rssi_cmpl_cb;    /* Callback function to be called when  */
194 /* read rssi function completes         */
195 TIMER_LIST_ENT       lnk_quality_timer;
196 tBTM_CMPL_CB        *p_lnk_qual_cmpl_cb;/* Callback function to be called when  */
197 /* read link quality function completes */
198 TIMER_LIST_ENT       txpwer_timer;
199 tBTM_CMPL_CB        *p_txpwer_cmpl_cb;    /* Callback function to be called when  */
200 /* read inq tx power function completes  */
201 
202 TIMER_LIST_ENT       qossu_timer;
203 tBTM_CMPL_CB        *p_qossu_cmpl_cb;   /* Callback function to be called when  */
204 /* qos setup function completes         */
205 
206 tBTM_ROLE_SWITCH_CMPL switch_role_ref_data;
207 tBTM_CMPL_CB        *p_switch_role_cb;  /* Callback function to be called when  */
208 /* requested switch role is completed   */
209 
210 TIMER_LIST_ENT       tx_power_timer;
211 tBTM_CMPL_CB        *p_tx_power_cmpl_cb;/* Callback function to be called       */
212 
213 #if CLASSIC_BT_INCLUDED == TRUE
214 TIMER_LIST_ENT       afh_channels_timer;
215 tBTM_CMPL_CB        *p_afh_channels_cmpl_cb; /* Callback function to be called  When */
216 /* set AFH channels is completed   */
217 #endif
218 
219 DEV_CLASS            dev_class;         /* Local device class                   */
220 
221 #if BLE_INCLUDED == TRUE
222 
223 TIMER_LIST_ENT       ble_channels_timer;
224 tBTM_CMPL_CB        *p_ble_channels_cmpl_cb; /* Callback function to be called  When
225                                                 ble set host channels is completed   */
226 
227 tBTM_CMPL_CB        *p_le_test_cmd_cmpl_cb;   /* Callback function to be called when
228                                                   LE test mode command has been sent successfully */
229 
230 BD_ADDR                 read_tx_pwr_addr;   /* read TX power target address     */
231 
232 #define BTM_LE_SUPPORT_STATE_SIZE   8
233 UINT8                   le_supported_states[BTM_LE_SUPPORT_STATE_SIZE];
234 
235 tBTM_BLE_LOCAL_ID_KEYS id_keys; /* local BLE ID keys */
236 BT_OCTET16 ble_encryption_key_value; /* BLE encryption key */
237 
238 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
239 BOOLEAN                 no_disc_if_pair_fail;
240 BOOLEAN                 enable_test_mac_val;
241 BT_OCTET8               test_mac;
242 BOOLEAN                 enable_test_local_sign_cntr;
243 UINT32                  test_local_sign_cntr;
244 #endif
245 
246 #endif  /* BLE_INCLUDED */
247 
248 tBTM_IO_CAP          loc_io_caps;       /* IO capability of the local device */
249 tBTM_AUTH_REQ        loc_auth_req;      /* the auth_req flag  */
250 BOOLEAN              secure_connections_only;    /* Rejects service level 0 connections if */
251 /* itself or peer device doesn't support */
252 /* secure connections */
253 } tBTM_DEVCB;
254 
255 
256 /* Define the structures and constants used for inquiry
257 */
258 
259 /* Definitions of limits for inquiries */
260 #define BTM_PER_INQ_MIN_MAX_PERIOD      HCI_PER_INQ_MIN_MAX_PERIOD
261 #define BTM_PER_INQ_MAX_MAX_PERIOD      HCI_PER_INQ_MAX_MAX_PERIOD
262 #define BTM_PER_INQ_MIN_MIN_PERIOD      HCI_PER_INQ_MIN_MIN_PERIOD
263 #define BTM_PER_INQ_MAX_MIN_PERIOD      HCI_PER_INQ_MAX_MIN_PERIOD
264 #define BTM_MAX_INQUIRY_LENGTH          HCI_MAX_INQUIRY_LENGTH
265 #define BTM_MIN_INQUIRY_LEN             0x01
266 
267 #define BTM_MIN_INQ_TX_POWER    -70
268 #define BTM_MAX_INQ_TX_POWER    20
269 
270 typedef struct {
271 UINT32          inq_count;          /* Used for determining if a response has already been      */
272 /* received for the current inquiry operation. (We do not   */
273 /* want to flood the caller with multiple responses from    */
274 /* the same device.                                         */
275 BD_ADDR         bd_addr;
276 } tINQ_BDADDR;
277 
278 typedef struct {
279 UINT32          time_of_resp;
280 UINT32          inq_count;          /* "timestamps" the entry with a particular inquiry count   */
281 /* Used for determining if a response has already been      */
282 /* received for the current inquiry operation. (We do not   */
283 /* want to flood the caller with multiple responses from    */
284 /* the same device.                                         */
285 tBTM_INQ_INFO   inq_info;
286 BOOLEAN         in_use;
287 
288 #if (BLE_INCLUDED == TRUE)
289 BOOLEAN         scan_rsp;
290 #endif
291 } tINQ_DB_ENT;
292 
293 
294 enum {
295 INQ_NONE,
296 INQ_LE_OBSERVE,
297 INQ_GENERAL
298 };
299 typedef UINT8 tBTM_INQ_TYPE;
300 
301 typedef struct {
302     tBTM_CMPL_CB *p_remname_cmpl_cb;
303 
304 #define BTM_EXT_RMT_NAME_TIMEOUT    40
305 
306 
307     TIMER_LIST_ENT  rmt_name_timer_ent;
308 
309     UINT16           discoverable_mode;
310     UINT16           connectable_mode;
311     UINT16           page_scan_window;
312     UINT16           page_scan_period;
313     UINT16           inq_scan_window;
314     UINT16           inq_scan_period;
315     UINT16           inq_scan_type;
316     UINT16           page_scan_type;        /* current page scan type */
317     tBTM_INQ_TYPE    scan_type;
318 
319     BD_ADDR          remname_bda;           /* Name of bd addr for active remote name request */
320 #define BTM_RMT_NAME_INACTIVE       0
321 #define BTM_RMT_NAME_EXT            0x1     /* Initiated through API */
322 #define BTM_RMT_NAME_SEC            0x2     /* Initiated internally by security manager */
323 #define BTM_RMT_NAME_INQ            0x4     /* Remote name initiated internally by inquiry */
324     BOOLEAN          remname_active;        /* State of a remote name request by external API */
325 
326     tBTM_CMPL_CB    *p_inq_cmpl_cb;
327     tBTM_INQ_RESULTS_CB *p_inq_results_cb;
328     tBTM_CMPL_CB    *p_inq_ble_cmpl_cb;     /*completion callback exclusively for LE Observe*/
329     tBTM_INQ_RESULTS_CB *p_inq_ble_results_cb;/*results callback exclusively for LE observe*/
330     tBTM_CMPL_CB    *p_inqfilter_cmpl_cb;   /* Called (if not NULL) after inquiry filter completed */
331     UINT32           inq_counter;           /* Counter incremented each time an inquiry completes */
332     /* Used for determining whether or not duplicate devices */
333     /* have responded to the same inquiry */
334     TIMER_LIST_ENT   inq_timer_ent;
335     tINQ_BDADDR     *p_bd_db;               /* Pointer to memory that holds bdaddrs */
336     UINT16           num_bd_entries;        /* Number of entries in database */
337     UINT16           max_bd_entries;        /* Maximum number of entries that can be stored */
338     tINQ_DB_ENT      inq_db[BTM_INQ_DB_SIZE];
339     tBTM_INQ_PARMS   inqparms;              /* Contains the parameters for the current inquiry */
340     tBTM_INQUIRY_CMPL inq_cmpl_info;        /* Status and number of responses from the last inquiry */
341 
342     UINT16           per_min_delay;         /* Current periodic minimum delay */
343     UINT16           per_max_delay;         /* Current periodic maximum delay */
344     BOOLEAN          inqfilt_active;
345     UINT8            pending_filt_complete_event; /* to take care of btm_event_filter_complete corresponding to */
346     /* inquiry that has been cancelled*/
347     UINT8            inqfilt_type;          /* Contains the inquiry filter type (BD ADDR, COD, or Clear) */
348 
349 #define BTM_INQ_INACTIVE_STATE      0
350 #define BTM_INQ_CLR_FILT_STATE      1   /* Currently clearing the inquiry filter preceeding the inquiry request */
351     /* (bypassed if filtering is not used)                                  */
352 #define BTM_INQ_SET_FILT_STATE      2   /* Sets the new filter (or turns off filtering) in this state */
353 #define BTM_INQ_ACTIVE_STATE        3   /* Actual inquiry or periodic inquiry is in progress */
354 #define BTM_INQ_REMNAME_STATE       4   /* Remote name requests are active  */
355 
356     UINT8            state;             /* Current state that the inquiry process is in */
357     UINT8            inq_active;        /* Bit Mask indicating type of inquiry is active */
358     BOOLEAN          no_inc_ssp;        /* TRUE, to stop inquiry on incoming SSP */
359 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
360     btm_inq_state    next_state;        /*interleaving state to determine next mode to be inquired*/
361 #endif
362 } tBTM_INQUIRY_VAR_ST;
363 
364 /* The MSB of the clock offset field indicates that the offset is valid if TRUE */
365 #define BTM_CLOCK_OFFSET_VALID      0x8000
366 
367 /* Define the structures needed by security management
368 */
369 
370 #define BTM_SEC_INVALID_HANDLE  0xFFFF
371 
372 typedef UINT8 *BTM_BD_NAME_PTR;                        /* Pointer to Device name */
373 
374 /* Security callback is called by this unit when security
375 **   procedures are completed.  Parameters are
376 **              BD Address of remote
377 **              Result of the operation
378 */
379 typedef tBTM_SEC_CBACK tBTM_SEC_CALLBACK;
380 
381 #define BTM_DATA_HANDLE_MASK 0x0FFF
382 
383 #define BTMD_GET_HANDLE(u16) (UINT16)((u16) & BTM_DATA_HANDLE_MASK)
384 
385 typedef void (tBTM_SCO_IND_CBACK) (UINT16 sco_inx) ;
386 
387 /* MACROs to convert from SCO packet types mask to ESCO and back */
388 #define BTM_SCO_PKT_TYPE_MASK   (   HCI_PKT_TYPES_MASK_HV1      \
389                                  |  HCI_PKT_TYPES_MASK_HV2      \
390                                  |  HCI_PKT_TYPES_MASK_HV3)
391 
392 /* Mask defining only the SCO types of an esco packet type */
393 #define BTM_ESCO_PKT_TYPE_MASK  (   HCI_ESCO_PKT_TYPES_MASK_HV1 \
394                                  |  HCI_ESCO_PKT_TYPES_MASK_HV2 \
395                                  |  HCI_ESCO_PKT_TYPES_MASK_HV3)
396 
397 #define BTM_SCO_2_ESCO(scotype)  ((UINT16)(((scotype) & BTM_SCO_PKT_TYPE_MASK) >> 5))
398 #define BTM_ESCO_2_SCO(escotype) ((UINT16)(((escotype) & BTM_ESCO_PKT_TYPE_MASK) << 5))
399 
400 /* Define masks for supported and exception 2.0 SCO packet types
401 */
402 #define BTM_SCO_SUPPORTED_PKTS_MASK      (HCI_ESCO_PKT_TYPES_MASK_HV1       | \
403                                           HCI_ESCO_PKT_TYPES_MASK_HV2       | \
404                                           HCI_ESCO_PKT_TYPES_MASK_HV3       | \
405                                           HCI_ESCO_PKT_TYPES_MASK_EV3       | \
406                                           HCI_ESCO_PKT_TYPES_MASK_EV4       | \
407                                           HCI_ESCO_PKT_TYPES_MASK_EV5)
408 
409 #define BTM_SCO_EXCEPTION_PKTS_MASK      (HCI_ESCO_PKT_TYPES_MASK_NO_2_EV3  | \
410                                           HCI_ESCO_PKT_TYPES_MASK_NO_3_EV3  | \
411                                           HCI_ESCO_PKT_TYPES_MASK_NO_2_EV5  | \
412                                           HCI_ESCO_PKT_TYPES_MASK_NO_3_EV5)
413 
414 
415 #define BTM_SCO_ROUTE_UNKNOWN       0xff
416 
417 /* Define the structure that contains (e)SCO data */
418 typedef struct {
419     tBTM_ESCO_CBACK    *p_esco_cback;   /* Callback for eSCO events     */
420     tBTM_ESCO_PARAMS    setup;
421     tBTM_ESCO_DATA      data;           /* Connection complete information */
422     UINT8               hci_status;
423 } tBTM_ESCO_INFO;
424 
425 /* Define the structure used for SCO Management
426 */
427 typedef struct {
428     tBTM_ESCO_INFO   esco;              /* Current settings             */
429 #if BTM_SCO_HCI_INCLUDED == TRUE
430 #define BTM_SCO_XMIT_QUEUE_THRS     30
431 #define BTM_SCO_XMIT_QUEUE_HIGH_WM  20
432     fixed_queue_t   *xmit_data_q;       /* SCO data transmitting queue  */
433     INT16           sent_not_acked;
434 #endif
435     tBTM_SCO_CB     *p_conn_cb;         /* Callback for when connected  */
436     tBTM_SCO_CB     *p_disc_cb;         /* Callback for when disconnect */
437     UINT16           state;             /* The state of the SCO link    */
438     UINT16           hci_handle;        /* HCI Handle                   */
439     BOOLEAN          is_orig;           /* TRUE if the originator       */
440     BOOLEAN          rem_bd_known;      /* TRUE if remote BD addr known */
441 } tSCO_CONN;
442 
443 /* SCO Management control block */
444 typedef struct {
445     tBTM_SCO_IND_CBACK  *app_sco_ind_cb;
446 #if BTM_SCO_HCI_INCLUDED == TRUE
447     tBTM_SCO_DATA_CB     *p_data_cb;        /* Callback for SCO data over HCI */
448     UINT32               xmit_window_size; /* Total SCO window in bytes  */
449     UINT16               num_lm_sco_bufs;
450 #endif
451     tSCO_CONN            sco_db[BTM_MAX_SCO_LINKS];
452     tBTM_ESCO_PARAMS     def_esco_parms;
453     BD_ADDR              xfer_addr;
454     UINT16               sco_disc_reason;
455     BOOLEAN              esco_supported;    /* TRUE if 1.2 cntlr AND supports eSCO links */
456     tBTM_SCO_TYPE        desired_sco_mode;
457     tBTM_SCO_TYPE        xfer_sco_type;
458     tBTM_SCO_PCM_PARAM   sco_pcm_param;
459     tBTM_SCO_CODEC_TYPE  codec_in_use;      /* None, CVSD, MSBC, etc. */
460 #if BTM_SCO_HCI_INCLUDED == TRUE
461     tBTM_SCO_ROUTE_TYPE  sco_path;
462 #endif
463 
464 } tSCO_CB;
465 
466 
467 #if BTM_SCO_INCLUDED == TRUE
468 void     btm_set_sco_ind_cback( tBTM_SCO_IND_CBACK *sco_ind_cb );
469 void     btm_accept_sco_link(UINT16 sco_inx, tBTM_ESCO_PARAMS *p_setup,
470                              tBTM_SCO_CB *p_conn_cb, tBTM_SCO_CB *p_disc_cb);
471 void     btm_reject_sco_link(UINT16 sco_inx );
472 void btm_sco_chk_pend_rolechange (UINT16 hci_handle);
473 #else
474 #define btm_accept_sco_link(sco_inx, p_setup, p_conn_cb, p_disc_cb)
475 #define btm_reject_sco_link(sco_inx)
476 #define btm_set_sco_ind_cback(sco_ind_cb)
477 #define btm_sco_chk_pend_rolechange(hci_handle)
478 #endif  /* BTM_SCO_INCLUDED */
479 
480 /*
481 ** Define structure for Security Service Record.
482 ** A record exists for each service registered with the Security Manager
483 */
484 #define BTM_SEC_OUT_FLAGS   (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHORIZE)
485 #define BTM_SEC_IN_FLAGS    (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHORIZE)
486 
487 #define BTM_SEC_OUT_LEVEL4_FLAGS   (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT | \
488                                         BTM_SEC_OUT_MITM | BTM_SEC_MODE4_LEVEL4)
489 
490 #define BTM_SEC_IN_LEVEL4_FLAGS    (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | \
491                                         BTM_SEC_IN_MITM | BTM_SEC_MODE4_LEVEL4)
492 
493 typedef struct {
494     UINT32          mx_proto_id;        /* Service runs over this multiplexer protocol */
495     UINT32          orig_mx_chan_id;    /* Channel on the multiplexer protocol    */
496     UINT32          term_mx_chan_id;    /* Channel on the multiplexer protocol    */
497     UINT16          psm;                /* L2CAP PSM value */
498     UINT16          security_flags;     /* Bitmap of required security features */
499     UINT8           service_id;         /* Passed in authorization callback */
500 #if (L2CAP_UCD_INCLUDED == TRUE)
501     UINT16          ucd_security_flags; /* Bitmap of required security features for UCD */
502 #endif
503 #if BTM_SEC_SERVICE_NAME_LEN > 0
504     UINT8           orig_service_name[BTM_SEC_SERVICE_NAME_LEN + 1];
505     UINT8           term_service_name[BTM_SEC_SERVICE_NAME_LEN + 1];
506 #endif
507 } tBTM_SEC_SERV_REC;
508 
509 #if BLE_INCLUDED == TRUE
510 /* LE Security information of device in Slave Role */
511 typedef struct {
512     BT_OCTET16          irk;            /* peer diverified identity root */
513     BT_OCTET16          pltk;           /* peer long term key */
514     BT_OCTET16          pcsrk;          /* peer SRK peer device used to secured sign local data  */
515 
516     BT_OCTET16          lltk;           /* local long term key */
517     BT_OCTET16          lcsrk;          /* local SRK peer device used to secured sign local data  */
518 
519     BT_OCTET8           rand;           /* random vector for LTK generation */
520     UINT16              ediv;           /* LTK diversifier of this slave device */
521     UINT16              div;            /* local DIV  to generate local LTK=d1(ER,DIV,0) and CSRK=d1(ER,DIV,1)  */
522     UINT8               sec_level;      /* local pairing security level */
523     UINT8               key_size;       /* key size of the LTK delivered to peer device */
524     UINT8               srk_sec_level;  /* security property of peer SRK for this device */
525     UINT8               local_csrk_sec_level;  /* security property of local CSRK for this device */
526 
527     UINT32              counter;        /* peer sign counter for verifying rcv signed cmd */
528     UINT32              local_counter;  /* local sign counter for sending signed write cmd*/
529 } tBTM_SEC_BLE_KEYS;
530 
531 typedef struct {
532     BD_ADDR pseudo_addr; /* LE pseudo address of the device if different from device address  */
533     tBLE_ADDR_TYPE      ble_addr_type;  /* LE device type: public or random address */
534     tBLE_ADDR_TYPE      static_addr_type;   /* static address type */
535     BD_ADDR             static_addr;    /* static address */
536 
537 #define BTM_WHITE_LIST_BIT          0x01
538 #define BTM_RESOLVING_LIST_BIT      0x02
539     UINT8               in_controller_list;   /* in controller resolving list or not */
540     UINT8               resolving_list_index;
541 #if BLE_PRIVACY_SPT == TRUE
542     BD_ADDR             cur_rand_addr;  /* current random address */
543 
544 #define BTM_BLE_ADDR_PSEUDO         0   /* address index device record */
545 #define BTM_BLE_ADDR_RRA            1   /* cur_rand_addr */
546 #define BTM_BLE_ADDR_STATIC         2   /* static_addr  */
547     UINT8               active_addr_type;
548 #endif
549 
550 #if SMP_INCLUDED == TRUE
551     tBTM_LE_KEY_TYPE    key_type;       /* bit mask of valid key types in record */
552     tBTM_SEC_BLE_KEYS   keys;           /* LE device security info in slave rode */
553 #if (SMP_SLAVE_CON_PARAMS_UPD_ENABLE == TRUE)
554     bool skip_update_conn_param;        /* skip update connection paraams or not*/
555 #endif
556     UINT16 auth_mode;                   /* Authentication mode */
557 #endif
558 #if (BLE_PRIVACY_SPT == TRUE && (!CONTROLLER_RPA_LIST_ENABLE))
559     tBLE_ADDR_TYPE      current_addr_type; /* current adv addr type*/
560     BD_ADDR             current_addr;      /* current adv addr*/
561     bool                current_addr_valid; /* current addr info is valid or not*/
562 #endif
563 } tBTM_SEC_BLE;
564 
565 
566 #endif  /* BLE_INCLUDED */
567 
568 /* Peering bond type */
569 enum {
570     BOND_TYPE_UNKNOWN,
571     BOND_TYPE_PERSISTENT,
572     BOND_TYPE_TEMPORARY
573 };
574 typedef UINT8 tBTM_BOND_TYPE;
575 
576 /*
577 ** Define structure for Security Device Record.
578 ** A record exists for each device authenticated with this device
579 */
580 struct tBTM_SEC_DEV_REC{
581     tBTM_SEC_SERV_REC   *p_cur_service;
582     tBTM_SEC_CALLBACK   *p_callback;
583     void                *p_ref_data;
584     UINT32               timestamp;         /* Timestamp of the last connection   */
585     UINT32               trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE];  /* Bitwise OR of trusted services     */
586     UINT16               hci_handle;        /* Handle to connection when exists   */
587     UINT16               clock_offset;      /* Latest known clock offset          */
588     BD_ADDR              bd_addr;           /* BD_ADDR of the device              */
589     DEV_CLASS            dev_class;         /* DEV_CLASS of the device            */
590     LINK_KEY             link_key;          /* Device link key                    */
591     UINT8                pin_code_length;   /* Length of the pin_code used for paring */
592 
593 #define BTM_SEC_AUTHORIZED      BTM_SEC_FLAG_AUTHORIZED     /* 0x01 */
594 #define BTM_SEC_AUTHENTICATED   BTM_SEC_FLAG_AUTHENTICATED  /* 0x02 */
595 #define BTM_SEC_ENCRYPTED       BTM_SEC_FLAG_ENCRYPTED      /* 0x04 */
596 #define BTM_SEC_NAME_KNOWN      0x08
597 #define BTM_SEC_LINK_KEY_KNOWN  BTM_SEC_FLAG_LKEY_KNOWN /* 0x10 */
598 #define BTM_SEC_LINK_KEY_AUTHED BTM_SEC_FLAG_LKEY_AUTHED    /* 0x20 */
599 #define BTM_SEC_ROLE_SWITCHED   0x40
600 #define BTM_SEC_IN_USE          0x80
601     /* LE link security flag */
602 #define BTM_SEC_LE_AUTHORIZATION   0x0100   /* LE link is authorized */
603 #define BTM_SEC_LE_AUTHENTICATED   0x0200   /* LE link is encrypted after pairing with MITM */
604 #define BTM_SEC_LE_ENCRYPTED       0x0400   /* LE link is encrypted */
605 #define BTM_SEC_LE_NAME_KNOWN      0x0800   /* not used */
606 #define BTM_SEC_LE_LINK_KEY_KNOWN  0x1000   /* bonded with peer (peer LTK and/or SRK is saved) */
607 #define BTM_SEC_LE_LINK_KEY_AUTHED 0x2000   /* pairing is done with MITM */
608 #define BTM_SEC_16_DIGIT_PIN_AUTHED 0x4000   /* pairing is done with 16 digit pin */
609 
610     UINT16           sec_flags;          /* Current device security state      */
611 
612     tBTM_BD_NAME    sec_bd_name;        /* User friendly name of the device. (may be truncated to save space in dev_rec table) */
613     BD_FEATURES     features[HCI_EXT_FEATURES_PAGE_MAX + 1];           /* Features supported by the device */
614     UINT8           num_read_pages;
615 
616 #define BTM_SEC_STATE_IDLE               0
617 #define BTM_SEC_STATE_AUTHENTICATING     1
618 #define BTM_SEC_STATE_ENCRYPTING         2
619 #define BTM_SEC_STATE_GETTING_NAME       3
620 #define BTM_SEC_STATE_AUTHORIZING        4
621 #define BTM_SEC_STATE_SWITCHING_ROLE     5
622 #define BTM_SEC_STATE_DISCONNECTING      6 /* disconnecting BR/EDR */
623 #define BTM_SEC_STATE_DELAY_FOR_ENC      7 /* delay to check for encryption to work around */
624     /* controller problems */
625 #define BTM_SEC_STATE_DISCONNECTING_BLE  8 /* disconnecting BLE */
626 #define BTM_SEC_STATE_DISCONNECTING_BOTH 9 /* disconnecting BR/EDR and BLE */
627 
628     UINT8       sec_state;              /* Operating state                    */
629     BOOLEAN     is_originator;          /* TRUE if device is originating connection */
630 #if (L2CAP_UCD_INCLUDED == TRUE)
631     BOOLEAN     is_ucd;                 /* TRUE if device is sending or receiving UCD */
632     /* if incoming security failed, received UCD will be discarded */
633 #endif
634     BOOLEAN     role_master;            /* TRUE if current mode is master     */
635     UINT16      security_required;      /* Security required for connection   */
636     BOOLEAN     link_key_not_sent;      /* link key notification has not been sent waiting for name */
637     UINT8       link_key_type;          /* Type of key used in pairing   */
638     BOOLEAN     link_key_changed;       /* Changed link key during current connection */
639 
640 #define BTM_MAX_PRE_SM4_LKEY_TYPE   BTM_LKEY_TYPE_REMOTE_UNIT /* the link key type used by legacy pairing */
641 
642 #define BTM_SM4_UNKNOWN     0x00
643 #define BTM_SM4_KNOWN       0x10
644 #define BTM_SM4_TRUE        0x11
645 #define BTM_SM4_REQ_PEND    0x08        /* set this bit when getting remote features */
646 #define BTM_SM4_UPGRADE     0x04        /* set this bit when upgrading link key */
647 #define BTM_SM4_RETRY       0x02        /* set this bit to retry on HCI_ERR_KEY_MISSING or HCI_ERR_LMP_ERR_TRANS_COLLISION */
648 #define BTM_SM4_DD_ACP      0x20        /* set this bit to indicate peer initiated dedicated bonding */
649 #define BTM_SM4_CONN_PEND   0x40        /* set this bit to indicate accepting acl conn; to be cleared on btm_acl_created */
650     UINT8       sm4;                    /* BTM_SM4_TRUE, if the peer supports SM4 */
651     tBTM_IO_CAP rmt_io_caps;            /* IO capability of the peer device */
652     tBTM_AUTH_REQ rmt_auth_req;         /* the auth_req flag as in the IO caps rsp evt */
653     BOOLEAN     remote_supports_secure_connections;
654     BOOLEAN     remote_features_needed; /* set to true if the local device is in */
655     /* "Secure Connections Only" mode and it receives */
656     /* HCI_IO_CAPABILITY_REQUEST_EVT from the peer before */
657     /* it knows peer's support for Secure Connections */
658     BOOLEAN     remote_secure_connection_previous_state;     /* Stores if peer ever supported
659     secure connection. This will be helpful to know when peer device downgrades it's security. */
660 
661     UINT16              ble_hci_handle;         /* use in DUMO connection */
662     UINT8               enc_key_size;           /* current link encryption key size */
663     tBT_DEVICE_TYPE     device_type;
664     BOOLEAN             new_encryption_key_is_p256; /* Set to TRUE when the newly generated LK
665                                                     ** is generated from P-256.
666                                                     ** Link encrypted with such LK can be used
667                                                     ** for SM over BR/EDR.
668                                                     */
669     BOOLEAN no_smp_on_br;       /* if set to TRUE then SMP on BR/EDR doesn't */
670     /* work, i.e. link keys crosspairing */
671     /* SC BR/EDR->SC LE doesn't happen */
672     tBTM_BOND_TYPE bond_type;   /* peering bond type */
673 
674 #if BLE_INCLUDED == TRUE
675     tBTM_SEC_BLE        ble;
676     tBTM_LE_CONN_PRAMS  conn_params;
677 #if (BLE_50_FEATURE_SUPPORT == TRUE)
678     tBTM_EXT_CONN_PARAMS ext_conn_params;
679 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
680 #endif
681 
682 // btla-specific ++
683 #if BTM_DISC_DURING_RS == TRUE
684 #define BTM_SEC_RS_NOT_PENDING          0           /* Role Switch not in progress */
685 #define BTM_SEC_RS_PENDING              1           /* Role Switch in progress */
686 #define BTM_SEC_DISC_PENDING            2           /* Disconnect is pending */
687     UINT8           rs_disc_pending;
688 #endif
689 // btla-specific --
690 #define BTM_SEC_NO_LAST_SERVICE_ID      0
691     UINT8           last_author_service_id;         /* ID of last serviced authorized: Reset after each l2cap connection */
692     BOOLEAN         enc_init_by_we;
693 };
694 
695 #define BTM_SEC_IS_SM4(sm) ((BOOLEAN)(BTM_SM4_TRUE == ((sm)&BTM_SM4_TRUE)))
696 #define BTM_SEC_IS_SM4_LEGACY(sm) ((BOOLEAN)(BTM_SM4_KNOWN == ((sm)&BTM_SM4_TRUE)))
697 #define BTM_SEC_IS_SM4_UNKNOWN(sm) ((BOOLEAN)(BTM_SM4_UNKNOWN == ((sm)&BTM_SM4_TRUE)))
698 
699 #define BTM_SEC_LE_MASK    (BTM_SEC_LE_AUTHENTICATED|BTM_SEC_LE_ENCRYPTED|BTM_SEC_LE_LINK_KEY_KNOWN|BTM_SEC_LE_LINK_KEY_AUTHED)
700 
701 /*
702 ** Define device configuration structure
703 */
704 typedef struct {
705 #if BTM_MAX_LOC_BD_NAME_LEN > 0
706     tBTM_LOC_BD_NAME bd_name;                    /* local Bluetooth device name */
707 #endif
708     BOOLEAN          pin_type;                   /* TRUE if PIN type is fixed */
709     UINT8            pin_code_len;               /* Bonding information */
710     PIN_CODE         pin_code;                   /* PIN CODE if pin type is fixed */
711     BOOLEAN          connectable;                /* If TRUE page scan should be enabled */
712     UINT8            def_inq_scan_mode;          /* ??? limited/general/none */
713 } tBTM_CFG;
714 
715 enum {
716     BTM_PM_SET_MODE_EVT,    /* Set power mode API is called. */
717     BTM_PM_UPDATE_EVT,
718     BTM_PM_RD_MODE_EVT     /* Read power mode API is called. */
719 };
720 typedef UINT8 tBTM_PM_EVENT;
721 
722 typedef struct {
723     UINT16          event;
724     UINT16          len;
725     UINT8           link_ind;
726 } tBTM_PM_MSG_DATA;
727 
728 typedef struct {
729     UINT8 hci_status;
730     UINT8 mode;
731     UINT16 interval;
732 } tBTM_PM_MD_CHG_DATA;
733 
734 typedef struct {
735     UINT8          pm_id;      /* the entity that calls SetPowerMode API */
736     tBTM_PM_PWR_MD *p_pmd;
737 } tBTM_PM_SET_MD_DATA;
738 
739 typedef struct {
740     void        *p_data;
741     UINT8        link_ind;
742 } tBTM_PM_SM_DATA;
743 
744 #define BTM_PM_REC_NOT_USED 0
745 typedef struct {
746     tBTM_PM_STATUS_CBACK *cback;/* to notify the registered party of mode change event */
747     UINT8                 mask; /* registered request mask. 0, if this entry is not used */
748 } tBTM_PM_RCB;
749 
750 enum {
751     BTM_BLI_ACL_UP_EVT,
752     BTM_BLI_ACL_DOWN_EVT,
753     BTM_BLI_PAGE_EVT,
754     BTM_BLI_PAGE_DONE_EVT,
755     BTM_BLI_INQ_EVT,
756     BTM_BLI_INQ_CANCEL_EVT,
757     BTM_BLI_INQ_DONE_EVT
758 };
759 typedef UINT8 tBTM_BLI_EVENT;
760 
761 /* Pairing State */
762 enum {
763     BTM_PAIR_STATE_IDLE,                        /* Idle                                         */
764     BTM_PAIR_STATE_GET_REM_NAME,                /* Getting the remote name (to check for SM4)   */
765     BTM_PAIR_STATE_WAIT_PIN_REQ,                /* Started authentication, waiting for PIN req (PIN is pre-fetched) */
766     BTM_PAIR_STATE_WAIT_LOCAL_PIN,              /* Waiting for local PIN code                   */
767     BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM,        /* Waiting user 'yes' to numeric confirmation   */
768     BTM_PAIR_STATE_KEY_ENTRY,                   /* Key entry state (we are a keyboard)          */
769     BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP,          /* Waiting for local response to peer OOB data  */
770     BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS,           /* Waiting for local IO capabilities and OOB data */
771     BTM_PAIR_STATE_INCOMING_SSP,                /* Incoming SSP (got peer IO caps when idle)    */
772     BTM_PAIR_STATE_WAIT_AUTH_COMPLETE,          /* All done, waiting authentication complete    */
773     BTM_PAIR_STATE_WAIT_DISCONNECT              /* Waiting to disconnect the ACL                */
774 };
775 typedef UINT8 tBTM_PAIRING_STATE;
776 
777 #define BTM_PAIR_FLAGS_WE_STARTED_DD    0x01    /* We want to do dedicated bonding              */
778 #define BTM_PAIR_FLAGS_PEER_STARTED_DD  0x02    /* Peer initiated dedicated bonding             */
779 #define BTM_PAIR_FLAGS_DISC_WHEN_DONE   0x04    /* Disconnect when done     */
780 #define BTM_PAIR_FLAGS_PIN_REQD         0x08    /* set this bit when pin_callback is called     */
781 #define BTM_PAIR_FLAGS_PRE_FETCH_PIN    0x10    /* set this bit when pre-fetch pin     */
782 #define BTM_PAIR_FLAGS_REJECTED_CONNECT 0x20    /* set this bit when rejected incoming connection  */
783 #define BTM_PAIR_FLAGS_WE_CANCEL_DD     0x40    /* set this bit when cancelling a bonding procedure */
784 #define BTM_PAIR_FLAGS_LE_ACTIVE        0x80    /* use this bit when SMP pairing is active */
785 
786 
787 typedef struct {
788     BOOLEAN             is_mux;
789     BD_ADDR             bd_addr;
790     UINT16              psm;
791     BOOLEAN             is_orig;
792     tBTM_SEC_CALLBACK   *p_callback;
793     void                *p_ref_data;
794     UINT32              mx_proto_id;
795     UINT32              mx_chan_id;
796     tBT_TRANSPORT       transport;
797 } tBTM_SEC_QUEUE_ENTRY;
798 
799 #if (L2CAP_UCD_INCLUDED == TRUE)
800 
801 #define CONN_ORIENT_TERM                0x00    /* incoming connection oriented */
802 #define CONN_ORIENT_ORIG                0x01    /* outgoing connection oriented */
803 #define CONNLESS_TERM                   0x02    /* incoming connectionless      */
804 #define CONNLESS_ORIG                   0x03    /* outgoing connectionless      */
805 #define CONNECTION_TYPE_ORIG_MASK       0x01    /* mask for direction           */
806 #define CONNECTION_TYPE_CONNLESS_MASK   0x02    /* mask for connectionless or not */
807 typedef UINT8 CONNECTION_TYPE;
808 
809 #else
810 
811 #define CONN_ORIENT_TERM                FALSE
812 #define CONN_ORIENT_ORIG                TRUE
813 typedef BOOLEAN CONNECTION_TYPE;
814 
815 #endif /* (L2CAP_UCD_INCLUDED == TRUE) */
816 
817 /* Define a structure to hold all the BTM data
818 */
819 
820 #define BTM_STATE_BUFFER_SIZE  5                  /* size of state buffer */
821 
822 #define BTM_INVALID_HANDLE    0xFFFF
823 
824 typedef struct {
825     tBTM_CFG    cfg;                        /* Device configuration */
826 
827     /****************************************************
828     **      ACL Management
829     ****************************************************/
830     list_t      *p_acl_db_list;
831 #if (CLASSIC_BT_INCLUDED == TRUE)
832     UINT8       btm_scn[BTM_MAX_SCN];        /* current SCNs: TRUE if SCN is in use */
833 #endif  ///CLASSIC_BT_INCLUDED == TRUE
834     UINT16      btm_def_link_policy;
835     UINT16      btm_def_link_super_tout;
836 
837     tBTM_BL_EVENT_MASK     bl_evt_mask;
838     tBTM_BL_CHANGE_CB     *p_bl_changed_cb;    /* Callback for when Busy Level changed */
839 
840     /****************************************************
841     **      Power Management
842     ****************************************************/
843     list_t      *p_pm_mode_db_list;
844     tBTM_PM_RCB pm_reg_db[BTM_MAX_PM_RECORDS + 1]; /* per application/module */
845     UINT16      pm_pend_link_hdl;  /* the index of acl_db, which has a pending PM cmd */
846     UINT8       pm_pend_id;        /* the id pf the module, which has a pending PM cmd */
847 
848     /*****************************************************
849     **      Device control
850     *****************************************************/
851     tBTM_DEVCB  devcb;
852 
853     /*****************************************************
854     **      BLE Device controllers
855     *****************************************************/
856 #if (BLE_INCLUDED == TRUE)
857     tBTM_BLE_CB             ble_ctr_cb;
858 
859     UINT16                  enc_handle;
860     BT_OCTET8               enc_rand;   /* received rand value from LTK request*/
861     UINT16                  ediv;       /* received ediv value from LTK request */
862     UINT8                   key_size;
863     tBTM_BLE_VSC_CB         cmn_ble_vsc_cb;
864 #endif
865 
866     /* Packet types supported by the local device */
867     UINT16      btm_acl_pkt_types_supported;
868     UINT16      btm_sco_pkt_types_supported;
869 
870 
871     /*****************************************************
872     **      Inquiry
873     *****************************************************/
874     tBTM_INQUIRY_VAR_ST     btm_inq_vars;
875 
876     /*****************************************************
877     **      SCO Management
878     *****************************************************/
879 #if BTM_SCO_INCLUDED == TRUE
880     tSCO_CB             sco_cb;
881 #endif
882 
883     /*****************************************************
884     **      Security Management
885     *****************************************************/
886     tBTM_APPL_INFO          api;
887 
888 #define BTM_SEC_MAX_RMT_NAME_CALLBACKS  2
889 
890     tBTM_RMT_NAME_CALLBACK  *p_rmt_name_callback[BTM_SEC_MAX_RMT_NAME_CALLBACKS];
891 #if (SMP_INCLUDED == TRUE)
892     tBTM_SEC_DEV_REC        *p_collided_dev_rec;
893 #endif  ///SMP_INCLUDED == TRUE
894     TIMER_LIST_ENT           sec_collision_tle;
895     UINT32                   collision_start_time;
896     UINT32                   max_collision_delay;
897     UINT32                   dev_rec_count;      /* Counter used for device record timestamp */
898     UINT8                    security_mode;
899     BOOLEAN                  pairing_disabled;
900     BOOLEAN                  connect_only_paired;
901     BOOLEAN                  security_mode_changed;  /* mode changed during bonding */
902     BOOLEAN                  sec_req_pending;       /*   TRUE if a request is pending */
903 #if (CLASSIC_BT_INCLUDED == TRUE)
904     BOOLEAN                  pin_type_changed;       /* pin type changed during bonding */
905 #endif  ///CLASSIC_BT_INCLUDED == TRUE
906 #if (SMP_INCLUDED == TRUE)
907 #if (CLASSIC_BT_INCLUDED == TRUE)
908 // btla-specific ++
909 #ifdef PORCHE_PAIRING_CONFLICT
910     UINT8                    pin_code_len_saved;     /* for legacy devices */
911 #endif
912 // btla-specific --
913 
914     UINT8                    pin_code_len;  /* for legacy devices */
915     PIN_CODE                 pin_code;      /* for legacy devices */
916     UINT8                    disc_reason;   /* for legacy devices */
917     UINT16                   disc_handle;   /* for legacy devices */
918 #endif  ///CLASSIC_BT_INCLUDED == TRUE
919     tBTM_PAIRING_STATE       pairing_state; /* The current pairing state    */
920     UINT8                    pairing_flags; /* The current pairing flags    */
921     BD_ADDR                  pairing_bda;   /* The device currently pairing */
922     TIMER_LIST_ENT           pairing_tle;   /* Timer for pairing process    */
923 
924 #endif  ///SMP_INCLUDED == TRUE
925 #if SMP_INCLUDED == TRUE || CLASSIC_BT_INCLUDED == TRUE
926     tBTM_SEC_SERV_REC        sec_serv_rec[BTM_SEC_MAX_SERVICE_RECORDS];
927 #endif // SMP_INCLUDED == TRUE || BT_CLASSIC_ENABLED == TRUE
928     list_t                  *p_sec_dev_rec_list;
929     tBTM_SEC_SERV_REC       *p_out_serv;
930     tBTM_MKEY_CALLBACK      *mkey_cback;
931 
932     BD_ADDR                  connecting_bda;
933     DEV_CLASS                connecting_dc;
934 
935     UINT8                   acl_disc_reason;
936     UINT8                   trace_level;
937     UINT8                   busy_level; /* the current busy level */
938     BOOLEAN                 is_paging;  /* TRUE, if paging is in progess */
939     BOOLEAN                 is_inquiry; /* TRUE, if inquiry is in progess */
940     fixed_queue_t           *page_queue;
941     BOOLEAN                 paging;
942     BOOLEAN                 discing;
943     fixed_queue_t           *sec_pending_q;  /* pending sequrity requests in tBTM_SEC_QUEUE_ENTRY format */
944 #if  (!defined(BT_TRACE_VERBOSE) || (BT_TRACE_VERBOSE == FALSE))
945     char state_temp_buffer[BTM_STATE_BUFFER_SIZE];
946 #endif
947 } tBTM_CB;
948 
949 typedef struct{
950   //connection parameters update callback
951   tBTM_UPDATE_CONN_PARAM_CBACK *update_conn_param_cb;
952 }tBTM_CallbackFunc;
953 
954 extern tBTM_CallbackFunc conn_param_update_cb;
955 /* security action for L2CAP COC channels */
956 #define BTM_SEC_OK                1
957 #define BTM_SEC_ENCRYPT           2    /* encrypt the link with current key */
958 #define BTM_SEC_ENCRYPT_NO_MITM   3    /* unauthenticated encryption or better */
959 #define BTM_SEC_ENCRYPT_MITM      4    /* authenticated encryption */
960 #define BTM_SEC_ENC_PENDING       5    /* wait for link encryption pending */
961 
962 typedef UINT8 tBTM_SEC_ACTION;
963 
964 #ifdef __cplusplus
965 extern "C"
966 {
967 #endif
968 
969 #if BTM_DYNAMIC_MEMORY == FALSE
970 extern tBTM_CB  btm_cb;
971 #else
972 extern tBTM_CB *btm_cb_ptr;
973 #define btm_cb (*btm_cb_ptr)
974 #endif
975 
976 typedef struct tSecDevContext {
977 #define SEC_DEV_BTDM_BDA 0x01
978 #define SEC_DEV_BDA      0x02
979 #define SEC_DEV_HDL      0x03
980 #define SEC_DEV_ID_ADDR  0x04
981     UINT8   type;
982     BOOLEAN free_check;
983     union {
984         BD_ADDR_PTR p_bd_addr;
985 	UINT16      handle;
986     }context;
987 }tSecDevContext;
988 
989 /* Internal functions provided by btm_main.c
990 ********************************************
991 */
992 void         btm_init (void);
993 void         btm_free (void);
994 
995 /* Internal functions provided by btm_inq.c
996 *******************************************
997 */
998 tBTM_STATUS  btm_initiate_rem_name (BD_ADDR remote_bda,
999                                     tBTM_INQ_INFO *p_cur,
1000                                     UINT8 origin, UINT32 timeout,
1001                                     tBTM_CMPL_CB *p_cb);
1002 
1003 void         btm_process_remote_name (BD_ADDR bda, BD_NAME name, UINT16 evt_len,
1004                                       UINT8 hci_status);
1005 void         btm_inq_rmt_name_failed(void);
1006 
1007 /* Inquiry related functions */
1008 void         btm_clr_inq_db (BD_ADDR p_bda);
1009 void         btm_inq_db_init (void);
1010 void         btm_process_inq_results (UINT8 *p, UINT8 inq_res_mode);
1011 void         btm_process_inq_complete (UINT8 status, UINT8 mode);
1012 void         btm_process_cancel_complete(UINT8 status, UINT8 mode);
1013 void         btm_event_filter_complete (UINT8 *p);
1014 void         btm_inq_stop_on_ssp(void);
1015 void         btm_inq_clear_ssp(void);
1016 tINQ_DB_ENT *btm_inq_db_find (BD_ADDR p_bda);
1017 BOOLEAN      btm_inq_find_bdaddr (BD_ADDR p_bda);
1018 
1019 BOOLEAN btm_lookup_eir(BD_ADDR_PTR p_rem_addr);
1020 
1021 /* Internal functions provided by btm_acl.c
1022 ********************************************
1023 */
1024 void         btm_acl_free(void);
1025 void         btm_acl_init (void);
1026 void         btm_acl_created (BD_ADDR bda, DEV_CLASS dc, BD_NAME bdn,
1027                               UINT16 hci_handle, UINT8 link_role, tBT_TRANSPORT transport);
1028 void         btm_acl_removed (BD_ADDR bda, tBT_TRANSPORT transport);
1029 void         btm_acl_device_down (void);
1030 void         btm_acl_update_busy_level (tBTM_BLI_EVENT event);
1031 
1032 void         btm_cont_rswitch (tACL_CONN *p,
1033                                tBTM_SEC_DEV_REC *p_dev_rec,
1034                                UINT8 hci_status);
1035 
1036 tACL_CONN    *btm_handle_to_acl (UINT16 hci_handle);
1037 void         btm_read_link_policy_complete (UINT8 *p);
1038 void         btm_read_rssi_complete (UINT8 *p);
1039 void         btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble);
1040 void         btm_read_link_quality_complete (UINT8 *p);
1041 tBTM_STATUS  btm_set_packet_types (tACL_CONN *p, UINT16 pkt_types);
1042 void         btm_process_clk_off_comp_evt (UINT16 hci_handle, UINT16 clock_offset);
1043 void         btm_acl_role_changed (UINT8 hci_status, BD_ADDR bd_addr, UINT8 new_role);
1044 void         btm_acl_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable);
1045 UINT16       btm_get_acl_disc_reason_code (void);
1046 tBTM_STATUS  btm_remove_acl (BD_ADDR bd_addr, tBT_TRANSPORT transport);
1047 void         btm_read_remote_features_complete (UINT8 *p);
1048 void         btm_read_remote_ext_features_complete (UINT8 *p);
1049 void         btm_read_remote_ext_features_failed (UINT8 status, UINT16 handle);
1050 void         btm_read_remote_version_complete (UINT8 *p);
1051 void         btm_establish_continue (tACL_CONN *p_acl_cb);
1052 
1053 // btla-specific ++
1054 void         btm_acl_chk_peer_pkt_type_support (tACL_CONN *p, UINT16 *p_pkt_type);
1055 // btla-specific --
1056 /* Read maximum data packet that can be sent over current connection */
1057 UINT16 btm_get_max_packet_size (BD_ADDR addr);
1058 tACL_CONN *btm_bda_to_acl (BD_ADDR bda, tBT_TRANSPORT transport);
1059 BOOLEAN    btm_acl_notif_conn_collision (BD_ADDR bda);
1060 
1061 void btm_pm_reset(void);
1062 tBTM_PM_MCB *btm_pm_sm_alloc(void);
1063 void btm_pm_proc_cmd_status(UINT8 status);
1064 void btm_pm_proc_mode_change (UINT8 hci_status, UINT16 hci_handle, UINT8 mode,
1065                               UINT16 interval);
1066 void btm_pm_proc_ssr_evt (UINT8 *p, UINT16 evt_len);
1067 #if BTM_SCO_INCLUDED == TRUE
1068 void btm_sco_chk_pend_unpark (UINT8 hci_status, UINT16 hci_handle);
1069 #if (BTM_SCO_HCI_INCLUDED == TRUE )
1070 void btm_sco_process_num_bufs (UINT16 num_lm_sco_bufs);
1071 void btm_sco_process_num_completed_pkts (UINT8 *p);
1072 #endif /* (BTM_SCO_HCI_INCLUDED == TRUE ) */
1073 #else
1074 #define btm_sco_chk_pend_unpark(hci_status, hci_handle)
1075 #endif /* BTM_SCO_INCLUDED */
1076 void btm_qos_setup_complete (UINT8 status, UINT16 handle, FLOW_SPEC *p_flow);
1077 void btm_qos_setup_timeout (void *p_tle);
1078 
1079 
1080 #if (BLE_50_FEATURE_SUPPORT == TRUE)
1081 void btm_create_sync_callback(UINT8 status);
1082 void btm_set_phy_callback(UINT8 status);
1083 void btm_read_phy_callback(uint8_t hci_status, uint16_t conn_handle, uint8_t tx_phy, uint8_t rx_phy);
1084 #endif
1085 /* Internal functions provided by btm_sco.c
1086 ********************************************
1087 */
1088 void btm_sco_init (void);
1089 void btm_sco_connected (UINT8 hci_status, BD_ADDR bda, UINT16 hci_handle,
1090                         tBTM_ESCO_DATA *p_esco_data);
1091 void btm_esco_proc_conn_chg (UINT8 status, UINT16 handle, UINT8 tx_interval,
1092                              UINT8 retrans_window, UINT16 rx_pkt_len,
1093                              UINT16 tx_pkt_len);
1094 void btm_sco_conn_req (BD_ADDR bda,  DEV_CLASS dev_class, UINT8 link_type);
1095 void btm_sco_removed (UINT16 hci_handle, UINT8 reason);
1096 void btm_sco_acl_removed (BD_ADDR bda);
1097 void btm_route_sco_data (BT_HDR *p_msg);
1098 BOOLEAN btm_is_sco_active (UINT16 handle);
1099 void btm_remove_sco_links (BD_ADDR bda);
1100 BOOLEAN btm_is_sco_active_by_bdaddr (BD_ADDR remote_bda);
1101 
1102 tBTM_SCO_TYPE btm_read_def_esco_mode (tBTM_ESCO_PARAMS *p_parms);
1103 UINT16  btm_find_scb_by_handle (UINT16 handle);
1104 void btm_sco_flush_sco_data(UINT16 sco_inx);
1105 
1106 /* Internal functions provided by btm_devctl.c
1107 **********************************************
1108 */
1109 void btm_dev_init (void);
1110 void btm_dev_timeout (TIMER_LIST_ENT *p_tle);
1111 void btm_read_local_name_complete (UINT8 *p, UINT16 evt_len);
1112 
1113 #if (BLE_INCLUDED == TRUE)
1114 void btm_ble_add_2_white_list_complete(UINT8 status);
1115 void btm_ble_remove_from_white_list_complete(UINT8 *p, UINT16 evt_len);
1116 void btm_ble_clear_white_list_complete(UINT8 *p, UINT16 evt_len);
1117 BOOLEAN btm_ble_addr_resolvable(BD_ADDR rpa, tBTM_SEC_DEV_REC *p_dev_rec);
1118 tBTM_STATUS btm_ble_read_resolving_list_entry(tBTM_SEC_DEV_REC *p_dev_rec);
1119 BOOLEAN btm_ble_resolving_list_load_dev(tBTM_SEC_DEV_REC *p_dev_rec);
1120 void btm_ble_resolving_list_remove_dev(tBTM_SEC_DEV_REC *p_dev_rec);
1121 #endif  /* BLE_INCLUDED */
1122 
1123 /* Vendor Specific Command complete evt handler */
1124 void btm_vsc_complete (UINT8 *p, UINT16 cc_opcode, UINT16 evt_len,
1125                        tBTM_CMPL_CB *p_vsc_cplt_cback);
1126 void btm_inq_db_reset (void);
1127 void btm_vendor_specific_evt (UINT8 *p, UINT8 evt_len);
1128 void btm_delete_stored_link_key_complete (UINT8 *p);
1129 void btm_report_device_status (tBTM_DEV_STATUS status);
1130 void btm_set_afh_channels_complete (UINT8 *p);
1131 void btm_ble_set_channels_complete (UINT8 *p);
1132 
1133 /* Internal functions provided by btm_dev.c
1134 **********************************************
1135 */
1136 BOOLEAN btm_dev_support_switch (BD_ADDR bd_addr);
1137 
1138 tBTM_SEC_DEV_REC  *btm_sec_alloc_dev (BD_ADDR bd_addr);
1139 void              btm_sec_free_dev (tBTM_SEC_DEV_REC *p_dev_rec, tBT_TRANSPORT transport);
1140 tBTM_SEC_DEV_REC  *btm_find_dev (BD_ADDR bd_addr);
1141 tBTM_SEC_DEV_REC  *btm_find_or_alloc_dev (BD_ADDR bd_addr);
1142 tBTM_SEC_DEV_REC  *btm_find_dev_by_handle (UINT16 handle);
1143 tBTM_BOND_TYPE     btm_get_bond_type_dev(BD_ADDR bd_addr);
1144 BOOLEAN            btm_set_bond_type_dev(BD_ADDR bd_addr,
1145         tBTM_BOND_TYPE bond_type);
1146 void               btm_sec_dev_init(void);
1147 void               btm_sec_dev_free(void);
1148 
1149 /* Internal functions provided by btm_sec.c
1150 **********************************************
1151 */
1152 BOOLEAN btm_dev_support_switch (BD_ADDR bd_addr);
1153 tBTM_STATUS  btm_sec_l2cap_access_req (BD_ADDR bd_addr, UINT16 psm,
1154                                        UINT16 handle, CONNECTION_TYPE conn_type,
1155                                        tBTM_SEC_CALLBACK *p_callback, void *p_ref_data);
1156 tBTM_STATUS  btm_sec_mx_access_request (BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator,
1157                                         UINT32 mx_proto_id, UINT32 mx_chan_id,
1158                                         tBTM_SEC_CALLBACK *p_callback, void *p_ref_data);
1159 void  btm_sec_conn_req (UINT8 *bda, UINT8 *dc);
1160 void btm_create_conn_cancel_complete (UINT8 *p);
1161 void btm_read_linq_tx_power_complete (UINT8 *p);
1162 
1163 void  btm_sec_init (UINT8 sec_mode);
1164 void  btm_sec_dev_reset (void);
1165 void  btm_sec_abort_access_req (BD_ADDR bd_addr);
1166 void  btm_sec_auth_complete (UINT16 handle, UINT8 status);
1167 void  btm_sec_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable);
1168 void  btm_sec_connected (UINT8 *bda, UINT16 handle, UINT8 status, UINT8 enc_mode);
1169 tBTM_STATUS btm_sec_disconnect (UINT16 handle, UINT8 reason);
1170 void  btm_sec_disconnected (UINT16 handle, UINT8 reason);
1171 void  btm_sec_rmt_name_request_complete (UINT8 *bd_addr, UINT8 *bd_name, UINT8 status);
1172 void  btm_sec_rmt_host_support_feat_evt (UINT8 *p);
1173 void  btm_io_capabilities_req (UINT8 *p);
1174 void  btm_io_capabilities_rsp (UINT8 *p);
1175 void  btm_proc_sp_req_evt (tBTM_SP_EVT event, UINT8 *p);
1176 void  btm_keypress_notif_evt (UINT8 *p);
1177 void  btm_simple_pair_complete (UINT8 *p);
1178 void  btm_sec_link_key_notification (UINT8 *p_bda, UINT8 *p_link_key, UINT8 key_type);
1179 void  btm_sec_link_key_request (UINT8 *p_bda);
1180 void  btm_sec_pin_code_request (UINT8 *p_bda);
1181 void  btm_sec_update_clock_offset (UINT16 handle, UINT16 clock_offset);
1182 void  btm_sec_dev_rec_cback_event (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 res, BOOLEAN is_le_transport);
1183 void btm_sec_set_peer_sec_caps (tACL_CONN *p_acl_cb, tBTM_SEC_DEV_REC *p_dev_rec);
1184 
1185 #if BLE_INCLUDED == TRUE
1186 void  btm_sec_clear_ble_keys (tBTM_SEC_DEV_REC  *p_dev_rec);
1187 BOOLEAN btm_sec_find_bonded_dev (UINT8 start_idx, UINT16 *p_found_handle, tBTM_SEC_DEV_REC **p_rec);
1188 BOOLEAN btm_sec_is_a_bonded_dev (BD_ADDR bda);
1189 void btm_consolidate_dev(tBTM_SEC_DEV_REC *p_target_rec);
1190 BOOLEAN btm_sec_is_le_capable_dev (BD_ADDR bda);
1191 BOOLEAN btm_ble_init_pseudo_addr (tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR new_pseudo_addr);
1192 extern BOOLEAN btm_ble_start_sec_check(BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator,
1193                             tBTM_SEC_CALLBACK *p_callback, void *p_ref_data);
1194 #endif /* BLE_INCLUDED */
1195 
1196 extern tBTM_SEC_SERV_REC *btm_sec_find_first_serv (CONNECTION_TYPE conn_type, UINT16 psm);
1197 
1198 tINQ_DB_ENT *btm_inq_db_new (BD_ADDR p_bda);
1199 
1200 #if BTM_OOB_INCLUDED == TRUE
1201 void  btm_rem_oob_req (UINT8 *p);
1202 void  btm_read_local_oob_complete (UINT8 *p);
1203 #else
1204 #define btm_rem_oob_req(p)
1205 #define btm_read_local_oob_complete(p)
1206 #endif
1207 
1208 void  btm_acl_resubmit_page (void);
1209 void  btm_acl_reset_paging (void);
1210 void  btm_acl_paging (BT_HDR *p, BD_ADDR dest);
1211 UINT8 btm_sec_clr_service_by_psm (UINT16 psm);
1212 void  btm_sec_clr_temp_auth_service (BD_ADDR bda);
1213 
1214 void btm_ble_lock_init(void);
1215 
1216 void btm_ble_sem_init(void);
1217 
1218 void btm_ble_sem_free(void);
1219 
1220 void btm_ble_lock_free(void);
1221 
1222 void btm_sec_handle_remote_legacy_auth_cmp(UINT16 handle);
1223 void btm_sec_update_legacy_auth_state(tACL_CONN *p_acl_cb, UINT8 legacy_auth_state);
1224 BOOLEAN btm_sec_legacy_authentication_mutual (tBTM_SEC_DEV_REC *p_dev_rec);
1225 BOOLEAN btm_find_sec_dev_in_list (void *p_node_data, void *context);
1226 
1227 BOOLEAN btm_sec_dev_authorization(BD_ADDR bd_addr, BOOLEAN authorized);
1228 
1229 /*
1230 #ifdef __cplusplus
1231 }
1232 #endif
1233 */
1234 
1235 #endif
1236