1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <stdbool.h>
9 #include <errno.h>
10 
11 #include <zephyr/net/buf.h>
12 #include <zephyr/bluetooth/bluetooth.h>
13 #include <zephyr/bluetooth/conn.h>
14 #include <zephyr/bluetooth/uuid.h>
15 #include <zephyr/bluetooth/mesh.h>
16 
17 #include <zephyr/logging/log.h>
18 #include <common/bt_str.h>
19 
20 #include "test.h"
21 #include "prov.h"
22 #include "provisioner.h"
23 #include "net.h"
24 #include "subnet.h"
25 #include "app_keys.h"
26 #include "rpl.h"
27 #include "cfg.h"
28 #include "beacon.h"
29 #include "lpn.h"
30 #include "friend.h"
31 #include "transport.h"
32 #include "heartbeat.h"
33 #include "access.h"
34 #include "foundation.h"
35 #include "proxy.h"
36 #include "pb_gatt_srv.h"
37 #include "settings.h"
38 #include "mesh.h"
39 #include "solicitation.h"
40 #include "gatt_cli.h"
41 #include "crypto.h"
42 
43 LOG_MODULE_REGISTER(bt_mesh_main, CONFIG_BT_MESH_LOG_LEVEL);
44 
bt_mesh_provision(const uint8_t net_key[16],uint16_t net_idx,uint8_t flags,uint32_t iv_index,uint16_t addr,const uint8_t dev_key[16])45 int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
46 		      uint8_t flags, uint32_t iv_index, uint16_t addr,
47 		      const uint8_t dev_key[16])
48 {
49 	struct bt_mesh_key mesh_dev_key;
50 	struct bt_mesh_key mesh_net_key;
51 	bool is_net_key_valid = false;
52 	bool is_dev_key_valid = false;
53 	int err = 0;
54 
55 	if (!atomic_test_bit(bt_mesh.flags, BT_MESH_INIT)) {
56 		return -ENODEV;
57 	}
58 
59 	struct bt_mesh_cdb_subnet *subnet = NULL;
60 	struct bt_mesh_cdb_node *node = NULL;
61 
62 	LOG_INF("Primary Element: 0x%04x", addr);
63 	LOG_DBG("net_idx 0x%04x flags 0x%02x iv_index 0x%04x", net_idx, flags, iv_index);
64 
65 	if (atomic_test_and_set_bit(bt_mesh.flags, BT_MESH_VALID)) {
66 		return -EALREADY;
67 	}
68 
69 	if (IS_ENABLED(CONFIG_BT_MESH_CDB) &&
70 	    atomic_test_bit(bt_mesh_cdb.flags, BT_MESH_CDB_VALID)) {
71 		const struct bt_mesh_comp *comp;
72 		const struct bt_mesh_prov *prov;
73 
74 		comp = bt_mesh_comp_get();
75 		if (comp == NULL) {
76 			LOG_ERR("Failed to get node composition");
77 			atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
78 			return -EINVAL;
79 		}
80 
81 		subnet = bt_mesh_cdb_subnet_get(net_idx);
82 		if (!subnet) {
83 			LOG_ERR("No subnet with idx %d", net_idx);
84 			atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
85 			return -ENOENT;
86 		}
87 
88 		prov = bt_mesh_prov_get();
89 		node = bt_mesh_cdb_node_alloc(prov->uuid, addr,
90 					      comp->elem_count, net_idx);
91 		if (node == NULL) {
92 			LOG_ERR("Failed to allocate database node");
93 			atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
94 			return -ENOMEM;
95 		}
96 
97 		if (BT_MESH_KEY_REFRESH(flags)) {
98 			subnet->kr_phase = BT_MESH_KR_PHASE_2;
99 		} else {
100 			subnet->kr_phase = BT_MESH_KR_NORMAL;
101 		}
102 
103 		/* The primary network key has been imported during cdb creation.
104 		 * Importing here leaves it 'as is' if the key is the same.
105 		 * Otherwise, cdb replaces the old one with the new one.
106 		 */
107 		err = bt_mesh_cdb_subnet_key_import(subnet, BT_MESH_KEY_REFRESH(flags) ? 1 : 0,
108 						    net_key);
109 		if (err) {
110 			LOG_ERR("Failed to import cdb network key");
111 			goto end;
112 		}
113 		bt_mesh_cdb_subnet_store(subnet);
114 
115 		addr = node->addr;
116 		bt_mesh_cdb_iv_update(iv_index, BT_MESH_IV_UPDATE(flags));
117 
118 		err = bt_mesh_cdb_node_key_import(node, dev_key);
119 		if (err) {
120 			LOG_ERR("Failed to import cdb device key");
121 			goto end;
122 		}
123 
124 		if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
125 			bt_mesh_cdb_node_store(node);
126 		}
127 	}
128 
129 	err = bt_mesh_key_import(BT_MESH_KEY_TYPE_DEV, dev_key, &mesh_dev_key);
130 	if (err) {
131 		LOG_ERR("Failed to import device key");
132 		goto end;
133 	}
134 	is_dev_key_valid = true;
135 
136 	err = bt_mesh_key_import(BT_MESH_KEY_TYPE_NET, net_key, &mesh_net_key);
137 	if (err) {
138 		LOG_ERR("Failed to import network key");
139 		goto end;
140 	}
141 	is_net_key_valid = true;
142 
143 	err = bt_mesh_net_create(net_idx, flags, &mesh_net_key, iv_index);
144 	if (err) {
145 		atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
146 		goto end;
147 	}
148 
149 	bt_mesh_net_settings_commit();
150 
151 	bt_mesh.seq = 0U;
152 
153 	bt_mesh_comp_provision(addr);
154 
155 	memcpy(&bt_mesh.dev_key, &mesh_dev_key, sizeof(struct bt_mesh_key));
156 
157 	if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER) &&
158 	    IS_ENABLED(CONFIG_BT_MESH_LPN_SUB_ALL_NODES_ADDR)) {
159 		bt_mesh_lpn_group_add(BT_MESH_ADDR_ALL_NODES);
160 	}
161 
162 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
163 		bt_mesh_net_store();
164 	}
165 
166 	bt_mesh_start();
167 
168 end:
169 	if (err && node != NULL && IS_ENABLED(CONFIG_BT_MESH_CDB)) {
170 		bt_mesh_cdb_node_del(node, true);
171 	}
172 
173 	if (err && is_dev_key_valid) {
174 		bt_mesh_key_destroy(&mesh_dev_key);
175 	}
176 
177 	if (err && is_net_key_valid) {
178 		bt_mesh_key_destroy(&mesh_net_key);
179 	}
180 
181 	return err;
182 }
183 
184 #if defined(CONFIG_BT_MESH_RPR_SRV)
bt_mesh_reprovision(uint16_t addr)185 void bt_mesh_reprovision(uint16_t addr)
186 {
187 	LOG_DBG("0x%04x devkey: %s", addr,
188 		bt_hex(&bt_mesh.dev_key_cand, sizeof(struct bt_mesh_key)));
189 
190 	if (addr != bt_mesh_primary_addr()) {
191 		bt_mesh.seq = 0U;
192 
193 		bt_mesh_comp_provision(addr);
194 		bt_mesh_trans_reset();
195 
196 		if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) {
197 			bt_mesh_friends_clear();
198 		}
199 
200 		if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) {
201 			bt_mesh_lpn_friendship_end();
202 		}
203 	}
204 
205 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
206 		LOG_DBG("Storing network information persistently");
207 		bt_mesh_net_store();
208 		bt_mesh_net_seq_store(true);
209 		bt_mesh_comp_data_clear();
210 	}
211 }
212 
bt_mesh_dev_key_cand(const uint8_t * key)213 void bt_mesh_dev_key_cand(const uint8_t *key)
214 {
215 	int err;
216 
217 	LOG_DBG("%s", bt_hex(key, 16));
218 
219 	err = bt_mesh_key_import(BT_MESH_KEY_TYPE_DEV, key, &bt_mesh.dev_key_cand);
220 	if (err) {
221 		LOG_ERR("Failed to import device key candidate");
222 		return;
223 	}
224 
225 	atomic_set_bit(bt_mesh.flags, BT_MESH_DEVKEY_CAND);
226 
227 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
228 		bt_mesh_net_dev_key_cand_store();
229 	}
230 }
231 
bt_mesh_dev_key_cand_remove(void)232 void bt_mesh_dev_key_cand_remove(void)
233 {
234 	if (!atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_DEVKEY_CAND)) {
235 		return;
236 	}
237 
238 	LOG_DBG("");
239 
240 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
241 		bt_mesh_net_dev_key_cand_store();
242 	}
243 }
244 
bt_mesh_dev_key_cand_activate(void)245 void bt_mesh_dev_key_cand_activate(void)
246 {
247 	if (!atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_DEVKEY_CAND)) {
248 		return;
249 	}
250 
251 	bt_mesh_key_destroy(&bt_mesh.dev_key);
252 	memcpy(&bt_mesh.dev_key, &bt_mesh.dev_key_cand, sizeof(struct bt_mesh_key));
253 	memset(&bt_mesh.dev_key_cand, 0, sizeof(struct bt_mesh_key));
254 
255 	LOG_DBG("");
256 
257 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
258 		bt_mesh_net_pending_net_store();
259 		bt_mesh_net_dev_key_cand_store();
260 	}
261 }
262 #endif
263 
bt_mesh_provision_adv(const uint8_t uuid[16],uint16_t net_idx,uint16_t addr,uint8_t attention_duration)264 int bt_mesh_provision_adv(const uint8_t uuid[16], uint16_t net_idx,
265 			  uint16_t addr, uint8_t attention_duration)
266 {
267 	if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
268 		return -EINVAL;
269 	}
270 
271 	if (bt_mesh_subnet_get(net_idx) == NULL) {
272 		return -EINVAL;
273 	}
274 
275 	if (IS_ENABLED(CONFIG_BT_MESH_PROVISIONER) &&
276 	    IS_ENABLED(CONFIG_BT_MESH_PB_ADV)) {
277 		return bt_mesh_pb_adv_open(uuid, net_idx, addr,
278 					   attention_duration);
279 	}
280 
281 	return -ENOTSUP;
282 }
283 
bt_mesh_provision_gatt(const uint8_t uuid[16],uint16_t net_idx,uint16_t addr,uint8_t attention_duration)284 int bt_mesh_provision_gatt(const uint8_t uuid[16], uint16_t net_idx, uint16_t addr,
285 			   uint8_t attention_duration)
286 {
287 	if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
288 		return -EINVAL;
289 	}
290 
291 	if (bt_mesh_subnet_get(net_idx) == NULL) {
292 		return -EINVAL;
293 	}
294 
295 	if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT_CLIENT)) {
296 		return bt_mesh_pb_gatt_open(uuid, net_idx, addr,
297 					    attention_duration);
298 	}
299 
300 	return -ENOTSUP;
301 }
302 
bt_mesh_provision_remote(struct bt_mesh_rpr_cli * cli,const struct bt_mesh_rpr_node * srv,const uint8_t uuid[16],uint16_t net_idx,uint16_t addr)303 int bt_mesh_provision_remote(struct bt_mesh_rpr_cli *cli,
304 			     const struct bt_mesh_rpr_node *srv,
305 			     const uint8_t uuid[16], uint16_t net_idx,
306 			     uint16_t addr)
307 {
308 	if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
309 		return -EINVAL;
310 	}
311 
312 	if (bt_mesh_subnet_get(net_idx) == NULL) {
313 		return -EINVAL;
314 	}
315 
316 	if (IS_ENABLED(CONFIG_BT_MESH_PROVISIONER) &&
317 	    IS_ENABLED(CONFIG_BT_MESH_RPR_CLI)) {
318 		return bt_mesh_pb_remote_open(cli, srv, uuid, net_idx, addr);
319 	}
320 
321 	return -ENOTSUP;
322 }
323 
bt_mesh_reprovision_remote(struct bt_mesh_rpr_cli * cli,struct bt_mesh_rpr_node * srv,uint16_t addr,bool comp_change)324 int bt_mesh_reprovision_remote(struct bt_mesh_rpr_cli *cli,
325 			       struct bt_mesh_rpr_node *srv,
326 			       uint16_t addr, bool comp_change)
327 {
328 	if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
329 		return -EINVAL;
330 	}
331 
332 	if (IS_ENABLED(CONFIG_BT_MESH_PROVISIONER) &&
333 	    IS_ENABLED(CONFIG_BT_MESH_RPR_CLI)) {
334 		return bt_mesh_pb_remote_open_node(cli, srv, addr, comp_change);
335 	}
336 
337 	return -ENOTSUP;
338 }
339 
bt_mesh_reset(void)340 void bt_mesh_reset(void)
341 {
342 	if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID) ||
343 	    !atomic_test_bit(bt_mesh.flags, BT_MESH_INIT)) {
344 		return;
345 	}
346 
347 	bt_mesh.iv_index = 0U;
348 	bt_mesh.ivu_duration = 0;
349 	bt_mesh.seq = 0U;
350 
351 	memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));
352 	atomic_set_bit(bt_mesh.flags, BT_MESH_INIT);
353 
354 	bt_mesh_scan_disable();
355 
356 	/* If this fails, the work handler will return early on the next
357 	 * execution, as the device is not provisioned. If the device is
358 	 * reprovisioned, the timer is always restarted.
359 	 */
360 	(void)k_work_cancel_delayable(&bt_mesh.ivu_timer);
361 
362 	bt_mesh_access_reset();
363 	bt_mesh_model_reset();
364 	bt_mesh_cfg_default_set();
365 	bt_mesh_trans_reset();
366 	bt_mesh_app_keys_reset();
367 	bt_mesh_net_keys_reset();
368 
369 	bt_mesh_net_loopback_clear(BT_MESH_KEY_ANY);
370 
371 	if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) {
372 		if (IS_ENABLED(CONFIG_BT_MESH_LPN_SUB_ALL_NODES_ADDR)) {
373 			uint16_t group = BT_MESH_ADDR_ALL_NODES;
374 
375 			bt_mesh_lpn_group_del(&group, 1);
376 		}
377 
378 		bt_mesh_lpn_disable(true);
379 	}
380 
381 	if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) {
382 		bt_mesh_friends_clear();
383 	}
384 
385 	if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
386 		(void)bt_mesh_proxy_gatt_disable();
387 	}
388 
389 	if (IS_ENABLED(CONFIG_BT_MESH_GATT_CLIENT)) {
390 		bt_mesh_gatt_client_deinit();
391 	}
392 
393 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
394 		bt_mesh_net_clear();
395 	}
396 
397 	bt_mesh_key_destroy(&bt_mesh.dev_key);
398 	memset(&bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key));
399 
400 	bt_mesh_beacon_disable();
401 
402 	bt_mesh_comp_unprovision();
403 
404 	if (IS_ENABLED(CONFIG_BT_MESH_PROXY_SOLICITATION)) {
405 		bt_mesh_sol_reset();
406 	}
407 
408 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
409 		bt_mesh_settings_store_pending();
410 	}
411 
412 	if (IS_ENABLED(CONFIG_BT_MESH_PROV)) {
413 		bt_mesh_prov_reset();
414 	}
415 }
416 
bt_mesh_is_provisioned(void)417 bool bt_mesh_is_provisioned(void)
418 {
419 	return atomic_test_bit(bt_mesh.flags, BT_MESH_VALID);
420 }
421 
model_suspend(const struct bt_mesh_model * mod,const struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)422 static void model_suspend(const struct bt_mesh_model *mod, const struct bt_mesh_elem *elem,
423 			  bool vnd, bool primary, void *user_data)
424 {
425 	if (mod->pub && mod->pub->update) {
426 		mod->pub->count = 0U;
427 		/* If this fails, the work handler will check the suspend call
428 		 * and exit without transmitting.
429 		 */
430 		(void)k_work_cancel_delayable(&mod->pub->timer);
431 	}
432 }
433 
bt_mesh_suspend(void)434 int bt_mesh_suspend(void)
435 {
436 	int err;
437 
438 	if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
439 		return -EINVAL;
440 	}
441 
442 	if (atomic_test_and_set_bit(bt_mesh.flags, BT_MESH_SUSPENDED)) {
443 		return -EALREADY;
444 	}
445 
446 	err = bt_mesh_scan_disable();
447 	if (err) {
448 		atomic_clear_bit(bt_mesh.flags, BT_MESH_SUSPENDED);
449 		LOG_WRN("Disabling scanning failed (err %d)", err);
450 		return err;
451 	}
452 
453 	if (IS_ENABLED(CONFIG_BT_MESH_GATT_CLIENT)) {
454 		bt_mesh_proxy_disconnect(BT_MESH_KEY_ANY);
455 	}
456 
457 	bt_mesh_hb_suspend();
458 
459 	bt_mesh_beacon_disable();
460 
461 	bt_mesh_model_foreach(model_suspend, NULL);
462 
463 	bt_mesh_access_suspend();
464 
465 	if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) {
466 		err = bt_mesh_pb_gatt_srv_disable();
467 		if (err && err != -EALREADY) {
468 			LOG_WRN("Disabling PB-GATT failed (err %d)", err);
469 			return err;
470 		}
471 	}
472 
473 	if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
474 		err = bt_mesh_proxy_gatt_disable();
475 		if (err && err != -EALREADY) {
476 			LOG_WRN("Disabling GATT proxy failed (err %d)", err);
477 			return err;
478 		}
479 	}
480 
481 	err = bt_mesh_adv_disable();
482 	if (err) {
483 		atomic_clear_bit(bt_mesh.flags, BT_MESH_SUSPENDED);
484 		LOG_WRN("Disabling advertisers failed (err %d)", err);
485 		return err;
486 	}
487 
488 	return 0;
489 }
490 
model_resume(const struct bt_mesh_model * mod,const struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)491 static void model_resume(const struct bt_mesh_model *mod, const struct bt_mesh_elem *elem,
492 			  bool vnd, bool primary, void *user_data)
493 {
494 	if (mod->pub && mod->pub->update) {
495 		int32_t period_ms = bt_mesh_model_pub_period_get(mod);
496 
497 		if (period_ms) {
498 			k_work_reschedule(&mod->pub->timer,
499 					  K_MSEC(period_ms));
500 		}
501 	}
502 }
503 
bt_mesh_resume(void)504 int bt_mesh_resume(void)
505 {
506 	int err;
507 
508 	if (!atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
509 		return -EINVAL;
510 	}
511 
512 	if (!atomic_test_and_clear_bit(bt_mesh.flags, BT_MESH_SUSPENDED)) {
513 		return -EALREADY;
514 	}
515 
516 	if (!IS_ENABLED(CONFIG_BT_EXT_ADV)) {
517 		bt_mesh_adv_init();
518 	}
519 
520 	err = bt_mesh_adv_enable();
521 	if (err) {
522 		atomic_set_bit(bt_mesh.flags, BT_MESH_SUSPENDED);
523 		LOG_WRN("Re-enabling advertisers failed (err %d)", err);
524 		return err;
525 	}
526 
527 	if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY) && bt_mesh_is_provisioned()) {
528 		err = bt_mesh_proxy_gatt_enable();
529 		if (err) {
530 			LOG_WRN("Re-enabling GATT proxy failed (err %d)", err);
531 			return err;
532 		}
533 	}
534 
535 	if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT) && !bt_mesh_is_provisioned()) {
536 		err = bt_mesh_pb_gatt_srv_enable();
537 		if (err) {
538 			LOG_WRN("Re-enabling PB-GATT failed (err %d)", err);
539 			return err;
540 		}
541 	}
542 
543 	err = bt_mesh_scan_enable();
544 	if (err) {
545 		LOG_WRN("Re-enabling scanning failed (err %d)", err);
546 		atomic_set_bit(bt_mesh.flags, BT_MESH_SUSPENDED);
547 		return err;
548 	}
549 
550 	bt_mesh_hb_resume();
551 
552 	if (bt_mesh_beacon_enabled() ||
553 	    bt_mesh_priv_beacon_get() == BT_MESH_PRIV_BEACON_ENABLED) {
554 		bt_mesh_beacon_enable();
555 	}
556 
557 	bt_mesh_model_foreach(model_resume, NULL);
558 
559 	err = bt_mesh_adv_gatt_send();
560 	if (err && (err != -ENOTSUP)) {
561 		LOG_WRN("GATT send failed (err %d)", err);
562 		return err;
563 	}
564 
565 	return 0;
566 }
567 
bt_mesh_init(const struct bt_mesh_prov * prov,const struct bt_mesh_comp * comp)568 int bt_mesh_init(const struct bt_mesh_prov *prov,
569 		 const struct bt_mesh_comp *comp)
570 {
571 	int err;
572 
573 	if (atomic_test_and_set_bit(bt_mesh.flags, BT_MESH_INIT)) {
574 		return -EALREADY;
575 	}
576 
577 	err = bt_mesh_test();
578 	if (err) {
579 		return err;
580 	}
581 
582 	err = bt_mesh_crypto_init();
583 	if (err) {
584 		return err;
585 	}
586 
587 	err = bt_mesh_comp_register(comp);
588 	if (err) {
589 		return err;
590 	}
591 
592 	if (IS_ENABLED(CONFIG_BT_MESH_PROV)) {
593 		err = bt_mesh_prov_init(prov);
594 		if (err) {
595 			return err;
596 		}
597 	}
598 
599 	bt_mesh_cfg_default_set();
600 	bt_mesh_net_init();
601 	bt_mesh_trans_init();
602 	bt_mesh_access_init();
603 	bt_mesh_hb_init();
604 	bt_mesh_beacon_init();
605 	bt_mesh_adv_init();
606 
607 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
608 		bt_mesh_settings_init();
609 	}
610 
611 	return 0;
612 }
613 
model_start(const struct bt_mesh_model * mod,const struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)614 static void model_start(const struct bt_mesh_model *mod, const struct bt_mesh_elem *elem,
615 			bool vnd, bool primary, void *user_data)
616 {
617 	if (mod->cb && mod->cb->start) {
618 		mod->cb->start(mod);
619 	}
620 }
621 
bt_mesh_start(void)622 int bt_mesh_start(void)
623 {
624 	int err;
625 
626 	err = bt_mesh_adv_enable();
627 	if (err) {
628 		LOG_ERR("Failed enabling advertiser");
629 		return err;
630 	}
631 
632 
633 	if (bt_mesh_beacon_enabled() ||
634 	    bt_mesh_priv_beacon_get() == BT_MESH_PRIV_BEACON_ENABLED) {
635 		bt_mesh_beacon_enable();
636 	}
637 
638 	if (!IS_ENABLED(CONFIG_BT_MESH_PROV) || !bt_mesh_prov_active() ||
639 	    bt_mesh_prov_link.bearer->type == BT_MESH_PROV_ADV) {
640 		if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) {
641 			(void)bt_mesh_pb_gatt_srv_disable();
642 		}
643 
644 		if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
645 			(void)bt_mesh_proxy_gatt_enable();
646 		}
647 	}
648 
649 	if (IS_ENABLED(CONFIG_BT_MESH_GATT_CLIENT)) {
650 		bt_mesh_gatt_client_init();
651 	}
652 
653 	if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER)) {
654 		bt_mesh_lpn_init();
655 	} else {
656 		bt_mesh_scan_enable();
657 	}
658 
659 	if (IS_ENABLED(CONFIG_BT_MESH_FRIEND)) {
660 		bt_mesh_friend_init();
661 	}
662 
663 	if (IS_ENABLED(CONFIG_BT_MESH_PROV)) {
664 		struct bt_mesh_subnet *sub = bt_mesh_subnet_next(NULL);
665 		uint16_t addr = bt_mesh_primary_addr();
666 
667 		bt_mesh_prov_complete(sub->net_idx, addr);
668 	}
669 
670 	bt_mesh_hb_start();
671 
672 	bt_mesh_model_foreach(model_start, NULL);
673 
674 	return 0;
675 }
676