1 /* Bluetooth Mesh */
2
3 /*
4 * Copyright (c) 2021 Nordic Semiconductor ASA
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <zephyr/kernel.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <stdbool.h>
13 #include <zephyr/types.h>
14 #include <zephyr/sys/util.h>
15 #include <zephyr/sys/byteorder.h>
16
17 #include <zephyr/bluetooth/bluetooth.h>
18 #include <zephyr/bluetooth/conn.h>
19 #include <zephyr/bluetooth/mesh.h>
20
21 #include "msg.h"
22
23 #include <common/bt_str.h>
24
25 #define LOG_LEVEL CONFIG_BT_MESH_MODEL_LOG_LEVEL
26 #include <zephyr/logging/log.h>
27 LOG_MODULE_REGISTER(bt_mesh_large_comp_data_cli);
28
29 #include "net.h"
30 #include "access.h"
31 #include "foundation.h"
32
33 static struct bt_mesh_large_comp_data_cli *cli;
34 static int32_t msg_timeout;
35
data_status(const 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))36 static int data_status(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
37 struct net_buf_simple *buf, uint32_t op,
38 void (*cb)(struct bt_mesh_large_comp_data_cli *cli, uint16_t addr,
39 struct bt_mesh_large_comp_data_rsp *rsp))
40 {
41 struct bt_mesh_large_comp_data_rsp *rsp;
42 uint8_t page;
43 uint16_t offset;
44 uint16_t total_size;
45
46 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
47 ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
48 bt_hex(buf->data, buf->len));
49
50 page = net_buf_simple_pull_u8(buf);
51 offset = net_buf_simple_pull_le16(buf);
52 total_size = net_buf_simple_pull_le16(buf);
53
54 if (bt_mesh_msg_ack_ctx_match(&cli->ack_ctx, op, ctx->addr, (void **)&rsp)) {
55 rsp->page = page;
56 rsp->offset = offset;
57 rsp->total_size = total_size;
58
59 if (rsp->data) {
60 net_buf_simple_add_mem(rsp->data, buf->data,
61 MIN(net_buf_simple_tailroom(rsp->data), buf->len));
62 }
63
64 bt_mesh_msg_ack_ctx_rx(&cli->ack_ctx);
65 }
66
67 if (cb) {
68 struct bt_mesh_large_comp_data_rsp status_rsp = {
69 .page = page,
70 .offset = offset,
71 .total_size = total_size,
72 .data = buf,
73 };
74
75 cb(cli, ctx->addr, &status_rsp);
76 }
77
78 return 0;
79 }
80
large_comp_data_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)81 static int large_comp_data_status(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
82 struct net_buf_simple *buf)
83 {
84 return data_status(model, ctx, buf, OP_LARGE_COMP_DATA_STATUS,
85 (cli->cb && cli->cb->large_comp_data_status ?
86 cli->cb->large_comp_data_status : NULL));
87 }
88
models_metadata_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)89 static int models_metadata_status(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
90 struct net_buf_simple *buf)
91 {
92 return data_status(model, ctx, buf, OP_MODELS_METADATA_STATUS,
93 (cli->cb && cli->cb->models_metadata_status ?
94 cli->cb->models_metadata_status : NULL));
95 }
96
97 const struct bt_mesh_model_op _bt_mesh_large_comp_data_cli_op[] = {
98 { OP_LARGE_COMP_DATA_STATUS, BT_MESH_LEN_MIN(5), large_comp_data_status },
99 { OP_MODELS_METADATA_STATUS, BT_MESH_LEN_MIN(5), models_metadata_status },
100 BT_MESH_MODEL_OP_END,
101 };
102
large_comp_data_cli_init(const struct bt_mesh_model * model)103 static int large_comp_data_cli_init(const struct bt_mesh_model *model)
104 {
105 if (!bt_mesh_model_in_primary(model)) {
106 LOG_ERR("Large Comp Data Client only allowed in primary element");
107 return -EINVAL;
108 }
109
110 model->keys[0] = BT_MESH_KEY_DEV_ANY;
111 model->rt->flags |= BT_MESH_MOD_DEVKEY_ONLY;
112
113 cli = model->rt->user_data;
114 cli->model = model;
115
116 msg_timeout = 5000;
117 bt_mesh_msg_ack_ctx_init(&cli->ack_ctx);
118
119 return 0;
120 }
121
122 const struct bt_mesh_model_cb _bt_mesh_large_comp_data_cli_cb = {
123 .init = large_comp_data_cli_init,
124 };
125
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)126 static int data_get(uint16_t net_idx, uint16_t addr, uint32_t op, uint32_t status_op, uint8_t page,
127 size_t offset, struct bt_mesh_large_comp_data_rsp *rsp)
128 {
129 BT_MESH_MODEL_BUF_DEFINE(msg, op, 3);
130 struct bt_mesh_msg_ctx ctx = BT_MESH_MSG_CTX_INIT_DEV(net_idx, addr);
131 const struct bt_mesh_msg_rsp_ctx rsp_ctx = {
132 .ack = &cli->ack_ctx,
133 .op = status_op,
134 .user_data = rsp,
135 .timeout = msg_timeout,
136 };
137
138 bt_mesh_model_msg_init(&msg, op);
139 net_buf_simple_add_u8(&msg, page);
140 net_buf_simple_add_le16(&msg, offset);
141
142 return bt_mesh_msg_ackd_send(cli->model, &ctx, &msg, rsp ? &rsp_ctx : NULL);
143 }
144
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)145 int bt_mesh_large_comp_data_get(uint16_t net_idx, uint16_t addr, uint8_t page,
146 size_t offset, struct bt_mesh_large_comp_data_rsp *rsp)
147 {
148 return data_get(net_idx, addr, OP_LARGE_COMP_DATA_GET, OP_LARGE_COMP_DATA_STATUS,
149 page, offset, rsp);
150 }
151
bt_mesh_models_metadata_get(uint16_t net_idx,uint16_t addr,uint8_t page,size_t offset,struct bt_mesh_large_comp_data_rsp * rsp)152 int bt_mesh_models_metadata_get(uint16_t net_idx, uint16_t addr, uint8_t page,
153 size_t offset, struct bt_mesh_large_comp_data_rsp *rsp)
154 {
155 return data_get(net_idx, addr, OP_MODELS_METADATA_GET, OP_MODELS_METADATA_STATUS,
156 page, offset, rsp);
157 }
158