component/bt: bluetooth storage module clean up and bug fix.
1. clean up bluetooth storage log prints; 2. reduce maximum link key storage size 3. modify so that the link key information of the latest connected device is stored at first
This commit is contained in:
parent
0f711963d7
commit
2c87e84b0a
5 changed files with 59 additions and 146 deletions
|
@ -18,9 +18,7 @@
|
||||||
|
|
||||||
#define LOG_TAG "bt_btif_config"
|
#define LOG_TAG "bt_btif_config"
|
||||||
|
|
||||||
// #include <assert.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
// #include <pthread.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
@ -31,7 +29,6 @@
|
||||||
#include "bdaddr.h"
|
#include "bdaddr.h"
|
||||||
#include "btif_config.h"
|
#include "btif_config.h"
|
||||||
#include "btif_util.h"
|
#include "btif_util.h"
|
||||||
// #include "osi/include/compat.h"
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "osi.h"
|
#include "osi.h"
|
||||||
|
|
||||||
|
@ -60,7 +57,7 @@ bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type)
|
||||||
return FALSE;
|
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 TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +77,7 @@ bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type)
|
||||||
return FALSE;
|
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;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,15 +89,13 @@ static osi_alarm_t *alarm_timer;
|
||||||
|
|
||||||
bool btif_config_init(void)
|
bool btif_config_init(void)
|
||||||
{
|
{
|
||||||
// karl LOG_ERROR("btif config_init\n");
|
|
||||||
|
|
||||||
pthread_mutex_init(&lock, NULL);
|
pthread_mutex_init(&lock, NULL);
|
||||||
config = config_new(CONFIG_FILE_PATH);
|
config = config_new(CONFIG_FILE_PATH);
|
||||||
if (!config) {
|
if (!config) {
|
||||||
LOG_WARN("%s unable to load config file; starting unconfigured.\n", __func__);
|
LOG_WARN("%s unable to load config file; starting unconfigured.\n", __func__);
|
||||||
config = config_new_empty();
|
config = config_new_empty();
|
||||||
if (!config) {
|
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;
|
goto error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,11 +109,10 @@ bool btif_config_init(void)
|
||||||
// write back to disk.
|
// write back to disk.
|
||||||
alarm_timer = osi_alarm_new("btif_config", timer_config_save, NULL, CONFIG_SETTLE_PERIOD_MS, false);
|
alarm_timer = osi_alarm_new("btif_config", timer_config_save, NULL, CONFIG_SETTLE_PERIOD_MS, false);
|
||||||
if (!alarm_timer) {
|
if (!alarm_timer) {
|
||||||
LOG_ERROR("%s unable to create alarm.", __func__);
|
LOG_ERROR("%s unable to create alarm.\n", __func__);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// LOG_ERROR("btif config_init end ok\n");
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
error:;
|
error:;
|
||||||
|
@ -127,7 +121,7 @@ error:;
|
||||||
pthread_mutex_destroy(&lock);
|
pthread_mutex_destroy(&lock);
|
||||||
alarm_timer = NULL;
|
alarm_timer = NULL;
|
||||||
config = NULL;
|
config = NULL;
|
||||||
LOG_ERROR("btif config_init end failed\n");
|
LOG_ERROR("%s failed\n", __func__);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,7 +228,7 @@ bool btif_config_set_str(const char *section, const char *key, const char *value
|
||||||
assert(value != NULL);
|
assert(value != NULL);
|
||||||
|
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
config_set_string(config, section, key, value);
|
config_set_string(config, section, key, value, false);
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -316,7 +310,7 @@ bool btif_config_set_bin(const char *section, const char *key, const uint8_t *va
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
config_set_string(config, section, key, str);
|
config_set_string(config, section, key, str, false);
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
|
|
||||||
osi_free(str);
|
osi_free(str);
|
||||||
|
@ -374,13 +368,11 @@ void btif_config_flush(void)
|
||||||
{
|
{
|
||||||
assert(config != NULL);
|
assert(config != NULL);
|
||||||
assert(alarm_timer != 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);
|
pthread_mutex_lock(&lock);
|
||||||
config_save(config, CONFIG_FILE_PATH);
|
config_save(config, CONFIG_FILE_PATH);
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
LOG_ERROR("flush end\n"); // karl
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int btif_config_clear(void)
|
int btif_config_clear(void)
|
||||||
|
|
|
@ -225,6 +225,7 @@ static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
|
||||||
status = BT_STATUS_FAIL;
|
status = BT_STATUS_FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
(void) status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
|
|
|
@ -28,18 +28,14 @@ bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
|
||||||
bdstr_t bdstr;
|
bdstr_t bdstr;
|
||||||
|
|
||||||
bdaddr_to_string(remote_bd_addr, bdstr, sizeof(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);
|
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);
|
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));
|
ret &= btif_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY));
|
||||||
BTIF_TRACE_WARNING("p3\n");
|
|
||||||
/* write bonded info immediately */
|
/* write bonded info immediately */
|
||||||
// karl
|
|
||||||
btif_config_flush();
|
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;
|
return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +52,6 @@ bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
|
||||||
static bt_status_t btif_in_fetch_bonded_devices(int add)
|
static bt_status_t btif_in_fetch_bonded_devices(int add)
|
||||||
{
|
{
|
||||||
BOOLEAN bt_linkkey_file_found = FALSE;
|
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)) {
|
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);
|
const char *name = btif_config_section_name(iter);
|
||||||
|
@ -64,7 +59,7 @@ static bt_status_t btif_in_fetch_bonded_devices(int add)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
BTIF_TRACE_WARNING("Remote device:%s", name);
|
BTIF_TRACE_WARNING("Remote device:%s\n", name);
|
||||||
LINK_KEY link_key;
|
LINK_KEY link_key;
|
||||||
size_t size = sizeof(link_key);
|
size_t size = sizeof(link_key);
|
||||||
if (btif_config_get_bin(name, "LinkKey", link_key, &size)) {
|
if (btif_config_get_bin(name, "LinkKey", link_key, &size)) {
|
||||||
|
@ -86,56 +81,15 @@ static bt_status_t btif_in_fetch_bonded_devices(int add)
|
||||||
(UINT8)linkkey_type, 0, pin_length);
|
(UINT8)linkkey_type, 0, pin_length);
|
||||||
}
|
}
|
||||||
bt_linkkey_file_found = TRUE;
|
bt_linkkey_file_found = TRUE;
|
||||||
} else {
|
|
||||||
BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid", name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!bt_linkkey_file_found) {
|
|
||||||
BTIF_TRACE_EVENT("Remote device:%s, no link key", 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 {
|
} else {
|
||||||
BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid\n", name);
|
BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid\n", name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (0);
|
if (!bt_linkkey_file_found) {
|
||||||
|
BTIF_TRACE_EVENT("Remote device:%s, no link key\n", name);
|
||||||
|
}
|
||||||
|
}
|
||||||
return BT_STATUS_SUCCESS;
|
return BT_STATUS_SUCCESS;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -155,7 +109,7 @@ bt_status_t btif_storage_load_bonded_devices(void)
|
||||||
{
|
{
|
||||||
bt_status_t status;
|
bt_status_t status;
|
||||||
status = btif_in_fetch_bonded_devices(1);
|
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;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,7 +127,7 @@ bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
|
||||||
{
|
{
|
||||||
bdstr_t bdstr;
|
bdstr_t bdstr;
|
||||||
bdaddr_to_string(remote_bd_addr, bdstr, sizeof(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;
|
int ret = 1;
|
||||||
if (btif_config_exist(bdstr, "LinkKeyType")) {
|
if (btif_config_exist(bdstr, "LinkKeyType")) {
|
||||||
|
|
|
@ -21,7 +21,6 @@
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include "nvs.h"
|
#include "nvs.h"
|
||||||
|
|
||||||
// #include <assert.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -34,8 +33,8 @@
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "bt_trace.h"
|
#include "bt_trace.h"
|
||||||
|
|
||||||
#define CONFIG_FILE_MAX_SIZE (4096)
|
#define CONFIG_FILE_MAX_SIZE (512)
|
||||||
#define CONFIG_KEY "cfg_key8"
|
#define CONFIG_KEY "cfg_key1"
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *key;
|
char *key;
|
||||||
char *value;
|
char *value;
|
||||||
|
@ -63,24 +62,17 @@ static entry_t *entry_new(const char *key, const char *value);
|
||||||
static void entry_free(void *ptr);
|
static void entry_free(void *ptr);
|
||||||
static entry_t *entry_find(const config_t *config, const char *section, const char *key);
|
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_new_empty(void)
|
||||||
{
|
{
|
||||||
config_t *config = osi_calloc(sizeof(config_t));
|
config_t *config = osi_calloc(sizeof(config_t));
|
||||||
if (!config) {
|
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;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
config->sections = list_new(section_free);
|
config->sections = list_new(section_free);
|
||||||
if (!config->sections) {
|
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;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +87,6 @@ config_t *config_new(const char *filename)
|
||||||
{
|
{
|
||||||
assert(filename != NULL);
|
assert(filename != NULL);
|
||||||
|
|
||||||
// LOG_ERROR("config new bgn\n");
|
|
||||||
config_t *config = config_new_empty();
|
config_t *config = config_new_empty();
|
||||||
if (!config) {
|
if (!config) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -105,15 +96,13 @@ config_t *config_new(const char *filename)
|
||||||
nvs_handle fp;
|
nvs_handle fp;
|
||||||
err = nvs_open(filename, NVS_READWRITE, &fp);
|
err = nvs_open(filename, NVS_READWRITE, &fp);
|
||||||
if (err != ESP_OK) {
|
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);
|
config_free(config);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// LOG_ERROR("config parse bgn\n");
|
|
||||||
config_parse(fp, config);
|
config_parse(fp, config);
|
||||||
// LOG_ERROR("config parse end\n");
|
nvs_close(fp);
|
||||||
my_nvs_close(fp);
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,7 +192,7 @@ void config_set_int(config_t *config, const char *section, const char *key, int
|
||||||
|
|
||||||
char value_str[32] = { 0 };
|
char value_str[32] = { 0 };
|
||||||
sprintf(value_str, "%d", value);
|
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)
|
void config_set_bool(config_t *config, const char *section, const char *key, bool value)
|
||||||
|
@ -212,15 +201,19 @@ void config_set_bool(config_t *config, const char *section, const char *key, boo
|
||||||
assert(section != NULL);
|
assert(section != NULL);
|
||||||
assert(key != 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);
|
section_t *sec = section_find(config, section);
|
||||||
if (!sec) {
|
if (!sec) {
|
||||||
sec = section_new(section);
|
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)) {
|
for (const list_node_t *node = list_begin(sec->entries); node != list_end(sec->entries); node = list_next(node)) {
|
||||||
|
@ -297,45 +290,40 @@ bool config_save(const config_t *config, const char *filename)
|
||||||
assert(*filename != '\0');
|
assert(*filename != '\0');
|
||||||
|
|
||||||
esp_err_t err;
|
esp_err_t err;
|
||||||
|
int err_code = 0;
|
||||||
nvs_handle fp;
|
nvs_handle fp;
|
||||||
char *line = osi_calloc(1024);
|
char *line = osi_calloc(1024);
|
||||||
char *buf = osi_calloc(CONFIG_FILE_MAX_SIZE);
|
char *buf = osi_calloc(CONFIG_FILE_MAX_SIZE);
|
||||||
if (!line || !buf) {
|
if (!line || !buf) {
|
||||||
|
err_code |= 0x01;
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = nvs_open(filename, NVS_READWRITE, &fp);
|
err = nvs_open(filename, NVS_READWRITE, &fp);
|
||||||
// LOG_ERROR("nvs open: %d\n", (int)fp);
|
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
// LOG_ERROR("%s unable to write file '%s'\n", __func__, filename);
|
err_code |= 0x02;
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// LOG_ERROR("m1, %s\n", filename);
|
|
||||||
int w_cnt, w_cnt_total = 0;
|
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)) {
|
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);
|
const section_t *section = (const section_t *)list_node(node);
|
||||||
// LOG_ERROR("m11\n");
|
LOG_DEBUG("section name: %s\n", section->name);
|
||||||
// LOG_ERROR("m12, %s\n", section->name);
|
|
||||||
w_cnt = snprintf(line, 1024, "[%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) {
|
if (w_cnt + w_cnt_total < CONFIG_FILE_MAX_SIZE) {
|
||||||
memcpy(buf + w_cnt_total, line, w_cnt);
|
memcpy(buf + w_cnt_total, line, w_cnt);
|
||||||
w_cnt_total += w_cnt;
|
w_cnt_total += w_cnt;
|
||||||
// LOG_ERROR("m21\n");
|
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// LOG_ERROR("m22\n");
|
|
||||||
for (const list_node_t *enode = list_begin(section->entries); enode != list_end(section->entries); enode = list_next(enode)) {
|
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);
|
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);
|
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) {
|
if (w_cnt + w_cnt_total < CONFIG_FILE_MAX_SIZE) {
|
||||||
memcpy(buf + w_cnt_total, line, w_cnt);
|
memcpy(buf + w_cnt_total, line, w_cnt);
|
||||||
w_cnt_total += w_cnt;
|
w_cnt_total += w_cnt;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -352,55 +340,38 @@ bool config_save(const config_t *config, const char *filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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';
|
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_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) {
|
if (err != ESP_OK) {
|
||||||
// LOG_ERROR("m40\n");
|
nvs_close(fp);
|
||||||
my_nvs_close(fp);
|
err_code |= 0x04;
|
||||||
LOG_ERROR("m41\n");
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
// LOG_ERROR("m5\n");
|
|
||||||
err = nvs_commit(fp);
|
err = nvs_commit(fp);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
LOG_ERROR("m50\n");
|
nvs_close(fp);
|
||||||
my_nvs_close(fp);
|
err_code |= 0x08;
|
||||||
LOG_ERROR("m51\n");
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// LOG_ERROR("m6\n");
|
nvs_close(fp);
|
||||||
my_nvs_close(fp);
|
|
||||||
osi_free(line);
|
osi_free(line);
|
||||||
osi_free(buf);
|
osi_free(buf);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
LOG_ERROR("m7\n");
|
|
||||||
if (buf) {
|
if (buf) {
|
||||||
osi_free(buf);
|
osi_free(buf);
|
||||||
}
|
}
|
||||||
if (line) {
|
if (line) {
|
||||||
osi_free(line);
|
osi_free(line);
|
||||||
}
|
}
|
||||||
|
if (err_code) {
|
||||||
|
LOG_ERROR("%s, err_code: 0x%x\n", __func__, err_code);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -428,41 +399,39 @@ static void config_parse(nvs_handle fp, config_t *config)
|
||||||
assert(fp != 0);
|
assert(fp != 0);
|
||||||
assert(config != NULL);
|
assert(config != NULL);
|
||||||
|
|
||||||
LOG_ERROR("cfg parse\n");
|
|
||||||
int line_num = 0;
|
int line_num = 0;
|
||||||
|
int err_code = 0;
|
||||||
char *line = osi_calloc(1024);
|
char *line = osi_calloc(1024);
|
||||||
char *section = osi_calloc(1024);
|
char *section = osi_calloc(1024);
|
||||||
char *buf = osi_calloc(CONFIG_FILE_MAX_SIZE);
|
char *buf = osi_calloc(CONFIG_FILE_MAX_SIZE);
|
||||||
if (!line || !section || !buf) {
|
if (!line || !section || !buf) {
|
||||||
|
err_code |= 0x01;
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// LOG_ERROR("p1\n");
|
|
||||||
esp_err_t err;
|
esp_err_t err;
|
||||||
size_t length = CONFIG_FILE_MAX_SIZE;
|
size_t length = CONFIG_FILE_MAX_SIZE;
|
||||||
err = nvs_get_blob(fp, CONFIG_KEY, buf, &length);
|
err = nvs_get_blob(fp, CONFIG_KEY, buf, &length);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
// LOG_ERROR("p2\n");
|
err_code |= 0x02;
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_ERROR("p3 %d\n%s\n", length, buf);
|
|
||||||
char *p_line_end;
|
char *p_line_end;
|
||||||
char *p_line_bgn = buf;
|
char *p_line_bgn = buf;
|
||||||
strcpy(section, CONFIG_DEFAULT_SECTION);
|
strcpy(section, CONFIG_DEFAULT_SECTION);
|
||||||
// LOG_ERROR("p4\n");
|
|
||||||
while ( (p_line_bgn < buf + length - 1) && (p_line_end = strchr(p_line_bgn, '\n'))) {
|
while ( (p_line_bgn < buf + length - 1) && (p_line_end = strchr(p_line_bgn, '\n'))) {
|
||||||
|
|
||||||
// get one line
|
// get one line
|
||||||
int line_len = p_line_end - p_line_bgn;
|
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) {
|
if (line_len > 1023) {
|
||||||
|
LOG_WARN("%s exceed max line length on line %d.\n", __func__, line_num);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
memcpy(line, p_line_bgn, line_len);
|
memcpy(line, p_line_bgn, line_len);
|
||||||
line[line_len] = '\0';
|
line[line_len] = '\0';
|
||||||
p_line_bgn = p_line_end + 1;
|
p_line_bgn = p_line_end + 1;
|
||||||
// LOG_ERROR("pi0\n");
|
|
||||||
char *line_ptr = trim(line);
|
char *line_ptr = trim(line);
|
||||||
++line_num;
|
++line_num;
|
||||||
|
|
||||||
|
@ -471,31 +440,26 @@ static void config_parse(nvs_handle fp, config_t *config)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// LOG_ERROR("pi1\n");
|
|
||||||
if (*line_ptr == '[') {
|
if (*line_ptr == '[') {
|
||||||
size_t len = strlen(line_ptr);
|
size_t len = strlen(line_ptr);
|
||||||
if (line_ptr[len - 1] != ']') {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
strncpy(section, line_ptr + 1, len - 2);
|
strncpy(section, line_ptr + 1, len - 2);
|
||||||
section[len - 2] = '\0';
|
section[len - 2] = '\0';
|
||||||
// LOG_ERROR("pi2\n");
|
|
||||||
} else {
|
} else {
|
||||||
// LOG_ERROR("pi3\n");
|
|
||||||
char *split = strchr(line_ptr, '=');
|
char *split = strchr(line_ptr, '=');
|
||||||
if (!split) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
// LOG_ERROR("pi4\n");
|
|
||||||
*split = '\0';
|
*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:
|
error:
|
||||||
// LOG_ERROR("p5\n");
|
|
||||||
if (buf) {
|
if (buf) {
|
||||||
osi_free(buf);
|
osi_free(buf);
|
||||||
}
|
}
|
||||||
|
@ -505,7 +469,9 @@ error:
|
||||||
if (section) {
|
if (section) {
|
||||||
osi_free(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)
|
static section_t *section_new(const char *name)
|
||||||
|
|
|
@ -81,7 +81,7 @@ void config_set_bool(config_t *config, const char *section, const char *key, boo
|
||||||
// Sets a string value for the |key| in |section|. If |key| or |section| do
|
// Sets a string value for the |key| in |section|. If |key| or |section| do
|
||||||
// not already exist, this function creates them. |config|, |section|, |key|, and
|
// not already exist, this function creates them. |config|, |section|, |key|, and
|
||||||
// |value| must not be NULL.
|
// |value| must not be NULL.
|
||||||
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);
|
||||||
|
|
||||||
// Removes |section| from the |config| (and, as a result, all keys in the section).
|
// Removes |section| from the |config| (and, as a result, all keys in the section).
|
||||||
// Returns true if |section| was found and removed from |config|, false otherwise.
|
// Returns true if |section| was found and removed from |config|, false otherwise.
|
||||||
|
|
Loading…
Reference in a new issue