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