BLE: Add generic event filter.

This filter prevent events to be signaled multiple times to the upper layer. It
also signal events to a newly set event processor hook.
pull/5299/head
Vincent Coubard 2017-10-11 15:02:34 +01:00
parent 0025b685ea
commit 63668cb7d2
2 changed files with 26 additions and 1 deletions

View File

@ -1560,6 +1560,7 @@ private:
InstanceID_t instanceID;
BLEInstanceBase *transport; /* The device-specific backend */
OnEventsToProcessCallback_t whenEventsToProcess;
bool event_signaled;
};
typedef BLE BLEDevice; /**< @deprecated This type alias is retained for the

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
#include <stdio.h>
#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(&params);
}
}
void BLE::signalEventsToProcess()
{
if (event_signaled == true) {
return;
}
event_signaled = true;
if (whenEventsToProcess) {
OnEventsToProcessCallbackContext params = {
*this