@@ -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
@@ -736,9 +726,8 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID* uuidFilter) {
736726 return false ;
737727 }
738728
739- int rc = 0 ;
740- TaskHandle_t cur_task = xTaskGetCurrentTaskHandle ();
741- BleTaskData taskData = {this , cur_task, 0 , nullptr };
729+ int rc = 0 ;
730+ NimBLETaskData taskData (this );
742731
743732 if (uuidFilter == nullptr ) {
744733 rc = ble_gattc_disc_all_svcs (m_connHandle, NimBLEClient::serviceDiscoveredCB, &taskData);
@@ -752,24 +741,15 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID* uuidFilter) {
752741 return false ;
753742 }
754743
755- # ifdef ulTaskNotifyValueClear
756- // Clear the task notification value to ensure we block
757- ulTaskNotifyValueClear (cur_task, ULONG_MAX);
758- # endif
759-
760- // wait until we have all the services
761- ulTaskNotifyTake (pdTRUE, portMAX_DELAY);
762- m_lastErr = taskData.rc ;
763-
764- if (taskData.rc == 0 ) {
744+ NimBLEUtils::taskWait (taskData, BLE_NPL_TIME_FOREVER);
745+ rc = taskData.m_flags ;
746+ if (rc == 0 || rc == BLE_HS_EDONE) {
765747 return true ;
766- } else {
767- NIMBLE_LOGE (LOG_TAG,
768- " Could not retrieve services, rc=%d %s" ,
769- taskData.rc ,
770- NimBLEUtils::returnCodeToString (taskData.rc ));
771- return false ;
772748 }
749+
750+ m_lastErr = rc;
751+ NIMBLE_LOGE (LOG_TAG, " Could not retrieve services, rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
752+ return false ;
773753} // getServices
774754
775755/* *
@@ -786,8 +766,8 @@ int NimBLEClient::serviceDiscoveredCB(uint16_t connHandle,
786766 error->status ,
787767 (error->status == 0 ) ? service->start_handle : -1 );
788768
789- BleTaskData* pTaskData = (BleTaskData *)arg;
790- NimBLEClient* pClient = (NimBLEClient*)pTaskData->pATT ;
769+ NimBLETaskData* pTaskData = (NimBLETaskData *)arg;
770+ NimBLEClient* pClient = (NimBLEClient*)pTaskData->m_pInstance ;
791771
792772 // Make sure the service discovery is for this device
793773 if (pClient->getConnHandle () != connHandle) {
@@ -800,15 +780,7 @@ int NimBLEClient::serviceDiscoveredCB(uint16_t connHandle,
800780 return 0 ;
801781 }
802782
803- if (error->status == BLE_HS_EDONE) {
804- pTaskData->rc = 0 ;
805- } else {
806- NIMBLE_LOGE (LOG_TAG, " serviceDiscoveredCB() rc=%d %s" , error->status , NimBLEUtils::returnCodeToString (error->status ));
807- pTaskData->rc = error->status ;
808- }
809-
810- xTaskNotifyGive (pTaskData->task );
811-
783+ NimBLEUtils::taskRelease (*pTaskData, error->status );
812784 NIMBLE_LOGD (LOG_TAG, " << Service Discovered" );
813785 return error->status ;
814786}
@@ -1204,10 +1176,7 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
12041176 } // Switch
12051177
12061178 if (pClient->m_pTaskData != nullptr ) {
1207- pClient->m_pTaskData ->rc = rc;
1208- if (pClient->m_pTaskData ->task ) {
1209- xTaskNotifyGive (pClient->m_pTaskData ->task );
1210- }
1179+ NimBLEUtils::taskRelease (*pClient->m_pTaskData , rc);
12111180 pClient->m_pTaskData = nullptr ;
12121181 }
12131182
0 commit comments