Convert Gap::timeout callback to a callback chain. Add 'boolean'
conversion and function operator syntax to CallChainOfFunctionPointerWithContext
This commit is contained in:
parent
162fb4f75b
commit
dfc03ce0e6
3 changed files with 51 additions and 6 deletions
|
@ -97,6 +97,14 @@ public:
|
|||
return common_add(new FunctionPointerWithContext<ContextType>(tptr, mptr));
|
||||
}
|
||||
|
||||
/** Add a function at the front of the chain.
|
||||
*
|
||||
* @param func The FunctionPointerWithContext to add.
|
||||
*/
|
||||
void add(const FunctionPointerWithContext<ContextType>& func) {
|
||||
common_add(new FunctionPointerWithContext<ContextType>(func));
|
||||
}
|
||||
|
||||
/**
|
||||
* Detach a function pointer from a callchain
|
||||
*
|
||||
|
@ -153,6 +161,29 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief same as above but const
|
||||
*/
|
||||
void call(ContextType context) const {
|
||||
if (chainHead) {
|
||||
chainHead->call(context);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief same as above but with function call operator
|
||||
*/
|
||||
void operator()(ContextType context) const {
|
||||
call(context);
|
||||
}
|
||||
|
||||
typedef void (CallChainOfFunctionPointersWithContext::*bool_type)() const;
|
||||
void True() const {}
|
||||
|
||||
operator bool_type() const {
|
||||
return chainHead == NULL ? 0 : &CallChainOfFunctionPointersWithContext::True;
|
||||
}
|
||||
|
||||
private:
|
||||
pFunctionPointerWithContext_t common_add(pFunctionPointerWithContext_t pf) {
|
||||
if (chainHead == NULL) {
|
||||
|
|
|
@ -94,6 +94,13 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Same as above
|
||||
*/
|
||||
void operator()(ContextType context) const {
|
||||
call(context);
|
||||
}
|
||||
|
||||
/** Same as above, workaround for mbed os FunctionPointer implementation. */
|
||||
void call(ContextType context) {
|
||||
_caller(this, context);
|
||||
|
|
19
ble/Gap.h
19
ble/Gap.h
|
@ -140,8 +140,9 @@ public:
|
|||
return (durationInMillis * 1000) / UNIT_1_25_MS;
|
||||
}
|
||||
|
||||
typedef FunctionPointerWithContext<TimeoutSource_t> TimeoutEventCallback_t;
|
||||
typedef CallChainOfFunctionPointersWithContext<TimeoutSource_t> TimeoutEventCallbackChain_t;
|
||||
|
||||
typedef void (*TimeoutEventCallback_t)(TimeoutSource_t source);
|
||||
typedef void (*ConnectionEventCallback_t)(const ConnectionCallbackParams_t *params);
|
||||
typedef void (*DisconnectionEventCallback_t)(const DisconnectionCallbackParams_t *params);
|
||||
typedef FunctionPointerWithContext<bool> RadioNotificationEventCallback_t;
|
||||
|
@ -893,7 +894,13 @@ public:
|
|||
* Set up a callback for timeout events. Refer to TimeoutSource_t for
|
||||
* possible event types.
|
||||
*/
|
||||
void onTimeout(TimeoutEventCallback_t callback) {timeoutCallback = callback;}
|
||||
void onTimeout(TimeoutEventCallback_t callback) {
|
||||
timeoutCallbackChain.add(callback);
|
||||
}
|
||||
|
||||
TimeoutEventCallbackChain_t& onTimeout() {
|
||||
return timeoutCallbackChain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append to a chain of callbacks to be invoked upon GAP connection.
|
||||
|
@ -956,7 +963,7 @@ protected:
|
|||
_scanResponse(),
|
||||
state(),
|
||||
scanningActive(false),
|
||||
timeoutCallback(NULL),
|
||||
timeoutCallbackChain(),
|
||||
radioNotificationCallback(),
|
||||
onAdvertisementReport(),
|
||||
connectionCallChain(),
|
||||
|
@ -1002,8 +1009,8 @@ public:
|
|||
}
|
||||
|
||||
void processTimeoutEvent(TimeoutSource_t source) {
|
||||
if (timeoutCallback) {
|
||||
timeoutCallback(source);
|
||||
if (timeoutCallbackChain) {
|
||||
timeoutCallbackChain(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1017,7 +1024,7 @@ protected:
|
|||
bool scanningActive;
|
||||
|
||||
protected:
|
||||
TimeoutEventCallback_t timeoutCallback;
|
||||
TimeoutEventCallbackChain_t timeoutCallbackChain;
|
||||
RadioNotificationEventCallback_t radioNotificationCallback;
|
||||
AdvertisementReportCallback_t onAdvertisementReport;
|
||||
CallChainOfFunctionPointersWithContext<const ConnectionCallbackParams_t*> connectionCallChain;
|
||||
|
|
Loading…
Reference in a new issue