From 34f30a878edadf3183e2cae5ce0a7af78f030b9e Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 17 Aug 2020 20:11:17 +0800 Subject: [PATCH] ble_mesh: stack: Using the latest iv_index for provisioning Provisioner should always uses the latest IV Index for provisioning. For example, if the current IV Index is 0x00000001, but prov->iv_index is still initialized with 0x00000000, and if Provisioner uses prov-> iv_index for provisioning, this will cause the Provisioner failing to control the node. So here bt_mesh.iv_index is used instead of prov->iv_index. --- .../mesh_core/include/mesh_main.h | 8 --- components/bt/esp_ble_mesh/mesh_core/main.c | 67 +++++++++---------- .../esp_ble_mesh/mesh_core/provisioner_main.c | 5 -- .../esp_ble_mesh/mesh_core/provisioner_prov.c | 19 +++--- .../esp_ble_mesh/mesh_core/provisioner_prov.h | 6 +- .../bt/esp_ble_mesh/mesh_core/settings.c | 11 +-- 6 files changed, 47 insertions(+), 69 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h index 35d38a9c7..a5b1256d9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h @@ -383,14 +383,6 @@ int bt_mesh_prov_input_string(const char *str); */ int bt_mesh_prov_input_number(u32_t num); -/** @brief Enable Provisioner corresponding functionalities, e.g. scan, etc. - * - * @param bearers Bit-wise OR of provisioning bearers. - * - * @return Zero on success or (negative) error code otherwise. - */ -int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers); - /** @brief Enable specific provisioning bearers * * Enable one or more provisioning bearers. diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c index 5b7066d7b..9f55a2612 100644 --- a/components/bt/esp_ble_mesh/mesh_core/main.c +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -514,10 +514,41 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param) } #if defined(CONFIG_BLE_MESH_PROVISIONER) -int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers) +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; + } + + 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()); + + 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)) { @@ -552,40 +583,6 @@ int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers) return 0; } -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; - } - - err = bt_mesh_provisioner_set_prov_info(); - if (err) { - BT_ERR("Failed to set provisioning info"); - return err; - } - - err = bt_mesh_provisioner_net_create(); - if (err) { - BT_ERR("Failed to create network"); - return err; - } - - bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_PROVISIONER); - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_role(); - } - - return bt_mesh_provisioner_net_start(bearers); -} - int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers) { bt_mesh_prov_bearer_t enable = 0U; diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c index 469094c5a..416fbd3f8 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -83,11 +83,6 @@ int bt_mesh_provisioner_net_create(void) return -EINVAL; } - /* If the device only acts as a Provisioner, need to initialize - * each element's address. - */ - bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr()); - if (bt_mesh.p_sub[0]) { /* Provisioner is already enabled (enable -> disable -> enable), * or Provisioner is restored from flash. diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c index 0439a279a..18497ae0b 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c @@ -237,7 +237,7 @@ struct bt_mesh_prov_ctx { u16_t curr_net_idx; /* Current flags going to be used in provisioning data */ - u16_t curr_flags; + u8_t curr_flags; /* Current iv_index going to be used in provisioning data */ u16_t curr_iv_index; @@ -1128,10 +1128,8 @@ int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info) return 0; } -int bt_mesh_provisioner_set_prov_info(void) +int bt_mesh_provisioner_init_prov_info(void) { - const struct bt_mesh_comp *comp = NULL; - if (prov_ctx.primary_addr == BLE_MESH_ADDR_UNASSIGNED) { /* If unicast address of primary element of Provisioner has not been set * before, then the following initialization procedure will be used. @@ -1143,7 +1141,7 @@ int bt_mesh_provisioner_set_prov_info(void) return -EINVAL; } - comp = bt_mesh_comp_get(); + const struct bt_mesh_comp *comp = bt_mesh_comp_get(); if (!comp) { BT_ERR("Invalid composition data"); return -EINVAL; @@ -1156,15 +1154,19 @@ int bt_mesh_provisioner_set_prov_info(void) } else { prov_ctx.curr_alloc_addr = prov->prov_start_address; } + + /* Update primary element address with the initialized value here. */ prov_ctx.primary_addr = prov->prov_unicast_addr; if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr); } } + prov_ctx.curr_net_idx = BLE_MESH_KEY_PRIMARY; - prov_ctx.curr_flags = prov->flags; - prov_ctx.curr_iv_index = prov->iv_index; + struct bt_mesh_subnet *sub = bt_mesh_provisioner_subnet_get(BLE_MESH_KEY_PRIMARY); + prov_ctx.curr_flags = bt_mesh_net_flags(sub); + prov_ctx.curr_iv_index = bt_mesh.iv_index; return 0; } @@ -1241,7 +1243,8 @@ int bt_mesh_provisioner_set_primary_elem_addr(u16_t addr) if (addr + comp->elem_count > prov_ctx.curr_alloc_addr) { prov_ctx.curr_alloc_addr = addr + comp->elem_count; } - BT_INFO("Provisioner primary address updated, old 0x%04x, new 0x%04x", prov_ctx.primary_addr, addr); + + BT_INFO("Primary address updated, old 0x%04x, new 0x%04x", prov_ctx.primary_addr, addr); prov_ctx.primary_addr = addr; if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h index 959717caf..04c961e9f 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h @@ -277,12 +277,12 @@ int bt_mesh_provisioner_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb); int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info); /** - * @brief This function sets the provisioning information needed by Provisioner, - * including unicast address, IV Index, etc. + * @brief This function initializes the provisioning information needed by Provisioner, + * including NetKey Index, flags, IV Index, etc. * * @return Zero - success, otherwise - fail */ -int bt_mesh_provisioner_set_prov_info(void); +int bt_mesh_provisioner_init_prov_info(void); /** * @brief This function sets the provisioning bearer type used by Provisioner. diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index 284f91d4c..97893b66a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -249,7 +249,7 @@ static int iv_set(const char *name) bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, iv.iv_update); bt_mesh.ivu_duration = iv.iv_duration; - BT_INFO("Restored IV Index 0x%04x (IV Update Flag %u) duration %u hours", + BT_INFO("Restored IV Index 0x%08x (IV Update Flag %u) duration %u hours", iv.iv_index, iv.iv_update, iv.iv_duration); return 0; @@ -1365,8 +1365,6 @@ int settings_core_commit(void) BT_INFO("p_sub[0]->net_idx 0x%03x", bt_mesh.p_sub[0]->net_idx); - bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr()); - for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { sub = bt_mesh.p_sub[i]; @@ -1378,7 +1376,6 @@ int settings_core_commit(void) if (err) { BT_ERR("Failed to init subnet 0x%03x", sub->net_idx); } - sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; } } #endif /* CONFIG_BLE_MESH_PROVISIONER */ @@ -1417,12 +1414,6 @@ int settings_core_commit(void) } #endif /* CONFIG_BLE_MESH_NODE */ -#if defined(CONFIG_BLE_MESH_PROVISIONER) - if (bt_mesh_is_provisioner()) { - bt_mesh_provisioner_net_start(BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT); - } -#endif /* CONFIG_BLE_MESH_PROVISIONER */ - return 0; }