1 /*
2  * SPDX-FileCopyrightText: 2018 Intel Corporation
3  * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <errno.h>
9 #include <string.h>
10 
11 #include "mesh.h"
12 #include "crypto.h"
13 #include "transport.h"
14 #include "access.h"
15 #include "foundation.h"
16 #include "proxy_server.h"
17 #include "cfg_srv.h"
18 #include "mesh_common.h"
19 #include "settings_nvs.h"
20 #include "settings.h"
21 #include "settings_uid.h"
22 #include "provisioner_main.h"
23 #include "provisioner_prov.h"
24 
25 /* BLE Mesh NVS Key and corresponding data struct.
26  * Note: The length of nvs key must be <= 15.
27  *       "xxxx" (2 octet) means the rpl_src, net_idx, app_idx, model_key, etc.
28  *       Model model_key is a combination "elem_idx << 8 | model_idx".
29  * key: "mesh/net"    -> write/read to set/get NET data
30  * key: "mesh/iv"     -> write/read to set/get IV data
31  * key: "mesh/seq"    -> write/read to set/get SEQ data
32  * key: "mesh/hb_pub" -> write/read to set/get CFG HB_PUB data
33  * key: "mesh/cfg"    -> write/read to set/get CFG data
34  * key: "mesh/rpl"    -> write/read to set/get all RPL src.
35  *      key: "mesh/rpl/xxxx" -> write/read to set/get the "xxxx" RPL data
36  * key: "mesh/netkey" -> write/read to set/get all NetKey Indexes
37  *      key: "mesh/nk/xxxx" -> write/read to set/get the "xxxx" NetKey data
38  * key: "mesh/appkey" -> write/read to set/get all AppKey Indexes
39  *      key: "mesh/ak/xxxx" -> write/read to set/get the "xxxx" AppKey data
40  * key: "mesh/sig" -> write/read to set/get all SIG MODEL model_keys.
41  *      key: "mesh/s/xxxx/b"  -> write/read to set/get SIG MODEL Bind AppKey List
42  *      key: "mesh/s/xxxx/s" -> write/read to set/get SIG MODEL Subscription List
43  *      key: "mesh/s/xxxx/p" -> write/read to set/get SIG MODEL Publication
44  * key: "mesh/vnd" -> write/read to set/get all VENDOR MODEL model_keys.
45  *      key: "mesh/v/xxxx/b"  -> write/read to set/get VENDOR MODEL Bind AppKey List
46  *      key: "mesh/v/xxxx/s" -> write/read to set/get VENDOR MODEL Subscription List
47  *      key: "mesh/v/xxxx/p" -> write/read to set/get VENDOR MODEL Publication
48  * key: "mesh/vaddr" -> write/read to set/get all virtual addresses
49  *      key: "mesh/va/xxxx" -> write/read to set/get the "xxxx" virtual address
50  */
51 
52 #if CONFIG_BLE_MESH_SETTINGS
53 
54 /* Tracking of what storage changes are pending for App and Net Keys. We
55  * track this in a separate array here instead of within the respective
56  * bt_mesh_app_key and bt_mesh_subnet structs themselves, since once a key
57  * gets deleted its struct becomes invalid and may be reused for other keys.
58  */
59 static struct key_update {
60     uint16_t key_idx:12, /* AppKey or NetKey Index */
61              valid:1,    /* 1 if this entry is valid, 0 if not */
62              app_key:1,  /* 1 if this is an AppKey, 0 if a NetKey */
63              clear:1;    /* 1 if key needs clearing, 0 if storing */
64 } key_updates[CONFIG_BLE_MESH_APP_KEY_COUNT + CONFIG_BLE_MESH_SUBNET_COUNT];
65 
66 static struct k_delayed_work pending_store;
67 static void store_pending(struct k_work *work);
68 
69 /* Mesh network storage information */
70 struct net_val {
71     uint16_t primary_addr;
72     uint8_t  dev_key[16];
73 } __packed;
74 
75 /* Sequence number storage */
76 struct seq_val {
77     uint8_t val[3];
78 } __packed;
79 
80 /* Heartbeat Publication storage */
81 struct hb_pub_val {
82     uint16_t dst;
83     uint8_t  period;
84     uint8_t  ttl;
85     uint16_t feat;
86     uint16_t net_idx:12,
87              indefinite:1;
88 };
89 
90 /* Miscellaneous configuration server model states */
91 struct cfg_val {
92     uint8_t net_transmit;
93     uint8_t relay;
94     uint8_t relay_retransmit;
95     uint8_t beacon;
96     uint8_t gatt_proxy;
97     uint8_t frnd;
98     uint8_t default_ttl;
99 };
100 
101 /* IV Index & IV Update storage */
102 struct iv_val {
103     uint32_t iv_index;
104     uint8_t  iv_update:1,
105              iv_duration:7;
106 } __packed;
107 
108 /* Replay Protection List storage */
109 struct rpl_val {
110     uint32_t seq:24,
111              old_iv:1;
112 };
113 
114 /* NetKey storage information */
115 struct net_key_val {
116     uint8_t kr_flag:1,
117             kr_phase:7;
118     uint8_t val[2][16];
119 } __packed;
120 
121 /* AppKey storage information */
122 struct app_key_val {
123     uint16_t net_idx;
124     bool     updated;
125     uint8_t  val[2][16];
126 } __packed;
127 
128 struct mod_pub_val {
129     uint16_t addr;
130     uint16_t key;
131     uint8_t  ttl;
132     uint8_t  retransmit;
133     uint8_t  period;
134     uint8_t  period_div:4,
135              cred:1;
136 };
137 
138 /* Virtual Address information */
139 struct va_val {
140     uint16_t ref;
141     uint16_t addr;
142     uint8_t  uuid[16];
143 } __packed;
144 
145 /* We need this so we don't overwrite app-hardcoded values in case FCB
146  * contains a history of changes but then has a NULL at the end.
147  */
148 static struct {
149     bool valid;
150     struct cfg_val cfg;
151 } stored_cfg;
152 
153 struct prov_info {
154     uint16_t primary_addr;
155     uint16_t alloc_addr;
156 };
157 
158 struct node_info {
159     uint8_t  addr[6];
160     uint8_t  addr_type;
161     uint8_t  dev_uuid[16];
162     uint16_t oob_info;
163     uint16_t unicast_addr;
164     uint8_t  element_num;
165     uint16_t net_idx;
166     uint8_t  flags;
167     uint32_t iv_index;
168     uint8_t  dev_key[16];
169 } __packed;
170 
171 static bt_mesh_mutex_t settings_lock;
172 
bt_mesh_settings_mutex_new(void)173 static inline void bt_mesh_settings_mutex_new(void)
174 {
175     if (settings_lock.mutex == NULL) {
176         bt_mesh_mutex_create(&settings_lock);
177     }
178 }
179 
180 #if CONFIG_BLE_MESH_DEINIT
bt_mesh_settings_mutex_free(void)181 static inline void bt_mesh_settings_mutex_free(void)
182 {
183     bt_mesh_mutex_free(&settings_lock);
184 }
185 #endif /* CONFIG_BLE_MESH_DEINIT */
186 
bt_mesh_settings_lock(void)187 void bt_mesh_settings_lock(void)
188 {
189     bt_mesh_mutex_lock(&settings_lock);
190 }
191 
bt_mesh_settings_unlock(void)192 void bt_mesh_settings_unlock(void)
193 {
194     bt_mesh_mutex_unlock(&settings_lock);
195 }
196 
role_set(const char * name)197 static int role_set(const char *name)
198 {
199     bool exist = false;
200     int err = 0;
201 
202     BT_DBG("%s", __func__);
203 
204     err = bt_mesh_load_core_settings(name, (uint8_t *)bt_mesh.flags, sizeof(bt_mesh.flags), &exist);
205     if (err) {
206         BT_ERR("Failed to load mesh device role");
207         return err;
208     }
209 
210     if (exist == false) {
211 #if CONFIG_BLE_MESH_SETTINGS_BACKWARD_COMPATIBILITY
212         if (IS_ENABLED(CONFIG_BLE_MESH_NODE) &&
213             !IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) {
214             bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE);
215         }
216 #else
217         return 0;
218 #endif
219     }
220 
221     return 0;
222 }
223 
net_set(const char * name)224 static int net_set(const char *name)
225 {
226     struct net_val net = {0};
227     bool exist = false;
228     int err = 0;
229 
230     BT_DBG("%s", __func__);
231 
232     err = bt_mesh_load_core_settings(name, (uint8_t *)&net, sizeof(net), &exist);
233     if (err) {
234         BT_ERR("Failed to load node net info");
235         memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key));
236         bt_mesh_comp_unprovision();
237         return 0;
238     }
239 
240     if (exist == false) {
241         return 0;
242     }
243 
244     memcpy(bt_mesh.dev_key, net.dev_key, sizeof(bt_mesh.dev_key));
245     bt_mesh_comp_provision(net.primary_addr);
246 
247     BT_INFO("Restored Primary Address 0x%04x", net.primary_addr);
248     BT_INFO("Restored DevKey %s", bt_hex(bt_mesh.dev_key, 16));
249 
250     return 0;
251 }
252 
iv_set(const char * name)253 static int iv_set(const char *name)
254 {
255     struct iv_val iv = {0};
256     bool exist = false;
257     int err = 0;
258 
259     BT_DBG("%s", __func__);
260 
261     err = bt_mesh_load_core_settings(name, (uint8_t *)&iv, sizeof(iv), &exist);
262     if (err) {
263         BT_ERR("Failed to load iv_index");
264         bt_mesh.iv_index = 0U;
265         bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS);
266         return 0;
267     }
268 
269     if (exist == false) {
270         return 0;
271     }
272 
273     bt_mesh.iv_index = iv.iv_index;
274     bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, iv.iv_update);
275     bt_mesh.ivu_duration = iv.iv_duration;
276 
277     BT_INFO("Restored IV Index 0x%08x (IV Update Flag %u) duration %u hours",
278            iv.iv_index, iv.iv_update, iv.iv_duration);
279 
280     return 0;
281 }
282 
seq_set(const char * name)283 static int seq_set(const char *name)
284 {
285     struct seq_val seq = {0};
286     bool exist = false;
287     int err = 0;
288 
289     BT_DBG("%s", __func__);
290 
291     err = bt_mesh_load_core_settings(name, (uint8_t *)&seq, sizeof(seq), &exist);
292     if (err) {
293         BT_ERR("Failed to load sequence number");
294         bt_mesh.seq = 0U;
295         return 0;
296     }
297 
298     if (exist == false) {
299         return 0;
300     }
301 
302     bt_mesh.seq = sys_get_le24(seq.val);
303 
304 #if CONFIG_BLE_MESH_SEQ_STORE_RATE > 0
305     /* Make sure we have a large enough sequence number. We
306      * subtract 1 so that the first transmission causes a write
307      * to the settings storage.
308      */
309     bt_mesh.seq += (CONFIG_BLE_MESH_SEQ_STORE_RATE -
310                     (bt_mesh.seq % CONFIG_BLE_MESH_SEQ_STORE_RATE));
311     bt_mesh.seq--;
312 #endif
313 
314     BT_INFO("Restored Sequence Number 0x%06x", bt_mesh.seq);
315 
316     return 0;
317 }
318 
rpl_find(uint16_t src)319 static struct bt_mesh_rpl *rpl_find(uint16_t src)
320 {
321     int i;
322 
323     for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
324         if (bt_mesh.rpl[i].src == src) {
325             return &bt_mesh.rpl[i];
326         }
327     }
328 
329     return NULL;
330 }
331 
rpl_alloc(uint16_t src)332 static struct bt_mesh_rpl *rpl_alloc(uint16_t src)
333 {
334     int i;
335 
336     for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
337         if (bt_mesh.rpl[i].src == BLE_MESH_ADDR_UNASSIGNED) {
338             bt_mesh.rpl[i].src = src;
339             return &bt_mesh.rpl[i];
340         }
341     }
342 
343     return NULL;
344 }
345 
rpl_set(const char * name)346 static int rpl_set(const char *name)
347 {
348     struct net_buf_simple *buf = NULL;
349     struct bt_mesh_rpl *entry = NULL;
350     struct rpl_val rpl = {0};
351     char get[16] = {'\0'};
352     bool exist = false;
353     size_t length = 0U;
354     int err = 0;
355     int i;
356 
357     BT_DBG("%s", __func__);
358 
359     buf = bt_mesh_get_core_settings_item(name);
360     if (!buf) {
361         return 0;
362     }
363 
364     length = buf->len;
365 
366     for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
367         uint16_t src = net_buf_simple_pull_le16(buf);
368 
369         if (!BLE_MESH_ADDR_IS_UNICAST(src)) {
370             BT_ERR("Invalid source address 0x%04x", src);
371             continue;
372         }
373 
374         sprintf(get, "mesh/rpl/%04x", src);
375 
376         err = bt_mesh_load_core_settings(get, (uint8_t *)&rpl, sizeof(rpl), &exist);
377         if (err) {
378             BT_ERR("Failed to load RPL entry 0x%04x", src);
379             continue;
380         }
381 
382         if (exist == false) {
383             continue;
384         }
385 
386         entry = rpl_find(src);
387         if (!entry) {
388             entry = rpl_alloc(src);
389             if (!entry) {
390                 BT_ERR("No space for a new RPL 0x%04x", src);
391                 err = -ENOMEM;
392                 goto free;
393             }
394         }
395 
396         entry->src = src;
397         entry->seq = rpl.seq;
398         entry->old_iv = rpl.old_iv;
399 
400         BT_INFO("Restored RPL entry 0x%04x: seq 0x%06x, old_iv %u", src, rpl.seq, rpl.old_iv);
401     }
402 
403 free:
404     bt_mesh_free_buf(buf);
405     return err;
406 }
407 
subnet_alloc(uint16_t net_idx)408 static struct bt_mesh_subnet *subnet_alloc(uint16_t net_idx)
409 {
410     int i;
411 
412     for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
413         if (bt_mesh.sub[i].net_idx == BLE_MESH_KEY_UNUSED) {
414             bt_mesh.sub[i].net_idx = net_idx;
415             return &bt_mesh.sub[i];
416         }
417     }
418 
419     return NULL;
420 }
421 
net_key_set(const char * name)422 static int net_key_set(const char *name)
423 {
424     struct net_buf_simple *buf = NULL;
425     struct bt_mesh_subnet *sub = NULL;
426     struct net_key_val key = {0};
427     char get[16] = {'\0'};
428     bool exist = false;
429     size_t length = 0U;
430     int err = 0;
431     int i;
432 
433     BT_DBG("%s", __func__);
434 
435     buf = bt_mesh_get_core_settings_item(name);
436     if (!buf) {
437         return 0;
438     }
439 
440     length = buf->len;
441 
442     for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
443         uint16_t net_idx = net_buf_simple_pull_le16(buf);
444         sprintf(get, "mesh/nk/%04x", net_idx);
445 
446         err = bt_mesh_load_core_settings(get, (uint8_t *)&key, sizeof(key), &exist);
447         if (err) {
448             BT_ERR("Failed to load NetKey 0x%03x", net_idx);
449             continue;
450         }
451 
452         if (exist == false) {
453             continue;
454         }
455 
456         sub = bt_mesh_subnet_get(net_idx);
457         if (!sub) {
458             sub = subnet_alloc(net_idx);
459             if (!sub) {
460                 BT_ERR("No space for a new subnet 0x%03x", net_idx);
461                 err = -ENOMEM;
462                 goto free;
463             }
464         }
465 
466         sub->net_idx = net_idx;
467         sub->kr_flag = key.kr_flag;
468         sub->kr_phase = key.kr_phase;
469         memcpy(sub->keys[0].net, &key.val[0], 16);
470         memcpy(sub->keys[1].net, &key.val[1], 16);
471 
472         BT_INFO("Restored NetKeyIndex 0x%03x", sub->net_idx);
473         BT_INFO("Restored NetKey %s", bt_hex(sub->keys[0].net, 16));
474     }
475 
476 free:
477     bt_mesh_free_buf(buf);
478     return err;
479 }
480 
app_key_set(const char * name)481 static int app_key_set(const char *name)
482 {
483     struct bt_mesh_app_key *app = NULL;
484     struct bt_mesh_subnet *sub = NULL;
485     struct net_buf_simple *buf = NULL;
486     struct app_key_val key = {0};
487     char get[16] = {'\0'};
488     bool exist = false;
489     size_t length = 0U;
490     int err = 0;
491     int i;
492 
493     BT_DBG("%s", __func__);
494 
495     buf = bt_mesh_get_core_settings_item(name);
496     if (!buf) {
497         return 0;
498     }
499 
500     length = buf->len;
501 
502     for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
503         uint16_t app_idx = net_buf_simple_pull_le16(buf);
504         sprintf(get, "mesh/ak/%04x", app_idx);
505 
506         err = bt_mesh_load_core_settings(get, (uint8_t *)&key, sizeof(key), &exist);
507         if (err) {
508             BT_ERR("Failed to load AppKey 0x%03x", app_idx);
509             continue;
510         }
511 
512         if (exist == false) {
513             continue;
514         }
515 
516         sub = bt_mesh_subnet_get(key.net_idx);
517         if (!sub) {
518             BT_ERR("Failed to find subnet 0x%03x", key.net_idx);
519             continue;
520         }
521 
522         app = bt_mesh_app_key_find(app_idx);
523         if (!app) {
524             app = bt_mesh_app_key_alloc(app_idx);
525             if (!app) {
526                 BT_ERR("No space for a new appkey 0x%03x", app_idx);
527                 err = -ENOMEM;
528                 goto free;
529             }
530         }
531 
532         app->net_idx = key.net_idx;
533         app->app_idx = app_idx;
534         app->updated = key.updated;
535         memcpy(app->keys[0].val, key.val[0], 16);
536         memcpy(app->keys[1].val, key.val[1], 16);
537         bt_mesh_app_id(app->keys[0].val, &app->keys[0].id);
538         bt_mesh_app_id(app->keys[1].val, &app->keys[1].id);
539 
540         BT_INFO("Restored AppKeyIndex 0x%03x, NetKeyIndex 0x%03x",
541             app->app_idx, app->net_idx);
542         BT_INFO("Restored AppKey %s", bt_hex(app->keys[0].val, 16));
543     }
544 
545 free:
546     bt_mesh_free_buf(buf);
547     return err;
548 }
549 
hb_pub_set(const char * name)550 static int hb_pub_set(const char *name)
551 {
552     struct bt_mesh_hb_pub *hb_pub = bt_mesh_hb_pub_get();
553     struct hb_pub_val hb_val = {0};
554     bool exist = false;
555     int err = 0;
556 
557     BT_DBG("%s", __func__);
558 
559     if (!hb_pub) {
560         BT_ERR("Invalid heartbeat publication");
561         return -EINVAL;
562     }
563 
564     err = bt_mesh_load_core_settings(name, (uint8_t *)&hb_val, sizeof(hb_val), &exist);
565     if (err) {
566         BT_ERR("Failed to load heartbeat publication");
567         hb_pub->dst = BLE_MESH_ADDR_UNASSIGNED;
568         hb_pub->count = 0U;
569         hb_pub->ttl = 0U;
570         hb_pub->period = 0U;
571         hb_pub->feat = 0U;
572         return 0;
573     }
574 
575     if (exist == false) {
576         return 0;
577     }
578 
579     hb_pub->dst = hb_val.dst;
580     hb_pub->period = hb_val.period;
581     hb_pub->ttl = hb_val.ttl;
582     hb_pub->feat = hb_val.feat;
583     hb_pub->net_idx = hb_val.net_idx;
584     if (hb_val.indefinite) {
585         hb_pub->count = 0xffff;
586     } else {
587         hb_pub->count = 0U;
588     }
589 
590     BT_INFO("Restored Heartbeat Publication, dst 0x%04x, period %d, net_idx 0x%03x",
591             hb_pub->dst, hb_pub->period, hb_pub->net_idx);
592 
593     return 0;
594 }
595 
cfg_set(const char * name)596 static int cfg_set(const char *name)
597 {
598     struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get();
599     struct cfg_val val = {0};
600     bool exist = false;
601     int err = 0;
602 
603     BT_DBG("%s", __func__);
604 
605     if (!cfg) {
606         BT_ERR("Invalid configuration");
607         stored_cfg.valid = false;
608         return -EINVAL;
609     }
610 
611     err = bt_mesh_load_core_settings(name, (uint8_t *)&val, sizeof(val), &exist);
612     if (err) {
613         BT_ERR("Failed to load configuration state");
614         stored_cfg.valid = false;
615         return 0;
616     }
617 
618     if (exist == false) {
619         return 0;
620     }
621 
622     memcpy(&stored_cfg.cfg, &val, sizeof(val));
623     stored_cfg.valid = true;
624 
625     BT_INFO("Restored Configuration, ttl %d, transmit 0x%02x, retransmit 0x%02x",
626             val.default_ttl, val.net_transmit, val.relay_retransmit);
627 
628     return 0;
629 }
630 
model_set_bind(bool vnd,struct bt_mesh_model * model,uint16_t model_key)631 static int model_set_bind(bool vnd, struct bt_mesh_model *model, uint16_t model_key)
632 {
633     char name[16] = {'\0'};
634     bool exist = false;
635     int err = 0;
636     int i;
637 
638     /* Start with empty array regardless of cleared or set value */
639     for (i = 0; i < ARRAY_SIZE(model->keys); i++) {
640         model->keys[i] = BLE_MESH_KEY_UNUSED;
641     }
642 
643     sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key);
644     err = bt_mesh_load_core_settings(name, (uint8_t *)model->keys, sizeof(model->keys), &exist);
645     if (err) {
646         BT_ERR("Failed to load model bound keys");
647         return -EIO;
648     }
649 
650     if (exist == true) {
651         BT_INFO("Restored Model Bound AppKey, index %s", bt_hex(model->keys, sizeof(model->keys)));
652     }
653 
654     return 0;
655 }
656 
model_set_sub(bool vnd,struct bt_mesh_model * model,uint16_t model_key)657 static int model_set_sub(bool vnd, struct bt_mesh_model *model, uint16_t model_key)
658 {
659     char name[16] = {'\0'};
660     bool exist = false;
661     int err = 0;
662     int i;
663 
664     /* Start with empty array regardless of cleared or set value */
665     for (i = 0; i < ARRAY_SIZE(model->groups); i++) {
666         model->groups[i] = BLE_MESH_ADDR_UNASSIGNED;
667     }
668 
669     sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key);
670     err = bt_mesh_load_core_settings(name, (uint8_t *)model->groups, sizeof(model->groups), &exist);
671     if (err) {
672         BT_ERR("Failed to load model subscriptions");
673         return -EIO;
674     }
675 
676     if (exist == true) {
677         BT_INFO("Restored Model Subscription, address %s", bt_hex(model->groups, sizeof(model->groups)));
678     }
679 
680     return 0;
681 }
682 
model_set_pub(bool vnd,struct bt_mesh_model * model,uint16_t model_key)683 static int model_set_pub(bool vnd, struct bt_mesh_model *model, uint16_t model_key)
684 {
685     struct mod_pub_val pub = {0};
686     char name[16] = {'\0'};
687     bool exist = false;
688     int err = 0;
689 
690     if (!model->pub) {
691         BT_INFO("Not support publication, model_id 0x%04x, cid 0x%04x",
692             vnd ? model->vnd.id : model->id, vnd ? model->vnd.company : 0xFFFF);
693         return 0;
694     }
695 
696     sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key);
697     err = bt_mesh_load_core_settings(name, (uint8_t *)&pub, sizeof(pub), &exist);
698     if (err) {
699         BT_ERR("Failed to load model publication");
700         model->pub->addr = BLE_MESH_ADDR_UNASSIGNED;
701         model->pub->key = 0U;
702         model->pub->cred = 0U;
703         model->pub->ttl = 0U;
704         model->pub->period = 0U;
705         model->pub->retransmit = 0U;
706         model->pub->count = 0U;
707         return -EIO;
708     }
709 
710     if (exist == false) {
711         return 0;
712     }
713 
714     model->pub->addr = pub.addr;
715     model->pub->key = pub.key;
716     model->pub->cred = pub.cred;
717     model->pub->ttl = pub.ttl;
718     model->pub->period = pub.period;
719     model->pub->retransmit = pub.retransmit;
720     model->pub->count = 0U;
721 
722     BT_INFO("Restored Model Publication, address 0x%04x, app_idx 0x%03x", pub.addr, pub.key);
723 
724     return 0;
725 }
726 
model_set(bool vnd,const char * name)727 static int model_set(bool vnd, const char *name)
728 {
729     struct bt_mesh_model *model = NULL;
730     struct net_buf_simple *buf = NULL;
731     uint8_t elem_idx = 0U, model_idx = 0U;
732     size_t length = 0U;
733     int i;
734 
735     BT_DBG("%s", __func__);
736 
737     buf = bt_mesh_get_core_settings_item(name);
738     if (!buf) {
739         return 0;
740     }
741 
742     length = buf->len;
743 
744     for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
745         uint16_t model_key = net_buf_simple_pull_le16(buf);
746 
747         elem_idx = BLE_MESH_GET_ELEM_IDX(model_key);
748         model_idx = BLE_MESH_GET_MODEL_IDX(model_key);
749 
750         model = bt_mesh_model_get(vnd, elem_idx, model_idx);
751         if (!model) {
752             BT_ERR("%s model not found, elem_idx %u, model_idx %u",
753                 vnd ? "vnd" : "sig", elem_idx, model_idx);
754             continue;
755         }
756 
757         model_set_bind(vnd, model, model_key);
758         model_set_sub(vnd, model, model_key);
759         model_set_pub(vnd, model, model_key);
760     }
761 
762     bt_mesh_free_buf(buf);
763     return 0;
764 }
765 
sig_mod_set(const char * name)766 static int sig_mod_set(const char *name)
767 {
768     return model_set(false, name);
769 }
770 
vnd_mod_set(const char * name)771 static int vnd_mod_set(const char *name)
772 {
773     return model_set(true, name);
774 }
775 
776 #if CONFIG_BLE_MESH_LABEL_COUNT > 0
va_set(const char * name)777 static int va_set(const char *name)
778 {
779     struct net_buf_simple *buf = NULL;
780     struct va_val va = {0};
781     char get[16] = {'\0'};
782     struct label *lab = NULL;
783     size_t length = 0U;
784     bool exist = false;
785     int err = 0;
786     int i;
787 
788     BT_DBG("%s", __func__);
789 
790     buf = bt_mesh_get_core_settings_item(name);
791     if (!buf) {
792         return 0;
793     }
794 
795     length = buf->len;
796 
797     for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
798         uint16_t index = net_buf_simple_pull_le16(buf);
799         sprintf(get, "mesh/va/%04x", index);
800 
801         err = bt_mesh_load_core_settings(get, (uint8_t *)&va, sizeof(va), &exist);
802         if (err) {
803             BT_ERR("Failed to load virtual address 0x%04x", index);
804             continue;
805         }
806 
807         if (exist == false) {
808             continue;
809         }
810 
811         if (va.ref == 0) {
812             BT_DBG("Ignore virtual address %s with ref = 0", get);
813             continue;
814         }
815 
816         lab = get_label(index);
817         if (lab == NULL) {
818             BT_WARN("Out of labels buffers");
819             err = -ENOBUFS;
820             goto free;
821         }
822 
823         memcpy(lab->uuid, va.uuid, 16);
824         lab->addr = va.addr;
825         lab->ref = va.ref;
826 
827         BT_INFO("Restored Virtual Address 0x%04x, ref 0x%04x", index, lab->ref);
828     }
829 
830 free:
831     bt_mesh_free_buf(buf);
832     return err;
833 }
834 #endif
835 
836 #if CONFIG_BLE_MESH_PROVISIONER
p_prov_set(const char * name)837 static int p_prov_set(const char *name)
838 {
839     struct prov_info val = {0};
840     bool exist = false;
841     int err = 0;
842 
843     BT_DBG("%s", __func__);
844 
845     err = bt_mesh_load_core_settings(name, (uint8_t *)&val, sizeof(val), &exist);
846     if (err) {
847         BT_ERR("Failed to load next address allocation");
848         return 0;
849     }
850 
851     if (exist == false) {
852         return 0;
853     }
854 
855     bt_mesh_provisioner_restore_prov_info(val.primary_addr, val.alloc_addr);
856 
857     BT_INFO("Restored Primary Address 0x%04x, next address alloc 0x%04x",
858         val.primary_addr, val.alloc_addr);
859 
860     return 0;
861 }
862 
p_net_idx_set(const char * name)863 static int p_net_idx_set(const char *name)
864 {
865     uint16_t net_idx = 0U;
866     bool exist = false;
867     int err = 0;
868 
869     BT_DBG("%s", __func__);
870 
871     err = bt_mesh_load_core_settings(name, (uint8_t *)&net_idx, sizeof(net_idx), &exist);
872     if (err) {
873         BT_ERR("Failed to load next NetKeyIndex alloc");
874         return 0;
875     }
876 
877     if (exist == false) {
878         return 0;
879     }
880 
881     bt_mesh.p_net_idx_next = net_idx;
882 
883     BT_INFO("Restored next NetKeyIndex alloc 0x%03x", bt_mesh.p_net_idx_next);
884 
885     return 0;
886 }
887 
p_app_idx_set(const char * name)888 static int p_app_idx_set(const char *name)
889 {
890     uint16_t app_idx = 0U;
891     bool exist = false;
892     int err = 0;
893 
894     BT_DBG("%s", __func__);
895 
896     err = bt_mesh_load_core_settings(name, (uint8_t *)&app_idx, sizeof(app_idx), &exist);
897     if (err) {
898         BT_ERR("Failed to load next AppKeyIndex alloc");
899         return 0;
900     }
901 
902     if (exist == false) {
903         return 0;
904     }
905 
906     bt_mesh.p_app_idx_next = app_idx;
907 
908     BT_INFO("Restored next AppKeyIndex alloc 0x%03x", bt_mesh.p_app_idx_next);
909 
910     return 0;
911 }
912 
p_subnet_alloc(void)913 static struct bt_mesh_subnet *p_subnet_alloc(void)
914 {
915     int i;
916 
917     for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
918         if (bt_mesh.p_sub[i] == NULL) {
919             bt_mesh.p_sub[i] = bt_mesh_calloc(sizeof(struct bt_mesh_subnet));
920             if (!bt_mesh.p_sub[i]) {
921                 BT_ERR("%s, Out of memory", __func__);
922                 return NULL;
923             }
924 
925             return bt_mesh.p_sub[i];
926         }
927     }
928 
929     return NULL;
930 }
931 
p_appkey_alloc(void)932 static struct bt_mesh_app_key *p_appkey_alloc(void)
933 {
934     int i;
935 
936     for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) {
937         if (bt_mesh.p_app_keys[i] == NULL) {
938             bt_mesh.p_app_keys[i] = bt_mesh_calloc(sizeof(struct bt_mesh_app_key));
939             if (!bt_mesh.p_app_keys[i]) {
940                 BT_ERR("%s, Out of memory", __func__);
941                 return NULL;
942             }
943 
944             return bt_mesh.p_app_keys[i];
945         }
946     }
947 
948     return NULL;
949 }
950 
p_net_key_set(const char * name)951 static int p_net_key_set(const char *name)
952 {
953     struct net_buf_simple *buf = NULL;
954     struct bt_mesh_subnet *sub = NULL;
955     struct net_key_val key = {0};
956     char get[16] = {'\0'};
957     size_t length = 0U;
958     bool exist = false;
959     int err = 0;
960     int i;
961 
962     BT_DBG("%s", __func__);
963 
964     buf = bt_mesh_get_core_settings_item(name);
965     if (!buf) {
966         return 0;
967     }
968 
969     length = buf->len;
970 
971     for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
972         uint16_t net_idx = net_buf_simple_pull_le16(buf);
973         sprintf(get, "mesh/pnk/%04x", net_idx);
974 
975         err = bt_mesh_load_core_settings(get, (uint8_t *)&key, sizeof(key), &exist);
976         if (err) {
977             BT_ERR("Failed to load NetKey 0x%03x", net_idx);
978             continue;
979         }
980 
981         if (exist == false) {
982             continue;
983         }
984 
985         sub = bt_mesh_provisioner_subnet_get(net_idx);
986         if (!sub) {
987             sub = p_subnet_alloc();
988             if (!sub) {
989                 BT_ERR("No space for a new subnet 0x%03x", net_idx);
990                 err = -ENOMEM;
991                 goto free;
992             }
993         }
994 
995         sub->net_idx = net_idx;
996         sub->kr_flag = key.kr_flag;
997         sub->kr_phase = key.kr_phase;
998         memcpy(sub->keys[0].net, &key.val[0], 16);
999         memcpy(sub->keys[1].net, &key.val[1], 16);
1000 
1001         BT_INFO("Restored NetKeyIndex 0x%03x", sub->net_idx);
1002         BT_INFO("Restored NetKey %s", bt_hex(sub->keys[0].net, 16));
1003     }
1004 
1005 free:
1006     bt_mesh_free_buf(buf);
1007     return err;
1008 }
1009 
p_app_key_set(const char * name)1010 static int p_app_key_set(const char *name)
1011 {
1012     struct bt_mesh_app_key *app = NULL;
1013     struct bt_mesh_subnet *sub = NULL;
1014     struct net_buf_simple *buf = NULL;
1015     struct app_key_val key = {0};
1016     char get[16] = {'\0'};
1017     size_t length = 0U;
1018     bool exist = false;
1019     int err = 0;
1020     int i;
1021 
1022     BT_DBG("%s", __func__);
1023 
1024     buf = bt_mesh_get_core_settings_item(name);
1025     if (!buf) {
1026         return 0;
1027     }
1028 
1029     length = buf->len;
1030 
1031     for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
1032         uint16_t app_idx = net_buf_simple_pull_le16(buf);
1033         sprintf(get, "mesh/pak/%04x", app_idx);
1034 
1035         err = bt_mesh_load_core_settings(get, (uint8_t *)&key, sizeof(key), &exist);
1036         if (err) {
1037             BT_ERR("Failed to load AppKey 0x%03x", app_idx);
1038             continue;
1039         }
1040 
1041         if (exist == false) {
1042             continue;
1043         }
1044 
1045         sub = bt_mesh_provisioner_subnet_get(key.net_idx);
1046         if (!sub) {
1047             BT_ERR("Failed to find subnet 0x%03x", key.net_idx);
1048             continue;
1049         }
1050 
1051         app = bt_mesh_provisioner_app_key_find(app_idx);
1052         if (!app) {
1053             app = p_appkey_alloc();
1054             if (!app) {
1055                 BT_ERR("No space for a new appkey 0x%03x", app_idx);
1056                 err = -ENOMEM;
1057                 goto free;
1058             }
1059         }
1060 
1061         app->net_idx = key.net_idx;
1062         app->app_idx = app_idx;
1063         app->updated = key.updated;
1064         memcpy(app->keys[0].val, key.val[0], 16);
1065         memcpy(app->keys[1].val, key.val[1], 16);
1066         bt_mesh_app_id(app->keys[0].val, &app->keys[0].id);
1067         bt_mesh_app_id(app->keys[1].val, &app->keys[1].id);
1068 
1069         BT_INFO("Restored AppKeyIndex 0x%03x, NetKeyIndex 0x%03x",
1070             app->app_idx, app->net_idx);
1071         BT_INFO("Restored AppKey %s", bt_hex(app->keys[0].val, 16));
1072     }
1073 
1074 free:
1075     bt_mesh_free_buf(buf);
1076     return err;
1077 }
1078 
node_info_set(uint16_t addr,bool * exist)1079 static int node_info_set(uint16_t addr, bool *exist)
1080 {
1081     struct bt_mesh_node node = {0};
1082     struct node_info info = {0};
1083     char get[16] = {'\0'};
1084     int err = 0;
1085 
1086     sprintf(get, "mesh/pn/%04x/i", addr);
1087     err = bt_mesh_load_core_settings(get, (uint8_t *)&info, sizeof(info), exist);
1088     if (err) {
1089         BT_ERR("Failed to load node 0x%04x info", addr);
1090         return -EIO;
1091     }
1092 
1093     if (*exist == false) {
1094         return 0;
1095     }
1096 
1097     memcpy(node.addr, info.addr, BLE_MESH_ADDR_LEN);
1098     node.addr_type = info.addr_type;
1099     memcpy(node.dev_uuid, info.dev_uuid, 16);
1100     node.oob_info = info.oob_info;
1101     node.unicast_addr = info.unicast_addr;
1102     node.element_num = info.element_num;
1103     node.net_idx = info.net_idx;
1104     node.flags = info.flags;
1105     node.iv_index = info.iv_index;
1106     memcpy(node.dev_key, info.dev_key, 16);
1107 
1108     err = bt_mesh_provisioner_restore_node_info(&node);
1109     if (err) {
1110         BT_ERR("Failed to restore node 0x%04x info", addr);
1111         return -EIO;
1112     }
1113 
1114     BT_INFO("Restored Node 0x%04x, UUID %s", addr, bt_hex(node.dev_uuid, 16));
1115 
1116     return 0;
1117 }
1118 
node_name_set(uint16_t addr)1119 static int node_name_set(uint16_t addr)
1120 {
1121     char name[BLE_MESH_NODE_NAME_SIZE + 1] = {0};
1122     char get[16] = {'\0'};
1123     bool exist = false;
1124     int err = 0;
1125 
1126     sprintf(get, "mesh/pn/%04x/n", addr);
1127     err = bt_mesh_load_core_settings(get, (uint8_t *)name, BLE_MESH_NODE_NAME_SIZE, &exist);
1128     if (err) {
1129         BT_ERR("Failed to load node 0x%04x name", addr);
1130         return -EIO;
1131     }
1132 
1133     if (exist == false) {
1134         return 0;
1135     }
1136 
1137     err = bt_mesh_provisioner_restore_node_name(addr, name);
1138     if (err) {
1139         BT_ERR("Failed to restore node 0x%04x name", addr);
1140         return -EIO;
1141     }
1142 
1143     BT_INFO("Restored Node 0x%04x, Name %s", addr, name);
1144 
1145     return 0;
1146 }
1147 
node_comp_data_set(uint16_t addr)1148 static int node_comp_data_set(uint16_t addr)
1149 {
1150     struct net_buf_simple *buf = NULL;
1151     char get[16] = {'\0'};
1152     int err = 0;
1153 
1154     sprintf(get, "mesh/pn/%04x/c", addr);
1155     buf = bt_mesh_get_core_settings_item(get);
1156     if (!buf) {
1157         return 0;
1158     }
1159 
1160     err = bt_mesh_provisioner_restore_node_comp_data(addr, buf->data, buf->len);
1161     if (err) {
1162         BT_ERR("Failed to restore node 0x%04x comp data", addr);
1163     } else {
1164         BT_INFO("Restored Node 0x%04x, Composition Data %s", addr, bt_hex(buf->data, buf->len));
1165     }
1166 
1167     bt_mesh_free_buf(buf);
1168     return err;
1169 }
1170 
p_node_set(const char * name)1171 static int p_node_set(const char *name)
1172 {
1173     struct net_buf_simple *buf = NULL;
1174     bool exist = false;
1175     size_t length = 0U;
1176     int i;
1177 
1178     buf = bt_mesh_get_core_settings_item(name);
1179     if (!buf) {
1180         return 0;
1181     }
1182 
1183     length = buf->len;
1184 
1185     for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
1186         uint16_t addr = net_buf_simple_pull_le16(buf);
1187         if (!BLE_MESH_ADDR_IS_UNICAST(addr)) {
1188             BT_ERR("Invalid unicast address 0x%04x", addr);
1189             continue;
1190         }
1191 
1192         if (node_info_set(addr, &exist)) {
1193             continue;
1194         }
1195 
1196         if (exist == false) {
1197             continue;
1198         }
1199 
1200         if (node_name_set(addr)) {
1201             continue;
1202         }
1203 
1204         if (node_comp_data_set(addr)) {
1205             continue;
1206         }
1207     }
1208 
1209     bt_mesh_free_buf(buf);
1210     return 0;
1211 }
1212 #endif /* CONFIG_BLE_MESH_PROVISIONER */
1213 
1214 const struct bt_mesh_setting {
1215     const char *name;
1216     int (*func)(const char *name);
1217 } settings[] = {
1218     { "mesh/role",     role_set      }, /* For Node & Provisioner */
1219     { "mesh/net",      net_set       }, /* For Node */
1220     { "mesh/iv",       iv_set        }, /* For Node & Provisioner */
1221     { "mesh/seq",      seq_set       }, /* For Node & Provisioner */
1222     { "mesh/rpl",      rpl_set       }, /* For Node & Provisioner */
1223     { "mesh/netkey",   net_key_set   }, /* For Node */
1224     { "mesh/appkey",   app_key_set   }, /* For Node */
1225     { "mesh/hb_pub",   hb_pub_set    }, /* For Node */
1226     { "mesh/cfg",      cfg_set       }, /* For Node */
1227     { "mesh/sig",      sig_mod_set   }, /* For Node & Provisioner */
1228     { "mesh/vnd",      vnd_mod_set   }, /* For Node & Provisioner */
1229 #if CONFIG_BLE_MESH_LABEL_COUNT > 0
1230     { "mesh/vaddr",    va_set        }, /* For Node */
1231 #endif
1232 #if CONFIG_BLE_MESH_PROVISIONER
1233     { "mesh/p_prov",   p_prov_set    }, /* For Provisioner */
1234     { "mesh/p_netidx", p_net_idx_set }, /* For Provisioner */
1235     { "mesh/p_appidx", p_app_idx_set }, /* For Provisioner */
1236     { "mesh/p_netkey", p_net_key_set }, /* For Provisioner */
1237     { "mesh/p_appkey", p_app_key_set }, /* For Provisioner */
1238     { "mesh/p_node",   p_node_set    }, /* For Provisioner */
1239 #endif
1240 };
1241 
1242 /**
1243  * For Provisioner, the load operation needs the following actions:
1244  * role_set:    Need, restore the device role
1245  * net_set:     Not needed
1246  * iv_set:      Need, restore the last IV Index status
1247  * seq_set:     Need, restore the previous sequence number
1248  * rpl_set:     Need, restore the previous Replay Protection List
1249  * net_key_set: Not needed
1250  * app_key_set: Not needed
1251  * hb_pub_set:  Not needed currently
1252  * cfg_set:     Not needed currently
1253  * sig_mod_set: Need, restore SIG models related info (app, sub, pub)
1254  * vnd_mod_set: Need, restore vendor models related info (app, sub, pub)
1255  * va_set:      Not needed currently
1256  */
settings_core_load(void)1257 int settings_core_load(void)
1258 {
1259     int i;
1260 
1261     BT_DBG("%s", __func__);
1262 
1263     for (i = 0; i < ARRAY_SIZE(settings); i++) {
1264         if ((!strcmp(settings[i].name, "mesh/net") ||
1265             !strcmp(settings[i].name, "mesh/netkey") ||
1266             !strcmp(settings[i].name, "mesh/appkey") ||
1267             !strcmp(settings[i].name, "mesh/hb_pub") ||
1268             !strcmp(settings[i].name, "mesh/cfg") ||
1269             !strcmp(settings[i].name, "mesh/vaddr")) &&
1270             (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioner())) {
1271             BT_DBG("Not restoring %s for Provisioner", settings[i].name);
1272             continue;
1273         }
1274 
1275         if ((!strcmp(settings[i].name, "mesh/p_prov") ||
1276             !strcmp(settings[i].name, "mesh/p_netidx") ||
1277             !strcmp(settings[i].name, "mesh/p_appidx") ||
1278             !strcmp(settings[i].name, "mesh/p_netkey") ||
1279             !strcmp(settings[i].name, "mesh/p_appkey") ||
1280             !strcmp(settings[i].name, "mesh/p_node")) &&
1281             (!IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) || bt_mesh_is_node())) {
1282             BT_DBG("Not restoring %s for Node", settings[i].name);
1283             continue;
1284         }
1285 
1286         settings[i].func(settings[i].name);
1287 
1288         if (!strcmp(settings[i].name, "mesh/role")) {
1289             uint8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK;
1290             switch (role) {
1291             case BLE_MESH_SETTINGS_ROLE_NONE:
1292                 BT_INFO("Mesh device just starts up, no restore");
1293                 return 0;
1294             case BLE_MESH_SETTINGS_ROLE_NODE:
1295                 BT_INFO("Restored mesh device role: Node");
1296                 break;
1297             case BLE_MESH_SETTINGS_ROLE_PROV:
1298                 BT_INFO("Restored mesh device role: Provisioner");
1299                 break;
1300             default:
1301                 BT_ERR("Restored mesh device role: Unknown");
1302                 return 0;
1303             }
1304         }
1305     }
1306 
1307     return 0;
1308 }
1309 
subnet_init(struct bt_mesh_subnet * sub)1310 static int subnet_init(struct bt_mesh_subnet *sub)
1311 {
1312     int err = 0;
1313 
1314     err = bt_mesh_net_keys_create(&sub->keys[0], sub->keys[0].net);
1315     if (err) {
1316         BT_ERR("Unable to generate keys for subnet");
1317         return -EIO;
1318     }
1319 
1320     if (sub->kr_phase != BLE_MESH_KR_NORMAL) {
1321         err = bt_mesh_net_keys_create(&sub->keys[1], sub->keys[1].net);
1322         if (err) {
1323             BT_ERR("Unable to generate keys for subnet");
1324             (void)memset(&sub->keys[0], 0, sizeof(sub->keys[0]));
1325             return -EIO;
1326         }
1327     }
1328 
1329     if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
1330         sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED;
1331     } else {
1332         sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED;
1333     }
1334 
1335     /* Make sure we have valid beacon data to be sent */
1336     bt_mesh_net_beacon_update(sub);
1337 
1338     return 0;
1339 }
1340 
commit_model(struct bt_mesh_model * model,struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)1341 static void commit_model(struct bt_mesh_model *model, struct bt_mesh_elem *elem,
1342                          bool vnd, bool primary, void *user_data)
1343 {
1344     if (model->pub && model->pub->update &&
1345             model->pub->addr != BLE_MESH_ADDR_UNASSIGNED) {
1346         int32_t ms = bt_mesh_model_pub_period_get(model);
1347         if (ms) {
1348             BT_DBG("Starting publish timer (period %u ms)", ms);
1349             k_delayed_work_submit(&model->pub->timer, ms);
1350         }
1351     }
1352 }
1353 
settings_core_commit(void)1354 int settings_core_commit(void)
1355 {
1356     struct bt_mesh_subnet *sub = NULL;
1357     int err = 0;
1358     int i;
1359 
1360 #if defined(CONFIG_BLE_MESH_NODE)
1361     if (bt_mesh_is_node()) {
1362         if (bt_mesh.sub[0].net_idx == BLE_MESH_KEY_UNUSED) {
1363             /* Nothing to do since we're not yet provisioned */
1364             return 0;
1365         }
1366 
1367         BT_INFO("Settings commit, sub[0].net_idx 0x%03x", bt_mesh.sub[0].net_idx);
1368 
1369         if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
1370             bt_mesh_proxy_server_prov_disable(true);
1371         }
1372 
1373         for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
1374             sub = &bt_mesh.sub[i];
1375 
1376             if (sub->net_idx == BLE_MESH_KEY_UNUSED) {
1377                 continue;
1378             }
1379 
1380             err = subnet_init(sub);
1381             if (err) {
1382                 BT_ERR("Failed to init subnet 0x%03x", sub->net_idx);
1383             }
1384         }
1385     }
1386 #endif /* CONFIG_BLE_MESH_NODE */
1387 
1388 #if defined(CONFIG_BLE_MESH_PROVISIONER)
1389     if (bt_mesh_is_provisioner()) {
1390         if (bt_mesh.p_sub[0] == NULL ||
1391             bt_mesh.p_sub[0]->net_idx == BLE_MESH_KEY_UNUSED) {
1392             return 0;
1393         }
1394 
1395         BT_INFO("Settings commit, p_sub[0]->net_idx 0x%03x", bt_mesh.p_sub[0]->net_idx);
1396 
1397         for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
1398             sub = bt_mesh.p_sub[i];
1399 
1400             if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) {
1401                 continue;
1402             }
1403 
1404             err = subnet_init(sub);
1405             if (err) {
1406                 BT_ERR("Failed to init subnet 0x%03x", sub->net_idx);
1407             }
1408         }
1409     }
1410 #endif /* CONFIG_BLE_MESH_PROVISIONER */
1411 
1412     if (bt_mesh_is_node() || bt_mesh_is_provisioner()) {
1413         if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) {
1414             k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT);
1415         }
1416 
1417         bt_mesh_model_foreach(commit_model, NULL);
1418     }
1419 
1420 #if defined(CONFIG_BLE_MESH_NODE)
1421     if (bt_mesh_is_node()) {
1422         struct bt_mesh_hb_pub *hb_pub = NULL;
1423         struct bt_mesh_cfg_srv *cfg = NULL;
1424 
1425         hb_pub = bt_mesh_hb_pub_get();
1426         if (hb_pub && hb_pub->dst != BLE_MESH_ADDR_UNASSIGNED &&
1427                 hb_pub->count && hb_pub->period) {
1428             BT_DBG("Starting heartbeat publication");
1429             k_work_submit(&hb_pub->timer.work);
1430         }
1431 
1432         cfg = bt_mesh_cfg_get();
1433         if (cfg && stored_cfg.valid) {
1434             cfg->net_transmit = stored_cfg.cfg.net_transmit;
1435             cfg->relay = stored_cfg.cfg.relay;
1436             cfg->relay_retransmit = stored_cfg.cfg.relay_retransmit;
1437             cfg->beacon = stored_cfg.cfg.beacon;
1438             cfg->gatt_proxy = stored_cfg.cfg.gatt_proxy;
1439             cfg->frnd = stored_cfg.cfg.frnd;
1440             cfg->default_ttl = stored_cfg.cfg.default_ttl;
1441         }
1442 
1443         bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID);
1444         bt_mesh_net_start();
1445     }
1446 #endif /* CONFIG_BLE_MESH_NODE */
1447 
1448     return 0;
1449 }
1450 
1451 /* Pending flags that use K_NO_WAIT as the storage timeout */
1452 #define NO_WAIT_PENDING_BITS (BIT(BLE_MESH_NET_PENDING) |       \
1453                               BIT(BLE_MESH_IV_PENDING) |        \
1454                               BIT(BLE_MESH_SEQ_PENDING))
1455 
1456 /* Pending flags that use CONFIG_BLE_MESH_STORE_TIMEOUT */
1457 #define GENERIC_PENDING_BITS (BIT(BLE_MESH_KEYS_PENDING) |      \
1458                               BIT(BLE_MESH_HB_PUB_PENDING) |    \
1459                               BIT(BLE_MESH_CFG_PENDING) |       \
1460                               BIT(BLE_MESH_MOD_PENDING))
1461 
schedule_store(int flag)1462 static void schedule_store(int flag)
1463 {
1464     int32_t timeout = 0, remaining = 0;
1465 
1466     bt_mesh_atomic_set_bit(bt_mesh.flags, flag);
1467 
1468     /* When Node is not provisioned OR Provisioner is disabled,
1469      * we will directly erase the stored information.
1470      */
1471     if (!bt_mesh_is_provisioned() && !bt_mesh_is_provisioner_en()) {
1472         store_pending(NULL);
1473         return;
1474     }
1475 
1476     if (bt_mesh_atomic_get(bt_mesh.flags) & NO_WAIT_PENDING_BITS) {
1477         timeout = K_NO_WAIT;
1478     } else if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_RPL_PENDING) &&
1479                (!(bt_mesh_atomic_get(bt_mesh.flags) & GENERIC_PENDING_BITS) ||
1480                 (CONFIG_BLE_MESH_RPL_STORE_TIMEOUT < CONFIG_BLE_MESH_STORE_TIMEOUT))) {
1481         timeout = K_SECONDS(CONFIG_BLE_MESH_RPL_STORE_TIMEOUT);
1482     } else {
1483         timeout = K_SECONDS(CONFIG_BLE_MESH_STORE_TIMEOUT);
1484     }
1485 
1486     remaining = k_delayed_work_remaining_get(&pending_store);
1487     if (remaining && remaining < timeout) {
1488         BT_DBG("Not rescheduling due to existing earlier deadline");
1489         return;
1490     }
1491 
1492     BT_INFO("Settings store, waiting %d seconds", timeout / MSEC_PER_SEC);
1493 
1494     if (timeout) {
1495         k_delayed_work_submit(&pending_store, timeout);
1496     } else {
1497         k_work_submit(&pending_store.work);
1498     }
1499 }
1500 
clear_net(void)1501 static void clear_net(void)
1502 {
1503     BT_DBG("Clearing Network");
1504     bt_mesh_erase_core_settings("mesh/net");
1505 }
1506 
store_pending_net(void)1507 static void store_pending_net(void)
1508 {
1509     struct net_val net = {0};
1510 
1511     BT_DBG("Primary address 0x%04x DevKey %s", bt_mesh_primary_addr(),
1512            bt_hex(bt_mesh.dev_key, 16));
1513 
1514     net.primary_addr = bt_mesh_primary_addr();
1515     memcpy(net.dev_key, bt_mesh.dev_key, 16);
1516 
1517     bt_mesh_save_core_settings("mesh/net", (const uint8_t *)&net, sizeof(net));
1518 }
1519 
bt_mesh_store_role(void)1520 void bt_mesh_store_role(void)
1521 {
1522     BT_DBG("Store, device role %lu", bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK);
1523 
1524     bt_mesh_save_core_settings("mesh/role", (const uint8_t *)bt_mesh.flags, sizeof(bt_mesh.flags));
1525 }
1526 
bt_mesh_store_net(void)1527 void bt_mesh_store_net(void)
1528 {
1529     schedule_store(BLE_MESH_NET_PENDING);
1530 }
1531 
store_pending_iv(void)1532 static void store_pending_iv(void)
1533 {
1534     struct iv_val iv = {0};
1535 
1536     iv.iv_index = bt_mesh.iv_index;
1537     iv.iv_update = bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS);
1538     iv.iv_duration = bt_mesh.ivu_duration;
1539 
1540     bt_mesh_save_core_settings("mesh/iv", (const uint8_t *)&iv, sizeof(iv));
1541 }
1542 
bt_mesh_store_iv(bool only_duration)1543 void bt_mesh_store_iv(bool only_duration)
1544 {
1545     schedule_store(BLE_MESH_IV_PENDING);
1546 
1547     if (!only_duration) {
1548         /* Always update Seq whenever IV changes */
1549         schedule_store(BLE_MESH_SEQ_PENDING);
1550     }
1551 }
1552 
clear_iv(void)1553 static void clear_iv(void)
1554 {
1555     BT_DBG("Clearing IV");
1556     bt_mesh_erase_core_settings("mesh/iv");
1557 }
1558 
store_pending_seq(void)1559 static void store_pending_seq(void)
1560 {
1561     struct seq_val seq = {0};
1562 
1563     sys_put_le24(bt_mesh.seq, seq.val);
1564 
1565     bt_mesh_save_core_settings("mesh/seq", (const uint8_t *)&seq, sizeof(seq));
1566 }
1567 
bt_mesh_store_seq(void)1568 void bt_mesh_store_seq(void)
1569 {
1570     if (CONFIG_BLE_MESH_SEQ_STORE_RATE &&
1571             (bt_mesh.seq % CONFIG_BLE_MESH_SEQ_STORE_RATE)) {
1572         return;
1573     }
1574 
1575     schedule_store(BLE_MESH_SEQ_PENDING);
1576 }
1577 
bt_mesh_clear_seq(void)1578 void bt_mesh_clear_seq(void)
1579 {
1580     BT_DBG("Clearing Seq");
1581     bt_mesh_erase_core_settings("mesh/seq");
1582 }
1583 
store_rpl(struct bt_mesh_rpl * entry)1584 static void store_rpl(struct bt_mesh_rpl *entry)
1585 {
1586     struct rpl_val rpl = {0};
1587     char name[16] = {'\0'};
1588     int err = 0;
1589 
1590     BT_DBG("src 0x%04x seq 0x%06x old_iv %u", entry->src, entry->seq, entry->old_iv);
1591 
1592     rpl.seq = entry->seq;
1593     rpl.old_iv = entry->old_iv;
1594 
1595     sprintf(name, "mesh/rpl/%04x", entry->src);
1596     err = bt_mesh_save_core_settings(name, (const uint8_t *)&rpl, sizeof(rpl));
1597     if (err) {
1598         BT_ERR("Failed to store RPL entry 0x%04x", entry->src);
1599         return;
1600     }
1601 
1602     err = bt_mesh_add_core_settings_item("mesh/rpl", entry->src);
1603     if (err) {
1604         BT_ERR("Failed to add 0x%04x to mesh/rpl", entry->src);
1605     }
1606 
1607     return;
1608 }
1609 
clear_rpl(void)1610 static void clear_rpl(void)
1611 {
1612     struct net_buf_simple *buf = NULL;
1613     char name[16] = {'\0'};
1614     size_t length = 0U;
1615     uint16_t src = 0U;
1616     int i;
1617 
1618     BT_DBG("%s", __func__);
1619 
1620     buf = bt_mesh_get_core_settings_item("mesh/rpl");
1621     if (!buf) {
1622         bt_mesh_erase_core_settings("mesh/rpl");
1623         return;
1624     }
1625 
1626     length = buf->len;
1627 
1628     for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) {
1629         src = net_buf_simple_pull_le16(buf);
1630 
1631         if (!BLE_MESH_ADDR_IS_UNICAST(src)) {
1632             BT_ERR("Invalid source address 0x%04x", src);
1633             continue;
1634         }
1635 
1636         sprintf(name, "mesh/rpl/%04x", src);
1637         bt_mesh_erase_core_settings(name);
1638     }
1639 
1640     bt_mesh_erase_core_settings("mesh/rpl");
1641 
1642     bt_mesh_free_buf(buf);
1643     return;
1644 }
1645 
store_pending_rpl(void)1646 static void store_pending_rpl(void)
1647 {
1648     int i;
1649 
1650     BT_DBG("%s", __func__);
1651 
1652     for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) {
1653         struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i];
1654 
1655         if (rpl->store) {
1656             rpl->store = false;
1657             store_rpl(rpl);
1658         }
1659     }
1660 }
1661 
store_pending_hb_pub(void)1662 static void store_pending_hb_pub(void)
1663 {
1664     struct bt_mesh_hb_pub *hb_pub = bt_mesh_hb_pub_get();
1665     struct hb_pub_val val = {0};
1666 
1667     if (!hb_pub) {
1668         BT_ERR("Invalid heartbeat publication");
1669         return;
1670     }
1671 
1672     val.indefinite = (hb_pub->count = 0xffff);
1673     val.dst = hb_pub->dst;
1674     val.period = hb_pub->period;
1675     val.ttl = hb_pub->ttl;
1676     val.feat = hb_pub->feat;
1677     val.net_idx = hb_pub->net_idx;
1678 
1679     bt_mesh_save_core_settings("mesh/hb_pub", (const uint8_t *)&val, sizeof(val));
1680 }
1681 
clear_hb_pub(void)1682 static void clear_hb_pub(void)
1683 {
1684     BT_DBG("Clear heartbeat publication");
1685     bt_mesh_erase_core_settings("mesh/hb_pub");
1686 }
1687 
store_pending_cfg(void)1688 static void store_pending_cfg(void)
1689 {
1690     struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get();
1691     struct cfg_val val = {0};
1692 
1693     if (!cfg) {
1694         BT_WARN("NULL configuration state");
1695         return;
1696     }
1697 
1698     val.net_transmit = cfg->net_transmit;
1699     val.relay = cfg->relay;
1700     val.relay_retransmit = cfg->relay_retransmit;
1701     val.beacon = cfg->beacon;
1702     val.gatt_proxy = cfg->gatt_proxy;
1703     val.frnd = cfg->frnd;
1704     val.default_ttl = cfg->default_ttl;
1705 
1706     bt_mesh_save_core_settings("mesh/cfg", (const uint8_t *)&val, sizeof(val));
1707 }
1708 
clear_cfg(void)1709 static void clear_cfg(void)
1710 {
1711     BT_DBG("Clearing configuration");
1712     bt_mesh_erase_core_settings("mesh/cfg");
1713 }
1714 
clear_app_key(uint16_t app_idx)1715 static void clear_app_key(uint16_t app_idx)
1716 {
1717     char name[16] = {'\0'};
1718     int err = 0;
1719 
1720     BT_DBG("AppKeyIndex 0x%03x", app_idx);
1721 
1722     sprintf(name, "mesh/ak/%04x", app_idx);
1723     bt_mesh_erase_core_settings(name);
1724 
1725     err = bt_mesh_remove_core_settings_item("mesh/appkey", app_idx);
1726     if (err) {
1727         BT_ERR("Failed to remove 0x%03x from mesh/appkey", app_idx);
1728     }
1729 
1730     return;
1731 }
1732 
clear_net_key(uint16_t net_idx)1733 static void clear_net_key(uint16_t net_idx)
1734 {
1735     char name[16] = {'\0'};
1736     int err = 0;
1737 
1738     BT_DBG("NetKeyIndex 0x%03x", net_idx);
1739 
1740     sprintf(name, "mesh/nk/%04x", net_idx);
1741     bt_mesh_erase_core_settings(name);
1742 
1743     err = bt_mesh_remove_core_settings_item("mesh/netkey", net_idx);
1744     if (err) {
1745         BT_ERR("Failed to remove 0x%03x from mesh/netkey", net_idx);
1746     }
1747 
1748     return;
1749 }
1750 
store_net_key(struct bt_mesh_subnet * sub)1751 static void store_net_key(struct bt_mesh_subnet *sub)
1752 {
1753     struct net_key_val key = {0};
1754     char name[16] = {'\0'};
1755     int err = 0;
1756 
1757     BT_DBG("NetKeyIndex 0x%03x NetKey %s", sub->net_idx,
1758            bt_hex(sub->keys[0].net, 16));
1759 
1760     memcpy(&key.val[0], sub->keys[0].net, 16);
1761     memcpy(&key.val[1], sub->keys[1].net, 16);
1762     key.kr_flag = sub->kr_flag;
1763     key.kr_phase = sub->kr_phase;
1764 
1765     sprintf(name, "mesh/nk/%04x", sub->net_idx);
1766     err = bt_mesh_save_core_settings(name, (const uint8_t *)&key, sizeof(key));
1767     if (err) {
1768         BT_ERR("Failed to store NetKey 0x%03x", sub->net_idx);
1769         return;
1770     }
1771 
1772     err = bt_mesh_add_core_settings_item("mesh/netkey", sub->net_idx);
1773     if (err) {
1774         BT_ERR("Failed to add 0x%03x to mesh/netkey", sub->net_idx);
1775     }
1776 
1777     return;
1778 }
1779 
store_app_key(struct bt_mesh_app_key * app)1780 static void store_app_key(struct bt_mesh_app_key *app)
1781 {
1782     struct app_key_val key = {0};
1783     char name[16] = {'\0'};
1784     int err = 0;
1785 
1786     key.net_idx = app->net_idx;
1787     key.updated = app->updated;
1788     memcpy(key.val[0], app->keys[0].val, 16);
1789     memcpy(key.val[1], app->keys[1].val, 16);
1790 
1791     sprintf(name, "mesh/ak/%04x", app->app_idx);
1792     err = bt_mesh_save_core_settings(name, (const uint8_t *)&key, sizeof(key));
1793     if (err) {
1794         BT_ERR("Failed to store AppKey 0x%03x", app->app_idx);
1795         return;
1796     }
1797 
1798     err = bt_mesh_add_core_settings_item("mesh/appkey", app->app_idx);
1799     if (err) {
1800         BT_ERR("Failed to add 0x%03x to mesh/appkey", app->app_idx);
1801     }
1802 
1803     return;
1804 }
1805 
store_pending_keys(void)1806 static void store_pending_keys(void)
1807 {
1808     int i;
1809 
1810     for (i = 0; i < ARRAY_SIZE(key_updates); i++) {
1811         struct key_update *update = &key_updates[i];
1812 
1813         if (!update->valid) {
1814             continue;
1815         }
1816 
1817         if (update->clear) {
1818             if (update->app_key) {
1819                 clear_app_key(update->key_idx);
1820             } else {
1821                 clear_net_key(update->key_idx);
1822             }
1823         } else {
1824             if (update->app_key) {
1825                 struct bt_mesh_app_key *key = NULL;
1826                 key = bt_mesh_app_key_find(update->key_idx);
1827                 if (key) {
1828                     store_app_key(key);
1829                 } else {
1830                     BT_WARN("AppKeyIndex 0x%03x not found", update->key_idx);
1831                 }
1832             } else {
1833                 struct bt_mesh_subnet *sub = NULL;
1834                 sub = bt_mesh_subnet_get(update->key_idx);
1835                 if (sub) {
1836                     store_net_key(sub);
1837                 } else {
1838                     BT_WARN("NetKeyIndex 0x%03x not found", update->key_idx);
1839                 }
1840             }
1841         }
1842 
1843         update->valid = 0U;
1844     }
1845 }
1846 
store_pending_mod_bind(struct bt_mesh_model * model,bool vnd)1847 static void store_pending_mod_bind(struct bt_mesh_model *model, bool vnd)
1848 {
1849     char name[16] = {'\0'};
1850     uint16_t model_key = 0U;
1851     int err = 0;
1852 
1853     model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
1854     sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key);
1855 
1856     err = bt_mesh_save_core_settings(name, (const uint8_t *)model->keys, sizeof(model->keys));
1857     if (err) {
1858         BT_ERR("Failed to store %s", name);
1859         return;
1860     }
1861 
1862     err = bt_mesh_add_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key);
1863     if (err) {
1864         BT_ERR("Failed to add bound key to %s, model_key 0x%04x",
1865             vnd ? "mesh/vnd" : "mesh/sig", model_key);
1866     }
1867 
1868     return;
1869 }
1870 
store_pending_mod_sub(struct bt_mesh_model * model,bool vnd)1871 static void store_pending_mod_sub(struct bt_mesh_model *model, bool vnd)
1872 {
1873     char name[16] = {'\0'};
1874     uint16_t model_key = 0U;
1875     int err = 0;
1876 
1877     model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
1878     sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key);
1879 
1880     err = bt_mesh_save_core_settings(name, (const uint8_t *)model->groups, sizeof(model->groups));
1881     if (err) {
1882         BT_ERR("Failed to store %s", name);
1883         return;
1884     }
1885 
1886     err = bt_mesh_add_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key);
1887     if (err) {
1888         BT_ERR("Failed to add subscription to %s, model_key 0x%04x",
1889             vnd ? "mesh/vnd" : "mesh/sig", model_key);
1890     }
1891 
1892     return;
1893 }
1894 
store_pending_mod_pub(struct bt_mesh_model * model,bool vnd)1895 static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd)
1896 {
1897     struct mod_pub_val pub = {0};
1898     char name[16] = {'\0'};
1899     uint16_t model_key = 0U;
1900     int err = 0;
1901 
1902     if (!model->pub) {
1903         BT_WARN("Model has no publication support");
1904         return;
1905     }
1906 
1907     model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
1908     sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key);
1909 
1910     pub.addr = model->pub->addr;
1911     pub.key = model->pub->key;
1912     pub.ttl = model->pub->ttl;
1913     pub.retransmit = model->pub->retransmit;
1914     pub.period = model->pub->period;
1915     pub.period_div = model->pub->period_div;
1916     pub.cred = model->pub->cred;
1917 
1918     err = bt_mesh_save_core_settings(name, (const uint8_t *)&pub, sizeof(pub));
1919     if (err) {
1920         BT_ERR("Failed to store %s", name);
1921         return;
1922     }
1923 
1924     err = bt_mesh_add_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key);
1925     if (err) {
1926         BT_ERR("Failed to add publication to %s, model_key 0x%04x",
1927                vnd ? "mesh/vnd" : "mesh/sig", model_key);
1928     }
1929 
1930     return;
1931 }
1932 
store_pending_mod(struct bt_mesh_model * model,struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)1933 static void store_pending_mod(struct bt_mesh_model *model,
1934                               struct bt_mesh_elem *elem, bool vnd,
1935                               bool primary, void *user_data)
1936 {
1937     if (!model->flags) {
1938         return;
1939     }
1940 
1941     if (model->flags & BLE_MESH_MOD_BIND_PENDING) {
1942         model->flags &= ~BLE_MESH_MOD_BIND_PENDING;
1943         store_pending_mod_bind(model, vnd);
1944     }
1945 
1946     if (model->flags & BLE_MESH_MOD_SUB_PENDING) {
1947         model->flags &= ~BLE_MESH_MOD_SUB_PENDING;
1948         store_pending_mod_sub(model, vnd);
1949     }
1950 
1951     if (model->flags & BLE_MESH_MOD_PUB_PENDING) {
1952         model->flags &= ~BLE_MESH_MOD_PUB_PENDING;
1953         store_pending_mod_pub(model, vnd);
1954     }
1955 }
1956 
clear_mod_bind(struct bt_mesh_model * model,bool vnd)1957 static void clear_mod_bind(struct bt_mesh_model *model, bool vnd)
1958 {
1959     char name[16] = {'\0'};
1960     uint16_t model_key = 0U;
1961 
1962     model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
1963     sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key);
1964 
1965     bt_mesh_erase_core_settings(name);
1966     bt_mesh_remove_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key);
1967 }
1968 
clear_mod_sub(struct bt_mesh_model * model,bool vnd)1969 static void clear_mod_sub(struct bt_mesh_model *model, bool vnd)
1970 {
1971     char name[16] = {'\0'};
1972     uint16_t model_key = 0U;
1973 
1974     model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
1975     sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key);
1976 
1977     bt_mesh_erase_core_settings(name);
1978     bt_mesh_remove_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key);
1979 }
1980 
clear_mod_pub(struct bt_mesh_model * model,bool vnd)1981 static void clear_mod_pub(struct bt_mesh_model *model, bool vnd)
1982 {
1983     char name[16] = {'\0'};
1984     uint16_t model_key = 0U;
1985 
1986     model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx);
1987     sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key);
1988 
1989     bt_mesh_erase_core_settings(name);
1990     bt_mesh_remove_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key);
1991 }
1992 
clear_pending_mod(struct bt_mesh_model * model,struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)1993 static void clear_pending_mod(struct bt_mesh_model *model,
1994                               struct bt_mesh_elem *elem, bool vnd,
1995                               bool primary, void *user_data)
1996 {
1997     if (!model->flags) {
1998         return;
1999     }
2000 
2001     if (model->flags & BLE_MESH_MOD_BIND_PENDING) {
2002         model->flags &= ~BLE_MESH_MOD_BIND_PENDING;
2003         clear_mod_bind(model, vnd);
2004     }
2005 
2006     if (model->flags & BLE_MESH_MOD_SUB_PENDING) {
2007         model->flags &= ~BLE_MESH_MOD_SUB_PENDING;
2008         clear_mod_sub(model, vnd);
2009     }
2010 
2011     if (model->flags & BLE_MESH_MOD_PUB_PENDING) {
2012         model->flags &= ~BLE_MESH_MOD_PUB_PENDING;
2013         clear_mod_pub(model, vnd);
2014     }
2015 }
2016 
2017 #define IS_VA_DEL(_label)   ((_label)->ref == 0)
store_pending_va(void)2018 static void store_pending_va(void)
2019 {
2020     struct va_val va = {0};
2021     char name[16] = {'\0'};
2022     struct label *lab = NULL;
2023     uint16_t i = 0U;
2024     int err = 0;
2025 
2026     for (i = 0U; (lab = get_label(i)) != NULL; i++) {
2027         if (!bt_mesh_atomic_test_and_clear_bit(lab->flags,
2028             BLE_MESH_VA_CHANGED)) {
2029             continue;
2030         }
2031 
2032         sprintf(name, "mesh/va/%04x", i);
2033 
2034         if (IS_VA_DEL(lab)) {
2035             err = bt_mesh_erase_core_settings(name);
2036         } else {
2037             va.ref = lab->ref;
2038             va.addr = lab->addr;
2039             memcpy(va.uuid, lab->uuid, 16);
2040             err = bt_mesh_save_core_settings(name, (const uint8_t *)&va, sizeof(va));
2041         }
2042         if (err) {
2043             BT_ERR("Failed to %s virtual address %s",
2044                 IS_VA_DEL(lab) ? "delete" : "store", name);
2045             return;
2046         }
2047 
2048         if (IS_VA_DEL(lab)) {
2049             err = bt_mesh_remove_core_settings_item("mesh/vaddr", i);
2050         } else {
2051             err = bt_mesh_add_core_settings_item("mesh/vaddr", i);
2052         }
2053         if (err) {
2054             BT_ERR("Failed to %s 0x%04x in mesh/vaddr",
2055                 IS_VA_DEL(lab) ? "delete" : "store", i);
2056             return;
2057         }
2058 
2059         BT_DBG("%s virtual address 0x%04x", IS_VA_DEL(lab) ? "Delete" : "Store", i);
2060     }
2061 }
2062 
store_pending(struct k_work * work)2063 static void store_pending(struct k_work *work)
2064 {
2065     BT_DBG("%s", __func__);
2066 
2067     if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_RPL_PENDING)) {
2068         if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) {
2069             store_pending_rpl();
2070         } else {
2071             clear_rpl();
2072         }
2073     }
2074 
2075     if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
2076         bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_KEYS_PENDING)) {
2077         store_pending_keys();
2078     }
2079 
2080     if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
2081         bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_NET_PENDING)) {
2082         if (bt_mesh_is_provisioned()) {
2083             store_pending_net();
2084         } else {
2085             clear_net();
2086         }
2087     }
2088 
2089     if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IV_PENDING)) {
2090         if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) {
2091             store_pending_iv();
2092         } else {
2093             clear_iv();
2094         }
2095     }
2096 
2097     if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_SEQ_PENDING)) {
2098         if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) {
2099             store_pending_seq();
2100         } else {
2101             bt_mesh_clear_seq();
2102         }
2103     }
2104 
2105     if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
2106         bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_HB_PUB_PENDING)) {
2107         if (bt_mesh_is_provisioned()) {
2108             store_pending_hb_pub();
2109         } else {
2110             clear_hb_pub();
2111         }
2112     }
2113 
2114     if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
2115         bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_CFG_PENDING)) {
2116         if (bt_mesh_is_provisioned()) {
2117             store_pending_cfg();
2118         } else {
2119             clear_cfg();
2120         }
2121     }
2122 
2123     if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_MOD_PENDING)) {
2124         if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) {
2125             bt_mesh_model_foreach(store_pending_mod, NULL);
2126         } else {
2127             bt_mesh_model_foreach(clear_pending_mod, NULL);
2128             bt_mesh_erase_core_settings("mesh/sig");
2129             bt_mesh_erase_core_settings("mesh/vnd");
2130         }
2131     }
2132 
2133     if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
2134         bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_VA_PENDING)) {
2135         store_pending_va();
2136     }
2137 }
2138 
bt_mesh_store_rpl(struct bt_mesh_rpl * entry)2139 void bt_mesh_store_rpl(struct bt_mesh_rpl *entry)
2140 {
2141     entry->store = true;
2142     schedule_store(BLE_MESH_RPL_PENDING);
2143 }
2144 
key_update_find(bool app_key,uint16_t key_idx,struct key_update ** free_slot)2145 static struct key_update *key_update_find(bool app_key, uint16_t key_idx,
2146                                           struct key_update **free_slot)
2147 {
2148     struct key_update *match = NULL;
2149     int i;
2150 
2151     *free_slot = NULL;
2152 
2153     for (i = 0; i < ARRAY_SIZE(key_updates); i++) {
2154         struct key_update *update = &key_updates[i];
2155 
2156         if (!update->valid) {
2157             *free_slot = update;
2158             continue;
2159         }
2160 
2161         if (update->app_key != app_key) {
2162             continue;
2163         }
2164 
2165         if (update->key_idx == key_idx) {
2166             match = update;
2167         }
2168     }
2169 
2170     return match;
2171 }
2172 
bt_mesh_store_subnet(struct bt_mesh_subnet * sub)2173 void bt_mesh_store_subnet(struct bt_mesh_subnet *sub)
2174 {
2175     struct key_update *free_slot = NULL;
2176     struct key_update *update = NULL;
2177 
2178     BT_DBG("NetKeyIndex 0x%03x", sub->net_idx);
2179 
2180     update = key_update_find(false, sub->net_idx, &free_slot);
2181     if (update) {
2182         update->clear = 0U;
2183         schedule_store(BLE_MESH_KEYS_PENDING);
2184         return;
2185     }
2186 
2187     if (!free_slot) {
2188         store_net_key(sub);
2189         return;
2190     }
2191 
2192     free_slot->valid = 1U;
2193     free_slot->key_idx = sub->net_idx;
2194     free_slot->app_key = 0U;
2195     free_slot->clear = 0U;
2196 
2197     schedule_store(BLE_MESH_KEYS_PENDING);
2198 }
2199 
bt_mesh_store_app_key(struct bt_mesh_app_key * key)2200 void bt_mesh_store_app_key(struct bt_mesh_app_key *key)
2201 {
2202     struct key_update *free_slot = NULL;
2203     struct key_update *update = NULL;
2204 
2205     BT_DBG("AppKeyIndex 0x%03x", key->app_idx);
2206 
2207     update = key_update_find(true, key->app_idx, &free_slot);
2208     if (update) {
2209         update->clear = 0U;
2210         schedule_store(BLE_MESH_KEYS_PENDING);
2211         return;
2212     }
2213 
2214     if (!free_slot) {
2215         store_app_key(key);
2216         return;
2217     }
2218 
2219     free_slot->valid = 1U;
2220     free_slot->key_idx = key->app_idx;
2221     free_slot->app_key = 1U;
2222     free_slot->clear = 0U;
2223 
2224     schedule_store(BLE_MESH_KEYS_PENDING);
2225 }
2226 
bt_mesh_store_hb_pub(void)2227 void bt_mesh_store_hb_pub(void)
2228 {
2229     schedule_store(BLE_MESH_HB_PUB_PENDING);
2230 }
2231 
bt_mesh_store_cfg(void)2232 void bt_mesh_store_cfg(void)
2233 {
2234     schedule_store(BLE_MESH_CFG_PENDING);
2235 }
2236 
bt_mesh_clear_role(void)2237 void bt_mesh_clear_role(void)
2238 {
2239     BT_DBG("Clear device role");
2240     bt_mesh_erase_core_settings("mesh/role");
2241 }
2242 
bt_mesh_clear_net(void)2243 void bt_mesh_clear_net(void)
2244 {
2245     schedule_store(BLE_MESH_NET_PENDING);
2246     schedule_store(BLE_MESH_IV_PENDING);
2247     schedule_store(BLE_MESH_CFG_PENDING);
2248 }
2249 
bt_mesh_clear_subnet(struct bt_mesh_subnet * sub)2250 void bt_mesh_clear_subnet(struct bt_mesh_subnet *sub)
2251 {
2252     struct key_update *free_slot = NULL;
2253     struct key_update *update = NULL;
2254 
2255     BT_DBG("NetKeyIndex 0x%03x", sub->net_idx);
2256 
2257     update = key_update_find(false, sub->net_idx, &free_slot);
2258     if (update) {
2259         update->clear = 1U;
2260         schedule_store(BLE_MESH_KEYS_PENDING);
2261         return;
2262     }
2263 
2264     if (!free_slot) {
2265         clear_net_key(sub->net_idx);
2266         return;
2267     }
2268 
2269     free_slot->valid = 1U;
2270     free_slot->key_idx = sub->net_idx;
2271     free_slot->app_key = 0U;
2272     free_slot->clear = 1U;
2273 
2274     schedule_store(BLE_MESH_KEYS_PENDING);
2275 }
2276 
bt_mesh_clear_app_key(struct bt_mesh_app_key * key)2277 void bt_mesh_clear_app_key(struct bt_mesh_app_key *key)
2278 {
2279     struct key_update *free_slot = NULL;
2280     struct key_update *update = NULL;
2281 
2282     BT_DBG("AppKeyIndex 0x%03x", key->app_idx);
2283 
2284     update = key_update_find(true, key->app_idx, &free_slot);
2285     if (update) {
2286         update->clear = 1U;
2287         schedule_store(BLE_MESH_KEYS_PENDING);
2288         return;
2289     }
2290 
2291     if (!free_slot) {
2292         clear_app_key(key->app_idx);
2293         return;
2294     }
2295 
2296     free_slot->valid = 1U;
2297     free_slot->key_idx = key->app_idx;
2298     free_slot->app_key = 1U;
2299     free_slot->clear = 1U;
2300 
2301     schedule_store(BLE_MESH_KEYS_PENDING);
2302 }
2303 
bt_mesh_clear_rpl(void)2304 void bt_mesh_clear_rpl(void)
2305 {
2306     schedule_store(BLE_MESH_RPL_PENDING);
2307 }
2308 
bt_mesh_store_mod_bind(struct bt_mesh_model * model)2309 void bt_mesh_store_mod_bind(struct bt_mesh_model *model)
2310 {
2311     model->flags |= BLE_MESH_MOD_BIND_PENDING;
2312     schedule_store(BLE_MESH_MOD_PENDING);
2313 }
2314 
bt_mesh_store_mod_sub(struct bt_mesh_model * model)2315 void bt_mesh_store_mod_sub(struct bt_mesh_model *model)
2316 {
2317     model->flags |= BLE_MESH_MOD_SUB_PENDING;
2318     schedule_store(BLE_MESH_MOD_PENDING);
2319 }
2320 
bt_mesh_store_mod_pub(struct bt_mesh_model * model)2321 void bt_mesh_store_mod_pub(struct bt_mesh_model *model)
2322 {
2323     model->flags |= BLE_MESH_MOD_PUB_PENDING;
2324     schedule_store(BLE_MESH_MOD_PENDING);
2325 }
2326 
bt_mesh_store_label(void)2327 void bt_mesh_store_label(void)
2328 {
2329     schedule_store(BLE_MESH_VA_PENDING);
2330 }
2331 
2332 #if CONFIG_BLE_MESH_PROVISIONER
2333 /**
2334  * key: "mesh/p_prov"   -> write/read to set/get prov_ctx.curr_addr
2335  * key: "mesh/p_netidx" -> write/read to set/get bt_mesh.p_net_idx_next
2336  * key: "mesh/p_appidx" -> write/read to set/get bt_mesh.p_app_idx_next
2337  * key: "mesh/p_netkey" -> write/read to set/get all Provisioner NetKey Index
2338  *      key: "mesh/pnk/xxxx" -> write/read to set/get the "xxxx" NetKey
2339  * key: "mesh/p_appkey" -> write/read to set/get all Provisioner AppKey Index
2340  *      key: "mesh/pak/xxxx" -> write/read to set/get the "xxxx" AppKey
2341  * key: "mesh/p_node"   -> write/read to set/get all self-provisioned nodes info
2342  *      key: "mesh/pn/xxxx/i" -> write/read to set/get the "xxxx" provisioned node info
2343  *      key: "mesh/pn/xxxx/n" -> write/read to set/get the "xxxx" provisioned node name
2344  *      key: "mesh/pn/xxxx/c" -> write/read to set/get the "xxxx" provisioned node composition data
2345  */
bt_mesh_store_prov_info(uint16_t primary_addr,uint16_t alloc_addr)2346 void bt_mesh_store_prov_info(uint16_t primary_addr, uint16_t alloc_addr)
2347 {
2348     struct prov_info val = {0};
2349 
2350     BT_DBG("Primary address 0x%04x, next address allocation 0x%04x", primary_addr, alloc_addr);
2351 
2352     val.primary_addr = primary_addr;
2353     val.alloc_addr = alloc_addr;
2354 
2355     bt_mesh_save_core_settings("mesh/p_prov", (const uint8_t *)&val, sizeof(val));
2356 }
2357 
bt_mesh_clear_prov_info(void)2358 void bt_mesh_clear_prov_info(void)
2359 {
2360     BT_DBG("Clearing prov info");
2361     bt_mesh_erase_core_settings("mesh/p_prov");
2362 }
2363 
store_p_net_key(struct bt_mesh_subnet * sub)2364 static void store_p_net_key(struct bt_mesh_subnet *sub)
2365 {
2366     struct net_key_val key = {0};
2367     char name[16] = {'\0'};
2368     int err = 0;
2369 
2370     memcpy(&key.val[0], sub->keys[0].net, 16);
2371     memcpy(&key.val[1], sub->keys[1].net, 16);
2372     key.kr_flag = sub->kr_flag;
2373     key.kr_phase = sub->kr_phase;
2374 
2375     sprintf(name, "mesh/pnk/%04x", sub->net_idx);
2376     err = bt_mesh_save_core_settings(name, (const uint8_t *)&key, sizeof(key));
2377     if (err) {
2378         BT_ERR("Failed to store NetKey 0x%03x", sub->net_idx);
2379         return;
2380     }
2381 
2382     err = bt_mesh_add_core_settings_item("mesh/p_netkey", sub->net_idx);
2383     if (err) {
2384         BT_ERR("Failed to add 0x%03x to mesh/p_netkey", sub->net_idx);
2385     }
2386 }
2387 
store_p_app_key(struct bt_mesh_app_key * app)2388 static void store_p_app_key(struct bt_mesh_app_key *app)
2389 {
2390     struct app_key_val key = {0};
2391     char name[16] = {'\0'};
2392     int err = 0;
2393 
2394     key.net_idx = app->net_idx;
2395     key.updated = app->updated;
2396     memcpy(key.val[0], app->keys[0].val, 16);
2397     memcpy(key.val[1], app->keys[1].val, 16);
2398 
2399     sprintf(name, "mesh/pak/%04x", app->app_idx);
2400     err = bt_mesh_save_core_settings(name, (const uint8_t *)&key, sizeof(key));
2401     if (err) {
2402         BT_ERR("Failed to store AppKey 0x%03x", app->app_idx);
2403         return;
2404     }
2405 
2406     err = bt_mesh_add_core_settings_item("mesh/p_appkey", app->app_idx);
2407     if (err) {
2408         BT_ERR("Failed to add 0x%03x to mesh/p_appkey", app->app_idx);
2409     }
2410 }
2411 
bt_mesh_store_p_net_idx(void)2412 void bt_mesh_store_p_net_idx(void)
2413 {
2414     BT_DBG("Store, p_net_idx_next 0x%03x", bt_mesh.p_net_idx_next);
2415 
2416     bt_mesh_save_core_settings("mesh/p_netidx",
2417         (const uint8_t *)&bt_mesh.p_net_idx_next, sizeof(bt_mesh.p_net_idx_next));
2418 }
2419 
bt_mesh_clear_p_net_idx(void)2420 void bt_mesh_clear_p_net_idx(void)
2421 {
2422     BT_DBG("Clearing NetKey Index");
2423     bt_mesh_erase_core_settings("mesh/p_netidx");
2424 }
2425 
bt_mesh_store_p_app_idx(void)2426 void bt_mesh_store_p_app_idx(void)
2427 {
2428     BT_DBG("Store, p_app_idx_next 0x%03x", bt_mesh.p_app_idx_next);
2429 
2430     bt_mesh_save_core_settings("mesh/p_appidx",
2431         (const uint8_t *)&bt_mesh.p_app_idx_next, sizeof(bt_mesh.p_app_idx_next));
2432 }
2433 
bt_mesh_clear_p_app_idx(void)2434 void bt_mesh_clear_p_app_idx(void)
2435 {
2436     BT_DBG("Clearing AppKey Index");
2437     bt_mesh_erase_core_settings("mesh/p_appidx");
2438 }
2439 
bt_mesh_store_p_subnet(struct bt_mesh_subnet * sub)2440 void bt_mesh_store_p_subnet(struct bt_mesh_subnet *sub)
2441 {
2442     if (sub == NULL) {
2443         BT_ERR("Invalid subnet");
2444         return;
2445     }
2446 
2447     BT_DBG("NetKeyIndex 0x%03x NetKey %s", sub->net_idx,
2448         bt_hex(sub->keys[0].net, 16));
2449 
2450     store_p_net_key(sub);
2451 }
2452 
bt_mesh_store_p_app_key(struct bt_mesh_app_key * key)2453 void bt_mesh_store_p_app_key(struct bt_mesh_app_key *key)
2454 {
2455     if (key == NULL) {
2456         BT_ERR("Invalid AppKey");
2457         return;
2458     }
2459 
2460     BT_DBG("AppKeyIndex 0x%03x AppKey %s", key->app_idx,
2461         bt_hex(key->keys[0].val, 16));
2462 
2463     store_p_app_key(key);
2464 }
2465 
bt_mesh_clear_p_subnet(uint16_t net_idx)2466 void bt_mesh_clear_p_subnet(uint16_t net_idx)
2467 {
2468     char name[16] = {'\0'};
2469     int err = 0;
2470 
2471     BT_DBG("NetKeyIndex 0x%03x", net_idx);
2472 
2473     sprintf(name, "mesh/pnk/%04x", net_idx);
2474     bt_mesh_erase_core_settings(name);
2475 
2476     err = bt_mesh_remove_core_settings_item("mesh/p_netkey", net_idx);
2477     if (err) {
2478         BT_ERR("Failed to remove 0x%04x from mesh/p_netkey", net_idx);
2479     }
2480 }
2481 
bt_mesh_clear_p_app_key(uint16_t app_idx)2482 void bt_mesh_clear_p_app_key(uint16_t app_idx)
2483 {
2484     char name[16] = {'\0'};
2485     int err = 0;
2486 
2487     BT_DBG("AppKeyIndex 0x%03x", app_idx);
2488 
2489     sprintf(name, "mesh/pak/%04x", app_idx);
2490     bt_mesh_erase_core_settings(name);
2491 
2492     err = bt_mesh_remove_core_settings_item("mesh/p_appkey", app_idx);
2493     if (err) {
2494         BT_ERR("Failed to remove 0x%04x from mesh/p_appkey", app_idx);
2495     }
2496 }
2497 
bt_mesh_clear_rpl_single(uint16_t src)2498 void bt_mesh_clear_rpl_single(uint16_t src)
2499 {
2500     char name[16] = {'\0'};
2501     int err = 0;
2502 
2503     if (!BLE_MESH_ADDR_IS_UNICAST(src)) {
2504         BT_ERR("Invalid src 0x%04x", src);
2505         return;
2506     }
2507 
2508     sprintf(name, "mesh/rpl/%04x", src);
2509     bt_mesh_erase_core_settings(name);
2510 
2511     err = bt_mesh_remove_core_settings_item("mesh/rpl", src);
2512     if (err) {
2513         BT_ERR("Failed to remove 0x%04x from mesh/rpl", src);
2514     }
2515 }
2516 
bt_mesh_store_node_info(struct bt_mesh_node * node)2517 void bt_mesh_store_node_info(struct bt_mesh_node *node)
2518 {
2519     struct node_info val = {0};
2520     char name[16] = {'\0'};
2521     int err = 0;
2522 
2523     if (node == NULL) {
2524         BT_ERR("Invalid node info");
2525         return;
2526     }
2527 
2528     memcpy(val.addr, node->addr, BLE_MESH_ADDR_LEN);
2529     val.addr_type = node->addr_type;
2530     memcpy(val.dev_uuid, node->dev_uuid, 16);
2531     val.oob_info = node->oob_info;
2532     val.unicast_addr = node->unicast_addr;
2533     val.element_num = node->element_num;
2534     val.net_idx = node->net_idx;
2535     val.flags = node->flags;
2536     val.iv_index = node->iv_index;
2537     memcpy(val.dev_key, node->dev_key, 16);
2538 
2539     sprintf(name, "mesh/pn/%04x/i", node->unicast_addr);
2540     err = bt_mesh_save_core_settings(name, (const uint8_t *)&val, sizeof(val));
2541     if (err) {
2542         BT_ERR("Failed to store node 0x%04x info", node->unicast_addr);
2543         return;
2544     }
2545 
2546     err = bt_mesh_add_core_settings_item("mesh/p_node", node->unicast_addr);
2547     if (err) {
2548         BT_ERR("Failed to add node 0x%04x info", node->unicast_addr);
2549     }
2550 }
2551 
clear_node(uint16_t addr)2552 static void clear_node(uint16_t addr)
2553 {
2554     char name[16] = {'\0'};
2555     int err = 0;
2556 
2557     /* Clear node information */
2558     sprintf(name, "mesh/pn/%04x/i", addr);
2559     bt_mesh_erase_core_settings(name);
2560 
2561     /* Clear node name */
2562     sprintf(name, "mesh/pn/%04x/n", addr);
2563     bt_mesh_erase_core_settings(name);
2564 
2565     /* Clear node composition data */
2566     sprintf(name, "mesh/pn/%04x/c", addr);
2567     bt_mesh_erase_core_settings(name);
2568 
2569     err = bt_mesh_remove_core_settings_item("mesh/p_node", addr);
2570     if (err) {
2571         BT_ERR("Failed to remove node 0x%04x", addr);
2572     }
2573 }
2574 
bt_mesh_clear_node_info(uint16_t unicast_addr)2575 void bt_mesh_clear_node_info(uint16_t unicast_addr)
2576 {
2577     if (!BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) {
2578         BT_ERR("Invalid unicast address 0x%04x", unicast_addr);
2579         return;
2580     }
2581 
2582     BT_DBG("Unicast address 0x%04x", unicast_addr);
2583 
2584     clear_node(unicast_addr);
2585 }
2586 
bt_mesh_store_node_name(struct bt_mesh_node * node)2587 void bt_mesh_store_node_name(struct bt_mesh_node *node)
2588 {
2589     char node_name[BLE_MESH_NODE_NAME_SIZE + 1] = {0};
2590     char name[16] = {'\0'};
2591     int err = 0;
2592 
2593     if (node == NULL) {
2594         BT_ERR("Invalid node info");
2595         return;
2596     }
2597 
2598     strncpy(node_name, node->name, BLE_MESH_NODE_NAME_SIZE + 1);
2599 
2600     sprintf(name, "mesh/pn/%04x/n", node->unicast_addr);
2601     err = bt_mesh_save_core_settings(name, (const uint8_t *)node_name, BLE_MESH_NODE_NAME_SIZE);
2602     if (err) {
2603         BT_ERR("Failed to store node 0x%04x name", node->unicast_addr);
2604     }
2605 }
2606 
bt_mesh_store_node_comp_data(struct bt_mesh_node * node)2607 void bt_mesh_store_node_comp_data(struct bt_mesh_node *node)
2608 {
2609     char name[16] = {'\0'};
2610     int err = 0;
2611 
2612     if (!node || !node->comp_data || node->comp_length == 0U) {
2613         BT_ERR("Invalid node info");
2614         return;
2615     }
2616 
2617     sprintf(name, "mesh/pn/%04x/c", node->unicast_addr);
2618     err = bt_mesh_save_core_settings(name, (const uint8_t *)node->comp_data, node->comp_length);
2619     if (err) {
2620         BT_ERR("Failed to store node 0x%04x comp data", node->unicast_addr);
2621     }
2622 }
2623 #endif /* CONFIG_BLE_MESH_PROVISIONER */
2624 
settings_core_init(void)2625 int settings_core_init(void)
2626 {
2627     k_delayed_work_init(&pending_store, store_pending);
2628     return 0;
2629 }
2630 
bt_mesh_settings_init(void)2631 int bt_mesh_settings_init(void)
2632 {
2633     bt_mesh_settings_mutex_new();
2634     bt_mesh_settings_init_foreach();
2635     return 0;
2636 }
2637 
2638 #if CONFIG_BLE_MESH_DEINIT
settings_core_deinit(void)2639 int settings_core_deinit(void)
2640 {
2641     k_delayed_work_free(&pending_store);
2642     return 0;
2643 }
2644 
settings_core_erase(void)2645 int settings_core_erase(void)
2646 {
2647     /* Erase here must not use the pending_store timer.
2648      * This is used for erasing the information which
2649      * could not be erased during the previous deinit
2650      * operations.
2651      */
2652     bt_mesh_clear_net();
2653     bt_mesh_clear_seq();
2654     bt_mesh_clear_role();
2655     return 0;
2656 }
2657 
bt_mesh_settings_deinit(bool erase)2658 int bt_mesh_settings_deinit(bool erase)
2659 {
2660     bt_mesh_settings_deinit_foreach(erase);
2661     bt_mesh_settings_mutex_free();
2662     return 0;
2663 }
2664 #endif /* CONFIG_BLE_MESH_DEINIT */
2665 
bt_mesh_settings_reset(bool erase)2666 void bt_mesh_settings_reset(bool erase)
2667 {
2668     k_delayed_work_cancel(&pending_store);
2669     if (erase) {
2670         bt_mesh_clear_net();
2671         bt_mesh_clear_seq();
2672         bt_mesh_clear_role();
2673     }
2674 }
2675 
2676 #endif /* CONFIG_BLE_MESH_SETTINGS */
2677