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 <mengabc1086@gmail.com>
This commit is contained in:
Lingao Meng 2020-05-25 01:33:58 -07:00 committed by Carles Cufí
commit 7239dc1cbd
4 changed files with 35 additions and 20 deletions

View file

@ -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.
*

View file

@ -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 */

View file

@ -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) {

View file

@ -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) {