From 31afd189779b66c4880bc41b59a3f2eeb5446b98 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Tue, 8 May 2018 09:21:57 +0300 Subject: [PATCH] Bluetooth: Mesh: Add support for clearing persistent network storage Add support for clearing all data that's currently supported to be persistently stored. Signed-off-by: Johan Hedberg --- subsys/bluetooth/host/mesh/cfg_srv.c | 8 +++++ subsys/bluetooth/host/mesh/main.c | 4 +++ subsys/bluetooth/host/mesh/settings.c | 47 ++++++++++++++++++++++++++ subsys/bluetooth/host/mesh/settings.h | 5 +++ subsys/bluetooth/host/mesh/transport.c | 4 +++ 5 files changed, 68 insertions(+) diff --git a/subsys/bluetooth/host/mesh/cfg_srv.c b/subsys/bluetooth/host/mesh/cfg_srv.c index 23bf70dea49..77d70568360 100644 --- a/subsys/bluetooth/host/mesh/cfg_srv.c +++ b/subsys/bluetooth/host/mesh/cfg_srv.c @@ -521,6 +521,10 @@ void bt_mesh_app_key_del(struct bt_mesh_app_key *key) { bt_mesh_model_foreach(_mod_unbind, &key->app_idx); + if (IS_ENABLED(CONFIG_BT_SETTINGS)) { + bt_mesh_clear_app_key(key); + } + key->net_idx = BT_MESH_KEY_UNUSED; memset(key->keys, 0, sizeof(key->keys)); } @@ -3291,6 +3295,10 @@ void bt_mesh_subnet_del(struct bt_mesh_subnet *sub) bt_mesh_friend_clear_net_idx(sub->net_idx); } + if (IS_ENABLED(CONFIG_BT_SETTINGS)) { + bt_mesh_clear_subnet(sub); + } + memset(sub, 0, sizeof(*sub)); sub->net_idx = BT_MESH_KEY_UNUSED; } diff --git a/subsys/bluetooth/host/mesh/main.c b/subsys/bluetooth/host/mesh/main.c index 79dd48fbc55..2bb5dd55076 100644 --- a/subsys/bluetooth/host/mesh/main.c +++ b/subsys/bluetooth/host/mesh/main.c @@ -108,6 +108,10 @@ void bt_mesh_reset(void) bt_mesh_proxy_gatt_disable(); } + if (IS_ENABLED(CONFIG_BT_SETTINGS)) { + bt_mesh_clear_net(); + } + memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key)); memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); diff --git a/subsys/bluetooth/host/mesh/settings.c b/subsys/bluetooth/host/mesh/settings.c index 45e9e1caff3..453f1f73849 100644 --- a/subsys/bluetooth/host/mesh/settings.c +++ b/subsys/bluetooth/host/mesh/settings.c @@ -683,6 +683,53 @@ void bt_mesh_store_app_key(struct bt_mesh_app_key *app) settings_save_one(path, str); } +void bt_mesh_clear_net(void) +{ + BT_DBG(""); + + settings_save_one("bt/mesh/IV", NULL); + settings_save_one("bt/mesh/Net", NULL); +} + +void bt_mesh_clear_subnet(struct bt_mesh_subnet *sub) +{ + char path[20]; + + BT_DBG("NetKeyIndex 0x%03x", sub->net_idx); + + snprintk(path, sizeof(path), "bt/mesh/NetKey/%x", sub->net_idx); + settings_save_one(path, NULL); +} + +void bt_mesh_clear_app_key(struct bt_mesh_app_key *key) +{ + char path[20]; + + BT_DBG("AppKeyIndex 0x%03x", key->app_idx); + + snprintk(path, sizeof(path), "bt/mesh/AppKey/%x", key->app_idx); + settings_save_one(path, NULL); +} + +void bt_mesh_clear_rpl(void) +{ + int i; + + BT_DBG(""); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; + char path[18]; + + if (!rpl->src) { + continue; + } + + snprintk(path, sizeof(path), "bt/mesh/RPL/%x", rpl->src); + settings_save_one(path, NULL); + } +} + void bt_mesh_settings_init(void) { #if CONFIG_BT_MESH_RPL_STORE_TIMEOUT > 0 diff --git a/subsys/bluetooth/host/mesh/settings.h b/subsys/bluetooth/host/mesh/settings.h index 9892afa9598..c0ff48a9d43 100644 --- a/subsys/bluetooth/host/mesh/settings.h +++ b/subsys/bluetooth/host/mesh/settings.h @@ -11,4 +11,9 @@ void bt_mesh_store_rpl(struct bt_mesh_rpl *rpl); void bt_mesh_store_subnet(struct bt_mesh_subnet *sub); void bt_mesh_store_app_key(struct bt_mesh_app_key *key); +void bt_mesh_clear_net(void); +void bt_mesh_clear_subnet(struct bt_mesh_subnet *sub); +void bt_mesh_clear_app_key(struct bt_mesh_app_key *key); +void bt_mesh_clear_rpl(void); + void bt_mesh_settings_init(void); diff --git a/subsys/bluetooth/host/mesh/transport.c b/subsys/bluetooth/host/mesh/transport.c index e79ed137ac6..2edb3d10764 100644 --- a/subsys/bluetooth/host/mesh/transport.c +++ b/subsys/bluetooth/host/mesh/transport.c @@ -1411,6 +1411,10 @@ void bt_mesh_rx_reset(void) for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { seg_rx_reset(&seg_rx[i], true); } + + if (IS_ENABLED(CONFIG_BT_SETTINGS)) { + bt_mesh_clear_rpl(); + } } void bt_mesh_tx_reset(void)