Skip to content

Commit bb5c893

Browse files
committed
Lot's of tweaks to MTU negotiation to fix Apple Watch
1 parent c26e143 commit bb5c893

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

src/NimBLEClient.cpp

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/NimBLEClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class NimBLEClient {
8484
friend class NimBLEDevice;
8585
friend class NimBLERemoteService;
8686

87+
static int handleMtuEvent(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t mtu, void *arg);
8788
static int handleGapEvent(struct ble_gap_event *event, void *arg);
8889
static int serviceDiscoveredCB(uint16_t conn_handle,
8990
const struct ble_gatt_error *error,

src/nimble/nimble/host/src/ble_gattc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
* The maximum time to wait for a single ATT response. The spec defines this
7070
* as the ATT transaction time (Vol. 3, Part F, 3.3.3)
7171
*/
72+
#ifndef BLE_GATTC_UNRESPONSIVE_TIMEOUT_MS
7273
#define BLE_GATTC_UNRESPONSIVE_TIMEOUT_MS 30000 /* ms */
74+
#endif
7375

7476
#define BLE_GATT_OP_NONE UINT8_MAX
7577
#define BLE_GATT_OP_MTU 0

0 commit comments

Comments
 (0)