1 /*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "syscfg/syscfg.h"
8
9 #if MYNEWT_VAL(BLE_MESH_SETTINGS)
10
11 #define BT_DBG_ENABLED MYNEWT_VAL(BLE_MESH_DEBUG_SETTINGS)
12
13 #include "mesh/mesh.h"
14 #include "mesh/glue.h"
15 #include "net.h"
16 #include "crypto.h"
17 #include "transport.h"
18 #include "access.h"
19 #include "foundation.h"
20 #include "proxy.h"
21 #include "settings.h"
22
23 #include "config/config.h"
24
25 /* Tracking of what storage changes are pending for App and Net Keys. We
26 * track this in a separate array here instead of within the respective
27 * bt_mesh_app_key and bt_mesh_subnet structs themselves, since once a key
28 * gets deleted its struct becomes invalid and may be reused for other keys.
29 */
30 static struct key_update {
31 u16_t key_idx:12, /* AppKey or NetKey Index */
32 valid:1, /* 1 if this entry is valid, 0 if not */
33 app_key:1, /* 1 if this is an AppKey, 0 if a NetKey */
34 clear:1; /* 1 if key needs clearing, 0 if storing */
35 } key_updates[CONFIG_BT_MESH_APP_KEY_COUNT + CONFIG_BT_MESH_SUBNET_COUNT];
36
37 static struct k_delayed_work pending_store;
38
39 /* Mesh network storage information */
40 struct net_val {
41 u16_t primary_addr;
42 u8_t dev_key[16];
43 } __packed;
44
45 /* Sequence number storage */
46 struct seq_val {
47 u8_t val[3];
48 } __packed;
49
50 /* Heartbeat Publication storage */
51 struct hb_pub_val {
52 u16_t dst;
53 u8_t period;
54 u8_t ttl;
55 u16_t feat;
56 u16_t net_idx:12,
57 indefinite:1;
58 };
59
60 /* Miscelaneous configuration server model states */
61 struct cfg_val {
62 u8_t net_transmit;
63 u8_t relay;
64 u8_t relay_retransmit;
65 u8_t beacon;
66 u8_t gatt_proxy;
67 u8_t frnd;
68 u8_t default_ttl;
69 };
70
71 /* IV Index & IV Update storage */
72 struct iv_val {
73 u32_t iv_index;
74 u8_t iv_update:1,
75 iv_duration:7;
76 } __packed;
77
78 /* Replay Protection List storage */
79 struct rpl_val {
80 u32_t seq:24,
81 old_iv:1;
82 };
83
84 /* NetKey storage information */
85 struct net_key_val {
86 u8_t kr_flag:1,
87 kr_phase:7;
88 u8_t val[2][16];
89 } __packed;
90
91 /* AppKey storage information */
92 struct app_key_val {
93 u16_t net_idx;
94 bool updated;
95 u8_t val[2][16];
96 } __packed;
97
98 struct mod_pub_val {
99 u16_t addr;
100 u16_t key;
101 u8_t ttl;
102 u8_t retransmit;
103 u8_t period;
104 u8_t period_div:4,
105 cred:1;
106 };
107
108 /* We need this so we don't overwrite app-hardcoded values in case FCB
109 * contains a history of changes but then has a NULL at the end.
110 */
111 static struct {
112 bool valid;
113 struct cfg_val cfg;
114 } stored_cfg;
115
net_set(int argc,char ** argv,char * val)116 static int net_set(int argc, char **argv, char *val)
117 {
118 struct net_val net;
119 int len, err;
120
121 BT_DBG("val %s", val ? val : "(null)");
122
123 if (!val) {
124 bt_mesh_comp_unprovision();
125 memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key));
126 return 0;
127 }
128
129 len = sizeof(net);
130 err = settings_bytes_from_str(val, &net, &len);
131 if (err) {
132 BT_ERR("Failed to decode value %s (err %d)", val, err);
133 return err;
134 }
135
136 if (len != sizeof(net)) {
137 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(net));
138 return -EINVAL;
139 }
140
141 memcpy(bt_mesh.dev_key, net.dev_key, sizeof(bt_mesh.dev_key));
142 bt_mesh_comp_provision(net.primary_addr);
143
144 BT_DBG("Provisioned with primary address 0x%04x", net.primary_addr);
145 BT_DBG("Recovered DevKey %s", bt_hex(bt_mesh.dev_key, 16));
146
147 return 0;
148 }
149
iv_set(int argc,char ** argv,char * val)150 static int iv_set(int argc, char **argv, char *val)
151 {
152 struct iv_val iv;
153 int len, err;
154
155 BT_DBG("val %s", val ? val : "(null)");
156
157 if (!val) {
158 bt_mesh.iv_index = 0;
159 bt_mesh.iv_update = 0;
160 return 0;
161 }
162
163 len = sizeof(iv);
164 err = settings_bytes_from_str(val, &iv, &len);
165 if (err) {
166 BT_ERR("Failed to decode value %s (err %d)", val, err);
167 return err;
168 }
169
170 if (len != sizeof(iv)) {
171 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(iv));
172 return -EINVAL;
173 }
174
175 bt_mesh.iv_index = iv.iv_index;
176 bt_mesh.iv_update = iv.iv_update;
177 bt_mesh.ivu_duration = iv.iv_duration;
178
179 BT_DBG("IV Index 0x%04x (IV Update Flag %u) duration %u hours",
180 bt_mesh.iv_index, bt_mesh.iv_update, bt_mesh.ivu_duration);
181
182 return 0;
183 }
184
seq_set(int argc,char ** argv,char * val)185 static int seq_set(int argc, char **argv, char *val)
186 {
187 struct seq_val seq;
188 int len, err;
189
190 BT_DBG("val %s", val ? val : "(null)");
191
192 if (!val) {
193 bt_mesh.seq = 0;
194 return 0;
195 }
196
197 len = sizeof(seq);
198 err = settings_bytes_from_str(val, &seq, &len);
199 if (err) {
200 BT_ERR("Failed to decode value %s (err %d)", val, err);
201 return err;
202 }
203
204 if (len != sizeof(seq)) {
205 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(seq));
206 return -EINVAL;
207 }
208
209 bt_mesh.seq = ((u32_t)seq.val[0] | ((u32_t)seq.val[1] << 8) |
210 ((u32_t)seq.val[2] << 16));
211
212 if (CONFIG_BT_MESH_SEQ_STORE_RATE > 0) {
213 /* Make sure we have a large enough sequence number. We
214 * subtract 1 so that the first transmission causes a write
215 * to the settings storage.
216 */
217 bt_mesh.seq += (CONFIG_BT_MESH_SEQ_STORE_RATE -
218 (bt_mesh.seq % CONFIG_BT_MESH_SEQ_STORE_RATE));
219 bt_mesh.seq--;
220 }
221
222 BT_DBG("Sequence Number 0x%06x", bt_mesh.seq);
223
224 return 0;
225 }
226
rpl_find(u16_t src)227 static struct bt_mesh_rpl *rpl_find(u16_t src)
228 {
229 int i;
230
231 for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
232 if (bt_mesh.rpl[i].src == src) {
233 return &bt_mesh.rpl[i];
234 }
235 }
236
237 return NULL;
238 }
239
rpl_alloc(u16_t src)240 static struct bt_mesh_rpl *rpl_alloc(u16_t src)
241 {
242 int i;
243
244 for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
245 if (!bt_mesh.rpl[i].src) {
246 bt_mesh.rpl[i].src = src;
247 return &bt_mesh.rpl[i];
248 }
249 }
250
251 return NULL;
252 }
253
rpl_set(int argc,char ** argv,char * val)254 static int rpl_set(int argc, char **argv, char *val)
255 {
256 struct bt_mesh_rpl *entry;
257 struct rpl_val rpl;
258 int len, err;
259 u16_t src;
260
261 if (argc < 1) {
262 BT_ERR("Invalid argc (%d)", argc);
263 return -ENOENT;
264 }
265
266 BT_DBG("argv[0] %s val %s", argv[0], val ? val : "(null)");
267
268 src = strtol(argv[0], NULL, 16);
269 entry = rpl_find(src);
270
271 if (!val) {
272 if (entry) {
273 memset(entry, 0, sizeof(*entry));
274 } else {
275 BT_WARN("Unable to find RPL entry for 0x%04x", src);
276 }
277
278 return 0;
279 }
280
281 if (!entry) {
282 entry = rpl_alloc(src);
283 if (!entry) {
284 BT_ERR("Unable to allocate RPL entry for 0x%04x", src);
285 return -ENOMEM;
286 }
287 }
288
289 len = sizeof(rpl);
290 err = settings_bytes_from_str(val, &rpl, &len);
291 if (err) {
292 BT_ERR("Failed to decode value %s (err %d)", val, err);
293 return err;
294 }
295
296 if (len != sizeof(rpl)) {
297 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(rpl));
298 return -EINVAL;
299 }
300
301 entry->seq = rpl.seq;
302 entry->old_iv = rpl.old_iv;
303
304 BT_DBG("RPL entry for 0x%04x: Seq 0x%06x old_iv %u", entry->src,
305 entry->seq, entry->old_iv);
306
307 return 0;
308 }
309
net_key_set(int argc,char ** argv,char * val)310 static int net_key_set(int argc, char **argv, char *val)
311 {
312 struct bt_mesh_subnet *sub;
313 struct net_key_val key;
314 int len, i, err;
315 u16_t net_idx;
316
317 BT_DBG("argv[0] %s val %s", argv[0], val ? val : "(null)");
318
319 net_idx = strtol(argv[0], NULL, 16);
320 sub = bt_mesh_subnet_get(net_idx);
321
322 if (!val) {
323 if (!sub) {
324 BT_ERR("No subnet with NetKeyIndex 0x%03x", net_idx);
325 return -ENOENT;
326 }
327
328 BT_DBG("Deleting NetKeyIndex 0x%03x", net_idx);
329 bt_mesh_subnet_del(sub, false);
330 return 0;
331 }
332
333 len = sizeof(key);
334 err = settings_bytes_from_str(val, &key, &len);
335 if (err) {
336 BT_ERR("Failed to decode value %s (err %d)", val, err);
337 return err;
338 }
339
340 if (len != sizeof(key)) {
341 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(key));
342 return -EINVAL;
343 }
344
345 if (sub) {
346 BT_DBG("Updating existing NetKeyIndex 0x%03x", net_idx);
347
348 sub->kr_flag = key.kr_flag;
349 sub->kr_phase = key.kr_phase;
350 memcpy(sub->keys[0].net, &key.val[0], 16);
351 memcpy(sub->keys[1].net, &key.val[1], 16);
352
353 return 0;
354 }
355
356 for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
357 if (bt_mesh.sub[i].net_idx == BT_MESH_KEY_UNUSED) {
358 sub = &bt_mesh.sub[i];
359 break;
360 }
361 }
362
363 if (!sub) {
364 BT_ERR("No space to allocate a new subnet");
365 return -ENOMEM;
366 }
367
368 sub->net_idx = net_idx;
369 sub->kr_flag = key.kr_flag;
370 sub->kr_phase = key.kr_phase;
371 memcpy(sub->keys[0].net, &key.val[0], 16);
372 memcpy(sub->keys[1].net, &key.val[1], 16);
373
374 BT_DBG("NetKeyIndex 0x%03x recovered from storage", net_idx);
375
376 return 0;
377 }
378
app_key_set(int argc,char ** argv,char * val)379 static int app_key_set(int argc, char **argv, char *val)
380 {
381 struct bt_mesh_app_key *app;
382 struct bt_mesh_subnet *sub;
383 struct app_key_val key;
384 u16_t app_idx;
385 int len, err;
386
387 BT_DBG("argv[0] %s val %s", argv[0], val ? val : "(null)");
388
389 app_idx = strtol(argv[0], NULL, 16);
390
391 if (!val) {
392 BT_DBG("Deleting AppKeyIndex 0x%03x", app_idx);
393
394 app = bt_mesh_app_key_find(app_idx);
395 if (app) {
396 bt_mesh_app_key_del(app, false);
397 }
398
399 return 0;
400 }
401
402 len = sizeof(key);
403 err = settings_bytes_from_str(val, &key, &len);
404 if (err) {
405 BT_ERR("Failed to decode value %s (err %d)", val, err);
406 return err;
407 }
408
409 if (len != sizeof(key)) {
410 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(key));
411 return -EINVAL;
412 }
413
414 sub = bt_mesh_subnet_get(key.net_idx);
415 if (!sub) {
416 BT_ERR("Failed to find subnet 0x%03x", key.net_idx);
417 return -ENOENT;
418 }
419
420 app = bt_mesh_app_key_find(app_idx);
421 if (!app) {
422 app = bt_mesh_app_key_alloc(app_idx);
423 }
424
425 if (!app) {
426 BT_ERR("No space for a new app key");
427 return -ENOMEM;
428 }
429
430 app->net_idx = key.net_idx;
431 app->app_idx = app_idx;
432 app->updated = key.updated;
433 memcpy(app->keys[0].val, key.val[0], 16);
434 memcpy(app->keys[1].val, key.val[1], 16);
435
436 bt_mesh_app_id(app->keys[0].val, &app->keys[0].id);
437 bt_mesh_app_id(app->keys[1].val, &app->keys[1].id);
438
439 BT_DBG("AppKeyIndex 0x%03x recovered from storage", app_idx);
440
441 return 0;
442 }
443
hb_pub_set(int argc,char ** argv,char * val)444 static int hb_pub_set(int argc, char **argv, char *val)
445 {
446 struct bt_mesh_hb_pub *pub = bt_mesh_hb_pub_get();
447 struct hb_pub_val hb_val;
448 int len, err;
449
450 BT_DBG("val %s", val ? val : "(null)");
451
452 if (!pub) {
453 return -ENOENT;
454 }
455
456 if (!val) {
457 pub->dst = BT_MESH_ADDR_UNASSIGNED;
458 pub->count = 0;
459 pub->ttl = 0;
460 pub->period = 0;
461 pub->feat = 0;
462
463 BT_DBG("Cleared heartbeat publication");
464 return 0;
465 }
466
467 len = sizeof(hb_val);
468 err = settings_bytes_from_str(val, &hb_val, &len);
469 if (err) {
470 BT_ERR("Failed to decode value %s (err %d)", val, err);
471 return err;
472 }
473
474 if (len != sizeof(hb_val)) {
475 BT_ERR("Unexpected value length (%d != %zu)", len,
476 sizeof(hb_val));
477 return -EINVAL;
478 }
479
480 pub->dst = hb_val.dst;
481 pub->period = hb_val.period;
482 pub->ttl = hb_val.ttl;
483 pub->feat = hb_val.feat;
484 pub->net_idx = hb_val.net_idx;
485
486 if (hb_val.indefinite) {
487 pub->count = 0xffff;
488 } else {
489 pub->count = 0;
490 }
491
492 BT_DBG("Restored heartbeat publication");
493
494 return 0;
495 }
496
cfg_set(int argc,char ** argv,char * val)497 static int cfg_set(int argc, char **argv, char *val)
498 {
499 struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get();
500 int len, err;
501
502 BT_DBG("val %s", val ? val : "(null)");
503
504 if (!cfg) {
505 return -ENOENT;
506 }
507
508 if (!val) {
509 stored_cfg.valid = false;
510 BT_DBG("Cleared configuration state");
511 return 0;
512 }
513
514 len = sizeof(stored_cfg.cfg);
515 err = settings_bytes_from_str(val, &stored_cfg.cfg, &len);
516 if (err) {
517 BT_ERR("Failed to decode value %s (err %d)", val, err);
518 return err;
519 }
520
521 if (len != sizeof(stored_cfg.cfg)) {
522 BT_ERR("Unexpected value length (%d != %zu)", len,
523 sizeof(stored_cfg.cfg));
524 return -EINVAL;
525 }
526
527 stored_cfg.valid = true;
528 BT_DBG("Restored configuration state");
529
530 return 0;
531 }
532
mod_set_bind(struct bt_mesh_model * mod,char * val)533 static int mod_set_bind(struct bt_mesh_model *mod, char *val)
534 {
535 int len, err, i;
536
537 /* Start with empty array regardless of cleared or set value */
538 for (i = 0; i < ARRAY_SIZE(mod->keys); i++) {
539 mod->keys[i] = BT_MESH_KEY_UNUSED;
540 }
541
542 if (!val) {
543 BT_DBG("Cleared bindings for model");
544 return 0;
545 }
546
547 len = sizeof(mod->keys);
548 err = settings_bytes_from_str(val, mod->keys, &len);
549 if (err) {
550 BT_ERR("Failed to decode value %s (err %d)", val, err);
551 return -EINVAL;
552 }
553
554 BT_DBG("Decoded %u bound keys for model", len / sizeof(mod->keys[0]));
555 return 0;
556 }
557
mod_set_sub(struct bt_mesh_model * mod,char * val)558 static int mod_set_sub(struct bt_mesh_model *mod, char *val)
559 {
560 int len, err;
561
562 /* Start with empty array regardless of cleared or set value */
563 memset(mod->groups, 0, sizeof(mod->groups));
564
565 if (!val) {
566 BT_DBG("Cleared subscriptions for model");
567 return 0;
568 }
569
570 len = sizeof(mod->groups);
571 err = settings_bytes_from_str(val, mod->groups, &len);
572 if (err) {
573 BT_ERR("Failed to decode value %s (err %d)", val, err);
574 return -EINVAL;
575 }
576
577 BT_DBG("Decoded %u subscribed group addresses for model",
578 len / sizeof(mod->groups[0]));
579 return 0;
580 }
581
mod_set_pub(struct bt_mesh_model * mod,char * val)582 static int mod_set_pub(struct bt_mesh_model *mod, char *val)
583 {
584 struct mod_pub_val pub;
585 int len, err;
586
587 if (!mod->pub) {
588 BT_WARN("Model has no publication context!");
589 return -EINVAL;
590 }
591
592 if (!val) {
593 mod->pub->addr = BT_MESH_ADDR_UNASSIGNED;
594 mod->pub->key = 0;
595 mod->pub->cred = 0;
596 mod->pub->ttl = 0;
597 mod->pub->period = 0;
598 mod->pub->retransmit = 0;
599 mod->pub->count = 0;
600
601 BT_DBG("Cleared publication for model");
602 return 0;
603 }
604
605 len = sizeof(pub);
606 err = settings_bytes_from_str(val, &pub, &len);
607 if (err) {
608 BT_ERR("Failed to decode value %s (err %d)", val, err);
609 return -EINVAL;
610 }
611
612 if (len != sizeof(pub)) {
613 BT_ERR("Invalid length for model publication");
614 return -EINVAL;
615 }
616
617 mod->pub->addr = pub.addr;
618 mod->pub->key = pub.key;
619 mod->pub->cred = pub.cred;
620 mod->pub->ttl = pub.ttl;
621 mod->pub->period = pub.period;
622 mod->pub->retransmit = pub.retransmit;
623 mod->pub->count = 0;
624
625 BT_DBG("Restored model publication, dst 0x%04x app_idx 0x%03x",
626 pub.addr, pub.key);
627
628 return 0;
629 }
630
mod_set(bool vnd,int argc,char ** argv,char * val)631 static int mod_set(bool vnd, int argc, char **argv, char *val)
632 {
633 struct bt_mesh_model *mod;
634 u8_t elem_idx, mod_idx;
635 u16_t mod_key;
636
637 if (argc < 2) {
638 BT_ERR("Too small argc (%d)", argc);
639 return -ENOENT;
640 }
641
642 mod_key = strtol(argv[0], NULL, 16);
643 elem_idx = mod_key >> 8;
644 mod_idx = mod_key;
645
646 BT_DBG("Decoded mod_key 0x%04x as elem_idx %u mod_idx %u",
647 mod_key, elem_idx, mod_idx);
648
649 mod = bt_mesh_model_get(vnd, elem_idx, mod_idx);
650 if (!mod) {
651 BT_ERR("Failed to get model for elem_idx %u mod_idx %u",
652 elem_idx, mod_idx);
653 return -ENOENT;
654 }
655
656 if (!strcmp(argv[1], "bind")) {
657 return mod_set_bind(mod, val);
658 }
659
660 if (!strcmp(argv[1], "sub")) {
661 return mod_set_sub(mod, val);
662 }
663
664 if (!strcmp(argv[1], "pub")) {
665 return mod_set_pub(mod, val);
666 }
667
668 BT_WARN("Unknown module key %s", argv[1]);
669 return -ENOENT;
670 }
671
sig_mod_set(int argc,char ** argv,char * val)672 static int sig_mod_set(int argc, char **argv, char *val)
673 {
674 return mod_set(false, argc, argv, val);
675 }
676
vnd_mod_set(int argc,char ** argv,char * val)677 static int vnd_mod_set(int argc, char **argv, char *val)
678 {
679 return mod_set(true, argc, argv, val);
680 }
681
682 const struct mesh_setting {
683 const char *name;
684 int (*func)(int argc, char **argv, char *val);
685 } settings[] = {
686 { "Net", net_set },
687 { "IV", iv_set },
688 { "Seq", seq_set },
689 { "RPL", rpl_set },
690 { "NetKey", net_key_set },
691 { "AppKey", app_key_set },
692 { "HBPub", hb_pub_set },
693 { "Cfg", cfg_set },
694 { "s", sig_mod_set },
695 { "v", vnd_mod_set },
696 };
697
mesh_set(int argc,char ** argv,char * val)698 static int mesh_set(int argc, char **argv, char *val)
699 {
700 int i;
701
702 if (argc < 1) {
703 BT_ERR("Insufficient number of arguments");
704 return -EINVAL;
705 }
706
707 BT_DBG("argv[0] %s val %s", argv[0], val ? val : "(null)");
708
709 for (i = 0; i < ARRAY_SIZE(settings); i++) {
710 if (!strcmp(settings[i].name, argv[0])) {
711 argc--;
712 argv++;
713
714 return settings[i].func(argc, argv, val);
715 }
716 }
717
718 BT_WARN("No matching handler for key %s", argv[0]);
719
720 return -ENOENT;
721 }
722
subnet_init(struct bt_mesh_subnet * sub)723 static int subnet_init(struct bt_mesh_subnet *sub)
724 {
725 int err;
726
727 err = bt_mesh_net_keys_create(&sub->keys[0], sub->keys[0].net);
728 if (err) {
729 BT_ERR("Unable to generate keys for subnet");
730 return -EIO;
731 }
732
733 if (sub->kr_phase != BT_MESH_KR_NORMAL) {
734 err = bt_mesh_net_keys_create(&sub->keys[1], sub->keys[1].net);
735 if (err) {
736 BT_ERR("Unable to generate keys for subnet");
737 memset(&sub->keys[0], 0, sizeof(sub->keys[0]));
738 return -EIO;
739 }
740 }
741
742 if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
743 sub->node_id = BT_MESH_NODE_IDENTITY_STOPPED;
744 } else {
745 sub->node_id = BT_MESH_NODE_IDENTITY_NOT_SUPPORTED;
746 }
747
748 /* Make sure we have valid beacon data to be sent */
749 bt_mesh_net_beacon_update(sub);
750
751 return 0;
752 }
753
commit_mod(struct bt_mesh_model * mod,struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)754 static void commit_mod(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
755 bool vnd, bool primary, void *user_data)
756 {
757 if (mod->pub && mod->pub->update &&
758 mod->pub->addr != BT_MESH_ADDR_UNASSIGNED) {
759 s32_t ms = bt_mesh_model_pub_period_get(mod);
760 if (ms) {
761 BT_DBG("Starting publish timer (period %u ms)", ms);
762 k_delayed_work_submit(&mod->pub->timer, ms);
763 }
764 }
765 }
766
mesh_commit(void)767 static int mesh_commit(void)
768 {
769 struct bt_mesh_hb_pub *hb_pub;
770 struct bt_mesh_cfg_srv *cfg;
771 int i;
772
773 BT_DBG("sub[0].net_idx 0x%03x", bt_mesh.sub[0].net_idx);
774
775 if (bt_mesh.sub[0].net_idx == BT_MESH_KEY_UNUSED) {
776 /* Nothing to do since we're not yet provisioned */
777 return 0;
778 }
779
780 if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) {
781 bt_mesh_proxy_prov_disable();
782 }
783
784 for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
785 struct bt_mesh_subnet *sub = &bt_mesh.sub[i];
786 int err;
787
788 if (sub->net_idx == BT_MESH_KEY_UNUSED) {
789 continue;
790 }
791
792 err = subnet_init(sub);
793 if (err) {
794 BT_ERR("Failed to init subnet 0x%03x", sub->net_idx);
795 }
796 }
797
798 if (bt_mesh.ivu_duration < BT_MESH_IVU_MIN_HOURS) {
799 k_delayed_work_submit(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);
800 }
801
802 bt_mesh_model_foreach(commit_mod, NULL);
803
804 hb_pub = bt_mesh_hb_pub_get();
805 if (hb_pub && hb_pub->dst != BT_MESH_ADDR_UNASSIGNED &&
806 hb_pub->count && hb_pub->period) {
807 BT_DBG("Starting heartbeat publication");
808 k_work_submit(&hb_pub->timer.work);
809 }
810
811 cfg = bt_mesh_cfg_get();
812 if (cfg && stored_cfg.valid) {
813 cfg->net_transmit = stored_cfg.cfg.net_transmit;
814 cfg->relay = stored_cfg.cfg.relay;
815 cfg->relay_retransmit = stored_cfg.cfg.relay_retransmit;
816 cfg->beacon = stored_cfg.cfg.beacon;
817 cfg->gatt_proxy = stored_cfg.cfg.gatt_proxy;
818 cfg->frnd = stored_cfg.cfg.frnd;
819 cfg->default_ttl = stored_cfg.cfg.default_ttl;
820 }
821
822 bt_mesh.valid = 1;
823
824 bt_mesh_net_start();
825
826 return 0;
827 }
828
schedule_store(int flag)829 static void schedule_store(int flag)
830 {
831 s32_t timeout;
832
833 atomic_set_bit(bt_mesh.flags, flag);
834
835 if (atomic_test_bit(bt_mesh.flags, BT_MESH_NET_PENDING) ||
836 atomic_test_bit(bt_mesh.flags, BT_MESH_IV_PENDING) ||
837 atomic_test_bit(bt_mesh.flags, BT_MESH_SEQ_PENDING)) {
838 timeout = K_NO_WAIT;
839 } else if (atomic_test_bit(bt_mesh.flags, BT_MESH_RPL_PENDING) &&
840 (CONFIG_BT_MESH_RPL_STORE_TIMEOUT <
841 CONFIG_BT_MESH_STORE_TIMEOUT)) {
842 timeout = K_SECONDS(CONFIG_BT_MESH_RPL_STORE_TIMEOUT);
843 } else {
844 timeout = K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT);
845 }
846
847 BT_DBG("Waiting %d seconds", timeout / MSEC_PER_SEC);
848
849 k_delayed_work_submit(&pending_store, timeout);
850 }
851
clear_iv(void)852 static void clear_iv(void)
853 {
854 BT_DBG("Clearing IV");
855 settings_save_one("bt_mesh/IV", NULL);
856 }
857
clear_net(void)858 static void clear_net(void)
859 {
860 BT_DBG("Clearing Network");
861 settings_save_one("bt_mesh/Net", NULL);
862 }
863
store_pending_net(void)864 static void store_pending_net(void)
865 {
866 char buf[BT_SETTINGS_SIZE(sizeof(struct net_val))];
867 struct net_val net;
868 char *str;
869
870 BT_DBG("addr 0x%04x DevKey %s", bt_mesh_primary_addr(),
871 bt_hex(bt_mesh.dev_key, 16));
872
873 net.primary_addr = bt_mesh_primary_addr();
874 memcpy(net.dev_key, bt_mesh.dev_key, 16);
875
876 str = settings_str_from_bytes(&net, sizeof(net), buf, sizeof(buf));
877 if (!str) {
878 BT_ERR("Unable to encode Network as value");
879 return;
880 }
881
882 BT_DBG("Saving Network as value %s", str);
883 settings_save_one("bt_mesh/Net", str);
884 }
885
bt_mesh_store_net(void)886 void bt_mesh_store_net(void)
887 {
888 schedule_store(BT_MESH_NET_PENDING);
889 }
890
store_pending_iv(void)891 static void store_pending_iv(void)
892 {
893 char buf[BT_SETTINGS_SIZE(sizeof(struct iv_val))];
894 struct iv_val iv;
895 char *str;
896
897 iv.iv_index = bt_mesh.iv_index;
898 iv.iv_update = bt_mesh.iv_update;
899 iv.iv_duration = bt_mesh.ivu_duration;
900
901 str = settings_str_from_bytes(&iv, sizeof(iv), buf, sizeof(buf));
902 if (!str) {
903 BT_ERR("Unable to encode IV as value");
904 return;
905 }
906
907 BT_DBG("Saving IV as value %s", str);
908 settings_save_one("bt_mesh/IV", str);
909 }
910
bt_mesh_store_iv(bool only_duration)911 void bt_mesh_store_iv(bool only_duration)
912 {
913 schedule_store(BT_MESH_IV_PENDING);
914
915 if (!only_duration) {
916 /* Always update Seq whenever IV changes */
917 schedule_store(BT_MESH_SEQ_PENDING);
918 }
919 }
920
store_pending_seq(void)921 static void store_pending_seq(void)
922 {
923 char buf[BT_SETTINGS_SIZE(sizeof(struct seq_val))];
924 struct seq_val seq;
925 char *str;
926
927 seq.val[0] = bt_mesh.seq;
928 seq.val[1] = bt_mesh.seq >> 8;
929 seq.val[2] = bt_mesh.seq >> 16;
930
931 str = settings_str_from_bytes(&seq, sizeof(seq), buf, sizeof(buf));
932 if (!str) {
933 BT_ERR("Unable to encode Seq as value");
934 return;
935 }
936
937 BT_DBG("Saving Seq as value %s", str);
938 settings_save_one("bt_mesh/Seq", str);
939 }
940
bt_mesh_store_seq(void)941 void bt_mesh_store_seq(void)
942 {
943 if (CONFIG_BT_MESH_SEQ_STORE_RATE &&
944 (bt_mesh.seq % CONFIG_BT_MESH_SEQ_STORE_RATE)) {
945 return;
946 }
947
948 schedule_store(BT_MESH_SEQ_PENDING);
949 }
950
store_rpl(struct bt_mesh_rpl * entry)951 static void store_rpl(struct bt_mesh_rpl *entry)
952 {
953 char buf[BT_SETTINGS_SIZE(sizeof(struct rpl_val))];
954 struct rpl_val rpl;
955 char path[18];
956 char *str;
957
958 BT_DBG("src 0x%04x seq 0x%06x old_iv %u", entry->src, entry->seq,
959 entry->old_iv);
960
961 rpl.seq = entry->seq;
962 rpl.old_iv = entry->old_iv;
963
964 str = settings_str_from_bytes(&rpl, sizeof(rpl), buf, sizeof(buf));
965 if (!str) {
966 BT_ERR("Unable to encode RPL as value");
967 return;
968 }
969
970 snprintk(path, sizeof(path), "bt_mesh/RPL/%x", entry->src);
971
972 BT_DBG("Saving RPL %s as value %s", path, str);
973 settings_save_one(path, str);
974 }
975
clear_rpl(void)976 static void clear_rpl(void)
977 {
978 int i;
979
980 BT_DBG("");
981
982 for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
983 struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i];
984 char path[18];
985
986 if (!rpl->src) {
987 continue;
988 }
989
990 snprintk(path, sizeof(path), "bt_mesh/RPL/%x", rpl->src);
991 settings_save_one(path, NULL);
992
993 memset(rpl, 0, sizeof(*rpl));
994 }
995 }
996
store_pending_rpl(void)997 static void store_pending_rpl(void)
998 {
999 int i;
1000
1001 BT_DBG("");
1002
1003 for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
1004 struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i];
1005
1006 if (rpl->store) {
1007 rpl->store = false;
1008 store_rpl(rpl);
1009 }
1010 }
1011 }
1012
store_pending_hb_pub(void)1013 static void store_pending_hb_pub(void)
1014 {
1015 char buf[BT_SETTINGS_SIZE(sizeof(struct hb_pub_val))];
1016 struct bt_mesh_hb_pub *pub = bt_mesh_hb_pub_get();
1017 struct hb_pub_val val;
1018 char *str;
1019
1020 if (!pub) {
1021 return;
1022 }
1023
1024 if (pub->dst == BT_MESH_ADDR_UNASSIGNED) {
1025 str = NULL;
1026 } else {
1027 val.indefinite = (pub->count = 0xffff);
1028 val.dst = pub->dst;
1029 val.period = pub->period;
1030 val.ttl = pub->ttl;
1031 val.feat = pub->feat;
1032 val.net_idx = pub->net_idx;
1033
1034 str = settings_str_from_bytes(&val, sizeof(val),
1035 buf, sizeof(buf));
1036 if (!str) {
1037 BT_ERR("Unable to encode hb pub as value");
1038 return;
1039 }
1040 }
1041
1042 BT_DBG("Saving Heartbeat Publication as value %s",
1043 str ? str : "(null)");
1044 settings_save_one("bt_mesh/HBPub", str);
1045 }
1046
store_pending_cfg(void)1047 static void store_pending_cfg(void)
1048 {
1049 char buf[BT_SETTINGS_SIZE(sizeof(struct cfg_val))];
1050 struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get();
1051 struct cfg_val val;
1052 char *str;
1053
1054 if (!cfg) {
1055 return;
1056 }
1057
1058 val.net_transmit = cfg->net_transmit;
1059 val.relay = cfg->relay;
1060 val.relay_retransmit = cfg->relay_retransmit;
1061 val.beacon = cfg->beacon;
1062 val.gatt_proxy = cfg->gatt_proxy;
1063 val.frnd = cfg->frnd;
1064 val.default_ttl = cfg->default_ttl;
1065
1066 str = settings_str_from_bytes(&val, sizeof(val), buf, sizeof(buf));
1067 if (!str) {
1068 BT_ERR("Unable to encode configuration as value");
1069 return;
1070 }
1071
1072 BT_DBG("Saving configuration as value %s", str);
1073 settings_save_one("bt_mesh/Cfg", str);
1074 }
1075
clear_cfg(void)1076 static void clear_cfg(void)
1077 {
1078 BT_DBG("Clearing configuration");
1079 settings_save_one("bt_mesh/Cfg", NULL);
1080 }
1081
clear_app_key(u16_t app_idx)1082 static void clear_app_key(u16_t app_idx)
1083 {
1084 char path[20];
1085
1086 BT_DBG("AppKeyIndex 0x%03x", app_idx);
1087
1088 snprintk(path, sizeof(path), "bt_mesh/AppKey/%x", app_idx);
1089 settings_save_one(path, NULL);
1090 }
1091
clear_net_key(u16_t net_idx)1092 static void clear_net_key(u16_t net_idx)
1093 {
1094 char path[20];
1095
1096 BT_DBG("NetKeyIndex 0x%03x", net_idx);
1097
1098 snprintk(path, sizeof(path), "bt_mesh/NetKey/%x", net_idx);
1099 settings_save_one(path, NULL);
1100 }
1101
store_net_key(struct bt_mesh_subnet * sub)1102 static void store_net_key(struct bt_mesh_subnet *sub)
1103 {
1104 char buf[BT_SETTINGS_SIZE(sizeof(struct net_key_val))];
1105 struct net_key_val key;
1106 char path[20];
1107 char *str;
1108
1109 BT_DBG("NetKeyIndex 0x%03x NetKey %s", sub->net_idx,
1110 bt_hex(sub->keys[0].net, 16));
1111
1112 memcpy(&key.val[0], sub->keys[0].net, 16);
1113 memcpy(&key.val[1], sub->keys[1].net, 16);
1114 key.kr_flag = sub->kr_flag;
1115 key.kr_phase = sub->kr_phase;
1116
1117 str = settings_str_from_bytes(&key, sizeof(key), buf, sizeof(buf));
1118 if (!str) {
1119 BT_ERR("Unable to encode NetKey as value");
1120 return;
1121 }
1122
1123 snprintk(path, sizeof(path), "bt_mesh/NetKey/%x", sub->net_idx);
1124
1125 BT_DBG("Saving NetKey %s as value %s", path, str);
1126 settings_save_one(path, str);
1127 }
1128
store_app_key(struct bt_mesh_app_key * app)1129 static void store_app_key(struct bt_mesh_app_key *app)
1130 {
1131 char buf[BT_SETTINGS_SIZE(sizeof(struct app_key_val))];
1132 struct app_key_val key;
1133 char path[20];
1134 char *str;
1135
1136 key.net_idx = app->net_idx;
1137 key.updated = app->updated;
1138 memcpy(key.val[0], app->keys[0].val, 16);
1139 memcpy(key.val[1], app->keys[1].val, 16);
1140
1141 str = settings_str_from_bytes(&key, sizeof(key), buf, sizeof(buf));
1142 if (!str) {
1143 BT_ERR("Unable to encode AppKey as value");
1144 return;
1145 }
1146
1147 snprintk(path, sizeof(path), "bt_mesh/AppKey/%x", app->app_idx);
1148
1149 BT_DBG("Saving AppKey %s as value %s", path, str);
1150 settings_save_one(path, str);
1151 }
1152
store_pending_keys(void)1153 static void store_pending_keys(void)
1154 {
1155 int i;
1156
1157 for (i = 0; i < ARRAY_SIZE(key_updates); i++) {
1158 struct key_update *update = &key_updates[i];
1159
1160 if (!update->valid) {
1161 continue;
1162 }
1163
1164 if (update->clear) {
1165 if (update->app_key) {
1166 clear_app_key(update->key_idx);
1167 } else {
1168 clear_net_key(update->key_idx);
1169 }
1170 } else {
1171 if (update->app_key) {
1172 struct bt_mesh_app_key *key;
1173
1174 key = bt_mesh_app_key_find(update->key_idx);
1175 if (key) {
1176 store_app_key(key);
1177 } else {
1178 BT_WARN("AppKeyIndex 0x%03x not found",
1179 update->key_idx);
1180 }
1181
1182 } else {
1183 struct bt_mesh_subnet *sub;
1184
1185 sub = bt_mesh_subnet_get(update->key_idx);
1186 if (sub) {
1187 store_net_key(sub);
1188 } else {
1189 BT_WARN("NetKeyIndex 0x%03x not found",
1190 update->key_idx);
1191 }
1192 }
1193 }
1194
1195 update->valid = 0;
1196 }
1197 }
1198
encode_mod_path(struct bt_mesh_model * mod,bool vnd,const char * key,char * path,size_t path_len)1199 static void encode_mod_path(struct bt_mesh_model *mod, bool vnd,
1200 const char *key, char *path, size_t path_len)
1201 {
1202 u16_t mod_key = (((u16_t)mod->elem_idx << 8) | mod->mod_idx);
1203
1204 if (vnd) {
1205 snprintk(path, path_len, "bt_mesh/v/%x/%s", mod_key, key);
1206 } else {
1207 snprintk(path, path_len, "bt_mesh/s/%x/%s", mod_key, key);
1208 }
1209 }
1210
store_pending_mod_bind(struct bt_mesh_model * mod,bool vnd)1211 static void store_pending_mod_bind(struct bt_mesh_model *mod, bool vnd)
1212 {
1213 u16_t keys[CONFIG_BT_MESH_MODEL_KEY_COUNT];
1214 char buf[BT_SETTINGS_SIZE(sizeof(keys))];
1215 char path[20];
1216 int i, count;
1217 char *val;
1218
1219 for (i = 0, count = 0; i < ARRAY_SIZE(mod->keys); i++) {
1220 if (mod->keys[i] != BT_MESH_KEY_UNUSED) {
1221 keys[count++] = mod->keys[i];
1222 }
1223 }
1224
1225 if (count) {
1226 val = settings_str_from_bytes(keys, count * sizeof(keys[0]),
1227 buf, sizeof(buf));
1228 if (!val) {
1229 BT_ERR("Unable to encode model bindings as value");
1230 return;
1231 }
1232 } else {
1233 val = NULL;
1234 }
1235
1236 encode_mod_path(mod, vnd, "bind", path, sizeof(path));
1237
1238 BT_DBG("Saving %s as %s", path, val ? val : "(null)");
1239 settings_save_one(path, val);
1240 }
1241
store_pending_mod_sub(struct bt_mesh_model * mod,bool vnd)1242 static void store_pending_mod_sub(struct bt_mesh_model *mod, bool vnd)
1243 {
1244 u16_t groups[CONFIG_BT_MESH_MODEL_GROUP_COUNT];
1245 char buf[BT_SETTINGS_SIZE(sizeof(groups))];
1246 char path[20];
1247 int i, count;
1248 char *val;
1249
1250 for (i = 0, count = 0; i < ARRAY_SIZE(mod->groups); i++) {
1251 if (mod->groups[i] != BT_MESH_ADDR_UNASSIGNED) {
1252 groups[count++] = mod->groups[i];
1253 }
1254 }
1255
1256 if (count) {
1257 val = settings_str_from_bytes(groups, count * sizeof(groups[0]),
1258 buf, sizeof(buf));
1259 if (!val) {
1260 BT_ERR("Unable to encode model subscription as value");
1261 return;
1262 }
1263 } else {
1264 val = NULL;
1265 }
1266
1267 encode_mod_path(mod, vnd, "sub", path, sizeof(path));
1268
1269 BT_DBG("Saving %s as %s", path, val ? val : "(null)");
1270 settings_save_one(path, val);
1271 }
1272
store_pending_mod_pub(struct bt_mesh_model * mod,bool vnd)1273 static void store_pending_mod_pub(struct bt_mesh_model *mod, bool vnd)
1274 {
1275 char buf[BT_SETTINGS_SIZE(sizeof(struct mod_pub_val))];
1276 struct mod_pub_val pub;
1277 char path[20];
1278 char *val;
1279
1280 if (!mod->pub || mod->pub->addr == BT_MESH_ADDR_UNASSIGNED) {
1281 val = NULL;
1282 } else {
1283 pub.addr = mod->pub->addr;
1284 pub.key = mod->pub->key;
1285 pub.ttl = mod->pub->ttl;
1286 pub.retransmit = mod->pub->retransmit;
1287 pub.period = mod->pub->period;
1288 pub.period_div = mod->pub->period_div;
1289 pub.cred = mod->pub->cred;
1290
1291 val = settings_str_from_bytes(&pub, sizeof(pub),
1292 buf, sizeof(buf));
1293 if (!val) {
1294 BT_ERR("Unable to encode model publication as value");
1295 return;
1296 }
1297 }
1298
1299 encode_mod_path(mod, vnd, "pub", path, sizeof(path));
1300
1301 BT_DBG("Saving %s as %s", path, val ? val : "(null)");
1302 settings_save_one(path, val);
1303 }
1304
store_pending_mod(struct bt_mesh_model * mod,struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)1305 static void store_pending_mod(struct bt_mesh_model *mod,
1306 struct bt_mesh_elem *elem, bool vnd,
1307 bool primary, void *user_data)
1308 {
1309 if (!mod->flags) {
1310 return;
1311 }
1312
1313 if (mod->flags & BT_MESH_MOD_BIND_PENDING) {
1314 mod->flags &= ~BT_MESH_MOD_BIND_PENDING;
1315 store_pending_mod_bind(mod, vnd);
1316 }
1317
1318 if (mod->flags & BT_MESH_MOD_SUB_PENDING) {
1319 mod->flags &= ~BT_MESH_MOD_SUB_PENDING;
1320 store_pending_mod_sub(mod, vnd);
1321 }
1322
1323 if (mod->flags & BT_MESH_MOD_PUB_PENDING) {
1324 mod->flags &= ~BT_MESH_MOD_PUB_PENDING;
1325 store_pending_mod_pub(mod, vnd);
1326 }
1327 }
1328
store_pending(struct ble_npl_event * work)1329 static void store_pending(struct ble_npl_event *work)
1330 {
1331 BT_DBG("");
1332
1333 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_RPL_PENDING)) {
1334 if (bt_mesh.valid) {
1335 store_pending_rpl();
1336 } else {
1337 clear_rpl();
1338 }
1339 }
1340
1341 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_KEYS_PENDING)) {
1342 store_pending_keys();
1343 }
1344
1345 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_NET_PENDING)) {
1346 if (bt_mesh.valid) {
1347 store_pending_net();
1348 } else {
1349 clear_net();
1350 }
1351 }
1352
1353 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_IV_PENDING)) {
1354 if (bt_mesh.valid) {
1355 store_pending_iv();
1356 } else {
1357 clear_iv();
1358 }
1359 }
1360
1361 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_SEQ_PENDING)) {
1362 store_pending_seq();
1363 }
1364
1365 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_HB_PUB_PENDING)) {
1366 store_pending_hb_pub();
1367 }
1368
1369 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_CFG_PENDING)) {
1370 if (bt_mesh.valid) {
1371 store_pending_cfg();
1372 } else {
1373 clear_cfg();
1374 }
1375 }
1376
1377 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_MOD_PENDING)) {
1378 bt_mesh_model_foreach(store_pending_mod, NULL);
1379 }
1380 }
1381
bt_mesh_store_rpl(struct bt_mesh_rpl * entry)1382 void bt_mesh_store_rpl(struct bt_mesh_rpl *entry)
1383 {
1384 entry->store = true;
1385 schedule_store(BT_MESH_RPL_PENDING);
1386 }
1387
key_update_find(bool app_key,u16_t key_idx,struct key_update ** free_slot)1388 static struct key_update *key_update_find(bool app_key, u16_t key_idx,
1389 struct key_update **free_slot)
1390 {
1391 struct key_update *match;
1392 int i;
1393
1394 match = NULL;
1395 *free_slot = NULL;
1396
1397 for (i = 0; i < ARRAY_SIZE(key_updates); i++) {
1398 struct key_update *update = &key_updates[i];
1399
1400 if (!update->valid) {
1401 *free_slot = update;
1402 continue;
1403 }
1404
1405 if (update->app_key != app_key) {
1406 continue;
1407 }
1408
1409 if (update->key_idx == key_idx) {
1410 match = update;
1411 }
1412 }
1413
1414 return match;
1415 }
1416
bt_mesh_store_subnet(struct bt_mesh_subnet * sub)1417 void bt_mesh_store_subnet(struct bt_mesh_subnet *sub)
1418 {
1419 struct key_update *update, *free_slot;
1420
1421 BT_DBG("NetKeyIndex 0x%03x", sub->net_idx);
1422
1423 update = key_update_find(false, sub->net_idx, &free_slot);
1424 if (update) {
1425 update->clear = 0;
1426 schedule_store(BT_MESH_KEYS_PENDING);
1427 return;
1428 }
1429
1430 if (!free_slot) {
1431 store_net_key(sub);
1432 return;
1433 }
1434
1435 free_slot->valid = 1;
1436 free_slot->key_idx = sub->net_idx;
1437 free_slot->app_key = 0;
1438 free_slot->clear = 0;
1439
1440 schedule_store(BT_MESH_KEYS_PENDING);
1441 }
1442
bt_mesh_store_app_key(struct bt_mesh_app_key * key)1443 void bt_mesh_store_app_key(struct bt_mesh_app_key *key)
1444 {
1445 struct key_update *update, *free_slot;
1446
1447 BT_DBG("AppKeyIndex 0x%03x", key->app_idx);
1448
1449 update = key_update_find(true, key->app_idx, &free_slot);
1450 if (update) {
1451 update->clear = 0;
1452 schedule_store(BT_MESH_KEYS_PENDING);
1453 return;
1454 }
1455
1456 if (!free_slot) {
1457 store_app_key(key);
1458 return;
1459 }
1460
1461 free_slot->valid = 1;
1462 free_slot->key_idx = key->app_idx;
1463 free_slot->app_key = 1;
1464 free_slot->clear = 0;
1465
1466 schedule_store(BT_MESH_KEYS_PENDING);
1467 }
1468
bt_mesh_store_hb_pub(void)1469 void bt_mesh_store_hb_pub(void)
1470 {
1471 schedule_store(BT_MESH_HB_PUB_PENDING);
1472 }
1473
bt_mesh_store_cfg(void)1474 void bt_mesh_store_cfg(void)
1475 {
1476 schedule_store(BT_MESH_CFG_PENDING);
1477 }
1478
bt_mesh_clear_net(void)1479 void bt_mesh_clear_net(void)
1480 {
1481 schedule_store(BT_MESH_NET_PENDING);
1482 schedule_store(BT_MESH_IV_PENDING);
1483 schedule_store(BT_MESH_CFG_PENDING);
1484 }
1485
bt_mesh_clear_subnet(struct bt_mesh_subnet * sub)1486 void bt_mesh_clear_subnet(struct bt_mesh_subnet *sub)
1487 {
1488 struct key_update *update, *free_slot;
1489
1490 BT_DBG("NetKeyIndex 0x%03x", sub->net_idx);
1491
1492 update = key_update_find(false, sub->net_idx, &free_slot);
1493 if (update) {
1494 update->clear = 1;
1495 schedule_store(BT_MESH_KEYS_PENDING);
1496 return;
1497 }
1498
1499 if (!free_slot) {
1500 clear_net_key(sub->net_idx);
1501 return;
1502 }
1503
1504 free_slot->valid = 1;
1505 free_slot->key_idx = sub->net_idx;
1506 free_slot->app_key = 0;
1507 free_slot->clear = 1;
1508
1509 schedule_store(BT_MESH_KEYS_PENDING);
1510 }
1511
bt_mesh_clear_app_key(struct bt_mesh_app_key * key)1512 void bt_mesh_clear_app_key(struct bt_mesh_app_key *key)
1513 {
1514 struct key_update *update, *free_slot;
1515
1516 BT_DBG("AppKeyIndex 0x%03x", key->app_idx);
1517
1518 update = key_update_find(true, key->app_idx, &free_slot);
1519 if (update) {
1520 update->clear = 1;
1521 schedule_store(BT_MESH_KEYS_PENDING);
1522 return;
1523 }
1524
1525 if (!free_slot) {
1526 clear_app_key(key->app_idx);
1527 return;
1528 }
1529
1530 free_slot->valid = 1;
1531 free_slot->key_idx = key->app_idx;
1532 free_slot->app_key = 1;
1533 free_slot->clear = 1;
1534
1535 schedule_store(BT_MESH_KEYS_PENDING);
1536 }
1537
bt_mesh_clear_rpl(void)1538 void bt_mesh_clear_rpl(void)
1539 {
1540 schedule_store(BT_MESH_RPL_PENDING);
1541 }
1542
bt_mesh_store_mod_bind(struct bt_mesh_model * mod)1543 void bt_mesh_store_mod_bind(struct bt_mesh_model *mod)
1544 {
1545 mod->flags |= BT_MESH_MOD_BIND_PENDING;
1546 schedule_store(BT_MESH_MOD_PENDING);
1547 }
1548
bt_mesh_store_mod_sub(struct bt_mesh_model * mod)1549 void bt_mesh_store_mod_sub(struct bt_mesh_model *mod)
1550 {
1551 mod->flags |= BT_MESH_MOD_SUB_PENDING;
1552 schedule_store(BT_MESH_MOD_PENDING);
1553 }
1554
bt_mesh_store_mod_pub(struct bt_mesh_model * mod)1555 void bt_mesh_store_mod_pub(struct bt_mesh_model *mod)
1556 {
1557 mod->flags |= BT_MESH_MOD_PUB_PENDING;
1558 schedule_store(BT_MESH_MOD_PENDING);
1559 }
1560
1561 static struct conf_handler bt_mesh_settings_conf_handler = {
1562 .ch_name = "bt_mesh",
1563 .ch_get = NULL,
1564 .ch_set = mesh_set,
1565 .ch_commit = mesh_commit,
1566 .ch_export = NULL,
1567 };
1568
bt_mesh_settings_init(void)1569 void bt_mesh_settings_init(void)
1570 {
1571 int rc;
1572
1573 rc = conf_register(&bt_mesh_settings_conf_handler);
1574
1575 SYSINIT_PANIC_ASSERT_MSG(rc == 0,
1576 "Failed to register bt_mesh_settings conf");
1577
1578 k_delayed_work_init(&pending_store, store_pending);
1579 }
1580
1581 #endif /* MYNEWT_VAL(BLE_MESH_SETTINGS) */
1582