• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bt_target.h"
26 #include "stack/include/bt_device_type.h"
27 #include "stack/include/bt_octets.h"
28 #include "types/ble_address_with_type.h"
29 #include "types/raw_address.h"
30 
31 /*******************************************************************************
32  *  Constants & Macros
33  ******************************************************************************/
34 #define BTIF_STORAGE_FILL_PROPERTY(p_prop, t, l, p_v) \
35   do {                                                \
36     (p_prop)->type = (t);                             \
37     (p_prop)->len = (l);                              \
38     (p_prop)->val = (p_v);                            \
39   } while (0)
40 
41 #define BTIF_STORAGE_PATH_REMOTE_SERVICE "Service"
42 
43 #define STORAGE_BDADDR_STRING_SZ (18) /* 00:11:22:33:44:55 */
44 #define STORAGE_UUID_STRING_SIZE \
45   (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(
73     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(
118     const RawAddress* remote_bd_addr, 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(
132     const RawAddress* remote_bd_addr, bt_property_t* property);
133 
134 /*******************************************************************************
135  *
136  * Function         btif_storage_get_io_caps
137  *
138  * Description      BTIF storage API - Fetches the local Input/Output
139  *                  capabilities of the device.
140  *
141  * Returns          Returns local IO Capability of device. If not stored,
142  *                  returns BTM_LOCAL_IO_CAPS.
143  *
144  ******************************************************************************/
145 uint8_t btif_storage_get_local_io_caps();
146 
147 /*******************************************************************************
148  *
149  * Function         btif_storage_add_remote_device
150  *
151  * Description      BTIF storage API - Adds a newly discovered device to
152  *                  track along with the timestamp. Also, stores the various
153  *                  properties - RSSI, BDADDR, NAME (if found in EIR)
154  *
155  * Returns          BT_STATUS_SUCCESS if successful,
156  *                  BT_STATUS_FAIL otherwise
157  *
158  ******************************************************************************/
159 bt_status_t btif_storage_add_remote_device(const RawAddress* remote_bd_addr,
160                                            uint32_t num_properties,
161                                            bt_property_t* properties);
162 
163 /*******************************************************************************
164  *
165  * Function         btif_storage_add_bonded_device
166  *
167  * Description      BTIF storage API - Adds the newly bonded device to NVRAM
168  *                  along with the link-key, Key type and Pin key length
169  *
170  * Returns          BT_STATUS_SUCCESS if the store was successful,
171  *                  BT_STATUS_FAIL otherwise
172  *
173  ******************************************************************************/
174 bt_status_t btif_storage_add_bonded_device(RawAddress* remote_bd_addr,
175                                            LinkKey link_key, uint8_t key_type,
176                                            uint8_t pin_length);
177 
178 /*******************************************************************************
179  *
180  * Function         btif_storage_remove_bonded_device
181  *
182  * Description      BTIF storage API - Deletes the bonded device from NVRAM
183  *
184  * Returns          BT_STATUS_SUCCESS if the deletion was successful,
185  *                  BT_STATUS_FAIL otherwise
186  *
187  ******************************************************************************/
188 bt_status_t btif_storage_remove_bonded_device(const RawAddress* remote_bd_addr);
189 
190 /*******************************************************************************
191  *
192  * Function         btif_storage_load_le_devices
193  *
194  * Description      BTIF storage API - Loads all LE-only and Dual Mode devices
195  *                  from NVRAM. This API invokes the adaper_properties_cb.
196  *                  It also invokes invoke_address_consolidate_cb
197  *                  to consolidate each Dual Mode device and
198  *                  invoke_le_address_associate_cb to associate each LE-only
199  *                  device between its RPA and identity address.
200  *
201  ******************************************************************************/
202 void btif_storage_load_le_devices(void);
203 
204 /*******************************************************************************
205  *
206  * Function         btif_storage_load_bonded_devices
207  *
208  * Description      BTIF storage API - Loads all the bonded devices from NVRAM
209  *                  and adds to the BTA.
210  *                  Additionally, this API also invokes the adaper_properties_cb
211  *                  and remote_device_properties_cb for each of the bonded
212  *                  devices.
213  *
214  * Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
215  *
216  ******************************************************************************/
217 bt_status_t btif_storage_load_bonded_devices(void);
218 
219 /*******************************************************************************
220  *
221  * Function         btif_storage_add_hid_device_info
222  *
223  * Description      BTIF storage API - Adds the hid information of bonded hid
224  *                  devices-to NVRAM
225  *
226  * Returns          BT_STATUS_SUCCESS if the store was successful,
227  *                  BT_STATUS_FAIL otherwise
228  *
229  ******************************************************************************/
230 
231 bt_status_t btif_storage_add_hid_device_info(
232     RawAddress* remote_bd_addr, uint16_t attr_mask, uint8_t sub_class,
233     uint8_t app_id, uint16_t vendor_id, uint16_t product_id, uint16_t version,
234     uint8_t ctry_code, uint16_t ssr_max_latency, uint16_t ssr_min_tout,
235     uint16_t dl_len, uint8_t* dsc_list);
236 
237 /*******************************************************************************
238  *
239  * Function         btif_storage_load_bonded_hid_info
240  *
241  * Description      BTIF storage API - Loads hid info for all the bonded devices
242  *                  from NVRAM and adds those devices  to the BTA_HH.
243  *
244  * Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
245  *
246  ******************************************************************************/
247 bt_status_t btif_storage_load_bonded_hid_info(void);
248 
249 /*******************************************************************************
250  *
251  * Function         btif_storage_remove_hid_info
252  *
253  * Description      BTIF storage API - Deletes the bonded hid device info from
254  *                  NVRAM
255  *
256  * Returns          BT_STATUS_SUCCESS if the deletion was successful,
257  *                  BT_STATUS_FAIL otherwise
258  *
259  ******************************************************************************/
260 bt_status_t btif_storage_remove_hid_info(const RawAddress& remote_bd_addr);
261 
262 /** Loads information about bonded hearing aid devices */
263 void btif_storage_load_bonded_hearing_aids();
264 
265 /** Deletes the bonded hearing aid device info from NVRAM */
266 void btif_storage_remove_hearing_aid(const RawAddress& address);
267 
268 /** Set/Unset the hearing aid device HEARING_AID_IS_ACCEPTLISTED flag. */
269 void btif_storage_set_hearing_aid_acceptlist(const RawAddress& address,
270                                              bool add_to_acceptlist);
271 
272 /** Stores information about GATT Client supported features support */
273 void btif_storage_set_gatt_cl_supp_feat(const RawAddress& bd_addr,
274                                         uint8_t feat);
275 
276 /** Get client supported features */
277 uint8_t btif_storage_get_gatt_cl_supp_feat(const RawAddress& bd_addr);
278 
279 /** Remove client supported features */
280 void btif_storage_remove_gatt_cl_supp_feat(const RawAddress& bd_addr);
281 
282 /** Stores information about GATT server supported features */
283 void btif_storage_set_gatt_sr_supp_feat(const RawAddress& addr, uint8_t feat);
284 
285 /** Gets information about GATT server supported features */
286 uint8_t btif_storage_get_sr_supp_feat(const RawAddress& bd_addr);
287 
288 /** Store last server database hash for remote client */
289 void btif_storage_set_gatt_cl_db_hash(const RawAddress& bd_addr, Octet16 hash);
290 
291 /** Get last server database hash for remote client */
292 Octet16 btif_storage_get_gatt_cl_db_hash(const RawAddress& bd_addr);
293 
294 /** Remove last server database hash for remote client */
295 void btif_storage_remove_gatt_cl_db_hash(const RawAddress& bd_addr);
296 
297 /** Get the hearing aid device properties. */
298 bool btif_storage_get_hearing_aid_prop(
299     const RawAddress& address, uint8_t* capabilities, uint64_t* hi_sync_id,
300     uint16_t* render_delay, uint16_t* preparation_delay, uint16_t* codecs);
301 
302 /** Store Le Audio device autoconnect flag */
303 void btif_storage_set_leaudio_autoconnect(const RawAddress& addr,
304                                           bool autoconnect);
305 
306 /** Store PACs information */
307 void btif_storage_leaudio_update_pacs_bin(const RawAddress& addr);
308 
309 /** Store ASEs information */
310 void btif_storage_leaudio_update_ase_bin(const RawAddress& addr);
311 
312 /** Store Handles information */
313 void btif_storage_leaudio_update_handles_bin(const RawAddress& addr);
314 
315 /** Store Le Audio device audio locations */
316 void btif_storage_set_leaudio_audio_location(const RawAddress& addr,
317                                              uint32_t sink_location,
318                                              uint32_t source_location);
319 
320 /** Store Le Audio device context types */
321 void btif_storage_set_leaudio_supported_context_types(
322     const RawAddress& addr, uint16_t sink_supported_context_type,
323     uint16_t source_supported_context_type);
324 
325 /** Remove Le Audio device from the storage */
326 void btif_storage_remove_leaudio(const RawAddress& address);
327 
328 /** Load bonded Le Audio devices */
329 void btif_storage_load_bonded_leaudio(void);
330 
331 /** Loads information about bonded HAS devices */
332 void btif_storage_load_bonded_leaudio_has_devices(void);
333 
334 /** Deletes the bonded HAS device info from NVRAM */
335 void btif_storage_remove_leaudio_has(const RawAddress& address);
336 
337 /** Set/Unset the HAS device acceptlist flag. */
338 void btif_storage_set_leaudio_has_acceptlist(const RawAddress& address,
339                                              bool add_to_acceptlist);
340 
341 /*******************************************************************************
342  *
343  * Function         btif_storage_is_retricted_device
344  *
345  * Description      BTIF storage API - checks if this device is a restricted
346  *                  device
347  *
348  * Returns          true  if the device is labled as restricted
349  *                  false otherwise
350  *
351  ******************************************************************************/
352 bool btif_storage_is_restricted_device(const RawAddress* remote_bd_addr);
353 
354 int btif_storage_get_num_bonded_devices(void);
355 
356 bt_status_t btif_storage_add_ble_bonding_key(RawAddress* remote_bd_addr,
357                                              const uint8_t* key,
358                                              uint8_t key_type,
359                                              uint8_t key_length);
360 bt_status_t btif_storage_get_ble_bonding_key(const RawAddress& remote_bd_addr,
361                                              uint8_t key_type,
362                                              uint8_t* key_value,
363                                              int key_length);
364 
365 bt_status_t btif_storage_add_ble_local_key(const Octet16& key,
366                                            uint8_t key_type);
367 bt_status_t btif_storage_remove_ble_bonding_keys(
368     const RawAddress* remote_bd_addr);
369 bt_status_t btif_storage_remove_ble_local_keys(void);
370 bt_status_t btif_storage_get_ble_local_key(uint8_t key_type,
371                                            Octet16* key_value);
372 
373 bt_status_t btif_storage_get_remote_addr_type(const RawAddress* remote_bd_addr,
374                                               tBLE_ADDR_TYPE* addr_type);
375 
376 bt_status_t btif_storage_set_remote_addr_type(const RawAddress* remote_bd_addr,
377                                               tBLE_ADDR_TYPE addr_type);
378 
379 bool btif_storage_get_remote_addr_type(const RawAddress& remote_bd_addr,
380                                        tBLE_ADDR_TYPE& addr_type);
381 void btif_storage_set_remote_addr_type(const RawAddress& remote_bd_addr,
382                                        const tBLE_ADDR_TYPE& addr_type);
383 bool btif_storage_get_remote_device_type(const RawAddress& remote_bd_addr,
384                                          tBT_DEVICE_TYPE& device_type);
385 void btif_storage_set_remote_device_type(const RawAddress& remote_bd_addr,
386                                          const tBT_DEVICE_TYPE& device_type);
387 
388 void btif_storage_add_groups(const RawAddress& addr);
389 void btif_storage_load_bonded_groups(void);
390 void btif_storage_remove_groups(const RawAddress& address);
391 
392 void btif_storage_set_csis_autoconnect(const RawAddress& addr,
393                                        bool autoconnect);
394 void btif_storage_update_csis_info(const RawAddress& addr);
395 void btif_storage_load_bonded_csis_devices();
396 void btif_storage_remove_csis_device(const RawAddress& address);
397 
398 /*******************************************************************************
399  * Function         btif_storage_load_hidd
400  *
401  * Description      Loads hidd bonded device and "plugs" it into hidd
402  *
403  * Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
404  *
405  ******************************************************************************/
406 bt_status_t btif_storage_load_hidd(void);
407 
408 /*******************************************************************************
409  *
410  * Function         btif_storage_set_hidd
411  *
412  * Description      Stores hidd bonded device info in nvram.
413  *
414  * Returns          BT_STATUS_SUCCESS
415  *
416  ******************************************************************************/
417 
418 bt_status_t btif_storage_set_hidd(const RawAddress& remote_bd_addr);
419 
420 /*******************************************************************************
421  *
422  * Function         btif_storage_remove_hidd
423  *
424  * Description      Removes hidd bonded device info from nvram
425  *
426  * Returns          BT_STATUS_SUCCESS
427  *
428  ******************************************************************************/
429 
430 bt_status_t btif_storage_remove_hidd(RawAddress* remote_bd_addr);
431 
432 // Gets the device name for a given Bluetooth address |bd_addr|.
433 // The device name (if found) is stored in |name|.
434 // Returns true if the device name is found, othervise false.
435 // Note: |name| should point to a buffer that can store string of length
436 // |BTM_MAX_REM_BD_NAME_LEN|.
437 bool btif_storage_get_stored_remote_name(const RawAddress& bd_addr, char* name);
438 
439 /******************************************************************************
440  * Exported for unit tests
441  *****************************************************************************/
442 size_t btif_split_uuids_string(const char* str, bluetooth::Uuid* p_uuid,
443                                size_t max_uuids);
444 
445 #endif /* BTIF_STORAGE_H */
446