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