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 <johan.hedberg@intel.com>
This commit is contained in:
parent
74e1f51350
commit
364ce8801f
3 changed files with 117 additions and 0 deletions
|
@ -58,6 +58,9 @@ int bt_mesh_cfg_relay_get(u16_t net_idx, u16_t addr, u8_t *status,
|
|||
int bt_mesh_cfg_relay_set(u16_t net_idx, u16_t addr, u8_t new_relay,
|
||||
u8_t new_transmit, u8_t *status, u8_t *transmit);
|
||||
|
||||
int bt_mesh_cfg_net_key_add(u16_t net_idx, u16_t addr, u16_t key_net_idx,
|
||||
const u8_t net_key[16], u8_t *status);
|
||||
|
||||
int bt_mesh_cfg_app_key_add(u16_t net_idx, u16_t addr, u16_t key_net_idx,
|
||||
u16_t key_app_idx, const u8_t app_key[16],
|
||||
u8_t *status);
|
||||
|
|
|
@ -137,6 +137,42 @@ static void relay_status(struct bt_mesh_model *model,
|
|||
k_sem_give(&cli->op_sync);
|
||||
}
|
||||
|
||||
struct net_key_param {
|
||||
u8_t *status;
|
||||
u16_t net_idx;
|
||||
};
|
||||
|
||||
static void net_key_status(struct bt_mesh_model *model,
|
||||
struct bt_mesh_msg_ctx *ctx,
|
||||
struct net_buf_simple *buf)
|
||||
{
|
||||
struct net_key_param *param;
|
||||
u16_t net_idx, app_idx;
|
||||
u8_t status;
|
||||
|
||||
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,
|
||||
bt_hex(buf->data, buf->len));
|
||||
|
||||
if (cli->op_pending != OP_NET_KEY_STATUS) {
|
||||
BT_WARN("Unexpected Net Key Status message");
|
||||
return;
|
||||
}
|
||||
|
||||
status = net_buf_simple_pull_u8(buf);
|
||||
key_idx_unpack(buf, &net_idx, &app_idx);
|
||||
|
||||
param = cli->op_param;
|
||||
if (param->net_idx != net_idx) {
|
||||
BT_WARN("Net Key Status key index does not match");
|
||||
return;
|
||||
}
|
||||
|
||||
*param->status = status;
|
||||
|
||||
k_sem_give(&cli->op_sync);
|
||||
}
|
||||
|
||||
struct app_key_param {
|
||||
u8_t *status;
|
||||
u16_t net_idx;
|
||||
|
@ -429,6 +465,7 @@ const struct bt_mesh_model_op bt_mesh_cfg_cli_op[] = {
|
|||
{ OP_FRIEND_STATUS, 1, friend_status },
|
||||
{ OP_GATT_PROXY_STATUS, 1, gatt_proxy_status },
|
||||
{ OP_RELAY_STATUS, 2, relay_status },
|
||||
{ OP_NET_KEY_STATUS, 3, net_key_status },
|
||||
{ OP_APP_KEY_STATUS, 4, app_key_status },
|
||||
{ OP_MOD_APP_STATUS, 7, mod_app_status },
|
||||
{ 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,
|
|||
return cli_wait(¶m, OP_RELAY_STATUS);
|
||||
}
|
||||
|
||||
int bt_mesh_cfg_net_key_add(u16_t net_idx, u16_t addr, u16_t key_net_idx,
|
||||
const u8_t net_key[16], u8_t *status)
|
||||
{
|
||||
struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 18 + 4);
|
||||
struct bt_mesh_msg_ctx ctx = {
|
||||
.net_idx = net_idx,
|
||||
.app_idx = BT_MESH_KEY_DEV,
|
||||
.addr = addr,
|
||||
.send_ttl = BT_MESH_TTL_DEFAULT,
|
||||
};
|
||||
struct net_key_param param = {
|
||||
.status = status,
|
||||
.net_idx = key_net_idx,
|
||||
};
|
||||
int err;
|
||||
|
||||
err = check_cli();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
bt_mesh_model_msg_init(msg, OP_NET_KEY_ADD);
|
||||
net_buf_simple_add_le16(msg, key_net_idx);
|
||||
net_buf_simple_add_mem(msg, net_key, 16);
|
||||
|
||||
err = bt_mesh_model_send(cli->model, &ctx, msg, NULL, NULL);
|
||||
if (err) {
|
||||
BT_ERR("model_send() failed (err %d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (!status) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return cli_wait(¶m, OP_NET_KEY_STATUS);
|
||||
}
|
||||
|
||||
int bt_mesh_cfg_app_key_add(u16_t net_idx, u16_t addr, u16_t key_net_idx,
|
||||
u16_t key_app_idx, const u8_t app_key[16],
|
||||
u8_t *status)
|
||||
|
|
|
@ -895,6 +895,44 @@ static int cmd_relay(int argc, char *argv[])
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_net_key_add(int argc, char *argv[])
|
||||
{
|
||||
u8_t key_val[16];
|
||||
u16_t key_net_idx;
|
||||
u8_t status;
|
||||
int err;
|
||||
|
||||
if (argc < 2) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
key_net_idx = strtoul(argv[1], NULL, 0);
|
||||
|
||||
if (argc > 2) {
|
||||
size_t len;
|
||||
|
||||
len = hex2bin(argv[3], key_val, sizeof(key_val));
|
||||
memset(key_val, 0, sizeof(key_val) - len);
|
||||
} else {
|
||||
memcpy(key_val, default_key, sizeof(key_val));
|
||||
}
|
||||
|
||||
err = bt_mesh_cfg_net_key_add(net.net_idx, net.dst, key_net_idx,
|
||||
key_val, &status);
|
||||
if (err) {
|
||||
printk("Unable to send NetKey Add (err %d)\n", err);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (status) {
|
||||
printk("NetKeyAdd failed with status 0x%02x\n", status);
|
||||
} else {
|
||||
printk("NetKey added with NetKey Index 0x%03x\n", key_net_idx);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_app_key_add(int argc, char *argv[])
|
||||
{
|
||||
u8_t key_val[16];
|
||||
|
@ -1870,6 +1908,7 @@ static const struct shell_cmd mesh_commands[] = {
|
|||
{ "friend", cmd_friend, "[val: off, on]" },
|
||||
{ "gatt-proxy", cmd_gatt_proxy, "[val: off, on]" },
|
||||
{ "relay", cmd_relay, "[val: off, on] [count: 0-7] [interval: 0-32]" },
|
||||
{ "net-key-add", cmd_net_key_add, "<NetKeyIndex> [val]" },
|
||||
{ "app-key-add", cmd_app_key_add, "<NetKeyIndex> <AppKeyIndex> [val]" },
|
||||
{ "mod-app-bind", cmd_mod_app_bind,
|
||||
"<addr> <AppIndex> <Model ID> [Company ID]" },
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue