From 70b5556b0964a15eaad6c40a3c0df5447a8e5485 Mon Sep 17 00:00:00 2001 From: Trond Einar Snekvik Date: Mon, 30 Sep 2019 13:27:02 +0200 Subject: [PATCH] Bluetooth: Mesh: Expose app key get Provides a utility function for getting an application key given a subnet and an app ID. Primary use-case in friendship re-encryption. Signed-off-by: Trond Einar Snekvik --- subsys/bluetooth/mesh/transport.c | 57 ++++++++++++++++++++----------- subsys/bluetooth/mesh/transport.h | 3 ++ 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/subsys/bluetooth/mesh/transport.c b/subsys/bluetooth/mesh/transport.c index 3dee0011750..6e6cad23c84 100644 --- a/subsys/bluetooth/mesh/transport.c +++ b/subsys/bluetooth/mesh/transport.c @@ -485,6 +485,7 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, { const u8_t *key; u8_t *ad; + u8_t aid; int err; if (net_buf_simple_tailroom(msg) < 4) { @@ -500,27 +501,14 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, tx->ctx->app_idx, tx->ctx->addr); BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); - if (tx->ctx->app_idx == BT_MESH_KEY_DEV) { - key = bt_mesh.dev_key; - tx->aid = 0U; - } else { - struct bt_mesh_app_key *app_key; - - app_key = bt_mesh_app_key_find(tx->ctx->app_idx); - if (!app_key) { - return -EINVAL; - } - - if (tx->sub->kr_phase == BT_MESH_KR_PHASE_2 && - app_key->updated) { - key = app_key->keys[1].val; - tx->aid = app_key->keys[1].id; - } else { - key = app_key->keys[0].val; - tx->aid = app_key->keys[0].id; - } + err = bt_mesh_app_key_get(tx->sub, tx->ctx->app_idx, &key, + &aid); + if (err) { + return err; } + tx->aid = aid; + if (!tx->ctx->send_rel || net_buf_simple_tailroom(msg) < 8) { tx->aszmic = 0U; } else { @@ -1600,3 +1588,34 @@ void bt_mesh_heartbeat_send(void) bt_mesh_ctl_send(&tx, TRANS_CTL_OP_HEARTBEAT, &hb, sizeof(hb), NULL, NULL, NULL); } + +int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, u16_t app_idx, + const u8_t **key, u8_t *aid) +{ + struct bt_mesh_app_key *app_key; + + if (app_idx == BT_MESH_KEY_DEV) { + *aid = 0; + *key = bt_mesh.dev_key; + return 0; + } + + if (!subnet) { + return -EINVAL; + } + + app_key = bt_mesh_app_key_find(app_idx); + if (!app_key) { + return -ENOENT; + } + + if (subnet->kr_phase == BT_MESH_KR_PHASE_2 && app_key->updated) { + *key = app_key->keys[1].val; + *aid = app_key->keys[1].id; + } else { + *key = app_key->keys[0].val; + *aid = app_key->keys[0].id; + } + + return 0; +} diff --git a/subsys/bluetooth/mesh/transport.h b/subsys/bluetooth/mesh/transport.h index 68b886a97b3..1c787029c22 100644 --- a/subsys/bluetooth/mesh/transport.h +++ b/subsys/bluetooth/mesh/transport.h @@ -97,3 +97,6 @@ void bt_mesh_trans_init(void); void bt_mesh_rpl_clear(void); void bt_mesh_heartbeat_send(void); + +int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, u16_t app_idx, + const u8_t **key, u8_t *aid);