| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793 |
- /* Bluetooth Mesh */
- /*
- * SPDX-FileCopyrightText: 2017 Intel Corporation
- * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #include <string.h>
- #include <errno.h>
- #include "adv.h"
- #include "scan.h"
- #include "beacon.h"
- #include "lpn.h"
- #include "rpl.h"
- #include "friend.h"
- #include "transport.h"
- #include "access.h"
- #include "foundation.h"
- #include "settings.h"
- #include "mesh.h"
- #include "mesh/hci.h"
- #include "mesh/common.h"
- #include "proxy_client.h"
- #include "proxy_server.h"
- #include "prov_common.h"
- #include "prov_node.h"
- #include "prov_pvnr.h"
- #include "pvnr_mgmt.h"
- #include "mesh_v1.1/utils.h"
- static bool mesh_init = false;
- bool bt_mesh_is_initialized(void)
- {
- return mesh_init;
- }
- int 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])
- {
- bool pb_gatt_enabled = false;
- int err = 0;
- BT_INFO("Primary Element: 0x%04x", addr);
- BT_INFO("net_idx 0x%04x flags 0x%02x iv_index 0x%04x",
- net_idx, flags, iv_index);
- BT_INFO("DevKey %s", bt_hex(dev_key, 16));
- BT_INFO("NetKey %s", bt_hex(net_key, 16));
- if (bt_mesh_atomic_test_and_set_bit(bt_mesh.flags, BLE_MESH_VALID)) {
- BT_ERR("Already enter network!");
- return -EALREADY;
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
- if (bt_mesh_proxy_server_prov_disable(false) == 0) {
- pb_gatt_enabled = true;
- } else {
- pb_gatt_enabled = false;
- }
- } else {
- pb_gatt_enabled = false;
- }
- err = bt_mesh_net_create(net_idx, flags, net_key, iv_index);
- if (err) {
- BT_ERR("Create network for node failed");
- bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID);
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && pb_gatt_enabled) {
- bt_mesh_proxy_server_prov_enable();
- }
- return err;
- }
- bt_mesh.seq = 0U;
- bt_mesh_comp_provision(addr);
- memcpy(bt_mesh.dev_key, dev_key, 16);
- if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) &&
- IS_ENABLED(CONFIG_BLE_MESH_LPN_SUB_ALL_NODES_ADDR)) {
- bt_mesh_lpn_group_add(BLE_MESH_ADDR_ALL_NODES);
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
- BT_DBG("Storing network information persistently");
- bt_mesh_store_net();
- bt_mesh_store_subnet(&bt_mesh.sub[0]);
- bt_mesh_store_iv(false);
- }
- bt_mesh_net_start();
- return 0;
- }
- void bt_mesh_node_reset(void)
- {
- if (!bt_mesh_is_provisioned()) {
- BT_WARN("%s, Not provisioned", __func__);
- return;
- }
- bt_mesh.iv_index = 0U;
- bt_mesh.seq = 0U;
- bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID);
- k_delayed_work_cancel(&bt_mesh.ivu_timer);
- bt_mesh_cfg_reset(true);
- bt_mesh_rx_reset();
- bt_mesh_tx_reset();
- bt_mesh_rpl_reset(true);
- if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
- if (IS_ENABLED(CONFIG_BLE_MESH_LPN_SUB_ALL_NODES_ADDR)) {
- uint16_t group = BLE_MESH_ADDR_ALL_NODES;
- bt_mesh_lpn_group_del(&group, 1);
- }
- bt_mesh_lpn_disable(true);
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
- bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY);
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
- bt_mesh_proxy_server_gatt_disable();
- }
- (void)memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key));
- bt_mesh_scan_disable();
- bt_mesh_secure_beacon_disable();
- #if CONFIG_BLE_MESH_PRB_SRV
- bt_mesh_private_beacon_disable();
- #endif
- bt_mesh_comp_unprovision();
- if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
- bt_mesh_clear_net();
- bt_mesh_clear_seq();
- bt_mesh_clear_dkca();
- bt_mesh_clear_role();
- if (IS_ENABLED(CONFIG_BLE_MESH_DF_SRV)) {
- bt_mesh_clear_all_directed_forwarding_table_data();
- }
- }
- memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));
- if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) {
- bt_mesh_prov_reset();
- }
- }
- bool bt_mesh_is_node(void)
- {
- return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_NODE);
- }
- bool bt_mesh_is_provisioned(void)
- {
- if (bt_mesh_is_node()) {
- return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID);
- } else {
- return false;
- }
- }
- bool bt_mesh_is_provisioner(void)
- {
- return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_PROVISIONER);
- }
- bool bt_mesh_is_provisioner_en(void)
- {
- if (bt_mesh_is_provisioner()) {
- return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);
- }
- return false;
- }
- static bool prov_bearers_valid(bt_mesh_prov_bearer_t bearers)
- {
- if ((!(bearers & (BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT))) ||
- (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
- !IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
- !(bearers & BLE_MESH_PROV_ADV)) ||
- (!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
- IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
- !(bearers & BLE_MESH_PROV_GATT))) {
- BT_ERR("Invalid bearers 0x%02x", bearers);
- return false;
- }
- return true;
- }
- int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
- {
- int err = 0;
- if (bt_mesh_is_provisioned()) {
- BT_WARN("%s, Already", __func__);
- return -EALREADY;
- }
- if (prov_bearers_valid(bearers) == false) {
- return -EINVAL;
- }
- /* Add this judgement here in case the device worked as a
- * Provisioner previously. Before the corresponding info
- * of Provisioner is erased from flash, users try to use
- * the device as a node, which will cause the information
- * in NVS been handled incorrectly.
- */
- uint8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK;
- if (role != BLE_MESH_SETTINGS_ROLE_NONE &&
- role != BLE_MESH_SETTINGS_ROLE_NODE) {
- BT_ERR("%s, Mismatch role %u", __func__, role);
- return -EIO;
- }
- if (role == BLE_MESH_SETTINGS_ROLE_NONE) {
- bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE);
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS_BACKWARD_COMPATIBILITY) ||
- role == BLE_MESH_SETTINGS_ROLE_NONE) {
- if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
- bt_mesh_store_role();
- }
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
- (bearers & BLE_MESH_PROV_ADV)) {
- /* Make sure we're scanning for provisioning invitations */
- err = bt_mesh_scan_enable();
- if (err) {
- return err;
- }
- /* Enable unprovisioned beacon sending */
- bt_mesh_secure_beacon_enable();
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
- (bearers & BLE_MESH_PROV_GATT)) {
- bt_mesh_proxy_server_prov_enable();
- bt_mesh_adv_update();
- }
- return 0;
- }
- int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)
- {
- if (bt_mesh_is_provisioned()) {
- BT_WARN("%s, Already provisioned", __func__);
- return -EALREADY;
- }
- if (prov_bearers_valid(bearers) == false) {
- return -EINVAL;
- }
- bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_NODE);
- if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
- bt_mesh_clear_role();
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
- (bearers & BLE_MESH_PROV_ADV)) {
- bt_mesh_secure_beacon_disable();
- bt_mesh_scan_disable();
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
- (bearers & BLE_MESH_PROV_GATT)) {
- bt_mesh_proxy_server_prov_disable(true);
- }
- return 0;
- }
- static void model_suspend(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
- bool vnd, bool primary, void *user_data)
- {
- if (mod->pub && mod->pub->update) {
- mod->pub->count = 0U;
- k_delayed_work_cancel(&mod->pub->timer);
- }
- }
- int bt_mesh_suspend(void)
- {
- int err = 0;
- if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) {
- return -EINVAL;
- }
- if (bt_mesh_atomic_test_and_set_bit(bt_mesh.flags, BLE_MESH_SUSPENDED)) {
- return -EALREADY;
- }
- err = bt_mesh_scan_disable();
- if (err) {
- bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_SUSPENDED);
- BT_WARN("Disabling scanning failed (err %d)", err);
- return err;
- }
- bt_mesh_hb_pub_disable();
- if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) {
- bt_mesh_secure_beacon_disable();
- }
- #if CONFIG_BLE_MESH_PRB_SRV
- if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
- bt_mesh_private_beacon_disable();
- }
- #endif
- bt_mesh_model_foreach(model_suspend, NULL);
- return 0;
- }
- static void model_resume(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
- bool vnd, bool primary, void *user_data)
- {
- if (mod->pub && mod->pub->update) {
- int32_t period_ms = bt_mesh_model_pub_period_get(mod);
- if (period_ms) {
- k_delayed_work_submit(&mod->pub->timer, period_ms);
- }
- }
- }
- int bt_mesh_resume(void)
- {
- int err = 0;
- if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) {
- return -EINVAL;
- }
- if (!bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_SUSPENDED)) {
- return -EALREADY;
- }
- err = bt_mesh_scan_enable();
- if (err) {
- BT_WARN("Re-enabling scanning failed (err %d)", err);
- bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_SUSPENDED);
- return err;
- }
- if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) {
- bt_mesh_secure_beacon_enable();
- }
- #if CONFIG_BLE_MESH_PRB_SRV
- if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
- bt_mesh_private_beacon_enable();
- }
- #endif
- bt_mesh_model_foreach(model_resume, NULL);
- return err;
- }
- int bt_mesh_init(const struct bt_mesh_prov *prov,
- const struct bt_mesh_comp *comp)
- {
- int err = 0;
- if (mesh_init == true) {
- BT_WARN("%s, Already", __func__);
- return -EALREADY;
- }
- extern int bt_mesh_v11_ext_init(void);
- err = bt_mesh_v11_ext_init();
- if (err) {
- BT_ERR("Bluetooth Mesh v1.1 init failed");
- return err;
- }
- bt_mesh_mutex_init();
- bt_mesh_timer_init();
- bt_mesh_hci_init();
- bt_mesh_adapt_init();
- err = bt_mesh_comp_register(comp);
- if (err) {
- return err;
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) {
- bt_mesh_gatt_init();
- }
- if ((IS_ENABLED(CONFIG_BLE_MESH_NODE) &&
- IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) ||
- IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
- bt_mesh_proxy_server_init();
- }
- if ((IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) &&
- IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) ||
- IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) {
- bt_mesh_proxy_client_init();
- }
- #if CONFIG_BLE_MESH_PROXY_SOLIC
- err = bt_mesh_proxy_solic_init();
- if (err) {
- return err;
- }
- #endif
- if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) {
- err = bt_mesh_prov_set(prov);
- if (err) {
- return err;
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) {
- err = bt_mesh_prov_init();
- if (err) {
- return err;
- }
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) {
- err = bt_mesh_provisioner_prov_init();
- if (err) {
- return err;
- }
- }
- }
- bt_mesh_net_init();
- bt_mesh_trans_init();
- /* Changed by Espressif, add a random delay (0 ~ 3s) */
- if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) {
- uint32_t delay = 0;
- bt_mesh_rand(&delay, sizeof(uint32_t));
- vTaskDelay((delay % 3000) / portTICK_PERIOD_MS);
- }
- bt_mesh_beacon_init();
- bt_mesh_adv_init();
- if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) {
- bt_mesh_provisioner_init();
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
- bt_mesh_settings_init();
- }
- mesh_init = true;
- return 0;
- }
- #if CONFIG_BLE_MESH_DEINIT
- int bt_mesh_deinit(struct bt_mesh_deinit_param *param)
- {
- int err = 0;
- if (param == NULL) {
- BT_ERR("%s, Invalid parameter", __func__);
- return -EINVAL;
- }
- if (mesh_init == false) {
- BT_WARN("%s, Already", __func__);
- return -EALREADY;
- }
- bt_mesh_scan_disable();
- bt_mesh_secure_beacon_disable();
- #if CONFIG_BLE_MESH_PRB_SRV
- bt_mesh_private_beacon_disable();
- #endif
- if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) {
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
- !bt_mesh_is_provisioned()) {
- bt_mesh_proxy_server_prov_disable(true);
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
- bt_mesh_is_provisioned()) {
- bt_mesh_proxy_server_gatt_disable();
- }
- if (bt_mesh_is_provisioned()) {
- /* Clear valid flag here in order to perform settings erase */
- bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID);
- bt_mesh_cfg_reset(param->erase);
- }
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) {
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
- bt_mesh_proxy_client_prov_disable();
- }
- /* Clear valid flag here in order to perform settings erase */
- bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) {
- if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) {
- err = bt_mesh_prov_deinit();
- if (err) {
- return err;
- }
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) {
- err = bt_mesh_provisioner_prov_deinit(param->erase);
- if (err) {
- return err;
- }
- err = bt_mesh_provisioner_deinit(param->erase);
- if (err) {
- return err;
- }
- }
- bt_mesh_prov_set(NULL);
- }
- bt_mesh_trans_deinit(param->erase);
- bt_mesh_net_deinit();
- bt_mesh_beacon_deinit();
- #if CONFIG_BLE_MESH_PROXY_SOLIC
- bt_mesh_proxy_solic_deinit();
- #endif
- if ((IS_ENABLED(CONFIG_BLE_MESH_NODE) &&
- IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) ||
- IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
- bt_mesh_proxy_server_deinit();
- }
- if ((IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) &&
- IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) ||
- IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) {
- bt_mesh_proxy_client_deinit();
- }
- bt_mesh_gatt_deinit();
- if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
- bt_mesh_friend_deinit();
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
- bt_mesh_lpn_deinit();
- }
- bt_mesh_adv_deinit();
- if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
- bt_mesh_settings_deinit(param->erase);
- }
- err = bt_mesh_comp_deregister();
- if (err) {
- return err;
- }
- bt_mesh_comp_unprovision();
- memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags));
- bt_mesh_timer_deinit();
- bt_mesh_mutex_deinit();
- mesh_init = false;
- return 0;
- }
- #endif /* CONFIG_BLE_MESH_DEINIT */
- #if CONFIG_BLE_MESH_PROVISIONER
- int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)
- {
- int err = 0;
- if (bt_mesh_is_provisioner_en()) {
- BT_WARN("%s, Already", __func__);
- return -EALREADY;
- }
- if (prov_bearers_valid(bearers) == false) {
- return -EINVAL;
- }
- /* Add this judgement here in case the device worked as a
- * node previously. Before the corresponding information
- * of the node is erased from flash, users try to use the
- * device as a Provisioner, which will cause the information
- * in NVS been handled incorrectly.
- */
- uint8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK;
- if (role != BLE_MESH_SETTINGS_ROLE_NONE &&
- role != BLE_MESH_SETTINGS_ROLE_PROV) {
- BT_ERR("%s, Mismatch role %u", __func__, role);
- return -EIO;
- }
- if (role == BLE_MESH_SETTINGS_ROLE_NONE) {
- bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_PROVISIONER);
- if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
- bt_mesh_store_role();
- }
- }
- /* Enable Provisioner here, because during the following net
- * creation, some information needs to be stored in flash.
- */
- bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);
- err = bt_mesh_provisioner_net_create();
- if (err) {
- BT_ERR("Failed to create network");
- return err;
- }
- err = bt_mesh_provisioner_init_prov_info();
- if (err) {
- BT_ERR("Failed to init prov info");
- return err;
- }
- bt_mesh_provisioner_set_prov_bearer(bearers, false);
- bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr());
- #if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
- (bearers & BLE_MESH_PROV_ADV)) {
- bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD,
- BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON,
- NULL);
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
- (bearers & BLE_MESH_PROV_GATT)) {
- bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD,
- BLE_MESH_EXCEP_LIST_TYPE_MESH_PROV_ADV,
- NULL);
- }
- #endif
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
- (bearers & BLE_MESH_PROV_GATT)) {
- bt_mesh_proxy_client_prov_enable();
- }
- if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
- bt_mesh_friend_init();
- }
- if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) {
- bt_mesh_secure_beacon_enable();
- }
- #if CONFIG_BLE_MESH_PRB_SRV
- if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
- bt_mesh_private_beacon_enable();
- }
- #endif
- err = bt_mesh_scan_enable();
- if (err) {
- return err;
- }
- return 0;
- }
- int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers)
- {
- bt_mesh_prov_bearer_t enable = 0U;
- if (!bt_mesh_is_provisioner_en()) {
- BT_WARN("%s, Already", __func__);
- return -EALREADY;
- }
- if (prov_bearers_valid(bearers) == false) {
- return -EINVAL;
- }
- enable = bt_mesh_provisioner_get_prov_bearer();
- if (!(enable & bearers)) {
- BT_ERR("Mismatch bearers 0x%02x", bearers);
- return -EINVAL;
- }
- bt_mesh_provisioner_set_prov_bearer(bearers, true);
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
- (enable & BLE_MESH_PROV_GATT) &&
- (bearers & BLE_MESH_PROV_GATT)) {
- bt_mesh_proxy_client_prov_disable();
- #if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
- bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE,
- BLE_MESH_EXCEP_LIST_TYPE_MESH_PROV_ADV,
- NULL);
- #endif
- }
- if (!(enable & (~bearers))) {
- /* Provisioner is disabled completely, disable scan here */
- bt_mesh_scan_disable();
- #if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
- if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
- (enable & BLE_MESH_PROV_ADV)) {
- bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE,
- BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON,
- NULL);
- }
- #endif
- /* Clear corresponding flags */
- bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV)));
- /* When Provisioner is disabled, the device role indicated by bt_mesh.flags
- * will not be cleared, because when Provisioner is restarted after disabled,
- * its previous information can be recovered from flash properly.
- */
- if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) {
- bt_mesh_secure_beacon_disable();
- }
- #if CONFIG_BLE_MESH_PRB_SRV
- if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
- bt_mesh_private_beacon_disable();
- }
- #endif
- if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
- bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY);
- }
- }
- return 0;
- }
- #endif /* CONFIG_BLE_MESH_PROVISIONER */
|