main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /* Bluetooth Mesh */
  2. /*
  3. * SPDX-FileCopyrightText: 2017 Intel Corporation
  4. * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. */
  8. #include <string.h>
  9. #include <errno.h>
  10. #include "adv.h"
  11. #include "scan.h"
  12. #include "beacon.h"
  13. #include "lpn.h"
  14. #include "rpl.h"
  15. #include "friend.h"
  16. #include "transport.h"
  17. #include "access.h"
  18. #include "foundation.h"
  19. #include "settings.h"
  20. #include "mesh.h"
  21. #include "mesh/hci.h"
  22. #include "mesh/common.h"
  23. #include "proxy_client.h"
  24. #include "proxy_server.h"
  25. #include "prov_common.h"
  26. #include "prov_node.h"
  27. #include "prov_pvnr.h"
  28. #include "pvnr_mgmt.h"
  29. #include "mesh_v1.1/utils.h"
  30. static bool mesh_init = false;
  31. bool bt_mesh_is_initialized(void)
  32. {
  33. return mesh_init;
  34. }
  35. int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
  36. uint8_t flags, uint32_t iv_index, uint16_t addr,
  37. const uint8_t dev_key[16])
  38. {
  39. bool pb_gatt_enabled = false;
  40. int err = 0;
  41. BT_INFO("Primary Element: 0x%04x", addr);
  42. BT_INFO("net_idx 0x%04x flags 0x%02x iv_index 0x%04x",
  43. net_idx, flags, iv_index);
  44. BT_INFO("DevKey %s", bt_hex(dev_key, 16));
  45. BT_INFO("NetKey %s", bt_hex(net_key, 16));
  46. if (bt_mesh_atomic_test_and_set_bit(bt_mesh.flags, BLE_MESH_VALID)) {
  47. BT_ERR("Already enter network!");
  48. return -EALREADY;
  49. }
  50. if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
  51. if (bt_mesh_proxy_server_prov_disable(false) == 0) {
  52. pb_gatt_enabled = true;
  53. } else {
  54. pb_gatt_enabled = false;
  55. }
  56. } else {
  57. pb_gatt_enabled = false;
  58. }
  59. err = bt_mesh_net_create(net_idx, flags, net_key, iv_index);
  60. if (err) {
  61. BT_ERR("Create network for node failed");
  62. bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID);
  63. if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && pb_gatt_enabled) {
  64. bt_mesh_proxy_server_prov_enable();
  65. }
  66. return err;
  67. }
  68. bt_mesh.seq = 0U;
  69. bt_mesh_comp_provision(addr);
  70. memcpy(bt_mesh.dev_key, dev_key, 16);
  71. if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) &&
  72. IS_ENABLED(CONFIG_BLE_MESH_LPN_SUB_ALL_NODES_ADDR)) {
  73. bt_mesh_lpn_group_add(BLE_MESH_ADDR_ALL_NODES);
  74. }
  75. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  76. BT_DBG("Storing network information persistently");
  77. bt_mesh_store_net();
  78. bt_mesh_store_subnet(&bt_mesh.sub[0]);
  79. bt_mesh_store_iv(false);
  80. }
  81. bt_mesh_net_start();
  82. return 0;
  83. }
  84. void bt_mesh_node_reset(void)
  85. {
  86. if (!bt_mesh_is_provisioned()) {
  87. BT_WARN("%s, Not provisioned", __func__);
  88. return;
  89. }
  90. bt_mesh.iv_index = 0U;
  91. bt_mesh.seq = 0U;
  92. bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID);
  93. k_delayed_work_cancel(&bt_mesh.ivu_timer);
  94. bt_mesh_cfg_reset(true);
  95. bt_mesh_rx_reset();
  96. bt_mesh_tx_reset();
  97. bt_mesh_rpl_reset(true);
  98. if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
  99. if (IS_ENABLED(CONFIG_BLE_MESH_LPN_SUB_ALL_NODES_ADDR)) {
  100. uint16_t group = BLE_MESH_ADDR_ALL_NODES;
  101. bt_mesh_lpn_group_del(&group, 1);
  102. }
  103. bt_mesh_lpn_disable(true);
  104. }
  105. if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
  106. bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY);
  107. }
  108. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
  109. bt_mesh_proxy_server_gatt_disable();
  110. }
  111. (void)memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key));
  112. bt_mesh_scan_disable();
  113. bt_mesh_secure_beacon_disable();
  114. #if CONFIG_BLE_MESH_PRB_SRV
  115. bt_mesh_private_beacon_disable();
  116. #endif
  117. bt_mesh_comp_unprovision();
  118. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  119. bt_mesh_clear_net();
  120. bt_mesh_clear_seq();
  121. bt_mesh_clear_dkca();
  122. bt_mesh_clear_role();
  123. if (IS_ENABLED(CONFIG_BLE_MESH_DF_SRV)) {
  124. bt_mesh_clear_all_directed_forwarding_table_data();
  125. }
  126. }
  127. memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));
  128. if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) {
  129. bt_mesh_prov_reset();
  130. }
  131. }
  132. bool bt_mesh_is_node(void)
  133. {
  134. return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_NODE);
  135. }
  136. bool bt_mesh_is_provisioned(void)
  137. {
  138. if (bt_mesh_is_node()) {
  139. return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID);
  140. } else {
  141. return false;
  142. }
  143. }
  144. bool bt_mesh_is_provisioner(void)
  145. {
  146. return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_PROVISIONER);
  147. }
  148. bool bt_mesh_is_provisioner_en(void)
  149. {
  150. if (bt_mesh_is_provisioner()) {
  151. return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);
  152. }
  153. return false;
  154. }
  155. static bool prov_bearers_valid(bt_mesh_prov_bearer_t bearers)
  156. {
  157. if ((!(bearers & (BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT))) ||
  158. (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
  159. !IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
  160. !(bearers & BLE_MESH_PROV_ADV)) ||
  161. (!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
  162. IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
  163. !(bearers & BLE_MESH_PROV_GATT))) {
  164. BT_ERR("Invalid bearers 0x%02x", bearers);
  165. return false;
  166. }
  167. return true;
  168. }
  169. int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
  170. {
  171. int err = 0;
  172. if (bt_mesh_is_provisioned()) {
  173. BT_WARN("%s, Already", __func__);
  174. return -EALREADY;
  175. }
  176. if (prov_bearers_valid(bearers) == false) {
  177. return -EINVAL;
  178. }
  179. /* Add this judgement here in case the device worked as a
  180. * Provisioner previously. Before the corresponding info
  181. * of Provisioner is erased from flash, users try to use
  182. * the device as a node, which will cause the information
  183. * in NVS been handled incorrectly.
  184. */
  185. uint8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK;
  186. if (role != BLE_MESH_SETTINGS_ROLE_NONE &&
  187. role != BLE_MESH_SETTINGS_ROLE_NODE) {
  188. BT_ERR("%s, Mismatch role %u", __func__, role);
  189. return -EIO;
  190. }
  191. if (role == BLE_MESH_SETTINGS_ROLE_NONE) {
  192. bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE);
  193. }
  194. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS_BACKWARD_COMPATIBILITY) ||
  195. role == BLE_MESH_SETTINGS_ROLE_NONE) {
  196. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  197. bt_mesh_store_role();
  198. }
  199. }
  200. if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
  201. (bearers & BLE_MESH_PROV_ADV)) {
  202. /* Make sure we're scanning for provisioning invitations */
  203. err = bt_mesh_scan_enable();
  204. if (err) {
  205. return err;
  206. }
  207. /* Enable unprovisioned beacon sending */
  208. bt_mesh_secure_beacon_enable();
  209. }
  210. if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
  211. (bearers & BLE_MESH_PROV_GATT)) {
  212. bt_mesh_proxy_server_prov_enable();
  213. bt_mesh_adv_update();
  214. }
  215. return 0;
  216. }
  217. int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)
  218. {
  219. if (bt_mesh_is_provisioned()) {
  220. BT_WARN("%s, Already provisioned", __func__);
  221. return -EALREADY;
  222. }
  223. if (prov_bearers_valid(bearers) == false) {
  224. return -EINVAL;
  225. }
  226. bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_NODE);
  227. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  228. bt_mesh_clear_role();
  229. }
  230. if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
  231. (bearers & BLE_MESH_PROV_ADV)) {
  232. bt_mesh_secure_beacon_disable();
  233. bt_mesh_scan_disable();
  234. }
  235. if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
  236. (bearers & BLE_MESH_PROV_GATT)) {
  237. bt_mesh_proxy_server_prov_disable(true);
  238. }
  239. return 0;
  240. }
  241. static void model_suspend(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
  242. bool vnd, bool primary, void *user_data)
  243. {
  244. if (mod->pub && mod->pub->update) {
  245. mod->pub->count = 0U;
  246. k_delayed_work_cancel(&mod->pub->timer);
  247. }
  248. }
  249. int bt_mesh_suspend(void)
  250. {
  251. int err = 0;
  252. if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) {
  253. return -EINVAL;
  254. }
  255. if (bt_mesh_atomic_test_and_set_bit(bt_mesh.flags, BLE_MESH_SUSPENDED)) {
  256. return -EALREADY;
  257. }
  258. err = bt_mesh_scan_disable();
  259. if (err) {
  260. bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_SUSPENDED);
  261. BT_WARN("Disabling scanning failed (err %d)", err);
  262. return err;
  263. }
  264. bt_mesh_hb_pub_disable();
  265. if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) {
  266. bt_mesh_secure_beacon_disable();
  267. }
  268. #if CONFIG_BLE_MESH_PRB_SRV
  269. if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
  270. bt_mesh_private_beacon_disable();
  271. }
  272. #endif
  273. bt_mesh_model_foreach(model_suspend, NULL);
  274. return 0;
  275. }
  276. static void model_resume(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
  277. bool vnd, bool primary, void *user_data)
  278. {
  279. if (mod->pub && mod->pub->update) {
  280. int32_t period_ms = bt_mesh_model_pub_period_get(mod);
  281. if (period_ms) {
  282. k_delayed_work_submit(&mod->pub->timer, period_ms);
  283. }
  284. }
  285. }
  286. int bt_mesh_resume(void)
  287. {
  288. int err = 0;
  289. if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) {
  290. return -EINVAL;
  291. }
  292. if (!bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_SUSPENDED)) {
  293. return -EALREADY;
  294. }
  295. err = bt_mesh_scan_enable();
  296. if (err) {
  297. BT_WARN("Re-enabling scanning failed (err %d)", err);
  298. bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_SUSPENDED);
  299. return err;
  300. }
  301. if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) {
  302. bt_mesh_secure_beacon_enable();
  303. }
  304. #if CONFIG_BLE_MESH_PRB_SRV
  305. if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
  306. bt_mesh_private_beacon_enable();
  307. }
  308. #endif
  309. bt_mesh_model_foreach(model_resume, NULL);
  310. return err;
  311. }
  312. int bt_mesh_init(const struct bt_mesh_prov *prov,
  313. const struct bt_mesh_comp *comp)
  314. {
  315. int err = 0;
  316. if (mesh_init == true) {
  317. BT_WARN("%s, Already", __func__);
  318. return -EALREADY;
  319. }
  320. extern int bt_mesh_v11_ext_init(void);
  321. err = bt_mesh_v11_ext_init();
  322. if (err) {
  323. BT_ERR("Bluetooth Mesh v1.1 init failed");
  324. return err;
  325. }
  326. bt_mesh_mutex_init();
  327. bt_mesh_timer_init();
  328. bt_mesh_hci_init();
  329. bt_mesh_adapt_init();
  330. err = bt_mesh_comp_register(comp);
  331. if (err) {
  332. return err;
  333. }
  334. if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) {
  335. bt_mesh_gatt_init();
  336. }
  337. if ((IS_ENABLED(CONFIG_BLE_MESH_NODE) &&
  338. IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) ||
  339. IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
  340. bt_mesh_proxy_server_init();
  341. }
  342. if ((IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) &&
  343. IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) ||
  344. IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) {
  345. bt_mesh_proxy_client_init();
  346. }
  347. #if CONFIG_BLE_MESH_PROXY_SOLIC
  348. err = bt_mesh_proxy_solic_init();
  349. if (err) {
  350. return err;
  351. }
  352. #endif
  353. if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) {
  354. err = bt_mesh_prov_set(prov);
  355. if (err) {
  356. return err;
  357. }
  358. if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) {
  359. err = bt_mesh_prov_init();
  360. if (err) {
  361. return err;
  362. }
  363. }
  364. if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) {
  365. err = bt_mesh_provisioner_prov_init();
  366. if (err) {
  367. return err;
  368. }
  369. }
  370. }
  371. bt_mesh_net_init();
  372. bt_mesh_trans_init();
  373. /* Changed by Espressif, add a random delay (0 ~ 3s) */
  374. if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) {
  375. uint32_t delay = 0;
  376. bt_mesh_rand(&delay, sizeof(uint32_t));
  377. vTaskDelay((delay % 3000) / portTICK_PERIOD_MS);
  378. }
  379. bt_mesh_beacon_init();
  380. bt_mesh_adv_init();
  381. if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) {
  382. bt_mesh_provisioner_init();
  383. }
  384. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  385. bt_mesh_settings_init();
  386. }
  387. mesh_init = true;
  388. return 0;
  389. }
  390. #if CONFIG_BLE_MESH_DEINIT
  391. int bt_mesh_deinit(struct bt_mesh_deinit_param *param)
  392. {
  393. int err = 0;
  394. if (param == NULL) {
  395. BT_ERR("%s, Invalid parameter", __func__);
  396. return -EINVAL;
  397. }
  398. if (mesh_init == false) {
  399. BT_WARN("%s, Already", __func__);
  400. return -EALREADY;
  401. }
  402. bt_mesh_scan_disable();
  403. bt_mesh_secure_beacon_disable();
  404. #if CONFIG_BLE_MESH_PRB_SRV
  405. bt_mesh_private_beacon_disable();
  406. #endif
  407. if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) {
  408. if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
  409. !bt_mesh_is_provisioned()) {
  410. bt_mesh_proxy_server_prov_disable(true);
  411. }
  412. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
  413. bt_mesh_is_provisioned()) {
  414. bt_mesh_proxy_server_gatt_disable();
  415. }
  416. if (bt_mesh_is_provisioned()) {
  417. /* Clear valid flag here in order to perform settings erase */
  418. bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID);
  419. bt_mesh_cfg_reset(param->erase);
  420. }
  421. }
  422. if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) {
  423. if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
  424. bt_mesh_proxy_client_prov_disable();
  425. }
  426. /* Clear valid flag here in order to perform settings erase */
  427. bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);
  428. }
  429. if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) {
  430. if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) {
  431. err = bt_mesh_prov_deinit();
  432. if (err) {
  433. return err;
  434. }
  435. }
  436. if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) {
  437. err = bt_mesh_provisioner_prov_deinit(param->erase);
  438. if (err) {
  439. return err;
  440. }
  441. err = bt_mesh_provisioner_deinit(param->erase);
  442. if (err) {
  443. return err;
  444. }
  445. }
  446. bt_mesh_prov_set(NULL);
  447. }
  448. bt_mesh_trans_deinit(param->erase);
  449. bt_mesh_net_deinit();
  450. bt_mesh_beacon_deinit();
  451. #if CONFIG_BLE_MESH_PROXY_SOLIC
  452. bt_mesh_proxy_solic_deinit();
  453. #endif
  454. if ((IS_ENABLED(CONFIG_BLE_MESH_NODE) &&
  455. IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) ||
  456. IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
  457. bt_mesh_proxy_server_deinit();
  458. }
  459. if ((IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) &&
  460. IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) ||
  461. IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) {
  462. bt_mesh_proxy_client_deinit();
  463. }
  464. bt_mesh_gatt_deinit();
  465. if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
  466. bt_mesh_friend_deinit();
  467. }
  468. if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
  469. bt_mesh_lpn_deinit();
  470. }
  471. bt_mesh_adv_deinit();
  472. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  473. bt_mesh_settings_deinit(param->erase);
  474. }
  475. err = bt_mesh_comp_deregister();
  476. if (err) {
  477. return err;
  478. }
  479. bt_mesh_comp_unprovision();
  480. memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));
  481. bt_mesh_timer_deinit();
  482. bt_mesh_mutex_deinit();
  483. mesh_init = false;
  484. return 0;
  485. }
  486. #endif /* CONFIG_BLE_MESH_DEINIT */
  487. #if CONFIG_BLE_MESH_PROVISIONER
  488. int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)
  489. {
  490. int err = 0;
  491. if (bt_mesh_is_provisioner_en()) {
  492. BT_WARN("%s, Already", __func__);
  493. return -EALREADY;
  494. }
  495. if (prov_bearers_valid(bearers) == false) {
  496. return -EINVAL;
  497. }
  498. /* Add this judgement here in case the device worked as a
  499. * node previously. Before the corresponding information
  500. * of the node is erased from flash, users try to use the
  501. * device as a Provisioner, which will cause the information
  502. * in NVS been handled incorrectly.
  503. */
  504. uint8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK;
  505. if (role != BLE_MESH_SETTINGS_ROLE_NONE &&
  506. role != BLE_MESH_SETTINGS_ROLE_PROV) {
  507. BT_ERR("%s, Mismatch role %u", __func__, role);
  508. return -EIO;
  509. }
  510. if (role == BLE_MESH_SETTINGS_ROLE_NONE) {
  511. bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_PROVISIONER);
  512. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  513. bt_mesh_store_role();
  514. }
  515. }
  516. /* Enable Provisioner here, because during the following net
  517. * creation, some information needs to be stored in flash.
  518. */
  519. bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);
  520. err = bt_mesh_provisioner_net_create();
  521. if (err) {
  522. BT_ERR("Failed to create network");
  523. return err;
  524. }
  525. err = bt_mesh_provisioner_init_prov_info();
  526. if (err) {
  527. BT_ERR("Failed to init prov info");
  528. return err;
  529. }
  530. bt_mesh_provisioner_set_prov_bearer(bearers, false);
  531. bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr());
  532. #if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
  533. if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
  534. (bearers & BLE_MESH_PROV_ADV)) {
  535. bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD,
  536. BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON,
  537. NULL);
  538. }
  539. if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
  540. (bearers & BLE_MESH_PROV_GATT)) {
  541. bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD,
  542. BLE_MESH_EXCEP_LIST_TYPE_MESH_PROV_ADV,
  543. NULL);
  544. }
  545. #endif
  546. if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
  547. (bearers & BLE_MESH_PROV_GATT)) {
  548. bt_mesh_proxy_client_prov_enable();
  549. }
  550. if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
  551. bt_mesh_friend_init();
  552. }
  553. if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) {
  554. bt_mesh_secure_beacon_enable();
  555. }
  556. #if CONFIG_BLE_MESH_PRB_SRV
  557. if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
  558. bt_mesh_private_beacon_enable();
  559. }
  560. #endif
  561. err = bt_mesh_scan_enable();
  562. if (err) {
  563. return err;
  564. }
  565. return 0;
  566. }
  567. int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers)
  568. {
  569. bt_mesh_prov_bearer_t enable = 0U;
  570. if (!bt_mesh_is_provisioner_en()) {
  571. BT_WARN("%s, Already", __func__);
  572. return -EALREADY;
  573. }
  574. if (prov_bearers_valid(bearers) == false) {
  575. return -EINVAL;
  576. }
  577. enable = bt_mesh_provisioner_get_prov_bearer();
  578. if (!(enable & bearers)) {
  579. BT_ERR("Mismatch bearers 0x%02x", bearers);
  580. return -EINVAL;
  581. }
  582. bt_mesh_provisioner_set_prov_bearer(bearers, true);
  583. if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
  584. (enable & BLE_MESH_PROV_GATT) &&
  585. (bearers & BLE_MESH_PROV_GATT)) {
  586. bt_mesh_proxy_client_prov_disable();
  587. #if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
  588. bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE,
  589. BLE_MESH_EXCEP_LIST_TYPE_MESH_PROV_ADV,
  590. NULL);
  591. #endif
  592. }
  593. if (!(enable & (~bearers))) {
  594. /* Provisioner is disabled completely, disable scan here */
  595. bt_mesh_scan_disable();
  596. #if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
  597. if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
  598. (enable & BLE_MESH_PROV_ADV)) {
  599. bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE,
  600. BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON,
  601. NULL);
  602. }
  603. #endif
  604. /* Clear corresponding flags */
  605. bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV)));
  606. /* When Provisioner is disabled, the device role indicated by bt_mesh.flags
  607. * will not be cleared, because when Provisioner is restarted after disabled,
  608. * its previous information can be recovered from flash properly.
  609. */
  610. if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) {
  611. bt_mesh_secure_beacon_disable();
  612. }
  613. #if CONFIG_BLE_MESH_PRB_SRV
  614. if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
  615. bt_mesh_private_beacon_disable();
  616. }
  617. #endif
  618. if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
  619. bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY);
  620. }
  621. }
  622. return 0;
  623. }
  624. #endif /* CONFIG_BLE_MESH_PROVISIONER */