1 /****************************************************************************** 2 * 3 * Copyright 2009-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #ifndef SYSTEM_BTIF_INCLUDE_BTIF_STORAGE_H_ 20 #define SYSTEM_BTIF_INCLUDE_BTIF_STORAGE_H_ 21 22 #include <hardware/bluetooth.h> 23 24 #include <string> 25 26 #include "internal_include/bt_target.h" 27 #include "stack/include/bt_device_type.h" 28 #include "stack/include/bt_octets.h" 29 #include "stack/include/btm_sec_api_types.h" 30 #include "types/ble_address_with_type.h" 31 #include "types/bluetooth/uuid.h" 32 #include "types/raw_address.h" 33 #include "types/bt_transport.h" 34 35 /******************************************************************************* 36 * Constants & Macros 37 ******************************************************************************/ 38 #define BTIF_STORAGE_FILL_PROPERTY(p_prop, t, l, p_v) \ 39 do { \ 40 (p_prop)->type = (t); \ 41 (p_prop)->len = (l); \ 42 (p_prop)->val = (p_v); \ 43 } while (0) 44 45 #define STORAGE_BDADDR_STRING_SZ (18) /* 00:11:22:33:44:55 */ 46 #define STORAGE_UUID_STRING_SIZE (36 + 1) /* 00001200-0000-1000-8000-00805f9b34fb; */ 47 #define STORAGE_PINLEN_STRING_MAX_SIZE (2) /* ascii pinlen max chars */ 48 #define STORAGE_KEYTYPE_STRING_MAX_SIZE (1) /* ascii keytype max chars */ 49 50 #define STORAGE_KEY_TYPE_MAX (10) 51 52 /******************************************************************************* 53 * Functions 54 ******************************************************************************/ 55 56 /******************************************************************************* 57 * 58 * Function btif_in_fetch_bonded_devices 59 * 60 * Description Helper function to fetch the bonded devices 61 * from NVRAM 62 * 63 * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise 64 * 65 ******************************************************************************/ 66 bt_status_t btif_in_fetch_bonded_device(const std::string& bdstr); 67 68 typedef struct { 69 uint32_t num_devices; 70 RawAddress devices[BTM_SEC_MAX_DEVICE_RECORDS]; 71 } btif_bonded_devices_t; 72 73 bt_status_t btif_in_fetch_bonded_ble_device(const std::string& remote_bd_addr, int add, 74 btif_bonded_devices_t* p_bonded_devices); 75 76 /******************************************************************************* 77 * 78 * Function btif_storage_get_adapter_property 79 * 80 * Description BTIF storage API - Fetches the adapter property->type 81 * from NVRAM and fills property->val. 82 * Caller should provide memory for property->val and 83 * set the property->val 84 * 85 * Returns BT_STATUS_SUCCESS if the fetch was successful, 86 * BT_STATUS_FAIL otherwise 87 * 88 ******************************************************************************/ 89 bt_status_t btif_storage_get_adapter_property(bt_property_t* property); 90 91 /******************************************************************************* 92 * 93 * Function btif_storage_set_adapter_property 94 * 95 * Description BTIF storage API - Stores the adapter property 96 * to NVRAM 97 * 98 * Returns BT_STATUS_SUCCESS if the store was successful, 99 * BT_STATUS_FAIL otherwise 100 * 101 ******************************************************************************/ 102 bt_status_t btif_storage_set_adapter_property(bt_property_t* property); 103 104 /******************************************************************************* 105 * 106 * Function btif_storage_get_remote_device_property 107 * 108 * Description BTIF storage API - Fetches the remote device property->type 109 * from NVRAM and fills property->val. 110 * Caller should provide memory for property->val and 111 * set the property->val 112 * 113 * Returns BT_STATUS_SUCCESS if the fetch was successful, 114 * BT_STATUS_FAIL otherwise 115 * 116 ******************************************************************************/ 117 bt_status_t btif_storage_get_remote_device_property(const RawAddress* remote_bd_addr, 118 bt_property_t* property); 119 120 /******************************************************************************* 121 * 122 * Function btif_storage_set_remote_device_property 123 * 124 * Description BTIF storage API - Stores the remote device property 125 * to NVRAM 126 * 127 * Returns BT_STATUS_SUCCESS if the store was successful, 128 * BT_STATUS_FAIL otherwise 129 * 130 ******************************************************************************/ 131 bt_status_t btif_storage_set_remote_device_property(const RawAddress* remote_bd_addr, 132 bt_property_t* property); 133 134 /******************************************************************************* 135 * 136 * Function btif_storage_add_remote_device 137 * 138 * Description BTIF storage API - Adds a newly discovered device to 139 * track along with the timestamp. Also, stores the various 140 * properties - RSSI, BDADDR, NAME (if found in EIR) 141 * 142 * Returns BT_STATUS_SUCCESS if successful, 143 * BT_STATUS_FAIL otherwise 144 * 145 ******************************************************************************/ 146 bt_status_t btif_storage_add_remote_device(const RawAddress* remote_bd_addr, 147 uint32_t num_properties, bt_property_t* properties); 148 149 /******************************************************************************* 150 * 151 * Function btif_storage_add_bonded_device 152 * 153 * Description BTIF storage API - Adds the newly bonded device to NVRAM 154 * along with the link-key, Key type and Pin key length 155 * 156 * Returns BT_STATUS_SUCCESS if the store was successful, 157 * BT_STATUS_FAIL otherwise 158 * 159 ******************************************************************************/ 160 bt_status_t btif_storage_add_bonded_device(RawAddress* remote_bd_addr, LinkKey link_key, 161 uint8_t key_type, uint8_t pin_length); 162 163 /******************************************************************************* 164 * 165 * Function btif_storage_remove_bonded_device 166 * 167 * Description BTIF storage API - Deletes the bonded device from NVRAM 168 * 169 * Returns BT_STATUS_SUCCESS if the deletion was successful, 170 * BT_STATUS_FAIL otherwise 171 * 172 ******************************************************************************/ 173 bt_status_t btif_storage_remove_bonded_device(const RawAddress* remote_bd_addr); 174 175 /******************************************************************************* 176 * 177 * Function btif_storage_load_le_devices 178 * 179 * Description BTIF storage API - Loads all LE-only and Dual Mode devices 180 * from NVRAM. This API invokes the adaper_properties_cb. 181 * It also invokes invoke_address_consolidate_cb 182 * to consolidate each Dual Mode device and 183 * invoke_le_address_associate_cb to associate each LE-only 184 * device between its RPA, identity address, and identity address type. 185 * 186 ******************************************************************************/ 187 void btif_storage_load_le_devices(void); 188 189 /******************************************************************************* 190 * 191 * Function btif_storage_load_bonded_devices 192 * 193 * Description BTIF storage API - Loads all the bonded devices from NVRAM 194 * and adds to the BTA. 195 * Additionally, this API also invokes the adaper_properties_cb 196 * and remote_device_properties_cb for each of the bonded 197 * devices. 198 * 199 * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise 200 * 201 ******************************************************************************/ 202 bt_status_t btif_storage_load_bonded_devices(void); 203 204 /******************************************************************************* 205 * 206 * Function btif_storage_add_hid_device_info 207 * 208 * Description BTIF storage API - Adds the hid information of bonded hid 209 * devices-to NVRAM 210 * 211 * Returns BT_STATUS_SUCCESS if the store was successful, 212 * BT_STATUS_FAIL otherwise 213 * 214 ******************************************************************************/ 215 216 bt_status_t btif_storage_add_hid_device_info(const tAclLinkSpec& link_spec, uint16_t attr_mask, 217 uint8_t sub_class, uint8_t app_id, uint16_t vendor_id, 218 uint16_t product_id, uint16_t version, 219 uint8_t ctry_code, uint16_t ssr_max_latency, 220 uint16_t ssr_min_tout, uint16_t dl_len, 221 uint8_t* dsc_list); 222 223 /******************************************************************************* 224 * 225 * Function btif_storage_load_bonded_hid_info 226 * 227 * Description BTIF storage API - Loads hid info for all the bonded devices 228 * from NVRAM and adds those devices to the BTA_HH. 229 * 230 * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise 231 * 232 ******************************************************************************/ 233 bt_status_t btif_storage_load_bonded_hid_info(void); 234 235 /******************************************************************************* 236 * 237 * Function btif_storage_remove_hid_info 238 * 239 * Description BTIF storage API - Deletes the bonded hid device info from 240 * NVRAM 241 * 242 * Returns BT_STATUS_SUCCESS if the deletion was successful, 243 * BT_STATUS_FAIL otherwise 244 * 245 ******************************************************************************/ 246 bt_status_t btif_storage_remove_hid_info(const tAclLinkSpec& link_spec); 247 248 /** Loads information about bonded hearing aid devices */ 249 void btif_storage_load_bonded_hearing_aids(); 250 251 /** Deletes the bonded hearing aid device info from NVRAM */ 252 void btif_storage_remove_hearing_aid(const RawAddress& address); 253 254 /** Set/Unset the hearing aid device HEARING_AID_IS_ACCEPTLISTED flag. */ 255 void btif_storage_set_hearing_aid_acceptlist(const RawAddress& address, bool add_to_acceptlist); 256 257 /** Stores information about GATT Client supported features support */ 258 void btif_storage_set_gatt_cl_supp_feat(const RawAddress& bd_addr, uint8_t feat); 259 260 /** Get client supported features */ 261 uint8_t btif_storage_get_gatt_cl_supp_feat(const RawAddress& bd_addr); 262 263 /** Remove client supported features */ 264 void btif_storage_remove_gatt_cl_supp_feat(const RawAddress& bd_addr); 265 266 /** Stores information about GATT server supported features */ 267 void btif_storage_set_gatt_sr_supp_feat(const RawAddress& addr, uint8_t feat); 268 269 /** Gets information about GATT server supported features */ 270 uint8_t btif_storage_get_sr_supp_feat(const RawAddress& bd_addr); 271 272 /** Store last server database hash for remote client */ 273 void btif_storage_set_gatt_cl_db_hash(const RawAddress& bd_addr, Octet16 hash); 274 275 /** Get last server database hash for remote client */ 276 Octet16 btif_storage_get_gatt_cl_db_hash(const RawAddress& bd_addr); 277 278 /** Remove last server database hash for remote client */ 279 void btif_storage_remove_gatt_cl_db_hash(const RawAddress& bd_addr); 280 281 /** Get the hearing aid device properties. */ 282 bool btif_storage_get_hearing_aid_prop(const RawAddress& address, uint8_t* capabilities, 283 uint64_t* hi_sync_id, uint16_t* render_delay, 284 uint16_t* preparation_delay, uint16_t* codecs); 285 286 /** Store Le Audio device autoconnect flag */ 287 void btif_storage_set_leaudio_autoconnect(const RawAddress& addr, bool autoconnect); 288 289 /** Store PACs information */ 290 void btif_storage_leaudio_update_pacs_bin(const RawAddress& addr); 291 292 /** Store ASEs information */ 293 void btif_storage_leaudio_update_ase_bin(const RawAddress& addr); 294 295 /** Store Handles information */ 296 void btif_storage_leaudio_update_handles_bin(const RawAddress& addr); 297 298 /** Store Le Audio device audio locations */ 299 void btif_storage_set_leaudio_sink_audio_location(const RawAddress& addr, uint32_t sink_location); 300 301 /** Store Le Audio device audio locations */ 302 void btif_storage_set_leaudio_source_audio_location(const RawAddress& addr, 303 uint32_t source_location); 304 305 /** Store Le Audio device context types */ 306 void btif_storage_set_leaudio_supported_context_types(const RawAddress& addr, 307 uint16_t sink_supported_context_type, 308 uint16_t source_supported_context_type); 309 310 /** Remove Le Audio device service data */ 311 void btif_storage_leaudio_clear_service_data(const RawAddress& address); 312 313 /** Remove Le Audio device from the storage */ 314 void btif_storage_remove_leaudio(const RawAddress& address); 315 316 /** Load bonded Le Audio devices */ 317 void btif_storage_load_bonded_leaudio(void); 318 319 /** Loads information about bonded HAS devices */ 320 void btif_storage_load_bonded_leaudio_has_devices(void); 321 322 /** Deletes the bonded HAS device info from NVRAM */ 323 void btif_storage_remove_leaudio_has(const RawAddress& address); 324 325 /** Set/Unset the HAS device acceptlist flag. */ 326 void btif_storage_set_leaudio_has_acceptlist(const RawAddress& address, bool add_to_acceptlist); 327 328 /******************************************************************************* 329 * 330 * Function btif_storage_is_retricted_device 331 * 332 * Description BTIF storage API - checks if this device is a restricted 333 * device 334 * 335 * Returns true if the device is labled as restricted 336 * false otherwise 337 * 338 ******************************************************************************/ 339 bool btif_storage_is_restricted_device(const RawAddress* remote_bd_addr); 340 341 /******************************************************************************* 342 * 343 * Function btif_storage_prune_devices 344 * 345 * Description Removes restricted mode devices in non-restricted mode 346 * 347 * Returns none 348 * 349 ******************************************************************************/ 350 void btif_storage_prune_devices(); 351 352 bt_status_t btif_storage_add_ble_bonding_key(RawAddress* remote_bd_addr, const uint8_t* key, 353 uint8_t key_type, uint8_t key_length); 354 bt_status_t btif_storage_get_ble_bonding_key(const RawAddress& remote_bd_addr, uint8_t key_type, 355 uint8_t* key_value, int key_length); 356 357 bt_status_t btif_storage_add_ble_local_key(const Octet16& key, uint8_t key_type); 358 bt_status_t btif_storage_remove_ble_bonding_keys(const RawAddress* remote_bd_addr); 359 bt_status_t btif_storage_get_ble_local_key(uint8_t key_type, Octet16* key_value); 360 361 bt_status_t btif_storage_get_remote_addr_type(const RawAddress* remote_bd_addr, 362 tBLE_ADDR_TYPE* addr_type); 363 364 bt_status_t btif_storage_set_remote_addr_type(const RawAddress* remote_bd_addr, 365 tBLE_ADDR_TYPE addr_type); 366 367 void btif_storage_add_groups(const RawAddress& addr); 368 void btif_storage_load_bonded_groups(void); 369 void btif_storage_remove_groups(const RawAddress& address); 370 371 void btif_storage_update_csis_info(const RawAddress& addr); 372 void btif_storage_load_bonded_csis_devices(); 373 void btif_storage_remove_csis_device(const RawAddress& address); 374 375 /******************************************************************************* 376 * Function btif_storage_load_hidd 377 * 378 * Description Loads hidd bonded device and "plugs" it into hidd 379 * 380 * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise 381 * 382 ******************************************************************************/ 383 bt_status_t btif_storage_load_hidd(void); 384 385 /******************************************************************************* 386 * 387 * Function btif_storage_set_hidd 388 * 389 * Description Stores hidd bonded device info in nvram. 390 * 391 * Returns BT_STATUS_SUCCESS 392 * 393 ******************************************************************************/ 394 395 bt_status_t btif_storage_set_hidd(const RawAddress& remote_bd_addr); 396 397 /******************************************************************************* 398 * 399 * Function btif_storage_remove_hidd 400 * 401 * Description Removes hidd bonded device info from nvram 402 * 403 * Returns BT_STATUS_SUCCESS 404 * 405 ******************************************************************************/ 406 407 bt_status_t btif_storage_remove_hidd(RawAddress* remote_bd_addr); 408 409 // Gets the device name for a given Bluetooth address |bd_addr|. 410 // The device name (if found) is stored in |name|. 411 // Returns true if the device name is found, othervise false. 412 // Note: |name| should point to a buffer that can store string of length 413 // |BD_NAME_LEN|. 414 bool btif_storage_get_stored_remote_name(const RawAddress& bd_addr, char* name); 415 416 /******************************************************************************* 417 * 418 * Function btif_storage_get_cod 419 * 420 * Description Retrieves the stored class of device. 421 * 422 * Returns true if successful, otherwise false 423 * 424 ******************************************************************************/ 425 bool btif_storage_get_cod(const RawAddress& bd_addr, uint32_t* cod); 426 427 /******************************************************************************* 428 * 429 * Function btif_storage_set_hid_connection_policy 430 * 431 * Description Stores connection policy info in nvram. 432 * 433 * Returns BT_STATUS_SUCCESS 434 * 435 ******************************************************************************/ 436 437 bt_status_t btif_storage_set_hid_connection_policy(const tAclLinkSpec& link_spec, 438 bool reconnect_allowed); 439 440 /******************************************************************************* 441 * 442 * Function btif_storage_get_hid_connection_policy 443 * 444 * Description Get connection policy info from nvram. 445 * 446 * Returns BT_STATUS_SUCCESS 447 * 448 ******************************************************************************/ 449 450 bt_status_t btif_storage_get_hid_connection_policy(const tAclLinkSpec& link_spec, 451 bool* reconnect_allowed); 452 453 /******************************************************************************* 454 * 455 * Function btif_storage_get_services 456 * 457 * Description Retrieves the stored all services for the remote device. 458 * 459 * Returns vector of UUIDs 460 * 461 ******************************************************************************/ 462 std::vector<bluetooth::Uuid> btif_storage_get_services(const RawAddress& bd_addr, 463 tBT_TRANSPORT transport = BT_TRANSPORT_AUTO); 464 465 void btif_storage_migrate_services(); 466 /****************************************************************************** 467 * Exported for unit tests 468 *****************************************************************************/ 469 size_t btif_split_uuids_string(const char* str, bluetooth::Uuid* p_uuid, size_t max_uuids); 470 471 #endif // SYSTEM_BTIF_INCLUDE_BTIF_STORAGE_H_ 472