diff --git a/features/FEATURE_BLE/ble/BLE.h b/features/FEATURE_BLE/ble/BLE.h index 560f6f02ea..0aea37dc32 100644 --- a/features/FEATURE_BLE/ble/BLE.h +++ b/features/FEATURE_BLE/ble/BLE.h @@ -1776,6 +1776,7 @@ private: InstanceID_t instanceID; BLEInstanceBase *transport; /* The device-specific backend */ OnEventsToProcessCallback_t whenEventsToProcess; + bool event_signaled; }; /** diff --git a/features/FEATURE_BLE/source/BLE.cpp b/features/FEATURE_BLE/source/BLE.cpp index d9349477a5..2d90b083bf 100644 --- a/features/FEATURE_BLE/source/BLE.cpp +++ b/features/FEATURE_BLE/source/BLE.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include "ble/BLE.h" #include "ble/BLEInstanceBase.h" @@ -139,7 +140,8 @@ void defaultSchedulingCallback(BLE::OnEventsToProcessCallbackContext* params) { BLE::BLE(InstanceID_t instanceIDIn) : instanceID(instanceIDIn), transport(), - whenEventsToProcess(defaultSchedulingCallback) + whenEventsToProcess(defaultSchedulingCallback), + event_signaled(false) { static BLEInstanceBase *transportInstances[NUM_INSTANCES]; @@ -168,6 +170,7 @@ ble_error_t BLE::shutdown(void) error("bad handle to underlying transport"); } + event_signaled = false; return transport->shutdown(); } @@ -263,20 +266,41 @@ void BLE::waitForEvent(void) void BLE::processEvents() { + if (event_signaled == false) { + return; + } + if (!transport) { error("bad handle to underlying transport"); } + event_signaled = false; + transport->processEvents(); } void BLE::onEventsToProcess(const BLE::OnEventsToProcessCallback_t& callback) { whenEventsToProcess = callback; + + // If events were previously signaled but the handler was not in place then + // signal immediately events availability + if (event_signaled && whenEventsToProcess) { + OnEventsToProcessCallbackContext params = { + *this + }; + whenEventsToProcess(¶ms); + } } void BLE::signalEventsToProcess() { + if (event_signaled == true) { + return; + } + + event_signaled = true; + if (whenEventsToProcess) { OnEventsToProcessCallbackContext params = { *this