diff --git a/libraries/BLE/src/BLERemoteCharacteristic.cpp b/libraries/BLE/src/BLERemoteCharacteristic.cpp index fbaae1a5..d6771044 100644 --- a/libraries/BLE/src/BLERemoteCharacteristic.cpp +++ b/libraries/BLE/src/BLERemoteCharacteristic.cpp @@ -237,6 +237,13 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event, break; } // ESP_GATTC_WRITE_CHAR_EVT + case ESP_GATTC_READ_DESCR_EVT: + case ESP_GATTC_WRITE_DESCR_EVT: + for (auto &myPair : m_descriptorMap) { + myPair.second->gattClientEventHandler( + event, gattc_if, evtParam); + } + break; default: break; @@ -468,7 +475,7 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback, if(!notifications) val[0] = 0x02; BLERemoteDescriptor* desc = getDescriptor(BLEUUID((uint16_t)0x2902)); if (desc != nullptr) - desc->writeValue(val, 2); + desc->writeValue(val, 2, true); } // End Register else { // If we weren't passed a callback function, then this is an unregistration. esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify( @@ -484,7 +491,7 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback, uint8_t val[] = {0x00, 0x00}; BLERemoteDescriptor* desc = getDescriptor((uint16_t)0x2902); if (desc != nullptr) - desc->writeValue(val, 2); + desc->writeValue(val, 2, true); } // End Unregister m_semaphoreRegForNotifyEvt.wait("registerForNotify"); diff --git a/libraries/BLE/src/BLERemoteDescriptor.cpp b/libraries/BLE/src/BLERemoteDescriptor.cpp index ad506aae..b1e0bef2 100644 --- a/libraries/BLE/src/BLERemoteDescriptor.cpp +++ b/libraries/BLE/src/BLERemoteDescriptor.cpp @@ -49,6 +49,23 @@ BLEUUID BLERemoteDescriptor::getUUID() { return m_uuid; } // getUUID +void BLERemoteDescriptor::gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam) { + switch(event) { + case ESP_GATTC_READ_DESCR_EVT: + if (evtParam->read.handle != getHandle()) + break; + m_semaphoreReadDescrEvt.give(); + break; + + case ESP_GATTC_WRITE_DESCR_EVT: + if (evtParam->write.handle != getHandle()) + break; + m_semaphoreWriteDescrEvt.give(); + break; + default: + break; + } +} std::string BLERemoteDescriptor::readValue() { log_v(">> readValue: %s", toString().c_str()); @@ -137,6 +154,8 @@ void BLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool response return; } + m_semaphoreWriteDescrEvt.take("writeValue"); + esp_err_t errRc = ::esp_ble_gattc_write_char_descr( m_pRemoteCharacteristic->getRemoteService()->getClient()->getGattcIf(), m_pRemoteCharacteristic->getRemoteService()->getClient()->getConnId(), @@ -149,6 +168,8 @@ void BLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool response if (errRc != ESP_OK) { log_e("esp_ble_gattc_write_char_descr: %d", errRc); } + + m_semaphoreWriteDescrEvt.wait("writeValue"); log_v("<< writeValue"); } // writeValue diff --git a/libraries/BLE/src/BLERemoteDescriptor.h b/libraries/BLE/src/BLERemoteDescriptor.h index 29efe573..ebd847f2 100644 --- a/libraries/BLE/src/BLERemoteDescriptor.h +++ b/libraries/BLE/src/BLERemoteDescriptor.h @@ -35,7 +35,7 @@ public: void writeValue(std::string newValue, bool response = false); void writeValue(uint8_t newValue, bool response = false); void setAuth(esp_gatt_auth_req_t auth); - + void gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam); private: friend class BLERemoteCharacteristic; @@ -49,6 +49,7 @@ private: std::string m_value; // Last received value of the descriptor. BLERemoteCharacteristic* m_pRemoteCharacteristic; // Reference to the Remote characteristic of which this descriptor is associated. FreeRTOS::Semaphore m_semaphoreReadDescrEvt = FreeRTOS::Semaphore("ReadDescrEvt"); + FreeRTOS::Semaphore m_semaphoreWriteDescrEvt = FreeRTOS::Semaphore("WriteDescrEvt"); esp_gatt_auth_req_t m_auth;