1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
42
43
44
45
46
47
48
51
52
55
56
57
58
59
60
61
62
63
64
65
69
70
71
72
73
74
75
76
77
78
79
87
90
91
92
93
94
95
96
97
98
99
100
101
106
107
108
109
110
111
112
113
114
115
116
117
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
205
206
207
210
211
212
215
216
221
222
223
224
225
226
227
228
229
230
231
232
233
236
237
238
239
240
241
242
243
244
245
246
247
248
249
252
253
254
257
258
262
263
264
265
266
267
268
269
274
275
280
281
287
288
294
295
296
297
298
299
300
301
302
303
304
305
306
313
314
321
322
328
329
330
331
332
333
334
335
336
339
340
341
342
343
347
348
/* ... */
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "common/bt_defs.h"
#include "common/bt_trace.h"
#include "osi/alarm.h"
#include "osi/allocator.h"
#include "device/bdaddr.h"
#include "btc/btc_config.h"
#include "btc/btc_util.h"
#include "osi/config.h"
#include "osi/osi.h"
#include "osi/mutex.h"
#include "stack/bt_types.h"
#include "nvs.h"15 includes
static char CONFIG_FILE_PATH[NVS_NS_NAME_MAX_SIZE] = "bt_config.conf";
static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000;
static void btc_key_value_to_string(uint8_t *key_value, char *value_str, int key_length);
static osi_mutex_t lock;
static config_t *config;
int btc_config_file_path_update(const char *file_path)
{
if (file_path != NULL && strlen(file_path) < NVS_NS_NAME_MAX_SIZE) {
memcpy(CONFIG_FILE_PATH, file_path, strlen(file_path));
CONFIG_FILE_PATH[strlen(file_path)] = '\0';
return 0;
}{...}
BTC_TRACE_ERROR("Update failed, file_path is NULL or length should be less than %d\n", NVS_NS_NAME_MAX_SIZE);
return -1;
}{ ... }
bool btc_compare_address_key_value(const char *section, const char *key_type, void *key_value, int key_length)
{
assert(key_value != NULL);
bool status = false;
char value_str[100] = {0};
if(key_length > sizeof(value_str)/2) {
return false;
}{...}
btc_key_value_to_string((uint8_t *)key_value, value_str, key_length);
if ((status = config_has_key_in_section(config, key_type, value_str)) == true) {
config_remove_section(config, section);
}{...}
return status;
}{ ... }
static void btc_key_value_to_string(uint8_t *key_value, char *value_str, int key_length)
{
const char *lookup = "0123456789abcdef";
assert(key_value != NULL);
assert(value_str != NULL);
for (size_t i = 0; i < key_length; ++i) {
value_str[(i * 2) + 0] = lookup[(key_value[i] >> 4) & 0x0F];
value_str[(i * 2) + 1] = lookup[key_value[i] & 0x0F];
}{...}
return;
}{ ... }
bool btc_config_init(void)
{
osi_mutex_new(&lock);
config = config_new(CONFIG_FILE_PATH);
if (!config) {
BTC_TRACE_WARNING("%s unable to load config file; starting unconfigured.\n", __func__);
config = config_new_empty();
if (!config) {
BTC_TRACE_ERROR("%s unable to allocate a config object.\n", __func__);
goto error;
}{...}
}{...}
if (config_save(config, CONFIG_FILE_PATH)) {
}{...}
return true;
error:;
config_free(config);
osi_mutex_free(&lock);
config = NULL;
BTC_TRACE_ERROR("%s failed\n", __func__);
return false;
}{ ... }
bool btc_config_shut_down(void)
{
btc_config_flush();
return true;
}{ ... }
bool btc_config_clean_up(void)
{
btc_config_flush();
config_free(config);
osi_mutex_free(&lock);
config = NULL;
return true;
}{ ... }
bool btc_config_has_section(const char *section)
{
assert(config != NULL);
assert(section != NULL);
return config_has_section(config, section);
}{ ... }
bool btc_config_exist(const char *section, const char *key)
{
assert(config != NULL);
assert(section != NULL);
assert(key != NULL);
return config_has_key(config, section, key);
}{ ... }
bool btc_config_get_int(const char *section, const char *key, int *value)
{
assert(config != NULL);
assert(section != NULL);
assert(key != NULL);
assert(value != NULL);
bool ret = config_has_key(config, section, key);
if (ret) {
*value = config_get_int(config, section, key, *value);
}{...}
return ret;
}{ ... }
bool btc_config_set_int(const char *section, const char *key, int value)
{
assert(config != NULL);
assert(section != NULL);
assert(key != NULL);
config_set_int(config, section, key, value);
return true;
}{ ... }
bool btc_config_get_str(const char *section, const char *key, char *value, int *size_bytes)
{
assert(config != NULL);
assert(section != NULL);
assert(key != NULL);
assert(value != NULL);
assert(size_bytes != NULL);
const char *stored_value = config_get_string(config, section, key, NULL);
if (!stored_value) {
return false;
}{...}
strlcpy(value, stored_value, *size_bytes);
*size_bytes = strlen(value) + 1;
return true;
}{ ... }
bool btc_config_set_str(const char *section, const char *key, const char *value)
{
assert(config != NULL);
assert(section != NULL);
assert(key != NULL);
assert(value != NULL);
config_set_string(config, section, key, value, false);
return true;
}{ ... }
bool btc_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length)
{
assert(config != NULL);
assert(section != NULL);
assert(key != NULL);
assert(value != NULL);
assert(length != NULL);
const char *value_str = config_get_string(config, section, key, NULL);
if (!value_str) {
return false;
}{...}
size_t value_len = strlen(value_str);
if ((value_len % 2) != 0 || *length < (value_len / 2)) {
return false;
}{...}
for (size_t i = 0; i < value_len; ++i)
if (!isxdigit((unsigned char)value_str[i])) {
return false;
}{...}
for (*length = 0; *value_str; value_str += 2, *length += 1) {
unsigned int val;
sscanf(value_str, "%02x", &val);
value[*length] = (uint8_t)(val);
}{...}
return true;
}{ ... }
size_t btc_config_get_bin_length(const char *section, const char *key)
{
assert(config != NULL);
assert(section != NULL);
assert(key != NULL);
const char *value_str = config_get_string(config, section, key, NULL);
if (!value_str) {
return 0;
}{...}
size_t value_len = strlen(value_str);
return ((value_len % 2) != 0) ? 0 : (value_len / 2);
}{ ... }
bool btc_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length)
{
const char *lookup = "0123456789abcdef";
assert(config != NULL);
assert(section != NULL);
assert(key != NULL);
if (length > 0) {
assert(value != NULL);
}{...}
char *str = (char *)osi_calloc(length * 2 + 1);
if (!str) {
return false;
}{...}
for (size_t i = 0; i < length; ++i) {
str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F];
str[(i * 2) + 1] = lookup[value[i] & 0x0F];
}{...}
config_set_string(config, section, key, str, false);
osi_free(str);
return true;
}{ ... }
const btc_config_section_iter_t *btc_config_section_begin(void)
{
assert(config != NULL);
return (const btc_config_section_iter_t *)config_section_begin(config);
}{ ... }
const btc_config_section_iter_t *btc_config_section_end(void)
{
assert(config != NULL);
return (const btc_config_section_iter_t *)config_section_end(config);
}{ ... }
const btc_config_section_iter_t *btc_config_section_next(const btc_config_section_iter_t *section)
{
assert(config != NULL);
assert(section != NULL);
return (const btc_config_section_iter_t *)config_section_next((const config_section_node_t *)section);
}{ ... }
const char *btc_config_section_name(const btc_config_section_iter_t *section)
{
assert(config != NULL);
assert(section != NULL);
return config_section_name((const config_section_node_t *)section);
}{ ... }
bool btc_config_remove(const char *section, const char *key)
{
assert(config != NULL);
assert(section != NULL);
assert(key != NULL);
return config_remove_key(config, section, key);
}{ ... }
bool btc_config_remove_section(const char *section)
{
assert(config != NULL);
assert(section != NULL);
return config_remove_section(config, section);
}{ ... }
bool btc_config_update_newest_section(const char *section)
{
assert(config != NULL);
assert(section != NULL);
return config_update_newest_section(config, section);
}{ ... }
void btc_config_flush(void)
{
assert(config != NULL);
config_save(config, CONFIG_FILE_PATH);
}{ ... }
int btc_config_clear(void)
{
assert(config != NULL);
config_free(config);
config = config_new_empty();
if (config == NULL) {
return false;
}{...}
int ret = config_save(config, CONFIG_FILE_PATH);
return ret;
}{ ... }
void btc_config_lock(void)
{
osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT);
}{ ... }
void btc_config_unlock(void)
{
osi_mutex_unlock(&lock);
}{ ... }