Bluetooth: Mesh: Add support for sending Health Fault Get message

Add the needed Health Client API for sending Health Fault Get, and add
a command to the shell to utilize it.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2017-11-22 09:29:55 +02:00 committed by Johan Hedberg
commit b26231d770
3 changed files with 161 additions and 0 deletions

View file

@ -34,6 +34,10 @@ extern const struct bt_mesh_model_op bt_mesh_health_cli_op[];
int bt_mesh_health_cli_set(struct bt_mesh_model *model);
int bt_mesh_health_fault_get(u16_t net_idx, u16_t addr, u16_t app_idx,
u16_t cid, u8_t *test_id, u8_t *faults,
size_t *fault_count);
s32_t bt_mesh_health_cli_timeout_get(void);
void bt_mesh_health_cli_timeout_set(s32_t timeout);

View file

@ -27,10 +27,117 @@ static s32_t msg_timeout = K_SECONDS(2);
static struct bt_mesh_health_cli *health_cli;
struct health_fault_param {
u16_t cid;
u8_t *test_id;
u8_t *faults;
size_t *fault_count;
};
static void health_fault_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct health_fault_param *param;
u8_t test_id;
u16_t cid;
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 (health_cli->op_pending != OP_HEALTH_FAULT_STATUS) {
BT_WARN("Unexpected Health Fault Status message");
return;
}
param = health_cli->op_param;
test_id = net_buf_simple_pull_u8(buf);
cid = net_buf_simple_pull_le16(buf);
if (cid != param->cid) {
BT_WARN("Health fault with unexpected Company ID");
return;
}
*param->test_id = test_id;
if (buf->len > *param->fault_count) {
BT_WARN("Got more faults than there's space for");
} else {
*param->fault_count = buf->len;
}
memcpy(param->faults, buf->data, *param->fault_count);
k_sem_give(&health_cli->op_sync);
}
const struct bt_mesh_model_op bt_mesh_health_cli_op[] = {
{ OP_HEALTH_FAULT_STATUS, 3, health_fault_status },
BT_MESH_MODEL_OP_END,
};
static int check_cli(void)
{
if (!health_cli) {
BT_ERR("No available Health Client context!");
return -EINVAL;
}
if (health_cli->op_pending) {
BT_WARN("Another synchronous operation pending");
return -EBUSY;
}
return 0;
}
int bt_mesh_health_fault_get(u16_t net_idx, u16_t addr, u16_t app_idx,
u16_t cid, u8_t *test_id, u8_t *faults,
size_t *fault_count)
{
struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 2 + 4);
struct bt_mesh_msg_ctx ctx = {
.net_idx = net_idx,
.app_idx = app_idx,
.addr = addr,
.send_ttl = BT_MESH_TTL_DEFAULT,
};
struct health_fault_param param = {
.cid = cid,
.test_id = test_id,
.faults = faults,
.fault_count = fault_count,
};
int err;
err = check_cli();
if (err) {
return err;
}
bt_mesh_model_msg_init(msg, OP_HEALTH_FAULT_GET);
net_buf_simple_add_le16(msg, cid);
err = bt_mesh_model_send(health_cli->model, &ctx, msg, NULL, NULL);
if (err) {
BT_ERR("model_send() failed (err %d)", err);
return err;
}
health_cli->op_param = &param;
health_cli->op_pending = OP_HEALTH_FAULT_STATUS;
err = k_sem_take(&health_cli->op_sync, msg_timeout);
health_cli->op_pending = 0;
health_cli->op_param = NULL;
return err;
}
s32_t bt_mesh_health_cli_timeout_get(void)
{
return msg_timeout;

View file

@ -66,6 +66,24 @@ static struct bt_mesh_model_pub health_pub = {
static struct bt_mesh_cfg_cli cfg_cli = {
};
void show_faults(u8_t test_id, u16_t cid, u8_t *faults, size_t fault_count)
{
size_t i;
if (!fault_count) {
printk("Health Test ID 0x%02x Company ID 0x%04x: no faults\n",
test_id, cid);
return;
}
printk("Health Test ID 0x%02x Company ID 0x%04x Fault Count %zu:\n",
test_id, cid, fault_count);
for (i = 0; i < fault_count; i++) {
printk("\t0x%02x\n", faults[i]);
}
}
static struct bt_mesh_health_cli health_cli = {
};
@ -1110,6 +1128,32 @@ int cmd_timeout(int argc, char *argv[])
return 0;
}
static int cmd_fault_get(int argc, char *argv[])
{
u8_t faults[32];
size_t fault_count;
u8_t test_id;
u16_t cid;
int err;
if (argc < 2) {
return -EINVAL;
}
cid = strtoul(argv[1], NULL, 0);
fault_count = sizeof(faults);
err = bt_mesh_health_fault_get(net.net_idx, net.dst, net.app_idx, cid,
&test_id, faults, &fault_count);
if (err) {
printk("Failed to send Health Fault Get (err %d)\n", err);
} else {
show_faults(test_id, cid, faults, fault_count);
}
return 0;
}
static const struct shell_cmd mesh_commands[] = {
{ "init", cmd_init, NULL },
{ "timeout", cmd_timeout, "[timeout in seconds]" },
@ -1132,6 +1176,8 @@ static const struct shell_cmd mesh_commands[] = {
{ "dst", cmd_dst, "[destination address]" },
{ "netidx", cmd_netidx, "[NetIdx]" },
{ "appidx", cmd_appidx, "[AppIdx]" },
/* Configuration Client Model operations */
{ "get-comp", cmd_get_comp, "[page]" },
{ "beacon", cmd_beacon, "[val: off, on]" },
{ "ttl", cmd_ttl, "[ttl: 0x00, 0x02-0x7f]" },
@ -1150,6 +1196,10 @@ static const struct shell_cmd mesh_commands[] = {
{ "hb-sub", cmd_hb_sub, "[<src> <dst> <period>]" },
{ "hb-pub", cmd_hb_pub,
"[<dst> <count> <period> <ttl> <features> <NetKeyIndex>]" },
/* Health Client Model Operations */
{ "fault-get", cmd_fault_get, "<Company ID>" },
{ NULL, NULL, NULL}
};