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