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 BTIF_STORAGE_H
20 #define BTIF_STORAGE_H
21 
22 #include <bluetooth/uuid.h>
23 #include <hardware/bluetooth.h>
24 
25 #include "internal_include/bt_target.h"
26 #include "stack/include/bt_device_type.h"
27 #include "stack/include/bt_octets.h"
28 #include "stack/include/btm_sec_api_types.h"
29 #include "types/ble_address_with_type.h"
30 #include "types/raw_address.h"
31 
32 /*******************************************************************************
33  *  Constants & Macros
34  ******************************************************************************/
35 #define BTIF_STORAGE_FILL_PROPERTY(p_prop, t, l, p_v) \
36   do {                                                \
37     (p_prop)->type = (t);                             \
38     (p_prop)->len = (l);                              \
39     (p_prop)->val = (p_v);                            \
40   } while (0)
41 
42 #define STORAGE_BDADDR_STRING_SZ (18) /* 00:11:22:33:44:55 */
43 #define STORAGE_UUID_STRING_SIZE \
44   (36 + 1) /* 00001200-0000-1000-8000-00805f9b34fb; */
45 #define STORAGE_PINLEN_STRING_MAX_SIZE (2)  /* ascii pinlen max chars */
46 #define STORAGE_KEYTYPE_STRING_MAX_SIZE (1) /* ascii keytype max chars */
47 
48 #define STORAGE_KEY_TYPE_MAX (10)
49 
50 /*******************************************************************************
51  *  Functions
52  ******************************************************************************/
53 
54 /*******************************************************************************
55  *
56  * Function         btif_in_fetch_bonded_devices
57  *
58  * Description      Helper function to fetch the bonded devices
59  *                  from NVRAM
60  *
61  * Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
62  *
63  ******************************************************************************/
64 bt_status_t btif_in_fetch_bonded_device(const std::string& bdstr);
65 
66 typedef struct {
67   uint32_t num_devices;
68   RawAddress devices[BTM_SEC_MAX_DEVICE_RECORDS];
69 } btif_bonded_devices_t;
70 
71 bt_status_t btif_in_fetch_bonded_ble_device(
72     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(
117     const RawAddress* remote_bd_addr, 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(
131     const RawAddress* remote_bd_addr, 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,
147                                            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,
161                                            LinkKey link_key, uint8_t key_type,
162                                            uint8_t pin_length);
163 
164 /*******************************************************************************
165  *
166  * Function         btif_storage_remove_bonded_device
167  *
168  * Description      BTIF storage API - Deletes the bonded device from NVRAM
169  *
170  * Returns          BT_STATUS_SUCCESS if the deletion was successful,
171  *                  BT_STATUS_FAIL otherwise
172  *
173  ******************************************************************************/
174 bt_status_t btif_storage_remove_bonded_device(const RawAddress* remote_bd_addr);
175 
176 /*******************************************************************************
177  *
178  * Function         btif_storage_load_le_devices
179  *
180  * Description      BTIF storage API - Loads all LE-only and Dual Mode devices
181  *                  from NVRAM. This API invokes the adaper_properties_cb.
182  *                  It also invokes invoke_address_consolidate_cb
183  *                  to consolidate each Dual Mode device and
184  *                  invoke_le_address_associate_cb to associate each LE-only
185  *                  device between its RPA and identity address.
186  *
187  ******************************************************************************/
188 void btif_storage_load_le_devices(void);
189 
190 /*******************************************************************************
191  *
192  * Function         btif_storage_load_bonded_devices
193  *
194  * Description      BTIF storage API - Loads all the bonded devices from NVRAM
195  *                  and adds to the BTA.
196  *                  Additionally, this API also invokes the adaper_properties_cb
197  *                  and remote_device_properties_cb for each of the bonded
198  *                  devices.
199  *
200  * Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
201  *
202  ******************************************************************************/
203 bt_status_t btif_storage_load_bonded_devices(void);
204 
205 /*******************************************************************************
206  *
207  * Function         btif_storage_add_hid_device_info
208  *
209  * Description      BTIF storage API - Adds the hid information of bonded hid
210  *                  devices-to NVRAM
211  *
212  * Returns          BT_STATUS_SUCCESS if the store was successful,
213  *                  BT_STATUS_FAIL otherwise
214  *
215  ******************************************************************************/
216 
217 bt_status_t btif_storage_add_hid_device_info(
218     const tAclLinkSpec& link_spec, uint16_t attr_mask, uint8_t sub_class,
219     uint8_t app_id, uint16_t vendor_id, uint16_t product_id, uint16_t version,
220     uint8_t ctry_code, uint16_t ssr_max_latency, uint16_t ssr_min_tout,
221     uint16_t dl_len, 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,
256                                              bool add_to_acceptlist);
257 
258 /** Stores information about GATT Client supported features support */
259 void btif_storage_set_gatt_cl_supp_feat(const RawAddress& bd_addr,
260                                         uint8_t feat);
261 
262 /** Get client supported features */
263 uint8_t btif_storage_get_gatt_cl_supp_feat(const RawAddress& bd_addr);
264 
265 /** Remove client supported features */
266 void btif_storage_remove_gatt_cl_supp_feat(const RawAddress& bd_addr);
267 
268 /** Stores information about GATT server supported features */
269 void btif_storage_set_gatt_sr_supp_feat(const RawAddress& addr, uint8_t feat);
270 
271 /** Gets information about GATT server supported features */
272 uint8_t btif_storage_get_sr_supp_feat(const RawAddress& bd_addr);
273 
274 /** Store last server database hash for remote client */
275 void btif_storage_set_gatt_cl_db_hash(const RawAddress& bd_addr, Octet16 hash);
276 
277 /** Get last server database hash for remote client */
278 Octet16 btif_storage_get_gatt_cl_db_hash(const RawAddress& bd_addr);
279 
280 /** Remove last server database hash for remote client */
281 void btif_storage_remove_gatt_cl_db_hash(const RawAddress& bd_addr);
282 
283 /** Get the hearing aid device properties. */
284 bool btif_storage_get_hearing_aid_prop(
285     const RawAddress& address, uint8_t* capabilities, uint64_t* hi_sync_id,
286     uint16_t* render_delay, uint16_t* preparation_delay, uint16_t* codecs);
287 
288 /** Store Le Audio device autoconnect flag */
289 void btif_storage_set_leaudio_autoconnect(const RawAddress& addr,
290                                           bool autoconnect);
291 
292 /** Store PACs information */
293 void btif_storage_leaudio_update_pacs_bin(const RawAddress& addr);
294 
295 /** Store ASEs information */
296 void btif_storage_leaudio_update_ase_bin(const RawAddress& addr);
297 
298 /** Store Handles information */
299 void btif_storage_leaudio_update_handles_bin(const RawAddress& addr);
300 
301 /** Store Le Audio device audio locations */
302 void btif_storage_set_leaudio_audio_location(const RawAddress& addr,
303                                              uint32_t sink_location,
304                                              uint32_t source_location);
305 
306 /** Store Le Audio device context types */
307 void btif_storage_set_leaudio_supported_context_types(
308     const RawAddress& addr, uint16_t sink_supported_context_type,
309     uint16_t source_supported_context_type);
310 
311 /** Remove Le Audio device service data */
312 void btif_storage_leaudio_clear_service_data(const RawAddress& address);
313 
314 /** Remove Le Audio device from the storage */
315 void btif_storage_remove_leaudio(const RawAddress& address);
316 
317 /** Load bonded Le Audio devices */
318 void btif_storage_load_bonded_leaudio(void);
319 
320 /** Loads information about bonded HAS devices */
321 void btif_storage_load_bonded_leaudio_has_devices(void);
322 
323 /** Deletes the bonded HAS device info from NVRAM */
324 void btif_storage_remove_leaudio_has(const RawAddress& address);
325 
326 /** Set/Unset the HAS device acceptlist flag. */
327 void btif_storage_set_leaudio_has_acceptlist(const RawAddress& address,
328                                              bool add_to_acceptlist);
329 
330 /*******************************************************************************
331  *
332  * Function         btif_storage_is_retricted_device
333  *
334  * Description      BTIF storage API - checks if this device is a restricted
335  *                  device
336  *
337  * Returns          true  if the device is labled as restricted
338  *                  false otherwise
339  *
340  ******************************************************************************/
341 bool btif_storage_is_restricted_device(const RawAddress* remote_bd_addr);
342 
343 bt_status_t btif_storage_add_ble_bonding_key(RawAddress* remote_bd_addr,
344                                              const uint8_t* key,
345                                              uint8_t key_type,
346                                              uint8_t key_length);
347 bt_status_t btif_storage_get_ble_bonding_key(const RawAddress& remote_bd_addr,
348                                              uint8_t key_type,
349                                              uint8_t* key_value,
350                                              int key_length);
351 
352 bt_status_t btif_storage_add_ble_local_key(const Octet16& key,
353                                            uint8_t key_type);
354 bt_status_t btif_storage_remove_ble_bonding_keys(
355     const RawAddress* remote_bd_addr);
356 bt_status_t btif_storage_get_ble_local_key(uint8_t key_type,
357                                            Octet16* key_value);
358 
359 bt_status_t btif_storage_get_remote_addr_type(const RawAddress* remote_bd_addr,
360                                               tBLE_ADDR_TYPE* addr_type);
361 
362 bt_status_t btif_storage_set_remote_addr_type(const RawAddress* remote_bd_addr,
363                                               tBLE_ADDR_TYPE addr_type);
364 
365 void btif_storage_add_groups(const RawAddress& addr);
366 void btif_storage_load_bonded_groups(void);
367 void btif_storage_remove_groups(const RawAddress& address);
368 
369 void btif_storage_update_csis_info(const RawAddress& addr);
370 void btif_storage_load_bonded_csis_devices();
371 void btif_storage_remove_csis_device(const RawAddress& address);
372 
373 /*******************************************************************************
374  * Function         btif_storage_load_hidd
375  *
376  * Description      Loads hidd bonded device and "plugs" it into hidd
377  *
378  * Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
379  *
380  ******************************************************************************/
381 bt_status_t btif_storage_load_hidd(void);
382 
383 /*******************************************************************************
384  *
385  * Function         btif_storage_set_hidd
386  *
387  * Description      Stores hidd bonded device info in nvram.
388  *
389  * Returns          BT_STATUS_SUCCESS
390  *
391  ******************************************************************************/
392 
393 bt_status_t btif_storage_set_hidd(const RawAddress& remote_bd_addr);
394 
395 /*******************************************************************************
396  *
397  * Function         btif_storage_remove_hidd
398  *
399  * Description      Removes hidd bonded device info from nvram
400  *
401  * Returns          BT_STATUS_SUCCESS
402  *
403  ******************************************************************************/
404 
405 bt_status_t btif_storage_remove_hidd(RawAddress* remote_bd_addr);
406 
407 // Gets the device name for a given Bluetooth address |bd_addr|.
408 // The device name (if found) is stored in |name|.
409 // Returns true if the device name is found, othervise false.
410 // Note: |name| should point to a buffer that can store string of length
411 // |BD_NAME_LEN|.
412 bool btif_storage_get_stored_remote_name(const RawAddress& bd_addr, char* name);
413 
414 /*******************************************************************************
415  *
416  * Function         btif_storage_set_hid_connection_policy
417  *
418  * Description      Stores connection policy info in nvram.
419  *
420  * Returns          BT_STATUS_SUCCESS
421  *
422  ******************************************************************************/
423 
424 bt_status_t btif_storage_set_hid_connection_policy(
425     const tAclLinkSpec& link_spec, bool reconnect_allowed);
426 
427 /*******************************************************************************
428  *
429  * Function         btif_storage_get_hid_connection_policy
430  *
431  * Description      Get connection policy info from nvram.
432  *
433  * Returns          BT_STATUS_SUCCESS
434  *
435  ******************************************************************************/
436 
437 bt_status_t btif_storage_get_hid_connection_policy(
438     const tAclLinkSpec& link_spec, bool* reconnect_allowed);
439 
440 /******************************************************************************
441  * Exported for unit tests
442  *****************************************************************************/
443 size_t btif_split_uuids_string(const char* str, bluetooth::Uuid* p_uuid,
444                                size_t max_uuids);
445 
446 #endif /* BTIF_STORAGE_H */
447