From 7239dc1cbdf09b326aa9ac11605f66418d861792 Mon Sep 17 00:00:00 2001 From: Lingao Meng Date: Mon, 25 May 2020 01:33:58 -0700 Subject: [PATCH] Bluetooth: Mesh: Add key-value concept to store model data Previous mode store function only can store single data, change this to store as KV model, let's app-layer to manager model data, other than by stack when node reset. Signed-off-by: Lingao Meng --- include/bluetooth/mesh/access.h | 12 +++++++--- subsys/bluetooth/mesh/access.h | 3 +-- subsys/bluetooth/mesh/cfg_srv.c | 2 -- subsys/bluetooth/mesh/settings.c | 38 +++++++++++++++++++++----------- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/include/bluetooth/mesh/access.h b/include/bluetooth/mesh/access.h index e59cc5d4822..019c4c5979e 100644 --- a/include/bluetooth/mesh/access.h +++ b/include/bluetooth/mesh/access.h @@ -460,6 +460,7 @@ struct bt_mesh_model_cb { * @sa settings_handler::h_set * * @param model Model to set the persistent data of. + * @param name Name/key of the settings item. * @param len_rd The size of the data found in the backend. * @param read_cb Function provided to read the data from the backend. * @param cb_arg Arguments for the read function provided by the @@ -468,8 +469,8 @@ struct bt_mesh_model_cb { * @return 0 on success, error otherwise. */ int (*const settings_set)(struct bt_mesh_model *model, - size_t len_rd, settings_read_cb read_cb, - void *cb_arg); + const char *name, size_t len_rd, + settings_read_cb read_cb, void *cb_arg); /** @brief Callback called when the mesh is started. * @@ -504,6 +505,9 @@ struct bt_mesh_model_cb { * Called when the mesh node is reset. All model data is deleted on * reset, and the model should clear its state. * + * @note If the model stores any persistent data, this needs to be + * erased manually. + * * @param model Model this callback belongs to. */ void (*const reset)(struct bt_mesh_model *model); @@ -662,13 +666,15 @@ static inline bool bt_mesh_model_in_primary(const struct bt_mesh_model *mod) * * @param mod Mesh model. * @param vnd This is a vendor model. + * @param name Name/key of the settings item. * @param data Model data to store, or NULL to delete any model data. * @param data_len Length of the model data. * * @return 0 on success, or (negative) error code on failure. */ int bt_mesh_model_data_store(struct bt_mesh_model *mod, bool vnd, - const void *data, size_t data_len); + const char *name, const void *data, + size_t data_len); /** @brief Let a model extend another. * diff --git a/subsys/bluetooth/mesh/access.h b/subsys/bluetooth/mesh/access.h index 4ddc6685d2c..a5a78d69ced 100644 --- a/subsys/bluetooth/mesh/access.h +++ b/subsys/bluetooth/mesh/access.h @@ -11,8 +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), - BT_MESH_MOD_NEXT_IS_PARENT = BIT(4), + BT_MESH_MOD_NEXT_IS_PARENT = BIT(3), }; /* Tree walk return codes */ diff --git a/subsys/bluetooth/mesh/cfg_srv.c b/subsys/bluetooth/mesh/cfg_srv.c index feaaae3e7a8..5be9a37da80 100644 --- a/subsys/bluetooth/mesh/cfg_srv.c +++ b/subsys/bluetooth/mesh/cfg_srv.c @@ -3311,8 +3311,6 @@ static void mod_reset(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, if (clear_count) { bt_mesh_store_mod_sub(mod); } - - bt_mesh_model_data_store(mod, vnd, NULL, 0); } if (mod->cb && mod->cb->reset) { diff --git a/subsys/bluetooth/mesh/settings.c b/subsys/bluetooth/mesh/settings.c index 9e6e73abd9c..b34ac8db6ad 100644 --- a/subsys/bluetooth/mesh/settings.c +++ b/subsys/bluetooth/mesh/settings.c @@ -652,6 +652,22 @@ static int mod_set_pub(struct bt_mesh_model *mod, size_t len_rd, return 0; } +static int mod_data_set(struct bt_mesh_model *mod, + const char *name, size_t len_rd, + settings_read_cb read_cb, void *cb_arg) +{ + const char *next; + + settings_name_next(name, &next); + + if (mod->cb && mod->cb->settings_set) { + return mod->cb->settings_set(mod, next, len_rd, + read_cb, cb_arg); + } + + return 0; +} + static int mod_set(bool vnd, const char *name, size_t len_rd, settings_read_cb read_cb, void *cb_arg) { @@ -700,11 +716,7 @@ static int mod_set(bool vnd, const char *name, size_t len_rd, } 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); - } + return mod_data_set(mod, next, len_rd, read_cb, cb_arg); } BT_WARN("Unknown module key %s", next); @@ -2436,22 +2448,22 @@ void bt_mesh_clear_cdb_app_key(struct bt_mesh_cdb_app_key *key) } int bt_mesh_model_data_store(struct bt_mesh_model *mod, bool vnd, - const void *data, size_t data_len) + const char *name, const void *data, + size_t data_len) { - char path[20]; + char path[30]; int err; encode_mod_path(mod, vnd, "data", path, sizeof(path)); + if (name) { + strcat(path, "/"); + strncat(path, name, 8); + } if (data_len) { - mod->flags |= BT_MESH_MOD_DATA_PRESENT; err = settings_save_one(path, data, data_len); - } 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; + err = settings_delete(path); } if (err) {