@@ -56,23 +56,23 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e
5656
5757 _hvn_sem = xSemaphoreCreateCounting (hvn_qsize, hvn_qsize);
5858 _wrcmd_sem = xSemaphoreCreateCounting (wrcmd_qsize, wrcmd_qsize);
59+ _hvc_sem = xSemaphoreCreateBinary ();
5960
6061 _sec_mode.sm = _sec_mode.lv = 1 ; // default to open
6162
6263 _bonded = false ;
63- _hvc_sem = NULL ;
6464 _hvc_received = false ;
6565
6666 _ediv = 0xFFFF ;
67+
68+ _indicate_confirm_timeout = portMAX_DELAY;
6769}
6870
6971BLEConnection::~BLEConnection ()
7072{
71- vSemaphoreDelete ( _hvn_sem );
72- vSemaphoreDelete ( _wrcmd_sem );
73-
74- // ------------- on-the-fly data must be freed -------------//
75- if (_hvc_sem ) vSemaphoreDelete (_hvc_sem );
73+ vSemaphoreDelete (_hvn_sem);
74+ vSemaphoreDelete (_wrcmd_sem);
75+ vSemaphoreDelete (_hvc_sem);
7676}
7777
7878uint16_t BLEConnection::handle (void )
@@ -285,17 +285,24 @@ bool BLEConnection::requestPairing(void)
285285 return Bluefruit.Security ._authenticate (_conn_hdl);
286286}
287287
288- bool BLEConnection::waitForIndicateConfirm ( void )
288+ void BLEConnection::setIndicateConfirmTimeout ( uint32_t timeout_ms )
289289{
290- // on the fly semaphore
291- _hvc_sem = xSemaphoreCreateBinary ();
290+ _indicate_confirm_timeout = (timeout_ms == UINT32_MAX) ? portMAX_DELAY : pdMS_TO_TICKS (timeout_ms);
291+ }
292292
293+ // Must be called BEFORE sd_ble_gatts_hvx() to clear any stale HVC state from a
294+ // previous indication (e.g. a late HVC that arrived after a timeout). Pairs with
295+ // waitForIndicateConfirm().
296+ bool BLEConnection::prepareForIndicateConfirm (void )
297+ {
298+ xSemaphoreTake (_hvc_sem, 0 );
293299 _hvc_received = false ;
294- xSemaphoreTake (_hvc_sem, portMAX_DELAY);
295-
296- vSemaphoreDelete (_hvc_sem);
297- _hvc_sem = NULL ;
300+ return true ;
301+ }
298302
303+ bool BLEConnection::waitForIndicateConfirm (void )
304+ {
305+ xSemaphoreTake (_hvc_sem, _indicate_confirm_timeout);
299306 return _hvc_received;
300307}
301308
@@ -428,7 +435,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
428435 LOG_LV2 (" GATTS" , " Confirm received handle = 0x%04X" , evt->evt .gatts_evt .params .hvc .handle );
429436
430437 _hvc_received = true ;
431- if ( _hvc_sem ) xSemaphoreGive (_hvc_sem);
438+ xSemaphoreGive (_hvc_sem);
432439 }
433440 break ;
434441
@@ -440,7 +447,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
440447 if (BLE_GATT_TIMEOUT_SRC_PROTOCOL == timeout_src)
441448 {
442449 _hvc_received = false ;
443- if ( _hvc_sem ) xSemaphoreGive (_hvc_sem);
450+ xSemaphoreGive (_hvc_sem);
444451 }
445452 }
446453 break ;
0 commit comments