1 /*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <stdbool.h>
11 #include <zephyr/types.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/sys/byteorder.h>
14
15 #include <zephyr/bluetooth/bluetooth.h>
16 #include <zephyr/bluetooth/conn.h>
17 #include <zephyr/bluetooth/mesh.h>
18
19 #include "common/bt_str.h"
20
21 #include "testing.h"
22
23 #include "mesh.h"
24 #include "net.h"
25 #include "rpl.h"
26 #include "lpn.h"
27 #include "transport.h"
28 #include "heartbeat.h"
29 #include "crypto.h"
30 #include "access.h"
31 #include "beacon.h"
32 #include "proxy.h"
33 #include "foundation.h"
34 #include "friend.h"
35 #include "settings.h"
36 #include "cfg.h"
37 #include "va.h"
38
39 #define LOG_LEVEL CONFIG_BT_MESH_MODEL_LOG_LEVEL
40 #include <zephyr/logging/log.h>
41 LOG_MODULE_REGISTER(bt_mesh_cfg_srv);
42
node_reset_pending_handler(struct k_work * work)43 static void node_reset_pending_handler(struct k_work *work)
44 {
45 bt_mesh_reset();
46 }
47
48 static K_WORK_DEFINE(node_reset_pending, node_reset_pending_handler);
49
dev_comp_data_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)50 static int dev_comp_data_get(const struct bt_mesh_model *model,
51 struct bt_mesh_msg_ctx *ctx,
52 struct net_buf_simple *buf)
53 {
54 NET_BUF_SIMPLE_DEFINE(sdu, BT_MESH_TX_SDU_MAX);
55 uint8_t page;
56 int err;
57
58 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
59 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
60
61 page = bt_mesh_comp_parse_page(buf);
62 LOG_DBG("Preparing Composition data page %d", page);
63
64 bt_mesh_model_msg_init(&sdu, OP_DEV_COMP_DATA_STATUS);
65
66 net_buf_simple_add_u8(&sdu, page);
67
68 if (atomic_test_bit(bt_mesh.flags, BT_MESH_COMP_DIRTY) && page < 128) {
69 sdu.size -= BT_MESH_MIC_SHORT;
70 err = bt_mesh_comp_read(&sdu, page);
71 sdu.size += BT_MESH_MIC_SHORT;
72 } else {
73 err = bt_mesh_comp_data_get_page(&sdu, page, 0);
74 }
75
76 if (err) {
77 LOG_ERR("Failed to get CDP%d, err:%d", page, err);
78 return err;
79 }
80
81 if (bt_mesh_model_send(model, ctx, &sdu, NULL, NULL)) {
82 LOG_ERR("Unable to send Device Composition Status response");
83 }
84
85 return err;
86 }
87
get_model(const struct bt_mesh_elem * elem,struct net_buf_simple * buf,bool * vnd)88 static const struct bt_mesh_model *get_model(const struct bt_mesh_elem *elem,
89 struct net_buf_simple *buf, bool *vnd)
90 {
91 if (buf->len < 4) {
92 uint16_t id;
93
94 id = net_buf_simple_pull_le16(buf);
95
96 LOG_DBG("ID 0x%04x addr 0x%04x", id, elem->rt->addr);
97
98 *vnd = false;
99
100 return bt_mesh_model_find(elem, id);
101 } else {
102 uint16_t company, id;
103
104 company = net_buf_simple_pull_le16(buf);
105 id = net_buf_simple_pull_le16(buf);
106
107 LOG_DBG("Company 0x%04x ID 0x%04x addr 0x%04x", company, id, elem->rt->addr);
108
109 *vnd = true;
110
111 return bt_mesh_model_find_vnd(elem, company, id);
112 }
113 }
114
_mod_pub_set(const struct bt_mesh_model * model,uint16_t pub_addr,const uint8_t * uuid,uint16_t app_idx,uint8_t cred_flag,uint8_t ttl,uint8_t period,uint8_t retransmit,bool store)115 static uint8_t _mod_pub_set(const struct bt_mesh_model *model, uint16_t pub_addr,
116 const uint8_t *uuid, uint16_t app_idx, uint8_t cred_flag,
117 uint8_t ttl, uint8_t period, uint8_t retransmit, bool store)
118 {
119 if (!model->pub) {
120 return STATUS_NVAL_PUB_PARAM;
121 }
122
123 if (!IS_ENABLED(CONFIG_BT_MESH_LOW_POWER) && cred_flag) {
124 return STATUS_FEAT_NOT_SUPP;
125 }
126
127 if (!model->pub->update && period) {
128 return STATUS_NVAL_PUB_PARAM;
129 }
130
131 if (pub_addr == BT_MESH_ADDR_UNASSIGNED) {
132 if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) {
133 return STATUS_SUCCESS;
134 }
135
136 model->pub->addr = BT_MESH_ADDR_UNASSIGNED;
137 model->pub->key = 0U;
138 model->pub->cred = 0U;
139 model->pub->ttl = 0U;
140 model->pub->period = 0U;
141 model->pub->retransmit = 0U;
142 model->pub->count = 0U;
143 model->pub->uuid = NULL;
144
145 if (model->pub->update) {
146 /* If this fails, the timer will check pub->addr and
147 * exit without transmitting.
148 */
149 (void)k_work_cancel_delayable(&model->pub->timer);
150 }
151
152 if (IS_ENABLED(CONFIG_BT_SETTINGS) && store) {
153 bt_mesh_model_pub_store(model);
154 }
155
156 return STATUS_SUCCESS;
157 }
158
159 if (!bt_mesh_app_key_exists(app_idx) || !bt_mesh_model_has_key(model, app_idx)) {
160 return STATUS_INVALID_APPKEY;
161 }
162
163 #if CONFIG_BT_MESH_LABEL_COUNT > 0
164 if (BT_MESH_ADDR_IS_VIRTUAL(model->pub->addr)) {
165 (void)bt_mesh_va_del(model->pub->uuid);
166 }
167 #endif
168
169 model->pub->addr = pub_addr;
170 model->pub->key = app_idx;
171 model->pub->cred = cred_flag;
172 model->pub->ttl = ttl;
173 model->pub->period = period;
174 model->pub->retransmit = retransmit;
175 model->pub->uuid = uuid;
176
177 if (model->pub->update) {
178 int32_t period_ms;
179
180 period_ms = bt_mesh_model_pub_period_get(model);
181 LOG_DBG("period %u ms", period_ms);
182
183 if (period_ms > 0) {
184 k_work_reschedule(&model->pub->timer,
185 K_MSEC(period_ms));
186 } else {
187 /* If this fails, publication will stop after the
188 * ongoing set of retransmits.
189 */
190 (void)k_work_cancel_delayable(&model->pub->timer);
191 }
192 }
193
194 if (IS_ENABLED(CONFIG_BT_SETTINGS) && store) {
195 bt_mesh_model_pub_store(model);
196 }
197
198 return STATUS_SUCCESS;
199 }
200
mod_bind(const struct bt_mesh_model * model,uint16_t key_idx)201 static uint8_t mod_bind(const struct bt_mesh_model *model, uint16_t key_idx)
202 {
203 int i;
204
205 LOG_DBG("model %p key_idx 0x%03x", model, key_idx);
206
207 if (!bt_mesh_app_key_exists(key_idx)) {
208 return STATUS_INVALID_APPKEY;
209 }
210
211 for (i = 0; i < model->keys_cnt; i++) {
212 LOG_DBG("model %p id 0x%04x i %d key 0x%03x", model, model->id, i, model->keys[i]);
213 /* Treat existing binding as success */
214 if (model->keys[i] == key_idx) {
215 return STATUS_SUCCESS;
216 }
217 }
218
219 for (i = 0; i < model->keys_cnt; i++) {
220 if (model->keys[i] == BT_MESH_KEY_UNUSED) {
221 model->keys[i] = key_idx;
222
223 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
224 bt_mesh_model_bind_store(model);
225 }
226
227 return STATUS_SUCCESS;
228 }
229 }
230
231 return STATUS_INSUFF_RESOURCES;
232 }
233
mod_unbind(const struct bt_mesh_model * model,uint16_t key_idx,bool store)234 static uint8_t mod_unbind(const struct bt_mesh_model *model, uint16_t key_idx, bool store)
235 {
236 int i;
237
238 LOG_DBG("model %p key_idx 0x%03x store %u", model, key_idx, store);
239
240 if (!bt_mesh_app_key_exists(key_idx)) {
241 return STATUS_INVALID_APPKEY;
242 }
243
244 for (i = 0; i < model->keys_cnt; i++) {
245 if (model->keys[i] != key_idx) {
246 continue;
247 }
248
249 model->keys[i] = BT_MESH_KEY_UNUSED;
250
251 if (IS_ENABLED(CONFIG_BT_SETTINGS) && store) {
252 bt_mesh_model_bind_store(model);
253 }
254
255 if (model->pub && model->pub->key == key_idx) {
256 _mod_pub_set(model, BT_MESH_ADDR_UNASSIGNED, NULL,
257 0, 0, 0, 0, 0, store);
258 }
259 }
260
261 return STATUS_SUCCESS;
262 }
263
key_idx_pack_list(struct net_buf_simple * buf,uint16_t * arr,size_t cnt)264 static void key_idx_pack_list(struct net_buf_simple *buf, uint16_t *arr, size_t cnt)
265 {
266 uint16_t *idx = NULL;
267
268 for (int i = 0; i < cnt; i++) {
269 if (arr[i] != BT_MESH_KEY_UNUSED) {
270 if (!idx) {
271 idx = &arr[i];
272 continue;
273 }
274
275 key_idx_pack_pair(buf, *idx, arr[i]);
276 idx = NULL;
277 }
278 }
279
280 if (idx) {
281 net_buf_simple_add_le16(buf, *idx);
282 }
283
284 }
285
send_app_key_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint8_t status,uint16_t app_idx,uint16_t net_idx)286 static int send_app_key_status(const struct bt_mesh_model *model,
287 struct bt_mesh_msg_ctx *ctx,
288 uint8_t status,
289 uint16_t app_idx, uint16_t net_idx)
290 {
291 BT_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4);
292
293 bt_mesh_model_msg_init(&msg, OP_APP_KEY_STATUS);
294 net_buf_simple_add_u8(&msg, status);
295 key_idx_pack_pair(&msg, net_idx, app_idx);
296
297 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
298 LOG_ERR("Unable to send App Key Status response");
299 }
300
301 return 0;
302 }
303
app_key_add(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)304 static int app_key_add(const struct bt_mesh_model *model,
305 struct bt_mesh_msg_ctx *ctx,
306 struct net_buf_simple *buf)
307 {
308 uint16_t key_net_idx, key_app_idx;
309 uint8_t status;
310
311 key_idx_unpack_pair(buf, &key_net_idx, &key_app_idx);
312
313 LOG_DBG("AppIdx 0x%04x NetIdx 0x%04x", key_app_idx, key_net_idx);
314
315 status = bt_mesh_app_key_add(key_app_idx, key_net_idx, buf->data);
316
317 return send_app_key_status(model, ctx, status, key_app_idx, key_net_idx);
318 }
319
app_key_update(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)320 static int app_key_update(const struct bt_mesh_model *model,
321 struct bt_mesh_msg_ctx *ctx,
322 struct net_buf_simple *buf)
323 {
324 uint16_t key_net_idx, key_app_idx;
325 uint8_t status;
326
327 key_idx_unpack_pair(buf, &key_net_idx, &key_app_idx);
328
329 LOG_DBG("AppIdx 0x%04x NetIdx 0x%04x", key_app_idx, key_net_idx);
330
331 status = bt_mesh_app_key_update(key_app_idx, key_net_idx, buf->data);
332 LOG_DBG("status 0x%02x", status);
333
334 return send_app_key_status(model, ctx, status, key_app_idx, key_net_idx);
335 }
336
mod_app_key_del(const struct bt_mesh_model * mod,const struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)337 static void mod_app_key_del(const struct bt_mesh_model *mod,
338 const struct bt_mesh_elem *elem, bool vnd, bool primary,
339 void *user_data)
340 {
341 uint16_t *app_idx = user_data;
342
343 mod_unbind(mod, *app_idx, true);
344 }
345
app_key_evt(uint16_t app_idx,uint16_t net_idx,enum bt_mesh_key_evt evt)346 static void app_key_evt(uint16_t app_idx, uint16_t net_idx,
347 enum bt_mesh_key_evt evt)
348 {
349 if (evt == BT_MESH_KEY_DELETED) {
350 bt_mesh_model_foreach(&mod_app_key_del, &app_idx);
351 }
352 }
353
354 BT_MESH_APP_KEY_CB_DEFINE(app_key_evt);
355
app_key_del(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)356 static int app_key_del(const struct bt_mesh_model *model,
357 struct bt_mesh_msg_ctx *ctx,
358 struct net_buf_simple *buf)
359 {
360 uint16_t key_net_idx, key_app_idx;
361 uint8_t status;
362
363 key_idx_unpack_pair(buf, &key_net_idx, &key_app_idx);
364
365 LOG_DBG("AppIdx 0x%04x NetIdx 0x%04x", key_app_idx, key_net_idx);
366
367 status = bt_mesh_app_key_del(key_app_idx, key_net_idx);
368
369 return send_app_key_status(model, ctx, status, key_app_idx, key_net_idx);
370 }
371
372 /* Index list length: 3 bytes for every pair and 2 bytes for an odd idx */
373 #define IDX_LEN(num) (((num) / 2) * 3 + ((num) % 2) * 2)
374
app_key_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)375 static int app_key_get(const struct bt_mesh_model *model,
376 struct bt_mesh_msg_ctx *ctx,
377 struct net_buf_simple *buf)
378 {
379 BT_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_LIST,
380 3 + IDX_LEN(CONFIG_BT_MESH_APP_KEY_COUNT));
381 uint16_t app_idx[CONFIG_BT_MESH_APP_KEY_COUNT];
382 uint16_t get_idx;
383 uint8_t status;
384 ssize_t count;
385
386 get_idx = net_buf_simple_pull_le16(buf);
387 if (get_idx > 0xfff) {
388 LOG_ERR("Invalid NetKeyIndex 0x%04x", get_idx);
389 return -EINVAL;
390 }
391
392 LOG_DBG("idx 0x%04x", get_idx);
393
394 bt_mesh_model_msg_init(&msg, OP_APP_KEY_LIST);
395
396 if (!bt_mesh_subnet_exists(get_idx)) {
397 status = STATUS_INVALID_NETKEY;
398 } else {
399 status = STATUS_SUCCESS;
400 }
401
402 net_buf_simple_add_u8(&msg, status);
403 net_buf_simple_add_le16(&msg, get_idx);
404
405 if (status != STATUS_SUCCESS) {
406 goto send_status;
407 }
408
409 count = bt_mesh_app_keys_get(get_idx, app_idx, ARRAY_SIZE(app_idx), 0);
410 if (count < 0 || count > ARRAY_SIZE(app_idx)) {
411 count = ARRAY_SIZE(app_idx);
412 }
413
414 key_idx_pack_list(&msg, app_idx, count);
415
416 send_status:
417 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
418 LOG_ERR("Unable to send AppKey List");
419 }
420
421 return 0;
422 }
423
beacon_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)424 static int beacon_get(const struct bt_mesh_model *model,
425 struct bt_mesh_msg_ctx *ctx,
426 struct net_buf_simple *buf)
427 {
428 BT_MESH_MODEL_BUF_DEFINE(msg, OP_BEACON_STATUS, 1);
429
430 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
431 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
432
433 bt_mesh_model_msg_init(&msg, OP_BEACON_STATUS);
434 net_buf_simple_add_u8(&msg, bt_mesh_beacon_enabled());
435
436 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
437 LOG_ERR("Unable to send Config Beacon Status response");
438 }
439
440 return 0;
441 }
442
beacon_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)443 static int beacon_set(const struct bt_mesh_model *model,
444 struct bt_mesh_msg_ctx *ctx,
445 struct net_buf_simple *buf)
446 {
447 BT_MESH_MODEL_BUF_DEFINE(msg, OP_BEACON_STATUS, 1);
448
449 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
450 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
451
452 if (buf->data[0] != 0x00 && buf->data[0] != 0x01) {
453 LOG_WRN("Invalid Config Beacon value 0x%02x", buf->data[0]);
454 return -EINVAL;
455 }
456
457 bt_mesh_beacon_set(buf->data[0]);
458
459 bt_mesh_model_msg_init(&msg, OP_BEACON_STATUS);
460 net_buf_simple_add_u8(&msg, buf->data[0]);
461
462 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
463 LOG_ERR("Unable to send Config Beacon Status response");
464 }
465
466 return 0;
467 }
468
default_ttl_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)469 static int default_ttl_get(const struct bt_mesh_model *model,
470 struct bt_mesh_msg_ctx *ctx,
471 struct net_buf_simple *buf)
472 {
473 BT_MESH_MODEL_BUF_DEFINE(msg, OP_DEFAULT_TTL_STATUS, 1);
474
475 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
476 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
477
478 bt_mesh_model_msg_init(&msg, OP_DEFAULT_TTL_STATUS);
479 net_buf_simple_add_u8(&msg, bt_mesh_default_ttl_get());
480
481 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
482 LOG_ERR("Unable to send Default TTL Status response");
483 }
484
485 return 0;
486 }
487
default_ttl_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)488 static int default_ttl_set(const struct bt_mesh_model *model,
489 struct bt_mesh_msg_ctx *ctx,
490 struct net_buf_simple *buf)
491 {
492 BT_MESH_MODEL_BUF_DEFINE(msg, OP_DEFAULT_TTL_STATUS, 1);
493 int err;
494
495 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
496 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
497
498 err = bt_mesh_default_ttl_set(buf->data[0]);
499 if (err) {
500 LOG_WRN("Prohibited Default TTL value 0x%02x", buf->data[0]);
501 return err;
502 }
503
504 bt_mesh_model_msg_init(&msg, OP_DEFAULT_TTL_STATUS);
505 net_buf_simple_add_u8(&msg, buf->data[0]);
506
507 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
508 LOG_ERR("Unable to send Default TTL Status response");
509 }
510
511 return 0;
512 }
513
send_gatt_proxy_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx)514 static int send_gatt_proxy_status(const struct bt_mesh_model *model,
515 struct bt_mesh_msg_ctx *ctx)
516 {
517 BT_MESH_MODEL_BUF_DEFINE(msg, OP_GATT_PROXY_STATUS, 1);
518
519 bt_mesh_model_msg_init(&msg, OP_GATT_PROXY_STATUS);
520 net_buf_simple_add_u8(&msg, bt_mesh_gatt_proxy_get());
521
522 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
523 LOG_ERR("Unable to send GATT Proxy Status");
524 }
525
526 return 0;
527 }
528
gatt_proxy_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)529 static int gatt_proxy_get(const struct bt_mesh_model *model,
530 struct bt_mesh_msg_ctx *ctx,
531 struct net_buf_simple *buf)
532 {
533 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
534 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
535
536 return send_gatt_proxy_status(model, ctx);
537 }
538
gatt_proxy_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)539 static int gatt_proxy_set(const struct bt_mesh_model *model,
540 struct bt_mesh_msg_ctx *ctx,
541 struct net_buf_simple *buf)
542 {
543 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
544 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
545
546 if (buf->data[0] != 0x00 && buf->data[0] != 0x01) {
547 LOG_WRN("Invalid GATT Proxy value 0x%02x", buf->data[0]);
548 return -EINVAL;
549 }
550
551 (void)bt_mesh_gatt_proxy_set(buf->data[0]);
552
553 /** 4.2.46.1: If the value of the Node Identity state of the node for any subnet is 0x01,
554 * then the value of the Private Node Identity state shall be Disable (0x00).
555 */
556 if (IS_ENABLED(CONFIG_BT_MESH_PRIV_BEACONS) && buf->data[0]) {
557 (void)bt_mesh_priv_gatt_proxy_set(BT_MESH_FEATURE_DISABLED);
558 }
559
560 return send_gatt_proxy_status(model, ctx);
561 }
562
net_transmit_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)563 static int net_transmit_get(const struct bt_mesh_model *model,
564 struct bt_mesh_msg_ctx *ctx,
565 struct net_buf_simple *buf)
566 {
567 BT_MESH_MODEL_BUF_DEFINE(msg, OP_NET_TRANSMIT_STATUS, 1);
568
569 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
570 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
571
572 bt_mesh_model_msg_init(&msg, OP_NET_TRANSMIT_STATUS);
573 net_buf_simple_add_u8(&msg, bt_mesh_net_transmit_get());
574
575 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
576 LOG_ERR("Unable to send Config Network Transmit Status");
577 }
578
579 return 0;
580 }
581
net_transmit_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)582 static int net_transmit_set(const struct bt_mesh_model *model,
583 struct bt_mesh_msg_ctx *ctx,
584 struct net_buf_simple *buf)
585 {
586 BT_MESH_MODEL_BUF_DEFINE(msg, OP_NET_TRANSMIT_STATUS, 1);
587
588 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
589 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
590
591 LOG_DBG("Transmit 0x%02x (count %u interval %ums)", buf->data[0],
592 BT_MESH_TRANSMIT_COUNT(buf->data[0]), BT_MESH_TRANSMIT_INT(buf->data[0]));
593
594 bt_mesh_net_transmit_set(buf->data[0]);
595
596 bt_mesh_model_msg_init(&msg, OP_NET_TRANSMIT_STATUS);
597 net_buf_simple_add_u8(&msg, buf->data[0]);
598
599 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
600 LOG_ERR("Unable to send Network Transmit Status");
601 }
602
603 return 0;
604 }
605
relay_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)606 static int relay_get(const struct bt_mesh_model *model,
607 struct bt_mesh_msg_ctx *ctx,
608 struct net_buf_simple *buf)
609 {
610 BT_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_STATUS, 2);
611
612 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
613 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
614
615 bt_mesh_model_msg_init(&msg, OP_RELAY_STATUS);
616 net_buf_simple_add_u8(&msg, bt_mesh_relay_get());
617 net_buf_simple_add_u8(&msg, bt_mesh_relay_retransmit_get());
618
619 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
620 LOG_ERR("Unable to send Config Relay Status response");
621 }
622
623 return 0;
624 }
625
relay_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)626 static int relay_set(const struct bt_mesh_model *model,
627 struct bt_mesh_msg_ctx *ctx,
628 struct net_buf_simple *buf)
629 {
630 BT_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_STATUS, 2);
631
632 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
633 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
634
635 if (buf->data[0] != 0x00 && buf->data[0] != 0x01) {
636 LOG_WRN("Invalid Relay value 0x%02x", buf->data[0]);
637 return -EINVAL;
638 }
639
640 (void)bt_mesh_relay_set(buf->data[0], buf->data[1]);
641
642 bt_mesh_model_msg_init(&msg, OP_RELAY_STATUS);
643 net_buf_simple_add_u8(&msg, bt_mesh_relay_get());
644 net_buf_simple_add_u8(&msg, bt_mesh_relay_retransmit_get());
645
646 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
647 LOG_ERR("Unable to send Relay Status response");
648 }
649
650 return 0;
651 }
652
send_mod_pub_status(const struct bt_mesh_model * cfg_mod,struct bt_mesh_msg_ctx * ctx,uint16_t elem_addr,uint16_t pub_addr,bool vnd,const struct bt_mesh_model * mod,uint8_t status,uint8_t * mod_id)653 static int send_mod_pub_status(const struct bt_mesh_model *cfg_mod,
654 struct bt_mesh_msg_ctx *ctx, uint16_t elem_addr,
655 uint16_t pub_addr, bool vnd,
656 const struct bt_mesh_model *mod, uint8_t status,
657 uint8_t *mod_id)
658 {
659 BT_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_STATUS, 14);
660
661 bt_mesh_model_msg_init(&msg, OP_MOD_PUB_STATUS);
662
663 net_buf_simple_add_u8(&msg, status);
664 net_buf_simple_add_le16(&msg, elem_addr);
665
666 if (status != STATUS_SUCCESS) {
667 (void)memset(net_buf_simple_add(&msg, 7), 0, 7);
668 } else {
669 uint16_t idx_cred;
670
671 net_buf_simple_add_le16(&msg, pub_addr);
672
673 idx_cred = mod->pub->key | (uint16_t)mod->pub->cred << 12;
674 net_buf_simple_add_le16(&msg, idx_cred);
675 net_buf_simple_add_u8(&msg, mod->pub->ttl);
676 net_buf_simple_add_u8(&msg, mod->pub->period);
677 net_buf_simple_add_u8(&msg, mod->pub->retransmit);
678 }
679
680 if (vnd) {
681 memcpy(net_buf_simple_add(&msg, 4), mod_id, 4);
682 } else {
683 memcpy(net_buf_simple_add(&msg, 2), mod_id, 2);
684 }
685
686 if (bt_mesh_model_send(cfg_mod, ctx, &msg, NULL, NULL)) {
687 LOG_ERR("Unable to send Model Publication Status");
688 }
689
690 return 0;
691 }
692
mod_pub_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)693 static int mod_pub_get(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
694 struct net_buf_simple *buf)
695 {
696 uint16_t elem_addr, pub_addr = 0U;
697 const struct bt_mesh_model *mod;
698 const struct bt_mesh_elem *elem;
699 uint8_t *mod_id, status;
700 bool vnd;
701
702 if ((buf->len != 4U) && (buf->len != 6U)) {
703 LOG_ERR("The message size for the application opcode is incorrect.");
704 return -EMSGSIZE;
705 }
706
707 elem_addr = net_buf_simple_pull_le16(buf);
708 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
709 LOG_WRN("Prohibited element address");
710 return -EINVAL;
711 }
712
713 mod_id = buf->data;
714
715 LOG_DBG("elem_addr 0x%04x", elem_addr);
716
717 elem = bt_mesh_elem_find(elem_addr);
718 if (!elem) {
719 mod = NULL;
720 vnd = (buf->len == 4U);
721 status = STATUS_INVALID_ADDRESS;
722 goto send_status;
723 }
724
725 mod = get_model(elem, buf, &vnd);
726 if (!mod) {
727 status = STATUS_INVALID_MODEL;
728 goto send_status;
729 }
730
731 if (!mod->pub) {
732 status = STATUS_NVAL_PUB_PARAM;
733 goto send_status;
734 }
735
736 pub_addr = mod->pub->addr;
737 status = STATUS_SUCCESS;
738
739 send_status:
740 return send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod,
741 status, mod_id);
742 }
743
mod_pub_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)744 static int mod_pub_set(const struct bt_mesh_model *model,
745 struct bt_mesh_msg_ctx *ctx,
746 struct net_buf_simple *buf)
747 {
748 uint8_t retransmit, status, pub_ttl, pub_period, cred_flag;
749 uint16_t elem_addr, pub_addr, pub_app_idx;
750 const struct bt_mesh_model *mod;
751 const struct bt_mesh_elem *elem;
752 uint8_t *mod_id;
753 bool vnd;
754
755 if ((buf->len != 11U) && (buf->len != 13U)) {
756 LOG_ERR("The message size for the application opcode is incorrect.");
757 return -EMSGSIZE;
758 }
759
760 elem_addr = net_buf_simple_pull_le16(buf);
761 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
762 LOG_WRN("Prohibited element address");
763 return -EINVAL;
764 }
765
766 pub_addr = net_buf_simple_pull_le16(buf);
767 pub_app_idx = net_buf_simple_pull_le16(buf);
768 cred_flag = ((pub_app_idx >> 12) & BIT_MASK(1));
769 pub_app_idx &= BIT_MASK(12);
770
771 pub_ttl = net_buf_simple_pull_u8(buf);
772 if (pub_ttl > BT_MESH_TTL_MAX && pub_ttl != BT_MESH_TTL_DEFAULT) {
773 LOG_ERR("Invalid TTL value 0x%02x", pub_ttl);
774 return -EINVAL;
775 }
776
777 pub_period = net_buf_simple_pull_u8(buf);
778 retransmit = net_buf_simple_pull_u8(buf);
779 mod_id = buf->data;
780
781 LOG_DBG("elem_addr 0x%04x pub_addr 0x%04x cred_flag %u", elem_addr, pub_addr, cred_flag);
782 LOG_DBG("pub_app_idx 0x%03x, pub_ttl %u pub_period 0x%02x", pub_app_idx, pub_ttl,
783 pub_period);
784 LOG_DBG("retransmit 0x%02x (count %u interval %ums)", retransmit,
785 BT_MESH_PUB_TRANSMIT_COUNT(retransmit), BT_MESH_PUB_TRANSMIT_INT(retransmit));
786
787 elem = bt_mesh_elem_find(elem_addr);
788 if (!elem) {
789 mod = NULL;
790 vnd = (buf->len == 4U);
791 status = STATUS_INVALID_ADDRESS;
792 goto send_status;
793 }
794
795 mod = get_model(elem, buf, &vnd);
796 if (!mod) {
797 status = STATUS_INVALID_MODEL;
798 goto send_status;
799 }
800
801 status = _mod_pub_set(mod, pub_addr, NULL, pub_app_idx, cred_flag, pub_ttl,
802 pub_period, retransmit, true);
803
804 send_status:
805 return send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod,
806 status, mod_id);
807 }
808
mod_sub_list_clear(const struct bt_mesh_model * mod)809 static size_t mod_sub_list_clear(const struct bt_mesh_model *mod)
810 {
811 size_t clear_count;
812 int i;
813
814 for (i = 0, clear_count = 0; i < mod->groups_cnt; i++) {
815 /* mod->groups contains both, group and virtual addrs. Virtual addrs deletion will
816 * be handled separately.
817 */
818 if (mod->groups[i] != BT_MESH_ADDR_UNASSIGNED) {
819 mod->groups[i] = BT_MESH_ADDR_UNASSIGNED;
820 clear_count++;
821 }
822 }
823
824 #if CONFIG_BT_MESH_LABEL_COUNT > 0
825 /* Unref stored labels related to this model */
826 for (i = 0; i < CONFIG_BT_MESH_LABEL_COUNT; i++) {
827 if (mod->uuids[i] == NULL) {
828 continue;
829 }
830
831 (void)bt_mesh_va_del(mod->uuids[i]);
832 mod->uuids[i] = NULL;
833 /* No need to increment `clear_count` as `groups` contains virtual addresses. */
834 }
835 #endif
836
837 return clear_count;
838 }
839
mod_pub_va_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)840 static int mod_pub_va_set(const struct bt_mesh_model *model,
841 struct bt_mesh_msg_ctx *ctx,
842 struct net_buf_simple *buf)
843 {
844 const struct bt_mesh_va *va;
845 uint8_t retransmit, status, pub_ttl, pub_period, cred_flag;
846 uint16_t elem_addr, pub_app_idx;
847 uint16_t pub_addr = 0U;
848 const struct bt_mesh_model *mod;
849 const struct bt_mesh_elem *elem;
850 const uint8_t *uuid;
851 uint8_t *mod_id;
852 bool vnd;
853
854 if ((buf->len != 25U) && (buf->len != 27U)) {
855 LOG_ERR("The message size for the application opcode is incorrect.");
856 return -EMSGSIZE;
857 }
858
859 elem_addr = net_buf_simple_pull_le16(buf);
860 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
861 LOG_WRN("Prohibited element address");
862 return -EINVAL;
863 }
864
865 uuid = net_buf_simple_pull_mem(buf, 16);
866 pub_app_idx = net_buf_simple_pull_le16(buf);
867 cred_flag = ((pub_app_idx >> 12) & BIT_MASK(1));
868 pub_app_idx &= BIT_MASK(12);
869 pub_ttl = net_buf_simple_pull_u8(buf);
870 if (pub_ttl > BT_MESH_TTL_MAX && pub_ttl != BT_MESH_TTL_DEFAULT) {
871 LOG_ERR("Invalid TTL value 0x%02x", pub_ttl);
872 return -EINVAL;
873 }
874
875 pub_period = net_buf_simple_pull_u8(buf);
876 retransmit = net_buf_simple_pull_u8(buf);
877 mod_id = buf->data;
878
879 LOG_DBG("elem_addr 0x%04x cred_flag %u", elem_addr, cred_flag);
880 LOG_DBG("pub_app_idx 0x%03x, pub_ttl %u pub_period 0x%02x", pub_app_idx, pub_ttl,
881 pub_period);
882 LOG_DBG("retransmit 0x%02x (count %u interval %ums)", retransmit,
883 BT_MESH_PUB_TRANSMIT_COUNT(retransmit), BT_MESH_PUB_TRANSMIT_INT(retransmit));
884
885 elem = bt_mesh_elem_find(elem_addr);
886 if (!elem) {
887 mod = NULL;
888 vnd = (buf->len == 4U);
889 status = STATUS_INVALID_ADDRESS;
890 goto send_status;
891 }
892
893 mod = get_model(elem, buf, &vnd);
894 if (!mod) {
895 status = STATUS_INVALID_MODEL;
896 goto send_status;
897 }
898
899 status = bt_mesh_va_add(uuid, &va);
900 if (status != STATUS_SUCCESS) {
901 goto send_status;
902 }
903
904 pub_addr = va->addr;
905
906 status = _mod_pub_set(mod, pub_addr, va->uuid, pub_app_idx, cred_flag, pub_ttl,
907 pub_period, retransmit, true);
908 if (status != STATUS_SUCCESS) {
909 (void)bt_mesh_va_del(va->uuid);
910 }
911
912 send_status:
913 return send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod,
914 status, mod_id);
915 }
916
send_mod_sub_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint8_t status,uint16_t elem_addr,uint16_t sub_addr,uint8_t * mod_id,bool vnd)917 static int send_mod_sub_status(const struct bt_mesh_model *model,
918 struct bt_mesh_msg_ctx *ctx, uint8_t status,
919 uint16_t elem_addr, uint16_t sub_addr, uint8_t *mod_id,
920 bool vnd)
921 {
922 BT_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_STATUS, 9);
923
924 LOG_DBG("status 0x%02x elem_addr 0x%04x sub_addr 0x%04x", status, elem_addr, sub_addr);
925
926 bt_mesh_model_msg_init(&msg, OP_MOD_SUB_STATUS);
927
928 net_buf_simple_add_u8(&msg, status);
929 net_buf_simple_add_le16(&msg, elem_addr);
930 net_buf_simple_add_le16(&msg, sub_addr);
931
932 if (vnd) {
933 memcpy(net_buf_simple_add(&msg, 4), mod_id, 4);
934 } else {
935 memcpy(net_buf_simple_add(&msg, 2), mod_id, 2);
936 }
937
938 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
939 LOG_ERR("Unable to send Model Subscription Status");
940 }
941
942 return 0;
943 }
944
mod_sub_add(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)945 static int mod_sub_add(const struct bt_mesh_model *model,
946 struct bt_mesh_msg_ctx *ctx,
947 struct net_buf_simple *buf)
948 {
949 uint16_t elem_addr, sub_addr;
950 const struct bt_mesh_model *mod;
951 const struct bt_mesh_elem *elem;
952 uint8_t *mod_id;
953 uint8_t status;
954 uint16_t *entry;
955 bool vnd;
956
957 if ((buf->len != 6U) && (buf->len != 8U)) {
958 LOG_ERR("The message size for the application opcode is incorrect.");
959 return -EMSGSIZE;
960 }
961
962 elem_addr = net_buf_simple_pull_le16(buf);
963 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
964 LOG_WRN("Prohibited element address");
965 return -EINVAL;
966 }
967
968 sub_addr = net_buf_simple_pull_le16(buf);
969
970 LOG_DBG("elem_addr 0x%04x, sub_addr 0x%04x", elem_addr, sub_addr);
971
972 mod_id = buf->data;
973
974 elem = bt_mesh_elem_find(elem_addr);
975 if (!elem) {
976 mod = NULL;
977 vnd = (buf->len == 4U);
978 status = STATUS_INVALID_ADDRESS;
979 goto send_status;
980 }
981
982 mod = get_model(elem, buf, &vnd);
983 if (!mod) {
984 status = STATUS_INVALID_MODEL;
985 goto send_status;
986 }
987
988 if (!BT_MESH_ADDR_IS_GROUP(sub_addr) && !BT_MESH_ADDR_IS_FIXED_GROUP(sub_addr)) {
989 status = STATUS_INVALID_ADDRESS;
990 goto send_status;
991 }
992
993 if (bt_mesh_model_find_group(&mod, sub_addr)) {
994 /* Tried to add existing subscription */
995 LOG_DBG("found existing subscription");
996 status = STATUS_SUCCESS;
997 goto send_status;
998 }
999
1000 entry = bt_mesh_model_find_group(&mod, BT_MESH_ADDR_UNASSIGNED);
1001 if (!entry) {
1002 status = STATUS_INSUFF_RESOURCES;
1003 goto send_status;
1004 }
1005
1006 *entry = sub_addr;
1007 status = STATUS_SUCCESS;
1008
1009 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
1010 bt_mesh_model_sub_store(mod);
1011 }
1012
1013 if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) {
1014 bt_mesh_lpn_group_add(sub_addr);
1015 }
1016
1017
1018 send_status:
1019 return send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
1020 mod_id, vnd);
1021 }
1022
mod_sub_del(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1023 static int mod_sub_del(const struct bt_mesh_model *model,
1024 struct bt_mesh_msg_ctx *ctx,
1025 struct net_buf_simple *buf)
1026 {
1027 uint16_t elem_addr, sub_addr;
1028 const struct bt_mesh_model *mod;
1029 const struct bt_mesh_elem *elem;
1030 uint8_t *mod_id;
1031 uint16_t *match;
1032 uint8_t status;
1033 bool vnd;
1034
1035 if ((buf->len != 6U) && (buf->len != 8U)) {
1036 LOG_ERR("The message size for the application opcode is incorrect.");
1037 return -EMSGSIZE;
1038 }
1039
1040 elem_addr = net_buf_simple_pull_le16(buf);
1041 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
1042 LOG_WRN("Prohibited element address");
1043 return -EINVAL;
1044 }
1045
1046 sub_addr = net_buf_simple_pull_le16(buf);
1047
1048 LOG_DBG("elem_addr 0x%04x sub_addr 0x%04x", elem_addr, sub_addr);
1049
1050 mod_id = buf->data;
1051
1052 elem = bt_mesh_elem_find(elem_addr);
1053 if (!elem) {
1054 mod = NULL;
1055 vnd = (buf->len == 4U);
1056 status = STATUS_INVALID_ADDRESS;
1057 goto send_status;
1058 }
1059
1060 mod = get_model(elem, buf, &vnd);
1061 if (!mod) {
1062 status = STATUS_INVALID_MODEL;
1063 goto send_status;
1064 }
1065
1066 if (!BT_MESH_ADDR_IS_GROUP(sub_addr) && !BT_MESH_ADDR_IS_FIXED_GROUP(sub_addr)) {
1067 status = STATUS_INVALID_ADDRESS;
1068 goto send_status;
1069 }
1070
1071 /* An attempt to remove a non-existing address shall be treated
1072 * as a success.
1073 */
1074 status = STATUS_SUCCESS;
1075
1076 if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) {
1077 bt_mesh_lpn_group_del(&sub_addr, 1);
1078 }
1079
1080 match = bt_mesh_model_find_group(&mod, sub_addr);
1081 if (match) {
1082 *match = BT_MESH_ADDR_UNASSIGNED;
1083
1084 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
1085 bt_mesh_model_sub_store(mod);
1086 }
1087 }
1088
1089 send_status:
1090 return send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
1091 mod_id, vnd);
1092 }
1093
mod_sub_clear_visitor(const struct bt_mesh_model * mod,void * user_data)1094 static enum bt_mesh_walk mod_sub_clear_visitor(const struct bt_mesh_model *mod, void *user_data)
1095 {
1096 if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) {
1097 bt_mesh_lpn_group_del(mod->groups, mod->groups_cnt);
1098 }
1099
1100 mod_sub_list_clear(mod);
1101
1102 return BT_MESH_WALK_CONTINUE;
1103 }
1104
mod_sub_overwrite(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1105 static int mod_sub_overwrite(const struct bt_mesh_model *model,
1106 struct bt_mesh_msg_ctx *ctx,
1107 struct net_buf_simple *buf)
1108 {
1109 uint16_t elem_addr, sub_addr;
1110 const struct bt_mesh_model *mod;
1111 const struct bt_mesh_elem *elem;
1112 uint8_t *mod_id;
1113 uint8_t status;
1114 bool vnd;
1115
1116 if ((buf->len != 6U) && (buf->len != 8U)) {
1117 LOG_ERR("The message size for the application opcode is incorrect.");
1118 return -EMSGSIZE;
1119 }
1120
1121 elem_addr = net_buf_simple_pull_le16(buf);
1122 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
1123 LOG_WRN("Prohibited element address");
1124 return -EINVAL;
1125 }
1126
1127 sub_addr = net_buf_simple_pull_le16(buf);
1128
1129 LOG_DBG("elem_addr 0x%04x sub_addr 0x%04x", elem_addr, sub_addr);
1130
1131 mod_id = buf->data;
1132
1133 elem = bt_mesh_elem_find(elem_addr);
1134 if (!elem) {
1135 mod = NULL;
1136 vnd = (buf->len == 4U);
1137 status = STATUS_INVALID_ADDRESS;
1138 goto send_status;
1139 }
1140
1141 mod = get_model(elem, buf, &vnd);
1142 if (!mod) {
1143 status = STATUS_INVALID_MODEL;
1144 goto send_status;
1145 }
1146
1147 if (!BT_MESH_ADDR_IS_GROUP(sub_addr) && !BT_MESH_ADDR_IS_FIXED_GROUP(sub_addr)) {
1148 status = STATUS_INVALID_ADDRESS;
1149 goto send_status;
1150 }
1151
1152
1153 if (mod->groups_cnt > 0) {
1154 bt_mesh_model_extensions_walk(mod, mod_sub_clear_visitor, NULL);
1155
1156 mod->groups[0] = sub_addr;
1157 status = STATUS_SUCCESS;
1158
1159 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
1160 bt_mesh_model_sub_store(mod);
1161 }
1162
1163 if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) {
1164 bt_mesh_lpn_group_add(sub_addr);
1165 }
1166 } else {
1167 status = STATUS_INSUFF_RESOURCES;
1168 }
1169
1170
1171 send_status:
1172 return send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
1173 mod_id, vnd);
1174 }
1175
mod_sub_del_all(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1176 static int mod_sub_del_all(const struct bt_mesh_model *model,
1177 struct bt_mesh_msg_ctx *ctx,
1178 struct net_buf_simple *buf)
1179 {
1180 const struct bt_mesh_model *mod;
1181 const struct bt_mesh_elem *elem;
1182 uint16_t elem_addr;
1183 uint8_t *mod_id;
1184 uint8_t status;
1185 bool vnd;
1186
1187 if ((buf->len != 4U) && (buf->len != 6U)) {
1188 LOG_ERR("The message size for the application opcode is incorrect.");
1189 return -EMSGSIZE;
1190 }
1191
1192 elem_addr = net_buf_simple_pull_le16(buf);
1193 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
1194 LOG_WRN("Prohibited element address");
1195 return -EINVAL;
1196 }
1197
1198 LOG_DBG("elem_addr 0x%04x", elem_addr);
1199
1200 mod_id = buf->data;
1201
1202 elem = bt_mesh_elem_find(elem_addr);
1203 if (!elem) {
1204 mod = NULL;
1205 vnd = (buf->len == 4U);
1206 status = STATUS_INVALID_ADDRESS;
1207 goto send_status;
1208 }
1209
1210 mod = get_model(elem, buf, &vnd);
1211 if (!mod) {
1212 status = STATUS_INVALID_MODEL;
1213 goto send_status;
1214 }
1215
1216 bt_mesh_model_extensions_walk(mod, mod_sub_clear_visitor, NULL);
1217
1218 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
1219 bt_mesh_model_sub_store(mod);
1220 }
1221
1222 status = STATUS_SUCCESS;
1223
1224 send_status:
1225 return send_mod_sub_status(model, ctx, status, elem_addr,
1226 BT_MESH_ADDR_UNASSIGNED, mod_id, vnd);
1227 }
1228
1229 struct mod_sub_list_ctx {
1230 uint16_t elem_idx;
1231 struct net_buf_simple *msg;
1232 };
1233
mod_sub_list_visitor(const struct bt_mesh_model * mod,void * ctx)1234 static enum bt_mesh_walk mod_sub_list_visitor(const struct bt_mesh_model *mod, void *ctx)
1235 {
1236 struct mod_sub_list_ctx *visit = ctx;
1237 int count = 0;
1238 int i;
1239
1240 if (mod->rt->elem_idx != visit->elem_idx) {
1241 return BT_MESH_WALK_CONTINUE;
1242 }
1243
1244 for (i = 0; i < mod->groups_cnt; i++) {
1245 if (mod->groups[i] == BT_MESH_ADDR_UNASSIGNED) {
1246 continue;
1247 }
1248
1249 if (net_buf_simple_tailroom(visit->msg) <
1250 2 + BT_MESH_MIC_SHORT) {
1251 LOG_WRN("No room for all groups");
1252 return BT_MESH_WALK_STOP;
1253 }
1254
1255 net_buf_simple_add_le16(visit->msg, mod->groups[i]);
1256 count++;
1257 }
1258
1259 LOG_DBG("sublist: model %u:%x: %u groups", mod->rt->elem_idx, mod->id, count);
1260
1261 return BT_MESH_WALK_CONTINUE;
1262 }
1263
mod_sub_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1264 static int mod_sub_get(const struct bt_mesh_model *model,
1265 struct bt_mesh_msg_ctx *ctx,
1266 struct net_buf_simple *buf)
1267 {
1268 NET_BUF_SIMPLE_DEFINE(msg, BT_MESH_TX_SDU_MAX);
1269 struct mod_sub_list_ctx visit_ctx;
1270 const struct bt_mesh_model *mod;
1271 const struct bt_mesh_elem *elem;
1272 uint16_t addr, id;
1273
1274 addr = net_buf_simple_pull_le16(buf);
1275 if (!BT_MESH_ADDR_IS_UNICAST(addr)) {
1276 LOG_WRN("Prohibited element address");
1277 return -EINVAL;
1278 }
1279
1280 id = net_buf_simple_pull_le16(buf);
1281
1282 LOG_DBG("addr 0x%04x id 0x%04x", addr, id);
1283
1284 bt_mesh_model_msg_init(&msg, OP_MOD_SUB_LIST);
1285
1286 elem = bt_mesh_elem_find(addr);
1287 if (!elem) {
1288 net_buf_simple_add_u8(&msg, STATUS_INVALID_ADDRESS);
1289 net_buf_simple_add_le16(&msg, addr);
1290 net_buf_simple_add_le16(&msg, id);
1291 goto send_list;
1292 }
1293
1294 mod = bt_mesh_model_find(elem, id);
1295 if (!mod) {
1296 net_buf_simple_add_u8(&msg, STATUS_INVALID_MODEL);
1297 net_buf_simple_add_le16(&msg, addr);
1298 net_buf_simple_add_le16(&msg, id);
1299 goto send_list;
1300 }
1301
1302 net_buf_simple_add_u8(&msg, STATUS_SUCCESS);
1303
1304 net_buf_simple_add_le16(&msg, addr);
1305 net_buf_simple_add_le16(&msg, id);
1306
1307 visit_ctx.msg = &msg;
1308 visit_ctx.elem_idx = mod->rt->elem_idx;
1309 bt_mesh_model_extensions_walk(mod, mod_sub_list_visitor, &visit_ctx);
1310
1311 send_list:
1312 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
1313 LOG_ERR("Unable to send Model Subscription List");
1314 }
1315
1316 return 0;
1317 }
1318
mod_sub_get_vnd(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1319 static int mod_sub_get_vnd(const struct bt_mesh_model *model,
1320 struct bt_mesh_msg_ctx *ctx,
1321 struct net_buf_simple *buf)
1322 {
1323 NET_BUF_SIMPLE_DEFINE(msg, BT_MESH_TX_SDU_MAX);
1324 struct mod_sub_list_ctx visit_ctx;
1325 const struct bt_mesh_model *mod;
1326 const struct bt_mesh_elem *elem;
1327 uint16_t company, addr, id;
1328
1329 addr = net_buf_simple_pull_le16(buf);
1330 if (!BT_MESH_ADDR_IS_UNICAST(addr)) {
1331 LOG_WRN("Prohibited element address");
1332 return -EINVAL;
1333 }
1334
1335 company = net_buf_simple_pull_le16(buf);
1336 id = net_buf_simple_pull_le16(buf);
1337
1338 LOG_DBG("addr 0x%04x company 0x%04x id 0x%04x", addr, company, id);
1339
1340 bt_mesh_model_msg_init(&msg, OP_MOD_SUB_LIST_VND);
1341
1342 elem = bt_mesh_elem_find(addr);
1343 if (!elem) {
1344 net_buf_simple_add_u8(&msg, STATUS_INVALID_ADDRESS);
1345 net_buf_simple_add_le16(&msg, addr);
1346 net_buf_simple_add_le16(&msg, company);
1347 net_buf_simple_add_le16(&msg, id);
1348 goto send_list;
1349 }
1350
1351 mod = bt_mesh_model_find_vnd(elem, company, id);
1352 if (!mod) {
1353 net_buf_simple_add_u8(&msg, STATUS_INVALID_MODEL);
1354 net_buf_simple_add_le16(&msg, addr);
1355 net_buf_simple_add_le16(&msg, company);
1356 net_buf_simple_add_le16(&msg, id);
1357 goto send_list;
1358 }
1359
1360 net_buf_simple_add_u8(&msg, STATUS_SUCCESS);
1361
1362 net_buf_simple_add_le16(&msg, addr);
1363 net_buf_simple_add_le16(&msg, company);
1364 net_buf_simple_add_le16(&msg, id);
1365
1366 visit_ctx.msg = &msg;
1367 visit_ctx.elem_idx = mod->rt->elem_idx;
1368 bt_mesh_model_extensions_walk(mod, mod_sub_list_visitor, &visit_ctx);
1369
1370 send_list:
1371 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
1372 LOG_ERR("Unable to send Vendor Model Subscription List");
1373 }
1374
1375 return 0;
1376 }
1377
mod_sub_va_add(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1378 static int mod_sub_va_add(const struct bt_mesh_model *model,
1379 struct bt_mesh_msg_ctx *ctx,
1380 struct net_buf_simple *buf)
1381 {
1382 const struct bt_mesh_va *va;
1383 uint16_t elem_addr, sub_addr = BT_MESH_ADDR_UNASSIGNED;
1384 const struct bt_mesh_model *mod;
1385 const struct bt_mesh_elem *elem;
1386 const uint8_t *uuid;
1387 uint8_t *mod_id;
1388 uint16_t *group_entry;
1389 const uint8_t **label_entry;
1390 uint8_t status;
1391 bool vnd;
1392
1393 if ((buf->len != 20U) && (buf->len != 22U)) {
1394 LOG_ERR("The message size for the application opcode is incorrect.");
1395 return -EMSGSIZE;
1396 }
1397
1398 elem_addr = net_buf_simple_pull_le16(buf);
1399 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
1400 LOG_WRN("Prohibited element address");
1401 return -EINVAL;
1402 }
1403
1404 uuid = net_buf_simple_pull_mem(buf, 16);
1405
1406 LOG_DBG("elem_addr 0x%04x", elem_addr);
1407
1408 mod_id = buf->data;
1409 elem = bt_mesh_elem_find(elem_addr);
1410 if (!elem) {
1411 mod = NULL;
1412 vnd = (buf->len == 4U);
1413 status = STATUS_INVALID_ADDRESS;
1414 goto send_status;
1415 }
1416
1417 mod = get_model(elem, buf, &vnd);
1418 if (!mod) {
1419 status = STATUS_INVALID_MODEL;
1420 goto send_status;
1421 }
1422
1423 status = bt_mesh_va_add(uuid, &va);
1424 if (status != STATUS_SUCCESS) {
1425 goto send_status;
1426 }
1427
1428 if (bt_mesh_model_find_uuid(&mod, va->uuid)) {
1429 /* Tried to add existing subscription */
1430 status = STATUS_SUCCESS;
1431 (void)bt_mesh_va_del(va->uuid);
1432 sub_addr = va->addr;
1433 goto send_status;
1434 }
1435
1436 label_entry = bt_mesh_model_find_uuid(&mod, NULL);
1437 if (!label_entry) {
1438 status = STATUS_INSUFF_RESOURCES;
1439 (void)bt_mesh_va_del(va->uuid);
1440 goto send_status;
1441 }
1442
1443 group_entry = NULL;
1444
1445 for (int i = 0; i < mod->groups_cnt; i++) {
1446 if (mod->groups[i] == BT_MESH_ADDR_UNASSIGNED) {
1447 group_entry = &mod->groups[i];
1448 break;
1449 }
1450 }
1451
1452 /* bt_mesh_model_find_uuid() should find a model where both, uuids and groups lists have
1453 * empty entry.
1454 */
1455 if (!group_entry) {
1456 status = STATUS_INSUFF_RESOURCES;
1457 (void)bt_mesh_va_del(va->uuid);
1458 goto send_status;
1459 }
1460
1461 *group_entry = va->addr;
1462 *label_entry = va->uuid;
1463
1464 if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER) && va->ref == 1 &&
1465 !bt_mesh_va_collision_check(va->addr)) {
1466 bt_mesh_lpn_group_add(va->addr);
1467 }
1468
1469 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
1470 bt_mesh_model_sub_store(mod);
1471 }
1472
1473 status = STATUS_SUCCESS;
1474 sub_addr = va->addr;
1475
1476 send_status:
1477 return send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
1478 mod_id, vnd);
1479 }
1480
mod_sub_va_del(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1481 static int mod_sub_va_del(const struct bt_mesh_model *model,
1482 struct bt_mesh_msg_ctx *ctx,
1483 struct net_buf_simple *buf)
1484 {
1485 const struct bt_mesh_va *va;
1486 uint16_t elem_addr, sub_addr = BT_MESH_ADDR_UNASSIGNED;
1487 const struct bt_mesh_model *mod;
1488 const struct bt_mesh_elem *elem;
1489 const uint8_t *uuid;
1490 uint8_t *mod_id;
1491 const uint8_t **label_match;
1492 uint8_t status;
1493 bool vnd;
1494
1495 if ((buf->len != 20U) && (buf->len != 22U)) {
1496 LOG_ERR("The message size for the application opcode is incorrect.");
1497 return -EMSGSIZE;
1498 }
1499
1500 elem_addr = net_buf_simple_pull_le16(buf);
1501 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
1502 LOG_WRN("Prohibited element address");
1503 return -EINVAL;
1504 }
1505
1506 uuid = net_buf_simple_pull_mem(buf, 16);
1507
1508 LOG_DBG("elem_addr 0x%04x", elem_addr);
1509
1510 mod_id = buf->data;
1511
1512 elem = bt_mesh_elem_find(elem_addr);
1513 if (!elem) {
1514 mod = NULL;
1515 vnd = (buf->len == 4U);
1516 status = STATUS_INVALID_ADDRESS;
1517 goto send_status;
1518 }
1519
1520 mod = get_model(elem, buf, &vnd);
1521 if (!mod) {
1522 status = STATUS_INVALID_MODEL;
1523 goto send_status;
1524 }
1525
1526 va = bt_mesh_va_find(uuid);
1527 if (!va) {
1528 status = STATUS_CANNOT_REMOVE;
1529 goto send_status;
1530 }
1531
1532 if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER) && va->ref == 1 &&
1533 !bt_mesh_va_collision_check(va->addr)) {
1534 bt_mesh_lpn_group_del(&va->addr, 1);
1535 }
1536
1537 label_match = bt_mesh_model_find_uuid(&mod, va->uuid);
1538 if (!label_match) {
1539 status = STATUS_CANNOT_REMOVE;
1540 goto send_status;
1541 }
1542
1543 for (int i = 0; i < mod->groups_cnt; i++) {
1544 if (mod->groups[i] == va->addr) {
1545 mod->groups[i] = BT_MESH_ADDR_UNASSIGNED;
1546 break;
1547 }
1548 }
1549
1550 *label_match = NULL;
1551
1552 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
1553 bt_mesh_model_sub_store(mod);
1554 }
1555
1556 sub_addr = va->addr;
1557 (void)bt_mesh_va_del(va->uuid);
1558 status = STATUS_SUCCESS;
1559
1560 send_status:
1561 return send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
1562 mod_id, vnd);
1563 }
1564
mod_sub_va_overwrite(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1565 static int mod_sub_va_overwrite(const struct bt_mesh_model *model,
1566 struct bt_mesh_msg_ctx *ctx,
1567 struct net_buf_simple *buf)
1568 {
1569 const struct bt_mesh_va *va;
1570 uint16_t elem_addr, sub_addr = BT_MESH_ADDR_UNASSIGNED;
1571 const struct bt_mesh_model *mod;
1572 const struct bt_mesh_elem *elem;
1573 const uint8_t *uuid;
1574 uint8_t *mod_id;
1575 uint8_t status;
1576 bool vnd;
1577
1578 if ((buf->len != 20U) && (buf->len != 22U)) {
1579 LOG_ERR("The message size for the application opcode is incorrect.");
1580 return -EMSGSIZE;
1581 }
1582
1583 elem_addr = net_buf_simple_pull_le16(buf);
1584 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
1585 LOG_WRN("Prohibited element address");
1586 return -EINVAL;
1587 }
1588
1589 uuid = net_buf_simple_pull_mem(buf, 16);
1590
1591 LOG_DBG("elem_addr 0x%04x", elem_addr);
1592
1593 mod_id = buf->data;
1594
1595 elem = bt_mesh_elem_find(elem_addr);
1596 if (!elem) {
1597 mod = NULL;
1598 vnd = (buf->len == 4U);
1599 status = STATUS_INVALID_ADDRESS;
1600 goto send_status;
1601 }
1602
1603 mod = get_model(elem, buf, &vnd);
1604 if (!mod) {
1605 status = STATUS_INVALID_MODEL;
1606 goto send_status;
1607 }
1608
1609 if (CONFIG_BT_MESH_LABEL_COUNT == 0 || mod->groups_cnt == 0) {
1610 (void)va;
1611 status = STATUS_INSUFF_RESOURCES;
1612 goto send_status;
1613 }
1614
1615 #if CONFIG_BT_MESH_LABEL_COUNT > 0
1616 status = bt_mesh_va_add(uuid, &va);
1617 if (status != STATUS_SUCCESS) {
1618 goto send_status;
1619 }
1620
1621 bt_mesh_model_extensions_walk(mod, mod_sub_clear_visitor, NULL);
1622 mod->groups[0] = va->addr;
1623 mod->uuids[0] = va->uuid;
1624
1625 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
1626 bt_mesh_model_sub_store(mod);
1627 }
1628
1629 if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER) && va->ref == 1 &&
1630 !bt_mesh_va_collision_check(va->addr)) {
1631 bt_mesh_lpn_group_add(va->addr);
1632 }
1633
1634 sub_addr = va->addr;
1635 #endif
1636 send_status:
1637 return send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
1638 mod_id, vnd);
1639 }
1640
send_net_key_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint16_t idx,uint8_t status)1641 static int send_net_key_status(const struct bt_mesh_model *model,
1642 struct bt_mesh_msg_ctx *ctx, uint16_t idx,
1643 uint8_t status)
1644 {
1645 BT_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_STATUS, 3);
1646
1647 bt_mesh_model_msg_init(&msg, OP_NET_KEY_STATUS);
1648
1649 net_buf_simple_add_u8(&msg, status);
1650 net_buf_simple_add_le16(&msg, idx);
1651
1652 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
1653 LOG_ERR("Unable to send NetKey Status");
1654 }
1655
1656 return 0;
1657 }
1658
net_key_add(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1659 static int net_key_add(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
1660 struct net_buf_simple *buf)
1661 {
1662 uint8_t status;
1663 uint16_t idx;
1664
1665 idx = net_buf_simple_pull_le16(buf);
1666 if (idx > 0xfff) {
1667 LOG_ERR("Invalid NetKeyIndex 0x%04x", idx);
1668 return -EINVAL;
1669 }
1670
1671 LOG_DBG("idx 0x%04x", idx);
1672
1673 status = bt_mesh_subnet_add(idx, buf->data);
1674
1675 return send_net_key_status(model, ctx, idx, status);
1676 }
1677
net_key_update(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1678 static int net_key_update(const struct bt_mesh_model *model,
1679 struct bt_mesh_msg_ctx *ctx,
1680 struct net_buf_simple *buf)
1681 {
1682 uint8_t status;
1683 uint16_t idx;
1684
1685 idx = net_buf_simple_pull_le16(buf);
1686 if (idx > 0xfff) {
1687 LOG_ERR("Invalid NetKeyIndex 0x%04x", idx);
1688 return -EINVAL;
1689 }
1690
1691 status = bt_mesh_subnet_update(idx, buf->data);
1692
1693 return send_net_key_status(model, ctx, idx, status);
1694 }
1695
net_key_del(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1696 static int net_key_del(const struct bt_mesh_model *model,
1697 struct bt_mesh_msg_ctx *ctx,
1698 struct net_buf_simple *buf)
1699 {
1700 uint16_t del_idx;
1701
1702 del_idx = net_buf_simple_pull_le16(buf);
1703 if (del_idx > 0xfff) {
1704 LOG_ERR("Invalid NetKeyIndex 0x%04x", del_idx);
1705 return -EINVAL;
1706 }
1707
1708 LOG_DBG("idx 0x%04x", del_idx);
1709
1710 /* The key that the message was encrypted with cannot be removed.
1711 * The NetKey List must contain a minimum of one NetKey.
1712 */
1713 if (ctx->net_idx == del_idx) {
1714 return send_net_key_status(model, ctx, del_idx,
1715 STATUS_CANNOT_REMOVE);
1716 }
1717
1718 (void)bt_mesh_subnet_del(del_idx);
1719
1720 return send_net_key_status(model, ctx, del_idx, STATUS_SUCCESS);
1721 }
1722
net_key_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1723 static int net_key_get(const struct bt_mesh_model *model,
1724 struct bt_mesh_msg_ctx *ctx,
1725 struct net_buf_simple *buf)
1726 {
1727 BT_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_LIST,
1728 IDX_LEN(CONFIG_BT_MESH_SUBNET_COUNT));
1729 uint16_t net_idx[CONFIG_BT_MESH_SUBNET_COUNT];
1730 ssize_t count;
1731
1732 bt_mesh_model_msg_init(&msg, OP_NET_KEY_LIST);
1733
1734 count = bt_mesh_subnets_get(net_idx, ARRAY_SIZE(net_idx), 0);
1735 if (count < 0 || count > ARRAY_SIZE(net_idx)) {
1736 count = ARRAY_SIZE(net_idx);
1737 }
1738
1739 key_idx_pack_list(&msg, net_idx, count);
1740
1741 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
1742 LOG_ERR("Unable to send NetKey List");
1743 }
1744
1745 return 0;
1746 }
1747
send_node_id_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint8_t status,uint16_t net_idx,uint8_t node_id)1748 static int send_node_id_status(const struct bt_mesh_model *model,
1749 struct bt_mesh_msg_ctx *ctx,
1750 uint8_t status,
1751 uint16_t net_idx, uint8_t node_id)
1752 {
1753 BT_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_IDENTITY_STATUS, 4);
1754
1755 bt_mesh_model_msg_init(&msg, OP_NODE_IDENTITY_STATUS);
1756 net_buf_simple_add_u8(&msg, status);
1757 net_buf_simple_add_le16(&msg, net_idx);
1758 net_buf_simple_add_u8(&msg, node_id);
1759
1760 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
1761 LOG_ERR("Unable to send Node Identity Status");
1762 }
1763
1764 return 0;
1765 }
1766
node_identity_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1767 static int node_identity_get(const struct bt_mesh_model *model,
1768 struct bt_mesh_msg_ctx *ctx,
1769 struct net_buf_simple *buf)
1770 {
1771 enum bt_mesh_feat_state node_id;
1772 uint8_t status;
1773 uint16_t idx;
1774
1775 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
1776 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
1777
1778 idx = net_buf_simple_pull_le16(buf);
1779 if (idx > 0xfff) {
1780 LOG_ERR("Invalid NetKeyIndex 0x%04x", idx);
1781 return -EINVAL;
1782 }
1783
1784 status = bt_mesh_subnet_node_id_get(idx, &node_id);
1785
1786 return send_node_id_status(model, ctx, status, idx, node_id);
1787 }
1788
node_identity_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1789 static int node_identity_set(const struct bt_mesh_model *model,
1790 struct bt_mesh_msg_ctx *ctx,
1791 struct net_buf_simple *buf)
1792 {
1793 uint8_t node_id, status;
1794 uint16_t idx;
1795
1796 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
1797 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
1798
1799 idx = net_buf_simple_pull_le16(buf);
1800 if (idx > 0xfff) {
1801 LOG_WRN("Invalid NetKeyIndex 0x%04x", idx);
1802 return -EINVAL;
1803 }
1804
1805 node_id = net_buf_simple_pull_u8(buf);
1806 if (node_id != 0x00 && node_id != 0x01) {
1807 LOG_WRN("Invalid Node ID value 0x%02x", node_id);
1808 return -EINVAL;
1809 }
1810
1811 status = bt_mesh_subnet_node_id_set(idx, node_id);
1812 if (status == STATUS_INVALID_NETKEY) {
1813 return send_node_id_status(model, ctx, status, idx,
1814 BT_MESH_NODE_IDENTITY_STOPPED);
1815 }
1816
1817 if (status == STATUS_FEAT_NOT_SUPP) {
1818 /* Should return success, even if feature isn't supported: */
1819 return send_node_id_status(model, ctx, STATUS_SUCCESS, idx,
1820 BT_MESH_NODE_IDENTITY_NOT_SUPPORTED);
1821 }
1822
1823 return send_node_id_status(model, ctx, status, idx, node_id);
1824 }
1825
create_mod_app_status(struct net_buf_simple * msg,const struct bt_mesh_model * mod,bool vnd,uint16_t elem_addr,uint16_t app_idx,uint8_t status,uint8_t * mod_id)1826 static void create_mod_app_status(struct net_buf_simple *msg,
1827 const struct bt_mesh_model *mod, bool vnd,
1828 uint16_t elem_addr, uint16_t app_idx,
1829 uint8_t status, uint8_t *mod_id)
1830 {
1831 bt_mesh_model_msg_init(msg, OP_MOD_APP_STATUS);
1832
1833 net_buf_simple_add_u8(msg, status);
1834 net_buf_simple_add_le16(msg, elem_addr);
1835 net_buf_simple_add_le16(msg, app_idx);
1836
1837 if (vnd) {
1838 memcpy(net_buf_simple_add(msg, 4), mod_id, 4);
1839 } else {
1840 memcpy(net_buf_simple_add(msg, 2), mod_id, 2);
1841 }
1842 }
1843
mod_app_bind(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1844 static int mod_app_bind(const struct bt_mesh_model *model,
1845 struct bt_mesh_msg_ctx *ctx,
1846 struct net_buf_simple *buf)
1847 {
1848 BT_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_STATUS, 9);
1849 uint16_t elem_addr, key_app_idx;
1850 const struct bt_mesh_model *mod;
1851 const struct bt_mesh_elem *elem;
1852 uint8_t *mod_id, status;
1853 bool vnd;
1854
1855 if ((buf->len != 6U) && (buf->len != 8U)) {
1856 LOG_ERR("The message size for the application opcode is incorrect.");
1857 return -EMSGSIZE;
1858 }
1859
1860 elem_addr = net_buf_simple_pull_le16(buf);
1861 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
1862 LOG_WRN("Prohibited element address");
1863 return -EINVAL;
1864 }
1865
1866 key_app_idx = net_buf_simple_pull_le16(buf);
1867 mod_id = buf->data;
1868
1869 elem = bt_mesh_elem_find(elem_addr);
1870 if (!elem) {
1871 mod = NULL;
1872 vnd = (buf->len == 4U);
1873 status = STATUS_INVALID_ADDRESS;
1874 goto send_status;
1875 }
1876
1877 mod = get_model(elem, buf, &vnd);
1878 if (!mod) {
1879 status = STATUS_INVALID_MODEL;
1880 goto send_status;
1881 }
1882
1883 /* Some models only allow device key based access */
1884 if (mod->rt->flags & BT_MESH_MOD_DEVKEY_ONLY) {
1885 LOG_ERR("Client tried to bind AppKey to DevKey based model");
1886 status = STATUS_CANNOT_BIND;
1887 goto send_status;
1888 }
1889
1890 status = mod_bind(mod, key_app_idx);
1891
1892 if (IS_ENABLED(CONFIG_BT_TESTING) && status == STATUS_SUCCESS) {
1893 bt_mesh_test_model_bound(ctx->addr, mod, key_app_idx);
1894 }
1895
1896 send_status:
1897 LOG_DBG("status 0x%02x", status);
1898 create_mod_app_status(&msg, mod, vnd, elem_addr, key_app_idx, status,
1899 mod_id);
1900
1901 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
1902 LOG_ERR("Unable to send Model App Bind Status response");
1903 }
1904
1905 return 0;
1906 }
1907
mod_app_unbind(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1908 static int mod_app_unbind(const struct bt_mesh_model *model,
1909 struct bt_mesh_msg_ctx *ctx,
1910 struct net_buf_simple *buf)
1911 {
1912 BT_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_STATUS, 9);
1913 uint16_t elem_addr, key_app_idx;
1914 const struct bt_mesh_model *mod;
1915 const struct bt_mesh_elem *elem;
1916 uint8_t *mod_id, status;
1917 bool vnd;
1918
1919 if ((buf->len != 6U) && (buf->len != 8U)) {
1920 LOG_ERR("The message size for the application opcode is incorrect.");
1921 return -EMSGSIZE;
1922 }
1923
1924 elem_addr = net_buf_simple_pull_le16(buf);
1925 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
1926 LOG_WRN("Prohibited element address");
1927 return -EINVAL;
1928 }
1929
1930 key_app_idx = net_buf_simple_pull_le16(buf);
1931 mod_id = buf->data;
1932
1933 elem = bt_mesh_elem_find(elem_addr);
1934 if (!elem) {
1935 mod = NULL;
1936 vnd = (buf->len == 4U);
1937 status = STATUS_INVALID_ADDRESS;
1938 goto send_status;
1939 }
1940
1941 mod = get_model(elem, buf, &vnd);
1942 if (!mod) {
1943 status = STATUS_INVALID_MODEL;
1944 goto send_status;
1945 }
1946
1947 status = mod_unbind(mod, key_app_idx, true);
1948
1949 if (IS_ENABLED(CONFIG_BT_TESTING) && status == STATUS_SUCCESS) {
1950 bt_mesh_test_model_unbound(ctx->addr, mod, key_app_idx);
1951 }
1952
1953 send_status:
1954 LOG_DBG("status 0x%02x", status);
1955 create_mod_app_status(&msg, mod, vnd, elem_addr, key_app_idx, status,
1956 mod_id);
1957
1958 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
1959 LOG_ERR("Unable to send Model App Unbind Status response");
1960 }
1961
1962 return 0;
1963 }
1964
1965 #define KEY_LIST_LEN (CONFIG_BT_MESH_MODEL_KEY_COUNT * 2)
1966
mod_app_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)1967 static int mod_app_get(const struct bt_mesh_model *model,
1968 struct bt_mesh_msg_ctx *ctx,
1969 struct net_buf_simple *buf)
1970 {
1971 NET_BUF_SIMPLE_DEFINE(msg,
1972 MAX(BT_MESH_MODEL_BUF_LEN(OP_VND_MOD_APP_LIST,
1973 9 + KEY_LIST_LEN),
1974 BT_MESH_MODEL_BUF_LEN(OP_SIG_MOD_APP_LIST,
1975 9 + KEY_LIST_LEN)));
1976 const struct bt_mesh_model *mod;
1977 const struct bt_mesh_elem *elem;
1978 uint8_t *mod_id, status;
1979 uint16_t elem_addr;
1980 bool vnd;
1981
1982 if ((buf->len != 4U) && (buf->len != 6U)) {
1983 LOG_ERR("The message size for the application opcode is incorrect.");
1984 return -EMSGSIZE;
1985 }
1986
1987 elem_addr = net_buf_simple_pull_le16(buf);
1988 if (!BT_MESH_ADDR_IS_UNICAST(elem_addr)) {
1989 LOG_WRN("Prohibited element address");
1990 return -EINVAL;
1991 }
1992
1993 mod_id = buf->data;
1994
1995 LOG_DBG("elem_addr 0x%04x", elem_addr);
1996
1997 elem = bt_mesh_elem_find(elem_addr);
1998 if (!elem) {
1999 mod = NULL;
2000 vnd = (buf->len == 4U);
2001 status = STATUS_INVALID_ADDRESS;
2002 goto send_list;
2003 }
2004
2005 mod = get_model(elem, buf, &vnd);
2006 if (!mod) {
2007 status = STATUS_INVALID_MODEL;
2008 goto send_list;
2009 }
2010
2011 status = STATUS_SUCCESS;
2012
2013 send_list:
2014 if (vnd) {
2015 bt_mesh_model_msg_init(&msg, OP_VND_MOD_APP_LIST);
2016 } else {
2017 bt_mesh_model_msg_init(&msg, OP_SIG_MOD_APP_LIST);
2018 }
2019
2020 net_buf_simple_add_u8(&msg, status);
2021 net_buf_simple_add_le16(&msg, elem_addr);
2022
2023 if (vnd) {
2024 net_buf_simple_add_mem(&msg, mod_id, 4);
2025 } else {
2026 net_buf_simple_add_mem(&msg, mod_id, 2);
2027 }
2028
2029 if (mod) {
2030 key_idx_pack_list(&msg, mod->keys, mod->keys_cnt);
2031 }
2032
2033 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
2034 LOG_ERR("Unable to send Model Application List message");
2035 }
2036
2037 return 0;
2038 }
2039
reset_send_start(uint16_t duration,int err,void * cb_data)2040 static void reset_send_start(uint16_t duration, int err, void *cb_data)
2041 {
2042 if (err) {
2043 LOG_ERR("Sending Node Reset Status failed (err %d)", err);
2044 k_work_submit(&node_reset_pending);
2045 }
2046 }
2047
reset_send_end(int err,void * cb_data)2048 static void reset_send_end(int err, void *cb_data)
2049 {
2050 k_work_submit(&node_reset_pending);
2051 }
2052
node_reset(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)2053 static int node_reset(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
2054 struct net_buf_simple *buf)
2055 {
2056 static const struct bt_mesh_send_cb reset_cb = {
2057 .start = reset_send_start,
2058 .end = reset_send_end,
2059 };
2060
2061 BT_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_RESET_STATUS, 0);
2062
2063 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
2064 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
2065
2066 bt_mesh_model_msg_init(&msg, OP_NODE_RESET_STATUS);
2067
2068 if (bt_mesh_model_send(model, ctx, &msg, &reset_cb, NULL)) {
2069 LOG_ERR("Unable to send Node Reset Status");
2070 }
2071
2072 return 0;
2073 }
2074
send_friend_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx)2075 static int send_friend_status(const struct bt_mesh_model *model,
2076 struct bt_mesh_msg_ctx *ctx)
2077 {
2078 BT_MESH_MODEL_BUF_DEFINE(msg, OP_FRIEND_STATUS, 1);
2079
2080 bt_mesh_model_msg_init(&msg, OP_FRIEND_STATUS);
2081 net_buf_simple_add_u8(&msg, bt_mesh_friend_get());
2082
2083 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
2084 LOG_ERR("Unable to send Friend Status");
2085 }
2086
2087 return 0;
2088 }
2089
friend_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)2090 static int friend_get(const struct bt_mesh_model *model,
2091 struct bt_mesh_msg_ctx *ctx,
2092 struct net_buf_simple *buf)
2093 {
2094 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
2095 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
2096
2097 return send_friend_status(model, ctx);
2098 }
2099
friend_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)2100 static int friend_set(const struct bt_mesh_model *model,
2101 struct bt_mesh_msg_ctx *ctx,
2102 struct net_buf_simple *buf)
2103 {
2104 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx,
2105 ctx->addr, buf->len, bt_hex(buf->data, buf->len));
2106
2107 if (buf->data[0] != 0x00 && buf->data[0] != 0x01) {
2108 LOG_WRN("Invalid Friend value 0x%02x", buf->data[0]);
2109 return -EINVAL;
2110 }
2111
2112 (void)bt_mesh_friend_set(buf->data[0]);
2113
2114 return send_friend_status(model, ctx);
2115 }
2116
lpn_timeout_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)2117 static int lpn_timeout_get(const struct bt_mesh_model *model,
2118 struct bt_mesh_msg_ctx *ctx,
2119 struct net_buf_simple *buf)
2120 {
2121 BT_MESH_MODEL_BUF_DEFINE(msg, OP_LPN_TIMEOUT_STATUS, 5);
2122 struct bt_mesh_friend *frnd;
2123 int32_t timeout_steps;
2124 uint16_t lpn_addr;
2125
2126 lpn_addr = net_buf_simple_pull_le16(buf);
2127
2128 LOG_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x lpn_addr 0x%02x", ctx->net_idx,
2129 ctx->app_idx, ctx->addr, lpn_addr);
2130
2131 if (!BT_MESH_ADDR_IS_UNICAST(lpn_addr)) {
2132 LOG_WRN("Invalid LPNAddress; ignoring msg");
2133 return -EINVAL;
2134 }
2135
2136 bt_mesh_model_msg_init(&msg, OP_LPN_TIMEOUT_STATUS);
2137 net_buf_simple_add_le16(&msg, lpn_addr);
2138
2139 if (!IS_ENABLED(CONFIG_BT_MESH_FRIEND)) {
2140 timeout_steps = 0;
2141 goto send_rsp;
2142 }
2143
2144 frnd = bt_mesh_friend_find(BT_MESH_KEY_ANY, lpn_addr, true, true);
2145 if (!frnd) {
2146 timeout_steps = 0;
2147 goto send_rsp;
2148 }
2149
2150 /* PollTimeout should be reported in steps of 100ms. */
2151 timeout_steps = frnd->poll_to / 100;
2152
2153 send_rsp:
2154 net_buf_simple_add_le24(&msg, timeout_steps);
2155
2156 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
2157 LOG_ERR("Unable to send LPN PollTimeout Status");
2158 }
2159
2160 return 0;
2161 }
2162
send_krp_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint16_t idx,uint8_t phase,uint8_t status)2163 static int send_krp_status(const struct bt_mesh_model *model,
2164 struct bt_mesh_msg_ctx *ctx, uint16_t idx,
2165 uint8_t phase, uint8_t status)
2166 {
2167 BT_MESH_MODEL_BUF_DEFINE(msg, OP_KRP_STATUS, 4);
2168
2169 bt_mesh_model_msg_init(&msg, OP_KRP_STATUS);
2170
2171 net_buf_simple_add_u8(&msg, status);
2172 net_buf_simple_add_le16(&msg, idx);
2173 net_buf_simple_add_u8(&msg, phase);
2174
2175 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
2176 LOG_ERR("Unable to send Key Refresh State Status");
2177 }
2178
2179 return 0;
2180 }
2181
krp_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)2182 static int krp_get(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
2183 struct net_buf_simple *buf)
2184 {
2185 uint8_t kr_phase, status;
2186 uint16_t idx;
2187
2188 idx = net_buf_simple_pull_le16(buf);
2189 if (idx > 0xfff) {
2190 LOG_ERR("Invalid NetKeyIndex 0x%04x", idx);
2191 return -EINVAL;
2192 }
2193
2194 LOG_DBG("idx 0x%04x", idx);
2195
2196 status = bt_mesh_subnet_kr_phase_get(idx, &kr_phase);
2197
2198 return send_krp_status(model, ctx, idx, kr_phase, status);
2199 }
2200
krp_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)2201 static int krp_set(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
2202 struct net_buf_simple *buf)
2203 {
2204 uint8_t phase, status;
2205 uint16_t idx;
2206
2207 idx = net_buf_simple_pull_le16(buf);
2208 phase = net_buf_simple_pull_u8(buf);
2209
2210 if (idx > 0xfff) {
2211 LOG_ERR("Invalid NetKeyIndex 0x%04x", idx);
2212 return -EINVAL;
2213 }
2214
2215 status = bt_mesh_subnet_kr_phase_set(idx, &phase);
2216 if (status == STATUS_CANNOT_UPDATE) {
2217 LOG_ERR("Invalid kr phase transition 0x%02x", phase);
2218 return -EINVAL;
2219 }
2220
2221 return send_krp_status(model, ctx, idx, phase, status);
2222 }
2223
hb_sub_count_log(uint32_t val)2224 static uint8_t hb_sub_count_log(uint32_t val)
2225 {
2226 if (val == 0xffff) {
2227 return 0xff;
2228 } else {
2229 return bt_mesh_hb_log(val);
2230 }
2231 }
2232
hb_pub_count_log(uint16_t val)2233 static uint8_t hb_pub_count_log(uint16_t val)
2234 {
2235 if (!val) {
2236 return 0x00;
2237 } else if (val == 0x01) {
2238 return 0x01;
2239 } else if (val == 0xfffe) {
2240 return 0x11;
2241 } else if (val == 0xffff) {
2242 return 0xff;
2243 } else {
2244 return 32 - __builtin_clz(val - 1) + 1;
2245 }
2246 }
2247
2248 struct hb_pub_param {
2249 uint16_t dst;
2250 uint8_t count_log;
2251 uint8_t period_log;
2252 uint8_t ttl;
2253 uint16_t feat;
2254 uint16_t net_idx;
2255 } __packed;
2256
hb_pub_send_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint8_t status,const struct bt_mesh_hb_pub * pub)2257 static int hb_pub_send_status(const struct bt_mesh_model *model,
2258 struct bt_mesh_msg_ctx *ctx, uint8_t status,
2259 const struct bt_mesh_hb_pub *pub)
2260 {
2261 BT_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_STATUS, 10);
2262
2263 LOG_DBG("src 0x%04x status 0x%02x", ctx->addr, status);
2264
2265 bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_PUB_STATUS);
2266
2267 net_buf_simple_add_u8(&msg, status);
2268
2269 net_buf_simple_add_le16(&msg, pub->dst);
2270 if (pub->dst == BT_MESH_ADDR_UNASSIGNED) {
2271 (void)memset(net_buf_simple_add(&msg, 7), 0, 7);
2272 } else {
2273 net_buf_simple_add_u8(&msg, hb_pub_count_log(pub->count));
2274 net_buf_simple_add_u8(&msg, bt_mesh_hb_log(pub->period));
2275 net_buf_simple_add_u8(&msg, pub->ttl);
2276 net_buf_simple_add_le16(&msg, pub->feat);
2277 net_buf_simple_add_le16(&msg, pub->net_idx & 0xfff);
2278 }
2279
2280 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
2281 LOG_ERR("Unable to send Heartbeat Publication Status");
2282 }
2283
2284 return 0;
2285 }
2286
heartbeat_pub_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)2287 static int heartbeat_pub_get(const struct bt_mesh_model *model,
2288 struct bt_mesh_msg_ctx *ctx,
2289 struct net_buf_simple *buf)
2290 {
2291 struct bt_mesh_hb_pub pub;
2292
2293 LOG_DBG("src 0x%04x", ctx->addr);
2294
2295 bt_mesh_hb_pub_get(&pub);
2296
2297 return hb_pub_send_status(model, ctx, STATUS_SUCCESS, &pub);
2298 }
2299
heartbeat_pub_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)2300 static int heartbeat_pub_set(const struct bt_mesh_model *model,
2301 struct bt_mesh_msg_ctx *ctx,
2302 struct net_buf_simple *buf)
2303 {
2304 struct hb_pub_param *param = (void *)buf->data;
2305 struct bt_mesh_hb_pub pub;
2306 uint8_t status;
2307
2308 LOG_DBG("src 0x%04x", ctx->addr);
2309
2310 pub.dst = sys_le16_to_cpu(param->dst);
2311 if (param->count_log == 0x11) {
2312 /* Special case defined in MshPRFv1.1 Errata 11737 */
2313 pub.count = 0xfffe;
2314 } else {
2315 pub.count = bt_mesh_hb_pwr2(param->count_log);
2316 }
2317
2318 if (param->period_log == 0x11) {
2319 pub.period = 0x10000;
2320 } else {
2321 pub.period = bt_mesh_hb_pwr2(param->period_log);
2322 }
2323
2324 pub.ttl = param->ttl;
2325 pub.feat = sys_le16_to_cpu(param->feat);
2326 pub.net_idx = sys_le16_to_cpu(param->net_idx);
2327
2328 /* All other address types but virtual are valid */
2329 if (BT_MESH_ADDR_IS_VIRTUAL(pub.dst)) {
2330 status = STATUS_INVALID_ADDRESS;
2331 goto rsp;
2332 }
2333
2334 if (param->count_log > 0x11 && param->count_log != 0xff) {
2335 status = STATUS_CANNOT_SET;
2336 goto rsp;
2337 }
2338
2339 if (param->period_log > 0x11) {
2340 status = STATUS_CANNOT_SET;
2341 goto rsp;
2342 }
2343
2344 if (param->ttl > BT_MESH_TTL_MAX && param->ttl != BT_MESH_TTL_DEFAULT) {
2345 LOG_ERR("Invalid TTL value 0x%02x", param->ttl);
2346 return -EINVAL;
2347 }
2348
2349 if (pub.net_idx > 0xfff) {
2350 LOG_ERR("Invalid NetKeyIndex 0x%04x", pub.net_idx);
2351 return -EINVAL;
2352 }
2353
2354 status = bt_mesh_hb_pub_set(&pub);
2355
2356 rsp:
2357 return hb_pub_send_status(model, ctx, status, &pub);
2358 }
2359
hb_sub_send_status(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,const struct bt_mesh_hb_sub * sub)2360 static int hb_sub_send_status(const struct bt_mesh_model *model,
2361 struct bt_mesh_msg_ctx *ctx,
2362 const struct bt_mesh_hb_sub *sub)
2363 {
2364 BT_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_STATUS, 9);
2365
2366 LOG_DBG("src 0x%04x ", ctx->addr);
2367
2368 bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_SUB_STATUS);
2369
2370 net_buf_simple_add_u8(&msg, STATUS_SUCCESS);
2371 net_buf_simple_add_le16(&msg, sub->src);
2372 net_buf_simple_add_le16(&msg, sub->dst);
2373 net_buf_simple_add_u8(&msg, bt_mesh_hb_log(sub->remaining));
2374 net_buf_simple_add_u8(&msg, hb_sub_count_log(sub->count));
2375 net_buf_simple_add_u8(&msg, sub->min_hops);
2376 net_buf_simple_add_u8(&msg, sub->max_hops);
2377
2378 if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
2379 LOG_ERR("Unable to send Heartbeat Subscription Status");
2380 }
2381
2382 return 0;
2383 }
2384
heartbeat_sub_get(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)2385 static int heartbeat_sub_get(const struct bt_mesh_model *model,
2386 struct bt_mesh_msg_ctx *ctx,
2387 struct net_buf_simple *buf)
2388 {
2389 struct bt_mesh_hb_sub sub;
2390
2391 LOG_DBG("src 0x%04x", ctx->addr);
2392
2393 bt_mesh_hb_sub_get(&sub);
2394
2395 return hb_sub_send_status(model, ctx, &sub);
2396 }
2397
heartbeat_sub_set(const struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)2398 static int heartbeat_sub_set(const struct bt_mesh_model *model,
2399 struct bt_mesh_msg_ctx *ctx,
2400 struct net_buf_simple *buf)
2401 {
2402 uint8_t period_log, status;
2403 struct bt_mesh_hb_sub sub;
2404 uint16_t sub_src, sub_dst;
2405 uint32_t period;
2406 int err;
2407
2408 LOG_DBG("src 0x%04x", ctx->addr);
2409
2410 sub_src = net_buf_simple_pull_le16(buf);
2411 sub_dst = net_buf_simple_pull_le16(buf);
2412 period_log = net_buf_simple_pull_u8(buf);
2413
2414 LOG_DBG("sub_src 0x%04x sub_dst 0x%04x period 0x%02x", sub_src, sub_dst, period_log);
2415
2416 if (period_log > 0x11) {
2417 LOG_WRN("Prohibited subscription period 0x%02x", period_log);
2418 return -EINVAL;
2419 }
2420
2421 if (period_log == 0x11) {
2422 period = 0x10000;
2423 } else {
2424 period = bt_mesh_hb_pwr2(period_log);
2425 }
2426
2427 status = bt_mesh_hb_sub_set(sub_src, sub_dst, period);
2428 if (status != STATUS_SUCCESS) {
2429 /* All errors are caused by invalid packets, which should be
2430 * ignored.
2431 */
2432 return -EINVAL;
2433 }
2434
2435 bt_mesh_hb_sub_get(&sub);
2436
2437 /* MESH/NODE/CFG/HBS/BV-01-C expects the MinHops to be 0x7f after
2438 * disabling subscription, but 0x00 for subsequent Get requests.
2439 */
2440 if (sub.src == BT_MESH_ADDR_UNASSIGNED || !period_log) {
2441 sub.min_hops = BT_MESH_TTL_MAX;
2442 }
2443
2444 err = hb_sub_send_status(model, ctx, &sub);
2445 if (err) {
2446 return err;
2447 }
2448
2449 /* MESH/NODE/CFG/HBS/BV-02-C expects us to return previous
2450 * count value and then reset it to 0.
2451 */
2452 if (sub.src != BT_MESH_ADDR_UNASSIGNED &&
2453 sub.dst != BT_MESH_ADDR_UNASSIGNED && !period) {
2454 bt_mesh_hb_sub_reset_count();
2455 }
2456
2457 return 0;
2458 }
2459
2460 const struct bt_mesh_model_op bt_mesh_cfg_srv_op[] = {
2461 { OP_DEV_COMP_DATA_GET, BT_MESH_LEN_EXACT(1), dev_comp_data_get },
2462 { OP_APP_KEY_ADD, BT_MESH_LEN_EXACT(19), app_key_add },
2463 { OP_APP_KEY_UPDATE, BT_MESH_LEN_EXACT(19), app_key_update },
2464 { OP_APP_KEY_DEL, BT_MESH_LEN_EXACT(3), app_key_del },
2465 { OP_APP_KEY_GET, BT_MESH_LEN_EXACT(2), app_key_get },
2466 { OP_BEACON_GET, BT_MESH_LEN_EXACT(0), beacon_get },
2467 { OP_BEACON_SET, BT_MESH_LEN_EXACT(1), beacon_set },
2468 { OP_DEFAULT_TTL_GET, BT_MESH_LEN_EXACT(0), default_ttl_get },
2469 { OP_DEFAULT_TTL_SET, BT_MESH_LEN_EXACT(1), default_ttl_set },
2470 { OP_GATT_PROXY_GET, BT_MESH_LEN_EXACT(0), gatt_proxy_get },
2471 { OP_GATT_PROXY_SET, BT_MESH_LEN_EXACT(1), gatt_proxy_set },
2472 { OP_NET_TRANSMIT_GET, BT_MESH_LEN_EXACT(0), net_transmit_get },
2473 { OP_NET_TRANSMIT_SET, BT_MESH_LEN_EXACT(1), net_transmit_set },
2474 { OP_RELAY_GET, BT_MESH_LEN_EXACT(0), relay_get },
2475 { OP_RELAY_SET, BT_MESH_LEN_EXACT(2), relay_set },
2476 { OP_MOD_PUB_GET, BT_MESH_LEN_MIN(4), mod_pub_get },
2477 { OP_MOD_PUB_SET, BT_MESH_LEN_MIN(11), mod_pub_set },
2478 { OP_MOD_PUB_VA_SET, BT_MESH_LEN_MIN(25), mod_pub_va_set },
2479 { OP_MOD_SUB_ADD, BT_MESH_LEN_MIN(6), mod_sub_add },
2480 { OP_MOD_SUB_VA_ADD, BT_MESH_LEN_MIN(20), mod_sub_va_add },
2481 { OP_MOD_SUB_DEL, BT_MESH_LEN_MIN(6), mod_sub_del },
2482 { OP_MOD_SUB_VA_DEL, BT_MESH_LEN_MIN(20), mod_sub_va_del },
2483 { OP_MOD_SUB_OVERWRITE, BT_MESH_LEN_MIN(6), mod_sub_overwrite },
2484 { OP_MOD_SUB_VA_OVERWRITE, BT_MESH_LEN_MIN(20), mod_sub_va_overwrite },
2485 { OP_MOD_SUB_DEL_ALL, BT_MESH_LEN_MIN(4), mod_sub_del_all },
2486 { OP_MOD_SUB_GET, BT_MESH_LEN_EXACT(4), mod_sub_get },
2487 { OP_MOD_SUB_GET_VND, BT_MESH_LEN_EXACT(6), mod_sub_get_vnd },
2488 { OP_NET_KEY_ADD, BT_MESH_LEN_EXACT(18), net_key_add },
2489 { OP_NET_KEY_UPDATE, BT_MESH_LEN_EXACT(18), net_key_update },
2490 { OP_NET_KEY_DEL, BT_MESH_LEN_EXACT(2), net_key_del },
2491 { OP_NET_KEY_GET, BT_MESH_LEN_EXACT(0), net_key_get },
2492 { OP_NODE_IDENTITY_GET, BT_MESH_LEN_EXACT(2), node_identity_get },
2493 { OP_NODE_IDENTITY_SET, BT_MESH_LEN_EXACT(3), node_identity_set },
2494 { OP_MOD_APP_BIND, BT_MESH_LEN_MIN(6), mod_app_bind },
2495 { OP_MOD_APP_UNBIND, BT_MESH_LEN_MIN(6), mod_app_unbind },
2496 { OP_SIG_MOD_APP_GET, BT_MESH_LEN_MIN(4), mod_app_get },
2497 { OP_VND_MOD_APP_GET, BT_MESH_LEN_MIN(6), mod_app_get },
2498 { OP_NODE_RESET, BT_MESH_LEN_EXACT(0), node_reset },
2499 { OP_FRIEND_GET, BT_MESH_LEN_EXACT(0), friend_get },
2500 { OP_FRIEND_SET, BT_MESH_LEN_EXACT(1), friend_set },
2501 { OP_LPN_TIMEOUT_GET, BT_MESH_LEN_EXACT(2), lpn_timeout_get },
2502 { OP_KRP_GET, BT_MESH_LEN_EXACT(2), krp_get },
2503 { OP_KRP_SET, BT_MESH_LEN_EXACT(3), krp_set },
2504 { OP_HEARTBEAT_PUB_GET, BT_MESH_LEN_EXACT(0), heartbeat_pub_get },
2505 { OP_HEARTBEAT_PUB_SET, BT_MESH_LEN_EXACT(9), heartbeat_pub_set },
2506 { OP_HEARTBEAT_SUB_GET, BT_MESH_LEN_EXACT(0), heartbeat_sub_get },
2507 { OP_HEARTBEAT_SUB_SET, BT_MESH_LEN_EXACT(5), heartbeat_sub_set },
2508 BT_MESH_MODEL_OP_END,
2509 };
2510
cfg_srv_init(const struct bt_mesh_model * model)2511 static int cfg_srv_init(const struct bt_mesh_model *model)
2512 {
2513 if (!bt_mesh_model_in_primary(model)) {
2514 LOG_ERR("Configuration Server only allowed in primary element");
2515 return -EINVAL;
2516 }
2517
2518 /*
2519 * Configuration Model security is device-key based and only the local
2520 * device-key is allowed to access this model.
2521 */
2522 model->keys[0] = BT_MESH_KEY_DEV_LOCAL;
2523 model->rt->flags |= BT_MESH_MOD_DEVKEY_ONLY;
2524
2525 return 0;
2526 }
2527
2528 const struct bt_mesh_model_cb bt_mesh_cfg_srv_cb = {
2529 .init = cfg_srv_init,
2530 };
2531
mod_reset(const struct bt_mesh_model * mod,const struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)2532 static void mod_reset(const struct bt_mesh_model *mod, const struct bt_mesh_elem *elem,
2533 bool vnd, bool primary, void *user_data)
2534 {
2535 size_t clear_count;
2536
2537 /* Clear model state that isn't otherwise cleared. E.g. AppKey
2538 * binding and model publication is cleared as a consequence
2539 * of removing all app keys, however model subscription and user data
2540 * clearing must be taken care of here.
2541 */
2542
2543 clear_count = mod_sub_list_clear(mod);
2544
2545 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
2546 if (clear_count) {
2547 bt_mesh_model_sub_store(mod);
2548 }
2549 }
2550
2551 if (mod->cb && mod->cb->reset) {
2552 mod->cb->reset(mod);
2553 }
2554 }
2555
bt_mesh_model_reset(void)2556 void bt_mesh_model_reset(void)
2557 {
2558 bt_mesh_model_foreach(mod_reset, NULL);
2559 }
2560