1616#define CEIL_DIVIDE (a, b ) (((a) + (b) - 1 ) / (b))
1717#define ROUND_DIVIDE (a, b ) (((a) + (b) / 2 ) / (b))
1818// Retry
19- constexpr TickType_t RetryTimeout = pdMS_TO_TICKS( 50 ) ;
19+ constexpr uint32_t RetryTimeout = 50 ;
2020constexpr int RetryCounter = 3 ;
2121
2222NimBLEL2CAPChannel::NimBLEL2CAPChannel (uint16_t psm, uint16_t mtu, NimBLEL2CAPChannelCallbacks* callbacks)
@@ -65,8 +65,6 @@ bool NimBLEL2CAPChannel::setupMemPool() {
6565 return false ;
6666 }
6767
68- this ->stalledSemaphore = xSemaphoreCreateBinary ();
69-
7068 return true ;
7169}
7270
@@ -83,7 +81,10 @@ int NimBLEL2CAPChannel::writeFragment(std::vector<uint8_t>::const_iterator begin
8381
8482 if (stalled) {
8583 NIMBLE_LOGD (LOG_TAG, " L2CAP Channel waiting for unstall..." );
86- xSemaphoreTake (this ->stalledSemaphore , portMAX_DELAY);
84+ NimBLETaskData taskData;
85+ m_pTaskData = &taskData;
86+ NimBLEUtils::taskWait (m_pTaskData, BLE_NPL_TIME_FOREVER);
87+ m_pTaskData = nullptr ;
8788 stalled = false ;
8889 NIMBLE_LOGD (LOG_TAG, " L2CAP Channel unstalled!" );
8990 }
@@ -125,7 +126,7 @@ int NimBLEL2CAPChannel::writeFragment(std::vector<uint8_t>::const_iterator begin
125126 case BLE_HS_EBUSY:
126127 NIMBLE_LOGD (LOG_TAG, " ble_l2cap_send returned %d. Retrying shortly..." , res);
127128 os_mbuf_free_chain (txd);
128- vTaskDelay ( RetryTimeout);
129+ ble_npl_time_delay ( ble_npl_time_ms_to_ticks32 ( RetryTimeout) );
129130 continue ;
130131
131132 case ESP_OK:
@@ -247,8 +248,11 @@ int NimBLEL2CAPChannel::handleDataReceivedEvent(struct ble_l2cap_event* event) {
247248}
248249
249250int NimBLEL2CAPChannel::handleTxUnstalledEvent (struct ble_l2cap_event * event) {
251+ if (m_pTaskData != nullptr ) {
252+ NimBLEUtils::taskRelease (*m_pTaskData, event->tx_unstalled .status );
253+ }
254+
250255 NIMBLE_LOGI (LOG_TAG, " L2CAP COC 0x%04X transmit unstalled." , psm);
251- xSemaphoreGive (this ->stalledSemaphore );
252256 return 0 ;
253257}
254258
@@ -268,7 +272,7 @@ int NimBLEL2CAPChannel::handleL2capEvent(struct ble_l2cap_event *event, void *ar
268272 int returnValue = 0 ;
269273
270274 switch (event->type ) {
271- case BLE_L2CAP_EVENT_COC_CONNECTED:
275+ case BLE_L2CAP_EVENT_COC_CONNECTED:
272276 returnValue = self->handleConnectionEvent (event);
273277 break ;
274278
0 commit comments