Merge branch 'fix/ble_mesh_micellaneous_fix_v5.1' into 'release/v5.1'

Fix/ble mesh micellaneous fix (v5.1)

See merge request espressif/esp-idf!41285
This commit is contained in:
Island
2025-09-17 16:04:37 +08:00
4 changed files with 100 additions and 36 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -27,6 +27,11 @@ void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex);
void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex); void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex);
void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex); void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex);
void bt_mesh_r_mutex_create(bt_mesh_mutex_t *mutex);
void bt_mesh_r_mutex_free(bt_mesh_mutex_t *mutex);
void bt_mesh_r_mutex_lock(bt_mesh_mutex_t *mutex);
void bt_mesh_r_mutex_unlock(bt_mesh_mutex_t *mutex);
void bt_mesh_alarm_lock(void); void bt_mesh_alarm_lock(void);
void bt_mesh_alarm_unlock(void); void bt_mesh_alarm_unlock(void);

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -74,6 +74,57 @@ void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex)
} }
} }
void bt_mesh_r_mutex_create(bt_mesh_mutex_t *mutex)
{
if (!mutex) {
BT_ERR("Create, invalid recursive mutex");
return;
}
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#endif
__ASSERT(mutex->buffer, "Failed to create recursive mutex buffer");
mutex->mutex = xSemaphoreCreateRecursiveMutexStatic(mutex->buffer);
__ASSERT(mutex->mutex, "Failed to create static recursive mutex");
#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
mutex->mutex = xSemaphoreCreateRecursiveMutex();
__ASSERT(mutex->mutex, "Failed to create recursive mutex");
#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
}
void bt_mesh_r_mutex_free(bt_mesh_mutex_t *mutex)
{
bt_mesh_mutex_free(mutex);
}
void bt_mesh_r_mutex_lock(bt_mesh_mutex_t *mutex)
{
if (!mutex) {
BT_ERR("Lock, invalid recursive mutex");
return;
}
if (mutex->mutex) {
xSemaphoreTakeRecursive(mutex->mutex, portMAX_DELAY);
}
}
void bt_mesh_r_mutex_unlock(bt_mesh_mutex_t *mutex)
{
if (!mutex) {
BT_ERR("Unlock, invalid recursive mutex");
return;
}
if (mutex->mutex) {
xSemaphoreGiveRecursive(mutex->mutex);
}
}
static inline void bt_mesh_alarm_mutex_new(void) static inline void bt_mesh_alarm_mutex_new(void)
{ {
if (!alarm_lock.mutex) { if (!alarm_lock.mutex) {

View File

@@ -2,7 +2,7 @@
/* /*
* SPDX-FileCopyrightText: 2017 Intel Corporation * SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileContributor: 2018-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -56,6 +56,8 @@ struct bt_mesh_queue {
}; };
static struct bt_mesh_queue adv_queue; static struct bt_mesh_queue adv_queue;
static bt_mesh_mutex_t adv_buf_alloc_lock;
/* We reserve one queue item for bt_mesh_adv_update() */ /* We reserve one queue item for bt_mesh_adv_update() */
#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV
#define BLE_MESH_ADV_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT + 1) #define BLE_MESH_ADV_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT + 1)
@@ -352,8 +354,10 @@ struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool,
return NULL; return NULL;
} }
bt_mesh_r_mutex_lock(&adv_buf_alloc_lock);
buf = net_buf_alloc(pool, timeout); buf = net_buf_alloc(pool, timeout);
if (!buf) { if (!buf) {
bt_mesh_r_mutex_unlock(&adv_buf_alloc_lock);
return NULL; return NULL;
} }
@@ -368,6 +372,7 @@ struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool,
adv->type = type; adv->type = type;
adv->xmit = xmit; adv->xmit = xmit;
bt_mesh_r_mutex_unlock(&adv_buf_alloc_lock);
return buf; return buf;
} }
@@ -653,6 +658,7 @@ void bt_mesh_adv_init(void)
__ASSERT(ret == pdTRUE, "Failed to create adv thread"); __ASSERT(ret == pdTRUE, "Failed to create adv thread");
(void)ret; (void)ret;
#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && (CONFIG_SPIRAM_CACHE_WORKAROUND || !CONFIG_IDF_TARGET_ESP32) && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY */ #endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && (CONFIG_SPIRAM_CACHE_WORKAROUND || !CONFIG_IDF_TARGET_ESP32) && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY */
bt_mesh_r_mutex_create(&adv_buf_alloc_lock);
} }
#if CONFIG_BLE_MESH_DEINIT #if CONFIG_BLE_MESH_DEINIT
@@ -707,7 +713,8 @@ void bt_mesh_adv_deinit(void)
#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV
bt_mesh_ble_adv_deinit(); bt_mesh_ble_adv_deinit();
#endif #endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */
bt_mesh_r_mutex_free(&adv_buf_alloc_lock);
} }
#endif /* CONFIG_BLE_MESH_DEINIT */ #endif /* CONFIG_BLE_MESH_DEINIT */

View File

@@ -83,6 +83,7 @@ static struct seg_tx {
const struct bt_mesh_send_cb *cb; const struct bt_mesh_send_cb *cb;
void *cb_data; void *cb_data;
struct k_delayed_work retransmit; /* Retransmit timer */ struct k_delayed_work retransmit; /* Retransmit timer */
bt_mesh_mutex_t lock;
} seg_tx[CONFIG_BLE_MESH_TX_SEG_MSG_COUNT]; } seg_tx[CONFIG_BLE_MESH_TX_SEG_MSG_COUNT];
static struct seg_rx { static struct seg_rx {
@@ -111,31 +112,16 @@ static uint8_t seg_rx_buf_data[(CONFIG_BLE_MESH_RX_SEG_MSG_COUNT *
static uint16_t hb_sub_dst = BLE_MESH_ADDR_UNASSIGNED; static uint16_t hb_sub_dst = BLE_MESH_ADDR_UNASSIGNED;
static bt_mesh_mutex_t tx_seg_lock;
static bt_mesh_mutex_t rx_seg_lock; static bt_mesh_mutex_t rx_seg_lock;
static inline void bt_mesh_tx_seg_mutex_new(void) static inline void bt_mesh_tx_seg_lock(struct seg_tx *tx)
{ {
if (!tx_seg_lock.mutex) { bt_mesh_r_mutex_lock(&tx->lock);
bt_mesh_mutex_create(&tx_seg_lock);
}
} }
#if CONFIG_BLE_MESH_DEINIT static inline void bt_mesh_tx_seg_unlock(struct seg_tx *tx)
static inline void bt_mesh_tx_seg_mutex_free(void)
{ {
bt_mesh_mutex_free(&tx_seg_lock); bt_mesh_r_mutex_unlock(&tx->lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */
static inline void bt_mesh_tx_seg_lock(void)
{
bt_mesh_mutex_lock(&tx_seg_lock);
}
static inline void bt_mesh_tx_seg_unlock(void)
{
bt_mesh_mutex_unlock(&tx_seg_lock);
} }
static inline void bt_mesh_rx_seg_mutex_new(void) static inline void bt_mesh_rx_seg_mutex_new(void)
@@ -275,7 +261,7 @@ static void seg_tx_reset(struct seg_tx *tx)
{ {
int i; int i;
bt_mesh_tx_seg_lock(); bt_mesh_tx_seg_lock(tx);
k_delayed_work_cancel(&tx->retransmit); k_delayed_work_cancel(&tx->retransmit);
@@ -295,7 +281,7 @@ static void seg_tx_reset(struct seg_tx *tx)
tx->nack_count = 0U; tx->nack_count = 0U;
bt_mesh_tx_seg_unlock(); bt_mesh_tx_seg_unlock(tx);
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING)) { if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING)) {
BT_DBG("Proceding with pending IV Update"); BT_DBG("Proceding with pending IV Update");
@@ -322,6 +308,7 @@ static inline void seg_tx_complete(struct seg_tx *tx, int err)
static void schedule_retransmit(struct seg_tx *tx) static void schedule_retransmit(struct seg_tx *tx)
{ {
bt_mesh_tx_seg_lock(tx);
/* It's possible that a segment broadcast hasn't finished, /* It's possible that a segment broadcast hasn't finished,
* but the tx are already released. Only the seg_pending * but the tx are already released. Only the seg_pending
* of this segment remains unprocessed. So, here, we * of this segment remains unprocessed. So, here, we
@@ -333,20 +320,24 @@ static void schedule_retransmit(struct seg_tx *tx)
if (tx->seg_pending) { if (tx->seg_pending) {
tx->seg_pending--; tx->seg_pending--;
} }
bt_mesh_tx_seg_unlock(tx);
return; return;
} }
if (--tx->seg_pending) { if (--tx->seg_pending) {
bt_mesh_tx_seg_unlock(tx);
return; return;
} }
if (!BLE_MESH_ADDR_IS_UNICAST(tx->dst) && !tx->attempts) { if (!BLE_MESH_ADDR_IS_UNICAST(tx->dst) && !tx->attempts) {
BT_INFO("Complete tx sdu to group"); BT_INFO("Complete tx sdu to group");
seg_tx_complete(tx, 0); seg_tx_complete(tx, 0);
bt_mesh_tx_seg_unlock(tx);
return; return;
} }
k_delayed_work_submit(&tx->retransmit, SEG_RETRANSMIT_TIMEOUT(tx)); k_delayed_work_submit(&tx->retransmit, SEG_RETRANSMIT_TIMEOUT(tx));
bt_mesh_tx_seg_unlock(tx);
} }
static void seg_first_send_start(uint16_t duration, int err, void *user_data) static void seg_first_send_start(uint16_t duration, int err, void *user_data)
@@ -392,11 +383,11 @@ static void seg_tx_send_unacked(struct seg_tx *tx)
{ {
int i, err = 0; int i, err = 0;
bt_mesh_tx_seg_lock(); bt_mesh_tx_seg_lock(tx);
if (!(tx->attempts--)) { if (!(tx->attempts--)) {
BT_WARN("Ran out of retransmit attempts"); BT_WARN("Ran out of retransmit attempts");
bt_mesh_tx_seg_unlock(); bt_mesh_tx_seg_unlock(tx);
seg_tx_complete(tx, -ETIMEDOUT); seg_tx_complete(tx, -ETIMEDOUT);
return; return;
} }
@@ -423,13 +414,13 @@ static void seg_tx_send_unacked(struct seg_tx *tx)
&seg_sent_cb, tx); &seg_sent_cb, tx);
if (err) { if (err) {
BT_ERR("Sending segment failed"); BT_ERR("Sending segment failed");
bt_mesh_tx_seg_unlock(); bt_mesh_tx_seg_unlock(tx);
seg_tx_complete(tx, -EIO); seg_tx_complete(tx, -EIO);
return; return;
} }
} }
bt_mesh_tx_seg_unlock(); bt_mesh_tx_seg_unlock(tx);
} }
static void seg_retransmit(struct k_work *work) static void seg_retransmit(struct k_work *work)
@@ -445,6 +436,7 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu,
uint8_t seg_hdr = 0U, seg_o = 0U; uint8_t seg_hdr = 0U, seg_o = 0U;
uint16_t seq_zero = 0U; uint16_t seq_zero = 0U;
struct seg_tx *tx = NULL; struct seg_tx *tx = NULL;
int err = 0;
int i; int i;
BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x aszmic %u sdu_len %u", BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x aszmic %u sdu_len %u",
@@ -520,6 +512,8 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu,
return -ENOBUFS; return -ENOBUFS;
} }
bt_mesh_tx_seg_lock(tx);
for (seg_o = 0U; sdu->len; seg_o++) { for (seg_o = 0U; sdu->len; seg_o++) {
struct net_buf *seg = NULL; struct net_buf *seg = NULL;
uint16_t len = 0U; uint16_t len = 0U;
@@ -530,7 +524,8 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu,
if (!seg) { if (!seg) {
BT_ERR("Out of segment buffers"); BT_ERR("Out of segment buffers");
seg_tx_reset(tx); seg_tx_reset(tx);
return -ENOBUFS; err = -ENOBUFS;
break;
} }
net_buf_reserve(seg, BLE_MESH_NET_HDR_LEN); net_buf_reserve(seg, BLE_MESH_NET_HDR_LEN);
@@ -577,10 +572,16 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu,
tx); tx);
if (err) { if (err) {
BT_ERR("Sending segment failed"); BT_ERR("Sending segment failed");
break;
}
}
bt_mesh_tx_seg_unlock(tx);
if (err) {
seg_tx_reset(tx); seg_tx_reset(tx);
return err; return err;
} }
}
/* This can happen if segments only went into the Friend Queue */ /* This can happen if segments only went into the Friend Queue */
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !tx->seg[0]) { if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !tx->seg[0]) {
@@ -979,9 +980,9 @@ static int trans_ack(struct bt_mesh_net_rx *rx, uint8_t hdr,
while ((bit = find_lsb_set(ack))) { while ((bit = find_lsb_set(ack))) {
if (tx->seg[bit - 1]) { if (tx->seg[bit - 1]) {
BT_DBG("seg %u/%u acked", bit - 1, tx->seg_n); BT_DBG("seg %u/%u acked", bit - 1, tx->seg_n);
bt_mesh_tx_seg_lock(); bt_mesh_tx_seg_lock(tx);
seg_tx_done(tx, bit - 1); seg_tx_done(tx, bit - 1);
bt_mesh_tx_seg_unlock(); bt_mesh_tx_seg_unlock(tx);
} }
ack &= ~BIT(bit - 1); ack &= ~BIT(bit - 1);
@@ -1905,6 +1906,7 @@ void bt_mesh_trans_init(void)
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
k_delayed_work_init(&seg_tx[i].retransmit, seg_retransmit); k_delayed_work_init(&seg_tx[i].retransmit, seg_retransmit);
bt_mesh_r_mutex_create(&seg_tx[i].lock);
} }
for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { for (i = 0; i < ARRAY_SIZE(seg_rx); i++) {
@@ -1914,7 +1916,6 @@ void bt_mesh_trans_init(void)
seg_rx[i].buf.data = seg_rx[i].buf.__buf; seg_rx[i].buf.data = seg_rx[i].buf.__buf;
} }
bt_mesh_tx_seg_mutex_new();
bt_mesh_rx_seg_mutex_new(); bt_mesh_rx_seg_mutex_new();
} }
@@ -1928,13 +1929,13 @@ void bt_mesh_trans_deinit(bool erase)
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
k_delayed_work_free(&seg_tx[i].retransmit); k_delayed_work_free(&seg_tx[i].retransmit);
bt_mesh_r_mutex_free(&seg_tx[i].lock);
} }
for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { for (i = 0; i < ARRAY_SIZE(seg_rx); i++) {
k_delayed_work_free(&seg_rx[i].ack); k_delayed_work_free(&seg_rx[i].ack);
} }
bt_mesh_tx_seg_mutex_free();
bt_mesh_rx_seg_mutex_free(); bt_mesh_rx_seg_mutex_free();
} }
#endif /* CONFIG_BLE_MESH_DEINIT */ #endif /* CONFIG_BLE_MESH_DEINIT */