Skip to content

Commit ed21292

Browse files
committed
Set task handle in constructor of NimBLETaskData
1 parent 82098d1 commit ed21292

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/NimBLEUtils.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ constexpr uint32_t TASK_BLOCK_BIT = (1 << CONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_
3333

3434
static const char* LOG_TAG = "NimBLEUtils";
3535

36+
/**
37+
* @brief Constructor.
38+
* @param [in] pInstance An instance of the class that is waiting.
39+
* @param [in] flags General purpose flags for the caller.
40+
* @param [in] buf A buffer for data.
41+
*/
42+
NimBLETaskData::NimBLETaskData(void* pInstance = nullptr, int flags = 0, void* buf = nullptr)
43+
: m_pInstance(pInstance), m_flags(flags), m_pBuf(buf) {
44+
# if defined INC_FREERTOS_H
45+
m_pHandle = xTaskGetCurrentTaskHandle();
46+
# else
47+
m_pHandle = new ble_npl_sem;
48+
if (ble_npl_sem_init(static_cast<ble_npl_sem*>(m_pHandle), 0) != BLE_NPL_OK) {
49+
NIMBLE_LOGE(LOG_TAG, "Failed to create semaphore");
50+
delete m_pHandle;
51+
m_pHandle = nullptr;
52+
}
53+
# endif
54+
}
55+
3656
/**
3757
* @brief Blocks the calling task until released or timeout.
3858
* @param [in] taskData A pointer to the task data structure.
@@ -54,20 +74,13 @@ bool NimBLEUtils::taskWait(const NimBLETaskData& taskData, uint32_t timeout) {
5474
return true;
5575
}
5676

57-
taskData.m_pHandle = xTaskGetCurrentTaskHandle();
5877
return xTaskNotifyWait(0, TASK_BLOCK_BIT, nullptr, ticks) == pdTRUE;
5978

6079
# else
61-
ble_npl_sem sem;
62-
ble_npl_error_t err = ble_npl_sem_init(&sem, 0);
63-
if (err != BLE_NPL_OK) {
64-
NIMBLE_LOGE(LOG_TAG, "Failed to create semaphore");
65-
return false;
66-
}
67-
68-
taskData.m_pHandle = &sem;
69-
err = ble_npl_sem_pend(&sem, ticks);
70-
ble_npl_sem_deinit(&sem);
80+
ble_npl_sem* sem = static_cast<ble_npl_sem*>(taskData.m_pHandle);
81+
ble_npl_error_t err = ble_npl_sem_pend(sem, ticks);
82+
ble_npl_sem_deinit(sem);
83+
delete taskData.m_pHandle;
7184
taskData.m_pHandle = nullptr;
7285
return err == BLE_NPL_OK;
7386
# endif

src/NimBLEUtils.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@ class NimBLEAddress;
2121
* All items are optional, the m_pHandle will be set in taskWait().
2222
*/
2323
struct NimBLETaskData {
24-
/**
25-
* @brief Constructor.
26-
* @param [in] pInstance An instance of the class that is waiting.
27-
* @param [in] flags General purpose flags for the caller.
28-
* @param [in] buf A buffer for data.
29-
*/
30-
NimBLETaskData(void* pInstance = nullptr, int flags = 0, void* buf = nullptr)
31-
: m_pInstance(pInstance), m_flags(flags), m_pBuf(buf) {}
24+
NimBLETaskData(void* pInstance = nullptr, int flags = 0, void* buf = nullptr);
3225
void* m_pInstance{nullptr};
3326
mutable int m_flags{0};
3427
void* m_pBuf{nullptr};

0 commit comments

Comments
 (0)