Skip to content

Commit 624e894

Browse files
committed
Refactor NimBLEUtils::taskWait to check notification value before blocking.
Instead of incrementing the notificatin value via `xTaskNotifyGive` this will now set a specific bit in the task notification value which will be tested before blocking a task. This will prevent a task from blocking indefinitely if the event that calls `taskRelease` occurs before entering the blocked state. * Adds a config setting for the bit to set in the task notification value.
1 parent 64d1492 commit 624e894

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/NimBLEUtils.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
# include <stdlib.h>
2828
# include <climits>
2929

30+
# if defined INC_FREERTOS_H
31+
constexpr uint32_t TASK_BLOCK_BIT = (1 << CONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT);
32+
# endif
33+
3034
static const char* LOG_TAG = "NimBLEUtils";
3135

3236
/**
@@ -44,12 +48,14 @@ bool NimBLEUtils::taskWait(const NimBLETaskData& taskData, uint32_t timeout) {
4448
}
4549

4650
# if defined INC_FREERTOS_H
51+
uint32_t notificationValue;
52+
xTaskNotifyWait(0, TASK_BLOCK_BIT, &notificationValue, 0);
53+
if (notificationValue & TASK_BLOCK_BIT) {
54+
return true;
55+
}
56+
4757
taskData.m_pHandle = xTaskGetCurrentTaskHandle();
48-
# ifdef ulTaskNotifyValueClear
49-
// Clear the task notification value to ensure we block
50-
ulTaskNotifyValueClear(static_cast<TaskHandle_t>(taskData.m_pHandle), ULONG_MAX);
51-
# endif
52-
return ulTaskNotifyTake(pdTRUE, ticks) == pdTRUE;
58+
return xTaskNotifyWait(0, TASK_BLOCK_BIT, nullptr, ticks) == pdTRUE;
5359

5460
# else
5561
ble_npl_sem sem;
@@ -73,10 +79,10 @@ bool NimBLEUtils::taskWait(const NimBLETaskData& taskData, uint32_t timeout) {
7379
* @param [in] flags A return value to set in the task data structure.
7480
*/
7581
void NimBLEUtils::taskRelease(const NimBLETaskData& taskData, int flags) {
82+
taskData.m_flags = flags;
7683
if (taskData.m_pHandle != nullptr) {
77-
taskData.m_flags = flags;
7884
# if defined INC_FREERTOS_H
79-
xTaskNotifyGive(static_cast<TaskHandle_t>(taskData.m_pHandle));
85+
xTaskNotify(static_cast<TaskHandle_t>(taskData.m_pHandle), TASK_BLOCK_BIT, eSetBits);
8086
# else
8187
ble_npl_sem_release(static_cast<ble_npl_sem*>(taskData.m_pHandle));
8288
# endif

src/nimconfig.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@
148148
*/
149149
// #define CONFIG_NIMBLE_STACK_USE_MEM_POOLS 1
150150

151+
/**
152+
* @brief Un-comment to change the bit used to block tasks during BLE operations
153+
* that call NimBLEUtils::taskWait. This should be different than any other
154+
* task notification flag used in the system.
155+
*/
156+
// #define CONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT 31
157+
151158
/**********************************
152159
End Arduino user-config
153160
**********************************/
@@ -350,6 +357,10 @@
350357
#define CONFIG_NIMBLE_CPP_DEBUG_ASSERT_ENABLED 0
351358
#endif
352359

360+
#ifndef CONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT
361+
#define CONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT 31
362+
#endif
363+
353364
#if CONFIG_NIMBLE_CPP_DEBUG_ASSERT_ENABLED && !defined NDEBUG
354365
void nimble_cpp_assert(const char *file, unsigned line) __attribute((weak, noreturn));
355366
# define NIMBLE_ATT_VAL_FILE (__builtin_strrchr(__FILE__, '/') ? \

0 commit comments

Comments
 (0)