1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "common/bt_defs.h"
12 #include "common/bt_trace.h"
13 #include "osi/alarm.h"
14 #include "osi/allocator.h"
15 #include "device/bdaddr.h"
16 #include "btc/btc_config.h"
17 #include "btc/btc_util.h"
18 #include "osi/config.h"
19 #include "osi/osi.h"
20 #include "osi/mutex.h"
21
22 #include "stack/bt_types.h"
23
24 static const char *CONFIG_FILE_PATH = "bt_config.conf";
25 static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000;
26
27 static void btc_key_value_to_string(uint8_t *key_value, char *value_str, int key_length);
28 static osi_mutex_t lock; // protects operations on |config|.
29 static config_t *config;
30
btc_compare_address_key_value(const char * section,const char * key_type,void * key_value,int key_length)31 bool btc_compare_address_key_value(const char *section, const char *key_type, void *key_value, int key_length)
32 {
33 assert(key_value != NULL);
34 bool status = false;
35 char value_str[100] = {0};
36 if(key_length > sizeof(value_str)/2) {
37 return false;
38 }
39 btc_key_value_to_string((uint8_t *)key_value, value_str, key_length);
40 if ((status = config_has_key_in_section(config, key_type, value_str)) == true) {
41 config_remove_section(config, section);
42 }
43 return status;
44 }
45
btc_key_value_to_string(uint8_t * key_value,char * value_str,int key_length)46 static void btc_key_value_to_string(uint8_t *key_value, char *value_str, int key_length)
47 {
48 const char *lookup = "0123456789abcdef";
49
50 assert(key_value != NULL);
51 assert(value_str != NULL);
52
53 for (size_t i = 0; i < key_length; ++i) {
54 value_str[(i * 2) + 0] = lookup[(key_value[i] >> 4) & 0x0F];
55 value_str[(i * 2) + 1] = lookup[key_value[i] & 0x0F];
56 }
57
58 return;
59 }
60
61 // Module lifecycle functions
62
btc_config_init(void)63 bool btc_config_init(void)
64 {
65 osi_mutex_new(&lock);
66 config = config_new(CONFIG_FILE_PATH);
67 if (!config) {
68 BTC_TRACE_WARNING("%s unable to load config file; starting unconfigured.\n", __func__);
69 config = config_new_empty();
70 if (!config) {
71 BTC_TRACE_ERROR("%s unable to allocate a config object.\n", __func__);
72 goto error;
73 }
74 }
75 if (config_save(config, CONFIG_FILE_PATH)) {
76 // unlink(LEGACY_CONFIG_FILE_PATH);
77 }
78
79 return true;
80
81 error:;
82 config_free(config);
83 osi_mutex_free(&lock);
84 config = NULL;
85 BTC_TRACE_ERROR("%s failed\n", __func__);
86 return false;
87 }
88
btc_config_shut_down(void)89 bool btc_config_shut_down(void)
90 {
91 btc_config_flush();
92 return true;
93 }
94
btc_config_clean_up(void)95 bool btc_config_clean_up(void)
96 {
97 btc_config_flush();
98
99 config_free(config);
100 osi_mutex_free(&lock);
101 config = NULL;
102 return true;
103 }
104
btc_config_has_section(const char * section)105 bool btc_config_has_section(const char *section)
106 {
107 assert(config != NULL);
108 assert(section != NULL);
109
110 return config_has_section(config, section);
111 }
112
btc_config_exist(const char * section,const char * key)113 bool btc_config_exist(const char *section, const char *key)
114 {
115 assert(config != NULL);
116 assert(section != NULL);
117 assert(key != NULL);
118
119 return config_has_key(config, section, key);
120 }
121
btc_config_get_int(const char * section,const char * key,int * value)122 bool btc_config_get_int(const char *section, const char *key, int *value)
123 {
124 assert(config != NULL);
125 assert(section != NULL);
126 assert(key != NULL);
127 assert(value != NULL);
128
129 bool ret = config_has_key(config, section, key);
130 if (ret) {
131 *value = config_get_int(config, section, key, *value);
132 }
133
134 return ret;
135 }
136
btc_config_set_int(const char * section,const char * key,int value)137 bool btc_config_set_int(const char *section, const char *key, int value)
138 {
139 assert(config != NULL);
140 assert(section != NULL);
141 assert(key != NULL);
142
143 config_set_int(config, section, key, value);
144
145 return true;
146 }
147
btc_config_get_str(const char * section,const char * key,char * value,int * size_bytes)148 bool btc_config_get_str(const char *section, const char *key, char *value, int *size_bytes)
149 {
150 assert(config != NULL);
151 assert(section != NULL);
152 assert(key != NULL);
153 assert(value != NULL);
154 assert(size_bytes != NULL);
155
156 const char *stored_value = config_get_string(config, section, key, NULL);
157
158 if (!stored_value) {
159 return false;
160 }
161
162 strlcpy(value, stored_value, *size_bytes);
163 *size_bytes = strlen(value) + 1;
164
165 return true;
166 }
167
btc_config_set_str(const char * section,const char * key,const char * value)168 bool btc_config_set_str(const char *section, const char *key, const char *value)
169 {
170 assert(config != NULL);
171 assert(section != NULL);
172 assert(key != NULL);
173 assert(value != NULL);
174
175 config_set_string(config, section, key, value, false);
176
177 return true;
178 }
179
btc_config_get_bin(const char * section,const char * key,uint8_t * value,size_t * length)180 bool btc_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length)
181 {
182 assert(config != NULL);
183 assert(section != NULL);
184 assert(key != NULL);
185 assert(value != NULL);
186 assert(length != NULL);
187
188 const char *value_str = config_get_string(config, section, key, NULL);
189
190 if (!value_str) {
191 return false;
192 }
193
194 size_t value_len = strlen(value_str);
195 if ((value_len % 2) != 0 || *length < (value_len / 2)) {
196 return false;
197 }
198
199 for (size_t i = 0; i < value_len; ++i)
200 if (!isxdigit((unsigned char)value_str[i])) {
201 return false;
202 }
203
204 for (*length = 0; *value_str; value_str += 2, *length += 1) {
205 unsigned int val;
206 sscanf(value_str, "%02x", &val);
207 value[*length] = (uint8_t)(val);
208 }
209
210 return true;
211 }
212
btc_config_get_bin_length(const char * section,const char * key)213 size_t btc_config_get_bin_length(const char *section, const char *key)
214 {
215 assert(config != NULL);
216 assert(section != NULL);
217 assert(key != NULL);
218
219 const char *value_str = config_get_string(config, section, key, NULL);
220
221 if (!value_str) {
222 return 0;
223 }
224
225 size_t value_len = strlen(value_str);
226 return ((value_len % 2) != 0) ? 0 : (value_len / 2);
227 }
228
btc_config_set_bin(const char * section,const char * key,const uint8_t * value,size_t length)229 bool btc_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length)
230 {
231 const char *lookup = "0123456789abcdef";
232
233 assert(config != NULL);
234 assert(section != NULL);
235 assert(key != NULL);
236
237 if (length > 0) {
238 assert(value != NULL);
239 }
240
241 char *str = (char *)osi_calloc(length * 2 + 1);
242 if (!str) {
243 return false;
244 }
245
246 for (size_t i = 0; i < length; ++i) {
247 str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F];
248 str[(i * 2) + 1] = lookup[value[i] & 0x0F];
249 }
250
251 config_set_string(config, section, key, str, false);
252
253 osi_free(str);
254 return true;
255 }
256
btc_config_section_begin(void)257 const btc_config_section_iter_t *btc_config_section_begin(void)
258 {
259 assert(config != NULL);
260 return (const btc_config_section_iter_t *)config_section_begin(config);
261 }
262
btc_config_section_end(void)263 const btc_config_section_iter_t *btc_config_section_end(void)
264 {
265 assert(config != NULL);
266 return (const btc_config_section_iter_t *)config_section_end(config);
267 }
268
btc_config_section_next(const btc_config_section_iter_t * section)269 const btc_config_section_iter_t *btc_config_section_next(const btc_config_section_iter_t *section)
270 {
271 assert(config != NULL);
272 assert(section != NULL);
273 return (const btc_config_section_iter_t *)config_section_next((const config_section_node_t *)section);
274 }
275
btc_config_section_name(const btc_config_section_iter_t * section)276 const char *btc_config_section_name(const btc_config_section_iter_t *section)
277 {
278 assert(config != NULL);
279 assert(section != NULL);
280 return config_section_name((const config_section_node_t *)section);
281 }
282
283
284
btc_config_remove(const char * section,const char * key)285 bool btc_config_remove(const char *section, const char *key)
286 {
287 assert(config != NULL);
288 assert(section != NULL);
289 assert(key != NULL);
290
291 return config_remove_key(config, section, key);
292 }
293
btc_config_remove_section(const char * section)294 bool btc_config_remove_section(const char *section)
295 {
296 assert(config != NULL);
297 assert(section != NULL);
298
299 return config_remove_section(config, section);
300 }
301
btc_config_flush(void)302 void btc_config_flush(void)
303 {
304 assert(config != NULL);
305
306 config_save(config, CONFIG_FILE_PATH);
307 }
308
btc_config_clear(void)309 int btc_config_clear(void)
310 {
311 assert(config != NULL);
312
313 config_free(config);
314
315 config = config_new_empty();
316 if (config == NULL) {
317 return false;
318 }
319 int ret = config_save(config, CONFIG_FILE_PATH);
320 return ret;
321 }
322
btc_config_lock(void)323 void btc_config_lock(void)
324 {
325 osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT);
326 }
327
btc_config_unlock(void)328 void btc_config_unlock(void)
329 {
330 osi_mutex_unlock(&lock);
331 }
332