From f317241b89f62d0a1593c792ef0653cb76f3c0e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Thu, 5 Nov 2020 11:55:37 +0100 Subject: [PATCH] mesh: Fail init on model init error Adds propagation of error returns from the model init callbacks in Access, and removing any other checks for successful init in the foundation models. this is port of a385873336e8255dae47ffdd76e6b776f88f7be2 --- nimble/host/mesh/include/mesh/access.h | 5 +++ nimble/host/mesh/src/access.c | 16 +++++--- nimble/host/mesh/src/cfg_srv.c | 57 ++++++++------------------ nimble/host/mesh/src/health_srv.c | 4 -- 4 files changed, 33 insertions(+), 49 deletions(-) diff --git a/nimble/host/mesh/include/mesh/access.h b/nimble/host/mesh/include/mesh/access.h index 920e76d92..a718e588e 100644 --- a/nimble/host/mesh/include/mesh/access.h +++ b/nimble/host/mesh/include/mesh/access.h @@ -460,6 +460,11 @@ struct bt_mesh_model_cb { * * Called on every model instance during mesh initialization. * + * + * If any of the model init callbacks return an error, the Mesh + * subsystem initialization will be aborted, and the error will be + * returned to the caller of @ref bt_mesh_init. + * * @param model Model to be initialized. * * @return 0 on success, error otherwise. diff --git a/nimble/host/mesh/src/access.c b/nimble/host/mesh/src/access.c index 6fe7659d6..a4b748867 100644 --- a/nimble/host/mesh/src/access.c +++ b/nimble/host/mesh/src/access.c @@ -285,6 +285,11 @@ static void mod_init(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, bool vnd, bool primary, void *user_data) { int i; + int *err = user_data; + + if (*err) { + return; + } if (mod->pub) { mod->pub->mod = mod; @@ -304,12 +309,14 @@ static void mod_init(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, } if (mod->cb && mod->cb->init) { - mod->cb->init(mod); + *err = mod->cb->init(mod); } } int bt_mesh_comp_register(const struct bt_mesh_comp *comp) { + int err; + /* There must be at least one element */ if (!comp->elem_count) { return -EINVAL; @@ -317,9 +324,10 @@ int bt_mesh_comp_register(const struct bt_mesh_comp *comp) dev_comp = comp; - bt_mesh_model_foreach(mod_init, NULL); + err = 0; + bt_mesh_model_foreach(mod_init, &err); - return 0; + return err; } void bt_mesh_comp_provision(u16_t addr) @@ -345,8 +353,6 @@ void bt_mesh_comp_unprovision(void) BT_DBG(""); dev_primary_addr = BT_MESH_ADDR_UNASSIGNED; - - bt_mesh_model_foreach(mod_init, NULL); } u16_t bt_mesh_primary_addr(void) diff --git a/nimble/host/mesh/src/cfg_srv.c b/nimble/host/mesh/src/cfg_srv.c index 60a70cc16..74b4741d6 100644 --- a/nimble/host/mesh/src/cfg_srv.c +++ b/nimble/host/mesh/src/cfg_srv.c @@ -665,9 +665,7 @@ static void beacon_set(struct bt_mesh_model *model, ctx->net_idx, ctx->app_idx, ctx->addr, buf->om_len, bt_hex(buf->om_data, buf->om_len)); - if (!cfg) { - BT_WARN("No Configuration Server context available"); - } else if (buf->om_data[0] == 0x00 || buf->om_data[0] == 0x01) { + if (buf->om_data[0] == 0x00 || buf->om_data[0] == 0x01) { if (buf->om_data[0] != cfg->beacon) { cfg->beacon = buf->om_data[0]; @@ -730,9 +728,7 @@ static void default_ttl_set(struct bt_mesh_model *model, ctx->net_idx, ctx->app_idx, ctx->addr, buf->om_len, bt_hex(buf->om_data, buf->om_len)); - if (!cfg) { - BT_WARN("No Configuration Server context available"); - } else if (buf->om_data[0] <= BT_MESH_TTL_MAX && buf->om_data[0] != 0x01) { + if (buf->om_data[0] <= BT_MESH_TTL_MAX && buf->om_data[0] != 0x01) { if (cfg->default_ttl != buf->om_data[0]) { cfg->default_ttl = buf->om_data[0]; @@ -866,14 +862,10 @@ static void net_transmit_set(struct bt_mesh_model *model, BT_MESH_TRANSMIT_COUNT(buf->om_data[0]), BT_MESH_TRANSMIT_INT(buf->om_data[0])); - if (!cfg) { - BT_WARN("No Configuration Server context available"); - } else { - cfg->net_transmit = buf->om_data[0]; + cfg->net_transmit = buf->om_data[0]; - if (IS_ENABLED(CONFIG_BT_SETTINGS)) { - bt_mesh_store_cfg(); - } + if (IS_ENABLED(CONFIG_BT_SETTINGS)) { + bt_mesh_store_cfg(); } bt_mesh_model_msg_init(msg, OP_NET_TRANSMIT_STATUS); @@ -919,9 +911,7 @@ static void relay_set(struct bt_mesh_model *model, ctx->net_idx, ctx->app_idx, ctx->addr, buf->om_len, bt_hex(buf->om_data, buf->om_len)); - if (!cfg) { - BT_WARN("No Configuration Server context available"); - } else if (buf->om_data[0] == 0x00 || buf->om_data[0] == 0x01) { + if (buf->om_data[0] == 0x00 || buf->om_data[0] == 0x01) { bool change; if (cfg->relay == BT_MESH_RELAY_NOT_SUPPORTED) { @@ -2783,11 +2773,6 @@ static void friend_set(struct bt_mesh_model *model, return; } - if (!cfg) { - BT_WARN("No Configuration Server context available"); - goto send_status; - } - BT_DBG("Friend 0x%02x -> 0x%02x", cfg->frnd, buf->om_data[0]); if (cfg->frnd == buf->om_data[0]) { @@ -3370,6 +3355,14 @@ static bool conf_is_valid(struct bt_mesh_cfg_srv *cfg) return false; } + if (cfg->frnd > 0x02) { + return false; + } + + if (cfg->gatt_proxy > 0x02) { + return false; + } + if (cfg->beacon > 0x01) { return false; } @@ -3469,10 +3462,6 @@ void bt_mesh_cfg_reset(void) BT_DBG(""); - if (!cfg) { - return; - } - bt_mesh_set_hb_sub_dst(BT_MESH_ADDR_UNASSIGNED); cfg->hb_sub.src = BT_MESH_ADDR_UNASSIGNED; @@ -3499,11 +3488,6 @@ void bt_mesh_heartbeat(u16_t src, u16_t dst, u8_t hops, u16_t feat) { struct bt_mesh_cfg_srv *cfg = conf; - if (!cfg) { - BT_WARN("No configuaration server context available"); - return; - } - if (src != cfg->hb_sub.src || dst != cfg->hb_sub.dst) { BT_WARN("No subscription for received heartbeat"); return; @@ -3550,9 +3534,8 @@ u8_t bt_mesh_relay_get(void) u8_t bt_mesh_friend_get(void) { - BT_DBG("conf %p conf->frnd 0x%02x", conf, conf->frnd); - if (conf) { + BT_DBG("conf %p conf->frnd 0x%02x", conf, conf->frnd); return conf->frnd; } @@ -3616,18 +3599,12 @@ u8_t *bt_mesh_label_uuid_get(u16_t addr) struct bt_mesh_hb_pub *bt_mesh_hb_pub_get(void) { - if (!conf) { - return NULL; - } - return &conf->hb_pub; } void bt_mesh_hb_pub_disable(void) { - if (conf) { - hb_pub_disable(conf); - } + hb_pub_disable(conf); } struct bt_mesh_cfg_srv *bt_mesh_cfg_get(void) @@ -3641,7 +3618,7 @@ void bt_mesh_subnet_del(struct bt_mesh_subnet *sub, bool store) BT_DBG("NetIdx 0x%03x store %u", sub->net_idx, store); - if (conf && conf->hb_pub.net_idx == sub->net_idx) { + if (conf->hb_pub.net_idx == sub->net_idx) { hb_pub_disable(conf); if (IS_ENABLED(CONFIG_BT_SETTINGS) && store) { diff --git a/nimble/host/mesh/src/health_srv.c b/nimble/host/mesh/src/health_srv.c index 16de83a99..7f9cff54b 100644 --- a/nimble/host/mesh/src/health_srv.c +++ b/nimble/host/mesh/src/health_srv.c @@ -389,10 +389,6 @@ static int health_srv_init(struct bt_mesh_model *model) struct bt_mesh_health_srv *srv = model->user_data; if (!srv) { - if (!bt_mesh_model_in_primary(model)) { - return 0; - } - BT_ERR("No Health Server context provided"); return -EINVAL; }