@@ -56,11 +56,11 @@ 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 ;
@@ -70,11 +70,9 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e
7070
7171BLEConnection::~BLEConnection ()
7272{
73- vSemaphoreDelete ( _hvn_sem );
74- vSemaphoreDelete ( _wrcmd_sem );
75-
76- // ------------- on-the-fly data must be freed -------------//
77- if (_hvc_sem ) vSemaphoreDelete (_hvc_sem );
73+ vSemaphoreDelete (_hvn_sem);
74+ vSemaphoreDelete (_wrcmd_sem);
75+ vSemaphoreDelete (_hvc_sem);
7876}
7977
8078uint16_t BLEConnection::handle (void )
@@ -289,25 +287,22 @@ bool BLEConnection::requestPairing(void)
289287
290288void BLEConnection::setIndicateConfirmTimeout (uint32_t timeout_ms)
291289{
292- _indicate_confirm_timeout = (timeout_ms == portMAX_DELAY ) ? portMAX_DELAY : pdMS_TO_TICKS (timeout_ms);
290+ _indicate_confirm_timeout = (timeout_ms == UINT32_MAX ) ? portMAX_DELAY : pdMS_TO_TICKS (timeout_ms);
293291}
294292
295- bool BLEConnection::waitForIndicateConfirm (void )
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 )
296297{
297- // Lazy-allocate once per connection and reuse. Avoids a create/delete race
298- // with the BLE event handler that may give the semaphore on HVC/timeout.
299- if (_hvc_sem == NULL )
300- {
301- _hvc_sem = xSemaphoreCreateBinary ();
302- if (_hvc_sem == NULL ) return false ;
303- }
304-
305- // Drain any stale signal left from a previous call (e.g. late HVC after timeout)
306298 xSemaphoreTake (_hvc_sem, 0 );
307-
308299 _hvc_received = false ;
309- xSemaphoreTake (_hvc_sem, _indicate_confirm_timeout);
300+ return true ;
301+ }
310302
303+ bool BLEConnection::waitForIndicateConfirm (void )
304+ {
305+ xSemaphoreTake (_hvc_sem, _indicate_confirm_timeout);
311306 return _hvc_received;
312307}
313308
@@ -440,7 +435,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
440435 LOG_LV2 (" GATTS" , " Confirm received handle = 0x%04X" , evt->evt .gatts_evt .params .hvc .handle );
441436
442437 _hvc_received = true ;
443- if ( _hvc_sem ) xSemaphoreGive (_hvc_sem);
438+ xSemaphoreGive (_hvc_sem);
444439 }
445440 break ;
446441
@@ -452,7 +447,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
452447 if (BLE_GATT_TIMEOUT_SRC_PROTOCOL == timeout_src)
453448 {
454449 _hvc_received = false ;
455- if ( _hvc_sem ) xSemaphoreGive (_hvc_sem);
450+ xSemaphoreGive (_hvc_sem);
456451 }
457452 }
458453 break ;
0 commit comments