1 /****************************************************************************** 2 * 3 * Copyright 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 <cstdint> 22 #include <memory> 23 #include <string> 24 25 #include "gd/common/circular_buffer.h" 26 #include "osi/include/allocator.h" 27 #include "osi/include/fixed_queue.h" 28 #include "osi/include/list.h" 29 #include "stack/acl/acl.h" 30 #include "stack/btm/btm_ble_int_types.h" 31 #include "stack/btm/btm_sco.h" 32 #include "stack/btm/neighbor_inquiry.h" 33 #include "stack/btm/security_device_record.h" 34 #include "stack/include/bt_octets.h" 35 #include "stack/include/btm_ble_api_types.h" 36 #include "stack/include/security_client_callbacks.h" 37 #include "types/raw_address.h" 38 39 #define BTM_MAX_SCN_ 31 // PORT_MAX_RFC_PORTS packages/modules/Bluetooth/system/stack/include/rfcdefs.h 40 41 constexpr size_t kMaxLogSize = 255; 42 constexpr size_t kBtmLogHistoryBufferSize = 100; 43 44 class TimestampedStringCircularBuffer 45 : public bluetooth::common::TimestampedCircularBuffer<std::string> { 46 public: TimestampedStringCircularBuffer(size_t size)47 explicit TimestampedStringCircularBuffer(size_t size) 48 : bluetooth::common::TimestampedCircularBuffer<std::string>(size) {} 49 Push(std::string s)50 void Push(std::string s) { 51 bluetooth::common::TimestampedCircularBuffer<std::string>::Push( 52 s.substr(0, kMaxLogSize)); 53 } 54 55 template <typename... Args> Push(Args...args)56 void Push(Args... args) { 57 char buf[kMaxLogSize]; 58 std::snprintf(buf, sizeof(buf), args...); 59 bluetooth::common::TimestampedCircularBuffer<std::string>::Push( 60 std::string(buf)); 61 } 62 }; 63 64 /* 65 * Local device configuration 66 */ 67 typedef struct { 68 tBTM_LOC_BD_NAME bd_name; /* local Bluetooth device name */ 69 bool pin_type; /* true if PIN type is fixed */ 70 uint8_t pin_code_len; /* Bonding information */ 71 PIN_CODE pin_code; /* PIN CODE if pin type is fixed */ 72 } tBTM_CFG; 73 74 /* Pairing State */ 75 enum { 76 BTM_PAIR_STATE_IDLE, /* Idle */ 77 BTM_PAIR_STATE_GET_REM_NAME, /* Getting the remote name (to check for SM4) */ 78 BTM_PAIR_STATE_WAIT_PIN_REQ, /* Started authentication, waiting for PIN req 79 (PIN is pre-fetched) */ 80 BTM_PAIR_STATE_WAIT_LOCAL_PIN, /* Waiting for local PIN code */ 81 BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM, /* Waiting user 'yes' to numeric 82 confirmation */ 83 BTM_PAIR_STATE_KEY_ENTRY, /* Key entry state (we are a keyboard) */ 84 BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP, /* Waiting for local response to peer OOB 85 data */ 86 BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS, /* Waiting for local IO capabilities and OOB 87 data */ 88 BTM_PAIR_STATE_INCOMING_SSP, /* Incoming SSP (got peer IO caps when idle) */ 89 BTM_PAIR_STATE_WAIT_AUTH_COMPLETE, /* All done, waiting authentication 90 cpmplete */ 91 BTM_PAIR_STATE_WAIT_DISCONNECT /* Waiting to disconnect the ACL */ 92 }; 93 typedef uint8_t tBTM_PAIRING_STATE; 94 95 #define BTM_PAIR_FLAGS_WE_STARTED_DD \ 96 0x01 /* We want to do dedicated bonding */ 97 #define BTM_PAIR_FLAGS_PEER_STARTED_DD \ 98 0x02 /* Peer initiated dedicated bonding */ 99 #define BTM_PAIR_FLAGS_DISC_WHEN_DONE 0x04 /* Disconnect when done */ 100 #define BTM_PAIR_FLAGS_PIN_REQD \ 101 0x08 /* set this bit when pin_callback is called */ 102 #define BTM_PAIR_FLAGS_PRE_FETCH_PIN \ 103 0x10 /* set this bit when pre-fetch pin */ 104 #define BTM_PAIR_FLAGS_REJECTED_CONNECT \ 105 0x20 /* set this bit when rejected incoming connection */ 106 #define BTM_PAIR_FLAGS_WE_CANCEL_DD \ 107 0x40 /* set this bit when cancelling a bonding procedure */ 108 #define BTM_PAIR_FLAGS_LE_ACTIVE \ 109 0x80 /* use this bit when SMP pairing is active */ 110 111 typedef struct { 112 bool is_mux; 113 RawAddress bd_addr; 114 uint16_t psm; 115 bool is_orig; 116 tBTM_SEC_CALLBACK* p_callback; 117 void* p_ref_data; 118 uint16_t rfcomm_security_requirement; 119 tBT_TRANSPORT transport; 120 tBTM_BLE_SEC_ACT sec_act; 121 } tBTM_SEC_QUEUE_ENTRY; 122 123 /* Define a structure to hold all the BTM data 124 */ 125 126 #define BTM_STATE_BUFFER_SIZE 5 /* size of state buffer */ 127 128 /* Define the Device Management control structure 129 */ 130 typedef struct tBTM_DEVCB { 131 tBTM_VS_EVT_CB* p_vend_spec_cb[BTM_MAX_VSE_CALLBACKS]; /* Register for vendor 132 specific events */ 133 134 tBTM_CMPL_CB* 135 p_stored_link_key_cmpl_cb; /* Read/Write/Delete stored link key */ 136 137 alarm_t* read_local_name_timer; /* Read local name timer */ 138 tBTM_CMPL_CB* p_rln_cmpl_cb; /* Callback function to be called when */ 139 /* read local name function complete */ 140 141 alarm_t* read_rssi_timer; /* Read RSSI timer */ 142 tBTM_CMPL_CB* p_rssi_cmpl_cb; /* Callback function to be called when */ 143 /* read RSSI function completes */ 144 145 alarm_t* read_failed_contact_counter_timer; /* Read Failed Contact Counter */ 146 /* timer */ 147 tBTM_CMPL_CB* p_failed_contact_counter_cmpl_cb; /* Callback function to be */ 148 /* called when read Failed Contact Counter function completes */ 149 150 alarm_t* 151 read_automatic_flush_timeout_timer; /* Read Automatic Flush Timeout */ 152 /* timer */ 153 tBTM_CMPL_CB* p_automatic_flush_timeout_cmpl_cb; /* Callback function to be */ 154 /* called when read Automatic Flush Timeout function completes */ 155 156 alarm_t* read_link_quality_timer; 157 tBTM_CMPL_CB* p_link_qual_cmpl_cb; /* Callback function to be called when */ 158 /* read link quality function completes */ 159 160 alarm_t* read_tx_power_timer; /* Read tx power timer */ 161 tBTM_CMPL_CB* p_tx_power_cmpl_cb; /* Callback function to be called */ 162 163 DEV_CLASS dev_class; /* Local device class */ 164 165 tBTM_CMPL_CB* 166 p_le_test_cmd_cmpl_cb; /* Callback function to be called when 167 LE test mode command has been sent successfully */ 168 169 RawAddress read_tx_pwr_addr; /* read TX power target address */ 170 171 tBTM_BLE_LOCAL_ID_KEYS id_keys; /* local BLE ID keys */ 172 Octet16 ble_encryption_key_value; /* BLE encryption key */ 173 174 #if (BTM_BLE_CONFORMANCE_TESTING == TRUE) 175 bool no_disc_if_pair_fail; 176 bool enable_test_mac_val; 177 BT_OCTET8 test_mac; 178 bool enable_test_local_sign_cntr; 179 uint32_t test_local_sign_cntr; 180 #endif 181 182 tBTM_IO_CAP loc_io_caps; /* IO capability of the local device */ 183 tBTM_AUTH_REQ loc_auth_req; /* the auth_req flag */ 184 InittBTM_DEVCB185 void Init() { 186 read_local_name_timer = alarm_new("btm.read_local_name_timer"); 187 read_rssi_timer = alarm_new("btm.read_rssi_timer"); 188 read_failed_contact_counter_timer = 189 alarm_new("btm.read_failed_contact_counter_timer"); 190 read_automatic_flush_timeout_timer = 191 alarm_new("btm.read_automatic_flush_timeout_timer"); 192 read_link_quality_timer = alarm_new("btm.read_link_quality_timer"); 193 read_tx_power_timer = alarm_new("btm.read_tx_power_timer"); 194 } 195 FreetBTM_DEVCB196 void Free() { 197 alarm_free(read_local_name_timer); 198 alarm_free(read_rssi_timer); 199 alarm_free(read_failed_contact_counter_timer); 200 alarm_free(read_automatic_flush_timeout_timer); 201 alarm_free(read_link_quality_timer); 202 alarm_free(read_tx_power_timer); 203 } 204 } tBTM_DEVCB; 205 206 typedef struct tBTM_CB { 207 tBTM_CFG cfg; /* Device configuration */ 208 209 /***************************************************** 210 ** Device control 211 *****************************************************/ 212 tBTM_DEVCB devcb; 213 214 /***************************************************** 215 ** BLE Device controllers 216 *****************************************************/ 217 tBTM_BLE_CB ble_ctr_cb; 218 219 private: 220 friend void btm_ble_ltk_request_reply(const RawAddress& bda, bool use_stk, 221 const Octet16& stk); 222 friend tBTM_STATUS btm_ble_start_encrypt(const RawAddress& bda, bool use_stk, 223 Octet16* p_stk); 224 friend void btm_ble_ltk_request_reply(const RawAddress& bda, bool use_stk, 225 const Octet16& stk); 226 uint16_t enc_handle{0}; 227 228 friend void btm_ble_ltk_request(uint16_t handle, uint8_t rand[8], 229 uint16_t ediv); 230 BT_OCTET8 enc_rand; /* received rand value from LTK request*/ 231 232 uint16_t ediv{0}; /* received ediv value from LTK request */ 233 234 uint8_t key_size{0}; 235 236 public: 237 tBTM_BLE_VSC_CB cmn_ble_vsc_cb; 238 239 /* Packet types supported by the local device */ 240 uint16_t btm_sco_pkt_types_supported{0}; 241 242 /***************************************************** 243 ** Inquiry 244 *****************************************************/ 245 tBTM_INQUIRY_VAR_ST btm_inq_vars; 246 247 /***************************************************** 248 ** SCO Management 249 *****************************************************/ 250 tSCO_CB sco_cb; 251 252 /***************************************************** 253 ** Security Management 254 *****************************************************/ 255 tBTM_APPL_INFO api; 256 257 #define BTM_SEC_MAX_RMT_NAME_CALLBACKS 2 258 tBTM_RMT_NAME_CALLBACK* p_rmt_name_callback[BTM_SEC_MAX_RMT_NAME_CALLBACKS]; 259 260 tBTM_SEC_DEV_REC* p_collided_dev_rec{nullptr}; 261 alarm_t* sec_collision_timer{nullptr}; 262 uint64_t collision_start_time{0}; 263 uint32_t dev_rec_count{0}; /* Counter used for device record timestamp */ 264 uint8_t security_mode{0}; 265 bool pairing_disabled{false}; 266 bool security_mode_changed{false}; /* mode changed during bonding */ 267 bool pin_type_changed{false}; /* pin type changed during bonding */ 268 bool sec_req_pending{false}; /* true if a request is pending */ 269 270 uint8_t pin_code_len{0}; /* for legacy devices */ 271 PIN_CODE pin_code; /* for legacy devices */ 272 tBTM_PAIRING_STATE pairing_state{ 273 BTM_PAIR_STATE_IDLE}; /* The current pairing state */ 274 uint8_t pairing_flags{0}; /* The current pairing flags */ 275 RawAddress pairing_bda; /* The device currently pairing */ 276 alarm_t* pairing_timer{nullptr}; /* Timer for pairing process */ 277 alarm_t* execution_wait_timer{nullptr}; /* To avoid concurrent auth request */ 278 uint16_t disc_handle{0}; /* for legacy devices */ 279 uint8_t disc_reason{0}; /* for legacy devices */ 280 tBTM_SEC_SERV_REC sec_serv_rec[BTM_SEC_MAX_SERVICE_RECORDS]; 281 list_t* sec_dev_rec{nullptr}; /* list of tBTM_SEC_DEV_REC */ 282 tBTM_SEC_SERV_REC* p_out_serv{nullptr}; 283 tBTM_MKEY_CALLBACK* mkey_cback{nullptr}; 284 285 RawAddress connecting_bda; 286 DEV_CLASS connecting_dc; 287 uint8_t trace_level; 288 bool is_paging{false}; /* true, if paging is in progess */ 289 bool is_inquiry{false}; /* true, if inquiry is in progess */ 290 fixed_queue_t* page_queue{nullptr}; 291 292 bool paging{false}; set_pagingtBTM_CB293 void set_paging() { paging = true; } reset_pagingtBTM_CB294 void reset_paging() { paging = false; } is_paging_activetBTM_CB295 bool is_paging_active() const { 296 return paging; 297 } // TODO remove all this paging state 298 299 fixed_queue_t* sec_pending_q{nullptr}; /* pending sequrity requests in 300 tBTM_SEC_QUEUE_ENTRY format */ 301 302 // BQR Receiver 303 tBTM_BT_QUALITY_REPORT_RECEIVER* p_bqr_report_receiver{nullptr}; 304 305 #define BTM_CODEC_TYPE_MAX_RECORDS 32 306 tBTM_BT_DYNAMIC_AUDIO_BUFFER_CB 307 dynamic_audio_buffer_cb[BTM_CODEC_TYPE_MAX_RECORDS]; 308 309 tACL_CB acl_cb_; 310 311 std::shared_ptr<TimestampedStringCircularBuffer> history_{nullptr}; 312 InittBTM_CB313 void Init(uint8_t initial_security_mode) { 314 memset(&cfg, 0, sizeof(cfg)); 315 memset(&devcb, 0, sizeof(devcb)); 316 memset(&ble_ctr_cb, 0, sizeof(ble_ctr_cb)); 317 memset(&enc_rand, 0, sizeof(enc_rand)); 318 memset(&cmn_ble_vsc_cb, 0, sizeof(cmn_ble_vsc_cb)); 319 memset(&btm_inq_vars, 0, sizeof(btm_inq_vars)); 320 memset(&sco_cb, 0, sizeof(sco_cb)); 321 memset(&api, 0, sizeof(api)); 322 memset(p_rmt_name_callback, 0, sizeof(p_rmt_name_callback)); 323 memset(&pin_code, 0, sizeof(pin_code)); 324 memset(sec_serv_rec, 0, sizeof(sec_serv_rec)); 325 326 connecting_bda = RawAddress::kEmpty; 327 memset(&connecting_dc, 0, sizeof(connecting_dc)); 328 329 acl_cb_ = {}; 330 331 page_queue = fixed_queue_new(SIZE_MAX); 332 sec_pending_q = fixed_queue_new(SIZE_MAX); 333 sec_collision_timer = alarm_new("btm.sec_collision_timer"); 334 pairing_timer = alarm_new("btm.pairing_timer"); 335 execution_wait_timer = alarm_new("btm.execution_wait_timer"); 336 337 #if defined(BTM_INITIAL_TRACE_LEVEL) 338 trace_level = BTM_INITIAL_TRACE_LEVEL; 339 #else 340 trace_level = BT_TRACE_LEVEL_NONE; /* No traces */ 341 #endif 342 security_mode = initial_security_mode; 343 pairing_bda = RawAddress::kAny; 344 sec_dev_rec = list_new(osi_free); 345 346 /* Initialize BTM component structures */ 347 btm_inq_vars.Init(); /* Inquiry Database and Structures */ 348 acl_cb_ = {}; 349 sco_cb.Init(); /* SCO Database and Structures (If included) */ 350 devcb.Init(); 351 352 history_ = std::make_shared<TimestampedStringCircularBuffer>( 353 kBtmLogHistoryBufferSize); 354 CHECK(history_ != nullptr); 355 history_->Push(std::string("Initialized btm history")); 356 } 357 FreetBTM_CB358 void Free() { 359 history_.reset(); 360 361 devcb.Free(); 362 sco_cb.Free(); 363 btm_inq_vars.Free(); 364 365 fixed_queue_free(page_queue, nullptr); 366 page_queue = nullptr; 367 368 fixed_queue_free(sec_pending_q, nullptr); 369 sec_pending_q = nullptr; 370 371 list_free(sec_dev_rec); 372 sec_dev_rec = nullptr; 373 374 alarm_free(sec_collision_timer); 375 sec_collision_timer = nullptr; 376 377 alarm_free(pairing_timer); 378 pairing_timer = nullptr; 379 380 alarm_free(execution_wait_timer); 381 execution_wait_timer = nullptr; 382 } 383 384 private: 385 friend uint8_t BTM_AllocateSCN(void); 386 friend bool BTM_TryAllocateSCN(uint8_t scn); 387 friend bool BTM_FreeSCN(uint8_t scn); 388 uint8_t btm_scn[BTM_MAX_SCN_]; 389 } tBTM_CB; 390 391 /* security action for L2CAP COC channels */ 392 #define BTM_SEC_OK 1 393 #define BTM_SEC_ENCRYPT 2 /* encrypt the link with current key */ 394 #define BTM_SEC_ENCRYPT_NO_MITM 3 /* unauthenticated encryption or better */ 395 #define BTM_SEC_ENCRYPT_MITM 4 /* authenticated encryption */ 396 #define BTM_SEC_ENC_PENDING 5 /* wait for link encryption pending */ 397 398 typedef uint8_t tBTM_SEC_ACTION; 399 400 #endif // BTM_INT_TYPES_H 401