@@ -33,6 +33,45 @@ constexpr uint32_t TASK_BLOCK_BIT = (1 << CONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_
3333
3434static 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
0 commit comments