bugfix(ble_mesh): Prevent the generation of link ID as 0.

This commit is contained in:
luoxu
2023-12-29 12:25:33 +08:00
committed by wangjialiang
parent 82bfc1de86
commit 4fe0523a5f
3 changed files with 38 additions and 7 deletions

View File

@ -1,7 +1,7 @@
/* /*
* SPDX-FileCopyrightText: 2017 Nordic Semiconductor ASA * SPDX-FileCopyrightText: 2017 Nordic Semiconductor ASA
* SPDX-FileCopyrightText: 2015-2016 Intel Corporation * 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 * 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"); BT_ERR("Invalid Provisioning Link ID");
return -EINVAL; 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)); sys_memcpy_swap(value, info, sizeof(uint32_t));
} }

View File

@ -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"); BT_ERR("Invalid Provisioning Link ID");
return -EINVAL; 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)); sys_memcpy_swap(value, info, sizeof(uint32_t));
} }

View File

@ -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 * 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) static void send_link_open(const uint8_t idx)
{ {
int j; 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) { 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++) { 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 (bt_mesh_atomic_test_bit(link[j].flags, LINK_ACTIVE) || link[j].linking) {
if (link[idx].link_id == link[j].link_id) { 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; break;
} }
} }