1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <bt_errno.h>
8 
9 #include <ble_os.h>
10 #include <settings/settings.h>
11 
12 #include <bluetooth/bluetooth.h>
13 #include <bluetooth/conn.h>
14 
15 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_SETTINGS)
16 #define LOG_MODULE_NAME bt_settings
17 #include "common/log.h"
18 
19 #include "hci_core.h"
20 #include "settings.h"
21 
22 #if defined(CONFIG_BT_SETTINGS_USE_PRINTK)
bt_settings_encode_key(char * path,size_t path_size,const char * subsys,const bt_addr_le_t * addr,const char * key)23 void bt_settings_encode_key(char *path, size_t path_size, const char *subsys,
24 			    const bt_addr_le_t *addr, const char *key)
25 {
26 	if (key) {
27 		snprintf(path, path_size,
28 			 "bt/%s/%02x%02x%02x%02x%02x%02x%u/%s", subsys,
29 			 addr->a.val[5], addr->a.val[4], addr->a.val[3],
30 			 addr->a.val[2], addr->a.val[1], addr->a.val[0],
31 			 addr->type, key);
32 	} else {
33 		snprintf(path, path_size,
34 			 "bt/%s/%02x%02x%02x%02x%02x%02x%u", subsys,
35 			 addr->a.val[5], addr->a.val[4], addr->a.val[3],
36 			 addr->a.val[2], addr->a.val[1], addr->a.val[0],
37 			 addr->type);
38 	}
39 
40 	BT_DBG("Encoded path %s", log_strdup(path));
41 }
42 #else
bt_settings_encode_key(char * path,size_t path_size,const char * subsys,const bt_addr_le_t * addr,const char * key)43 void bt_settings_encode_key(char *path, size_t path_size, const char *subsys,
44 			    const bt_addr_le_t *addr, const char *key)
45 {
46 	size_t len = 3;
47 
48 	/* Skip if path_size is less than 3; strlen("bt/") */
49 	if (len < path_size) {
50 		/* Key format:
51 		 *  "bt/<subsys>/<addr><type>/<key>", "/<key>" is optional
52 		 */
53 		strcpy(path, "bt/");
54 		strncpy(&path[len], subsys, path_size - len);
55 		len = strlen(path);
56 		if (len < path_size) {
57 			path[len] = '/';
58 			len++;
59 		}
60 
61 		for (s8_t i = 5; i >= 0 && len < path_size; i--) {
62 			len += bin2hex(&addr->a.val[i], 1, &path[len],
63 				       path_size - len);
64 		}
65 
66 		if (len < path_size) {
67 			/* Type can be either BT_ADDR_LE_PUBLIC or
68 			 * BT_ADDR_LE_RANDOM (value 0 or 1)
69 			 */
70 			path[len] = '0' + addr->type;
71 			len++;
72 		}
73 
74 		if (key && len < path_size) {
75 			path[len] = '/';
76 			len++;
77 			strncpy(&path[len], key, path_size - len);
78 			len += strlen(&path[len]);
79 		}
80 
81 		if (len >= path_size) {
82 			/* Truncate string */
83 			path[path_size - 1] = '\0';
84 		}
85 	} else if (path_size > 0) {
86 		*path = '\0';
87 	}
88 
89 	BT_DBG("Encoded path %s", log_strdup(path));
90 }
91 #endif
92 
bt_settings_decode_key(const char * key,bt_addr_le_t * addr)93 int bt_settings_decode_key(const char *key, bt_addr_le_t *addr)
94 {
95 	if (settings_name_next(key, NULL) != 13) {
96 		return -EINVAL;
97 	}
98 
99 	if (key[12] == '0') {
100 		addr->type = BT_ADDR_LE_PUBLIC;
101 	} else if (key[12] == '1') {
102 		addr->type = BT_ADDR_LE_RANDOM;
103 	} else {
104 		return -EINVAL;
105 	}
106 
107 	for (u8_t i = 0; i < 6; i++) {
108 		hex2bin(&key[i * 2], 2, &addr->a.val[5 - i], 1);
109 	}
110 
111 	BT_DBG("Decoded %s as %s", log_strdup(key), bt_addr_le_str(addr));
112 
113 	return 0;
114 }
115 
set(const char * name,size_t len_rd,settings_read_cb read_cb,void * cb_arg)116 static int set(const char *name, size_t len_rd, settings_read_cb read_cb,
117 	       void *cb_arg)
118 {
119 	ssize_t len;
120 	const char *next;
121 
122 	if (!name) {
123 		BT_ERR("Insufficient number of arguments");
124 		return -ENOENT;
125 	}
126 
127 	len = settings_name_next(name, &next);
128 
129 	if (!strncmp(name, "id", len)) {
130 		/* Any previously provided identities supersede flash */
131 		if (atomic_test_bit(bt_dev.flags, BT_DEV_PRESET_ID)) {
132 			BT_WARN("Ignoring identities stored in flash");
133 			return 0;
134 		}
135 
136 		len = read_cb(cb_arg, &bt_dev.id_addr, sizeof(bt_dev.id_addr));
137 		if (len < sizeof(bt_dev.id_addr[0])) {
138 			if (len < 0) {
139 				BT_ERR("Failed to read ID address from storage"
140 				       " (err %d)", len);
141 			} else {
142 				BT_ERR("Invalid length ID address in storage");
143 				//BT_HEXDUMP_DBG(&bt_dev.id_addr, len,
144 				//	       "data read");
145 			}
146 			(void)memset(bt_dev.id_addr, 0,
147 				     sizeof(bt_dev.id_addr));
148 			bt_dev.id_count = 0U;
149 		} else {
150 			int i;
151 
152 			bt_dev.id_count = len / sizeof(bt_dev.id_addr[0]);
153 			for (i = 0; i < bt_dev.id_count; i++) {
154 				BT_DBG("ID[%d] %s", i,
155 				       bt_addr_le_str(&bt_dev.id_addr[i]));
156 			}
157 		}
158 
159 		return 0;
160 	}
161 
162 #if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
163 	if (!strncmp(name, "name", len)) {
164 		len = read_cb(cb_arg, &bt_dev.name, sizeof(bt_dev.name) - 1);
165 		if (len < 0) {
166 			BT_ERR("Failed to read device name from storage"
167 				       " (err %d)", len);
168 		} else {
169 			bt_dev.name[len] = '\0';
170 
171 			BT_DBG("Name set to %s", log_strdup(bt_dev.name));
172 		}
173 		return 0;
174 	}
175 #endif
176 
177 #if defined(CONFIG_BT_PRIVACY)
178 	if (!strncmp(name, "irk", len)) {
179 		len = read_cb(cb_arg, bt_dev.irk, sizeof(bt_dev.irk));
180 		if (len < sizeof(bt_dev.irk[0])) {
181 			if (len < 0) {
182 				BT_ERR("Failed to read IRK from storage"
183 				       " (err %d)", len);
184 			} else {
185 				BT_ERR("Invalid length IRK in storage");
186 				(void)memset(bt_dev.irk, 0, sizeof(bt_dev.irk));
187 			}
188 		} else {
189 			int i, count;
190 
191 			count = len / sizeof(bt_dev.irk[0]);
192 			for (i = 0; i < count; i++) {
193 				BT_DBG("IRK[%d] %s", i,
194 				       bt_hex(bt_dev.irk[i], 16));
195 			}
196 		}
197 
198 		return 0;
199 	}
200 #endif /* CONFIG_BT_PRIVACY */
201 
202 	return -ENOENT;
203 }
204 
205 #define ID_DATA_LEN(array) (bt_dev.id_count * sizeof(array[0]))
206 
save_id(struct k_work * work)207 static void save_id(struct k_work *work)
208 {
209 	int err;
210 	BT_INFO("Saving ID");
211 	err = settings_save_one("bt/id", &bt_dev.id_addr,
212 				ID_DATA_LEN(bt_dev.id_addr));
213 	if (err) {
214 		BT_ERR("Failed to save ID (err %d)", err);
215 	}
216 
217 #if defined(CONFIG_BT_PRIVACY)
218 	err = settings_save_one("bt/irk", bt_dev.irk, ID_DATA_LEN(bt_dev.irk));
219 	if (err) {
220 		BT_ERR("Failed to save IRK (err %d)", err);
221 	}
222 #endif
223 }
224 
225 //K_WORK_DEFINE(save_id_work, save_id);
226 static struct k_work  save_id_work;
227 
bt_settings_save_id(void)228 void bt_settings_save_id(void)
229 {
230 	k_work_submit(&save_id_work);
231 }
232 
commit(void)233 static int commit(void)
234 {
235 	BT_DBG("");
236 
237 #if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
238 	if (bt_dev.name[0] == '\0') {
239 		bt_set_name(CONFIG_BT_DEVICE_NAME);
240 	}
241 #endif
242 	if (!bt_dev.id_count) {
243 		bt_setup_public_id_addr();
244 	}
245 
246 	if (!bt_dev.id_count) {
247 		int err;
248 
249 		err = bt_setup_random_id_addr();
250 		if (err) {
251 			BT_ERR("Unable to setup an identity address");
252 			return err;
253 		}
254 	}
255 
256 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
257 		bt_finalize_init();
258 	}
259 
260 	/* If any part of the Identity Information of the device has been
261 	 * generated this Identity needs to be saved persistently.
262 	 */
263 	if (atomic_test_and_clear_bit(bt_dev.flags, BT_DEV_STORE_ID)) {
264 		BT_DBG("Storing Identity Information");
265 		bt_settings_save_id();
266 	}
267 
268 	return 0;
269 }
270 
271 //SETTINGS_STATIC_HANDLER_DEFINE(bt, "bt", NULL, set, commit, NULL);
272 
bt_settings_init(void)273 int bt_settings_init(void)
274 {
275 	int err;
276 
277 	BT_DBG("");
278 
279 	k_work_init(&save_id_work, save_id);
280 
281 	err = settings_subsys_init();
282 	if (err) {
283 		BT_ERR("settings_subsys_init failed (err %d)", err);
284 		return err;
285 	}
286 
287     SETTINGS_HANDLER_DEFINE(bt, "bt", NULL, set, commit, NULL);
288 
289 	return 0;
290 }
291