From 4fe0523a5fb0c4bcc77d5e1675dabcf7b5d5716d Mon Sep 17 00:00:00 2001 From: luoxu Date: Fri, 29 Dec 2023 12:25:33 +0800 Subject: [PATCH] bugfix(ble_mesh): Prevent the generation of link ID as 0. --- .../bluedroid_host/mesh_bearer_adapt.c | 12 +++++++++- .../mesh_core/nimble_host/mesh_bearer_adapt.c | 10 ++++++++ .../esp_ble_mesh/mesh_core/provisioner_prov.c | 23 ++++++++++++++----- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c index 45c4d12fe6..1277bf6ae4 100644 --- a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c @@ -1,7 +1,7 @@ /* * SPDX-FileCopyrightText: 2017 Nordic Semiconductor ASA * SPDX-FileCopyrightText: 2015-2016 Intel Corporation - * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileContributor: 2018-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -1978,6 +1978,16 @@ int bt_mesh_update_exceptional_list(uint8_t sub_code, uint32_t type, void *info) BT_ERR("Invalid Provisioning Link ID"); return -EINVAL; } + + /* When removing an unused link (i.e., Link ID is 0), and since + * Controller has never added this Link ID, it will cause error + * log been wrongly reported. + * Therefore, add this check here to avoid such occurrences. + */ + if (*(uint32_t*)info == 0) { + return 0; + } + sys_memcpy_swap(value, info, sizeof(uint32_t)); } diff --git a/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c index 12a2f7e5e8..1b8db76b02 100644 --- a/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c @@ -1963,6 +1963,16 @@ int bt_mesh_update_exceptional_list(uint8_t sub_code, uint32_t type, void *info) BT_ERR("Invalid Provisioning Link ID"); return -EINVAL; } + + /* When removing an unused link (i.e., Link ID is 0), and since + * Controller has never added this Link ID, it will cause error + * log been wrongly reported. + * Therefore, add this check here to avoid such occurrences. + */ + if (*(uint32_t*)info == 0) { + return 0; + } + sys_memcpy_swap(value, info, sizeof(uint32_t)); } 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 7ac0d8bf92..4ee438d003 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -1459,16 +1459,27 @@ static int bearer_ctl_send(const uint8_t idx, uint8_t op, void *data, uint8_t da static void send_link_open(const uint8_t idx) { int j; + uint8_t count; + + link[idx].link_id = 0; - /** Generate link ID, and may need to check if this id is - * currently being used, which may will not happen ever. - */ - bt_mesh_rand(&link[idx].link_id, sizeof(uint32_t)); while (1) { + count = 0; + + /* Make sure the generated Link ID is not 0 */ + while(link[idx].link_id == 0) { + bt_mesh_rand(&link[idx].link_id, sizeof(link[idx].link_id)); + if (count++ > 10) { + BT_ERR("Link ID error: all zero"); + return; + } + } + + /* Check if the generated Link ID is the same with other links */ for (j = 0; j < CONFIG_BLE_MESH_PBA_SAME_TIME; j++) { if (bt_mesh_atomic_test_bit(link[j].flags, LINK_ACTIVE) || link[j].linking) { if (link[idx].link_id == link[j].link_id) { - bt_mesh_rand(&link[idx].link_id, sizeof(uint32_t)); + bt_mesh_rand(&link[idx].link_id, sizeof(link[idx].link_id)); break; } }