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