From e653a39ed722040587e924997efc6431812dd4ce Mon Sep 17 00:00:00 2001 From: alperen sener Date: Mon, 18 Nov 2024 14:17:11 +0100 Subject: [PATCH] Bluetooth: Mesh: Check that required models exists on the same element Referring to MshDFU_v1.0 Sections 6.1.1, 6.2.1 and 7.1.1 model descriptions: DFU/DFD server/clients extend BLOB Transfer root models and DFD server requires Firmware Update Client on the same element. For this reason we need to make sure that those main models or root models exist on the same element. And also firmware update client can not be forced to be in the first element. For all model extention call return the error code in case of an error. Signed-off-by: alperen sener --- subsys/bluetooth/mesh/dfd_srv.c | 26 +++++++++++++++++++++++-- subsys/bluetooth/mesh/dfu_cli.c | 15 +++++++++----- subsys/bluetooth/mesh/dfu_srv.c | 15 ++++++++++++-- subsys/bluetooth/mesh/priv_beacon_srv.c | 7 ++++++- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/subsys/bluetooth/mesh/dfd_srv.c b/subsys/bluetooth/mesh/dfd_srv.c index 4a395a175bd..1b640739e50 100644 --- a/subsys/bluetooth/mesh/dfd_srv.c +++ b/subsys/bluetooth/mesh/dfd_srv.c @@ -925,12 +925,34 @@ const struct bt_mesh_blob_srv_cb _bt_mesh_dfd_srv_blob_cb = { static int dfd_srv_init(const struct bt_mesh_model *mod) { + int err; struct bt_mesh_dfd_srv *srv = mod->rt->user_data; srv->mod = mod; - if (IS_ENABLED(CONFIG_BT_MESH_MODEL_EXTENSIONS)) { - bt_mesh_model_extend(mod, srv->upload.blob.mod); + const struct bt_mesh_model *blob_srv = + bt_mesh_model_find(bt_mesh_model_elem(mod), BT_MESH_MODEL_ID_BLOB_SRV); + + if (blob_srv == NULL) { + LOG_ERR("Missing BLOB Srv."); + return -EINVAL; + } + + /** BLOB client also shall be present on the same element, but it is already checked by + * initiation of dfu client which we check here. + */ + const struct bt_mesh_model *dfu_cli = + bt_mesh_model_find(bt_mesh_model_elem(mod), BT_MESH_MODEL_ID_DFU_CLI); + + if (dfu_cli == NULL) { + LOG_ERR("Missing FU Cli."); + return -EINVAL; + } + + err = bt_mesh_model_extend(mod, srv->upload.blob.mod); + + if (err) { + return err; } return 0; diff --git a/subsys/bluetooth/mesh/dfu_cli.c b/subsys/bluetooth/mesh/dfu_cli.c index f02b884d222..1c336b19d3d 100644 --- a/subsys/bluetooth/mesh/dfu_cli.c +++ b/subsys/bluetooth/mesh/dfu_cli.c @@ -963,17 +963,22 @@ const struct bt_mesh_model_op _bt_mesh_dfu_cli_op[] = { static int dfu_cli_init(const struct bt_mesh_model *mod) { + int err; struct bt_mesh_dfu_cli *cli = mod->rt->user_data; + cli->mod = mod; - if (mod->rt->elem_idx != 0) { - LOG_ERR("DFU update client must be instantiated on first elem"); + const struct bt_mesh_model *blob_cli = + bt_mesh_model_find(bt_mesh_model_elem(mod), BT_MESH_MODEL_ID_BLOB_CLI); + + if (blob_cli == NULL) { + LOG_ERR("Missing BLOB Cli."); return -EINVAL; } - cli->mod = mod; + err = bt_mesh_model_extend(mod, cli->blob.mod); - if (IS_ENABLED(CONFIG_BT_MESH_MODEL_EXTENSIONS)) { - bt_mesh_model_extend(mod, cli->blob.mod); + if (err) { + return err; } k_sem_init(&cli->req.sem, 0, 1); diff --git a/subsys/bluetooth/mesh/dfu_srv.c b/subsys/bluetooth/mesh/dfu_srv.c index 0cb4b5e524e..694a812322c 100644 --- a/subsys/bluetooth/mesh/dfu_srv.c +++ b/subsys/bluetooth/mesh/dfu_srv.c @@ -440,6 +440,7 @@ const struct bt_mesh_model_op _bt_mesh_dfu_srv_op[] = { static int dfu_srv_init(const struct bt_mesh_model *mod) { + int err; struct bt_mesh_dfu_srv *srv = mod->rt->user_data; srv->mod = mod; @@ -451,8 +452,18 @@ static int dfu_srv_init(const struct bt_mesh_model *mod) return -EINVAL; } - if (IS_ENABLED(CONFIG_BT_MESH_MODEL_EXTENSIONS)) { - bt_mesh_model_extend(mod, srv->blob.mod); + const struct bt_mesh_model *blob_srv = + bt_mesh_model_find(bt_mesh_model_elem(mod), BT_MESH_MODEL_ID_BLOB_SRV); + + if (blob_srv == NULL) { + LOG_ERR("Missing BLOB Srv."); + return -EINVAL; + } + + err = bt_mesh_model_extend(mod, srv->blob.mod); + + if (err) { + return err; } return 0; diff --git a/subsys/bluetooth/mesh/priv_beacon_srv.c b/subsys/bluetooth/mesh/priv_beacon_srv.c index 93404a942ff..8dd1ba3b1d8 100644 --- a/subsys/bluetooth/mesh/priv_beacon_srv.c +++ b/subsys/bluetooth/mesh/priv_beacon_srv.c @@ -195,6 +195,7 @@ const struct bt_mesh_model_op bt_mesh_priv_beacon_srv_op[] = { static int priv_beacon_srv_init(const struct bt_mesh_model *mod) { + int err; const struct bt_mesh_model *config_srv = bt_mesh_model_find(bt_mesh_model_elem(mod), BT_MESH_MODEL_ID_CFG_SRV); @@ -206,7 +207,11 @@ static int priv_beacon_srv_init(const struct bt_mesh_model *mod) priv_beacon_srv = mod; mod->keys[0] = BT_MESH_KEY_DEV_LOCAL; - bt_mesh_model_extend(mod, config_srv); + err = bt_mesh_model_extend(mod, config_srv); + + if (err) { + return err; + } return 0; }