@@ -287,10 +287,21 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttributes)
287287 ulTaskNotifyValueClear (cur_task, ULONG_MAX);
288288#endif
289289 // Wait for the connect timeout time +1 second for the connection to complete
290- if (ulTaskNotifyTake (pdTRUE, pdMS_TO_TICKS (m_connectTimeout + 1000 )) == pdFALSE) {
290+ if (ulTaskNotifyTake (pdTRUE, pdMS_TO_TICKS (m_connectTimeout + 1000 )) == pdFALSE) {
291291 m_pTaskData = nullptr ;
292- // If a connection was made but no response from MTU exchange; ignore
293- NIMBLE_LOGE (LOG_TAG, " Connection was made but no response from MTU exchange" );
292+ // If a connection was made but no response from MTU exchange; disconnect
293+ if (isConnected ()) {
294+ NIMBLE_LOGE (LOG_TAG, " Connect timeout - no response" );
295+ disconnect ();
296+ } else {
297+ // workaround; if the controller doesn't cancel the connection
298+ // at the timeout, cancel it here.
299+ NIMBLE_LOGE (LOG_TAG, " Connect timeout - cancelling" );
300+ ble_gap_conn_cancel ();
301+ }
302+
303+ return false ;
304+
294305 }
295306
296307 if (taskData.rc != 0 ){
@@ -910,6 +921,21 @@ uint16_t NimBLEClient::getMTU() {
910921} // getMTU
911922
912923
924+ int NimBLEClient::handleMtuEvent (uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t mtu, void *arg) {
925+ NimBLEClient* client = (NimBLEClient*)arg;
926+ NIMBLE_LOGD (LOG_TAG, " handleMtuEvent: conn_handle: %d, err %d, mtu: %d" , conn_handle, error->status , mtu);
927+
928+ if (client->m_pTaskData != nullptr ) {
929+ client->m_pTaskData ->rc = 0 ;
930+ if (client->m_pTaskData ->task ) {
931+ NIMBLE_LOGD (LOG_TAG, " GIVE: %d" , client->m_pTaskData ->rc );
932+ xTaskNotifyGive (client->m_pTaskData ->task );
933+ }
934+ client->m_pTaskData = nullptr ;
935+ }
936+ return 0 ;
937+ }
938+
913939/* *
914940 * @brief Handle a received GAP event.
915941 * @param [in] event The event structure sent by the NimBLE stack.
@@ -988,11 +1014,16 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event *event, void *arg) {
9881014 }
9891015
9901016 client->m_conn_id = event->connect .conn_handle ;
991-
992- rc = ble_gattc_exchange_mtu (client->m_conn_id , NULL , NULL );
993- if (rc != 0 ) {
994- NIMBLE_LOGE (LOG_TAG, " MTU exchange error; rc=%d %s" ,
995- rc, NimBLEUtils::returnCodeToString (rc));
1017+ if (ble_att_preferred_mtu () > 23 ) {
1018+ NIMBLE_LOGI (LOG_TAG, " MTU exchange" );
1019+ rc = ble_gattc_exchange_mtu (client->m_conn_id , handleMtuEvent, client);
1020+ if (rc != 0 ) {
1021+ NIMBLE_LOGE (LOG_TAG, " MTU exchange error; rc=%d %s" ,
1022+ rc, NimBLEUtils::returnCodeToString (rc));
1023+ break ;
1024+ }
1025+ } else {
1026+ rc = 0 ;
9961027 break ;
9971028 }
9981029
@@ -1129,8 +1160,8 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event *event, void *arg) {
11291160 NIMBLE_LOGI (LOG_TAG, " mtu update event; conn_handle=%d mtu=%d" ,
11301161 event->mtu .conn_handle ,
11311162 event->mtu .value );
1132- rc = 0 ;
1133- break ;
1163+
1164+ return 0 ;
11341165 } // BLE_GAP_EVENT_MTU
11351166
11361167 case BLE_GAP_EVENT_PASSKEY_ACTION: {
@@ -1199,6 +1230,7 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event *event, void *arg) {
11991230 if (client->m_pTaskData != nullptr ) {
12001231 client->m_pTaskData ->rc = rc;
12011232 if (client->m_pTaskData ->task ) {
1233+ NIMBLE_LOGD (LOG_TAG, " GIVE: %d" , client->m_pTaskData ->rc );
12021234 xTaskNotifyGive (client->m_pTaskData ->task );
12031235 }
12041236 client->m_pTaskData = nullptr ;
0 commit comments