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