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 <Trond.Einar.Snekvik@nordicsemi.no>
This commit is contained in:
Trond Einar Snekvik 2019-09-16 14:48:11 +02:00 committed by Johan Hedberg
commit b7d05fbf13
4 changed files with 34 additions and 7 deletions

View file

@ -422,6 +422,15 @@ struct bt_mesh_model_cb {
* @return 0 on success, error otherwise. * @return 0 on success, error otherwise.
*/ */
int (*const init)(struct bt_mesh_model *model); 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 */ /** Abstraction that describes a Mesh Model instance */

View file

@ -11,6 +11,7 @@ enum {
BT_MESH_MOD_BIND_PENDING = BIT(0), BT_MESH_MOD_BIND_PENDING = BIT(0),
BT_MESH_MOD_SUB_PENDING = BIT(1), BT_MESH_MOD_SUB_PENDING = BIT(1),
BT_MESH_MOD_PUB_PENDING = BIT(2), 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); void bt_mesh_elem_register(struct bt_mesh_elem *elem, u8_t count);

View file

@ -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 /* Clear model state that isn't otherwise cleared. E.g. AppKey
* binding and model publication is cleared as a consequence * binding and model publication is cleared as a consequence
* of removing all app keys, however model subscription clearing * of removing all app keys, however model subscription and user data
* must be taken care of here. * clearing must be taken care of here.
*/ */
clear_count = mod_sub_list_clear(mod); clear_count = mod_sub_list_clear(mod);
if (IS_ENABLED(CONFIG_BT_SETTINGS) && clear_count) { if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
bt_mesh_store_mod_sub(mod); 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);
} }
} }

View file

@ -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); return mod_set_pub(mod, len_rd, read_cb, cb_arg);
} }
if (!strncmp(next, "data", len) && mod->cb && mod->cb->settings_set) { if (!strncmp(next, "data", len)) {
return mod->cb->settings_set(mod, len_rd, read_cb, cb_arg); 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); 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)); encode_mod_path(mod, vnd, "data", path, sizeof(path));
if (data_len) { if (data_len) {
mod->flags |= BT_MESH_MOD_DATA_PRESENT;
err = settings_save_one(path, data, data_len); 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); err = settings_delete(path);
} else {
/* Nothing to delete */
err = 0;
} }
if (err) { if (err) {