#define LOG_TAG "bt_btif_config"
-// #include <assert.h>
#include <ctype.h>
-// #include <pthread.h>
#include <stdio.h>
#include <string.h>
#include "bdaddr.h"
#include "btif_config.h"
#include "btif_util.h"
-// #include "osi/include/compat.h"
#include "config.h"
#include "osi.h"
return FALSE;
}
- LOG_DEBUG("%s: Device [%s] type %d", __FUNCTION__, bd_addr_str, *p_device_type);
+ LOG_DEBUG("%s: Device [%s] type %d\n", __FUNCTION__, bd_addr_str, *p_device_type);
return TRUE;
}
return FALSE;
}
- LOG_DEBUG("%s: Device [%s] address type %d", __FUNCTION__, bd_addr_str, *p_addr_type);
+ LOG_DEBUG("%s: Device [%s] address type %d\n", __FUNCTION__, bd_addr_str, *p_addr_type);
return TRUE;
}
bool btif_config_init(void)
{
- // karl LOG_ERROR("btif config_init\n");
-
pthread_mutex_init(&lock, NULL);
config = config_new(CONFIG_FILE_PATH);
if (!config) {
LOG_WARN("%s unable to load config file; starting unconfigured.\n", __func__);
config = config_new_empty();
if (!config) {
- LOG_ERROR("%s unable to allocate a config object.", __func__);
+ LOG_ERROR("%s unable to allocate a config object.\n", __func__);
goto error;
}
}
// write back to disk.
alarm_timer = osi_alarm_new("btif_config", timer_config_save, NULL, CONFIG_SETTLE_PERIOD_MS, false);
if (!alarm_timer) {
- LOG_ERROR("%s unable to create alarm.", __func__);
+ LOG_ERROR("%s unable to create alarm.\n", __func__);
goto error;
}
- // LOG_ERROR("btif config_init end ok\n");
return true;
error:;
pthread_mutex_destroy(&lock);
alarm_timer = NULL;
config = NULL;
- LOG_ERROR("btif config_init end failed\n");
+ LOG_ERROR("%s failed\n", __func__);
return false;
}
assert(value != NULL);
pthread_mutex_lock(&lock);
- config_set_string(config, section, key, value);
+ config_set_string(config, section, key, value, false);
pthread_mutex_unlock(&lock);
return true;
}
pthread_mutex_lock(&lock);
- config_set_string(config, section, key, str);
+ config_set_string(config, section, key, str, false);
pthread_mutex_unlock(&lock);
osi_free(str);
{
assert(config != NULL);
assert(alarm_timer != NULL);
- LOG_ERROR("flush bgn\n"); // karl
- // osi_alarm_cancel(alarm_timer);
+ osi_alarm_cancel(alarm_timer);
pthread_mutex_lock(&lock);
config_save(config, CONFIG_FILE_PATH);
pthread_mutex_unlock(&lock);
- LOG_ERROR("flush end\n"); // karl
}
int btif_config_clear(void)
bdstr_t bdstr;
bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
- BTIF_TRACE_WARNING("add to storage: Remote device:%s", bdstr);
+ BTIF_TRACE_EVENT("add to storage: Remote device:%s\n", bdstr);
int ret = btif_config_set_int(bdstr, "LinkKeyType", (int)key_type);
- BTIF_TRACE_WARNING("p1\n");
ret &= btif_config_set_int(bdstr, "PinLength", (int)pin_length);
- BTIF_TRACE_WARNING("p2\n");
ret &= btif_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY));
- BTIF_TRACE_WARNING("p3\n");
/* write bonded info immediately */
- // karl
btif_config_flush();
- BTIF_TRACE_WARNING("Storage add rslt %d\n", ret);
+ BTIF_TRACE_EVENT("Storage add rslt %d\n", ret);
return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
}
static bt_status_t btif_in_fetch_bonded_devices(int add)
{
BOOLEAN bt_linkkey_file_found = FALSE;
- int device_type;
for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
const char *name = btif_config_section_name(iter);
continue;
}
- BTIF_TRACE_WARNING("Remote device:%s", name);
+ BTIF_TRACE_WARNING("Remote device:%s\n", name);
LINK_KEY link_key;
size_t size = sizeof(link_key);
if (btif_config_get_bin(name, "LinkKey", link_key, &size)) {
}
bt_linkkey_file_found = TRUE;
} else {
- BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid", name);
+ BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid\n", name);
}
}
if (!bt_linkkey_file_found) {
- BTIF_TRACE_EVENT("Remote device:%s, no link key", name);
+ BTIF_TRACE_EVENT("Remote device:%s, no link key\n", name);
}
}
return BT_STATUS_SUCCESS;
-#if 0
- int device_type;
-
- BTIF_TRACE_WARNING("fetch from storage");
- do {
- const char *name = "bt_host";
-
- bt_bdaddr_t bd_addr;
- size_t size = sizeof(bt_bdaddr_t);
- if (!btif_config_get_bin(name, "BdAddr", bd_addr.address, &size)) {
- continue;
- } else {
- bdstr_t bdstr;
- bdaddr_to_string(&bd_addr, bdstr, sizeof(bdstr));
- BTIF_TRACE_WARNING("fetch from storage: Remote device:%s\n", bdstr);
- }
-
- LINK_KEY link_key;
- size = sizeof(link_key);
- if (btif_config_get_bin(name, "LinkKey", link_key, &size)) {
- int linkkey_type;
- if (btif_config_get_int(name, "LinkKeyType", &linkkey_type)) {
- if (add) {
- DEV_CLASS dev_class = {0, 0, 0};
- int cod;
- int pin_length = 0;
- if (btif_config_get_int(name, "DevClass", &cod)) {
- uint2devclass((UINT32)cod, dev_class);
- }
- btif_config_get_int(name, "PinLength", &pin_length);
- BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0,
- (UINT8)linkkey_type, 0, pin_length);
-
- }
- } else {
- BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid\n", name);
- }
- }
- } while (0);
- return BT_STATUS_SUCCESS;
-#endif
}
{
bt_status_t status;
status = btif_in_fetch_bonded_devices(1);
- BTIF_TRACE_WARNING("Storage load rslt %d\n", status);
+ BTIF_TRACE_EVENT("Storage load rslt %d\n", status);
return status;
}
{
bdstr_t bdstr;
bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
- BTIF_TRACE_EVENT("add to storage: Remote device:%s\n", bdstr);
+ BTIF_TRACE_EVENT("Add to storage: Remote device:%s\n", bdstr);
int ret = 1;
if (btif_config_exist(bdstr, "LinkKeyType")) {
#include "nvs_flash.h"
#include "nvs.h"
-// #include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include "list.h"
#include "bt_trace.h"
-#define CONFIG_FILE_MAX_SIZE (4096)
-#define CONFIG_KEY "cfg_key8"
+#define CONFIG_FILE_MAX_SIZE (512)
+#define CONFIG_KEY "cfg_key1"
typedef struct {
char *key;
char *value;
static void entry_free(void *ptr);
static entry_t *entry_find(const config_t *config, const char *section, const char *key);
-static void my_nvs_close(nvs_handle fp)
-{
- // LOG_ERROR("nvs close %d\n", (int)fp);
- nvs_close(fp);
-}
-
-
config_t *config_new_empty(void)
{
config_t *config = osi_calloc(sizeof(config_t));
if (!config) {
- LOG_ERROR("%s unable to allocate memory for config_t.", __func__);
+ LOG_ERROR("%s unable to allocate memory for config_t.\n", __func__);
goto error;
}
config->sections = list_new(section_free);
if (!config->sections) {
- LOG_ERROR("%s unable to allocate list for sections.", __func__);
+ LOG_ERROR("%s unable to allocate list for sections.\n", __func__);
goto error;
}
{
assert(filename != NULL);
- // LOG_ERROR("config new bgn\n");
config_t *config = config_new_empty();
if (!config) {
return NULL;
nvs_handle fp;
err = nvs_open(filename, NVS_READWRITE, &fp);
if (err != ESP_OK) {
- LOG_ERROR("%s unable to open file '%s'", __func__, filename);
+ LOG_ERROR("%s unable to open file '%s'\n", __func__, filename);
config_free(config);
return NULL;
}
- // LOG_ERROR("config parse bgn\n");
config_parse(fp, config);
- // LOG_ERROR("config parse end\n");
- my_nvs_close(fp);
+ nvs_close(fp);
return config;
}
char value_str[32] = { 0 };
sprintf(value_str, "%d", value);
- config_set_string(config, section, key, value_str);
+ config_set_string(config, section, key, value_str, false);
}
void config_set_bool(config_t *config, const char *section, const char *key, bool value)
assert(section != NULL);
assert(key != NULL);
- config_set_string(config, section, key, value ? "true" : "false");
+ config_set_string(config, section, key, value ? "true" : "false", false);
}
-void config_set_string(config_t *config, const char *section, const char *key, const char *value)
+void config_set_string(config_t *config, const char *section, const char *key, const char *value, bool insert_back)
{
section_t *sec = section_find(config, section);
if (!sec) {
sec = section_new(section);
- list_append(config->sections, sec);
+ if (insert_back) {
+ list_append(config->sections, sec);
+ } else {
+ list_prepend(config->sections, sec);
+ }
}
for (const list_node_t *node = list_begin(sec->entries); node != list_end(sec->entries); node = list_next(node)) {
assert(*filename != '\0');
esp_err_t err;
+ int err_code = 0;
nvs_handle fp;
char *line = osi_calloc(1024);
char *buf = osi_calloc(CONFIG_FILE_MAX_SIZE);
if (!line || !buf) {
+ err_code |= 0x01;
goto error;
}
err = nvs_open(filename, NVS_READWRITE, &fp);
- // LOG_ERROR("nvs open: %d\n", (int)fp);
if (err != ESP_OK) {
- // LOG_ERROR("%s unable to write file '%s'\n", __func__, filename);
+ err_code |= 0x02;
goto error;
}
- // LOG_ERROR("m1, %s\n", filename);
int w_cnt, w_cnt_total = 0;
for (const list_node_t *node = list_begin(config->sections); node != list_end(config->sections); node = list_next(node)) {
const section_t *section = (const section_t *)list_node(node);
- // LOG_ERROR("m11\n");
- // LOG_ERROR("m12, %s\n", section->name);
+ LOG_DEBUG("section name: %s\n", section->name);
w_cnt = snprintf(line, 1024, "[%s]\n", section->name);
- // LOG_ERROR("m2 : %s\n", section->name);
if (w_cnt + w_cnt_total < CONFIG_FILE_MAX_SIZE) {
memcpy(buf + w_cnt_total, line, w_cnt);
w_cnt_total += w_cnt;
- // LOG_ERROR("m21\n");
} else {
break;
}
- // LOG_ERROR("m22\n");
+
for (const list_node_t *enode = list_begin(section->entries); enode != list_end(section->entries); enode = list_next(enode)) {
const entry_t *entry = (const entry_t *)list_node(enode);
- // LOG_ERROR("m30: %s, %s\n", entry->key, entry->value);
+ LOG_DEBUG("(key, val): (%s, %s)\n", entry->key, entry->value);
w_cnt = snprintf(line, 1024, "%s = %s\n", entry->key, entry->value);
- // LOG_ERROR("m3 : %s, %s\n", entry->key, entry->value);
if (w_cnt + w_cnt_total < CONFIG_FILE_MAX_SIZE) {
memcpy(buf + w_cnt_total, line, w_cnt);
w_cnt_total += w_cnt;
-
} else {
break;
}
}
}
- LOG_ERROR("m4 : %s, %d\n", buf, w_cnt_total);
- {
- // LOG_ERROR("m4x\n");
- size_t tmp_len = 4096;
- char *buf1 = osi_calloc(tmp_len);
- err = nvs_get_str(fp, CONFIG_KEY, buf1, &tmp_len);
- if (err == ESP_OK) {
- LOG_ERROR("rd %d\n%s", tmp_len, buf1);
- }
- // err = nvs_erase_key(fp, CONFIG_KEY);
- // LOG_ERROR("m4y\n");
- osi_free(buf1);
- }
buf[w_cnt_total] = '\0';
- // LOG_ERROR("set str bgn %d, %s, %d %d\n", (int)fp, CONFIG_KEY, w_cnt_total, strlen(buf));
err = nvs_set_blob(fp, CONFIG_KEY, buf, w_cnt_total);
- // err = nvs_set_str(fp, CONFIG_KEY, "abc");
- // LOG_ERROR("set str end\n");
if (err != ESP_OK) {
- // LOG_ERROR("m40\n");
- my_nvs_close(fp);
- LOG_ERROR("m41\n");
+ nvs_close(fp);
+ err_code |= 0x04;
goto error;
}
- // LOG_ERROR("m5\n");
+
err = nvs_commit(fp);
if (err != ESP_OK) {
- LOG_ERROR("m50\n");
- my_nvs_close(fp);
- LOG_ERROR("m51\n");
+ nvs_close(fp);
+ err_code |= 0x08;
goto error;
}
- // LOG_ERROR("m6\n");
- my_nvs_close(fp);
+ nvs_close(fp);
osi_free(line);
osi_free(buf);
return true;
error:
- LOG_ERROR("m7\n");
if (buf) {
osi_free(buf);
}
if (line) {
osi_free(line);
}
+ if (err_code) {
+ LOG_ERROR("%s, err_code: 0x%x\n", __func__, err_code);
+ }
return false;
}
assert(fp != 0);
assert(config != NULL);
- LOG_ERROR("cfg parse\n");
int line_num = 0;
+ int err_code = 0;
char *line = osi_calloc(1024);
char *section = osi_calloc(1024);
char *buf = osi_calloc(CONFIG_FILE_MAX_SIZE);
if (!line || !section || !buf) {
+ err_code |= 0x01;
goto error;
}
- // LOG_ERROR("p1\n");
esp_err_t err;
size_t length = CONFIG_FILE_MAX_SIZE;
err = nvs_get_blob(fp, CONFIG_KEY, buf, &length);
if (err != ESP_OK) {
- // LOG_ERROR("p2\n");
+ err_code |= 0x02;
goto error;
}
- LOG_ERROR("p3 %d\n%s\n", length, buf);
char *p_line_end;
char *p_line_bgn = buf;
strcpy(section, CONFIG_DEFAULT_SECTION);
- // LOG_ERROR("p4\n");
+
while ( (p_line_bgn < buf + length - 1) && (p_line_end = strchr(p_line_bgn, '\n'))) {
// get one line
int line_len = p_line_end - p_line_bgn;
- // LOG_ERROR("pii, %d, %x, %x, %x\n", line_len, p_line_bgn, p_line_end, buf);
if (line_len > 1023) {
+ LOG_WARN("%s exceed max line length on line %d.\n", __func__, line_num);
break;
}
memcpy(line, p_line_bgn, line_len);
line[line_len] = '\0';
p_line_bgn = p_line_end + 1;
- // LOG_ERROR("pi0\n");
char *line_ptr = trim(line);
++line_num;
continue;
}
- // LOG_ERROR("pi1\n");
if (*line_ptr == '[') {
size_t len = strlen(line_ptr);
if (line_ptr[len - 1] != ']') {
- LOG_WARN("%s unterminated section name on line %d.", __func__, line_num);
+ LOG_WARN("%s unterminated section name on line %d.\n", __func__, line_num);
continue;
}
strncpy(section, line_ptr + 1, len - 2);
section[len - 2] = '\0';
- // LOG_ERROR("pi2\n");
} else {
- // LOG_ERROR("pi3\n");
char *split = strchr(line_ptr, '=');
if (!split) {
- LOG_DEBUG("%s no key/value separator found on line %d.", __func__, line_num);
+ LOG_DEBUG("%s no key/value separator found on line %d.\n", __func__, line_num);
continue;
}
- // LOG_ERROR("pi4\n");
*split = '\0';
- config_set_string(config, section, trim(line_ptr), trim(split + 1));
+ config_set_string(config, section, trim(line_ptr), trim(split + 1), true);
}
}
error:
- // LOG_ERROR("p5\n");
if (buf) {
osi_free(buf);
}
if (section) {
osi_free(section);
}
- // LOG_ERROR("p6\n");
+ if (err_code) {
+ LOG_ERROR("%s returned with err code: %d\n", __func__, err_code);
+ }
}
static section_t *section_new(const char *name)