ble_mesh: stack: Use mutex for settings operation

Also expose the settings functions with the parameter
bt_mesh_nvs_handle_t, which will be used for further
updates.
This commit is contained in:
lly 2020-05-26 18:31:46 +08:00
parent a662d14a4e
commit 1a3515ab76
4 changed files with 95 additions and 12 deletions

View file

@ -168,6 +168,30 @@ struct node_info {
u8_t dev_key[16];
} __packed;
static bt_mesh_mutex_t settings_lock;
static void bt_mesh_settings_mutex_new(void)
{
if (settings_lock.mutex == NULL) {
bt_mesh_mutex_create(&settings_lock);
}
}
static void bt_mesh_settings_mutex_free(void)
{
bt_mesh_mutex_free(&settings_lock);
}
void bt_mesh_settings_lock(void)
{
bt_mesh_mutex_lock(&settings_lock);
}
void bt_mesh_settings_unlock(void)
{
bt_mesh_mutex_unlock(&settings_lock);
}
static int role_set(const char *name)
{
bool exist = false;
@ -2557,7 +2581,8 @@ int bt_mesh_settings_init(void)
{
BT_DBG("%s", __func__);
bt_mesh_settings_foreach();
bt_mesh_settings_mutex_new();
bt_mesh_settings_init_foreach();
return 0;
}
@ -2571,7 +2596,8 @@ int settings_core_deinit(void)
int bt_mesh_settings_deinit(void)
{
bt_mesh_settings_deforeach();
bt_mesh_settings_deinit_foreach();
bt_mesh_settings_mutex_free();
return 0;
}

View file

@ -64,6 +64,9 @@ void bt_mesh_store_node_name(struct bt_mesh_node *node);
void bt_mesh_store_node_comp_data(struct bt_mesh_node *node);
#endif
void bt_mesh_settings_lock(void);
void bt_mesh_settings_unlock(void);
int bt_mesh_settings_init(void);
int bt_mesh_settings_deinit(void);

View file

@ -55,7 +55,7 @@ static struct settings_context settings_ctx[] = {
/* API used to initialize, load and commit BLE Mesh related settings */
void bt_mesh_settings_foreach(void)
void bt_mesh_settings_init_foreach(void)
{
int err = 0;
int i;
@ -98,7 +98,7 @@ void bt_mesh_settings_foreach(void)
}
}
void bt_mesh_settings_deforeach(void)
void bt_mesh_settings_deinit_foreach(void)
{
int i;
@ -162,10 +162,20 @@ static int settings_save(bt_mesh_nvs_handle_t handle, const char *key, const u8_
return 0;
}
int bt_mesh_save_settings(bt_mesh_nvs_handle_t handle, const char *key,
const u8_t *val, size_t len)
{
int err = 0;
bt_mesh_settings_lock();
err = settings_save(handle, key, val, len);
bt_mesh_settings_unlock();
return err;
}
int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len)
{
bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE);
return settings_save(handle, key, val, len);
return bt_mesh_save_settings(handle, key, val, len);
}
/* API used to load BLE Mesh related settings */
@ -196,10 +206,20 @@ static int settings_load(bt_mesh_nvs_handle_t handle, const char *key,
return 0;
}
int bt_mesh_load_settings(bt_mesh_nvs_handle_t handle, const char *key,
u8_t *buf, size_t buf_len, bool *exist)
{
int err = 0;
bt_mesh_settings_lock();
err = settings_load(handle, key, buf, buf_len, exist);
bt_mesh_settings_unlock();
return err;
}
int bt_mesh_load_core_settings(const char *key, u8_t *buf, size_t buf_len, bool *exist)
{
bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE);
return settings_load(handle, key, buf, buf_len, exist);
return bt_mesh_load_settings(handle, key, buf, buf_len, exist);
}
/* API used to get length of BLE Mesh related settings */
@ -267,10 +287,19 @@ static struct net_buf_simple *settings_get_item(bt_mesh_nvs_handle_t handle, con
return buf;
}
struct net_buf_simple *bt_mesh_get_settings_item(bt_mesh_nvs_handle_t handle, const char *key)
{
struct net_buf_simple *buf = NULL;
bt_mesh_settings_lock();
buf = settings_get_item(handle, key);
bt_mesh_settings_unlock();
return buf;
}
struct net_buf_simple *bt_mesh_get_core_settings_item(const char *key)
{
bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE);
return settings_get_item(handle, key);
return bt_mesh_get_settings_item(handle, key);
}
/* API used to check if the settings item exists */
@ -339,10 +368,19 @@ static int settings_add_item(bt_mesh_nvs_handle_t handle, const char *key, const
return err;
}
int bt_mesh_add_settings_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val)
{
int err = 0;
bt_mesh_settings_lock();
err = settings_add_item(handle, key, val);
bt_mesh_settings_unlock();
return err;
}
int bt_mesh_add_core_settings_item(const char *key, const u16_t val)
{
bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE);
return settings_add_item(handle, key, val);
return bt_mesh_add_settings_item(handle, key, val);
}
/* API used to remove the settings item */
@ -394,10 +432,19 @@ static int settings_remove_item(bt_mesh_nvs_handle_t handle, const char *key, co
return err;
}
int bt_mesh_remove_settings_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val)
{
int err = 0;
bt_mesh_settings_lock();
err = settings_remove_item(handle, key, val);
bt_mesh_settings_unlock();
return err;
}
int bt_mesh_remove_core_settings_item(const char *key, const u16_t val)
{
bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE);
return settings_remove_item(handle, key, val);
return bt_mesh_remove_settings_item(handle, key, val);
}
#endif /* CONFIG_BLE_MESH_SETTINGS */

View file

@ -28,19 +28,26 @@ typedef nvs_handle bt_mesh_nvs_handle_t;
#define BLE_MESH_GET_ELEM_IDX(x) ((u8_t)((x) >> 8))
#define BLE_MESH_GET_MODEL_IDX(x) ((u8_t)(x))
#define BLE_MESH_GET_MODEL_KEY(a, b) ((u16_t)(((u16_t)((a) << 8)) | b))
#define BLE_MESH_GET_MODEL_KEY(a, b) ((u16_t)(((u16_t)((a) << 8)) | (b)))
void bt_mesh_settings_foreach(void);
void bt_mesh_settings_deforeach(void);
void bt_mesh_settings_init_foreach(void);
void bt_mesh_settings_deinit_foreach(void);
int bt_mesh_save_settings(bt_mesh_nvs_handle_t handle, const char *key,
const u8_t *val, size_t len);
int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len);
int bt_mesh_load_settings(bt_mesh_nvs_handle_t handle, const char *key,
u8_t *buf, size_t buf_len, bool *exist);
int bt_mesh_load_core_settings(const char *key, u8_t *buf, size_t buf_len, bool *exist);
struct net_buf_simple *bt_mesh_get_settings_item(bt_mesh_nvs_handle_t handle, const char *key);
struct net_buf_simple *bt_mesh_get_core_settings_item(const char *key);
int bt_mesh_add_settings_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val);
int bt_mesh_add_core_settings_item(const char *key, const u16_t val);
int bt_mesh_remove_settings_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val);
int bt_mesh_remove_core_settings_item(const char *key, const u16_t val);
#ifdef __cplusplus