forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbtp_mesh.c
5519 lines (4522 loc) · 132 KB
/
btp_mesh.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <va.h>
#include <zephyr/autoconf.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/mesh.h>
#include <zephyr/bluetooth/mesh/access.h>
#include <zephyr/bluetooth/mesh/cfg.h>
#include <zephyr/bluetooth/mesh/cfg_cli.h>
#include <zephyr/bluetooth/mesh/cfg_srv.h>
#include <zephyr/bluetooth/mesh/health_cli.h>
#include <zephyr/bluetooth/mesh/health_srv.h>
#include <zephyr/bluetooth/mesh/main.h>
#include <zephyr/bluetooth/mesh/msg.h>
#include <zephyr/bluetooth/mesh/proxy.h>
#include <zephyr/logging/log.h>
#include <zephyr/net_buf.h>
#include <zephyr/settings/settings.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/util_macro.h>
#include <zephyr/sys_clock.h>
#include "mesh/access.h"
#include "mesh/dfu_slot.h"
#include "mesh/testing.h"
#include "btp/btp.h"
#define LOG_MODULE_NAME bttester_mesh
LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_BTTESTER_LOG_LEVEL);
#define CID_LOCAL 0x05F1
#define COMPANY_ID_LF 0x05F1
#define COMPANY_ID_NORDIC_SEMI 0x05F9
/* Health server data */
#define CUR_FAULTS_MAX 4
#define HEALTH_TEST_ID 0x00
static uint8_t cur_faults[CUR_FAULTS_MAX];
static uint8_t reg_faults[CUR_FAULTS_MAX * 2];
/* Provision node data */
static uint8_t net_key[16];
static uint16_t net_key_idx;
static uint8_t flags;
static uint32_t iv_index;
static uint16_t addr;
static uint8_t dev_key[16];
static uint8_t input_size;
static uint8_t pub_key[64];
static uint8_t priv_key[32];
/* Configured provisioning data */
static uint8_t dev_uuid[16];
static uint8_t static_auth[BTP_MESH_PROV_AUTH_MAX_LEN];
static uint8_t static_auth_size;
/* Vendor Model data */
#define VND_MODEL_ID_1 0x1234
static uint8_t vnd_app_key[16];
static uint16_t vnd_app_key_idx = 0x000f;
/* Model send data */
#define MODEL_BOUNDS_MAX 100
#if defined(CONFIG_BT_MESH_BLOB_SRV) || defined(CONFIG_BT_MESH_BLOB_CLI)
/* BLOB Model data*/
static uint8_t blob_rx_sum;
static bool blob_valid;
static const char *blob_data = "11111111111111111111111111111111";
static int blob_io_open(const struct bt_mesh_blob_io *io,
const struct bt_mesh_blob_xfer *xfer,
enum bt_mesh_blob_io_mode mode)
{
blob_rx_sum = 0;
blob_valid = true;
return 0;
}
static int blob_chunk_wr(const struct bt_mesh_blob_io *io,
const struct bt_mesh_blob_xfer *xfer,
const struct bt_mesh_blob_block *block,
const struct bt_mesh_blob_chunk *chunk)
{
for (int i = 0; i < chunk->size; ++i) {
blob_rx_sum += chunk->data[i];
if (chunk->data[i] !=
blob_data[(i + chunk->offset) % strlen(blob_data)]) {
blob_valid = false;
}
}
return 0;
}
static int blob_chunk_rd(const struct bt_mesh_blob_io *io,
const struct bt_mesh_blob_xfer *xfer,
const struct bt_mesh_blob_block *block,
const struct bt_mesh_blob_chunk *chunk)
{
for (int i = 0; i < chunk->size; ++i) {
chunk->data[i] =
blob_data[(i + chunk->offset) % strlen(blob_data)];
}
return 0;
}
static const struct bt_mesh_blob_io dummy_blob_io = {
.open = blob_io_open,
.rd = blob_chunk_rd,
.wr = blob_chunk_wr,
};
#endif
#if defined(CONFIG_BT_MESH_DFD_SRV)
static const struct bt_mesh_dfu_slot *dfu_self_update_slot;
static bool is_self_update(struct bt_mesh_dfd_srv *srv)
{
for (int i = 0; i < ARRAY_SIZE(srv->targets); i++) {
if (bt_mesh_has_addr(srv->targets[i].blob.addr)) {
return true;
}
}
return false;
}
/* DFD Model data*/
static int dfd_srv_recv(struct bt_mesh_dfd_srv *srv,
const struct bt_mesh_dfu_slot *slot,
const struct bt_mesh_blob_io **io)
{
LOG_DBG("Uploading new firmware image to the distributor.");
*io = &dummy_blob_io;
return 0;
}
static void dfd_srv_del(struct bt_mesh_dfd_srv *srv,
const struct bt_mesh_dfu_slot *slot)
{
LOG_DBG("Deleting the firmware image from the distributor.");
}
static int dfd_srv_send(struct bt_mesh_dfd_srv *srv,
const struct bt_mesh_dfu_slot *slot,
const struct bt_mesh_blob_io **io)
{
LOG_DBG("Starting the firmware distribution.");
*io = &dummy_blob_io;
dfu_self_update_slot = NULL;
if (is_self_update(srv)) {
LOG_DBG("DFD server starts self-update...");
dfu_self_update_slot = slot;
}
return 0;
}
#ifdef CONFIG_BT_MESH_DFD_SRV_OOB_UPLOAD
static struct {
uint8_t uri[CONFIG_BT_MESH_DFU_URI_MAXLEN];
uint8_t uri_len;
uint8_t fwid[CONFIG_BT_MESH_DFU_FWID_MAXLEN];
uint8_t fwid_len;
const struct bt_mesh_dfu_slot *slot;
uint8_t progress;
bool started;
} dfd_srv_oob_ctx;
static void oob_check_handler(struct k_work *work);
static K_WORK_DEFINE(oob_check, oob_check_handler);
static void oob_store_handler(struct k_work *work);
static K_WORK_DEFINE(oob_store, oob_store_handler);
static int dfd_srv_start_oob_upload(struct bt_mesh_dfd_srv *srv,
const struct bt_mesh_dfu_slot *slot,
const char *uri, uint8_t uri_len,
const uint8_t *fwid, uint16_t fwid_len)
{
LOG_DBG("Start OOB Upload");
memcpy(dfd_srv_oob_ctx.uri, uri, uri_len);
dfd_srv_oob_ctx.uri_len = uri_len;
memcpy(dfd_srv_oob_ctx.fwid, fwid, fwid_len);
dfd_srv_oob_ctx.fwid_len = fwid_len;
dfd_srv_oob_ctx.slot = slot;
dfd_srv_oob_ctx.progress = 0;
dfd_srv_oob_ctx.started = true;
k_work_submit(&oob_check);
return BT_MESH_DFD_SUCCESS;
}
static void dfd_srv_cancel_oob_upload(struct bt_mesh_dfd_srv *srv,
const struct bt_mesh_dfu_slot *slot)
{
LOG_DBG("Cancel OOB Upload");
dfd_srv_oob_ctx.started = false;
}
static uint8_t dfd_srv_oob_progress_get(struct bt_mesh_dfd_srv *srv,
const struct bt_mesh_dfu_slot *slot)
{
uint8_t progress;
if (dfd_srv_oob_ctx.started) {
progress = dfd_srv_oob_ctx.progress;
dfd_srv_oob_ctx.progress = MIN(dfd_srv_oob_ctx.progress + 25, 99);
if (dfd_srv_oob_ctx.progress == 99) {
k_work_submit(&oob_store);
}
} else {
progress = 0;
}
LOG_DBG("OOB Progress Get (%sstarted: %d %%)", dfd_srv_oob_ctx.started ? "" : "not ",
progress);
return progress;
}
#endif /* CONFIG_BT_MESH_DFD_SRV_OOB_UPLOAD */
static struct bt_mesh_dfd_srv_cb dfd_srv_cb = {
.recv = dfd_srv_recv,
.del = dfd_srv_del,
.send = dfd_srv_send,
#ifdef CONFIG_BT_MESH_DFD_SRV_OOB_UPLOAD
.start_oob_upload = dfd_srv_start_oob_upload,
.cancel_oob_upload = dfd_srv_cancel_oob_upload,
.oob_progress_get = dfd_srv_oob_progress_get,
#endif
};
static struct bt_mesh_dfd_srv dfd_srv = BT_MESH_DFD_SRV_INIT(&dfd_srv_cb);
#ifdef CONFIG_BT_MESH_DFD_SRV_OOB_UPLOAD
#define SUPPORTED_SCHEME "http"
static void oob_check_handler(struct k_work *work)
{
uint8_t scheme[10];
int i;
int status;
int err;
for (i = 0; i < MIN(dfd_srv_oob_ctx.uri_len, sizeof(scheme)); i++) {
if (IN_RANGE(dfd_srv_oob_ctx.uri[i], 48, 57) || /* DIGIT */
IN_RANGE(dfd_srv_oob_ctx.uri[i], 65, 90) || /* ALPHA UPPER CASE */
IN_RANGE(dfd_srv_oob_ctx.uri[i], 97, 122) || /* ALPHA LOWER CASE */
dfd_srv_oob_ctx.uri[i] == '.' ||
dfd_srv_oob_ctx.uri[i] == '+' ||
dfd_srv_oob_ctx.uri[i] == '-') {
scheme[i] = dfd_srv_oob_ctx.uri[i];
} else {
break;
}
}
if (i == dfd_srv_oob_ctx.uri_len || dfd_srv_oob_ctx.uri[i] != ':') {
status = BT_MESH_DFD_ERR_URI_MALFORMED;
} else if (i != strlen(SUPPORTED_SCHEME) ||
memcmp(scheme, SUPPORTED_SCHEME, strlen(SUPPORTED_SCHEME))) {
status = BT_MESH_DFD_ERR_URI_NOT_SUPPORTED;
} else {
status = BT_MESH_DFD_SUCCESS;
}
err = bt_mesh_dfd_srv_oob_check_complete(&dfd_srv, dfd_srv_oob_ctx.slot, status,
dfd_srv_oob_ctx.fwid, dfd_srv_oob_ctx.fwid_len);
LOG_DBG("OOB check completed (err %d)", err);
}
static void oob_store_handler(struct k_work *work)
{
int err;
err = bt_mesh_dfd_srv_oob_store_complete(&dfd_srv, dfd_srv_oob_ctx.slot, true,
10000, "metadata", 8);
LOG_DBG("OOB store completed (err %d)", err);
}
#endif /* CONFIG_BT_MESH_DFD_SRV_OOB_UPLOAD */
#endif
#if defined(CONFIG_BT_MESH_BLOB_CLI)
static struct {
struct bt_mesh_blob_cli_inputs inputs;
struct bt_mesh_blob_target targets[32];
struct bt_mesh_blob_target_pull pull[32];
uint8_t target_count;
struct bt_mesh_blob_xfer xfer;
} blob_cli_xfer;
static void blob_cli_lost_target(struct bt_mesh_blob_cli *cli,
struct bt_mesh_blob_target *target,
enum bt_mesh_blob_status reason)
{
LOG_DBG("Mesh Blob: Lost target 0x%04x (reason: %u)", target->addr,
reason);
tester_event(BTP_SERVICE_ID_MESH, MESH_EV_BLOB_LOST_TARGET, NULL, 0);
}
static void blob_cli_caps(struct bt_mesh_blob_cli *cli,
const struct bt_mesh_blob_cli_caps *caps)
{
const char *const modes[] = {
"none",
"push",
"pull",
"all",
};
if (!caps) {
LOG_DBG("None of the targets can be used for BLOB transfer");
return;
}
LOG_DBG("Mesh BLOB: capabilities:");
LOG_DBG("\tMax BLOB size: %u bytes", caps->max_size);
LOG_DBG("\tBlock size: %u-%u (%u-%u bytes)", caps->min_block_size_log,
caps->max_block_size_log, 1 << caps->min_block_size_log,
1 << caps->max_block_size_log);
LOG_DBG("\tMax chunks: %u", caps->max_chunks);
LOG_DBG("\tChunk size: %u", caps->max_chunk_size);
LOG_DBG("\tMTU size: %u", caps->mtu_size);
LOG_DBG("\tModes: %s", modes[caps->modes]);
}
static void blob_cli_end(struct bt_mesh_blob_cli *cli,
const struct bt_mesh_blob_xfer *xfer, bool success)
{
if (success) {
LOG_DBG("Mesh BLOB transfer complete.");
} else {
LOG_DBG("Mesh BLOB transfer failed.");
}
}
static const struct bt_mesh_blob_cli_cb blob_cli_handlers = {
.lost_target = blob_cli_lost_target,
.caps = blob_cli_caps,
.end = blob_cli_end,
};
static struct bt_mesh_blob_cli blob_cli = { .cb = &blob_cli_handlers };
#endif
#if defined(CONFIG_BT_MESH_DFU_SRV)
const char *metadata_data = "1100000000000011";
static uint8_t dfu_fwid[CONFIG_BT_MESH_DFU_FWID_MAXLEN] = {
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static struct bt_mesh_dfu_img dfu_imgs[] = { {
.fwid = &dfu_fwid,
.fwid_len = sizeof(dfu_fwid),
} };
static int dfu_meta_check(struct bt_mesh_dfu_srv *srv,
const struct bt_mesh_dfu_img *img,
struct net_buf_simple *metadata,
enum bt_mesh_dfu_effect *effect)
{
char string[2 * CONFIG_BT_MESH_DFU_METADATA_MAXLEN + 1];
int i;
size_t len;
len = bin2hex(metadata->data, metadata->len, string, sizeof(string));
string[len] = '\0';
for (i = 0; i <= len; i++) {
if (string[i] != metadata_data[i]) {
LOG_ERR("Wrong Firmware Metadata");
return -EINVAL;
}
}
return 0;
}
static int dfu_start(struct bt_mesh_dfu_srv *srv,
const struct bt_mesh_dfu_img *img,
struct net_buf_simple *metadata,
const struct bt_mesh_blob_io **io)
{
LOG_DBG("DFU setup");
*io = &dummy_blob_io;
return 0;
}
static void dfu_end(struct bt_mesh_dfu_srv *srv,
const struct bt_mesh_dfu_img *img, bool success)
{
if (!success) {
LOG_ERR("DFU failed");
return;
}
if (!blob_valid) {
bt_mesh_dfu_srv_rejected(srv);
return;
}
bt_mesh_dfu_srv_verified(srv);
}
static int dfu_apply(struct bt_mesh_dfu_srv *srv,
const struct bt_mesh_dfu_img *img)
{
if (!blob_valid) {
return -EINVAL;
}
LOG_DBG("Applying DFU transfer...");
#if defined(CONFIG_BT_MESH_DFD_SRV)
if (is_self_update(&dfd_srv) && dfu_self_update_slot != NULL) {
LOG_DBG("Swapping fwid for self-update");
/* Simulate self-update by swapping fwid. */
memcpy(&dfu_fwid[0], dfu_self_update_slot->fwid, dfu_self_update_slot->fwid_len);
dfu_imgs[0].fwid_len = dfu_self_update_slot->fwid_len;
}
#endif
return 0;
}
static const struct bt_mesh_dfu_srv_cb dfu_handlers = {
.check = dfu_meta_check,
.start = dfu_start,
.end = dfu_end,
.apply = dfu_apply,
};
static struct bt_mesh_dfu_srv dfu_srv =
BT_MESH_DFU_SRV_INIT(&dfu_handlers, dfu_imgs, ARRAY_SIZE(dfu_imgs));
#endif /* CONFIG_BT_MESH_DFU_SRV */
/* Model Authentication Method */
#define AUTH_METHOD_STATIC 0x01
#define AUTH_METHOD_OUTPUT 0x02
#define AUTH_METHOD_INPUT 0x03
static struct model_data {
const struct bt_mesh_model *model;
uint16_t addr;
uint16_t appkey_idx;
} model_bound[MODEL_BOUNDS_MAX];
static struct {
uint16_t local;
uint16_t dst;
uint16_t net_idx;
} net = {
.local = BT_MESH_ADDR_UNASSIGNED,
.dst = BT_MESH_ADDR_UNASSIGNED,
};
static uint8_t supported_commands(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
struct btp_mesh_read_supported_commands_rp *rp = rsp;
*rsp_len = tester_supported_commands(BTP_SERVICE_ID_MESH, rp->data);
*rsp_len += sizeof(*rp);
return BTP_STATUS_SUCCESS;
}
static void get_faults(uint8_t *faults, uint8_t faults_size, uint8_t *dst, uint8_t *count)
{
uint8_t i, limit = *count;
for (i = 0U, *count = 0U; i < faults_size && *count < limit; i++) {
if (faults[i]) {
*dst++ = faults[i];
(*count)++;
}
}
}
static int fault_get_cur(const struct bt_mesh_model *model, uint8_t *test_id,
uint16_t *company_id, uint8_t *faults, uint8_t *fault_count)
{
LOG_DBG("");
*test_id = HEALTH_TEST_ID;
*company_id = CID_LOCAL;
get_faults(cur_faults, sizeof(cur_faults), faults, fault_count);
return 0;
}
static int fault_get_reg(const struct bt_mesh_model *model, uint16_t company_id,
uint8_t *test_id, uint8_t *faults, uint8_t *fault_count)
{
LOG_DBG("company_id 0x%04x", company_id);
if (company_id != CID_LOCAL) {
return -EINVAL;
}
*test_id = HEALTH_TEST_ID;
get_faults(reg_faults, sizeof(reg_faults), faults, fault_count);
return 0;
}
static int fault_clear(const struct bt_mesh_model *model, uint16_t company_id)
{
LOG_DBG("company_id 0x%04x", company_id);
if (company_id != CID_LOCAL) {
return -EINVAL;
}
(void)memset(reg_faults, 0, sizeof(reg_faults));
return 0;
}
static int fault_test(const struct bt_mesh_model *model, uint8_t test_id,
uint16_t company_id)
{
LOG_DBG("test_id 0x%02x company_id 0x%04x", test_id, company_id);
if (company_id != CID_LOCAL || test_id != HEALTH_TEST_ID) {
return -EINVAL;
}
return 0;
}
static const struct bt_mesh_health_srv_cb health_srv_cb = {
.fault_get_cur = fault_get_cur,
.fault_get_reg = fault_get_reg,
.fault_clear = fault_clear,
.fault_test = fault_test,
};
static void show_faults(uint8_t test_id, uint16_t cid, uint8_t *faults, size_t fault_count)
{
size_t i;
if (!fault_count) {
LOG_DBG("Health Test ID 0x%02x Company ID 0x%04x: no faults",
test_id, cid);
return;
}
LOG_DBG("Health Test ID 0x%02x Company ID 0x%04x Fault Count %zu: ",
test_id, cid, fault_count);
for (i = 0; i < fault_count; i++) {
LOG_DBG("0x%02x", faults[i]);
}
}
static void health_current_status(struct bt_mesh_health_cli *cli, uint16_t addr,
uint8_t test_id, uint16_t cid, uint8_t *faults,
size_t fault_count)
{
LOG_DBG("Health Current Status from 0x%04x", addr);
show_faults(test_id, cid, faults, fault_count);
}
#if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_CLI)
static struct bt_mesh_large_comp_data_cli lcd_cli = {
};
#endif
static struct bt_mesh_health_cli health_cli = {
.current_status = health_current_status,
};
static uint8_t health_tests[] = {
BT_MESH_HEALTH_TEST_INFO(COMPANY_ID_LF, 6, 0x01, 0x02, 0x03, 0x04, 0x34,
0x15),
BT_MESH_HEALTH_TEST_INFO(COMPANY_ID_NORDIC_SEMI, 3, 0x01, 0x02, 0x03),
};
static const uint8_t zero_metadata[100];
static const struct bt_mesh_models_metadata_entry health_srv_meta[] = {
BT_MESH_HEALTH_TEST_INFO_METADATA(health_tests),
{
.len = ARRAY_SIZE(zero_metadata),
.id = 0xABCD,
.data = zero_metadata,
},
BT_MESH_MODELS_METADATA_END,
};
static const uint8_t health_tests_alt[] = {
BT_MESH_HEALTH_TEST_INFO(COMPANY_ID_LF, 6, 0x11, 0x22, 0x33, 0x44, 0x55,
0x66),
BT_MESH_HEALTH_TEST_INFO(COMPANY_ID_NORDIC_SEMI, 3, 0x11, 0x22, 0x33),
};
static const struct bt_mesh_models_metadata_entry health_srv_meta_alt[] = {
BT_MESH_HEALTH_TEST_INFO_METADATA(health_tests_alt),
{
.len = ARRAY_SIZE(zero_metadata),
.id = 0xFEED,
.data = zero_metadata,
},
BT_MESH_MODELS_METADATA_END,
};
static struct bt_mesh_health_srv health_srv = {
.cb = &health_srv_cb,
};
BT_MESH_HEALTH_PUB_DEFINE(health_pub, CUR_FAULTS_MAX);
static struct bt_mesh_cfg_cli cfg_cli = {
};
#if defined(CONFIG_BT_MESH_SAR_CFG_CLI)
static struct bt_mesh_sar_cfg_cli sar_cfg_cli;
#endif
#if defined(CONFIG_BT_MESH_RPR_CLI)
static void rpr_scan_report(struct bt_mesh_rpr_cli *cli,
const struct bt_mesh_rpr_node *srv,
struct bt_mesh_rpr_unprov *unprov,
struct net_buf_simple *adv_data)
{
char uuid_hex_str[32 + 1];
bin2hex(unprov->uuid, 16, uuid_hex_str, sizeof(uuid_hex_str));
LOG_DBG("Server 0x%04x:\n"
"\tuuid: %s\n"
"\tOOB: 0x%04x",
srv->addr, uuid_hex_str, unprov->oob);
while (adv_data && adv_data->len > 2) {
uint8_t len, type;
uint8_t data[31];
len = net_buf_simple_pull_u8(adv_data) - 1;
type = net_buf_simple_pull_u8(adv_data);
memcpy(data, net_buf_simple_pull_mem(adv_data, len), len);
data[len] = '\0';
if (type == BT_DATA_URI) {
LOG_DBG("\tURI: \"\\x%02x%s\"",
data[0], &data[1]);
} else if (type == BT_DATA_NAME_COMPLETE) {
LOG_DBG("\tName: \"%s\"", data);
} else {
char string[64 + 1];
bin2hex(data, len, string, sizeof(string));
LOG_DBG("\t0x%02x: %s", type, string);
}
}
}
static struct bt_mesh_rpr_cli rpr_cli = {
.scan_report = rpr_scan_report,
};
#endif
#if defined(CONFIG_BT_MESH_DFU_SRV)
static uint8_t dfu_srv_apply(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
LOG_DBG("Applying image on server");
bt_mesh_dfu_srv_applied(&dfu_srv);
return BTP_STATUS_SUCCESS;
}
#endif
#ifdef CONFIG_BT_MESH_PRIV_BEACON_CLI
static struct bt_mesh_priv_beacon_cli priv_beacon_cli;
static uint8_t priv_beacon_get(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_priv_beacon_get_cmd *cp = cmd;
struct bt_mesh_priv_beacon val;
int err;
err = bt_mesh_priv_beacon_cli_get(net.net_idx, cp->dst, &val);
if (err) {
LOG_ERR("Failed to send Private Beacon Get (err %d)", err);
return BTP_STATUS_FAILED;
}
LOG_DBG("Private Beacon state: %u, %u", val.enabled, val.rand_interval);
return BTP_STATUS_SUCCESS;
}
static uint8_t priv_beacon_set(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_priv_beacon_set_cmd *cp = cmd;
struct bt_mesh_priv_beacon val;
int err;
val.enabled = cp->enabled;
val.rand_interval = cp->rand_interval;
err = bt_mesh_priv_beacon_cli_set(net.net_idx, cp->dst, &val, &val);
if (err) {
LOG_ERR("Failed to send Private Beacon Set (err %d)", err);
return BTP_STATUS_FAILED;
}
return BTP_STATUS_SUCCESS;
}
static uint8_t priv_gatt_proxy_get(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_priv_gatt_proxy_get_cmd *cp = cmd;
uint8_t state;
int err;
err = bt_mesh_priv_beacon_cli_gatt_proxy_get(net.net_idx, cp->dst, &state);
if (err) {
LOG_ERR("Failed to send Private GATT Proxy Get (err %d)", err);
return BTP_STATUS_FAILED;
}
LOG_DBG("Private GATT Proxy state: %u", state);
return BTP_STATUS_SUCCESS;
}
static uint8_t priv_gatt_proxy_set(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_priv_gatt_proxy_set_cmd *cp = cmd;
uint8_t state;
int err;
state = cp->state;
err = bt_mesh_priv_beacon_cli_gatt_proxy_set(net.net_idx, cp->dst, state, &state);
if (err) {
LOG_ERR("Failed to send Private GATT Proxy Set (err %d)", err);
return BTP_STATUS_FAILED;
}
return BTP_STATUS_SUCCESS;
}
static uint8_t priv_node_id_get(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_priv_node_id_get_cmd *cp = cmd;
struct bt_mesh_priv_node_id val;
uint16_t key_net_idx;
int err;
key_net_idx = cp->key_net_idx;
err = bt_mesh_priv_beacon_cli_node_id_get(net.net_idx, cp->dst, key_net_idx, &val);
if (err) {
LOG_ERR("Failed to send Private Node Identity Get (err %d)", err);
return BTP_STATUS_FAILED;
}
LOG_DBG("Private Node Identity state: %u %u %u", val.net_idx, val.state, val.status);
return BTP_STATUS_SUCCESS;
}
static uint8_t priv_node_id_set(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_priv_node_id_set_cmd *cp = cmd;
struct bt_mesh_priv_node_id val;
int err;
val.net_idx = cp->net_idx;
val.state = cp->state;
err = bt_mesh_priv_beacon_cli_node_id_set(net.net_idx, cp->dst, &val, &val);
if (err) {
LOG_ERR("Failed to send Private Node Identity Set (err %d)", err);
return BTP_STATUS_FAILED;
}
return BTP_STATUS_SUCCESS;
}
#endif
#ifdef CONFIG_BT_MESH_PRIV_BEACON_SRV
static uint8_t proxy_private_identity_enable(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_proxy_priv_identity_cmd *cp = cmd;
uint16_t net_idx[CONFIG_BT_MESH_SUBNET_COUNT];
enum bt_mesh_feat_state priv_node_id = BT_MESH_FEATURE_DISABLED;
ssize_t count;
int err;
LOG_DBG("");
count = bt_mesh_subnets_get(net_idx, ARRAY_SIZE(net_idx), 0);
if (count <= 0) {
LOG_ERR("No subnet (err:%i)", count);
return BTP_STATUS_FAILED;
}
if (cp->enabled) {
priv_node_id = BT_MESH_FEATURE_ENABLED;
}
for (int i = 0; i < count; i++) {
err = bt_mesh_subnet_priv_node_id_set(net_idx[i], priv_node_id);
if (err) {
LOG_ERR("Failed to %s proxy private identity for net idx:%x (err %d)",
cp->enabled ? "enable" : "disable", net_idx[i], err);
return BTP_STATUS_FAILED;
}
}
return BTP_STATUS_SUCCESS;
}
#endif
#if defined(CONFIG_BT_MESH_SOL_PDU_RPL_CLI)
static struct bt_mesh_sol_pdu_rpl_cli srpl_cli;
#endif
#if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_CLI)
static struct bt_mesh_od_priv_proxy_cli od_priv_proxy_cli;
static uint8_t od_priv_proxy_get(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_od_priv_proxy_get_cmd *cp = cmd;
uint8_t val_rsp;
int err;
LOG_DBG("");
err = bt_mesh_od_priv_proxy_cli_get(net.net_idx, cp->dst, &val_rsp);
if (err) {
LOG_ERR("Failed to get On-Demand Private Proxy state (err %d)", err);
return BTP_STATUS_FAILED;
}
LOG_DBG("On-Demand Private Proxy state: %u", val_rsp);
return BTP_STATUS_SUCCESS;
}
static uint8_t od_priv_proxy_set(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_od_priv_proxy_set_cmd *cp = cmd;
uint8_t val_rsp;
int err;
LOG_DBG("");
err = bt_mesh_od_priv_proxy_cli_set(net.net_idx, cp->dst, cp->val, &val_rsp);
if (err) {
LOG_ERR("Failed to set On-Demand Private Proxy state (err %d)", err);
return BTP_STATUS_FAILED;
}
LOG_DBG("On-Demand Private Proxy set state: %u", val_rsp);
return BTP_STATUS_SUCCESS;
}
#endif
#if defined(CONFIG_BT_MESH_SOL_PDU_RPL_CLI)
static uint8_t srpl_clear(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_srpl_clear_cmd *cp = cmd;
uint16_t app_idx = BT_MESH_KEY_UNUSED;
uint16_t start_rsp;
uint8_t len_rsp;
int err;
/* Lookup source address */
for (int i = 0; i < ARRAY_SIZE(model_bound); i++) {
if (model_bound[i].model->id == BT_MESH_MODEL_ID_SOL_PDU_RPL_CLI) {
app_idx = model_bound[i].appkey_idx;
break;
}
}
struct bt_mesh_msg_ctx ctx = BT_MESH_MSG_CTX_INIT_APP(app_idx, cp->dst);
if (cp->acked) {
err = bt_mesh_sol_pdu_rpl_clear(&ctx, cp->range_start, cp->range_len, &start_rsp,
&len_rsp);
} else {
err = bt_mesh_sol_pdu_rpl_clear_unack(&ctx, cp->range_start, cp->range_len);
}
if (err) {
LOG_ERR("Failed to clear SRPL (err %d)", err);
return BTP_STATUS_FAILED;
}
return BTP_STATUS_SUCCESS;
}
#endif
#if defined(CONFIG_BT_MESH_PROXY_SOLICITATION)
static uint8_t proxy_solicit(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_proxy_solicit_cmd *cp = cmd;
int err;
err = bt_mesh_proxy_solicit(cp->net_idx);
if (err) {
LOG_ERR("Failed to advertise solicitation PDU (err %d)", err);
return BTP_STATUS_FAILED;
}
return BTP_STATUS_SUCCESS;
}
#endif /* CONFIG_BT_MESH_PROXY_SOLICITATION */
#if defined(CONFIG_BT_MESH_BRG_CFG_CLI)
static struct bt_mesh_brg_cfg_cli brg_cfg_cli;
#endif /* CONFIG_BT_MESH_BRG_CFG_CLI */
static const struct bt_mesh_model primary_models[] = {
BT_MESH_MODEL_CFG_SRV,
BT_MESH_MODEL_CFG_CLI(&cfg_cli),
BT_MESH_MODEL_HEALTH_SRV(&health_srv, &health_pub, health_srv_meta),
BT_MESH_MODEL_HEALTH_CLI(&health_cli),
#if defined(CONFIG_BT_MESH_SAR_CFG_SRV)
BT_MESH_MODEL_SAR_CFG_SRV,
#endif
#if defined(CONFIG_BT_MESH_SAR_CFG_CLI)
BT_MESH_MODEL_SAR_CFG_CLI(&sar_cfg_cli),
#endif
#if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_SRV)
BT_MESH_MODEL_LARGE_COMP_DATA_SRV,
#endif
#if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_CLI)
BT_MESH_MODEL_LARGE_COMP_DATA_CLI(&lcd_cli),
#endif
#if defined(CONFIG_BT_MESH_OP_AGG_SRV)
BT_MESH_MODEL_OP_AGG_SRV,
#endif
#if defined(CONFIG_BT_MESH_OP_AGG_CLI)
BT_MESH_MODEL_OP_AGG_CLI,
#endif
#if defined(CONFIG_BT_MESH_RPR_CLI)
BT_MESH_MODEL_RPR_CLI(&rpr_cli),
#endif
#if defined(CONFIG_BT_MESH_RPR_SRV)
BT_MESH_MODEL_RPR_SRV,
#endif
#if defined(CONFIG_BT_MESH_PRIV_BEACON_SRV)
BT_MESH_MODEL_PRIV_BEACON_SRV,
#endif
#if defined(CONFIG_BT_MESH_PRIV_BEACON_CLI)
BT_MESH_MODEL_PRIV_BEACON_CLI(&priv_beacon_cli),
#endif
#if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_CLI)
BT_MESH_MODEL_OD_PRIV_PROXY_CLI(&od_priv_proxy_cli),
#endif