From 95bef3694e95a6adfb5ed6f5cfe933ece58971f3 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Mon, 20 Nov 2017 14:51:32 +0200 Subject: [PATCH] Bluetooth: Mesh: Fix node reset There were some things that were working only when receiving a Node Reset message from someone else, but not when the app called bt_mesh_reset() directly. There was also some state cleanup missing for the transport layer. This patch addresses all of these issues. Signed-off-by: Johan Hedberg --- subsys/bluetooth/host/mesh/cfg_srv.c | 60 +++++++++++++++---------- subsys/bluetooth/host/mesh/foundation.h | 2 + subsys/bluetooth/host/mesh/main.c | 8 +++- subsys/bluetooth/host/mesh/transport.c | 15 ++++++- subsys/bluetooth/host/mesh/transport.h | 1 + 5 files changed, 59 insertions(+), 27 deletions(-) diff --git a/subsys/bluetooth/host/mesh/cfg_srv.c b/subsys/bluetooth/host/mesh/cfg_srv.c index 427c4308f5c..570f1a52eb2 100644 --- a/subsys/bluetooth/host/mesh/cfg_srv.c +++ b/subsys/bluetooth/host/mesh/cfg_srv.c @@ -2375,8 +2375,6 @@ static void node_reset(struct bt_mesh_model *model, { /* Needed size: opcode (2 bytes) + msg + MIC */ struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 0 + 4); - struct bt_mesh_cfg_srv *cfg = model->user_data; - int i; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -2392,28 +2390,6 @@ static void node_reset(struct bt_mesh_model *model, BT_ERR("Unable to send Node Reset Status"); } - /* Delete all app keys */ - for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { - struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; - - if (key->net_idx != BT_MESH_KEY_UNUSED) { - _app_key_del(key); - } - } - - for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { - struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; - - if (cfg->hb_pub.net_idx == sub->net_idx) { - hb_pub_disable(cfg); - } - - memset(sub, 0, sizeof(*sub)); - sub->net_idx = BT_MESH_KEY_UNUSED; - } - - memset(labels, 0, sizeof(labels)); - bt_mesh_reset(); } @@ -3048,6 +3024,42 @@ int bt_mesh_cfg_srv_init(struct bt_mesh_model *model, bool primary) return 0; } +void bt_mesh_cfg_reset(void) +{ + struct bt_mesh_cfg_srv *cfg = conf; + int i; + + if (!cfg) { + return; + } + + bt_mesh_set_hb_sub_dst(BT_MESH_ADDR_UNASSIGNED); + + cfg->hb_sub.src = BT_MESH_ADDR_UNASSIGNED; + cfg->hb_sub.dst = BT_MESH_ADDR_UNASSIGNED; + cfg->hb_sub.expiry = 0; + + hb_pub_disable(cfg); + + /* Delete all app keys */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; + + if (key->net_idx != BT_MESH_KEY_UNUSED) { + _app_key_del(key); + } + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + + memset(sub, 0, sizeof(*sub)); + sub->net_idx = BT_MESH_KEY_UNUSED; + } + + memset(labels, 0, sizeof(labels)); +} + void bt_mesh_heartbeat(u16_t src, u16_t dst, u8_t hops, u16_t feat) { struct bt_mesh_cfg_srv *cfg = conf; diff --git a/subsys/bluetooth/host/mesh/foundation.h b/subsys/bluetooth/host/mesh/foundation.h index 9353600f290..9c7065df8f4 100644 --- a/subsys/bluetooth/host/mesh/foundation.h +++ b/subsys/bluetooth/host/mesh/foundation.h @@ -117,6 +117,8 @@ int bt_mesh_health_srv_init(struct bt_mesh_model *model, bool primary); int bt_mesh_cfg_cli_init(struct bt_mesh_model *model, bool primary); +void bt_mesh_cfg_reset(void); + void bt_mesh_heartbeat(u16_t src, u16_t dst, u8_t hops, u16_t feat); void bt_mesh_attention(struct bt_mesh_model *model, u8_t time); diff --git a/subsys/bluetooth/host/mesh/main.c b/subsys/bluetooth/host/mesh/main.c index a9bdeecd75b..69fc1ea5f3f 100644 --- a/subsys/bluetooth/host/mesh/main.c +++ b/subsys/bluetooth/host/mesh/main.c @@ -104,14 +104,18 @@ void bt_mesh_reset(void) bt_mesh.iv_index = 0; bt_mesh.seq = 0; bt_mesh.iv_update = 0; + bt_mesh.pending_update = 0; bt_mesh.valid = 0; bt_mesh.last_update = 0; bt_mesh.ivu_initiator = 0; - bt_mesh_set_hb_sub_dst(BT_MESH_ADDR_UNASSIGNED); - k_delayed_work_cancel(&bt_mesh.ivu_complete); + bt_mesh_cfg_reset(); + + bt_mesh_rx_reset(); + bt_mesh_tx_reset(); + if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) { bt_mesh_lpn_disable(true); } diff --git a/subsys/bluetooth/host/mesh/transport.c b/subsys/bluetooth/host/mesh/transport.c index 58668178e68..8a16ac962ff 100644 --- a/subsys/bluetooth/host/mesh/transport.c +++ b/subsys/bluetooth/host/mesh/transport.c @@ -1343,7 +1343,20 @@ void bt_mesh_rx_reset(void) BT_DBG(""); for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { - seg_rx[i].in_use = 0; + seg_rx_reset(&seg_rx[i]); + seg_rx[i].src = BT_MESH_ADDR_UNASSIGNED; + seg_rx[i].dst = BT_MESH_ADDR_UNASSIGNED; + } +} + +void bt_mesh_tx_reset(void) +{ + int i; + + BT_DBG(""); + + for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { + seg_tx_reset(&seg_tx[i]); } } diff --git a/subsys/bluetooth/host/mesh/transport.h b/subsys/bluetooth/host/mesh/transport.h index 5c46ff4f78c..84ece43b218 100644 --- a/subsys/bluetooth/host/mesh/transport.h +++ b/subsys/bluetooth/host/mesh/transport.h @@ -81,6 +81,7 @@ struct bt_mesh_app_key *bt_mesh_app_key_find(u16_t app_idx); bool bt_mesh_tx_in_progress(void); void bt_mesh_rx_reset(void); +void bt_mesh_tx_reset(void); int bt_mesh_ctl_send(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, size_t data_len, u64_t *seq_auth,