@@ -201,11 +201,10 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
201201 deleteServices ();
202202 }
203203
204- int rc = 0 ;
205- m_asyncConnect = asyncConnect;
206- m_exchangeMTU = exchangeMTU;
207- TaskHandle_t curTask = xTaskGetCurrentTaskHandle ();
208- BleTaskData taskData = {this , curTask, 0 , nullptr };
204+ int rc = 0 ;
205+ m_asyncConnect = asyncConnect;
206+ m_exchangeMTU = exchangeMTU;
207+ NimBLETaskData taskData (this );
209208 if (!asyncConnect) {
210209 m_pTaskData = &taskData;
211210 }
@@ -276,12 +275,8 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
276275 return true ;
277276 }
278277
279- # ifdef ulTaskNotifyValueClear
280- // Clear the task notification value to ensure we block
281- ulTaskNotifyValueClear (curTask, ULONG_MAX);
282- # endif
283278 // Wait for the connect timeout time +1 second for the connection to complete
284- if (ulTaskNotifyTake (pdTRUE, pdMS_TO_TICKS ( m_connectTimeout + 1000 )) == pdFALSE ) {
279+ if (! NimBLEUtils::taskWait (*m_pTaskData, m_connectTimeout + 1000 )) {
285280 m_pTaskData = nullptr ;
286281 // If a connection was made but no response from MTU exchange; disconnect
287282 if (isConnected ()) {
@@ -296,9 +291,9 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
296291
297292 return false ;
298293
299- } else if (taskData.rc != 0 ) {
300- m_lastErr = taskData.rc ;
301- NIMBLE_LOGE (LOG_TAG, " Connection failed; status=%d %s" , taskData. rc , NimBLEUtils::returnCodeToString (taskData. rc ));
294+ } else if (taskData.m_flags != 0 ) {
295+ m_lastErr = taskData.m_flags ;
296+ NIMBLE_LOGE (LOG_TAG, " Connection failed; status=%d %s" , m_lastErr , NimBLEUtils::returnCodeToString (m_lastErr ));
302297 // If the failure was not a result of a disconnection, make sure we disconnect now to avoid dangling connections
303298 if (isConnected ()) {
304299 disconnect ();
@@ -324,9 +319,8 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
324319 */
325320bool NimBLEClient::secureConnection () const {
326321 NIMBLE_LOGD (LOG_TAG, " >> secureConnection()" );
327- TaskHandle_t cur_task = xTaskGetCurrentTaskHandle ();
328- BleTaskData taskData = {const_cast <NimBLEClient*>(this ), cur_task, 0 , nullptr };
329- int retryCount = 1 ;
322+ NimBLETaskData taskData (const_cast <NimBLEClient*>(this ));
323+ int retryCount = 1 ;
330324
331325 do {
332326 m_pTaskData = &taskData;
@@ -337,16 +331,12 @@ bool NimBLEClient::secureConnection() const {
337331 return false ;
338332 }
339333
340- # ifdef ulTaskNotifyValueClear
341- // Clear the task notification value to ensure we block
342- ulTaskNotifyValueClear (cur_task, ULONG_MAX);
343- # endif
344- ulTaskNotifyTake (pdTRUE, portMAX_DELAY);
345- } while (taskData.rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--);
334+ NimBLEUtils::taskWait (*m_pTaskData, BLE_NPL_TIME_FOREVER);
335+ } while (taskData.m_flags == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--);
346336
347- if (taskData.rc != 0 ) {
348- m_lastErr = taskData.rc ;
349- NIMBLE_LOGE (LOG_TAG, " secureConnection: failed rc=%d" , taskData.rc );
337+ if (taskData.m_flags != 0 ) {
338+ m_lastErr = taskData.m_flags ;
339+ NIMBLE_LOGE (LOG_TAG, " secureConnection: failed rc=%d" , taskData.m_flags );
350340 return false ;
351341 }
352342
@@ -721,9 +711,8 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID* uuidFilter) {
721711 return false ;
722712 }
723713
724- int rc = 0 ;
725- TaskHandle_t cur_task = xTaskGetCurrentTaskHandle ();
726- BleTaskData taskData = {this , cur_task, 0 , nullptr };
714+ int rc = 0 ;
715+ NimBLETaskData taskData (this );
727716
728717 if (uuidFilter == nullptr ) {
729718 rc = ble_gattc_disc_all_svcs (m_connHandle, NimBLEClient::serviceDiscoveredCB, &taskData);
@@ -737,24 +726,15 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID* uuidFilter) {
737726 return false ;
738727 }
739728
740- # ifdef ulTaskNotifyValueClear
741- // Clear the task notification value to ensure we block
742- ulTaskNotifyValueClear (cur_task, ULONG_MAX);
743- # endif
744-
745- // wait until we have all the services
746- ulTaskNotifyTake (pdTRUE, portMAX_DELAY);
747- m_lastErr = taskData.rc ;
748-
749- if (taskData.rc == 0 ) {
729+ NimBLEUtils::taskWait (taskData, BLE_NPL_TIME_FOREVER);
730+ rc = taskData.m_flags ;
731+ if (rc == 0 || rc == BLE_HS_EDONE) {
750732 return true ;
751- } else {
752- NIMBLE_LOGE (LOG_TAG,
753- " Could not retrieve services, rc=%d %s" ,
754- taskData.rc ,
755- NimBLEUtils::returnCodeToString (taskData.rc ));
756- return false ;
757733 }
734+
735+ m_lastErr = rc;
736+ NIMBLE_LOGE (LOG_TAG, " Could not retrieve services, rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
737+ return false ;
758738} // getServices
759739
760740/* *
@@ -771,8 +751,8 @@ int NimBLEClient::serviceDiscoveredCB(uint16_t connHandle,
771751 error->status ,
772752 (error->status == 0 ) ? service->start_handle : -1 );
773753
774- BleTaskData* pTaskData = (BleTaskData *)arg;
775- NimBLEClient* pClient = (NimBLEClient*)pTaskData->pATT ;
754+ NimBLETaskData* pTaskData = (NimBLETaskData *)arg;
755+ NimBLEClient* pClient = (NimBLEClient*)pTaskData->m_pInstance ;
776756
777757 // Make sure the service discovery is for this device
778758 if (pClient->getConnHandle () != connHandle) {
@@ -785,15 +765,7 @@ int NimBLEClient::serviceDiscoveredCB(uint16_t connHandle,
785765 return 0 ;
786766 }
787767
788- if (error->status == BLE_HS_EDONE) {
789- pTaskData->rc = 0 ;
790- } else {
791- NIMBLE_LOGE (LOG_TAG, " serviceDiscoveredCB() rc=%d %s" , error->status , NimBLEUtils::returnCodeToString (error->status ));
792- pTaskData->rc = error->status ;
793- }
794-
795- xTaskNotifyGive (pTaskData->task );
796-
768+ NimBLEUtils::taskRelease (*pTaskData, error->status );
797769 NIMBLE_LOGD (LOG_TAG, " << Service Discovered" );
798770 return error->status ;
799771}
@@ -1189,10 +1161,7 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
11891161 } // Switch
11901162
11911163 if (pClient->m_pTaskData != nullptr ) {
1192- pClient->m_pTaskData ->rc = rc;
1193- if (pClient->m_pTaskData ->task ) {
1194- xTaskNotifyGive (pClient->m_pTaskData ->task );
1195- }
1164+ NimBLEUtils::taskRelease (*pClient->m_pTaskData , rc);
11961165 pClient->m_pTaskData = nullptr ;
11971166 }
11981167
0 commit comments