-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
provisioner_main.c
1777 lines (1443 loc) · 46.2 KB
/
provisioner_main.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
/*
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <errno.h>
#include "mesh.h"
#include "crypto.h"
#include "adv.h"
#include "access.h"
#include "settings.h"
#include "friend.h"
#include "transport.h"
#include "foundation.h"
#include "mesh_common.h"
#include "proxy_client.h"
#include "provisioner_prov.h"
#include "provisioner_main.h"
#if CONFIG_BLE_MESH_PROVISIONER
static struct bt_mesh_node *mesh_nodes[CONFIG_BLE_MESH_MAX_PROV_NODES];
static bt_mesh_mutex_t provisioner_lock;
static uint16_t node_count;
static int provisioner_remove_node(uint16_t index, bool erase);
static inline void bt_mesh_provisioner_mutex_new(void)
{
if (!provisioner_lock.mutex) {
bt_mesh_mutex_create(&provisioner_lock);
}
}
#if CONFIG_BLE_MESH_DEINIT
static inline void bt_mesh_provisioner_mutex_free(void)
{
bt_mesh_mutex_free(&provisioner_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */
static inline void bt_mesh_provisioner_lock(void)
{
bt_mesh_mutex_lock(&provisioner_lock);
}
static inline void bt_mesh_provisioner_unlock(void)
{
bt_mesh_mutex_unlock(&provisioner_lock);
}
int bt_mesh_provisioner_init(void)
{
bt_mesh_provisioner_mutex_new();
return 0;
}
/**
* When a Provisioner tries to create a network, it will check the
* status of the restored network keys firstly, and try to create
* one if they are not existed.
*/
int bt_mesh_provisioner_net_create(void)
{
const struct bt_mesh_prov *prov = NULL;
struct bt_mesh_subnet *sub = NULL;
uint8_t p_key[16] = {0};
BT_DBG("%s", __func__);
prov = bt_mesh_provisioner_get_prov_info();
if (!prov) {
BT_ERR("No provisioning context provided");
return -EINVAL;
}
if (bt_mesh.p_sub[0]) {
/* Provisioner is already enabled (enable -> disable -> enable),
* or Provisioner is restored from flash.
*/
BT_INFO("Provisioner already created network");
sub = bt_mesh.p_sub[0];
goto done;
}
/* Generate the primary netkey */
if (bt_mesh_rand(p_key, 16)) {
BT_ERR("Failed to generate Primary NetKey");
return -EIO;
}
sub = bt_mesh_calloc(sizeof(struct bt_mesh_subnet));
if (!sub) {
BT_ERR("%s, Out of memory", __func__);
return -ENOMEM;
}
sub->kr_flag = BLE_MESH_KEY_REFRESH(prov->flags);
if (sub->kr_flag) {
if (bt_mesh_net_keys_create(&sub->keys[1], p_key)) {
BT_ERR("Failed to generate net-related keys");
bt_mesh_free(sub);
return -EIO;
}
sub->kr_phase = BLE_MESH_KR_PHASE_2;
} else {
/* Currently provisioner only use keys[0] */
if (bt_mesh_net_keys_create(&sub->keys[0], p_key)) {
BT_ERR("Failed to create net-related keys");
bt_mesh_free(sub);
return -EIO;
}
sub->kr_phase = BLE_MESH_KR_NORMAL;
}
sub->net_idx = BLE_MESH_KEY_PRIMARY;
sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED;
bt_mesh.p_sub[0] = sub;
/* Dynamically added appkey & netkey will use these key_idx */
bt_mesh.p_app_idx_next = 0x0000;
bt_mesh.p_net_idx_next = 0x0001;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_p_net_idx();
bt_mesh_store_p_app_idx();
bt_mesh_store_p_subnet(bt_mesh.p_sub[0]);
}
bt_mesh.iv_index = prov->iv_index;
bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS,
BLE_MESH_IV_UPDATE(prov->flags));
/* Set minimum required hours, since the 96-hour minimum requirement
* doesn't apply straight after provisioning (since we can't know how
* long has actually passed since the network changed its state).
* This operation is the same with node initialization.
*/
bt_mesh.ivu_duration = BLE_MESH_IVU_MIN_HOURS;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_iv(true);
}
done:
BT_INFO("NetKeyIndex 0x%03x, NID 0x%02x", sub->net_idx, sub->keys[0].nid);
BT_INFO("NetKey %s", bt_hex(sub->keys[0].net, 16));
return 0;
}
void bt_mesh_provisioner_main_reset(bool erase)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
if (bt_mesh.p_sub[i]) {
bt_mesh_provisioner_local_net_key_del(bt_mesh.p_sub[i]->net_idx, erase);
}
}
/* Clear model state that isn't otherwise cleared. E.g. AppKey
* binding and model publication is cleared as a consequence
* of removing all app keys, however model subscription clearing
* must be taken care of here.
*/
bt_mesh_mod_sub_reset(erase);
bt_mesh.p_net_idx_next = 0U;
bt_mesh.p_app_idx_next = 0U;
if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_p_net_idx();
bt_mesh_clear_p_app_idx();
}
for (i = 0; i < CONFIG_BLE_MESH_MAX_PROV_NODES; i++) {
provisioner_remove_node(i, erase);
}
node_count = 0U;
}
#if CONFIG_BLE_MESH_DEINIT
int bt_mesh_provisioner_deinit(bool erase)
{
bt_mesh_provisioner_main_reset(erase);
bt_mesh_provisioner_mutex_free();
return 0;
}
#endif /* CONFIG_BLE_MESH_DEINIT */
bool bt_mesh_provisioner_check_is_addr_dup(uint16_t addr, uint8_t elem_num, bool comp_with_own)
{
const struct bt_mesh_comp *comp = NULL;
struct bt_mesh_node *node = NULL;
uint16_t primary_addr = BLE_MESH_ADDR_UNASSIGNED;
uint16_t comp_addr = BLE_MESH_ADDR_UNASSIGNED;
int i;
if (comp_with_own) {
comp = bt_mesh_comp_get();
if (!comp) {
BT_ERR("Invalid composition data");
return true;
}
primary_addr = bt_mesh_provisioner_get_primary_elem_addr();
if (!BLE_MESH_ADDR_IS_UNICAST(primary_addr)) {
BT_ERR("Invalid unicast address 0x%04x", primary_addr);
return true;
}
}
for (comp_addr = addr; comp_addr < addr + elem_num; comp_addr++) {
for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) {
node = mesh_nodes[i];
if (node && comp_addr >= node->unicast_addr &&
comp_addr < node->unicast_addr + node->element_num) {
BT_ERR("Duplicate with node address 0x%04x", comp_addr);
return true;
}
if (comp_with_own && comp_addr >= primary_addr &&
comp_addr < primary_addr + comp->elem_count) {
BT_ERR("Duplicate with Provisioner address 0x%04x", comp_addr);
return true;
}
}
}
return false;
}
static void provisioner_node_count_inc(void)
{
node_count++;
}
static void provisioner_node_count_dec(void)
{
if (node_count) {
node_count--;
}
}
uint16_t bt_mesh_provisioner_get_node_count(void)
{
return node_count;
}
static int provisioner_store_node(struct bt_mesh_node *node, bool store, uint16_t *index)
{
int i;
bt_mesh_provisioner_lock();
/* Check if the node already exists */
for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) {
if (mesh_nodes[i] && !memcmp(mesh_nodes[i]->dev_uuid, node->dev_uuid, 16)) {
BT_WARN("Node already exists, uuid %s", bt_hex(node->dev_uuid, 16));
bt_mesh_provisioner_unlock();
return -EEXIST;
}
}
for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) {
if (mesh_nodes[i] == NULL) {
mesh_nodes[i] = bt_mesh_calloc(sizeof(struct bt_mesh_node));
if (!mesh_nodes[i]) {
BT_ERR("%s, Out of memory", __func__);
bt_mesh_provisioner_unlock();
return -ENOMEM;
}
memcpy(mesh_nodes[i], node, sizeof(struct bt_mesh_node));
provisioner_node_count_inc();
if (index) {
*index = i;
}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) {
bt_mesh_store_node_info(mesh_nodes[i]);
}
bt_mesh_provisioner_unlock();
return 0;
}
}
BT_ERR("Node is full!");
bt_mesh_provisioner_unlock();
return -ENOMEM;
}
int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node)
{
if (!node) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
return provisioner_store_node(node, false, NULL);
}
int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const uint8_t uuid[16],
uint16_t oob_info, uint16_t unicast_addr,
uint8_t element_num, uint16_t net_idx,
uint8_t flags, uint32_t iv_index,
const uint8_t dev_key[16], uint16_t *index)
{
struct bt_mesh_node node = {0};
BT_DBG("%s", __func__);
if (!addr || !uuid || !dev_key || !index) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
BT_INFO("Unicast addr 0x%04x, element num %d, NetKeyIndex 0x%04x",
unicast_addr, element_num, net_idx);
BT_INFO("UUID %s", bt_hex(uuid, 16));
BT_INFO("DevKey %s", bt_hex(dev_key, 16));
memcpy(node.addr, addr->val, BLE_MESH_ADDR_LEN);
node.addr_type = addr->type;
memcpy(node.dev_uuid, uuid, 16);
node.oob_info = oob_info;
node.unicast_addr = unicast_addr;
node.element_num = element_num;
node.net_idx = net_idx;
node.flags = flags;
node.iv_index = iv_index;
memcpy(node.dev_key, dev_key, 16);
return provisioner_store_node(&node, true, index);
}
static int provisioner_remove_node(uint16_t index, bool erase)
{
struct bt_mesh_node *node = NULL;
int i;
BT_DBG("Remove node %d", index);
bt_mesh_provisioner_lock();
if (mesh_nodes[index] == NULL) {
bt_mesh_provisioner_unlock();
return 0;
}
node = mesh_nodes[index];
/* Reset corresponding network cache when reset the node */
bt_mesh_msg_cache_clear(node->unicast_addr, node->element_num);
/* Reset corresponding transport info when removing the node */
for (i = 0; i < node->element_num; i++) {
bt_mesh_rx_reset_single(node->unicast_addr + i);
}
for (i = 0; i < node->element_num; i++) {
bt_mesh_tx_reset_single(node->unicast_addr + i);
}
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
bt_mesh_friend_remove_lpn(node->unicast_addr);
}
if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_node_info(node->unicast_addr);
}
if (mesh_nodes[index]->comp_data) {
bt_mesh_free(mesh_nodes[index]->comp_data);
}
bt_mesh_free(mesh_nodes[index]);
mesh_nodes[index] = NULL;
provisioner_node_count_dec();
bt_mesh_provisioner_unlock();
return 0;
}
static struct bt_mesh_node *provisioner_find_node_with_uuid(const uint8_t uuid[16], uint16_t *index)
{
int i;
BT_DBG("%s", __func__);
if (uuid == NULL) {
BT_ERR("Invalid device uuid");
return NULL;
}
bt_mesh_provisioner_lock();
for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) {
if (mesh_nodes[i] && !memcmp(mesh_nodes[i]->dev_uuid, uuid, 16)) {
if (index) {
*index = i;
}
bt_mesh_provisioner_unlock();
return mesh_nodes[i];
}
}
bt_mesh_provisioner_unlock();
return NULL;
}
int bt_mesh_provisioner_remove_node(const uint8_t uuid[16])
{
struct bt_mesh_node *node = NULL;
uint16_t index = 0U;
int i;
if (uuid == NULL) {
for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) {
if (mesh_nodes[i]) {
provisioner_remove_node(i, true);
}
}
return 0;
}
node = provisioner_find_node_with_uuid(uuid, &index);
if (!node) {
BT_WARN("Node not found, uuid %s", bt_hex(uuid, 16));
return -ENODEV;
}
provisioner_remove_node(index, true);
return 0;
}
static struct bt_mesh_node *provisioner_find_node_with_addr(uint16_t addr, uint16_t *index)
{
struct bt_mesh_node *node = NULL;
int i;
BT_DBG("%s", __func__);
if (!BLE_MESH_ADDR_IS_UNICAST(addr)) {
BT_ERR("Invalid unicast address 0x%04x", addr);
return NULL;
}
bt_mesh_provisioner_lock();
for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) {
node = mesh_nodes[i];
if (node && addr >= node->unicast_addr &&
addr < (node->unicast_addr + node->element_num)) {
if (index) {
*index = i;
}
bt_mesh_provisioner_unlock();
return node;
}
}
bt_mesh_provisioner_unlock();
return NULL;
}
int bt_mesh_provisioner_restore_node_name(uint16_t addr, const char *name)
{
struct bt_mesh_node *node = NULL;
node = provisioner_find_node_with_addr(addr, NULL);
if (node == NULL) {
BT_ERR("Node not found, addr 0x%04x", addr);
return -ENODEV;
}
strncpy(node->name, name, BLE_MESH_NODE_NAME_SIZE);
node->name[BLE_MESH_NODE_NAME_SIZE] = 0;
return 0;
}
struct bt_mesh_node *bt_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16])
{
return provisioner_find_node_with_uuid(uuid, NULL);
}
struct bt_mesh_node *bt_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr)
{
return provisioner_find_node_with_addr(unicast_addr, NULL);
}
int bt_mesh_provisioner_delete_node_with_uuid(const uint8_t uuid[16])
{
struct bt_mesh_node *node = NULL;
uint16_t index = 0U;
node = provisioner_find_node_with_uuid(uuid, &index);
if (!node) {
BT_WARN("Node not found, uuid %s", bt_hex(uuid, 16));
return -ENODEV;
}
provisioner_remove_node(index, true);
return 0;
}
int bt_mesh_provisioner_delete_node_with_node_addr(uint16_t unicast_addr)
{
struct bt_mesh_node *node = NULL;
uint16_t index = 0U;
node = provisioner_find_node_with_addr(unicast_addr, &index);
if (!node) {
BT_WARN("Node not found, addr 0x%04x", unicast_addr);
return -ENODEV;
}
provisioner_remove_node(index, true);
return 0;
}
int bt_mesh_provisioner_delete_node_with_dev_addr(const bt_mesh_addr_t *addr)
{
int i;
for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) {
if (mesh_nodes[i] && mesh_nodes[i]->addr_type == addr->type &&
!memcmp(mesh_nodes[i]->addr, addr->val, BLE_MESH_ADDR_LEN)) {
return provisioner_remove_node(i, true);
}
}
BT_WARN("Node not exist, device address %s", bt_hex(addr->val, BLE_MESH_ADDR_LEN));
return -ENODEV;
}
static struct bt_mesh_node **provisioner_find_node_with_name(const char *name)
{
size_t length = 0U;
int i;
BT_DBG("node name %s", name);
length = MIN(strlen(name), BLE_MESH_NODE_NAME_SIZE);
bt_mesh_provisioner_lock();
for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) {
if (mesh_nodes[i]) {
if (strlen(mesh_nodes[i]->name) != length) {
continue;
}
if (!strncmp(mesh_nodes[i]->name, name, length)) {
bt_mesh_provisioner_unlock();
return &mesh_nodes[i];
}
}
}
bt_mesh_provisioner_unlock();
return NULL;
}
int bt_mesh_provisioner_set_node_name(uint16_t index, const char *name)
{
if (index >= ARRAY_SIZE(mesh_nodes)) {
BT_ERR("Invalid node index %d", index);
return -EINVAL;
}
if (mesh_nodes[index] == NULL) {
BT_ERR("Node not exists, index %d", index);
return -EINVAL;
}
if (name == NULL) {
BT_ERR("Invalid node name");
return -EINVAL;
}
if (provisioner_find_node_with_name(name)) {
BT_WARN("Node name \"%s\" already exists", name);
return -EEXIST;
}
memset(mesh_nodes[index]->name, 0, sizeof(mesh_nodes[index]->name));
strncpy(mesh_nodes[index]->name, name, BLE_MESH_NODE_NAME_SIZE);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_node_name(mesh_nodes[index]);
}
return 0;
}
const char *bt_mesh_provisioner_get_node_name(uint16_t index)
{
if (index >= ARRAY_SIZE(mesh_nodes)) {
BT_ERR("Invalid node index %d", index);
return NULL;
}
if (mesh_nodes[index] == NULL) {
BT_ERR("Node not exists, index %d", index);
return NULL;
}
return mesh_nodes[index]->name;
}
uint16_t bt_mesh_provisioner_get_node_index(const char *name)
{
struct bt_mesh_node **node = NULL;
if (name == NULL) {
BT_ERR("Invalid node name");
return BLE_MESH_INVALID_NODE_INDEX;
}
node = provisioner_find_node_with_name(name);
if (node == NULL) {
BT_ERR("Node name \"%s\" not exists", name);
return BLE_MESH_INVALID_NODE_INDEX;
}
return (node - mesh_nodes);
}
struct bt_mesh_node *bt_mesh_provisioner_get_node_with_name(const char *name)
{
struct bt_mesh_node **node = NULL;
if (name == NULL) {
BT_ERR("Invalid node name");
return NULL;
}
node = provisioner_find_node_with_name(name);
if (node == NULL) {
BT_ERR("Node name \"%s\" not exists", name);
return NULL;
}
return *node;
}
const struct bt_mesh_node **bt_mesh_provisioner_get_node_table_entry(void)
{
return (const struct bt_mesh_node **)mesh_nodes;
}
#define COMP_DATA_PAGE_0_MIN_LEN 16
static int store_node_comp_data(uint16_t addr, const uint8_t *data, uint16_t length, bool store)
{
struct bt_mesh_node *node = NULL;
if (!BLE_MESH_ADDR_IS_UNICAST(addr)) {
BT_ERR("Invalid unicast address 0x%04x", addr);
return -EINVAL;
}
if (data == NULL || (length % 2) || length < COMP_DATA_PAGE_0_MIN_LEN) {
BT_ERR("Invalid composition data");
return -EINVAL;
}
node = provisioner_find_node_with_addr(addr, NULL);
if (node == NULL) {
BT_ERR("Node not found, addr 0x%04x", addr);
return -ENODEV;
}
node->comp_data = bt_mesh_calloc(length);
if (node->comp_data == NULL) {
BT_ERR("%s, Out of memory", __func__);
return -ENOMEM;
}
memcpy(node->comp_data, data, length);
node->comp_length = length;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) {
bt_mesh_store_node_comp_data(node);
}
return 0;
}
int bt_mesh_provisioner_store_node_comp_data(uint16_t addr, const uint8_t *data, uint16_t length)
{
return store_node_comp_data(addr, data, length, true);
}
int bt_mesh_provisioner_restore_node_comp_data(uint16_t addr, const uint8_t *data, uint16_t length)
{
return store_node_comp_data(addr, data, length, false);
}
/* Provisioner DevKey, NetKey and AppKey related functions */
const uint8_t *bt_mesh_provisioner_net_key_get(uint16_t net_idx)
{
struct bt_mesh_subnet *sub = NULL;
int i;
BT_DBG("%s", __func__);
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
sub = bt_mesh.p_sub[i];
if (sub && sub->net_idx == net_idx) {
if (sub->kr_flag) {
return sub->keys[1].net;
} else {
return sub->keys[0].net;
}
}
}
return NULL;
}
struct bt_mesh_subnet *bt_mesh_provisioner_subnet_get(uint16_t net_idx)
{
struct bt_mesh_subnet *sub = NULL;
int i;
BT_DBG("%s", __func__);
if (net_idx == BLE_MESH_KEY_ANY) {
return bt_mesh.p_sub[0];
}
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
sub = bt_mesh.p_sub[i];
if (sub && sub->net_idx == net_idx) {
return sub;
}
}
return NULL;
}
bool bt_mesh_provisioner_check_msg_dst(uint16_t dst)
{
struct bt_mesh_node *node = NULL;
int i;
BT_DBG("%s", __func__);
if (!BLE_MESH_ADDR_IS_UNICAST(dst)) {
return true;
}
for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) {
node = mesh_nodes[i];
if (node && dst >= node->unicast_addr &&
dst < node->unicast_addr + node->element_num) {
return true;
}
}
return false;
}
const uint8_t *bt_mesh_provisioner_dev_key_get(uint16_t dst)
{
/* Device key is only used to encrypt configuration messages.
* Configuration model shall only be supported by the primary
* element which uses the primary unicast address.
*/
struct bt_mesh_node *node = NULL;
int i;
BT_DBG("%s", __func__);
if (!BLE_MESH_ADDR_IS_UNICAST(dst)) {
BT_ERR("Invalid unicast address 0x%04x", dst);
return NULL;
}
for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) {
node = mesh_nodes[i];
if (node && node->unicast_addr == dst) {
return node->dev_key;
}
}
return NULL;
}
struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(uint16_t app_idx)
{
struct bt_mesh_app_key *key = NULL;
int i;
BT_DBG("%s", __func__);
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) {
key = bt_mesh.p_app_keys[i];
if (key && key->net_idx != BLE_MESH_KEY_UNUSED &&
key->app_idx == app_idx) {
return key;
}
}
return NULL;
}
static int provisioner_check_app_key(const uint8_t app_key[16], uint16_t *app_idx)
{
struct bt_mesh_app_key *key = NULL;
int i;
if (!app_key) {
return 0;
}
/* Check if app_key is already existed */
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) {
key = bt_mesh.p_app_keys[i];
if (key && (!memcmp(key->keys[0].val, app_key, 16) ||
!memcmp(key->keys[1].val, app_key, 16))) {
*app_idx = key->app_idx;
return -EEXIST;
}
}
return 0;
}
static int provisioner_check_app_idx(uint16_t app_idx, bool exist)
{
struct bt_mesh_app_key *key = NULL;
int i;
if (exist) {
/* Check if app_idx is already existed */
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) {
key = bt_mesh.p_app_keys[i];
if (key && (key->app_idx == app_idx)) {
return -EEXIST;
}
}
return 0;
}
/* Check if app_idx is not existed */
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) {
key = bt_mesh.p_app_keys[i];
if (key && (key->app_idx == app_idx)) {
return 0;
}
}
return -ENODEV;
}
static int provisioner_check_app_key_full(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) {
if (!bt_mesh.p_app_keys[i]) {
return i;
}
}
return -ENOMEM;
}
static int provisioner_check_net_key(const uint8_t net_key[16], uint16_t *net_idx)
{
struct bt_mesh_subnet *sub = NULL;
int i;
if (!net_key) {
return 0;
}
/* Check if net_key is already existed */
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
sub = bt_mesh.p_sub[i];
if (sub && (!memcmp(sub->keys[0].net, net_key, 16) ||
!memcmp(sub->keys[1].net, net_key, 16))) {
*net_idx = sub->net_idx;
return -EEXIST;
}
}
return 0;
}
static int provisioner_check_net_idx(uint16_t net_idx, bool exist)
{
struct bt_mesh_subnet *sub = NULL;
int i;
if (exist) {
/* Check if net_idx is already existed */
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
sub = bt_mesh.p_sub[i];
if (sub && (sub->net_idx == net_idx)) {
return -EEXIST;
}
}
return 0;
}
/* Check if net_idx is not existed */
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
sub = bt_mesh.p_sub[i];
if (sub && (sub->net_idx == net_idx)) {
return 0;
}
}
return -ENODEV;
}
static int provisioner_check_net_key_full(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
if (!bt_mesh.p_sub[i]) {
return i;
}
}
return -ENOMEM;
}
int bt_mesh_provisioner_local_app_key_add(const uint8_t app_key[16],
uint16_t net_idx, uint16_t *app_idx)
{
struct bt_mesh_app_keys *keys = NULL;
struct bt_mesh_app_key *key = NULL;
uint8_t p_key[16] = {0};
int add = -1;
if (bt_mesh.p_app_idx_next >= 0x1000) {
BT_ERR("No AppKeyIndex available");
return -EIO;
}
if (!app_idx || (*app_idx != 0xFFFF && *app_idx >= 0x1000)) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
/* Check if the same application key already exists */
if (provisioner_check_app_key(app_key, app_idx)) {
BT_WARN("AppKey exists, AppKeyIndex updated");
return 0;
}
/* Check if the net_idx exists */
if (provisioner_check_net_idx(net_idx, false)) {
BT_ERR("Invalid NetKeyIndex 0x%04x", net_idx);
return -ENODEV;
}
/* Check if the same app_idx already exists */
if (provisioner_check_app_idx(*app_idx, true)) {
BT_ERR("Invalid AppKeyIndex 0x%04x", *app_idx);
return -EEXIST;
}
add = provisioner_check_app_key_full();
if (add < 0) {
BT_ERR("AppKey is full!");
return -ENOMEM;
}
if (!app_key) {
if (bt_mesh_rand(p_key, 16)) {
BT_ERR("Failed to generate AppKey");
return -EIO;
}
} else {
memcpy(p_key, app_key, 16);
}
key = bt_mesh_calloc(sizeof(struct bt_mesh_app_key));
if (!key) {
BT_ERR("%s, Out of memory", __func__);
return -ENOMEM;
}
keys = &key->keys[0];
if (bt_mesh_app_id(p_key, &keys->id)) {
BT_ERR("Failed to generate AID");