Skip to content

Commit 6a5d6ef

Browse files
committed
Set task handle in constructor of NimBLETaskData.
* Create destructor for NimBLETaskData to delete semaphore if created.
1 parent 5aa2fb1 commit 6a5d6ef

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

src/NimBLEUtils.cpp

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,45 @@ 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 Construct a NimBLETaskData instance.
38+
* @param [in] pInstance An instance of the class that will be 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, int flags, void* buf)
43+
: m_pInstance{pInstance},
44+
m_flags{flags},
45+
m_pBuf{buf}
46+
# if defined INC_FREERTOS_H
47+
,
48+
m_pHandle{xTaskGetCurrentTaskHandle()} {
49+
}
50+
# else
51+
{
52+
ble_npl_sem* sem = new ble_npl_sem;
53+
if (ble_npl_sem_init(sem, 0) != BLE_NPL_OK) {
54+
NIMBLE_LOGE(LOG_TAG, "Failed to init semaphore");
55+
delete sem;
56+
m_pHandle = nullptr;
57+
} else {
58+
m_pHandle = sem;
59+
}
60+
}
61+
# endif
62+
63+
/**
64+
* @brief Destructor.
65+
*/
66+
NimBLETaskData::~NimBLETaskData() {
67+
# if !defined INC_FREERTOS_H
68+
if (m_pHandle != nullptr) {
69+
ble_npl_sem_deinit(static_cast<ble_npl_sem*>(m_pHandle));
70+
delete static_cast<ble_npl_sem*>(m_pHandle);
71+
}
72+
# endif
73+
}
74+
3675
/**
3776
* @brief Blocks the calling task until released or timeout.
3877
* @param [in] taskData A pointer to the task data structure.
@@ -54,22 +93,10 @@ bool NimBLEUtils::taskWait(const NimBLETaskData& taskData, uint32_t timeout) {
5493
return true;
5594
}
5695

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

6098
# 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);
71-
taskData.m_pHandle = nullptr;
72-
return err == BLE_NPL_OK;
99+
return ble_npl_sem_pend(static_cast<ble_npl_sem*>(taskData.m_pHandle), ticks) == BLE_NPL_OK;
73100
# endif
74101
} // taskWait
75102

src/NimBLEUtils.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,8 @@ 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);
25+
~NimBLETaskData();
3226
void* m_pInstance{nullptr};
3327
mutable int m_flags{0};
3428
void* m_pBuf{nullptr};

0 commit comments

Comments
 (0)