Bluetooth: Mesh: Use dedicated struct for heartbeat subscription

This simplifies the API since there is no-longer a need to pass a huge
number of function arguments around.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2017-11-16 14:21:23 +02:00 committed by Johan Hedberg
parent d4365e16f9
commit bd69b2127e
4 changed files with 64 additions and 54 deletions

View file

@ -101,12 +101,20 @@ int bt_mesh_cfg_mod_sub_add_vnd(u16_t net_idx, u16_t addr, u16_t elem_addr,
u16_t sub_addr, u16_t mod_id, u16_t cid,
u8_t *status);
int bt_mesh_cfg_hb_sub_set(u16_t net_idx, u16_t addr, u16_t src, u16_t dst,
u8_t period, u8_t *status);
struct bt_mesh_cfg_hb_sub {
u16_t src;
u16_t dst;
u8_t period;
u8_t count;
u8_t min;
u8_t max;
};
int bt_mesh_cfg_hb_sub_get(u16_t net_idx, u16_t addr, u16_t *src, u16_t *dst,
u8_t *period, u8_t *count, u8_t *min, u8_t *max,
u8_t *status);
int bt_mesh_cfg_hb_sub_set(u16_t net_idx, u16_t addr,
struct bt_mesh_cfg_hb_sub *sub, u8_t *status);
int bt_mesh_cfg_hb_sub_get(u16_t net_idx, u16_t addr,
struct bt_mesh_cfg_hb_sub *sub, u8_t *status);
struct bt_mesh_cfg_hb_pub {
u16_t dst;

View file

@ -173,10 +173,16 @@ static void configure(void)
printk("Publishing heartbeat messages\n");
}
#else
/* Heartbeat subscription */
bt_mesh_cfg_hb_sub_set(net_idx, addr, PROV_ADDR, GROUP_ADDR, 0x10,
NULL);
printk("Subscribing to heartbeat messages\n");
{
struct bt_mesh_cfg_hb_sub sub = {
.src = PROV_ADDR,
.dst = GROUP_ADDR,
.period = 0x10,
};
bt_mesh_cfg_hb_sub_set(net_idx, addr, &sub, NULL);
printk("Subscribing to heartbeat messages\n");
}
#endif
printk("Configuration complete\n");

View file

@ -349,12 +349,7 @@ static void mod_sub_status(struct bt_mesh_model *model,
struct hb_sub_param {
u8_t *status;
u16_t *src;
u16_t *dst;
u8_t *period;
u8_t *count;
u8_t *min;
u8_t *max;
struct bt_mesh_cfg_hb_sub *sub;
};
static void hb_sub_status(struct bt_mesh_model *model,
@ -376,14 +371,12 @@ static void hb_sub_status(struct bt_mesh_model *model,
*param->status = net_buf_simple_pull_u8(buf);
if (param->src) {
*param->src = net_buf_simple_pull_le16(buf);
*param->dst = net_buf_simple_pull_le16(buf);
*param->period = net_buf_simple_pull_u8(buf);
*param->count = net_buf_simple_pull_u8(buf);
*param->min = net_buf_simple_pull_u8(buf);
*param->max = net_buf_simple_pull_u8(buf);
}
param->sub->src = net_buf_simple_pull_le16(buf);
param->sub->dst = net_buf_simple_pull_le16(buf);
param->sub->period = net_buf_simple_pull_u8(buf);
param->sub->count = net_buf_simple_pull_u8(buf);
param->sub->min = net_buf_simple_pull_u8(buf);
param->sub->max = net_buf_simple_pull_u8(buf);
k_sem_give(&cli->op_sync);
}
@ -1054,8 +1047,8 @@ int bt_mesh_cfg_mod_pub_set_vnd(u16_t net_idx, u16_t addr, u16_t elem_addr,
pub, status);
}
int bt_mesh_cfg_hb_sub_set(u16_t net_idx, u16_t addr, u16_t src, u16_t dst,
u8_t period, u8_t *status)
int bt_mesh_cfg_hb_sub_set(u16_t net_idx, u16_t addr,
struct bt_mesh_cfg_hb_sub *sub, u8_t *status)
{
struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 5 + 4);
struct bt_mesh_msg_ctx ctx = {
@ -1066,6 +1059,7 @@ int bt_mesh_cfg_hb_sub_set(u16_t net_idx, u16_t addr, u16_t src, u16_t dst,
};
struct hb_sub_param param = {
.status = status,
.sub = sub,
};
int err;
@ -1075,9 +1069,9 @@ int bt_mesh_cfg_hb_sub_set(u16_t net_idx, u16_t addr, u16_t src, u16_t dst,
}
bt_mesh_model_msg_init(msg, OP_HEARTBEAT_SUB_SET);
net_buf_simple_add_le16(msg, src);
net_buf_simple_add_le16(msg, dst);
net_buf_simple_add_u8(msg, period);
net_buf_simple_add_le16(msg, sub->src);
net_buf_simple_add_le16(msg, sub->dst);
net_buf_simple_add_u8(msg, sub->period);
err = bt_mesh_model_send(cli->model, &ctx, msg, NULL, NULL);
if (err) {
@ -1100,9 +1094,8 @@ int bt_mesh_cfg_hb_sub_set(u16_t net_idx, u16_t addr, u16_t src, u16_t dst,
return err;
}
int bt_mesh_cfg_hb_sub_get(u16_t net_idx, u16_t addr, u16_t *src, u16_t *dst,
u8_t *period, u8_t *count, u8_t *min, u8_t *max,
u8_t *status)
int bt_mesh_cfg_hb_sub_get(u16_t net_idx, u16_t addr,
struct bt_mesh_cfg_hb_sub *sub, u8_t *status)
{
struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 0 + 4);
struct bt_mesh_msg_ctx ctx = {
@ -1113,12 +1106,7 @@ int bt_mesh_cfg_hb_sub_get(u16_t net_idx, u16_t addr, u16_t *src, u16_t *dst,
};
struct hb_sub_param param = {
.status = status,
.src = src,
.dst = dst,
.period = period,
.count = count,
.min = min,
.max = max,
.sub = sub,
};
int err;

View file

@ -798,14 +798,26 @@ static int cmd_mod_pub(int argc, char *argv[])
}
}
static void hb_sub_print(struct bt_mesh_cfg_hb_sub *sub)
{
printk("Heartbeat Subscription:\n"
"\tSource: 0x%04x\n"
"\tDestination: 0x%04x\n"
"\tPeriodLog: 0x%02x\n"
"\tCountLog: 0x%02x\n"
"\tMinHops: %u\n"
"\tMaxHops: %u\n",
sub->src, sub->dst, sub->period, sub->count,
sub->min, sub->max);
}
static int hb_sub_get(int argc, char *argv[])
{
u8_t status, period, count, min, max;
u16_t src, dst;
struct bt_mesh_cfg_hb_sub sub;
u8_t status;
int err;
err = bt_mesh_cfg_hb_sub_get(net.net_idx, net.dst, &src, &dst, &period,
&count, &min, &max, &status);
err = bt_mesh_cfg_hb_sub_get(net.net_idx, net.dst, &sub, &status);
if (err) {
printk("Heartbeat Subscription Get failed (err %d)\n", err);
return 0;
@ -814,28 +826,24 @@ static int hb_sub_get(int argc, char *argv[])
if (status) {
printk("Heartbeat Subscription Get failed (status 0x%02x)\n",
status);
return 0;
} else {
hb_sub_print(&sub);
}
printk("Heartbeat subscription:\n");
printk("\tsrc 0x%04x dst 0x%04x period 0x%02x\n", src, dst, period);
printk("\tcount 0x%02x min 0x%02x max 0x%02x\n", count, min, max);
return 0;
}
static int hb_sub_set(int argc, char *argv[])
{
u8_t status, period;
u16_t sub_src, sub_dst;
struct bt_mesh_cfg_hb_sub sub;
u8_t status;
int err;
sub_src = strtoul(argv[1], NULL, 0);
sub_dst = strtoul(argv[2], NULL, 0);
period = strtoul(argv[3], NULL, 0);
sub.src = strtoul(argv[1], NULL, 0);
sub.dst = strtoul(argv[2], NULL, 0);
sub.period = strtoul(argv[3], NULL, 0);
err = bt_mesh_cfg_hb_sub_set(net.net_idx, net.dst, sub_src, sub_dst,
period, &status);
err = bt_mesh_cfg_hb_sub_set(net.net_idx, net.dst, &sub, &status);
if (err) {
printk("Heartbeat Subscription Set failed (err %d)\n", err);
return 0;
@ -845,7 +853,7 @@ static int hb_sub_set(int argc, char *argv[])
printk("Heartbeat Subscription Set failed (status 0x%02x)\n",
status);
} else {
printk("Heartbeat subscription successfully set\n");
hb_sub_print(&sub);
}
return 0;