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