Bluetooth: Mesh: Add async API for Large Comp Data Client
Add asynchronous API for Large Composition Data Client. Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
This commit is contained in:
parent
b99d4dbce2
commit
c4fa085ec4
5 changed files with 241 additions and 129 deletions
|
@ -30,63 +30,68 @@ LOG_MODULE_REGISTER(bt_mesh_large_comp_data_cli);
|
|||
#include "access.h"
|
||||
#include "foundation.h"
|
||||
|
||||
/** Mesh Large Composition Data Client Model Context */
|
||||
static struct bt_mesh_large_comp_data_cli {
|
||||
/** Composition data model entry pointer. */
|
||||
struct bt_mesh_model *model;
|
||||
|
||||
/* Internal parameters for tracking message responses. */
|
||||
struct bt_mesh_msg_ack_ctx ack_ctx;
|
||||
} cli;
|
||||
|
||||
static struct bt_mesh_large_comp_data_cli *cli;
|
||||
static int32_t msg_timeout;
|
||||
|
||||
static int large_comp_data_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
|
||||
struct net_buf_simple *buf)
|
||||
static int data_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
|
||||
struct net_buf_simple *buf, uint32_t op,
|
||||
void (*cb)(struct bt_mesh_large_comp_data_cli *cli, uint16_t addr,
|
||||
struct bt_mesh_large_comp_data_rsp *rsp))
|
||||
{
|
||||
struct net_buf_simple *comp;
|
||||
size_t to_copy;
|
||||
struct bt_mesh_large_comp_data_rsp *rsp;
|
||||
uint8_t page;
|
||||
uint16_t offset;
|
||||
uint16_t total_size;
|
||||
|
||||
LOG_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 (!bt_mesh_msg_ack_ctx_match(&cli.ack_ctx, OP_LARGE_COMP_DATA_STATUS,
|
||||
ctx->addr, (void **)&comp)) {
|
||||
return 0;
|
||||
page = net_buf_simple_pull_u8(buf);
|
||||
offset = net_buf_simple_pull_le16(buf);
|
||||
total_size = net_buf_simple_pull_le16(buf);
|
||||
|
||||
if (bt_mesh_msg_ack_ctx_match(&cli->ack_ctx, op, ctx->addr, (void **)&rsp)) {
|
||||
rsp->page = page;
|
||||
rsp->offset = offset;
|
||||
rsp->total_size = total_size;
|
||||
|
||||
if (rsp->data) {
|
||||
net_buf_simple_add_mem(rsp->data, buf->data,
|
||||
MIN(net_buf_simple_tailroom(rsp->data), buf->len));
|
||||
}
|
||||
|
||||
bt_mesh_msg_ack_ctx_rx(&cli->ack_ctx);
|
||||
}
|
||||
|
||||
to_copy = MIN(net_buf_simple_tailroom(comp), buf->len);
|
||||
net_buf_simple_add_mem(comp, buf->data, to_copy);
|
||||
if (cb) {
|
||||
struct bt_mesh_large_comp_data_rsp status_rsp = {
|
||||
.page = page,
|
||||
.offset = offset,
|
||||
.total_size = total_size,
|
||||
.data = buf,
|
||||
};
|
||||
|
||||
bt_mesh_msg_ack_ctx_rx(&cli.ack_ctx);
|
||||
cb(cli, ctx->addr, &status_rsp);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int large_comp_data_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
|
||||
struct net_buf_simple *buf)
|
||||
{
|
||||
return data_status(model, ctx, buf, OP_LARGE_COMP_DATA_STATUS,
|
||||
(cli->cb && cli->cb->large_comp_data_status ?
|
||||
cli->cb->large_comp_data_status : NULL));
|
||||
}
|
||||
|
||||
static int models_metadata_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
|
||||
struct net_buf_simple *buf)
|
||||
{
|
||||
struct net_buf_simple *metadata;
|
||||
size_t to_copy;
|
||||
|
||||
LOG_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 (!bt_mesh_msg_ack_ctx_match(&cli.ack_ctx, OP_MODELS_METADATA_STATUS,
|
||||
ctx->addr, (void **)&metadata)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
metadata = (struct net_buf_simple *)cli.ack_ctx.user_data;
|
||||
|
||||
to_copy = MIN(net_buf_simple_tailroom(metadata), buf->len);
|
||||
net_buf_simple_add_mem(metadata, buf->data, to_copy);
|
||||
|
||||
bt_mesh_msg_ack_ctx_rx(&cli.ack_ctx);
|
||||
|
||||
return 0;
|
||||
return data_status(model, ctx, buf, OP_MODELS_METADATA_STATUS,
|
||||
(cli->cb && cli->cb->models_metadata_status ?
|
||||
cli->cb->models_metadata_status : NULL));
|
||||
}
|
||||
|
||||
const struct bt_mesh_model_op _bt_mesh_large_comp_data_cli_op[] = {
|
||||
|
@ -98,17 +103,18 @@ const struct bt_mesh_model_op _bt_mesh_large_comp_data_cli_op[] = {
|
|||
static int large_comp_data_cli_init(struct bt_mesh_model *model)
|
||||
{
|
||||
if (!bt_mesh_model_in_primary(model)) {
|
||||
LOG_ERR("Configuration Client only allowed in primary element");
|
||||
LOG_ERR("Large Comp Data Client only allowed in primary element");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
model->keys[0] = BT_MESH_KEY_DEV_ANY;
|
||||
model->flags |= BT_MESH_MOD_DEVKEY_ONLY;
|
||||
|
||||
cli.model = model;
|
||||
cli = model->user_data;
|
||||
cli->model = model;
|
||||
|
||||
msg_timeout = 5000;
|
||||
bt_mesh_msg_ack_ctx_init(&cli.ack_ctx);
|
||||
bt_mesh_msg_ack_ctx_init(&cli->ack_ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -117,40 +123,35 @@ const struct bt_mesh_model_cb _bt_mesh_large_comp_data_cli_cb = {
|
|||
.init = large_comp_data_cli_init,
|
||||
};
|
||||
|
||||
int bt_mesh_large_comp_data_get(uint16_t net_idx, uint16_t addr, uint8_t page,
|
||||
size_t offset, struct net_buf_simple *comp)
|
||||
static int data_get(uint16_t net_idx, uint16_t addr, uint32_t op, uint32_t status_op, uint8_t page,
|
||||
size_t offset, struct bt_mesh_large_comp_data_rsp *rsp)
|
||||
{
|
||||
BT_MESH_MODEL_BUF_DEFINE(msg, OP_LARGE_COMP_DATA_GET, 3);
|
||||
BT_MESH_MODEL_BUF_DEFINE(msg, op, 3);
|
||||
struct bt_mesh_msg_ctx ctx = BT_MESH_MSG_CTX_INIT_DEV(net_idx, addr);
|
||||
const struct bt_mesh_msg_rsp_ctx rsp = {
|
||||
.ack = &cli.ack_ctx,
|
||||
.op = OP_LARGE_COMP_DATA_STATUS,
|
||||
.user_data = comp,
|
||||
const struct bt_mesh_msg_rsp_ctx rsp_ctx = {
|
||||
.ack = &cli->ack_ctx,
|
||||
.op = status_op,
|
||||
.user_data = rsp,
|
||||
.timeout = msg_timeout,
|
||||
};
|
||||
|
||||
bt_mesh_model_msg_init(&msg, OP_LARGE_COMP_DATA_GET);
|
||||
bt_mesh_model_msg_init(&msg, op);
|
||||
net_buf_simple_add_u8(&msg, page);
|
||||
net_buf_simple_add_le16(&msg, offset);
|
||||
|
||||
return bt_mesh_msg_ackd_send(cli.model, &ctx, &msg, comp ? &rsp : NULL);
|
||||
return bt_mesh_msg_ackd_send(cli->model, &ctx, &msg, rsp ? &rsp_ctx : NULL);
|
||||
}
|
||||
|
||||
int bt_mesh_large_comp_data_get(uint16_t net_idx, uint16_t addr, uint8_t page,
|
||||
size_t offset, struct bt_mesh_large_comp_data_rsp *rsp)
|
||||
{
|
||||
return data_get(net_idx, addr, OP_LARGE_COMP_DATA_GET, OP_LARGE_COMP_DATA_STATUS,
|
||||
page, offset, rsp);
|
||||
}
|
||||
|
||||
int bt_mesh_models_metadata_get(uint16_t net_idx, uint16_t addr, uint8_t page,
|
||||
size_t offset, struct net_buf_simple *metadata)
|
||||
size_t offset, struct bt_mesh_large_comp_data_rsp *rsp)
|
||||
{
|
||||
BT_MESH_MODEL_BUF_DEFINE(msg, OP_MODELS_METADATA_STATUS, 3);
|
||||
struct bt_mesh_msg_ctx ctx = BT_MESH_MSG_CTX_INIT_DEV(net_idx, addr);
|
||||
const struct bt_mesh_msg_rsp_ctx rsp = {
|
||||
.ack = &cli.ack_ctx,
|
||||
.op = OP_MODELS_METADATA_STATUS,
|
||||
.user_data = metadata,
|
||||
.timeout = msg_timeout,
|
||||
};
|
||||
|
||||
bt_mesh_model_msg_init(&msg, OP_MODELS_METADATA_GET);
|
||||
net_buf_simple_add_u8(&msg, page);
|
||||
net_buf_simple_add_le16(&msg, offset);
|
||||
|
||||
return bt_mesh_msg_ackd_send(cli.model, &ctx, &msg, metadata ? &rsp : NULL);
|
||||
return data_get(net_idx, addr, OP_MODELS_METADATA_GET, OP_MODELS_METADATA_STATUS,
|
||||
page, offset, rsp);
|
||||
}
|
||||
|
|
|
@ -11,9 +11,28 @@
|
|||
|
||||
#include "utils.h"
|
||||
|
||||
extern const struct shell *bt_mesh_shell_ctx_shell;
|
||||
|
||||
static void status_print(int err, char *msg, uint16_t addr, struct bt_mesh_large_comp_data_rsp *rsp)
|
||||
{
|
||||
if (err) {
|
||||
shell_error(bt_mesh_shell_ctx_shell,
|
||||
"Failed to send %s Get message (err %d)", msg, err);
|
||||
return;
|
||||
}
|
||||
|
||||
shell_print(bt_mesh_shell_ctx_shell,
|
||||
"%s [0x%04x]: page: %u offset: %u total size: %u", msg, addr, rsp->page,
|
||||
rsp->offset, rsp->total_size);
|
||||
shell_hexdump(bt_mesh_shell_ctx_shell, rsp->data->data, rsp->data->len);
|
||||
}
|
||||
|
||||
static int cmd_large_comp_data_get(const struct shell *sh, size_t argc, char *argv[])
|
||||
{
|
||||
NET_BUF_SIMPLE_DEFINE(comp, 64);
|
||||
struct bt_mesh_large_comp_data_rsp rsp = {
|
||||
.data = &comp,
|
||||
};
|
||||
uint8_t page;
|
||||
uint16_t offset;
|
||||
int err = 0;
|
||||
|
@ -29,21 +48,18 @@ static int cmd_large_comp_data_get(const struct shell *sh, size_t argc, char *ar
|
|||
}
|
||||
|
||||
err = bt_mesh_large_comp_data_get(bt_mesh_shell_target_ctx.net_idx,
|
||||
bt_mesh_shell_target_ctx.dst, page, offset,
|
||||
&comp);
|
||||
if (err) {
|
||||
shell_print(sh, "Failed to send Large Composition Data Get (err=%d)", err);
|
||||
return err;
|
||||
}
|
||||
bt_mesh_shell_target_ctx.dst, page, offset, &rsp);
|
||||
status_print(err, "Composition Data", bt_mesh_shell_target_ctx.dst, &rsp);
|
||||
|
||||
shell_print(sh, "Large Composition Data Get len=%d", comp.len);
|
||||
|
||||
return 0;
|
||||
return err;
|
||||
}
|
||||
|
||||
static int cmd_models_metadata_get(const struct shell *sh, size_t argc, char *argv[])
|
||||
{
|
||||
NET_BUF_SIMPLE_DEFINE(metadata, 64);
|
||||
struct bt_mesh_large_comp_data_rsp rsp = {
|
||||
.data = &metadata,
|
||||
};
|
||||
uint8_t page;
|
||||
uint16_t offset;
|
||||
int err = 0;
|
||||
|
@ -59,16 +75,10 @@ static int cmd_models_metadata_get(const struct shell *sh, size_t argc, char *ar
|
|||
}
|
||||
|
||||
err = bt_mesh_models_metadata_get(bt_mesh_shell_target_ctx.net_idx,
|
||||
bt_mesh_shell_target_ctx.dst, page, offset,
|
||||
&metadata);
|
||||
if (err) {
|
||||
shell_print(sh, "Failed to send Models Metadata Get (err=%d)", err);
|
||||
return err;
|
||||
}
|
||||
bt_mesh_shell_target_ctx.dst, page, offset, &rsp);
|
||||
status_print(err, "Models Metadata", bt_mesh_shell_target_ctx.dst, &rsp);
|
||||
|
||||
shell_print(sh, "Models Metadata Get len=%d", metadata.len);
|
||||
|
||||
return 0;
|
||||
return err;
|
||||
}
|
||||
|
||||
SHELL_STATIC_SUBCMD_SET_CREATE(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue