/
settings.c
2584 lines (2089 loc) · 66.8 KB
/
settings.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) 2018 Intel Corporation
* Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <string.h>
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_SETTINGS)
#include "mesh.h"
#include "crypto.h"
#include "transport.h"
#include "access.h"
#include "foundation.h"
#include "proxy_server.h"
#include "cfg_srv.h"
#include "mesh_common.h"
#include "settings_nvs.h"
#include "provisioner_main.h"
#include "provisioner_prov.h"
/* BLE Mesh NVS Key and corresponding data struct.
* Note: The length of nvs key must be <= 15.
* "xxxx" (2 octet) means the rpl_src, net_idx, app_idx, model_key, etc.
* Model model_key is a combination "elem_idx << 8 | model_idx".
* key: "mesh/net" -> write/read to set/get NET data
* key: "mesh/iv" -> write/read to set/get IV data
* key: "mesh/seq" -> write/read to set/get SEQ data
* key: "mesh/hb_pub" -> write/read to set/get CFG HB_PUB data
* key: "mesh/cfg" -> write/read to set/get CFG data
* key: "mesh/rpl" -> write/read to set/get all RPL src.
* key: "mesh/rpl/xxxx" -> write/read to set/get the "xxxx" RPL data
* key: "mesh/netkey" -> write/read to set/get all NetKey Indexes
* key: "mesh/nk/xxxx" -> write/read to set/get the "xxxx" NetKey data
* key: "mesh/appkey" -> write/read to set/get all AppKey Indexes
* key: "mesh/ak/xxxx" -> write/read to set/get the "xxxx" AppKey data
* key: "mesh/sig" -> write/read to set/get all SIG MODEL model_keys.
* key: "mesh/s/xxxx/b" -> write/read to set/get SIG MODEL Bind AppKey List
* key: "mesh/s/xxxx/s" -> write/read to set/get SIG MODEL Subscription List
* key: "mesh/s/xxxx/p" -> write/read to set/get SIG MODEL Publication
* key: "mesh/vnd" -> write/read to set/get all VENDOR MODEL model_keys.
* key: "mesh/v/xxxx/b" -> write/read to set/get VENDOR MODEL Bind AppKey List
* key: "mesh/v/xxxx/s" -> write/read to set/get VENDOR MODEL Subscription List
* key: "mesh/v/xxxx/p" -> write/read to set/get VENDOR MODEL Publication
* key: "mesh/vaddr" -> write/read to set/get all virtual addresses
* key: "mesh/va/xxxx" -> write/read to set/get the "xxxx" virtual address
*/
#if CONFIG_BLE_MESH_SETTINGS
/* Tracking of what storage changes are pending for App and Net Keys. We
* track this in a separate array here instead of within the respective
* bt_mesh_app_key and bt_mesh_subnet structs themselves, since once a key
* gets deleted its struct becomes invalid and may be reused for other keys.
*/
static struct key_update {
u16_t key_idx: 12, /* AppKey or NetKey Index */
valid: 1, /* 1 if this entry is valid, 0 if not */
app_key: 1, /* 1 if this is an AppKey, 0 if a NetKey */
clear: 1; /* 1 if key needs clearing, 0 if storing */
} key_updates[CONFIG_BLE_MESH_APP_KEY_COUNT + CONFIG_BLE_MESH_SUBNET_COUNT];
static struct k_delayed_work pending_store;
/* Mesh network storage information */
struct net_val {
u16_t primary_addr;
u8_t dev_key[16];
} __packed;
/* Sequence number storage */
struct seq_val {
u8_t val[3];
} __packed;
/* Heartbeat Publication storage */
struct hb_pub_val {
u16_t dst;
u8_t period;
u8_t ttl;
u16_t feat;
u16_t net_idx: 12,
indefinite: 1;
};
/* Miscelaneous configuration server model states */
struct cfg_val {
u8_t net_transmit;
u8_t relay;
u8_t relay_retransmit;
u8_t beacon;
u8_t gatt_proxy;
u8_t frnd;
u8_t default_ttl;
};
/* IV Index & IV Update storage */
struct iv_val {
u32_t iv_index;
u8_t iv_update: 1,
iv_duration: 7;
} __packed;
/* Replay Protection List storage */
struct rpl_val {
u32_t seq: 24,
old_iv: 1;
};
/* NetKey storage information */
struct net_key_val {
u8_t kr_flag: 1,
kr_phase: 7;
u8_t val[2][16];
} __packed;
/* AppKey storage information */
struct app_key_val {
u16_t net_idx;
bool updated;
u8_t val[2][16];
} __packed;
struct mod_pub_val {
u16_t addr;
u16_t key;
u8_t ttl;
u8_t retransmit;
u8_t period;
u8_t period_div: 4,
cred: 1;
};
/* Virtual Address information */
struct va_val {
u16_t ref;
u16_t addr;
u8_t uuid[16];
} __packed;
/* We need this so we don't overwrite app-hardcoded values in case FCB
* contains a history of changes but then has a NULL at the end.
*/
static struct {
bool valid;
struct cfg_val cfg;
} stored_cfg;
struct prov_info {
u16_t primary_addr;
u16_t alloc_addr;
};
struct node_info {
u8_t addr[6];
u8_t addr_type;
u8_t dev_uuid[16];
u16_t oob_info;
u16_t unicast_addr;
u8_t element_num;
u16_t net_idx;
u8_t flags;
u32_t iv_index;
u8_t dev_key[16];
} __packed;
#define DEVICE_ROLE_BITS (BIT(BLE_MESH_NODE) | BIT(BLE_MESH_PROVISIONER))
static int role_set(const char *name)
{
bool exist = false;
int err = 0;
BT_DBG("%s", __func__);
err = bt_mesh_load_core_settings(name, (u8_t *)bt_mesh.flags, sizeof(bt_mesh.flags), &exist);
if (err) {
return err;
}
if (exist == false) {
return 0;
}
BT_INFO("Restored mesh device role %lu", bt_mesh_atomic_get(bt_mesh.flags) & DEVICE_ROLE_BITS);
return 0;
}
static int net_set(const char *name)
{
struct net_val net = {0};
bool exist = false;
int err = 0;
BT_DBG("%s", __func__);
err = bt_mesh_load_core_settings(name, (u8_t *)&net, sizeof(net), &exist);
if (err) {
BT_WARN("%s, Clear NET", __func__);
memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key));
bt_mesh_comp_unprovision();
return 0;
}
if (exist == false) {
return 0;
}
memcpy(bt_mesh.dev_key, net.dev_key, sizeof(bt_mesh.dev_key));
bt_mesh_comp_provision(net.primary_addr);
BT_INFO("Restored primary address 0x%04x", net.primary_addr);
BT_INFO("Restored DevKey %s", bt_hex(bt_mesh.dev_key, 16));
return 0;
}
static int iv_set(const char *name)
{
struct iv_val iv = {0};
bool exist = false;
int err = 0;
BT_DBG("%s", __func__);
err = bt_mesh_load_core_settings(name, (u8_t *)&iv, sizeof(iv), &exist);
if (err) {
BT_WARN("%s, Clear IV", __func__);
bt_mesh.iv_index = 0U;
bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS);
return 0;
}
if (exist == false) {
return 0;
}
bt_mesh.iv_index = iv.iv_index;
bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, iv.iv_update);
bt_mesh.ivu_duration = iv.iv_duration;
BT_INFO("Restored IV Index 0x%04x (IV Update Flag %u) duration %u hours",
iv.iv_index, iv.iv_update, iv.iv_duration);
return 0;
}
static int seq_set(const char *name)
{
struct seq_val seq = {0};
bool exist = false;
int err = 0;
BT_DBG("%s", __func__);
err = bt_mesh_load_core_settings(name, (u8_t *)&seq, sizeof(seq), &exist);
if (err) {
BT_WARN("%s, Clear SEQ", __func__);
bt_mesh.seq = 0U;
return 0;
}
if (exist == false) {
return 0;
}
bt_mesh.seq = ((u32_t)seq.val[0] | ((u32_t)seq.val[1] << 8) |
((u32_t)seq.val[2] << 16));
#if CONFIG_BLE_MESH_SEQ_STORE_RATE > 0
/* Make sure we have a large enough sequence number. We
* subtract 1 so that the first transmission causes a write
* to the settings storage.
*/
bt_mesh.seq += (CONFIG_BLE_MESH_SEQ_STORE_RATE -
(bt_mesh.seq % CONFIG_BLE_MESH_SEQ_STORE_RATE));
bt_mesh.seq--;
#endif
BT_INFO("Restored Sequence Number 0x%06x", bt_mesh.seq);
return 0;
}
static struct bt_mesh_rpl *rpl_find(u16_t src)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
if (bt_mesh.rpl[i].src == src) {
return &bt_mesh.rpl[i];
}
}
return NULL;
}
static struct bt_mesh_rpl *rpl_alloc(u16_t src)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
if (bt_mesh.rpl[i].src == BLE_MESH_ADDR_UNASSIGNED) {
bt_mesh.rpl[i].src = src;
return &bt_mesh.rpl[i];
}
}
return NULL;
}
static int rpl_set(const char *name)
{
struct net_buf_simple *buf = NULL;
struct bt_mesh_rpl *entry = NULL;
struct rpl_val rpl = {0};
char get[16] = {'\0'};
bool exist = false;
size_t length = 0U;
int err = 0;
int i;
BT_DBG("%s", __func__);
buf = bt_mesh_get_core_settings_item(name);
if (!buf) {
return 0;
}
length = buf->len;
for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
u16_t src = net_buf_simple_pull_le16(buf);
sprintf(get, "mesh/rpl/%04x", src);
err = bt_mesh_load_core_settings(get, (u8_t *)&rpl, sizeof(rpl), &exist);
if (err) {
BT_ERR("%s, Failed to load RPL %s, reset RPL", __func__, get);
bt_mesh_rpl_reset();
goto free;
}
if (exist == false) {
continue;
}
entry = rpl_find(src);
if (!entry) {
entry = rpl_alloc(src);
if (!entry) {
BT_ERR("%s, No space for a new RPL 0x%04x", __func__, src);
err = -ENOMEM;
goto free;
}
}
BT_INFO("Restored RPL 0x%04x: Seq 0x%06x, old_iv %u", src, rpl.seq, rpl.old_iv);
entry->src = src;
entry->seq = rpl.seq;
entry->old_iv = rpl.old_iv;
}
free:
bt_mesh_free_buf(buf);
return err;
}
static struct bt_mesh_subnet *subnet_alloc(u16_t net_idx)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
if (bt_mesh.sub[i].net_idx == BLE_MESH_KEY_UNUSED) {
bt_mesh.sub[i].net_idx = net_idx;
return &bt_mesh.sub[i];
}
}
return NULL;
}
static int net_key_set(const char *name)
{
struct net_buf_simple *buf = NULL;
struct bt_mesh_subnet *sub = NULL;
struct net_key_val key = {0};
char get[16] = {'\0'};
bool exist = false;
size_t length = 0U;
int err = 0;
int i;
BT_DBG("%s", __func__);
buf = bt_mesh_get_core_settings_item(name);
if (!buf) {
return 0;
}
length = buf->len;
for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
u16_t net_idx = net_buf_simple_pull_le16(buf);
sprintf(get, "mesh/nk/%04x", net_idx);
err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist);
if (err) {
BT_ERR("%s, Failed to load NetKey %s", __func__, get);
goto free;
}
if (exist == false) {
continue;
}
sub = bt_mesh_subnet_get(net_idx);
if (!sub) {
sub = subnet_alloc(net_idx);
if (!sub) {
BT_ERR("%s, No space for a new subnet 0x%03x", __func__, net_idx);
err = -ENOMEM;
goto free;
}
}
BT_INFO("Restored NetKey Index 0x%03x", net_idx);
sub->net_idx = net_idx;
sub->kr_flag = key.kr_flag;
sub->kr_phase = key.kr_phase;
memcpy(sub->keys[0].net, &key.val[0], 16);
memcpy(sub->keys[1].net, &key.val[1], 16);
}
free:
bt_mesh_free_buf(buf);
return err;
}
static int app_key_set(const char *name)
{
struct bt_mesh_app_key *app = NULL;
struct bt_mesh_subnet *sub = NULL;
struct net_buf_simple *buf = NULL;
struct app_key_val key = {0};
char get[16] = {'\0'};
bool exist = false;
size_t length = 0U;
int err = 0;
int i;
BT_DBG("%s", __func__);
buf = bt_mesh_get_core_settings_item(name);
if (!buf) {
return 0;
}
length = buf->len;
for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
u16_t app_idx = net_buf_simple_pull_le16(buf);
sprintf(get, "mesh/ak/%04x", app_idx);
err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist);
if (err) {
BT_ERR("%s, Failed to load AppKey %s", __func__, get);
goto free;
}
if (exist == false) {
continue;
}
sub = bt_mesh_subnet_get(key.net_idx);
if (!sub) {
BT_ERR("%s, Failed to find subnet 0x%03x", __func__, key.net_idx);
err = -ENOENT;
goto free;
}
app = bt_mesh_app_key_find(app_idx);
if (!app) {
app = bt_mesh_app_key_alloc(app_idx);
if (!app) {
BT_ERR("%s, No space for a new app key 0x%03x", __func__, app_idx);
err = -ENOMEM;
goto free;
}
}
BT_INFO("Restored AppKey Index 0x%03x", app_idx);
app->net_idx = key.net_idx;
app->app_idx = app_idx;
app->updated = key.updated;
memcpy(app->keys[0].val, key.val[0], 16);
memcpy(app->keys[1].val, key.val[1], 16);
bt_mesh_app_id(app->keys[0].val, &app->keys[0].id);
bt_mesh_app_id(app->keys[1].val, &app->keys[1].id);
}
free:
bt_mesh_free_buf(buf);
return err;
}
static int hb_pub_set(const char *name)
{
struct bt_mesh_hb_pub *hb_pub = bt_mesh_hb_pub_get();
struct hb_pub_val hb_val = {0};
bool exist = false;
int err = 0;
BT_DBG("%s", __func__);
if (!hb_pub) {
BT_ERR("%s, NULL cfg hb pub", __func__);
return -EINVAL;
}
err = bt_mesh_load_core_settings(name, (u8_t *)&hb_val, sizeof(hb_val), &exist);
if (err) {
BT_WARN("%s, Cleared heartbeat publication", __func__);
hb_pub->dst = BLE_MESH_ADDR_UNASSIGNED;
hb_pub->count = 0U;
hb_pub->ttl = 0U;
hb_pub->period = 0U;
hb_pub->feat = 0U;
return 0;
}
if (exist == false) {
return 0;
}
hb_pub->dst = hb_val.dst;
hb_pub->period = hb_val.period;
hb_pub->ttl = hb_val.ttl;
hb_pub->feat = hb_val.feat;
hb_pub->net_idx = hb_val.net_idx;
if (hb_val.indefinite) {
hb_pub->count = 0xffff;
} else {
hb_pub->count = 0U;
}
BT_INFO("Restored heartbeat publication, dst 0x%04x", hb_pub->dst);
return 0;
}
static int cfg_set(const char *name)
{
struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get();
struct cfg_val val = {0};
bool exist = false;
int err = 0;
BT_DBG("%s", __func__);
if (!cfg) {
BT_ERR("%s, NULL cfg", __func__);
stored_cfg.valid = false;
return -EINVAL;
}
err = bt_mesh_load_core_settings(name, (u8_t *)&val, sizeof(val), &exist);
if (err) {
BT_WARN("%s, Cleared configuration", __func__);
stored_cfg.valid = false;
return 0;
}
if (exist == false) {
return 0;
}
memcpy(&stored_cfg.cfg, &val, sizeof(val));
stored_cfg.valid = true;
BT_INFO("Restored configuration state");
return 0;
}
static int model_set_bind(bool vnd, struct bt_mesh_model *model, u16_t model_key)
{
char name[16] = {'\0'};
bool exist = false;
int err = 0;
int i;
/* Start with empty array regardless of cleared or set value */
for (i = 0; i < ARRAY_SIZE(model->keys); i++) {
model->keys[i] = BLE_MESH_KEY_UNUSED;
}
sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key);
err = bt_mesh_load_core_settings(name, (u8_t *)model->keys, sizeof(model->keys), &exist);
if (err) {
BT_ERR("%s, Failed to get model bind keys", __func__);
return -EIO;
}
return 0;
}
static int model_set_sub(bool vnd, struct bt_mesh_model *model, u16_t model_key)
{
char name[16] = {'\0'};
bool exist = false;
int err = 0;
int i;
/* Start with empty array regardless of cleared or set value */
for (i = 0; i < ARRAY_SIZE(model->groups); i++) {
model->groups[i] = BLE_MESH_ADDR_UNASSIGNED;
}
sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key);
err = bt_mesh_load_core_settings(name, (u8_t *)model->groups, sizeof(model->groups), &exist);
if (err) {
BT_ERR("%s, Failed to get model subscriptions", __func__);
return -EIO;
}
return 0;
}
static int model_set_pub(bool vnd, struct bt_mesh_model *model, u16_t model_key)
{
struct mod_pub_val pub = {0};
char name[16] = {'\0'};
bool exist = false;
int err = 0;
if (!model->pub) {
BT_WARN("%s, Model has no publication context", __func__);
return 0;
}
sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key);
err = bt_mesh_load_core_settings(name, (u8_t *)&pub, sizeof(pub), &exist);
if (err) {
BT_WARN("%s, Cleared model publication", __func__);
model->pub->addr = BLE_MESH_ADDR_UNASSIGNED;
model->pub->key = 0U;
model->pub->cred = 0U;
model->pub->ttl = 0U;
model->pub->period = 0U;
model->pub->retransmit = 0U;
model->pub->count = 0U;
return 0;
}
if (exist == false) {
return 0;
}
model->pub->addr = pub.addr;
model->pub->key = pub.key;
model->pub->cred = pub.cred;
model->pub->ttl = pub.ttl;
model->pub->period = pub.period;
model->pub->retransmit = pub.retransmit;
model->pub->count = 0U;
BT_INFO("Restored model publication, pub_addr 0x%04x, app_idx 0x%03x",
pub.addr, pub.key);
return 0;
}
static int model_set(bool vnd, const char *name)
{
struct bt_mesh_model *model = NULL;
struct net_buf_simple *buf = NULL;
u8_t elem_idx = 0U, model_idx = 0U;
size_t length = 0U;
int err = 0;
int i;
BT_DBG("%s", __func__);
buf = bt_mesh_get_core_settings_item(name);
if (!buf) {
return 0;
}
length = buf->len;
for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
u16_t model_key = net_buf_simple_pull_le16(buf);
elem_idx = BLE_MESH_GET_ELEM_IDX(model_key);
model_idx = BLE_MESH_GET_MODEL_IDX(model_key);
model = bt_mesh_model_get(vnd, elem_idx, model_idx);
if (!model) {
BT_ERR("%s, Failed to get %s model, elem_idx %u model_idx %u",
__func__, vnd ? "vnd" : "sig", elem_idx, model_idx);
err = -ENOENT;
goto free;
}
err = model_set_bind(vnd, model, model_key);
if (err) {
BT_ERR("%s, model_set_bind fail", __func__);
goto free;
}
err = model_set_sub(vnd, model, model_key);
if (err) {
BT_ERR("%s, model_set_sub fail", __func__);
goto free;
}
err = model_set_pub(vnd, model, model_key);
if (err) {
BT_ERR("%s, model_set_pub fail", __func__);
goto free;
}
}
free:
bt_mesh_free_buf(buf);
return err;
}
static int sig_mod_set(const char *name)
{
return model_set(false, name);
}
static int vnd_mod_set(const char *name)
{
return model_set(true, name);
}
#if CONFIG_BLE_MESH_LABEL_COUNT > 0
static int va_set(const char *name)
{
struct net_buf_simple *buf = NULL;
struct va_val va = {0};
char get[16] = {'\0'};
struct label *lab = NULL;
size_t length = 0U;
bool exist = false;
int err = 0;
int i;
BT_DBG("%s", __func__);
buf = bt_mesh_get_core_settings_item(name);
if (!buf) {
return 0;
}
length = buf->len;
for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
u16_t index = net_buf_simple_pull_le16(buf);
sprintf(get, "mesh/va/%04x", index);
err = bt_mesh_load_core_settings(get, (u8_t *)&va, sizeof(va), &exist);
if (err) {
BT_ERR("%s, Failed to load virtual address %s", __func__, get);
goto free;
}
if (exist == false) {
continue;
}
if (va.ref == 0) {
BT_DBG("Ignore virtual address %s with ref = 0", get);
goto free;
}
lab = get_label(index);
if (lab == NULL) {
BT_WARN("%s, Out of labels buffers", __func__);
err = -ENOBUFS;
goto free;
}
memcpy(lab->uuid, va.uuid, 16);
lab->addr = va.addr;
lab->ref = va.ref;
BT_INFO("Restored virtual address 0x%04x ref 0x%04x", index, lab->ref);
}
free:
bt_mesh_free_buf(buf);
return err;
}
#endif
#if CONFIG_BLE_MESH_PROVISIONER
static int p_prov_set(const char *name)
{
struct prov_info val = {0};
bool exist = false;
int err = 0;
BT_DBG("%s", __func__);
err = bt_mesh_load_core_settings(name, (u8_t *)&val, sizeof(val), &exist);
if (err) {
BT_ERR("%s, Failed to load Provisioner prov info", __func__);
return 0;
}
if (exist == false) {
return 0;
}
bt_mesh_provisoner_restore_prov_info(val.primary_addr, val.alloc_addr);
BT_INFO("Restored primary_addr 0x%04x, alloc_addr 0x%04x",
val.primary_addr, val.alloc_addr);
return 0;
}
static int p_net_idx_set(const char *name)
{
u16_t net_idx = 0U;
bool exist = false;
int err = 0;
BT_DBG("%s", __func__);
err = bt_mesh_load_core_settings(name, (u8_t *)&net_idx, sizeof(net_idx), &exist);
if (err) {
BT_ERR("%s, Failed to load Provisioner keys", __func__);
return 0;
}
if (exist == false) {
return 0;
}
bt_mesh.p_net_idx_next = net_idx;
BT_INFO("Restored p_net_idx_next 0x%04x", bt_mesh.p_net_idx_next);
return 0;
}
static int p_app_idx_set(const char *name)
{
u16_t app_idx = 0U;
bool exist = false;
int err = 0;
BT_DBG("%s", __func__);
err = bt_mesh_load_core_settings(name, (u8_t *)&app_idx, sizeof(app_idx), &exist);
if (err) {
return 0;
}
if (exist == false) {
return 0;
}
bt_mesh.p_app_idx_next = app_idx;
BT_INFO("Restored p_app_idx_next 0x%04x", bt_mesh.p_app_idx_next);
return 0;
}
static struct bt_mesh_subnet *p_subnet_alloc(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
if (bt_mesh.p_sub[i] == NULL) {
bt_mesh.p_sub[i] = bt_mesh_calloc(sizeof(struct bt_mesh_subnet));
if (!bt_mesh.p_sub[i]) {
BT_ERR("%s, Failed to allocate memory", __func__);
return NULL;
}
return bt_mesh.p_sub[i];
}
}
return NULL;
}
static struct bt_mesh_app_key *p_appkey_alloc(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) {
if (bt_mesh.p_app_keys[i] == NULL) {
bt_mesh.p_app_keys[i] = bt_mesh_calloc(sizeof(struct bt_mesh_app_key));
if (!bt_mesh.p_app_keys[i]) {
BT_ERR("%s, Failed to allocate memory", __func__);
return NULL;
}
return bt_mesh.p_app_keys[i];
}
}
return NULL;
}
static int p_net_key_set(const char *name)
{
struct net_buf_simple *buf = NULL;
struct bt_mesh_subnet *sub = NULL;
struct net_key_val key = {0};
char get[16] = {'\0'};
size_t length = 0U;
bool exist = false;
int err = 0;
int i;
BT_DBG("%s", __func__);
buf = bt_mesh_get_core_settings_item(name);
if (!buf) {
return 0;
}
length = buf->len;
for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
u16_t net_idx = net_buf_simple_pull_le16(buf);
sprintf(get, "mesh/pnk/%04x", net_idx);
err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist);
if (err) {
BT_ERR("%s, Failed to load Provisioner NetKey %s", __func__, get);
goto free;
}
if (exist == false) {
continue;
}
sub = bt_mesh_provisioner_subnet_get(net_idx);
if (!sub) {
sub = p_subnet_alloc();
if (!sub) {
BT_ERR("%s, No space for a new subnet 0x%03x", __func__, net_idx);
err = -ENOMEM;
goto free;
}
}
sub->net_idx = net_idx;
sub->kr_flag = key.kr_flag;
sub->kr_phase = key.kr_phase;
memcpy(sub->keys[0].net, &key.val[0], 16);
memcpy(sub->keys[1].net, &key.val[1], 16);
BT_INFO("Restored NetIdx 0x%03x", sub->net_idx);
BT_INFO("Restored NetKey %s", bt_hex(sub->keys[0].net, 16));
}
free:
bt_mesh_free_buf(buf);
return err;
}
static int p_app_key_set(const char *name)
{
struct bt_mesh_app_key *app = NULL;
struct bt_mesh_subnet *sub = NULL;
struct net_buf_simple *buf = NULL;
struct app_key_val key = {0};
char get[16] = {'\0'};
size_t length = 0U;
bool exist = false;
int err = 0;
int i;
BT_DBG("%s", __func__);
buf = bt_mesh_get_core_settings_item(name);
if (!buf) {
return 0;
}
length = buf->len;
for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
u16_t app_idx = net_buf_simple_pull_le16(buf);
sprintf(get, "mesh/pak/%04x", app_idx);
err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist);
if (err) {
BT_ERR("%s, Failed to load Provisioner AppKey %s", __func__, get);