X Tutup
Skip to content

Commit 0957d7f

Browse files
committed
The latest versions of IDF include a new esp_timer library that seems to be incrementing the task notification values in unexpected places depending on other tasks in operation.
This causes task blocking to fail in client operations leading to exceptions and crashing. This is a workaround for this situation and will need to be reworked properly in the future.
1 parent 5facd89 commit 0957d7f

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

src/NimBLEClient.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
207207
m_peerAddress = address;
208208
}
209209

210-
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr};
210+
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
211+
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
211212
m_pTaskData = &taskData;
212213
int rc = 0;
213214

@@ -260,6 +261,10 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
260261
return false;
261262
}
262263

264+
#ifdef ulTaskNotifyValueClear
265+
// Clear the task notification value to ensure we block
266+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
267+
#endif
263268
// Wait for the connect timeout time +1 second for the connection to complete
264269
if(ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(m_connectTimeout + 1000)) == pdFALSE) {
265270
m_pTaskData = nullptr;
@@ -310,7 +315,8 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
310315
* @return True on success.
311316
*/
312317
bool NimBLEClient::secureConnection() {
313-
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr};
318+
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
319+
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
314320

315321
int retryCount = 1;
316322

@@ -324,6 +330,10 @@ bool NimBLEClient::secureConnection() {
324330
return false;
325331
}
326332

333+
#ifdef ulTaskNotifyValueClear
334+
// Clear the task notification value to ensure we block
335+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
336+
#endif
327337
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
328338
} while (taskData.rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--);
329339

@@ -670,7 +680,8 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) {
670680
}
671681

672682
int rc = 0;
673-
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr};
683+
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
684+
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
674685

675686
if(uuid_filter == nullptr) {
676687
rc = ble_gattc_disc_all_svcs(m_conn_id, NimBLEClient::serviceDiscoveredCB, &taskData);
@@ -685,6 +696,11 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) {
685696
return false;
686697
}
687698

699+
#ifdef ulTaskNotifyValueClear
700+
// Clear the task notification value to ensure we block
701+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
702+
#endif
703+
688704
// wait until we have all the services
689705
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
690706
m_lastErr = taskData.rc;

src/NimBLERemoteCharacteristic.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt
237237
}
238238

239239
int rc = 0;
240-
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr};
240+
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
241+
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
241242

242243
// If we don't know the end handle of this characteristic retrieve the next one in the service
243244
// The end handle is the next characteristic definition handle -1.
@@ -252,6 +253,10 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt
252253
return false;
253254
}
254255

256+
#ifdef ulTaskNotifyValueClear
257+
// Clear the task notification value to ensure we block
258+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
259+
#endif
255260
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
256261

257262
if (taskData.rc != 0) {
@@ -273,6 +278,10 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt
273278
return false;
274279
}
275280

281+
#ifdef ulTaskNotifyValueClear
282+
// Clear the task notification value to ensure we block
283+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
284+
#endif
276285
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
277286

278287
if (taskData.rc != 0) {
@@ -473,7 +482,8 @@ std::string NimBLERemoteCharacteristic::readValue(time_t *timestamp) {
473482

474483
int rc = 0;
475484
int retryCount = 1;
476-
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(),0, &value};
485+
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
486+
ble_task_data_t taskData = {this, cur_task, 0, &value};
477487

478488
do {
479489
rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0,
@@ -485,6 +495,10 @@ std::string NimBLERemoteCharacteristic::readValue(time_t *timestamp) {
485495
return value;
486496
}
487497

498+
#ifdef ulTaskNotifyValueClear
499+
// Clear the task notification value to ensure we block
500+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
501+
#endif
488502
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
489503
rc = taskData.rc;
490504

@@ -742,7 +756,8 @@ bool NimBLERemoteCharacteristic::writeValue(const uint8_t* data, size_t length,
742756
return (rc==0);
743757
}
744758

745-
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr};
759+
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
760+
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
746761

747762
do {
748763
if(length > mtu) {
@@ -762,6 +777,10 @@ bool NimBLERemoteCharacteristic::writeValue(const uint8_t* data, size_t length,
762777
return false;
763778
}
764779

780+
#ifdef ulTaskNotifyValueClear
781+
// Clear the task notification value to ensure we block
782+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
783+
#endif
765784
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
766785
rc = taskData.rc;
767786

src/NimBLERemoteDescriptor.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ std::string NimBLERemoteDescriptor::readValue() {
137137

138138
int rc = 0;
139139
int retryCount = 1;
140-
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(),0, &value};
140+
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
141+
ble_task_data_t taskData = {this, cur_task, 0, &value};
141142

142143
do {
143144
rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0,
@@ -149,6 +150,10 @@ std::string NimBLERemoteDescriptor::readValue() {
149150
return value;
150151
}
151152

153+
#ifdef ulTaskNotifyValueClear
154+
// Clear the task notification value to ensure we block
155+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
156+
#endif
152157
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
153158
rc = taskData.rc;
154159

@@ -289,7 +294,8 @@ bool NimBLERemoteDescriptor::writeValue(const uint8_t* data, size_t length, bool
289294
return (rc == 0);
290295
}
291296

292-
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr};
297+
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
298+
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
293299

294300
do {
295301
if(length > mtu) {
@@ -310,6 +316,10 @@ bool NimBLERemoteDescriptor::writeValue(const uint8_t* data, size_t length, bool
310316
return false;
311317
}
312318

319+
#ifdef ulTaskNotifyValueClear
320+
// Clear the task notification value to ensure we block
321+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
322+
#endif
313323
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
314324
rc = taskData.rc;
315325

src/NimBLERemoteService.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID *uuid_filter)
196196
NIMBLE_LOGD(LOG_TAG, ">> retrieveCharacteristics() for service: %s", getUUID().toString().c_str());
197197

198198
int rc = 0;
199-
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr};
199+
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
200+
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
200201

201202
if(uuid_filter == nullptr) {
202203
rc = ble_gattc_disc_all_chrs(m_pClient->getConnId(),
@@ -218,6 +219,10 @@ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID *uuid_filter)
218219
return false;
219220
}
220221

222+
#ifdef ulTaskNotifyValueClear
223+
// Clear the task notification value to ensure we block
224+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
225+
#endif
221226
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
222227

223228
if(taskData.rc == 0){

src/NimBLEScan.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,15 @@ NimBLEScanResults NimBLEScan::start(uint32_t duration, bool is_continue) {
356356
NIMBLE_LOGW(LOG_TAG, "Blocking scan called with duration = forever");
357357
}
358358

359-
ble_task_data_t taskData = {nullptr, xTaskGetCurrentTaskHandle(),0, nullptr};
359+
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
360+
ble_task_data_t taskData = {nullptr, cur_task, 0, nullptr};
360361
m_pTaskData = &taskData;
361362

362363
if(start(duration, nullptr, is_continue)) {
364+
#ifdef ulTaskNotifyValueClear
365+
// Clear the task notification value to ensure we block
366+
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
367+
#endif
363368
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
364369
}
365370

0 commit comments

Comments
 (0)
X Tutup