Skip to content

Commit 364ce88

Browse files
Johan Hedbergjhedberg
authored andcommitted
Bluetooth: Mesh: Add support for sending NetKey Add message
Add configuration client model support for NetKey Add message, as well as a mesh shell command for calling the new API. Signed-off-by: Johan Hedberg <[email protected]>
1 parent 74e1f51 commit 364ce88

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

include/bluetooth/mesh/cfg_cli.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ int bt_mesh_cfg_relay_get(u16_t net_idx, u16_t addr, u8_t *status,
5858
int bt_mesh_cfg_relay_set(u16_t net_idx, u16_t addr, u8_t new_relay,
5959
u8_t new_transmit, u8_t *status, u8_t *transmit);
6060

61+
int bt_mesh_cfg_net_key_add(u16_t net_idx, u16_t addr, u16_t key_net_idx,
62+
const u8_t net_key[16], u8_t *status);
63+
6164
int bt_mesh_cfg_app_key_add(u16_t net_idx, u16_t addr, u16_t key_net_idx,
6265
u16_t key_app_idx, const u8_t app_key[16],
6366
u8_t *status);

subsys/bluetooth/host/mesh/cfg_cli.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,42 @@ static void relay_status(struct bt_mesh_model *model,
137137
k_sem_give(&cli->op_sync);
138138
}
139139

140+
struct net_key_param {
141+
u8_t *status;
142+
u16_t net_idx;
143+
};
144+
145+
static void net_key_status(struct bt_mesh_model *model,
146+
struct bt_mesh_msg_ctx *ctx,
147+
struct net_buf_simple *buf)
148+
{
149+
struct net_key_param *param;
150+
u16_t net_idx, app_idx;
151+
u8_t status;
152+
153+
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
154+
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
155+
bt_hex(buf->data, buf->len));
156+
157+
if (cli->op_pending != OP_NET_KEY_STATUS) {
158+
BT_WARN("Unexpected Net Key Status message");
159+
return;
160+
}
161+
162+
status = net_buf_simple_pull_u8(buf);
163+
key_idx_unpack(buf, &net_idx, &app_idx);
164+
165+
param = cli->op_param;
166+
if (param->net_idx != net_idx) {
167+
BT_WARN("Net Key Status key index does not match");
168+
return;
169+
}
170+
171+
*param->status = status;
172+
173+
k_sem_give(&cli->op_sync);
174+
}
175+
140176
struct app_key_param {
141177
u8_t *status;
142178
u16_t net_idx;
@@ -429,6 +465,7 @@ const struct bt_mesh_model_op bt_mesh_cfg_cli_op[] = {
429465
{ OP_FRIEND_STATUS, 1, friend_status },
430466
{ OP_GATT_PROXY_STATUS, 1, gatt_proxy_status },
431467
{ OP_RELAY_STATUS, 2, relay_status },
468+
{ OP_NET_KEY_STATUS, 3, net_key_status },
432469
{ OP_APP_KEY_STATUS, 4, app_key_status },
433470
{ OP_MOD_APP_STATUS, 7, mod_app_status },
434471
{ OP_MOD_PUB_STATUS, 12, mod_pub_status },
@@ -673,6 +710,44 @@ int bt_mesh_cfg_relay_set(u16_t net_idx, u16_t addr, u8_t new_relay,
673710
return cli_wait(&param, OP_RELAY_STATUS);
674711
}
675712

713+
int bt_mesh_cfg_net_key_add(u16_t net_idx, u16_t addr, u16_t key_net_idx,
714+
const u8_t net_key[16], u8_t *status)
715+
{
716+
struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 18 + 4);
717+
struct bt_mesh_msg_ctx ctx = {
718+
.net_idx = net_idx,
719+
.app_idx = BT_MESH_KEY_DEV,
720+
.addr = addr,
721+
.send_ttl = BT_MESH_TTL_DEFAULT,
722+
};
723+
struct net_key_param param = {
724+
.status = status,
725+
.net_idx = key_net_idx,
726+
};
727+
int err;
728+
729+
err = check_cli();
730+
if (err) {
731+
return err;
732+
}
733+
734+
bt_mesh_model_msg_init(msg, OP_NET_KEY_ADD);
735+
net_buf_simple_add_le16(msg, key_net_idx);
736+
net_buf_simple_add_mem(msg, net_key, 16);
737+
738+
err = bt_mesh_model_send(cli->model, &ctx, msg, NULL, NULL);
739+
if (err) {
740+
BT_ERR("model_send() failed (err %d)", err);
741+
return err;
742+
}
743+
744+
if (!status) {
745+
return 0;
746+
}
747+
748+
return cli_wait(&param, OP_NET_KEY_STATUS);
749+
}
750+
676751
int bt_mesh_cfg_app_key_add(u16_t net_idx, u16_t addr, u16_t key_net_idx,
677752
u16_t key_app_idx, const u8_t app_key[16],
678753
u8_t *status)

subsys/bluetooth/host/mesh/shell.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,44 @@ static int cmd_relay(int argc, char *argv[])
895895
return 0;
896896
}
897897

898+
static int cmd_net_key_add(int argc, char *argv[])
899+
{
900+
u8_t key_val[16];
901+
u16_t key_net_idx;
902+
u8_t status;
903+
int err;
904+
905+
if (argc < 2) {
906+
return -EINVAL;
907+
}
908+
909+
key_net_idx = strtoul(argv[1], NULL, 0);
910+
911+
if (argc > 2) {
912+
size_t len;
913+
914+
len = hex2bin(argv[3], key_val, sizeof(key_val));
915+
memset(key_val, 0, sizeof(key_val) - len);
916+
} else {
917+
memcpy(key_val, default_key, sizeof(key_val));
918+
}
919+
920+
err = bt_mesh_cfg_net_key_add(net.net_idx, net.dst, key_net_idx,
921+
key_val, &status);
922+
if (err) {
923+
printk("Unable to send NetKey Add (err %d)\n", err);
924+
return 0;
925+
}
926+
927+
if (status) {
928+
printk("NetKeyAdd failed with status 0x%02x\n", status);
929+
} else {
930+
printk("NetKey added with NetKey Index 0x%03x\n", key_net_idx);
931+
}
932+
933+
return 0;
934+
}
935+
898936
static int cmd_app_key_add(int argc, char *argv[])
899937
{
900938
u8_t key_val[16];
@@ -1870,6 +1908,7 @@ static const struct shell_cmd mesh_commands[] = {
18701908
{ "friend", cmd_friend, "[val: off, on]" },
18711909
{ "gatt-proxy", cmd_gatt_proxy, "[val: off, on]" },
18721910
{ "relay", cmd_relay, "[val: off, on] [count: 0-7] [interval: 0-32]" },
1911+
{ "net-key-add", cmd_net_key_add, "<NetKeyIndex> [val]" },
18731912
{ "app-key-add", cmd_app_key_add, "<NetKeyIndex> <AppKeyIndex> [val]" },
18741913
{ "mod-app-bind", cmd_mod_app_bind,
18751914
"<addr> <AppIndex> <Model ID> [Company ID]" },

0 commit comments

Comments
 (0)