1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <zephyr/bluetooth/addr.h>
11 
12 #include <zephyr/settings/settings.h>
13 
14 /* Max settings key length (with all components) */
15 #define BT_SETTINGS_KEY_MAX 36
16 
17 /* Base64-encoded string buffer size of in_size bytes */
18 #define BT_SETTINGS_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
19 
20 #define BT_SETTINGS_DEFINE(_hname, _subtree, _set, _commit)                                        \
21 	SETTINGS_STATIC_HANDLER_DEFINE(bt_##_hname, "bt/" _subtree, NULL, _set, _commit, NULL)
22 
23 #define ID_DATA_LEN(array) (bt_dev.id_count * sizeof(array[0]))
24 
25 int bt_settings_store(const char *key, uint8_t id, const bt_addr_le_t *addr, const void *value,
26 		      size_t val_len);
27 int bt_settings_delete(const char *key, uint8_t id, const bt_addr_le_t *addr);
28 
29 void bt_testing_settings_store_hook(const char *key, const void *value, size_t val_len);
30 void bt_testing_settings_delete_hook(const char *key);
31 
32 /* Helpers for keys containing a bdaddr */
33 void bt_settings_encode_key(char *path, size_t path_size, const char *subsys,
34 			    const bt_addr_le_t *addr, const char *key);
35 int bt_settings_decode_key(const char *key, bt_addr_le_t *addr);
36 
37 void bt_settings_save_id(void);
38 
39 int bt_settings_init(void);
40 
41 int bt_settings_store_sc(uint8_t id, const bt_addr_le_t *addr, const void *value, size_t val_len);
42 int bt_settings_delete_sc(uint8_t id, const bt_addr_le_t *addr);
43 
44 int bt_settings_store_cf(uint8_t id, const bt_addr_le_t *addr, const void *value, size_t val_len);
45 int bt_settings_delete_cf(uint8_t id, const bt_addr_le_t *addr);
46 
47 int bt_settings_store_ccc(uint8_t id, const bt_addr_le_t *addr, const void *value, size_t val_len);
48 int bt_settings_delete_ccc(uint8_t id, const bt_addr_le_t *addr);
49 
50 int bt_settings_store_hash(const void *value, size_t val_len);
51 int bt_settings_delete_hash(void);
52 
53 int bt_settings_store_name(const void *value, size_t val_len);
54 int bt_settings_delete_name(void);
55 
56 int bt_settings_store_appearance(const void *value, size_t val_len);
57 int bt_settings_delete_appearance(void);
58 
59 int bt_settings_store_id(void);
60 int bt_settings_delete_id(void);
61 
62 int bt_settings_store_irk(void);
63 int bt_settings_delete_irk(void);
64 
65 int bt_settings_store_link_key(const bt_addr_le_t *addr, const void *value, size_t val_len);
66 int bt_settings_delete_link_key(const bt_addr_le_t *addr);
67 
68 int bt_settings_store_keys(uint8_t id, const bt_addr_le_t *addr, const void *value, size_t val_len);
69 int bt_settings_delete_keys(uint8_t id, const bt_addr_le_t *addr);
70