From b7d05fbf1355c7285a98fdc7ec96de6298ec382c Mon Sep 17 00:00:00 2001 From: Trond Einar Snekvik Date: Mon, 16 Sep 2019 14:48:11 +0200 Subject: [PATCH] Bluetooth: Mesh: Add model reset callback Adds additional model callback that gets called on node_reset. Will also erase any user data when this happens. Signed-off-by: Trond Einar Snekvik --- include/bluetooth/mesh/access.h | 9 +++++++++ subsys/bluetooth/mesh/access.h | 1 + subsys/bluetooth/mesh/cfg_srv.c | 16 ++++++++++++---- subsys/bluetooth/mesh/settings.c | 15 ++++++++++++--- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/include/bluetooth/mesh/access.h b/include/bluetooth/mesh/access.h index 90a75da02f3..3a4f284ca91 100644 --- a/include/bluetooth/mesh/access.h +++ b/include/bluetooth/mesh/access.h @@ -422,6 +422,15 @@ struct bt_mesh_model_cb { * @return 0 on success, error otherwise. */ int (*const init)(struct bt_mesh_model *model); + + /** @brief Model reset callback. + * + * Called when the mesh node is reset. All model data is deleted on + * reset, and the model should clear its state. + * + * @param model Model this callback belongs to. + */ + void (*const reset)(struct bt_mesh_model *model); }; /** Abstraction that describes a Mesh Model instance */ diff --git a/subsys/bluetooth/mesh/access.h b/subsys/bluetooth/mesh/access.h index 394b82abb29..e80beec11a2 100644 --- a/subsys/bluetooth/mesh/access.h +++ b/subsys/bluetooth/mesh/access.h @@ -11,6 +11,7 @@ enum { BT_MESH_MOD_BIND_PENDING = BIT(0), BT_MESH_MOD_SUB_PENDING = BIT(1), BT_MESH_MOD_PUB_PENDING = BIT(2), + BT_MESH_MOD_DATA_PRESENT = BIT(3), }; void bt_mesh_elem_register(struct bt_mesh_elem *elem, u8_t count); diff --git a/subsys/bluetooth/mesh/cfg_srv.c b/subsys/bluetooth/mesh/cfg_srv.c index 7a002a4213a..c27abfc1307 100644 --- a/subsys/bluetooth/mesh/cfg_srv.c +++ b/subsys/bluetooth/mesh/cfg_srv.c @@ -3289,14 +3289,22 @@ static void mod_reset(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, /* Clear model state that isn't otherwise cleared. E.g. AppKey * binding and model publication is cleared as a consequence - * of removing all app keys, however model subscription clearing - * must be taken care of here. + * of removing all app keys, however model subscription and user data + * clearing must be taken care of here. */ clear_count = mod_sub_list_clear(mod); - if (IS_ENABLED(CONFIG_BT_SETTINGS) && clear_count) { - bt_mesh_store_mod_sub(mod); + if (IS_ENABLED(CONFIG_BT_SETTINGS)) { + if (clear_count) { + bt_mesh_store_mod_sub(mod); + } + + bt_mesh_model_data_store(mod, vnd, NULL, 0); + } + + if (mod->cb && mod->cb->reset) { + mod->cb->reset(mod); } } diff --git a/subsys/bluetooth/mesh/settings.c b/subsys/bluetooth/mesh/settings.c index a1583491030..25c786db074 100644 --- a/subsys/bluetooth/mesh/settings.c +++ b/subsys/bluetooth/mesh/settings.c @@ -660,8 +660,12 @@ static int mod_set(bool vnd, const char *name, size_t len_rd, return mod_set_pub(mod, len_rd, read_cb, cb_arg); } - if (!strncmp(next, "data", len) && mod->cb && mod->cb->settings_set) { - return mod->cb->settings_set(mod, len_rd, read_cb, cb_arg); + if (!strncmp(next, "data", len)) { + mod->flags |= BT_MESH_MOD_DATA_PRESENT; + + if (mod->cb && mod->cb->settings_set) { + return mod->cb->settings_set(mod, len_rd, read_cb, cb_arg); + } } BT_WARN("Unknown module key %s", next); @@ -1591,9 +1595,14 @@ int bt_mesh_model_data_store(struct bt_mesh_model *mod, bool vnd, encode_mod_path(mod, vnd, "data", path, sizeof(path)); if (data_len) { + mod->flags |= BT_MESH_MOD_DATA_PRESENT; err = settings_save_one(path, data, data_len); - } else { + } else if (mod->flags & BT_MESH_MOD_DATA_PRESENT) { + mod->flags &= ~BT_MESH_MOD_DATA_PRESENT; err = settings_delete(path); + } else { + /* Nothing to delete */ + err = 0; } if (err) {