Merge branch 'bugfix/ble_mesh_settings_fix_v3.3' into 'release/v3.3'

Bugfix/ble mesh settings fix (v3.3)

See merge request espressif/esp-idf!10786
This commit is contained in:
Island 2020-10-13 10:50:07 +08:00
commit 733aaa4af0
34 changed files with 490 additions and 301 deletions

View file

@ -1132,6 +1132,7 @@ typedef union {
*/
struct ble_mesh_provisioner_add_local_app_key_comp_param {
int err_code; /*!< Indicate the result of adding local AppKey by the Provisioner */
uint16_t net_idx; /*!< NetKey Index */
uint16_t app_idx; /*!< AppKey Index */
} provisioner_add_app_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT */
/**
@ -1176,14 +1177,14 @@ typedef union {
/**
* @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT
*/
struct ble_mesh_provisioner_delete_node_with_uuid_comp_data_comp_param {
struct ble_mesh_provisioner_delete_node_with_uuid_comp_param {
int err_code; /*!< Indicate the result of deleting node with uuid by the Provisioner */
uint8_t uuid[16]; /*!< Node device uuid */
} provisioner_delete_node_with_uuid_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT */
/**
* @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT
*/
struct ble_mesh_provisioner_delete_node_with_addr_comp_data_comp_param {
struct ble_mesh_provisioner_delete_node_with_addr_comp_param {
int err_code; /*!< Indicate the result of deleting node with unicast address by the Provisioner */
uint16_t unicast_addr; /*!< Node unicast address */
} provisioner_delete_node_with_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT */

View file

@ -1823,10 +1823,11 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg)
app_key = arg->add_local_app_key.app_key;
}
act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT;
param.provisioner_add_app_key_comp.app_idx = arg->add_local_app_key.app_idx;
param.provisioner_add_app_key_comp.err_code =
bt_mesh_provisioner_local_app_key_add(app_key, arg->add_local_app_key.net_idx,
&arg->add_local_app_key.app_idx);
param.provisioner_add_app_key_comp.net_idx = arg->add_local_app_key.net_idx;
param.provisioner_add_app_key_comp.app_idx = arg->add_local_app_key.app_idx;
break;
}
case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY:
@ -1856,9 +1857,9 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg)
net_key = arg->add_local_net_key.net_key;
}
act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT;
param.provisioner_add_net_key_comp.net_idx = arg->add_local_net_key.net_idx;
param.provisioner_add_net_key_comp.err_code =
bt_mesh_provisioner_local_net_key_add(net_key, &arg->add_local_net_key.net_idx);
param.provisioner_add_net_key_comp.net_idx = arg->add_local_net_key.net_idx;
break;
}
case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY:

View file

@ -35,6 +35,11 @@ void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod,
{
int i, j;
if (dev_comp == NULL) {
BT_ERR("Invalid device composition");
return;
}
for (i = 0; i < dev_comp->elem_count; i++) {
struct bt_mesh_elem *elem = &dev_comp->elem[i];

View file

@ -218,7 +218,7 @@ static void clear_friendship(bool force, bool disable)
return;
}
bt_mesh_rx_reset();
bt_mesh_rx_reset(true);
k_delayed_work_cancel(&lpn->timer);

View file

@ -107,7 +107,7 @@ void bt_mesh_node_reset(void)
bt_mesh_cfg_reset();
bt_mesh_rx_reset();
bt_mesh_rx_reset(true);
bt_mesh_tx_reset();
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
@ -199,10 +199,27 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
return -EINVAL;
}
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE);
/* Add this judgement here in case the device worked as a
* Provisioner previously. Before the corresponding info
* of Provisioner is erased from flash, users try to use
* the device as a node, which will cause the information
* in NVS been handled incorrectly.
*/
u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK;
if (role != BLE_MESH_SETTINGS_ROLE_NONE &&
role != BLE_MESH_SETTINGS_ROLE_NODE) {
BT_ERR("%s, Mismatch role %u", __func__, role);
return -EIO;
}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_role();
if (role == BLE_MESH_SETTINGS_ROLE_NONE) {
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE);
}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS_BACKWARD_COMPATIBILITY) ||
role == BLE_MESH_SETTINGS_ROLE_NONE) {
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_role();
}
}
if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
@ -225,6 +242,7 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)
{
if (bt_mesh_is_provisioned()) {
BT_WARN("%s, Already provisioned", __func__);
return -EALREADY;
}
@ -504,10 +522,7 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param)
bt_mesh_comp_unprovision();
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
if (param->erase) {
bt_mesh_clear_role();
}
bt_mesh_settings_deinit();
bt_mesh_settings_deinit(param->erase);
}
bt_mesh_timer_deinit();
@ -532,6 +547,27 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)
return -EINVAL;
}
/* Add this judgement here in case the device worked as a
* node previously. Before the corresponding information
* of the node is erased from flash, users try to use the
* device as a Provisioner, which will cause the information
* in NVS been handled incorrectly.
*/
u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK;
if (role != BLE_MESH_SETTINGS_ROLE_NONE &&
role != BLE_MESH_SETTINGS_ROLE_PROV) {
BT_ERR("%s, Mismatch role %u", __func__, role);
return -EIO;
}
if (role == BLE_MESH_SETTINGS_ROLE_NONE) {
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_PROVISIONER);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_role();
}
}
err = bt_mesh_provisioner_net_create();
if (err) {
BT_ERR("Failed to create network");
@ -548,12 +584,6 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)
bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr());
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_PROVISIONER);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_role();
}
#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN)
if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
(bearers & BLE_MESH_PROV_ADV)) {

View file

@ -2,6 +2,7 @@
/*
* Copyright (c) 2017 Intel Corporation
* Additional Copyright (c) 2020 Espressif Systems (Shanghai) PTE LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -15,13 +16,13 @@
extern "C" {
#endif
#define BLE_MESH_KEY_PRIMARY 0x0000
#define BLE_MESH_KEY_ANY 0xffff
#define BLE_MESH_KEY_PRIMARY 0x0000
#define BLE_MESH_KEY_ANY 0xffff
#define BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000)
#define BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) <= 0xff00)
#define BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000)
#define BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfffb)
#define BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000)
#define BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) <= 0xff00)
#define BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000)
#define BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfffb)
struct bt_mesh_net;

View file

@ -165,7 +165,7 @@ int bt_mesh_provisioner_deinit(bool erase)
for (i = 0; i < CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT; i++) {
if (bt_mesh.p_sub[i]) {
if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_p_subnet(bt_mesh.p_sub[i]);
bt_mesh_clear_p_subnet(bt_mesh.p_sub[i]->net_idx);
}
bt_mesh_free(bt_mesh.p_sub[i]);
bt_mesh.p_sub[i] = NULL;
@ -175,7 +175,7 @@ int bt_mesh_provisioner_deinit(bool erase)
for (i = 0; i < CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT; i++) {
if (bt_mesh.p_app_keys[i]) {
if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_p_app_key(bt_mesh.p_app_keys[i]);
bt_mesh_clear_p_app_key(bt_mesh.p_app_keys[i]->app_idx);
}
bt_mesh_free(bt_mesh.p_app_keys[i]);
bt_mesh.p_app_keys[i] = NULL;
@ -1027,6 +1027,7 @@ int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16],
}
}
*app_idx = key->app_idx;
bt_mesh.p_app_idx_next++;
}
key->updated = false;
@ -1198,7 +1199,7 @@ int bt_mesh_provisioner_local_app_key_delete(u16_t net_idx, u16_t app_idx)
bt_mesh_model_foreach(_model_unbind, &app_idx);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_p_app_key(key);
bt_mesh_clear_p_app_key(app_idx);
}
bt_mesh_free(bt_mesh.p_app_keys[i]);
@ -1283,6 +1284,7 @@ int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx
}
}
*net_idx = sub->net_idx;
bt_mesh.p_net_idx_next++;
}
sub->kr_phase = BLE_MESH_KR_NORMAL;
sub->kr_flag = false;
@ -1391,7 +1393,7 @@ int bt_mesh_provisioner_local_net_key_delete(u16_t net_idx)
}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_p_subnet(sub);
bt_mesh_clear_p_subnet(net_idx);
}
bt_mesh_free(bt_mesh.p_sub[i]);

View file

@ -1117,6 +1117,11 @@ int bt_mesh_provisioner_init_prov_info(void)
/* If unicast address of primary element of Provisioner has not been set
* before, then the following initialization procedure will be used.
*/
if (prov == NULL) {
BT_ERR("No provisioning context provided");
return -EINVAL;
}
if (!BLE_MESH_ADDR_IS_UNICAST(prov->prov_unicast_addr) ||
!BLE_MESH_ADDR_IS_UNICAST(prov->prov_start_address)) {
BT_ERR("Invalid address, own 0x%04x, start 0x%04x",

View file

@ -19,6 +19,7 @@
#include "cfg_srv.h"
#include "mesh_common.h"
#include "settings_nvs.h"
#include "settings.h"
#include "provisioner_main.h"
#include "provisioner_prov.h"
@ -167,7 +168,29 @@ struct node_info {
u8_t dev_key[16];
} __packed;
#define DEVICE_ROLE_BITS (BIT(BLE_MESH_NODE) | BIT(BLE_MESH_PROVISIONER))
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)
{
@ -340,13 +363,18 @@ static int rpl_set(const char *name)
for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
u16_t src = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(src)) {
BT_ERR("Invalid source address 0x%04x", src);
continue;
}
sprintf(get, "mesh/rpl/%04x", src);
err = bt_mesh_load_core_settings(get, (u8_t *)&rpl, sizeof(rpl), &exist);
if (err) {
BT_ERR("Failed to load RPL entry 0x%04x", src);
bt_mesh_rpl_reset();
goto free;
continue;
}
if (exist == false) {
@ -363,10 +391,11 @@ static int rpl_set(const char *name)
}
}
BT_INFO("Restored RPL entry 0x%04x: seq 0x%06x, old_iv %u", src, rpl.seq, rpl.old_iv);
entry->src = src;
entry->seq = rpl.seq;
entry->old_iv = rpl.old_iv;
BT_INFO("Restored RPL entry 0x%04x: seq 0x%06x, old_iv %u", src, rpl.seq, rpl.old_iv);
}
free:
@ -415,7 +444,7 @@ static int net_key_set(const char *name)
err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist);
if (err) {
BT_ERR("Failed to load NetKey 0x%03x", net_idx);
goto free;
continue;
}
if (exist == false) {
@ -475,7 +504,7 @@ static int app_key_set(const char *name)
err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist);
if (err) {
BT_ERR("Failed to load AppKey 0x%03x", app_idx);
goto free;
continue;
}
if (exist == false) {
@ -485,8 +514,7 @@ static int app_key_set(const char *name)
sub = bt_mesh_subnet_get(key.net_idx);
if (!sub) {
BT_ERR("Failed to find subnet 0x%03x", key.net_idx);
err = -ENOENT;
goto free;
continue;
}
app = bt_mesh_app_key_find(app_idx);
@ -527,13 +555,13 @@ static int hb_pub_set(const char *name)
BT_DBG("%s", __func__);
if (!hb_pub) {
BT_ERR("Invalid heartbeat pub");
BT_ERR("Invalid heartbeat publication");
return -EINVAL;
}
err = bt_mesh_load_core_settings(name, (u8_t *)&hb_val, sizeof(hb_val), &exist);
if (err) {
BT_ERR("Failed to load heartbeat pub");
BT_ERR("Failed to load heartbeat publication");
hb_pub->dst = BLE_MESH_ADDR_UNASSIGNED;
hb_pub->count = 0U;
hb_pub->ttl = 0U;
@ -557,7 +585,8 @@ static int hb_pub_set(const char *name)
hb_pub->count = 0U;
}
BT_INFO("Restored Heartbeat Publication, dst 0x%04x", hb_pub->dst);
BT_INFO("Restored Heartbeat Publication, dst 0x%04x, period %d, net_idx 0x%03x",
hb_pub->dst, hb_pub->period, hb_pub->net_idx);
return 0;
}
@ -590,7 +619,10 @@ static int cfg_set(const char *name)
memcpy(&stored_cfg.cfg, &val, sizeof(val));
stored_cfg.valid = true;
BT_INFO("Restored Configuration State");
BT_INFO("Restored Configuration, ttl %d, transmit 0x%02x, retransmit 0x%02x",
val.default_ttl, val.net_transmit, val.relay_retransmit);
return 0;
}
@ -613,6 +645,10 @@ static int model_set_bind(bool vnd, struct bt_mesh_model *model, u16_t model_key
return -EIO;
}
if (exist == true) {
BT_INFO("Restored Model Bound AppKey, index %s", bt_hex(model->keys, sizeof(model->keys)));
}
return 0;
}
@ -635,6 +671,10 @@ static int model_set_sub(bool vnd, struct bt_mesh_model *model, u16_t model_key)
return -EIO;
}
if (exist == true) {
BT_INFO("Restored Model Subscription, address %s", bt_hex(model->groups, sizeof(model->groups)));
}
return 0;
}
@ -662,7 +702,7 @@ static int model_set_pub(bool vnd, struct bt_mesh_model *model, u16_t model_key)
model->pub->period = 0U;
model->pub->retransmit = 0U;
model->pub->count = 0U;
return 0;
return -EIO;
}
if (exist == false) {
@ -677,8 +717,7 @@ static int model_set_pub(bool vnd, struct bt_mesh_model *model, u16_t model_key)
model->pub->retransmit = pub.retransmit;
model->pub->count = 0U;
BT_INFO("Restored Model Publication, address 0x%04x, app_idx 0x%03x",
pub.addr, pub.key);
BT_INFO("Restored Model Publication, address 0x%04x, app_idx 0x%03x", pub.addr, pub.key);
return 0;
}
@ -689,7 +728,6 @@ static int model_set(bool vnd, const char *name)
struct net_buf_simple *buf = NULL;
u8_t elem_idx = 0U, model_idx = 0U;
size_t length = 0U;
int err = 0;
int i;
BT_DBG("%s", __func__);
@ -703,6 +741,7 @@ static int model_set(bool vnd, const char *name)
for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
u16_t model_key = net_buf_simple_pull_le16(buf);
elem_idx = BLE_MESH_GET_ELEM_IDX(model_key);
model_idx = BLE_MESH_GET_MODEL_IDX(model_key);
@ -710,29 +749,16 @@ static int model_set(bool vnd, const char *name)
if (!model) {
BT_ERR("%s model not found, elem_idx %u, model_idx %u",
vnd ? "vnd" : "sig", elem_idx, model_idx);
err = -ENOENT;
goto free;
continue;
}
err = model_set_bind(vnd, model, model_key);
if (err) {
goto free;
}
err = model_set_sub(vnd, model, model_key);
if (err) {
goto free;
}
err = model_set_pub(vnd, model, model_key);
if (err) {
goto free;
}
model_set_bind(vnd, model, model_key);
model_set_sub(vnd, model, model_key);
model_set_pub(vnd, model, model_key);
}
free:
bt_mesh_free_buf(buf);
return err;
return 0;
}
static int sig_mod_set(const char *name)
@ -773,7 +799,7 @@ static int va_set(const char *name)
err = bt_mesh_load_core_settings(get, (u8_t *)&va, sizeof(va), &exist);
if (err) {
BT_ERR("Failed to load virtual address 0x%04x", index);
goto free;
continue;
}
if (exist == false) {
@ -782,7 +808,7 @@ static int va_set(const char *name)
if (va.ref == 0) {
BT_DBG("Ignore virtual address %s with ref = 0", get);
goto free;
continue;
}
lab = get_label(index);
@ -947,7 +973,7 @@ static int p_net_key_set(const char *name)
err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist);
if (err) {
BT_ERR("Failed to load NetKey 0x%03x", net_idx);
goto free;
continue;
}
if (exist == false) {
@ -1007,7 +1033,7 @@ static int p_app_key_set(const char *name)
err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist);
if (err) {
BT_ERR("Failed to load AppKey 0x%03x", app_idx);
goto free;
continue;
}
if (exist == false) {
@ -1017,8 +1043,7 @@ static int p_app_key_set(const char *name)
sub = bt_mesh_provisioner_subnet_get(key.net_idx);
if (!sub) {
BT_ERR("Failed to find subnet 0x%03x", key.net_idx);
err = -ENOENT;
goto free;
continue;
}
app = bt_mesh_provisioner_app_key_find(app_idx);
@ -1188,43 +1213,44 @@ const struct bt_mesh_setting {
const char *name;
int (*func)(const char *name);
} settings[] = {
{ "mesh/role", role_set },
{ "mesh/net", net_set },
{ "mesh/iv", iv_set },
{ "mesh/seq", seq_set },
{ "mesh/rpl", rpl_set },
{ "mesh/netkey", net_key_set },
{ "mesh/appkey", app_key_set },
{ "mesh/hb_pub", hb_pub_set },
{ "mesh/cfg", cfg_set },
{ "mesh/sig", sig_mod_set },
{ "mesh/vnd", vnd_mod_set },
{ "mesh/role", role_set }, /* For Node & Provisioner */
{ "mesh/net", net_set }, /* For Node */
{ "mesh/iv", iv_set }, /* For Node & Provisioner */
{ "mesh/seq", seq_set }, /* For Node & Provisioner */
{ "mesh/rpl", rpl_set }, /* For Node & Provisioner */
{ "mesh/netkey", net_key_set }, /* For Node */
{ "mesh/appkey", app_key_set }, /* For Node */
{ "mesh/hb_pub", hb_pub_set }, /* For Node */
{ "mesh/cfg", cfg_set }, /* For Node */
{ "mesh/sig", sig_mod_set }, /* For Node & Provisioner */
{ "mesh/vnd", vnd_mod_set }, /* For Node & Provisioner */
#if CONFIG_BLE_MESH_LABEL_COUNT > 0
{ "mesh/vaddr", va_set },
{ "mesh/vaddr", va_set }, /* For Node */
#endif
#if CONFIG_BLE_MESH_PROVISIONER
{ "mesh/p_prov", p_prov_set },
{ "mesh/p_netidx", p_net_idx_set },
{ "mesh/p_appidx", p_app_idx_set },
{ "mesh/p_netkey", p_net_key_set },
{ "mesh/p_appkey", p_app_key_set },
{ "mesh/p_node", p_node_set },
{ "mesh/p_prov", p_prov_set }, /* For Provisioner */
{ "mesh/p_netidx", p_net_idx_set }, /* For Provisioner */
{ "mesh/p_appidx", p_app_idx_set }, /* For Provisioner */
{ "mesh/p_netkey", p_net_key_set }, /* For Provisioner */
{ "mesh/p_appkey", p_app_key_set }, /* For Provisioner */
{ "mesh/p_node", p_node_set }, /* For Provisioner */
#endif
};
/**
* For Provisioner, the load operation needs the following actions:
* role_set: Need, restore the device role
* net_set: Not needed
* iv_set: Need, although Provisioner will do some initialization of IV Index
* during startup, but we need to restore the last IV Index status
* iv_set: Need, restore the last IV Index status
* seq_set: Need, restore the previous sequence number
* rpl_set: Need, restore the previous Replay Protection List
* net_key_set: Need, restore the previous network keys
* app_key_set: Need, restore the previous application keys
* net_key_set: Not needed
* app_key_set: Not needed
* hb_pub_set: Not needed currently
* cfg_set: Not needed currently
* sig_mod_set: Need, restore SIG models related info (app, sub, pub)
* vnd_mod_set: Need, restore vendor models related info (app, sub, pub)
* va_set: Not needed currently
*/
int settings_core_load(void)
{
@ -1237,7 +1263,8 @@ int settings_core_load(void)
!strcmp(settings[i].name, "mesh/netkey") ||
!strcmp(settings[i].name, "mesh/appkey") ||
!strcmp(settings[i].name, "mesh/hb_pub") ||
!strcmp(settings[i].name, "mesh/cfg")) &&
!strcmp(settings[i].name, "mesh/cfg") ||
!strcmp(settings[i].name, "mesh/vaddr")) &&
(!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioner())) {
BT_DBG("Not restoring %s for Provisioner", settings[i].name);
continue;
@ -1257,15 +1284,15 @@ int settings_core_load(void)
settings[i].func(settings[i].name);
if (!strcmp(settings[i].name, "mesh/role")) {
u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & DEVICE_ROLE_BITS;
u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK;
switch (role) {
case 0U:
case BLE_MESH_SETTINGS_ROLE_NONE:
BT_INFO("Mesh device just starts up, no restore");
return 0;
case BIT(BLE_MESH_NODE):
case BLE_MESH_SETTINGS_ROLE_NODE:
BT_INFO("Restored mesh device role: Node");
break;
case BIT(BLE_MESH_PROVISIONER):
case BLE_MESH_SETTINGS_ROLE_PROV:
BT_INFO("Restored mesh device role: Provisioner");
break;
default:
@ -1330,13 +1357,13 @@ int settings_core_commit(void)
#if defined(CONFIG_BLE_MESH_NODE)
if (bt_mesh_is_node()) {
BT_INFO("sub[0].net_idx 0x%03x", bt_mesh.sub[0].net_idx);
if (bt_mesh.sub[0].net_idx == BLE_MESH_KEY_UNUSED) {
/* Nothing to do since we're not yet provisioned */
return 0;
}
BT_INFO("Settings commit, sub[0].net_idx 0x%03x", bt_mesh.sub[0].net_idx);
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
bt_mesh_proxy_server_prov_disable(true);
}
@ -1363,7 +1390,7 @@ int settings_core_commit(void)
return 0;
}
BT_INFO("p_sub[0]->net_idx 0x%03x", bt_mesh.p_sub[0]->net_idx);
BT_INFO("Settings commit, p_sub[0]->net_idx 0x%03x", bt_mesh.p_sub[0]->net_idx);
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
sub = bt_mesh.p_sub[i];
@ -1380,11 +1407,13 @@ int settings_core_commit(void)
}
#endif /* CONFIG_BLE_MESH_PROVISIONER */
if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) {
k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT);
}
if (bt_mesh_is_node() || bt_mesh_is_provisioner()) {
if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) {
k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT);
}
bt_mesh_model_foreach(commit_model, NULL);
bt_mesh_model_foreach(commit_model, NULL);
}
#if defined(CONFIG_BLE_MESH_NODE)
if (bt_mesh_is_node()) {
@ -1450,7 +1479,7 @@ static void schedule_store(int flag)
return;
}
BT_INFO("Waiting %d seconds", timeout / MSEC_PER_SEC);
BT_INFO("Settings store, waiting %d seconds", timeout / MSEC_PER_SEC);
if (timeout) {
k_delayed_work_submit(&pending_store, timeout);
@ -1459,16 +1488,10 @@ static void schedule_store(int flag)
}
}
static void clear_iv(void)
{
BT_DBG("Clearing IV");
bt_mesh_save_core_settings("mesh/iv", NULL, 0);
}
static void clear_net(void)
{
BT_DBG("Clearing Network");
bt_mesh_save_core_settings("mesh/net", NULL, 0);
bt_mesh_erase_core_settings("mesh/net");
}
static void store_pending_net(void)
@ -1486,7 +1509,7 @@ static void store_pending_net(void)
void bt_mesh_store_role(void)
{
BT_DBG("Store, device role %lu", bt_mesh_atomic_get(bt_mesh.flags) & DEVICE_ROLE_BITS);
BT_DBG("Store, device role %lu", bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK);
bt_mesh_save_core_settings("mesh/role", (const u8_t *)bt_mesh.flags, sizeof(bt_mesh.flags));
}
@ -1519,7 +1542,8 @@ void bt_mesh_store_iv(bool only_duration)
void bt_mesh_clear_iv(void)
{
clear_iv();
BT_DBG("Clearing IV");
bt_mesh_erase_core_settings("mesh/iv");
}
static void store_pending_seq(void)
@ -1543,7 +1567,8 @@ void bt_mesh_store_seq(void)
void bt_mesh_clear_seq(void)
{
bt_mesh_save_core_settings("mesh/seq", NULL, 0);
BT_DBG("Clearing Seq");
bt_mesh_erase_core_settings("mesh/seq");
}
static void store_rpl(struct bt_mesh_rpl *entry)
@ -1582,11 +1607,9 @@ static void clear_rpl(void)
BT_DBG("%s", __func__);
bt_mesh_rpl_clear();
buf = bt_mesh_get_core_settings_item("mesh/rpl");
if (!buf) {
bt_mesh_save_core_settings("mesh/rpl", NULL, 0);
bt_mesh_erase_core_settings("mesh/rpl");
return;
}
@ -1594,11 +1617,17 @@ static void clear_rpl(void)
for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
src = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(src)) {
BT_ERR("Invalid source address 0x%04x", src);
continue;
}
sprintf(name, "mesh/rpl/%04x", src);
bt_mesh_save_core_settings(name, NULL, 0);
bt_mesh_erase_core_settings(name);
}
bt_mesh_save_core_settings("mesh/rpl", NULL, 0);
bt_mesh_erase_core_settings("mesh/rpl");
bt_mesh_free_buf(buf);
return;
@ -1626,7 +1655,7 @@ static void store_pending_hb_pub(void)
struct hb_pub_val val = {0};
if (!hb_pub) {
BT_WARN("NULL heartbeat publication");
BT_ERR("Invalid heartbeat publication");
return;
}
@ -1640,6 +1669,12 @@ static void store_pending_hb_pub(void)
bt_mesh_save_core_settings("mesh/hb_pub", (const u8_t *)&val, sizeof(val));
}
static void clear_hb_pub(void)
{
BT_DBG("Clear heartbeat publication");
bt_mesh_erase_core_settings("mesh/hb_pub");
}
static void store_pending_cfg(void)
{
struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get();
@ -1664,7 +1699,7 @@ static void store_pending_cfg(void)
static void clear_cfg(void)
{
BT_DBG("Clearing configuration");
bt_mesh_save_core_settings("mesh/cfg", NULL, 0);
bt_mesh_erase_core_settings("mesh/cfg");
}
static void clear_app_key(u16_t app_idx)
@ -1675,7 +1710,7 @@ static void clear_app_key(u16_t app_idx)
BT_DBG("AppKeyIndex 0x%03x", app_idx);
sprintf(name, "mesh/ak/%04x", app_idx);
bt_mesh_save_core_settings(name, NULL, 0);
bt_mesh_erase_core_settings(name);
err = bt_mesh_remove_core_settings_item("mesh/appkey", app_idx);
if (err) {
@ -1693,7 +1728,7 @@ static void clear_net_key(u16_t net_idx)
BT_DBG("NetKeyIndex 0x%03x", net_idx);
sprintf(name, "mesh/nk/%04x", net_idx);
bt_mesh_save_core_settings(name, NULL, 0);
bt_mesh_erase_core_settings(name);
err = bt_mesh_remove_core_settings_item("mesh/netkey", net_idx);
if (err) {
@ -1806,15 +1841,8 @@ static void store_pending_mod_bind(struct bt_mesh_model *model, bool vnd)
int err = 0;
model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key);
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
!bt_mesh_is_provisioned()) {
bt_mesh_save_core_settings(name, NULL, 0);
return;
}
err = bt_mesh_save_core_settings(name, (const u8_t *)model->keys, sizeof(model->keys));
if (err) {
BT_ERR("Failed to store %s", name);
@ -1837,15 +1865,8 @@ static void store_pending_mod_sub(struct bt_mesh_model *model, bool vnd)
int err = 0;
model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key);
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
!bt_mesh_is_provisioned()) {
bt_mesh_save_core_settings(name, NULL, 0);
return;
}
err = bt_mesh_save_core_settings(name, (const u8_t *)model->groups, sizeof(model->groups));
if (err) {
BT_ERR("Failed to store %s", name);
@ -1873,6 +1894,9 @@ static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd)
return;
}
model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key);
pub.addr = model->pub->addr;
pub.key = model->pub->key;
pub.ttl = model->pub->ttl;
@ -1881,16 +1905,6 @@ static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd)
pub.period_div = model->pub->period_div;
pub.cred = model->pub->cred;
model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key);
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
!bt_mesh_is_provisioned()) {
bt_mesh_save_core_settings(name, NULL, 0);
return;
}
err = bt_mesh_save_core_settings(name, (const u8_t *)&pub, sizeof(pub));
if (err) {
BT_ERR("Failed to store %s", name);
@ -1930,6 +1944,66 @@ static void store_pending_mod(struct bt_mesh_model *model,
}
}
static void clear_mod_bind(struct bt_mesh_model *model, bool vnd)
{
char name[16] = {'\0'};
u16_t model_key = 0U;
model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key);
bt_mesh_erase_core_settings(name);
bt_mesh_remove_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key);
}
static void clear_mod_sub(struct bt_mesh_model *model, bool vnd)
{
char name[16] = {'\0'};
u16_t model_key = 0U;
model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key);
bt_mesh_erase_core_settings(name);
bt_mesh_remove_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key);
}
static void clear_mod_pub(struct bt_mesh_model *model, bool vnd)
{
char name[16] = {'\0'};
u16_t model_key = 0U;
model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key);
bt_mesh_erase_core_settings(name);
bt_mesh_remove_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key);
}
static void clear_pending_mod(struct bt_mesh_model *model,
struct bt_mesh_elem *elem, bool vnd,
bool primary, void *user_data)
{
if (!model->flags) {
return;
}
if (model->flags & BLE_MESH_MOD_BIND_PENDING) {
model->flags &= ~BLE_MESH_MOD_BIND_PENDING;
clear_mod_bind(model, vnd);
}
if (model->flags & BLE_MESH_MOD_SUB_PENDING) {
model->flags &= ~BLE_MESH_MOD_SUB_PENDING;
clear_mod_sub(model, vnd);
}
if (model->flags & BLE_MESH_MOD_PUB_PENDING) {
model->flags &= ~BLE_MESH_MOD_PUB_PENDING;
clear_mod_pub(model, vnd);
}
}
#define IS_VA_DEL(_label) ((_label)->ref == 0)
static void store_pending_va(void)
{
@ -1948,7 +2022,7 @@ static void store_pending_va(void)
sprintf(name, "mesh/va/%04x", i);
if (IS_VA_DEL(lab)) {
err = bt_mesh_save_core_settings(name, NULL, 0);
err = bt_mesh_erase_core_settings(name);
} else {
va.ref = lab->ref;
va.addr = lab->addr;
@ -1981,7 +2055,7 @@ static void store_pending(struct k_work *work)
BT_DBG("%s", __func__);
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_RPL_PENDING)) {
if (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioned()) {
if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) {
store_pending_rpl();
} else {
clear_rpl();
@ -2003,20 +2077,28 @@ static void store_pending(struct k_work *work)
}
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IV_PENDING)) {
if (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioned()) {
if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) {
store_pending_iv();
} else {
clear_iv();
bt_mesh_clear_iv();
}
}
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_SEQ_PENDING)) {
store_pending_seq();
if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) {
store_pending_seq();
} else {
bt_mesh_clear_seq();
}
}
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_HB_PUB_PENDING)) {
store_pending_hb_pub();
if (bt_mesh_is_provisioned()) {
store_pending_hb_pub();
} else {
clear_hb_pub();
}
}
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
@ -2029,15 +2111,17 @@ static void store_pending(struct k_work *work)
}
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_MOD_PENDING)) {
bt_mesh_model_foreach(store_pending_mod, NULL);
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
!bt_mesh_is_provisioned()) {
bt_mesh_save_core_settings("mesh/sig", NULL, 0);
bt_mesh_save_core_settings("mesh/vnd", NULL, 0);
if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) {
bt_mesh_model_foreach(store_pending_mod, NULL);
} else {
bt_mesh_model_foreach(clear_pending_mod, NULL);
bt_mesh_erase_core_settings("mesh/sig");
bt_mesh_erase_core_settings("mesh/vnd");
}
}
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_VA_PENDING)) {
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_VA_PENDING)) {
store_pending_va();
}
}
@ -2143,7 +2227,7 @@ void bt_mesh_store_cfg(void)
void bt_mesh_clear_role(void)
{
BT_DBG("Clear device role");
bt_mesh_save_core_settings("mesh/role", NULL, 0);
bt_mesh_erase_core_settings("mesh/role");
}
void bt_mesh_clear_net(void)
@ -2263,35 +2347,8 @@ void bt_mesh_store_prov_info(u16_t primary_addr, u16_t alloc_addr)
void bt_mesh_clear_prov_info(void)
{
bt_mesh_save_core_settings("mesh/p_prov", NULL, 0);
}
static void clear_p_net_key(u16_t net_idx)
{
char name[16] = {'\0'};
int err = 0;
sprintf(name, "mesh/pnk/%04x", net_idx);
bt_mesh_save_core_settings(name, NULL, 0);
err = bt_mesh_remove_core_settings_item("mesh/p_netkey", net_idx);
if (err) {
BT_ERR("Failed to remove 0x%03x from mesh/p_netkey", net_idx);
}
}
static void clear_p_app_key(u16_t app_idx)
{
char name[16] = {'\0'};
int err = 0;
sprintf(name, "mesh/pak/%04x", app_idx);
bt_mesh_save_core_settings(name, NULL, 0);
err = bt_mesh_remove_core_settings_item("mesh/p_appkey", app_idx);
if (err) {
BT_ERR("Failed to remove 0x%03x from mesh/p_appkey", app_idx);
}
BT_DBG("Clearing prov info");
bt_mesh_erase_core_settings("mesh/p_prov");
}
static void store_p_net_key(struct bt_mesh_subnet *sub)
@ -2344,7 +2401,7 @@ static void store_p_app_key(struct bt_mesh_app_key *app)
void bt_mesh_store_p_net_idx(void)
{
BT_DBG("p_net_idx_next 0x%03x", bt_mesh.p_net_idx_next);
BT_DBG("Store, p_net_idx_next 0x%03x", bt_mesh.p_net_idx_next);
bt_mesh_save_core_settings("mesh/p_netidx",
(const u8_t *)&bt_mesh.p_net_idx_next, sizeof(bt_mesh.p_net_idx_next));
@ -2352,12 +2409,13 @@ void bt_mesh_store_p_net_idx(void)
void bt_mesh_clear_p_net_idx(void)
{
bt_mesh_save_core_settings("mesh/p_netidx", NULL, 0);
BT_DBG("Clearing NetKey Index");
bt_mesh_erase_core_settings("mesh/p_netidx");
}
void bt_mesh_store_p_app_idx(void)
{
BT_DBG("p_app_idx_next 0x%03x", bt_mesh.p_app_idx_next);
BT_DBG("Store, p_app_idx_next 0x%03x", bt_mesh.p_app_idx_next);
bt_mesh_save_core_settings("mesh/p_appidx",
(const u8_t *)&bt_mesh.p_app_idx_next, sizeof(bt_mesh.p_app_idx_next));
@ -2365,7 +2423,8 @@ void bt_mesh_store_p_app_idx(void)
void bt_mesh_clear_p_app_idx(void)
{
bt_mesh_save_core_settings("mesh/p_appidx", NULL, 0);
BT_DBG("Clearing AppKey Index");
bt_mesh_erase_core_settings("mesh/p_appidx");
}
void bt_mesh_store_p_subnet(struct bt_mesh_subnet *sub)
@ -2394,28 +2453,36 @@ void bt_mesh_store_p_app_key(struct bt_mesh_app_key *key)
store_p_app_key(key);
}
void bt_mesh_clear_p_subnet(struct bt_mesh_subnet *sub)
void bt_mesh_clear_p_subnet(u16_t net_idx)
{
if (sub == NULL) {
BT_ERR("Invalid subnet");
return;
char name[16] = {'\0'};
int err = 0;
BT_DBG("NetKeyIndex 0x%03x", net_idx);
sprintf(name, "mesh/pnk/%04x", net_idx);
bt_mesh_erase_core_settings(name);
err = bt_mesh_remove_core_settings_item("mesh/p_netkey", net_idx);
if (err) {
BT_ERR("Failed to remove 0x%04x from mesh/p_netkey", net_idx);
}
BT_DBG("NetKeyIndex 0x%03x", sub->net_idx);
clear_p_net_key(sub->net_idx);
}
void bt_mesh_clear_p_app_key(struct bt_mesh_app_key *key)
void bt_mesh_clear_p_app_key(u16_t app_idx)
{
if (key == NULL) {
BT_ERR("Invalid AppKey");
return;
char name[16] = {'\0'};
int err = 0;
BT_DBG("AppKeyIndex 0x%03x", app_idx);
sprintf(name, "mesh/pak/%04x", app_idx);
bt_mesh_erase_core_settings(name);
err = bt_mesh_remove_core_settings_item("mesh/p_appkey", app_idx);
if (err) {
BT_ERR("Failed to remove 0x%04x from mesh/p_appkey", app_idx);
}
BT_DBG("AppKeyIndex 0x%03x", key->app_idx);
clear_p_app_key(key->app_idx);
}
void bt_mesh_clear_rpl_single(u16_t src)
@ -2429,7 +2496,7 @@ void bt_mesh_clear_rpl_single(u16_t src)
}
sprintf(name, "mesh/rpl/%04x", src);
bt_mesh_save_core_settings(name, NULL, 0);
bt_mesh_erase_core_settings(name);
err = bt_mesh_remove_core_settings_item("mesh/rpl", src);
if (err) {
@ -2479,15 +2546,15 @@ static void clear_node(u16_t addr)
/* Clear node information */
sprintf(name, "mesh/pn/%04x/i", addr);
bt_mesh_save_core_settings(name, NULL, 0);
bt_mesh_erase_core_settings(name);
/* Clear node name */
sprintf(name, "mesh/pn/%04x/n", addr);
bt_mesh_save_core_settings(name, NULL, 0);
bt_mesh_erase_core_settings(name);
/* Clear node composition data */
sprintf(name, "mesh/pn/%04x/c", addr);
bt_mesh_save_core_settings(name, NULL, 0);
bt_mesh_erase_core_settings(name);
err = bt_mesh_remove_core_settings_item("mesh/p_node", addr);
if (err) {
@ -2547,33 +2614,34 @@ void bt_mesh_store_node_comp_data(struct bt_mesh_node *node)
int settings_core_init(void)
{
BT_DBG("%s", __func__);
k_delayed_work_init(&pending_store, store_pending);
return 0;
}
int bt_mesh_settings_init(void)
{
BT_DBG("%s", __func__);
bt_mesh_settings_foreach();
return 0;
}
int settings_core_deinit(void)
{
k_delayed_work_free(&pending_store);
return 0;
}
int bt_mesh_settings_deinit(void)
int settings_core_erase(void)
{
bt_mesh_settings_deforeach();
/* Erase here must not use the pending_store timer. */
bt_mesh_clear_role();
return 0;
}
int bt_mesh_settings_init(void)
{
bt_mesh_settings_mutex_new();
bt_mesh_settings_init_foreach();
return 0;
}
int bt_mesh_settings_deinit(bool erase)
{
bt_mesh_settings_deinit_foreach(erase);
bt_mesh_settings_mutex_free();
return 0;
}

View file

@ -14,10 +14,10 @@
extern "C" {
#endif
int settings_core_init(void);
int settings_core_load(void);
int settings_core_commit(void);
int settings_core_deinit(void);
#define BLE_MESH_SETTINGS_ROLE_NONE 0
#define BLE_MESH_SETTINGS_ROLE_NODE (BIT(BLE_MESH_NODE))
#define BLE_MESH_SETTINGS_ROLE_PROV (BIT(BLE_MESH_PROVISIONER))
#define BLE_MESH_SETTINGS_ROLE_BIT_MASK (BIT(BLE_MESH_NODE) | BIT(BLE_MESH_PROVISIONER))
void bt_mesh_store_role(void);
void bt_mesh_store_net(void);
@ -50,8 +50,8 @@ void bt_mesh_store_p_app_idx(void);
void bt_mesh_clear_p_app_idx(void);
void bt_mesh_store_p_subnet(struct bt_mesh_subnet *sub);
void bt_mesh_store_p_app_key(struct bt_mesh_app_key *key);
void bt_mesh_clear_p_subnet(struct bt_mesh_subnet *sub);
void bt_mesh_clear_p_app_key(struct bt_mesh_app_key *key);
void bt_mesh_clear_p_subnet(u16_t net_idx);
void bt_mesh_clear_p_app_key(u16_t app_idx);
void bt_mesh_clear_rpl_single(u16_t src);
void bt_mesh_store_node_info(struct bt_mesh_node *node);
void bt_mesh_clear_node_info(u16_t unicast_addr);
@ -59,8 +59,17 @@ 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 settings_core_init(void);
int settings_core_load(void);
int settings_core_commit(void);
int settings_core_deinit(void);
int settings_core_erase(void);
int bt_mesh_settings_init(void);
int bt_mesh_settings_deinit(void);
int bt_mesh_settings_deinit(bool erase);
#ifdef __cplusplus
}

View file

@ -15,9 +15,6 @@
#include <string.h>
#include <errno.h>
#include "nvs.h"
#include "nvs_flash.h"
#include "mesh_common.h"
#include "settings_nvs.h"
#include "settings.h"
@ -26,12 +23,11 @@
enum settings_type {
SETTINGS_CORE,
SETTINGS_SERVER,
};
struct settings_context {
char *nvs_name;
nvs_handle handle;
bt_mesh_nvs_handle_t handle;
int (*settings_init)(void);
int (*settings_load)(void);
@ -47,18 +43,13 @@ static struct settings_context settings_ctx[] = {
.settings_load = settings_core_load,
.settings_commit = settings_core_commit,
.settings_deinit = settings_core_deinit,
},
[SETTINGS_SERVER] = {
.nvs_name = "mesh_server",
.settings_init = NULL,
.settings_load = NULL,
.settings_commit = NULL,
.settings_erase = settings_core_erase,
},
};
/* 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;
@ -101,7 +92,7 @@ void bt_mesh_settings_foreach(void)
}
}
void bt_mesh_settings_deforeach(void)
void bt_mesh_settings_deinit_foreach(bool erase)
{
int i;
@ -113,6 +104,11 @@ void bt_mesh_settings_deforeach(void)
continue;
}
if (erase && ctx->settings_erase && ctx->settings_erase()) {
BT_ERR("Erase settings failed, name %s", ctx->nvs_name);
continue;
}
nvs_close(ctx->handle);
}
@ -123,14 +119,14 @@ void bt_mesh_settings_deforeach(void)
/* API used to get BLE Mesh related nvs handle */
static inline nvs_handle settings_get_nvs_handle(enum settings_type type)
static inline bt_mesh_nvs_handle_t settings_get_nvs_handle(enum settings_type type)
{
return settings_ctx[type].handle;
}
/* API used to store/erase BLE Mesh related settings */
static int settings_save(nvs_handle handle, const char *key, const u8_t *val, size_t len)
static int settings_save(bt_mesh_nvs_handle_t handle, const char *key, const u8_t *val, size_t len)
{
int err = 0;
@ -165,15 +161,35 @@ static int settings_save(nvs_handle handle, const char *key, const u8_t *val, si
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)
{
nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE);
return settings_save(handle, key, val, len);
bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE);
return bt_mesh_save_settings(handle, key, val, len);
}
int bt_mesh_erase_settings(bt_mesh_nvs_handle_t handle, const char *key)
{
return bt_mesh_save_settings(handle, key, NULL, 0);
}
int bt_mesh_erase_core_settings(const char *key)
{
return bt_mesh_save_core_settings(key, NULL, 0);
}
/* API used to load BLE Mesh related settings */
static int settings_load(nvs_handle handle, const char *key,
static int settings_load(bt_mesh_nvs_handle_t handle, const char *key,
u8_t *buf, size_t buf_len, bool *exist)
{
int err = 0;
@ -199,15 +215,25 @@ static int settings_load(nvs_handle 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)
{
nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE);
return settings_load(handle, key, buf, buf_len, exist);
bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE);
return bt_mesh_load_settings(handle, key, buf, buf_len, exist);
}
/* API used to get length of BLE Mesh related settings */
static size_t settings_get_length(nvs_handle handle, const char *key)
static size_t settings_get_length(bt_mesh_nvs_handle_t handle, const char *key)
{
size_t len = 0U;
int err = 0;
@ -233,7 +259,7 @@ static size_t settings_get_length(nvs_handle handle, const char *key)
* Mesh settings.
*/
static struct net_buf_simple *settings_get_item(nvs_handle handle, const char *key)
static struct net_buf_simple *settings_get_item(bt_mesh_nvs_handle_t handle, const char *key)
{
struct net_buf_simple *buf = NULL;
size_t length = 0U;
@ -270,10 +296,19 @@ static struct net_buf_simple *settings_get_item(nvs_handle handle, const char *k
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)
{
nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE);
return settings_get_item(handle, key);
bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE);
return bt_mesh_get_settings_item(handle, key);
}
/* API used to check if the settings item exists */
@ -305,7 +340,7 @@ static bool is_settings_item_exist(struct net_buf_simple *buf, const u16_t val)
/* API used to add the settings item */
static int settings_add_item(nvs_handle handle, const char *key, const u16_t val)
static int settings_add_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val)
{
struct net_buf_simple *store = NULL;
struct net_buf_simple *buf = NULL;
@ -342,15 +377,24 @@ static int settings_add_item(nvs_handle handle, const char *key, const u16_t val
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)
{
nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE);
return settings_add_item(handle, key, val);
bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE);
return bt_mesh_add_settings_item(handle, key, val);
}
/* API used to remove the settings item */
static int settings_remove_item(nvs_handle handle, const char *key, const u16_t val)
static int settings_remove_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val)
{
struct net_buf_simple *store = NULL;
struct net_buf_simple *buf = NULL;
@ -397,10 +441,19 @@ static int settings_remove_item(nvs_handle handle, const char *key, const u16_t
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)
{
nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE);
return settings_remove_item(handle, key, val);
bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE);
return bt_mesh_remove_settings_item(handle, key, val);
}
#endif /* CONFIG_BLE_MESH_SETTINGS */

View file

@ -15,29 +15,42 @@
#ifndef _BLE_MESH_SETTINGS_NVS_H_
#define _BLE_MESH_SETTINGS_NVS_H_
#include "nvs_flash.h"
#include "mesh_buf.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef nvs_handle bt_mesh_nvs_handle_t;
#define SETTINGS_ITEM_SIZE sizeof(u16_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(bool erase);
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_erase_settings(bt_mesh_nvs_handle_t handle, const char *key);
int bt_mesh_erase_core_settings(const char *key);
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

View file

@ -1768,7 +1768,7 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx)
return err;
}
void bt_mesh_rx_reset(void)
void bt_mesh_rx_reset(bool erase)
{
int i;
@ -1778,10 +1778,10 @@ void bt_mesh_rx_reset(void)
seg_rx_reset(&seg_rx[i], true);
}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
(void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl));
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && erase) {
bt_mesh_clear_rpl();
} else {
(void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl));
}
}
@ -1863,16 +1863,7 @@ void bt_mesh_trans_deinit(bool erase)
{
int i;
for (i = 0; i < ARRAY_SIZE(seg_rx); i++) {
seg_rx_reset(&seg_rx[i], true);
}
if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_rpl();
} else {
bt_mesh_rpl_clear();
}
bt_mesh_rx_reset(erase);
bt_mesh_tx_reset();
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
@ -1887,12 +1878,6 @@ void bt_mesh_trans_deinit(bool erase)
bt_mesh_rx_seg_mutex_free();
}
void bt_mesh_rpl_clear(void)
{
BT_DBG("%s", __func__);
(void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl));
}
void bt_mesh_heartbeat_send(void)
{
struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get();

View file

@ -97,7 +97,7 @@ struct bt_mesh_app_key *bt_mesh_app_key_find(u16_t app_idx);
bool bt_mesh_tx_in_progress(void);
void bt_mesh_rx_reset(void);
void bt_mesh_rx_reset(bool erase);
void bt_mesh_tx_reset(void);
void bt_mesh_rx_reset_single(u16_t src);
void bt_mesh_tx_reset_single(u16_t dst);
@ -114,8 +114,6 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx);
void bt_mesh_trans_init(void);
void bt_mesh_trans_deinit(bool erase);
void bt_mesh_rpl_clear(void);
void bt_mesh_heartbeat_send(void);
int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, u16_t app_idx,

View file

@ -10,5 +10,6 @@ CONFIG_BTU_TASK_STACK_SIZE=4512
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_PROVISIONER=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y
CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y

View file

@ -10,5 +10,6 @@ CONFIG_NIMBLE_ENABLED=y
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_PROVISIONER=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y
CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y

View file

@ -18,4 +18,4 @@ CONFIG_BLE_MESH_ADV_BUF_COUNT=100
CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10
CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y
CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y

View file

@ -11,4 +11,5 @@ CONFIG_BTU_TASK_STACK_SIZE=4512
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_FAST_PROV=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_CFG_CLI=y

View file

@ -10,4 +10,5 @@ CONFIG_NIMBLE_ENABLED=y
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_FAST_PROV=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_CFG_CLI=y

View file

@ -22,4 +22,4 @@ CONFIG_BLE_MESH_ADV_BUF_COUNT=200
CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10
CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10
CONFIG_BLE_MESH_TRACE_LEVEL_ERROR=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_CFG_CLI=y

View file

@ -13,4 +13,5 @@ CONFIG_BTU_TASK_STACK_SIZE=4512
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_NODE=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y

View file

@ -10,4 +10,5 @@ CONFIG_NIMBLE_ENABLED=y
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_NODE=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y

View file

@ -12,3 +12,4 @@ CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_NODE=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_FRIEND=y
CONFIG_BLE_MESH_SETTINGS=y

View file

@ -11,3 +11,4 @@ CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_NODE=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_LOW_POWER=y
CONFIG_BLE_MESH_SETTINGS=y

View file

@ -12,5 +12,6 @@ CONFIG_BTU_TASK_STACK_SIZE=4512
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_PROVISIONER=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y

View file

@ -10,5 +10,6 @@ CONFIG_NIMBLE_ENABLED=y
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_PROVISIONER=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y

View file

@ -10,5 +10,6 @@ CONFIG_BTU_TASK_STACK_SIZE=4512
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_PROVISIONER=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_SENSOR_CLI=y

View file

@ -10,5 +10,6 @@ CONFIG_NIMBLE_ENABLED=y
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_PROVISIONER=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_SENSOR_CLI=y

View file

@ -11,3 +11,4 @@ CONFIG_BTU_TASK_STACK_SIZE=4512
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_NODE=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y

View file

@ -10,3 +10,4 @@ CONFIG_NIMBLE_ENABLED=y
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_NODE=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y

View file

@ -10,4 +10,5 @@ CONFIG_BTU_TASK_STACK_SIZE=4512
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_PROVISIONER=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_CFG_CLI=y

View file

@ -10,4 +10,5 @@ CONFIG_NIMBLE_ENABLED=y
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_PROVISIONER=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_CFG_CLI=y
CONFIG_BLE_MESH_SETTINGS=y
CONFIG_BLE_MESH_CFG_CLI=y

View file

@ -10,4 +10,5 @@ CONFIG_BTU_TASK_STACK_SIZE=4512
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_NODE=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y

View file

@ -9,4 +9,5 @@ CONFIG_NIMBLE_ENABLED=y
CONFIG_BLE_MESH=y
CONFIG_BLE_MESH_NODE=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_PB_GATT=y
CONFIG_BLE_MESH_SETTINGS=y