Skip to content

Commit

Permalink
ble_mesh: stack: Persistent storage misc fixes
Browse files Browse the repository at this point in the history
* Fix the issue that deinit node with "erase_flash"
  set to true, but info is not erased from nvs
* Reuse bt_mesh_cfg_reset() when deinit node
* Optimize Provisioner related erase operations
* No store pending timeout will be used when Node
  is not provisioned OR Provisioner is disabled
  and erase operation is performed
* Change the default timeout for settings operation
  to 0, and rpl store rate to 0
  • Loading branch information
lly committed Oct 13, 2020
1 parent c01f0a0 commit 0c13662
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 76 deletions.
4 changes: 2 additions & 2 deletions components/bt/esp_ble_mesh/Kconfig.in
Expand Up @@ -310,7 +310,7 @@ if BLE_MESH
config BLE_MESH_SEQ_STORE_RATE
int "How often the sequence number gets updated in storage"
range 0 1000000
default 6
default 0
help
This value defines how often the local sequence number gets updated in
persistent storage (i.e. flash). e.g. a value of 100 means that the
Expand All @@ -325,7 +325,7 @@ if BLE_MESH
config BLE_MESH_RPL_STORE_TIMEOUT
int "Minimum frequency that the RPL gets updated in storage"
range 0 1000000
default 5
default 0
help
This value defines in seconds how soon the RPL (Replay Protection List)
gets written to persistent storage after a change occurs. If the node
Expand Down
19 changes: 14 additions & 5 deletions components/bt/esp_ble_mesh/mesh_core/cfg_srv.c
Expand Up @@ -3403,7 +3403,10 @@ static int cfg_srv_deinit(struct bt_mesh_model *model)
return -EINVAL;
}

bt_mesh_cfg_reset();
/* Use "false" here because if cfg needs to be erased,
* it will already be erased in the ble_mesh_deinit().
*/
bt_mesh_cfg_reset(false);

k_delayed_work_free(&cfg->hb_pub.timer);
cfg->hb_pub.dst = BLE_MESH_ADDR_UNASSIGNED;
Expand All @@ -3421,6 +3424,7 @@ const struct bt_mesh_model_cb bt_mesh_cfg_srv_cb = {
static void mod_reset(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
bool vnd, bool primary, void *user_data)
{
bool store = *(bool *)user_data;
size_t clear_count = 0U;

/* Clear model state that isn't otherwise cleared. E.g. AppKey
Expand All @@ -3431,12 +3435,17 @@ static void mod_reset(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,

clear_count = mod_sub_list_clear(mod);

if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && clear_count) {
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && clear_count && store) {
bt_mesh_store_mod_sub(mod);
}
}

void bt_mesh_cfg_reset(void)
void bt_mesh_mod_sub_reset(bool store)
{
bt_mesh_model_foreach(mod_reset, &store);
}

void bt_mesh_cfg_reset(bool store)
{
struct bt_mesh_cfg_srv *cfg = conf;
int i;
Expand All @@ -3460,11 +3469,11 @@ void bt_mesh_cfg_reset(void)
struct bt_mesh_subnet *sub = &bt_mesh.sub[i];

if (sub->net_idx != BLE_MESH_KEY_UNUSED) {
bt_mesh_subnet_del(sub, true);
bt_mesh_subnet_del(sub, store);
}
}

bt_mesh_model_foreach(mod_reset, NULL);
bt_mesh_mod_sub_reset(store);

(void)memset(labels, 0, sizeof(labels));
}
Expand Down
4 changes: 3 additions & 1 deletion components/bt/esp_ble_mesh/mesh_core/foundation.h
Expand Up @@ -132,7 +132,9 @@ struct label {
bt_mesh_atomic_t flags[1];
};

void bt_mesh_cfg_reset(void);
void bt_mesh_mod_sub_reset(bool store);

void bt_mesh_cfg_reset(bool store);

void bt_mesh_heartbeat(u16_t src, u16_t dst, u8_t hops, u16_t feat);

Expand Down
62 changes: 37 additions & 25 deletions components/bt/esp_ble_mesh/mesh_core/main.c
Expand Up @@ -105,7 +105,7 @@ void bt_mesh_node_reset(void)

k_delayed_work_cancel(&bt_mesh.ivu_timer);

bt_mesh_cfg_reset();
bt_mesh_cfg_reset(true);

bt_mesh_rx_reset(true);
bt_mesh_tx_reset();
Expand All @@ -122,10 +122,6 @@ void bt_mesh_node_reset(void)
bt_mesh_proxy_server_gatt_disable();
}

if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_net();
}

(void)memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key));

bt_mesh_scan_disable();
Expand All @@ -134,6 +130,7 @@ void bt_mesh_node_reset(void)
bt_mesh_comp_unprovision();

if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_net();
bt_mesh_clear_seq();
bt_mesh_clear_role();
}
Expand Down Expand Up @@ -443,23 +440,34 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param)
return -EALREADY;
}

if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) {
if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV)) {
bt_mesh_beacon_disable();
bt_mesh_scan_disable();
}
bt_mesh_scan_disable();
bt_mesh_beacon_disable();

if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) {
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
!bt_mesh_is_provisioned()) {
bt_mesh_proxy_server_prov_disable(true);
}

if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
bt_mesh_is_provisioned()) {
bt_mesh_proxy_server_gatt_disable();
}

if (bt_mesh_is_provisioned()) {
/* Clear valid flag here in order to perform settings erase */
bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID);

bt_mesh_cfg_reset(param->erase);
}
}

if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) {
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
bt_mesh_proxy_client_prov_disable();
}

bt_mesh_scan_disable();
/* Clear valid flag here in order to perform settings erase */
bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);
}

Expand All @@ -470,16 +478,22 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param)
return err;
}
}

if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) {
err = bt_mesh_provisioner_prov_deinit(param->erase);
if (err) {
return err;
}

err = bt_mesh_provisioner_deinit(param->erase);
if (err) {
return err;
}
}
}

bt_mesh_trans_deinit(param->erase);
bt_mesh_net_deinit(param->erase);
bt_mesh_net_deinit();

bt_mesh_beacon_deinit();

Expand All @@ -497,13 +511,6 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param)

bt_mesh_gatt_deinit();

if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) {
err = bt_mesh_provisioner_deinit(param->erase);
if (err) {
return err;
}
}

if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
bt_mesh_friend_deinit();
}
Expand All @@ -514,16 +521,18 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param)

bt_mesh_adv_deinit();

if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_settings_deinit(param->erase);
}

err = bt_mesh_comp_deregister();
if (err) {
return err;
}

bt_mesh_comp_unprovision();

if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_settings_deinit(param->erase);
}
memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));

bt_mesh_timer_deinit();

Expand Down Expand Up @@ -568,6 +577,11 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)
}
}

/* Enable Provisioner here, because during the following net
* creation, some information needs to be stored in flash.
*/
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);

err = bt_mesh_provisioner_net_create();
if (err) {
BT_ERR("Failed to create network");
Expand Down Expand Up @@ -603,8 +617,6 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)
bt_mesh_proxy_client_prov_enable();
}

bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);

if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
bt_mesh_friend_init();
}
Expand Down
9 changes: 1 addition & 8 deletions components/bt/esp_ble_mesh/mesh_core/net.c
Expand Up @@ -1557,7 +1557,7 @@ void bt_mesh_net_init(void)
k_work_init(&bt_mesh.local_work, bt_mesh_net_local);
}

void bt_mesh_net_deinit(bool erase)
void bt_mesh_net_deinit(void)
{
k_delayed_work_free(&bt_mesh.ivu_timer);

Expand All @@ -1579,11 +1579,4 @@ void bt_mesh_net_deinit(bool erase)

bt_mesh.iv_index = 0U;
bt_mesh.seq = 0U;

memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));

if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_seq();
bt_mesh_clear_iv();
}
}
2 changes: 1 addition & 1 deletion components/bt/esp_ble_mesh/mesh_core/net.h
Expand Up @@ -375,7 +375,7 @@ u32_t bt_mesh_next_seq(void);
void bt_mesh_net_start(void);

void bt_mesh_net_init(void);
void bt_mesh_net_deinit(bool erase);
void bt_mesh_net_deinit(void);

void bt_mesh_net_header_parse(struct net_buf_simple *buf,
struct bt_mesh_net_rx *rx);
Expand Down

0 comments on commit 0c13662

Please sign in to comment.