Skip to content

Commit

Permalink
Merge branch 'bugfix/ble_mesh_settings_fix_v4.1' into 'release/v4.1'
Browse files Browse the repository at this point in the history
Bugfix/ble mesh settings fix (v4.1)

See merge request espressif/esp-idf!10785
  • Loading branch information
Isl2017 committed Oct 13, 2020
2 parents d94867a + d48b719 commit c01f0a0
Show file tree
Hide file tree
Showing 34 changed files with 486 additions and 297 deletions.
5 changes: 3 additions & 2 deletions components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@ typedef union {
*/
struct ble_mesh_provisioner_add_local_app_key_comp_param {
int err_code; /*!< Indicate the result of adding local AppKey by the Provisioner */
uint16_t net_idx; /*!< NetKey Index */
uint16_t app_idx; /*!< AppKey Index */
} provisioner_add_app_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT */
/**
Expand Down Expand Up @@ -1176,14 +1177,14 @@ typedef union {
/**
* @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT
*/
struct ble_mesh_provisioner_delete_node_with_uuid_comp_data_comp_param {
struct ble_mesh_provisioner_delete_node_with_uuid_comp_param {
int err_code; /*!< Indicate the result of deleting node with uuid by the Provisioner */
uint8_t uuid[16]; /*!< Node device uuid */
} provisioner_delete_node_with_uuid_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT */
/**
* @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT
*/
struct ble_mesh_provisioner_delete_node_with_addr_comp_data_comp_param {
struct ble_mesh_provisioner_delete_node_with_addr_comp_param {
int err_code; /*!< Indicate the result of deleting node with unicast address by the Provisioner */
uint16_t unicast_addr; /*!< Node unicast address */
} provisioner_delete_node_with_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT */
Expand Down
5 changes: 3 additions & 2 deletions components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c
Original file line number Diff line number Diff line change
Expand Up @@ -1823,10 +1823,11 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg)
app_key = arg->add_local_app_key.app_key;
}
act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT;
param.provisioner_add_app_key_comp.app_idx = arg->add_local_app_key.app_idx;
param.provisioner_add_app_key_comp.err_code =
bt_mesh_provisioner_local_app_key_add(app_key, arg->add_local_app_key.net_idx,
&arg->add_local_app_key.app_idx);
param.provisioner_add_app_key_comp.net_idx = arg->add_local_app_key.net_idx;
param.provisioner_add_app_key_comp.app_idx = arg->add_local_app_key.app_idx;
break;
}
case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY:
Expand Down Expand Up @@ -1856,9 +1857,9 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg)
net_key = arg->add_local_net_key.net_key;
}
act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT;
param.provisioner_add_net_key_comp.net_idx = arg->add_local_net_key.net_idx;
param.provisioner_add_net_key_comp.err_code =
bt_mesh_provisioner_local_net_key_add(net_key, &arg->add_local_net_key.net_idx);
param.provisioner_add_net_key_comp.net_idx = arg->add_local_net_key.net_idx;
break;
}
case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY:
Expand Down
5 changes: 5 additions & 0 deletions components/bt/esp_ble_mesh/mesh_core/access.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod,
{
int i, j;

if (dev_comp == NULL) {
BT_ERR("Invalid device composition");
return;
}

for (i = 0; i < dev_comp->elem_count; i++) {
struct bt_mesh_elem *elem = &dev_comp->elem[i];

Expand Down
2 changes: 1 addition & 1 deletion components/bt/esp_ble_mesh/mesh_core/lpn.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ static void clear_friendship(bool force, bool disable)
return;
}

bt_mesh_rx_reset();
bt_mesh_rx_reset(true);

k_delayed_work_cancel(&lpn->timer);

Expand Down
58 changes: 44 additions & 14 deletions components/bt/esp_ble_mesh/mesh_core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void bt_mesh_node_reset(void)

bt_mesh_cfg_reset();

bt_mesh_rx_reset();
bt_mesh_rx_reset(true);
bt_mesh_tx_reset();

if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
Expand Down Expand Up @@ -199,10 +199,27 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
return -EINVAL;
}

bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE);
/* 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.
*/
u8_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 (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_role();
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) &&
Expand All @@ -225,6 +242,7 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)
{
if (bt_mesh_is_provisioned()) {
BT_WARN("%s, Already provisioned", __func__);
return -EALREADY;
}

Expand Down Expand Up @@ -504,10 +522,7 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param)
bt_mesh_comp_unprovision();

if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
if (param->erase) {
bt_mesh_clear_role();
}
bt_mesh_settings_deinit();
bt_mesh_settings_deinit(param->erase);
}

bt_mesh_timer_deinit();
Expand All @@ -532,6 +547,27 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)
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.
*/
u8_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();
}
}

err = bt_mesh_provisioner_net_create();
if (err) {
BT_ERR("Failed to create network");
Expand All @@ -548,12 +584,6 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)

bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr());

bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_PROVISIONER);

if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_role();
}

#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN)
if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
(bearers & BLE_MESH_PROV_ADV)) {
Expand Down
13 changes: 7 additions & 6 deletions components/bt/esp_ble_mesh/mesh_core/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/*
* Copyright (c) 2017 Intel Corporation
* Additional Copyright (c) 2020 Espressif Systems (Shanghai) PTE LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -15,13 +16,13 @@
extern "C" {
#endif

#define BLE_MESH_KEY_PRIMARY 0x0000
#define BLE_MESH_KEY_ANY 0xffff
#define BLE_MESH_KEY_PRIMARY 0x0000
#define BLE_MESH_KEY_ANY 0xffff

#define BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000)
#define BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) <= 0xff00)
#define BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000)
#define BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfffb)
#define BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000)
#define BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) <= 0xff00)
#define BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000)
#define BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfffb)

struct bt_mesh_net;

Expand Down
10 changes: 6 additions & 4 deletions components/bt/esp_ble_mesh/mesh_core/provisioner_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ int bt_mesh_provisioner_deinit(bool erase)
for (i = 0; i < CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT; i++) {
if (bt_mesh.p_sub[i]) {
if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_p_subnet(bt_mesh.p_sub[i]);
bt_mesh_clear_p_subnet(bt_mesh.p_sub[i]->net_idx);
}
bt_mesh_free(bt_mesh.p_sub[i]);
bt_mesh.p_sub[i] = NULL;
Expand All @@ -175,7 +175,7 @@ int bt_mesh_provisioner_deinit(bool erase)
for (i = 0; i < CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT; i++) {
if (bt_mesh.p_app_keys[i]) {
if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_p_app_key(bt_mesh.p_app_keys[i]);
bt_mesh_clear_p_app_key(bt_mesh.p_app_keys[i]->app_idx);
}
bt_mesh_free(bt_mesh.p_app_keys[i]);
bt_mesh.p_app_keys[i] = NULL;
Expand Down Expand Up @@ -1027,6 +1027,7 @@ int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16],
}
}
*app_idx = key->app_idx;
bt_mesh.p_app_idx_next++;
}
key->updated = false;

Expand Down Expand Up @@ -1198,7 +1199,7 @@ int bt_mesh_provisioner_local_app_key_delete(u16_t net_idx, u16_t app_idx)
bt_mesh_model_foreach(_model_unbind, &app_idx);

if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_p_app_key(key);
bt_mesh_clear_p_app_key(app_idx);
}

bt_mesh_free(bt_mesh.p_app_keys[i]);
Expand Down Expand Up @@ -1283,6 +1284,7 @@ int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx
}
}
*net_idx = sub->net_idx;
bt_mesh.p_net_idx_next++;
}
sub->kr_phase = BLE_MESH_KR_NORMAL;
sub->kr_flag = false;
Expand Down Expand Up @@ -1391,7 +1393,7 @@ int bt_mesh_provisioner_local_net_key_delete(u16_t net_idx)
}

if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_clear_p_subnet(sub);
bt_mesh_clear_p_subnet(net_idx);
}

bt_mesh_free(bt_mesh.p_sub[i]);
Expand Down
5 changes: 5 additions & 0 deletions components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,11 @@ int bt_mesh_provisioner_init_prov_info(void)
/* If unicast address of primary element of Provisioner has not been set
* before, then the following initialization procedure will be used.
*/
if (prov == NULL) {
BT_ERR("No provisioning context provided");
return -EINVAL;
}

if (!BLE_MESH_ADDR_IS_UNICAST(prov->prov_unicast_addr) ||
!BLE_MESH_ADDR_IS_UNICAST(prov->prov_start_address)) {
BT_ERR("Invalid address, own 0x%04x, start 0x%04x",
Expand Down

0 comments on commit c01f0a0

Please sign in to comment.