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