From f7e780a71943c60eb07e5c8d01e75bbd649fceac Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Mon, 7 May 2018 09:48:36 +0300 Subject: [PATCH] Bluetooth: Mesh: Increase visibility of net & app key helpers These app key and net key (subnet) helpers will soon be needed to be called from the persistent storage code. Signed-off-by: Johan Hedberg --- subsys/bluetooth/host/mesh/adv.c | 2 +- subsys/bluetooth/host/mesh/cfg_cli.c | 1 + subsys/bluetooth/host/mesh/cfg_srv.c | 59 ++++++++++++++----------- subsys/bluetooth/host/mesh/foundation.h | 5 +++ subsys/bluetooth/host/mesh/health_cli.c | 1 + 5 files changed, 40 insertions(+), 28 deletions(-) diff --git a/subsys/bluetooth/host/mesh/adv.c b/subsys/bluetooth/host/mesh/adv.c index a30f13ee259..637159bdebc 100644 --- a/subsys/bluetooth/host/mesh/adv.c +++ b/subsys/bluetooth/host/mesh/adv.c @@ -23,8 +23,8 @@ #include "../hci_core.h" #include "adv.h" -#include "foundation.h" #include "net.h" +#include "foundation.h" #include "beacon.h" #include "prov.h" #include "proxy.h" diff --git a/subsys/bluetooth/host/mesh/cfg_cli.c b/subsys/bluetooth/host/mesh/cfg_cli.c index ab41f79615d..0617c4222c0 100644 --- a/subsys/bluetooth/host/mesh/cfg_cli.c +++ b/subsys/bluetooth/host/mesh/cfg_cli.c @@ -21,6 +21,7 @@ #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG_MODEL) #include "common/log.h" +#include "net.h" #include "foundation.h" #define CID_NVAL 0xffff diff --git a/subsys/bluetooth/host/mesh/cfg_srv.c b/subsys/bluetooth/host/mesh/cfg_srv.c index 2d04b2b4150..02b5dade61c 100644 --- a/subsys/bluetooth/host/mesh/cfg_srv.c +++ b/subsys/bluetooth/host/mesh/cfg_srv.c @@ -354,7 +354,7 @@ static u8_t mod_unbind(struct bt_mesh_model *model, u16_t key_idx) return STATUS_SUCCESS; } -static struct bt_mesh_app_key *app_key_alloc(u16_t app_idx) +struct bt_mesh_app_key *bt_mesh_app_key_alloc(u16_t app_idx) { int i; @@ -428,7 +428,7 @@ static u8_t app_key_set(u16_t net_idx, u16_t app_idx, const u8_t val[16], } } - key = app_key_alloc(app_idx); + key = bt_mesh_app_key_alloc(app_idx); if (!key) { return STATUS_INSUFF_RESOURCES; } @@ -511,7 +511,7 @@ static void _mod_unbind(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, mod_unbind(mod, *key_idx); } -static void _app_key_del(struct bt_mesh_app_key *key) +void bt_mesh_app_key_del(struct bt_mesh_app_key *key) { bt_mesh_model_foreach(_mod_unbind, &key->app_idx); @@ -551,7 +551,7 @@ static void app_key_del(struct bt_mesh_model *model, goto send_status; } - _app_key_del(key); + bt_mesh_app_key_del(key); status = STATUS_SUCCESS; send_status: @@ -2095,9 +2095,8 @@ static void net_key_del(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - struct bt_mesh_cfg_srv *cfg = model->user_data; struct bt_mesh_subnet *sub; - u16_t del_idx, i; + u16_t del_idx; u8_t status; del_idx = net_buf_simple_pull_le16(buf); @@ -2125,26 +2124,7 @@ static void net_key_del(struct bt_mesh_model *model, goto send_status; } - if (cfg->hb_pub.net_idx == del_idx) { - hb_pub_disable(cfg); - } - - /* Delete any app keys bound to this NetKey index */ - 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 == del_idx) { - _app_key_del(key); - } - } - - if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) { - bt_mesh_friend_clear_net_idx(del_idx); - } - - memset(sub, 0, sizeof(*sub)); - sub->net_idx = BT_MESH_KEY_UNUSED; - + bt_mesh_subnet_del(sub); status = STATUS_SUCCESS; send_status: @@ -3148,7 +3128,7 @@ void bt_mesh_cfg_reset(void) struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; if (key->net_idx != BT_MESH_KEY_UNUSED) { - _app_key_del(key); + bt_mesh_app_key_del(key); } } @@ -3280,3 +3260,28 @@ u8_t *bt_mesh_label_uuid_get(u16_t addr) return NULL; } + +void bt_mesh_subnet_del(struct bt_mesh_subnet *sub) +{ + int i; + + if (conf && conf->hb_pub.net_idx == sub->net_idx) { + hb_pub_disable(conf); + } + + /* Delete any app keys bound to this NetKey index */ + 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 == sub->net_idx) { + bt_mesh_app_key_del(key); + } + } + + if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) { + bt_mesh_friend_clear_net_idx(sub->net_idx); + } + + memset(sub, 0, sizeof(*sub)); + sub->net_idx = BT_MESH_KEY_UNUSED; +} diff --git a/subsys/bluetooth/host/mesh/foundation.h b/subsys/bluetooth/host/mesh/foundation.h index 8b1fd13422e..f5459e946b3 100644 --- a/subsys/bluetooth/host/mesh/foundation.h +++ b/subsys/bluetooth/host/mesh/foundation.h @@ -134,6 +134,11 @@ u8_t bt_mesh_beacon_get(void); u8_t bt_mesh_gatt_proxy_get(void); u8_t bt_mesh_default_ttl_get(void); +void bt_mesh_subnet_del(struct bt_mesh_subnet *sub); + +struct bt_mesh_app_key *bt_mesh_app_key_alloc(u16_t app_idx); +void bt_mesh_app_key_del(struct bt_mesh_app_key *key); + #include static inline void key_idx_pack(struct net_buf_simple *buf, diff --git a/subsys/bluetooth/host/mesh/health_cli.c b/subsys/bluetooth/host/mesh/health_cli.c index 24abd180a9e..c3139f5aea3 100644 --- a/subsys/bluetooth/host/mesh/health_cli.c +++ b/subsys/bluetooth/host/mesh/health_cli.c @@ -21,6 +21,7 @@ #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG_MODEL) #include "common/log.h" +#include "net.h" #include "foundation.h" static s32_t msg_timeout = K_SECONDS(2);