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 "bt_types.h"
27
28/*******************************************************************************
29 *  Constants & Macros
30 ******************************************************************************/
31#define BTIF_STORAGE_FILL_PROPERTY(p_prop, t, l, p_v) \
32  do {                                                \
33    (p_prop)->type = (t);                             \
34    (p_prop)->len = (l);                              \
35    (p_prop)->val = (p_v);                            \
36  } while (0)
37
38/*******************************************************************************
39 *  Functions
40 ******************************************************************************/
41
42/*******************************************************************************
43 *
44 * Function         btif_storage_get_adapter_property
45 *
46 * Description      BTIF storage API - Fetches the adapter property->type
47 *                  from NVRAM and fills property->val.
48 *                  Caller should provide memory for property->val and
49 *                  set the property->val
50 *
51 * Returns          BT_STATUS_SUCCESS if the fetch was successful,
52 *                  BT_STATUS_FAIL otherwise
53 *
54 ******************************************************************************/
55bt_status_t btif_storage_get_adapter_property(bt_property_t* property);
56
57/*******************************************************************************
58 *
59 * Function         btif_storage_set_adapter_property
60 *
61 * Description      BTIF storage API - Stores the adapter property
62 *                  to NVRAM
63 *
64 * Returns          BT_STATUS_SUCCESS if the store was successful,
65 *                  BT_STATUS_FAIL otherwise
66 *
67 ******************************************************************************/
68bt_status_t btif_storage_set_adapter_property(bt_property_t* property);
69
70/*******************************************************************************
71 *
72 * Function         btif_storage_get_remote_device_property
73 *
74 * Description      BTIF storage API - Fetches the remote device property->type
75 *                  from NVRAM and fills property->val.
76 *                  Caller should provide memory for property->val and
77 *                  set the property->val
78 *
79 * Returns          BT_STATUS_SUCCESS if the fetch was successful,
80 *                  BT_STATUS_FAIL otherwise
81 *
82 ******************************************************************************/
83bt_status_t btif_storage_get_remote_device_property(
84    const RawAddress* remote_bd_addr, bt_property_t* property);
85
86/*******************************************************************************
87 *
88 * Function         btif_storage_set_remote_device_property
89 *
90 * Description      BTIF storage API - Stores the remote device property
91 *                  to NVRAM
92 *
93 * Returns          BT_STATUS_SUCCESS if the store was successful,
94 *                  BT_STATUS_FAIL otherwise
95 *
96 ******************************************************************************/
97bt_status_t btif_storage_set_remote_device_property(
98    const RawAddress* remote_bd_addr, bt_property_t* property);
99
100/*******************************************************************************
101 *
102 * Function         btif_storage_add_remote_device
103 *
104 * Description      BTIF storage API - Adds a newly discovered device to
105 *                  track along with the timestamp. Also, stores the various
106 *                  properties - RSSI, BDADDR, NAME (if found in EIR)
107 *
108 * Returns          BT_STATUS_SUCCESS if successful,
109 *                  BT_STATUS_FAIL otherwise
110 *
111 ******************************************************************************/
112bt_status_t btif_storage_add_remote_device(const RawAddress* remote_bd_addr,
113                                           uint32_t num_properties,
114                                           bt_property_t* properties);
115
116/*******************************************************************************
117 *
118 * Function         btif_storage_add_bonded_device
119 *
120 * Description      BTIF storage API - Adds the newly bonded device to NVRAM
121 *                  along with the link-key, Key type and Pin key length
122 *
123 * Returns          BT_STATUS_SUCCESS if the store was successful,
124 *                  BT_STATUS_FAIL otherwise
125 *
126 ******************************************************************************/
127bt_status_t btif_storage_add_bonded_device(RawAddress* remote_bd_addr,
128                                           LINK_KEY link_key, uint8_t key_type,
129                                           uint8_t pin_length);
130
131/*******************************************************************************
132 *
133 * Function         btif_storage_remove_bonded_device
134 *
135 * Description      BTIF storage API - Deletes the bonded device from NVRAM
136 *
137 * Returns          BT_STATUS_SUCCESS if the deletion was successful,
138 *                  BT_STATUS_FAIL otherwise
139 *
140 ******************************************************************************/
141bt_status_t btif_storage_remove_bonded_device(const RawAddress* remote_bd_addr);
142
143/*******************************************************************************
144 *
145 * Function         btif_storage_remove_bonded_device
146 *
147 * Description      BTIF storage API - Deletes the bonded device from NVRAM
148 *
149 * Returns          BT_STATUS_SUCCESS if the deletion was successful,
150 *                  BT_STATUS_FAIL otherwise
151 *
152 ******************************************************************************/
153bt_status_t btif_storage_load_bonded_devices(void);
154
155/*******************************************************************************
156 *
157 * Function         btif_storage_add_hid_device_info
158 *
159 * Description      BTIF storage API - Adds the hid information of bonded hid
160 *                  devices-to NVRAM
161 *
162 * Returns          BT_STATUS_SUCCESS if the store was successful,
163 *                  BT_STATUS_FAIL otherwise
164 *
165 ******************************************************************************/
166
167bt_status_t btif_storage_add_hid_device_info(
168    RawAddress* remote_bd_addr, uint16_t attr_mask, uint8_t sub_class,
169    uint8_t app_id, uint16_t vendor_id, uint16_t product_id, uint16_t version,
170    uint8_t ctry_code, uint16_t ssr_max_latency, uint16_t ssr_min_tout,
171    uint16_t dl_len, uint8_t* dsc_list);
172
173/*******************************************************************************
174 *
175 * Function         btif_storage_load_bonded_hid_info
176 *
177 * Description      BTIF storage API - Loads hid info for all the bonded devices
178 *                  from NVRAM and adds those devices  to the BTA_HH.
179 *
180 * Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
181 *
182 ******************************************************************************/
183bt_status_t btif_storage_load_bonded_hid_info(void);
184
185/*******************************************************************************
186 *
187 * Function         btif_storage_remove_hid_info
188 *
189 * Description      BTIF storage API - Deletes the bonded hid device info from
190 *                  NVRAM
191 *
192 * Returns          BT_STATUS_SUCCESS if the deletion was successful,
193 *                  BT_STATUS_FAIL otherwise
194 *
195 ******************************************************************************/
196bt_status_t btif_storage_remove_hid_info(RawAddress* remote_bd_addr);
197
198/** Loads information about bonded hearing aid devices */
199void btif_storage_load_bonded_hearing_aids();
200
201/** Deletes the bonded hearing aid device info from NVRAM */
202void btif_storage_remove_hearing_aid(const RawAddress& address);
203
204/** Remove the hearing aid device from white list */
205void btif_storage_remove_hearing_aid_white_list(const RawAddress& address);
206
207/*******************************************************************************
208 *
209 * Function         btif_storage_is_retricted_device
210 *
211 * Description      BTIF storage API - checks if this device is a restricted
212 *                  device
213 *
214 * Returns          true  if the device is labled as restricted
215 *                  false otherwise
216 *
217 ******************************************************************************/
218bool btif_storage_is_restricted_device(const RawAddress* remote_bd_addr);
219
220bt_status_t btif_storage_add_ble_bonding_key(RawAddress* remote_bd_addr,
221                                             const uint8_t* key,
222                                             uint8_t key_type,
223                                             uint8_t key_length);
224bt_status_t btif_storage_get_ble_bonding_key(RawAddress* remote_bd_addr,
225                                             uint8_t key_type,
226                                             uint8_t* key_value,
227                                             int key_length);
228
229bt_status_t btif_storage_add_ble_local_key(char* key, uint8_t key_type,
230                                           uint8_t key_length);
231bt_status_t btif_storage_remove_ble_bonding_keys(
232    const RawAddress* remote_bd_addr);
233bt_status_t btif_storage_remove_ble_local_keys(void);
234bt_status_t btif_storage_get_ble_local_key(uint8_t key_type, char* key_value,
235                                           int key_len);
236
237bt_status_t btif_storage_get_remote_addr_type(const RawAddress* remote_bd_addr,
238                                              int* addr_type);
239
240bt_status_t btif_storage_set_remote_addr_type(const RawAddress* remote_bd_addr,
241                                              uint8_t addr_type);
242
243/*******************************************************************************
244 * Function         btif_storage_load_hidd
245 *
246 * Description      Loads hidd bonded device and "plugs" it into hidd
247 *
248 * Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
249 *
250 ******************************************************************************/
251bt_status_t btif_storage_load_hidd(void);
252
253/*******************************************************************************
254 *
255 * Function         btif_storage_set_hidd
256 *
257 * Description      Stores hidd bonded device info in nvram.
258 *
259 * Returns          BT_STATUS_SUCCESS
260 *
261 ******************************************************************************/
262
263bt_status_t btif_storage_set_hidd(RawAddress* remote_bd_addr);
264
265/*******************************************************************************
266 *
267 * Function         btif_storage_remove_hidd
268 *
269 * Description      Removes hidd bonded device info from nvram
270 *
271 * Returns          BT_STATUS_SUCCESS
272 *
273 ******************************************************************************/
274
275bt_status_t btif_storage_remove_hidd(RawAddress* remote_bd_addr);
276
277// Gets the device name for a given Bluetooth address |bd_addr|.
278// The device name (if found) is stored in |name|.
279// Returns true if the device name is found, othervise false.
280// Note: |name| should point to a buffer that can store string of length
281// |BTM_MAX_REM_BD_NAME_LEN|.
282bool btif_storage_get_stored_remote_name(const RawAddress& bd_addr, char* name);
283
284/******************************************************************************
285 * Exported for unit tests
286 *****************************************************************************/
287size_t btif_split_uuids_string(const char* str, bluetooth::Uuid* p_uuid,
288                               size_t max_uuids);
289
290#endif /* BTIF_STORAGE_H */
291