diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.cpp b/libraries/Bluefruit52Lib/src/BLEConnection.cpp index 9dd36bfe..942e41d5 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.cpp +++ b/libraries/Bluefruit52Lib/src/BLEConnection.cpp @@ -53,8 +53,8 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e _wrcmd_sem = xSemaphoreCreateCounting(wrcmd_qsize, wrcmd_qsize); _paired = false; - hvc_sem = NULL; - hvc_received = false; + _hvc_sem = NULL; + _hvc_received = false; pair_sem = NULL; ediv = 0xFFFF; // invalid ediv value bond_keys = NULL; @@ -64,6 +64,8 @@ BLEConnection::~BLEConnection() { vSemaphoreDelete( _hvn_sem ); vSemaphoreDelete( _wrcmd_sem ); + + if ( _hvc_sem ) vSemaphoreDelete( _hvc_sem ); } uint16_t BLEConnection::handle (void) @@ -112,6 +114,20 @@ bool BLEConnection::getWriteCmdPacket (void) return xSemaphoreTake(_wrcmd_sem, ms2tick(BLE_GENERIC_TIMEOUT)); } +bool BLEConnection::waitForIndicateConfirm(void) +{ + // on the fly semaphore + _hvc_sem = xSemaphoreCreateBinary(); + + _hvc_received = false; + xSemaphoreTake(_hvc_sem, portMAX_DELAY); + + vSemaphoreDelete(_hvc_sem); + _hvc_sem = NULL; + + return _hvc_received; +} + void BLEConnection::_eventHandler(ble_evt_t* evt) { switch(evt->header.evt_id) @@ -124,6 +140,28 @@ void BLEConnection::_eventHandler(ble_evt_t* evt) for(uint8_t i=0; ievt.gattc_evt.params.write_cmd_tx_complete.count; i++) xSemaphoreGive(_wrcmd_sem); break; + case BLE_GATTS_EVT_HVC: + { + LOG_LV2("GATTS", "Confirm received handle = 0x%04X", evt->evt.gatts_evt.params.hvc.handle); + + _hvc_received = true; + if ( _hvc_sem ) xSemaphoreGive(_hvc_sem); + } + break; + + case BLE_GATTS_EVT_TIMEOUT: + { + uint8_t timeout_src = evt->evt.gatts_evt.params.timeout.src; + LOG_LV2("GATTS", "Timeout Source = %d", timeout_src); + + if (BLE_GATT_TIMEOUT_SRC_PROTOCOL == timeout_src) + { + _hvc_received = false; + if ( _hvc_sem ) xSemaphoreGive(_hvc_sem); + } + } + break; + default: break; } } diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.h b/libraries/Bluefruit52Lib/src/BLEConnection.h index 011e9029..2a603a50 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.h +++ b/libraries/Bluefruit52Lib/src/BLEConnection.h @@ -52,14 +52,13 @@ class BLEConnection SemaphoreHandle_t _hvn_sem; SemaphoreHandle_t _wrcmd_sem; - public: - bool _paired; - // On-demand semaphore that are created on the fly - SemaphoreHandle_t hvc_sem; - bool hvc_received; + SemaphoreHandle_t _hvc_sem; + bool _hvc_received; + public: SemaphoreHandle_t pair_sem; + bool _paired; uint16_t ediv; bond_keys_t* bond_keys; // Shared keys with bonded device, size ~ 80 bytes @@ -81,6 +80,8 @@ class BLEConnection bool getHvnPacket(void); bool getWriteCmdPacket(void); + bool waitForIndicateConfirm(void); + /*------------------------------------------------------------------*/ /* INTERNAL USAGE ONLY * Although declare as public, it is meant to be invoked by internal code. diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index 65f6d5e3..424dce53 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -78,16 +78,7 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe bool BLEGatt::waitForIndicateConfirm(uint16_t conn_hdl) { BLEConnection* conn = Bluefruit.Gap.getConnection(conn_hdl); - - // hvi confirm semaphore is created on the fly - conn->hvc_sem = xSemaphoreCreateBinary(); - - xSemaphoreTake(conn->hvc_sem, portMAX_DELAY); - - vSemaphoreDelete(conn->hvc_sem); - conn->hvc_sem = NULL; - - return conn->hvc_received; + return conn->waitForIndicateConfirm(); } void BLEGatt::_eventHandler(ble_evt_t* evt) @@ -230,24 +221,6 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) } break; - case BLE_GATTS_EVT_HVC: - { - LOG_LV2("GATTS", "Confirm received handle = 0x%04X", evt->evt.gatts_evt.params.hvc.handle); - - if ( conn->hvc_sem ) xSemaphoreGive(conn->hvc_sem); - conn->hvc_received = true; - } - break; - - case BLE_GATTS_EVT_TIMEOUT: - { - LOG_LV2("GATTS", "Timeout Source = %d", evt->evt.gatts_evt.params.timeout.src); - - if ( conn->hvc_sem ) xSemaphoreGive(conn->hvc_sem); - conn->hvc_received = false; - } - break; - default: break; } }