1 /*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "syscfg/syscfg.h"
8 #define MESH_LOG_MODULE BLE_MESH_SETTINGS_LOG
9
10 #if MYNEWT_VAL(BLE_MESH_SETTINGS)
11
12 #include "mesh/mesh.h"
13 #include "mesh/glue.h"
14 #include "net.h"
15 #include "crypto.h"
16 #include "transport.h"
17 #include "access.h"
18 #include "foundation.h"
19 #include "proxy.h"
20 #include "settings.h"
21 #include "nodes.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 /* Virtual Address information */
109 struct va_val {
110 u16_t ref;
111 u16_t addr;
112 u8_t uuid[16];
113 } __packed;
114
115 /* Node storage information */
116 struct node_val {
117 u16_t net_idx;
118 u8_t dev_key[16];
119 u8_t num_elem;
120 } __packed;
121
122 struct node_update {
123 u16_t addr;
124 bool clear;
125 };
126
127 #if MYNEWT_VAL(BLE_MESH_PROVISIONER)
128 static struct node_update node_updates[CONFIG_BT_MESH_NODE_COUNT];
129 #else
130 static struct node_update node_updates[0];
131 #endif
132
133 /* We need this so we don't overwrite app-hardcoded values in case FCB
134 * contains a history of changes but then has a NULL at the end.
135 */
136 static struct {
137 bool valid;
138 struct cfg_val cfg;
139 } stored_cfg;
140
net_set(int argc,char ** argv,char * val)141 static int net_set(int argc, char **argv, char *val)
142 {
143 struct net_val net;
144 int len, err;
145 BT_DBG("val %s", val ? val : "(null)");
146
147 if (!val) {
148 bt_mesh_comp_unprovision();
149 memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key));
150 return 0;
151 }
152
153 len = sizeof(net);
154 err = settings_bytes_from_str(val, &net, &len);
155 if (err) {
156 BT_ERR("Failed to decode value %s (err %d)", val, err);
157 return err;
158 }
159
160 if (len != sizeof(net)) {
161 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(net));
162 return -EINVAL;
163 }
164
165 memcpy(bt_mesh.dev_key, net.dev_key, sizeof(bt_mesh.dev_key));
166 bt_mesh_comp_provision(net.primary_addr);
167 BT_DBG("Provisioned with primary address 0x%04x", net.primary_addr);
168 BT_DBG("Recovered DevKey %s", bt_hex(bt_mesh.dev_key, 16)); // 16:len
169 return 0;
170 }
171
iv_set(int argc,char ** argv,char * val)172 static int iv_set(int argc, char **argv, char *val)
173 {
174 struct iv_val iv;
175 int len, err;
176 BT_DBG("val %s", val ? val : "(null)");
177
178 if (!val) {
179 bt_mesh.iv_index = 0U;
180 atomic_clear_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS);
181 return 0;
182 }
183
184 len = sizeof(iv);
185 err = settings_bytes_from_str(val, &iv, &len);
186 if (err) {
187 BT_ERR("Failed to decode value %s (err %d)", val, err);
188 return err;
189 }
190
191 if (len != sizeof(iv)) {
192 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(iv));
193 return -EINVAL;
194 }
195
196 bt_mesh.iv_index = iv.iv_index;
197 atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS, iv.iv_update);
198 bt_mesh.ivu_duration = iv.iv_duration;
199 BT_DBG("IV Index 0x%04x (IV Update Flag %u) duration %u hours",
200 (unsigned) iv.iv_index, iv.iv_update, iv.iv_duration);
201 return 0;
202 }
203
seq_set(int argc,char ** argv,char * val)204 static int seq_set(int argc, char **argv, char *val)
205 {
206 struct seq_val seq;
207 int len, err;
208 BT_DBG("val %s", val ? val : "(null)");
209
210 if (!val) {
211 bt_mesh.seq = 0;
212 return 0;
213 }
214
215 len = sizeof(seq);
216 err = settings_bytes_from_str(val, &seq, &len);
217 if (err) {
218 BT_ERR("Failed to decode value %s (err %d)", val, err);
219 return err;
220 }
221
222 if (len != sizeof(seq)) {
223 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(seq));
224 return -EINVAL;
225 }
226
227 bt_mesh.seq = ((u32_t)seq.val[0] | ((u32_t)seq.val[1] << 8) | // 8:byte alignment
228 ((u32_t)seq.val[2] << 16)); // 16:byte alignment, 2:array element
229
230 if (CONFIG_BT_MESH_SEQ_STORE_RATE > 0) {
231 /* Make sure we have a large enough sequence number. We
232 * subtract 1 so that the first transmission causes a write
233 * to the settings storage.
234 */
235 bt_mesh.seq += (CONFIG_BT_MESH_SEQ_STORE_RATE -
236 (bt_mesh.seq % CONFIG_BT_MESH_SEQ_STORE_RATE));
237 bt_mesh.seq--;
238 }
239
240 BT_DBG("Sequence Number 0x%06x", bt_mesh.seq);
241 return 0;
242 }
243
rpl_find(u16_t src)244 static struct bt_mesh_rpl *rpl_find(u16_t src)
245 {
246 int i;
247
248 for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
249 if (bt_mesh.rpl[i].src == src) {
250 return &bt_mesh.rpl[i];
251 }
252 }
253
254 return NULL;
255 }
256
rpl_alloc(u16_t src)257 static struct bt_mesh_rpl *rpl_alloc(u16_t src)
258 {
259 int i;
260
261 for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
262 if (!bt_mesh.rpl[i].src) {
263 bt_mesh.rpl[i].src = src;
264 return &bt_mesh.rpl[i];
265 }
266 }
267
268 return NULL;
269 }
270
rpl_set(int argc,char ** argv,char * val)271 static int rpl_set(int argc, char **argv, char *val)
272 {
273 struct bt_mesh_rpl *entry;
274 struct rpl_val rpl;
275 int len, err;
276 u16_t src;
277
278 if (argc < 1) {
279 BT_ERR("Invalid argc (%d)", argc);
280 return -ENOENT;
281 }
282
283 BT_DBG("argv[0] %s val %s", argv[0], val ? val : "(null)");
284 src = strtol(argv[0], NULL, 16); // 16:byte alignment
285 entry = rpl_find(src);
286
287 if (!val) {
288 if (entry) {
289 memset(entry, 0, sizeof(*entry));
290 } else {
291 BT_WARN("Unable to find RPL entry for 0x%04x", src);
292 }
293
294 return 0;
295 }
296
297 if (!entry) {
298 entry = rpl_alloc(src);
299 if (!entry) {
300 BT_ERR("Unable to allocate RPL entry for 0x%04x", src);
301 return -ENOMEM;
302 }
303 }
304
305 len = sizeof(rpl);
306 err = settings_bytes_from_str(val, &rpl, &len);
307 if (err) {
308 BT_ERR("Failed to decode value %s (err %d)", val, err);
309 return err;
310 }
311
312 if (len != sizeof(rpl)) {
313 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(rpl));
314 return -EINVAL;
315 }
316
317 entry->seq = rpl.seq;
318 entry->old_iv = rpl.old_iv;
319 BT_DBG("RPL entry for 0x%04x: Seq 0x%06x old_iv %u", entry->src,
320 (unsigned) entry->seq, entry->old_iv);
321 return 0;
322 }
323
net_key_set(int argc,char ** argv,char * val)324 static int net_key_set(int argc, char **argv, char *val)
325 {
326 struct bt_mesh_subnet *sub;
327 struct net_key_val key;
328 int len, i, err;
329 u16_t net_idx;
330 BT_DBG("argv[0] %s val %s", argv[0], val ? val : "(null)");
331 net_idx = strtol(argv[0], NULL, 16); // 16:byte alignment
332 sub = bt_mesh_subnet_get(net_idx);
333
334 if (!val) {
335 if (!sub) {
336 BT_ERR("No subnet with NetKeyIndex 0x%03x", net_idx);
337 return -ENOENT;
338 }
339
340 BT_DBG("Deleting NetKeyIndex 0x%03x", net_idx);
341 bt_mesh_subnet_del(sub, false);
342 return 0;
343 }
344
345 len = sizeof(key);
346 err = settings_bytes_from_str(val, &key, &len);
347 if (err) {
348 BT_ERR("Failed to decode value %s (err %d)", val, err);
349 return err;
350 }
351
352 if (len != sizeof(key)) {
353 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(key));
354 return -EINVAL;
355 }
356
357 if (sub) {
358 BT_DBG("Updating existing NetKeyIndex 0x%03x", net_idx);
359 sub->kr_flag = key.kr_flag;
360 sub->kr_phase = key.kr_phase;
361 memcpy(sub->keys[0].net, &key.val[0], 16); // 16:byte alignment
362 memcpy(sub->keys[1].net, &key.val[1], 16); // 16:byte alignment
363 return 0;
364 }
365
366 for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
367 if (bt_mesh.sub[i].net_idx == BT_MESH_KEY_UNUSED) {
368 sub = &bt_mesh.sub[i];
369 break;
370 }
371 }
372
373 if (!sub) {
374 BT_ERR("No space to allocate a new subnet");
375 return -ENOMEM;
376 }
377
378 sub->net_idx = net_idx;
379 sub->kr_flag = key.kr_flag;
380 sub->kr_phase = key.kr_phase;
381 memcpy(sub->keys[0].net, &key.val[0], 16); // 16:byte alignment
382 memcpy(sub->keys[1].net, &key.val[1], 16); // 16:byte alignment
383 BT_DBG("NetKeyIndex 0x%03x recovered from storage", net_idx);
384 return 0;
385 }
386
app_key_set(int argc,char ** argv,char * val)387 static int app_key_set(int argc, char **argv, char *val)
388 {
389 struct bt_mesh_app_key *app;
390 struct app_key_val key;
391 u16_t app_idx;
392 int len, err;
393 BT_DBG("argv[0] %s val %s", argv[0], val ? val : "(null)");
394 app_idx = strtol(argv[0], NULL, 16); // 16:byte alignment
395
396 if (!val) {
397 BT_DBG("Deleting AppKeyIndex 0x%03x", app_idx);
398 app = bt_mesh_app_key_find(app_idx);
399 if (app) {
400 bt_mesh_app_key_del(app, false);
401 }
402
403 return 0;
404 }
405
406 len = sizeof(key);
407 err = settings_bytes_from_str(val, &key, &len);
408 if (err) {
409 BT_ERR("Failed to decode value %s (err %d)", val, err);
410 return err;
411 }
412
413 if (len != sizeof(key)) {
414 BT_ERR("Unexpected value length (%d != %zu)", len, sizeof(key));
415 return -EINVAL;
416 }
417
418 app = bt_mesh_app_key_find(app_idx);
419 if (!app) {
420 app = bt_mesh_app_key_alloc(app_idx);
421 }
422
423 if (!app) {
424 BT_ERR("No space for a new app key");
425 return -ENOMEM;
426 }
427
428 app->net_idx = key.net_idx;
429 app->app_idx = app_idx;
430 app->updated = key.updated;
431 memcpy(app->keys[0].val, key.val[0], 16); // 16:byte alignment
432 memcpy(app->keys[1].val, key.val[1], 16); // 16:byte alignment
433 bt_mesh_app_id(app->keys[0].val, &app->keys[0].id);
434 bt_mesh_app_id(app->keys[1].val, &app->keys[1].id);
435 BT_DBG("AppKeyIndex 0x%03x recovered from storage", app_idx);
436 return 0;
437 }
438
hb_pub_set(int argc,char ** argv,char * val)439 static int hb_pub_set(int argc, char **argv, char *val)
440 {
441 struct bt_mesh_hb_pub *pub = bt_mesh_hb_pub_get();
442 struct hb_pub_val hb_val;
443 int len, err;
444 BT_DBG("val %s", val ? val : "(null)");
445
446 if (!pub) {
447 return -ENOENT;
448 }
449
450 if (!val) {
451 pub->dst = BT_MESH_ADDR_UNASSIGNED;
452 pub->count = 0;
453 pub->ttl = 0;
454 pub->period = 0;
455 pub->feat = 0;
456 BT_DBG("Cleared heartbeat publication");
457 return 0;
458 }
459
460 len = sizeof(hb_val);
461 err = settings_bytes_from_str(val, &hb_val, &len);
462 if (err) {
463 BT_ERR("Failed to decode value %s (err %d)", val, err);
464 return err;
465 }
466
467 if (len != sizeof(hb_val)) {
468 BT_ERR("Unexpected value length (%d != %zu)", len,
469 sizeof(hb_val));
470 return -EINVAL;
471 }
472
473 pub->dst = hb_val.dst;
474 pub->period = hb_val.period;
475 pub->ttl = hb_val.ttl;
476 pub->feat = hb_val.feat;
477 pub->net_idx = hb_val.net_idx;
478
479 if (hb_val.indefinite) {
480 pub->count = 0xffff;
481 } else {
482 pub->count = 0;
483 }
484
485 BT_DBG("Restored heartbeat publication");
486 return 0;
487 }
488
cfg_set(int argc,char ** argv,char * val)489 static int cfg_set(int argc, char **argv, char *val)
490 {
491 struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get();
492 int len, err;
493 BT_DBG("val %s", val ? val : "(null)");
494
495 if (!cfg) {
496 return -ENOENT;
497 }
498
499 if (!val) {
500 stored_cfg.valid = false;
501 BT_DBG("Cleared configuration state");
502 return 0;
503 }
504
505 len = sizeof(stored_cfg.cfg);
506 err = settings_bytes_from_str(val, &stored_cfg.cfg, &len);
507 if (err) {
508 BT_ERR("Failed to decode value %s (err %d)", val, err);
509 return err;
510 }
511
512 if (len != sizeof(stored_cfg.cfg)) {
513 BT_ERR("Unexpected value length (%d != %zu)", len,
514 sizeof(stored_cfg.cfg));
515 return -EINVAL;
516 }
517
518 stored_cfg.valid = true;
519 BT_DBG("Restored configuration state");
520 return 0;
521 }
522
mod_set_bind(struct bt_mesh_model * mod,char * val)523 static int mod_set_bind(struct bt_mesh_model *mod, char *val)
524 {
525 int len, err, i;
526
527 /* Start with empty array regardless of cleared or set value */
528 for (i = 0; i < ARRAY_SIZE(mod->keys); i++) {
529 mod->keys[i] = BT_MESH_KEY_UNUSED;
530 }
531
532 if (!val) {
533 BT_DBG("Cleared bindings for model");
534 return 0;
535 }
536
537 len = sizeof(mod->keys);
538 err = settings_bytes_from_str(val, mod->keys, &len);
539 if (err) {
540 BT_ERR("Failed to decode value %s (err %d)", val, err);
541 return -EINVAL;
542 }
543
544 BT_DBG("Decoded %u bound keys for model", len / sizeof(mod->keys[0]));
545 return 0;
546 }
547
mod_set_sub(struct bt_mesh_model * mod,char * val)548 static int mod_set_sub(struct bt_mesh_model *mod, char *val)
549 {
550 int len, err;
551 /* Start with empty array regardless of cleared or set value */
552 memset(mod->groups, 0, sizeof(mod->groups));
553
554 if (!val) {
555 BT_DBG("Cleared subscriptions for model");
556 return 0;
557 }
558
559 len = sizeof(mod->groups);
560 err = settings_bytes_from_str(val, mod->groups, &len);
561 if (err) {
562 BT_ERR("Failed to decode value %s (err %d)", val, err);
563 return -EINVAL;
564 }
565
566 BT_DBG("Decoded %u subscribed group addresses for model",
567 len / sizeof(mod->groups[0]));
568 return 0;
569 }
570
mod_set_pub(struct bt_mesh_model * mod,char * val)571 static int mod_set_pub(struct bt_mesh_model *mod, char *val)
572 {
573 struct mod_pub_val pub;
574 int len, err;
575
576 if (!mod->pub) {
577 BT_WARN("Model has no publication context!");
578 return -EINVAL;
579 }
580
581 if (!val) {
582 mod->pub->addr = BT_MESH_ADDR_UNASSIGNED;
583 mod->pub->key = 0;
584 mod->pub->cred = 0;
585 mod->pub->ttl = 0;
586 mod->pub->period = 0;
587 mod->pub->retransmit = 0;
588 mod->pub->count = 0;
589 BT_DBG("Cleared publication for model");
590 return 0;
591 }
592
593 len = sizeof(pub);
594 err = settings_bytes_from_str(val, &pub, &len);
595 if (err) {
596 BT_ERR("Failed to decode value %s (err %d)", val, err);
597 return -EINVAL;
598 }
599
600 if (len != sizeof(pub)) {
601 BT_ERR("Invalid length for model publication");
602 return -EINVAL;
603 }
604
605 mod->pub->addr = pub.addr;
606 mod->pub->key = pub.key;
607 mod->pub->cred = pub.cred;
608 mod->pub->ttl = pub.ttl;
609 mod->pub->period = pub.period;
610 mod->pub->retransmit = pub.retransmit;
611 mod->pub->count = 0;
612 BT_DBG("Restored model publication, dst 0x%04x app_idx 0x%03x",
613 pub.addr, pub.key);
614 return 0;
615 }
616
mod_set(bool vnd,int argc,char ** argv,char * val)617 static int mod_set(bool vnd, int argc, char **argv, char *val)
618 {
619 struct bt_mesh_model *mod;
620 u8_t elem_idx, mod_idx;
621 u16_t mod_key;
622
623 if (argc < 2) { // 2:Number of parameters
624 BT_ERR("Too small argc (%d)", argc);
625 return -ENOENT;
626 }
627
628 mod_key = strtol(argv[0], NULL, 16); // 16:byte alignment
629 elem_idx = mod_key >> 8; // 8:byte alignment
630 mod_idx = mod_key;
631 BT_DBG("Decoded mod_key 0x%04x as elem_idx %u mod_idx %u",
632 mod_key, elem_idx, mod_idx);
633 mod = bt_mesh_model_get(vnd, elem_idx, mod_idx);
634 if (!mod) {
635 BT_ERR("Failed to get model for elem_idx %u mod_idx %u",
636 elem_idx, mod_idx);
637 return -ENOENT;
638 }
639
640 if (!strcmp(argv[1], "bind")) {
641 return mod_set_bind(mod, val);
642 }
643
644 if (!strcmp(argv[1], "sub")) {
645 return mod_set_sub(mod, val);
646 }
647
648 if (!strcmp(argv[1], "pub")) {
649 return mod_set_pub(mod, val);
650 }
651
652 if (!strcmp(argv[1], "data")) {
653 mod->flags |= BT_MESH_MOD_DATA_PRESENT;
654
655 if (mod->cb && mod->cb->settings_set) {
656 return mod->cb->settings_set(mod, val);
657 }
658 }
659
660 BT_WARN("Unknown module key %s", argv[1]);
661 return -ENOENT;
662 }
663
sig_mod_set(int argc,char ** argv,char * val)664 static int sig_mod_set(int argc, char **argv, char *val)
665 {
666 return mod_set(false, argc, argv, val);
667 }
668
vnd_mod_set(int argc,char ** argv,char * val)669 static int vnd_mod_set(int argc, char **argv, char *val)
670 {
671 return mod_set(true, argc, argv, val);
672 }
673
674 #if CONFIG_BT_MESH_LABEL_COUNT > 0
va_set(int argc,char ** argv,char * val)675 static int va_set(int argc, char **argv, char *val)
676 {
677 struct va_val va;
678 struct label *lab;
679 u16_t index;
680 int len, err;
681
682 if (argc < 1) {
683 BT_ERR("Insufficient number of arguments");
684 return -ENOENT;
685 }
686
687 index = strtol(argv[0], NULL, 16); // 16:byte alignment
688
689 if (val == NULL) {
690 BT_WARN("Mesh Virtual Address length = 0");
691 return 0;
692 }
693
694 err = settings_bytes_from_str(val, &va, &len)
695 if (err) {
696 BT_ERR("Failed to decode value %s (err %d)", val, err);
697 return -EINVAL;
698 }
699
700 if (len != sizeof(struct va_val)) {
701 BT_ERR("Invalid length for virtual address");
702 return -EINVAL;
703 }
704
705 if (va.ref == 0) {
706 BT_WARN("Ignore Mesh Virtual Address ref = 0");
707 return 0;
708 }
709
710 lab = get_label(index);
711 if (lab == NULL) {
712 BT_WARN("Out of labels buffers");
713 return -ENOBUFS;
714 }
715
716 memcpy(lab->uuid, va.uuid, 16); // 16:byte alignment
717 lab->addr = va.addr;
718 lab->ref = va.ref;
719 BT_DBG("Restored Virtual Address, addr 0x%04x ref 0x%04x",
720 lab->addr, lab->ref);
721 return 0;
722 }
723 #endif
724
725 #if MYNEWT_VAL(BLE_MESH_PROVISIONER)
node_set(int argc,char ** argv,char * str)726 static int node_set(int argc, char **argv, char *str)
727 {
728 struct bt_mesh_node *node;
729 struct node_val val;
730 u16_t addr;
731 int len, err;
732
733 if (argc < 1) {
734 BT_ERR("Insufficient number of arguments");
735 return -ENOENT;
736 }
737
738 addr = strtol(argv[0], NULL, 16); // 16:byte alignment
739
740 if (str == NULL) {
741 BT_DBG("val (null)");
742 BT_DBG("Deleting node 0x%04x", addr);
743 node = bt_mesh_node_find(addr);
744 if (node) {
745 bt_mesh_node_del(node, false);
746 }
747
748 return 0;
749 }
750
751 err = settings_bytes_from_str(str, &val, &len);
752 if (err) {
753 BT_ERR("Failed to decode value %s (err %d)", val, err);
754 return -EINVAL;
755 }
756
757 if (len != sizeof(struct node_val)) {
758 BT_ERR("Invalid length for node_val");
759 return -EINVAL;
760 }
761
762 node = bt_mesh_node_find(addr);
763 if (!node) {
764 node = bt_mesh_node_alloc(addr, val.num_elem, val.net_idx);
765 }
766
767 if (!node) {
768 BT_ERR("No space for a new node");
769 return -ENOMEM;
770 }
771
772 memcpy(node->dev_key, &val.dev_key, 16); // 16:byte alignment
773 BT_DBG("Node 0x%04x recovered from storage", addr);
774 return 0;
775 }
776 #endif
777
778 const struct mesh_setting {
779 const char *name;
780 int (*func)(int argc, char **argv, char *val);
781 } settings[] = {
782 { "Net", net_set },
783 { "IV", iv_set },
784 { "Seq", seq_set },
785 { "RPL", rpl_set },
786 { "NetKey", net_key_set },
787 { "AppKey", app_key_set },
788 { "HBPub", hb_pub_set },
789 { "Cfg", cfg_set },
790 { "s", sig_mod_set },
791 { "v", vnd_mod_set },
792 #if CONFIG_BT_MESH_LABEL_COUNT > 0
793 { "Va", va_set },
794 #endif
795 #if MYNEWT_VAL(BLE_MESH_PROVISIONER)
796 { "Node", node_set },
797 #endif
798 };
799
mesh_set(int argc,char ** argv,char * val)800 static int mesh_set(int argc, char **argv, char *val)
801 {
802 int i;
803
804 if (argc < 1) {
805 BT_ERR("Insufficient number of arguments");
806 return -EINVAL;
807 }
808
809 BT_DBG("argv[0] %s val %s", argv[0], val ? val : "(null)");
810
811 for (i = 0; i < ARRAY_SIZE(settings); i++) {
812 if (!strcmp(settings[i].name, argv[0])) {
813 argc--;
814 argv++;
815 return settings[i].func(argc, argv, val);
816 }
817 }
818
819 BT_WARN("No matching handler for key %s", argv[0]);
820 return -ENOENT;
821 }
822
subnet_init(struct bt_mesh_subnet * sub)823 static int subnet_init(struct bt_mesh_subnet *sub)
824 {
825 int err;
826 err = bt_mesh_net_keys_create(&sub->keys[0], sub->keys[0].net);
827 if (err) {
828 BT_ERR("Unable to generate keys for subnet");
829 return -EIO;
830 }
831
832 if (sub->kr_phase != BT_MESH_KR_NORMAL) {
833 err = bt_mesh_net_keys_create(&sub->keys[1], sub->keys[1].net);
834 if (err) {
835 BT_ERR("Unable to generate keys for subnet");
836 memset(&sub->keys[0], 0, sizeof(sub->keys[0]));
837 return -EIO;
838 }
839 }
840
841 if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
842 sub->node_id = BT_MESH_NODE_IDENTITY_STOPPED;
843 } else {
844 sub->node_id = BT_MESH_NODE_IDENTITY_NOT_SUPPORTED;
845 }
846
847 /* Make sure we have valid beacon data to be sent */
848 bt_mesh_net_beacon_update(sub);
849 return 0;
850 }
851
commit_mod(struct bt_mesh_model * mod,struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)852 static void commit_mod(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
853 bool vnd, bool primary, void *user_data)
854 {
855 if (mod->pub && mod->pub->update &&
856 mod->pub->addr != BT_MESH_ADDR_UNASSIGNED) {
857 s32_t ms = bt_mesh_model_pub_period_get(mod);
858 if (ms) {
859 BT_DBG("Starting publish timer (period %u ms)",
860 (unsigned) ms);
861 k_delayed_work_submit(&mod->pub->timer, ms);
862 }
863 }
864
865 if (mod->cb && mod->cb->settings_commit) {
866 mod->cb->settings_commit(mod);
867 }
868 }
869
mesh_commit(void)870 static int mesh_commit(void)
871 {
872 struct bt_mesh_hb_pub *hb_pub;
873 struct bt_mesh_cfg_srv *cfg;
874 int i;
875 BT_DBG("sub[0].net_idx 0x%03x", bt_mesh.sub[0].net_idx);
876
877 if (bt_mesh.sub[0].net_idx == BT_MESH_KEY_UNUSED) {
878 /* Nothing to do since we're not yet provisioned */
879 return 0;
880 }
881
882 if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) {
883 bt_mesh_proxy_prov_disable(true);
884 }
885
886 for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
887 struct bt_mesh_subnet *sub = &bt_mesh.sub[i];
888 int err;
889
890 if (sub->net_idx == BT_MESH_KEY_UNUSED) {
891 continue;
892 }
893
894 err = subnet_init(sub);
895 if (err) {
896 BT_ERR("Failed to init subnet 0x%03x", sub->net_idx);
897 }
898 }
899
900 if (bt_mesh.ivu_duration < BT_MESH_IVU_MIN_HOURS) {
901 k_delayed_work_submit(&bt_mesh.ivu_timer, BT_MESH_IVU_TIMEOUT);
902 }
903
904 bt_mesh_model_foreach(commit_mod, NULL);
905 hb_pub = bt_mesh_hb_pub_get();
906 if (hb_pub && hb_pub->dst != BT_MESH_ADDR_UNASSIGNED &&
907 hb_pub->count && hb_pub->period) {
908 BT_DBG("Starting heartbeat publication");
909 k_work_submit(&hb_pub->timer.work);
910 }
911
912 cfg = bt_mesh_cfg_get();
913 if (cfg && stored_cfg.valid) {
914 cfg->net_transmit = stored_cfg.cfg.net_transmit;
915 cfg->relay = stored_cfg.cfg.relay;
916 cfg->relay_retransmit = stored_cfg.cfg.relay_retransmit;
917 cfg->beacon = stored_cfg.cfg.beacon;
918 cfg->gatt_proxy = stored_cfg.cfg.gatt_proxy;
919 cfg->frnd = stored_cfg.cfg.frnd;
920 cfg->default_ttl = stored_cfg.cfg.default_ttl;
921 }
922
923 atomic_set_bit(bt_mesh.flags, BT_MESH_VALID);
924 bt_mesh_net_start();
925 return 0;
926 }
927
928 /* Pending flags that use K_NO_WAIT as the storage timeout */
929 #define NO_WAIT_PENDING_BITS (BIT(BT_MESH_NET_PENDING) | \
930 BIT(BT_MESH_IV_PENDING) | \
931 BIT(BT_MESH_SEQ_PENDING))
932
933 /* Pending flags that use CONFIG_BT_MESH_STORE_TIMEOUT */
934 #define GENERIC_PENDING_BITS (BIT(BT_MESH_KEYS_PENDING) | \
935 BIT(BT_MESH_HB_PUB_PENDING) | \
936 BIT(BT_MESH_CFG_PENDING) | \
937 BIT(BT_MESH_MOD_PENDING) | \
938 BIT(BT_MESH_NODES_PENDING))
939
schedule_store(int flag)940 static void schedule_store(int flag)
941 {
942 s32_t timeout, remaining;
943 atomic_set_bit(bt_mesh.flags, flag);
944
945 if (atomic_get(bt_mesh.flags) & NO_WAIT_PENDING_BITS) {
946 timeout = K_NO_WAIT;
947 } else if (atomic_test_bit(bt_mesh.flags, BT_MESH_RPL_PENDING) &&
948 (!(atomic_get(bt_mesh.flags) & GENERIC_PENDING_BITS) ||
949 (CONFIG_BT_MESH_RPL_STORE_TIMEOUT <
950 CONFIG_BT_MESH_STORE_TIMEOUT))) {
951 timeout = K_SECONDS(CONFIG_BT_MESH_RPL_STORE_TIMEOUT);
952 } else {
953 timeout = K_SECONDS(CONFIG_BT_MESH_STORE_TIMEOUT);
954 }
955
956 remaining = k_delayed_work_remaining_get(&pending_store);
957 if (remaining && remaining < timeout) {
958 BT_DBG("Not rescheduling due to existing earlier deadline");
959 return;
960 }
961
962 BT_DBG("Waiting %d seconds", (int)(timeout / MSEC_PER_SEC));
963 k_delayed_work_submit(&pending_store, timeout);
964 }
965
clear_iv(void)966 static void clear_iv(void)
967 {
968 int err;
969 err = settings_save_one("bt_mesh/IV", NULL);
970 if (err) {
971 BT_ERR("Failed to clear IV");
972 } else {
973 BT_DBG("Cleared IV");
974 }
975 }
976
clear_net(void)977 static void clear_net(void)
978 {
979 int err;
980 err = settings_save_one("bt_mesh/Net", NULL);
981 if (err) {
982 BT_ERR("Failed to clear Network");
983 } else {
984 BT_DBG("Cleared Network");
985 }
986 }
987
store_pending_net(void)988 static void store_pending_net(void)
989 {
990 char buf[BT_SETTINGS_SIZE(sizeof(struct net_val))];
991 struct net_val net;
992 char *str;
993 int err;
994 BT_DBG("addr 0x%04x DevKey %s", bt_mesh_primary_addr(),
995 bt_hex(bt_mesh.dev_key, 16)); // 16:byte alignment
996 net.primary_addr = bt_mesh_primary_addr();
997 memcpy(net.dev_key, bt_mesh.dev_key, 16); // 16:size
998 str = settings_str_from_bytes(&net, sizeof(net), buf, sizeof(buf));
999 if (!str) {
1000 BT_ERR("Unable to encode Network as value");
1001 return;
1002 }
1003
1004 BT_DBG("Saving Network as value %s", str);
1005 err = settings_save_one("bt_mesh/Net", str);
1006 if (err) {
1007 BT_ERR("Failed to store Network");
1008 } else {
1009 BT_DBG("Stored Network");
1010 }
1011 }
1012
bt_mesh_store_net(void)1013 void bt_mesh_store_net(void)
1014 {
1015 schedule_store(BT_MESH_NET_PENDING);
1016 }
1017
store_pending_iv(void)1018 static void store_pending_iv(void)
1019 {
1020 char buf[BT_SETTINGS_SIZE(sizeof(struct iv_val))];
1021 struct iv_val iv;
1022 char *str;
1023 int err;
1024 iv.iv_index = bt_mesh.iv_index;
1025 iv.iv_update = atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS);
1026 iv.iv_duration = bt_mesh.ivu_duration;
1027 str = settings_str_from_bytes(&iv, sizeof(iv), buf, sizeof(buf));
1028 if (!str) {
1029 BT_ERR("Unable to encode IV as value");
1030 return;
1031 }
1032
1033 BT_DBG("Saving IV as value %s", str);
1034 err = settings_save_one("bt_mesh/IV", str);
1035 if (err) {
1036 BT_ERR("Failed to store IV");
1037 } else {
1038 BT_DBG("Stored IV");
1039 }
1040 }
1041
bt_mesh_store_iv(bool only_duration)1042 void bt_mesh_store_iv(bool only_duration)
1043 {
1044 schedule_store(BT_MESH_IV_PENDING);
1045
1046 if (!only_duration) {
1047 /* Always update Seq whenever IV changes */
1048 schedule_store(BT_MESH_SEQ_PENDING);
1049 }
1050 }
1051
store_pending_seq(void)1052 static void store_pending_seq(void)
1053 {
1054 char buf[BT_SETTINGS_SIZE(sizeof(struct seq_val))];
1055 struct seq_val seq;
1056 char *str;
1057 int err;
1058 seq.val[0] = bt_mesh.seq;
1059 seq.val[1] = bt_mesh.seq >> 8; // 8:byte alignment
1060 seq.val[2] = bt_mesh.seq >> 16; // 16:byte alignment
1061 str = settings_str_from_bytes(&seq, sizeof(seq), buf, sizeof(buf));
1062 if (!str) {
1063 BT_ERR("Unable to encode Seq as value");
1064 return;
1065 }
1066
1067 BT_DBG("Saving Seq as value %s", str);
1068 err = settings_save_one("bt_mesh/Seq", str);
1069 if (err) {
1070 BT_ERR("Failed to store Seq");
1071 } else {
1072 BT_DBG("Stored Seq");
1073 }
1074 }
1075
bt_mesh_store_seq(void)1076 void bt_mesh_store_seq(void)
1077 {
1078 if (CONFIG_BT_MESH_SEQ_STORE_RATE &&
1079 (bt_mesh.seq % CONFIG_BT_MESH_SEQ_STORE_RATE)) {
1080 return;
1081 }
1082
1083 schedule_store(BT_MESH_SEQ_PENDING);
1084 }
1085
store_rpl(struct bt_mesh_rpl * entry)1086 static void store_rpl(struct bt_mesh_rpl *entry)
1087 {
1088 char buf[BT_SETTINGS_SIZE(sizeof(struct rpl_val))];
1089 struct rpl_val rpl;
1090 char path[18];
1091 char *str;
1092 int err;
1093 BT_DBG("src 0x%04x seq 0x%06x old_iv %u", entry->src,
1094 (unsigned) entry->seq, entry->old_iv);
1095 rpl.seq = entry->seq;
1096 rpl.old_iv = entry->old_iv;
1097 str = settings_str_from_bytes(&rpl, sizeof(rpl), buf, sizeof(buf));
1098 if (!str) {
1099 BT_ERR("Unable to encode RPL as value");
1100 return;
1101 }
1102
1103 snprintk(path, sizeof(path), "bt_mesh/RPL/%x", entry->src);
1104 BT_DBG("Saving RPL %s as value %s", path, str);
1105 err = settings_save_one(path, str);
1106 if (err) {
1107 BT_ERR("Failed to store RPL");
1108 } else {
1109 BT_DBG("Stored RPL");
1110 }
1111 }
1112
clear_rpl(void)1113 static void clear_rpl(void)
1114 {
1115 int i, err;
1116 BT_DBG("");
1117
1118 for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
1119 struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i];
1120 char path[18];
1121
1122 if (!rpl->src) {
1123 continue;
1124 }
1125
1126 snprintk(path, sizeof(path), "bt_mesh/RPL/%x", rpl->src);
1127 err = settings_save_one(path, NULL);
1128 if (err) {
1129 BT_ERR("Failed to clear RPL");
1130 } else {
1131 BT_DBG("Cleared RPL");
1132 }
1133
1134 memset(rpl, 0, sizeof(*rpl));
1135 }
1136 }
1137
store_pending_rpl(void)1138 static void store_pending_rpl(void)
1139 {
1140 int i;
1141 BT_DBG("");
1142
1143 for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
1144 struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i];
1145
1146 if (rpl->store) {
1147 rpl->store = false;
1148 store_rpl(rpl);
1149 }
1150 }
1151 }
1152
store_pending_hb_pub(void)1153 static void store_pending_hb_pub(void)
1154 {
1155 char buf[BT_SETTINGS_SIZE(sizeof(struct hb_pub_val))];
1156 struct bt_mesh_hb_pub *pub = bt_mesh_hb_pub_get();
1157 struct hb_pub_val val;
1158 char *str;
1159 int err;
1160
1161 if (!pub) {
1162 return;
1163 }
1164
1165 if (pub->dst == BT_MESH_ADDR_UNASSIGNED) {
1166 str = NULL;
1167 } else {
1168 val.indefinite = (pub->count == 0xffff);
1169 val.dst = pub->dst;
1170 val.period = pub->period;
1171 val.ttl = pub->ttl;
1172 val.feat = pub->feat;
1173 val.net_idx = pub->net_idx;
1174 str = settings_str_from_bytes(&val, sizeof(val),
1175 buf, sizeof(buf));
1176 if (!str) {
1177 BT_ERR("Unable to encode hb pub as value");
1178 return;
1179 }
1180 }
1181
1182 BT_DBG("Saving Heartbeat Publication as value %s",
1183 str ? str : "(null)");
1184 err = settings_save_one("bt_mesh/HBPub", str);
1185 if (err) {
1186 BT_ERR("Failed to store Heartbeat Publication");
1187 } else {
1188 BT_DBG("Stored Heartbeat Publication");
1189 }
1190 }
1191
store_pending_cfg(void)1192 static void store_pending_cfg(void)
1193 {
1194 char buf[BT_SETTINGS_SIZE(sizeof(struct cfg_val))];
1195 struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get();
1196 struct cfg_val val;
1197 char *str;
1198 int err;
1199
1200 if (!cfg) {
1201 return;
1202 }
1203
1204 val.net_transmit = cfg->net_transmit;
1205 val.relay = cfg->relay;
1206 val.relay_retransmit = cfg->relay_retransmit;
1207 val.beacon = cfg->beacon;
1208 val.gatt_proxy = cfg->gatt_proxy;
1209 val.frnd = cfg->frnd;
1210 val.default_ttl = cfg->default_ttl;
1211 str = settings_str_from_bytes(&val, sizeof(val), buf, sizeof(buf));
1212 if (!str) {
1213 BT_ERR("Unable to encode configuration as value");
1214 return;
1215 }
1216
1217 BT_DBG("Saving configuration as value %s", str);
1218 err = settings_save_one("bt_mesh/Cfg", str);
1219 if (err) {
1220 BT_ERR("Failed to store configuration");
1221 } else {
1222 BT_DBG("Stored configuration");
1223 }
1224 }
1225
clear_cfg(void)1226 static void clear_cfg(void)
1227 {
1228 int err;
1229 err = settings_save_one("bt_mesh/Cfg", NULL);
1230 if (err) {
1231 BT_ERR("Failed to clear configuration");
1232 } else {
1233 BT_DBG("Cleared configuration");
1234 }
1235 }
1236
clear_app_key(u16_t app_idx)1237 static void clear_app_key(u16_t app_idx)
1238 {
1239 char path[20];
1240 int err;
1241 BT_DBG("AppKeyIndex 0x%03x", app_idx);
1242 snprintk(path, sizeof(path), "bt_mesh/AppKey/%x", app_idx);
1243 err = settings_save_one(path, NULL);
1244 if (err) {
1245 BT_ERR("Failed to clear AppKeyIndex 0x%03x", app_idx);
1246 } else {
1247 BT_DBG("Cleared AppKeyIndex 0x%03x", app_idx);
1248 }
1249 }
1250
clear_net_key(u16_t net_idx)1251 static void clear_net_key(u16_t net_idx)
1252 {
1253 char path[20];
1254 int err;
1255 BT_DBG("NetKeyIndex 0x%03x", net_idx);
1256 snprintk(path, sizeof(path), "bt_mesh/NetKey/%x", net_idx);
1257 err = settings_save_one(path, NULL);
1258 if (err) {
1259 BT_ERR("Failed to clear NetKeyIndex 0x%03x", net_idx);
1260 } else {
1261 BT_DBG("Cleared NetKeyIndex 0x%03x", net_idx);
1262 }
1263 }
1264
store_net_key(struct bt_mesh_subnet * sub)1265 static void store_net_key(struct bt_mesh_subnet *sub)
1266 {
1267 char buf[BT_SETTINGS_SIZE(sizeof(struct net_key_val))];
1268 struct net_key_val key;
1269 char path[20];
1270 char *str;
1271 int err;
1272 BT_DBG("NetKeyIndex 0x%03x NetKey %s", sub->net_idx,
1273 bt_hex(sub->keys[0].net, 16)); // 16:len
1274 memcpy(&key.val[0], sub->keys[0].net, 16); // 16:size
1275 memcpy(&key.val[1], sub->keys[1].net, 16); // 16:size
1276 key.kr_flag = sub->kr_flag;
1277 key.kr_phase = sub->kr_phase;
1278 str = settings_str_from_bytes(&key, sizeof(key), buf, sizeof(buf));
1279 if (!str) {
1280 BT_ERR("Unable to encode NetKey as value");
1281 return;
1282 }
1283
1284 snprintk(path, sizeof(path), "bt_mesh/NetKey/%x", sub->net_idx);
1285 BT_DBG("Saving NetKey %s as value %s", path, str);
1286 err = settings_save_one(path, str);
1287 if (err) {
1288 BT_ERR("Failed to store NetKey");
1289 } else {
1290 BT_DBG("Stored NetKey");
1291 }
1292 }
1293
store_app_key(struct bt_mesh_app_key * app)1294 static void store_app_key(struct bt_mesh_app_key *app)
1295 {
1296 char buf[BT_SETTINGS_SIZE(sizeof(struct app_key_val))];
1297 struct app_key_val key;
1298 char path[20];
1299 char *str;
1300 int err;
1301 key.net_idx = app->net_idx;
1302 key.updated = app->updated;
1303 memcpy(key.val[0], app->keys[0].val, 16); // 16:size
1304 memcpy(key.val[1], app->keys[1].val, 16); // 16:size
1305 str = settings_str_from_bytes(&key, sizeof(key), buf, sizeof(buf));
1306 if (!str) {
1307 BT_ERR("Unable to encode AppKey as value");
1308 return;
1309 }
1310
1311 snprintk(path, sizeof(path), "bt_mesh/AppKey/%x", app->app_idx);
1312 BT_DBG("Saving AppKey %s as value %s", path, str);
1313 err = settings_save_one(path, str);
1314 if (err) {
1315 BT_ERR("Failed to store AppKey");
1316 } else {
1317 BT_DBG("Stored AppKey");
1318 }
1319 }
1320
store_pending_keys(void)1321 static void store_pending_keys(void)
1322 {
1323 int i;
1324
1325 for (i = 0; i < ARRAY_SIZE(key_updates); i++) {
1326 struct key_update *update = &key_updates[i];
1327
1328 if (!update->valid) {
1329 continue;
1330 }
1331
1332 if (update->clear) {
1333 if (update->app_key) {
1334 clear_app_key(update->key_idx);
1335 } else {
1336 clear_net_key(update->key_idx);
1337 }
1338 } else {
1339 if (update->app_key) {
1340 struct bt_mesh_app_key *key;
1341 key = bt_mesh_app_key_find(update->key_idx);
1342 if (key) {
1343 store_app_key(key);
1344 } else {
1345 BT_WARN("AppKeyIndex 0x%03x not found",
1346 update->key_idx);
1347 }
1348 } else {
1349 struct bt_mesh_subnet *sub;
1350 sub = bt_mesh_subnet_get(update->key_idx);
1351 if (sub) {
1352 store_net_key(sub);
1353 } else {
1354 BT_WARN("NetKeyIndex 0x%03x not found",
1355 update->key_idx);
1356 }
1357 }
1358 }
1359
1360 update->valid = 0;
1361 }
1362 }
1363
store_node(struct bt_mesh_node * node)1364 static void store_node(struct bt_mesh_node *node)
1365 {
1366 char buf[BT_SETTINGS_SIZE(sizeof(struct node_val))];
1367 struct node_val val;
1368 char path[20];
1369 char *str;
1370 int err;
1371 val.net_idx = node->net_idx;
1372 val.num_elem = node->num_elem;
1373 memcpy(val.dev_key, node->dev_key, 16); // 16:size
1374 snprintk(path, sizeof(path), "bt_mesh/Node/%x", node->addr);
1375 str = settings_str_from_bytes(&val, sizeof(val), buf, sizeof(buf));
1376 if (!str) {
1377 BT_ERR("Unable to encode Node as value");
1378 return;
1379 }
1380
1381 err = settings_save_one(path, str);
1382 if (err) {
1383 BT_ERR("Failed to store Node %s value", path);
1384 } else {
1385 BT_DBG("Stored Node %s value", path);
1386 }
1387 }
1388
clear_node(u16_t addr)1389 static void clear_node(u16_t addr)
1390 {
1391 char path[20];
1392 int err;
1393 BT_DBG("Node 0x%04x", addr);
1394 snprintk(path, sizeof(path), "bt_mesh/Node/%x", addr);
1395 err = settings_save_one(path, NULL);
1396 if (err) {
1397 BT_ERR("Failed to clear Node 0x%04x", addr);
1398 } else {
1399 BT_DBG("Cleared Node 0x%04x", addr);
1400 }
1401 }
1402
store_pending_nodes(void)1403 static void store_pending_nodes(void)
1404 {
1405 int i;
1406
1407 for (i = 0; i < ARRAY_SIZE(node_updates); ++i) {
1408 struct node_update *update = &node_updates[i];
1409
1410 if (update->addr == BT_MESH_ADDR_UNASSIGNED) {
1411 continue;
1412 }
1413
1414 if (update->clear) {
1415 clear_node(update->addr);
1416 } else {
1417 struct bt_mesh_node *node;
1418 node = bt_mesh_node_find(update->addr);
1419 if (node) {
1420 store_node(node);
1421 } else {
1422 BT_WARN("Node 0x%04x not found", update->addr);
1423 }
1424 }
1425
1426 update->addr = BT_MESH_ADDR_UNASSIGNED;
1427 }
1428 }
1429
node_update_find(u16_t addr,struct node_update ** free_slot)1430 static struct node_update *node_update_find(u16_t addr,
1431 struct node_update **free_slot)
1432 {
1433 struct node_update *match;
1434 int i;
1435 match = NULL;
1436 *free_slot = NULL;
1437
1438 for (i = 0; i < ARRAY_SIZE(node_updates); i++) {
1439 struct node_update *update = &node_updates[i];
1440
1441 if (update->addr == BT_MESH_ADDR_UNASSIGNED) {
1442 *free_slot = update;
1443 continue;
1444 }
1445
1446 if (update->addr == addr) {
1447 match = update;
1448 }
1449 }
1450
1451 return match;
1452 }
1453
encode_mod_path(struct bt_mesh_model * mod,bool vnd,const char * key,char * path,size_t path_len)1454 static void encode_mod_path(struct bt_mesh_model *mod, bool vnd,
1455 const char *key, char *path, size_t path_len)
1456 {
1457 u16_t mod_key = (((u16_t)mod->elem_idx << 8) | mod->mod_idx);
1458
1459 if (vnd) {
1460 snprintk(path, path_len, "bt_mesh/v/%x/%s", mod_key, key);
1461 } else {
1462 snprintk(path, path_len, "bt_mesh/s/%x/%s", mod_key, key);
1463 }
1464 }
1465
store_pending_mod_bind(struct bt_mesh_model * mod,bool vnd)1466 static void store_pending_mod_bind(struct bt_mesh_model *mod, bool vnd)
1467 {
1468 u16_t keys[CONFIG_BT_MESH_MODEL_KEY_COUNT];
1469 char buf[BT_SETTINGS_SIZE(sizeof(keys))];
1470 char path[20];
1471 int i, count, err;
1472 char *val;
1473
1474 for (i = 0, count = 0; i < ARRAY_SIZE(mod->keys); i++) {
1475 if (mod->keys[i] != BT_MESH_KEY_UNUSED) {
1476 keys[count++] = mod->keys[i];
1477 }
1478 }
1479
1480 if (count) {
1481 val = settings_str_from_bytes(keys, count * sizeof(keys[0]),
1482 buf, sizeof(buf));
1483 if (!val) {
1484 BT_ERR("Unable to encode model bindings as value");
1485 return;
1486 }
1487 } else {
1488 val = NULL;
1489 }
1490
1491 encode_mod_path(mod, vnd, "bind", path, sizeof(path));
1492 BT_DBG("Saving %s as %s", path, val ? val : "(null)");
1493 err = settings_save_one(path, val);
1494 if (err) {
1495 BT_ERR("Failed to store bind");
1496 } else {
1497 BT_DBG("Stored bind");
1498 }
1499 }
1500
store_pending_mod_sub(struct bt_mesh_model * mod,bool vnd)1501 static void store_pending_mod_sub(struct bt_mesh_model *mod, bool vnd)
1502 {
1503 u16_t groups[CONFIG_BT_MESH_MODEL_GROUP_COUNT];
1504 char buf[BT_SETTINGS_SIZE(sizeof(groups))];
1505 char path[20];
1506 int i, count, err;
1507 char *val;
1508
1509 for (i = 0, count = 0; i < CONFIG_BT_MESH_MODEL_GROUP_COUNT; i++) {
1510 if (mod->groups[i] != BT_MESH_ADDR_UNASSIGNED) {
1511 groups[count++] = mod->groups[i];
1512 }
1513 }
1514
1515 if (count) {
1516 val = settings_str_from_bytes(groups, count * sizeof(groups[0]),
1517 buf, sizeof(buf));
1518 if (!val) {
1519 BT_ERR("Unable to encode model subscription as value");
1520 return;
1521 }
1522 } else {
1523 val = NULL;
1524 }
1525
1526 encode_mod_path(mod, vnd, "sub", path, sizeof(path));
1527 BT_DBG("Saving %s as %s", path, val ? val : "(null)");
1528 err = settings_save_one(path, val);
1529 if (err) {
1530 BT_ERR("Failed to store sub");
1531 } else {
1532 BT_DBG("Stored sub");
1533 }
1534 }
1535
store_pending_mod_pub(struct bt_mesh_model * mod,bool vnd)1536 static void store_pending_mod_pub(struct bt_mesh_model *mod, bool vnd)
1537 {
1538 char buf[BT_SETTINGS_SIZE(sizeof(struct mod_pub_val))];
1539 struct mod_pub_val pub;
1540 char path[20];
1541 char *val;
1542 int err;
1543
1544 if (!mod->pub || mod->pub->addr == BT_MESH_ADDR_UNASSIGNED) {
1545 val = NULL;
1546 } else {
1547 pub.addr = mod->pub->addr;
1548 pub.key = mod->pub->key;
1549 pub.ttl = mod->pub->ttl;
1550 pub.retransmit = mod->pub->retransmit;
1551 pub.period = mod->pub->period;
1552 pub.period_div = mod->pub->period_div;
1553 pub.cred = mod->pub->cred;
1554 val = settings_str_from_bytes(&pub, sizeof(pub),
1555 buf, sizeof(buf));
1556 if (!val) {
1557 BT_ERR("Unable to encode model publication as value");
1558 return;
1559 }
1560 }
1561
1562 encode_mod_path(mod, vnd, "pub", path, sizeof(path));
1563 BT_DBG("Saving %s as %s", path, val ? val : "(null)");
1564 err = settings_save_one(path, val);
1565 if (err) {
1566 BT_ERR("Failed to store pub");
1567 } else {
1568 BT_DBG("Stored pub");
1569 }
1570 }
1571
store_pending_mod(struct bt_mesh_model * mod,struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)1572 static void store_pending_mod(struct bt_mesh_model *mod,
1573 struct bt_mesh_elem *elem, bool vnd,
1574 bool primary, void *user_data)
1575 {
1576 if (!mod->flags) {
1577 return;
1578 }
1579
1580 if (mod->flags & BT_MESH_MOD_BIND_PENDING) {
1581 mod->flags &= ~BT_MESH_MOD_BIND_PENDING;
1582 store_pending_mod_bind(mod, vnd);
1583 }
1584
1585 if (mod->flags & BT_MESH_MOD_SUB_PENDING) {
1586 mod->flags &= ~BT_MESH_MOD_SUB_PENDING;
1587 store_pending_mod_sub(mod, vnd);
1588 }
1589
1590 if (mod->flags & BT_MESH_MOD_PUB_PENDING) {
1591 mod->flags &= ~BT_MESH_MOD_PUB_PENDING;
1592 store_pending_mod_pub(mod, vnd);
1593 }
1594 }
1595
1596 #define IS_VA_DEL(_label) ((_label)->ref == 0)
store_pending_va(void)1597 static void store_pending_va(void)
1598 {
1599 char buf[BT_SETTINGS_SIZE(sizeof(struct va_val))];
1600 struct label *lab;
1601 struct va_val va;
1602 char path[18];
1603 char *val;
1604 u16_t i;
1605 int err = 0;
1606
1607 for (i = 0; (lab = get_label(i)) != NULL; i++) {
1608 if (!atomic_test_and_clear_bit(lab->flags,
1609 BT_MESH_VA_CHANGED)) {
1610 continue;
1611 }
1612
1613 snprintk(path, sizeof(path), "bt_mesh/Va/%x", i);
1614
1615 if (IS_VA_DEL(lab)) {
1616 val = NULL;
1617 } else {
1618 va.ref = lab->ref;
1619 va.addr = lab->addr;
1620 memcpy(va.uuid, lab->uuid, 16); // 16:size
1621 val = settings_str_from_bytes(&va, sizeof(va),
1622 buf, sizeof(buf));
1623 if (!val) {
1624 BT_ERR("Unable to encode model publication as value");
1625 return;
1626 }
1627 err = settings_save_one(path, val);
1628 }
1629
1630 if (err) {
1631 BT_ERR("Failed to %s %s value (err %d)",
1632 IS_VA_DEL(lab) ? "delete" : "store", path, err);
1633 } else {
1634 BT_DBG("%s %s value",
1635 IS_VA_DEL(lab) ? "Deleted" : "Stored", path);
1636 }
1637 }
1638 }
1639
store_pending(struct ble_npl_event * work)1640 static void store_pending(struct ble_npl_event *work)
1641 {
1642 BT_DBG("");
1643
1644 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_RPL_PENDING)) {
1645 if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
1646 store_pending_rpl();
1647 } else {
1648 clear_rpl();
1649 }
1650 }
1651
1652 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_KEYS_PENDING)) {
1653 store_pending_keys();
1654 }
1655
1656 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_NET_PENDING)) {
1657 if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
1658 store_pending_net();
1659 } else {
1660 clear_net();
1661 }
1662 }
1663
1664 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_IV_PENDING)) {
1665 if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
1666 store_pending_iv();
1667 } else {
1668 clear_iv();
1669 }
1670 }
1671
1672 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_SEQ_PENDING)) {
1673 store_pending_seq();
1674 }
1675
1676 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_HB_PUB_PENDING)) {
1677 store_pending_hb_pub();
1678 }
1679
1680 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_CFG_PENDING)) {
1681 if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
1682 store_pending_cfg();
1683 } else {
1684 clear_cfg();
1685 }
1686 }
1687
1688 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_MOD_PENDING)) {
1689 bt_mesh_model_foreach(store_pending_mod, NULL);
1690 }
1691
1692 if (atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_VA_PENDING)) {
1693 store_pending_va();
1694 }
1695
1696 if (IS_ENABLED(CONFIG_BT_MESH_PROVISIONER) &&
1697 atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_NODES_PENDING)) {
1698 store_pending_nodes();
1699 }
1700 }
1701
bt_mesh_store_rpl(struct bt_mesh_rpl * entry)1702 void bt_mesh_store_rpl(struct bt_mesh_rpl *entry)
1703 {
1704 entry->store = true;
1705 schedule_store(BT_MESH_RPL_PENDING);
1706 }
1707
key_update_find(bool app_key,u16_t key_idx,struct key_update ** free_slot)1708 static struct key_update *key_update_find(bool app_key, u16_t key_idx,
1709 struct key_update **free_slot)
1710 {
1711 struct key_update *match;
1712 int i;
1713 match = NULL;
1714 *free_slot = NULL;
1715
1716 for (i = 0; i < ARRAY_SIZE(key_updates); i++) {
1717 struct key_update *update = &key_updates[i];
1718
1719 if (!update->valid) {
1720 *free_slot = update;
1721 continue;
1722 }
1723
1724 if (update->app_key != app_key) {
1725 continue;
1726 }
1727
1728 if (update->key_idx == key_idx) {
1729 match = update;
1730 }
1731 }
1732
1733 return match;
1734 }
1735
bt_mesh_store_subnet(struct bt_mesh_subnet * sub)1736 void bt_mesh_store_subnet(struct bt_mesh_subnet *sub)
1737 {
1738 struct key_update *update, *free_slot;
1739 BT_DBG("NetKeyIndex 0x%03x", sub->net_idx);
1740 update = key_update_find(false, sub->net_idx, &free_slot);
1741 if (update) {
1742 update->clear = 0;
1743 schedule_store(BT_MESH_KEYS_PENDING);
1744 return;
1745 }
1746
1747 if (!free_slot) {
1748 store_net_key(sub);
1749 return;
1750 }
1751
1752 free_slot->valid = 1;
1753 free_slot->key_idx = sub->net_idx;
1754 free_slot->app_key = 0;
1755 free_slot->clear = 0;
1756 schedule_store(BT_MESH_KEYS_PENDING);
1757 }
1758
bt_mesh_store_app_key(struct bt_mesh_app_key * key)1759 void bt_mesh_store_app_key(struct bt_mesh_app_key *key)
1760 {
1761 struct key_update *update, *free_slot;
1762 BT_DBG("AppKeyIndex 0x%03x", key->app_idx);
1763 update = key_update_find(true, key->app_idx, &free_slot);
1764 if (update) {
1765 update->clear = 0;
1766 schedule_store(BT_MESH_KEYS_PENDING);
1767 return;
1768 }
1769
1770 if (!free_slot) {
1771 store_app_key(key);
1772 return;
1773 }
1774
1775 free_slot->valid = 1;
1776 free_slot->key_idx = key->app_idx;
1777 free_slot->app_key = 1;
1778 free_slot->clear = 0;
1779 schedule_store(BT_MESH_KEYS_PENDING);
1780 }
1781
bt_mesh_store_hb_pub(void)1782 void bt_mesh_store_hb_pub(void)
1783 {
1784 schedule_store(BT_MESH_HB_PUB_PENDING);
1785 }
1786
bt_mesh_store_cfg(void)1787 void bt_mesh_store_cfg(void)
1788 {
1789 schedule_store(BT_MESH_CFG_PENDING);
1790 }
1791
bt_mesh_clear_net(void)1792 void bt_mesh_clear_net(void)
1793 {
1794 schedule_store(BT_MESH_NET_PENDING);
1795 schedule_store(BT_MESH_IV_PENDING);
1796 schedule_store(BT_MESH_CFG_PENDING);
1797 }
1798
bt_mesh_clear_subnet(struct bt_mesh_subnet * sub)1799 void bt_mesh_clear_subnet(struct bt_mesh_subnet *sub)
1800 {
1801 struct key_update *update, *free_slot;
1802 BT_DBG("NetKeyIndex 0x%03x", sub->net_idx);
1803 update = key_update_find(false, sub->net_idx, &free_slot);
1804 if (update) {
1805 update->clear = 1;
1806 schedule_store(BT_MESH_KEYS_PENDING);
1807 return;
1808 }
1809
1810 if (!free_slot) {
1811 clear_net_key(sub->net_idx);
1812 return;
1813 }
1814
1815 free_slot->valid = 1;
1816 free_slot->key_idx = sub->net_idx;
1817 free_slot->app_key = 0;
1818 free_slot->clear = 1;
1819 schedule_store(BT_MESH_KEYS_PENDING);
1820 }
1821
bt_mesh_clear_app_key(struct bt_mesh_app_key * key)1822 void bt_mesh_clear_app_key(struct bt_mesh_app_key *key)
1823 {
1824 struct key_update *update, *free_slot;
1825 BT_DBG("AppKeyIndex 0x%03x", key->app_idx);
1826 update = key_update_find(true, key->app_idx, &free_slot);
1827 if (update) {
1828 update->clear = 1;
1829 schedule_store(BT_MESH_KEYS_PENDING);
1830 return;
1831 }
1832
1833 if (!free_slot) {
1834 clear_app_key(key->app_idx);
1835 return;
1836 }
1837
1838 free_slot->valid = 1;
1839 free_slot->key_idx = key->app_idx;
1840 free_slot->app_key = 1;
1841 free_slot->clear = 1;
1842 schedule_store(BT_MESH_KEYS_PENDING);
1843 }
1844
bt_mesh_clear_rpl(void)1845 void bt_mesh_clear_rpl(void)
1846 {
1847 schedule_store(BT_MESH_RPL_PENDING);
1848 }
1849
bt_mesh_store_mod_bind(struct bt_mesh_model * mod)1850 void bt_mesh_store_mod_bind(struct bt_mesh_model *mod)
1851 {
1852 mod->flags |= BT_MESH_MOD_BIND_PENDING;
1853 schedule_store(BT_MESH_MOD_PENDING);
1854 }
1855
bt_mesh_store_mod_sub(struct bt_mesh_model * mod)1856 void bt_mesh_store_mod_sub(struct bt_mesh_model *mod)
1857 {
1858 mod->flags |= BT_MESH_MOD_SUB_PENDING;
1859 schedule_store(BT_MESH_MOD_PENDING);
1860 }
1861
bt_mesh_store_mod_pub(struct bt_mesh_model * mod)1862 void bt_mesh_store_mod_pub(struct bt_mesh_model *mod)
1863 {
1864 mod->flags |= BT_MESH_MOD_PUB_PENDING;
1865 schedule_store(BT_MESH_MOD_PENDING);
1866 }
1867
bt_mesh_store_label(void)1868 void bt_mesh_store_label(void)
1869 {
1870 schedule_store(BT_MESH_VA_PENDING);
1871 }
1872
bt_mesh_store_node(struct bt_mesh_node * node)1873 void bt_mesh_store_node(struct bt_mesh_node *node)
1874 {
1875 struct node_update *update, *free_slot;
1876 BT_DBG("Node 0x%04x", node->addr);
1877 update = node_update_find(node->addr, &free_slot);
1878 if (update) {
1879 update->clear = false;
1880 schedule_store(BT_MESH_NODES_PENDING);
1881 return;
1882 }
1883
1884 if (!free_slot) {
1885 store_node(node);
1886 return;
1887 }
1888
1889 free_slot->addr = node->addr;
1890 schedule_store(BT_MESH_NODES_PENDING);
1891 }
1892
bt_mesh_clear_node(struct bt_mesh_node * node)1893 void bt_mesh_clear_node(struct bt_mesh_node *node)
1894 {
1895 struct node_update *update, *free_slot;
1896 BT_DBG("Node 0x%04x", node->addr);
1897 update = node_update_find(node->addr, &free_slot);
1898 if (update) {
1899 update->clear = true;
1900 schedule_store(BT_MESH_NODES_PENDING);
1901 return;
1902 }
1903
1904 if (!free_slot) {
1905 clear_node(node->addr);
1906 return;
1907 }
1908
1909 free_slot->addr = node->addr;
1910 schedule_store(BT_MESH_NODES_PENDING);
1911 }
1912
bt_mesh_model_data_store(struct bt_mesh_model * mod,bool vnd,const void * data,size_t data_len)1913 int bt_mesh_model_data_store(struct bt_mesh_model *mod, bool vnd,
1914 const void *data, size_t data_len)
1915 {
1916 char path[20];
1917 char buf[BT_SETTINGS_SIZE(sizeof(struct mod_pub_val))];
1918 char *val;
1919 int err;
1920 encode_mod_path(mod, vnd, "data", path, sizeof(path));
1921
1922 if (data_len) {
1923 mod->flags |= BT_MESH_MOD_DATA_PRESENT;
1924 val = settings_str_from_bytes(data, data_len,
1925 buf, sizeof(buf));
1926 if (!val) {
1927 BT_ERR("Unable to encode model publication as value");
1928 return -EINVAL;
1929 }
1930
1931 err = settings_save_one(path, val);
1932 } else if (mod->flags & BT_MESH_MOD_DATA_PRESENT) {
1933 mod->flags &= ~BT_MESH_MOD_DATA_PRESENT;
1934 err = settings_save_one(path, NULL);
1935 } else {
1936 /* Nothing to delete */
1937 err = 0;
1938 }
1939
1940 if (err) {
1941 BT_ERR("Failed to store %s value", path);
1942 } else {
1943 BT_DBG("Stored %s value", path);
1944 }
1945
1946 return err;
1947 }
1948
1949 static struct conf_handler bt_mesh_settings_conf_handler = {
1950 .ch_name = "bt_mesh",
1951 .ch_get = NULL,
1952 .ch_set = mesh_set,
1953 .ch_commit = mesh_commit,
1954 .ch_export = NULL,
1955 };
1956
bt_mesh_settings_init(void)1957 void bt_mesh_settings_init(void)
1958 {
1959 int rc;
1960 rc = conf_register(&bt_mesh_settings_conf_handler);
1961 SYSINIT_PANIC_ASSERT_MSG(rc == 0,
1962 "Failed to register bt_mesh_settings conf");
1963 k_delayed_work_init(&pending_store, store_pending);
1964 }
1965
1966 #endif /* MYNEWT_VAL(BLE_MESH_SETTINGS) */