Skip to content

Commit f7ce777

Browse files
committed
Introduce ChainableGapEventHandler subclass of ChainableEventHandler
The ChainableGapEventHandler enables chaining together Gap::EventHandlers so separate parts of an application can be notified of Gap events as needed. The application can register multiple Gap::EventHandler objects to one ChainableGapEventHandler and then set the global Gap::EventHandler to the ChainableGapEventHandler. All registered EventHandlers will then be called when a Gap Event occurs.
1 parent 4466c25 commit f7ce777

File tree

5 files changed

+142
-65
lines changed

5 files changed

+142
-65
lines changed

connectivity/FEATURE_BLE/include/ble/Gap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,12 @@ class Gap {
598598
* module to signal events back to the application.
599599
*
600600
* @param handler Application implementation of an EventHandler.
601+
*
602+
* @note Multiple discrete EventHandler instances may be used by adding them
603+
* to a ChainableGapEventHandler and then setting the chain as the primary
604+
* Gap EventHandler using this function.
605+
*
606+
* @see ChainableGapEventHandler
601607
*/
602608
void setEventHandler(EventHandler *handler);
603609

connectivity/FEATURE_BLE/include/ble/GattServer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ class GattServer {
204204
* module to signal events back to the application.
205205
*
206206
* @param handler Application implementation of an EventHandler.
207+
*
208+
* @note Multiple discrete EventHandler instances may be used by adding them
209+
* to a ChainableGattServerEventHandler and then setting the chain as the primary
210+
* GattServer EventHandler using this function.
211+
*
212+
* @see ChainableGattServerEventHandler
207213
*/
208214
void setEventHandler(EventHandler *handler);
209215

connectivity/FEATURE_BLE/include/ble/common/ChainableEventHandler.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ChainableEventHandler
3333

3434
ChainableEventHandler() { }
3535

36-
virtual ~ChainableEventHandler() {
36+
~ChainableEventHandler() {
3737
// Clean up all nodes
3838
auto it = head;
3939
while(it) {
@@ -45,13 +45,15 @@ class ChainableEventHandler
4545

4646
/**
4747
* Add an EventHandler to be notified of events sent to
48-
* this ChainableGattServerEventHandler
48+
* this ChainableEventHandler
4949
*
5050
* @param[in] event_handler Handler to add to chain
51+
*
52+
* @retval true if adding EventHandler was successful, false otherwise
5153
*/
52-
void addEventHandler(T* event_handler) {
54+
bool addEventHandler(T* event_handler) {
5355
auto eh = new (std::nothrow) node_t { event_handler, nullptr };
54-
if(!eh) { return; }
56+
if(!eh) { return false; }
5557
if(!head) {
5658
head = eh;
5759
} else {
@@ -61,6 +63,7 @@ class ChainableEventHandler
6163
}
6264
it->next = eh;
6365
}
66+
return true;
6467
}
6568

6669
/**
@@ -93,10 +96,12 @@ class ChainableEventHandler
9396

9497
protected:
9598

96-
template<typename... Args>
97-
void execute_on_all(void (T::*fn)(Args...), Args&... args) {
99+
template<typename... FnArgs, typename... Args>
100+
void execute_on_all(void (T::*fn)(FnArgs...), Args&&... args) {
98101
auto it = head;
99102
while(it) {
103+
// we do not use std::forward, args have to remain lvalues
104+
// as they are passed to multiple handlers
100105
(it->eh->*fn)(args...);
101106
it = it->next;
102107
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2020 ARM Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef MBED_CHAINABLEGAPEVENTHANDLER_H_
20+
#define MBED_CHAINABLEGAPEVENTHANDLER_H_
21+
22+
#include "ble/Gap.h"
23+
#include "ble/common/ChainableEventHandler.h"
24+
25+
/**
26+
* Gap::EventHandler implementation that allows the application
27+
* to register multiple separate EventHandlers to be called when
28+
* Gap events happen.
29+
*/
30+
class ChainableGapEventHandler : public ble::Gap::EventHandler,
31+
public ChainableEventHandler<ble::Gap::EventHandler>
32+
{
33+
34+
public:
35+
36+
ChainableGapEventHandler() { }
37+
38+
~ChainableGapEventHandler() { }
39+
40+
void onScanRequestReceived(const ble::ScanRequestEvent &event) override {
41+
execute_on_all(&ble::Gap::EventHandler::onScanRequestReceived, event);
42+
}
43+
44+
void onAdvertisingEnd(const ble::AdvertisingEndEvent &event) override {
45+
execute_on_all(&ble::Gap::EventHandler::onAdvertisingEnd, event);
46+
}
47+
48+
void onAdvertisingReport(const ble::AdvertisingReportEvent &event) override {
49+
execute_on_all(&ble::Gap::EventHandler::onAdvertisingReport, event);
50+
}
51+
52+
void onScanTimeout(const ble::ScanTimeoutEvent &event) override {
53+
execute_on_all(&ble::Gap::EventHandler::onScanTimeout, event);
54+
}
55+
56+
void onPeriodicAdvertisingSyncEstablished(
57+
const ble::PeriodicAdvertisingSyncEstablishedEvent &event) override {
58+
execute_on_all(&ble::Gap::EventHandler::onPeriodicAdvertisingSyncEstablished, event);
59+
}
60+
61+
void onPeriodicAdvertisingReport(
62+
const ble::PeriodicAdvertisingReportEvent &event) override {
63+
execute_on_all(&ble::Gap::EventHandler::onPeriodicAdvertisingReport, event);
64+
}
65+
66+
void onPeriodicAdvertisingSyncLoss(
67+
const ble::PeriodicAdvertisingSyncLoss &event) override {
68+
execute_on_all(&ble::Gap::EventHandler::onPeriodicAdvertisingSyncLoss, event);
69+
}
70+
71+
void onConnectionComplete(const ble::ConnectionCompleteEvent &event) override {
72+
execute_on_all(&ble::Gap::EventHandler::onConnectionComplete, event);
73+
}
74+
75+
void onUpdateConnectionParametersRequest(
76+
const ble::UpdateConnectionParametersRequestEvent &event) override {
77+
execute_on_all(&ble::Gap::EventHandler::onUpdateConnectionParametersRequest, event);
78+
}
79+
80+
void onConnectionParametersUpdateComplete(
81+
const ble::ConnectionParametersUpdateCompleteEvent &event) override {
82+
execute_on_all(&ble::Gap::EventHandler::onConnectionParametersUpdateComplete, event);
83+
}
84+
85+
void onDisconnectionComplete(const ble::DisconnectionCompleteEvent &event) override {
86+
execute_on_all(&ble::Gap::EventHandler::onDisconnectionComplete, event);
87+
}
88+
89+
void onReadPhy(
90+
ble_error_t status,
91+
ble::connection_handle_t connectionHandle,
92+
ble::phy_t txPhy,
93+
ble::phy_t rxPhy) override {
94+
execute_on_all(&ble::Gap::EventHandler::onReadPhy, status,
95+
connectionHandle, txPhy, rxPhy);
96+
}
97+
98+
void onPhyUpdateComplete(
99+
ble_error_t status,
100+
ble::connection_handle_t connectionHandle,
101+
ble::phy_t txPhy,
102+
ble::phy_t rxPhy) override {
103+
execute_on_all(&ble::Gap::EventHandler::onPhyUpdateComplete, status,
104+
connectionHandle, txPhy, rxPhy);
105+
}
106+
107+
void onDataLengthChange(
108+
ble::connection_handle_t connectionHandle,
109+
uint16_t txSize,
110+
uint16_t rxSize) override {
111+
execute_on_all(&ble::Gap::EventHandler::onDataLengthChange,
112+
connectionHandle, txSize, rxSize);
113+
}
114+
115+
};
116+
117+
#endif /* MBED_CHAINABLEGAPEVENTHANDLER_H_ */

connectivity/FEATURE_BLE/include/ble/gatt/ChainableGattServerEventHandler.h

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -35,94 +35,37 @@ class ChainableGattServerEventHandler : public ble::GattServer::EventHandler,
3535

3636
ChainableGattServerEventHandler() { }
3737

38-
virtual ~ChainableGattServerEventHandler() { }
39-
40-
/**
41-
* Function invoked when the connections changes the ATT_MTU which controls
42-
* the maximum size of an attribute that can be read in a single L2CAP packet
43-
* which might be fragmented across multiple packets.
44-
*
45-
* @param connectionHandle The handle of the connection that changed the size.
46-
* @param attMtuSize
47-
*/
38+
~ChainableGattServerEventHandler() { }
39+
4840
void onAttMtuChange(ble::connection_handle_t connectionHandle, uint16_t attMtuSize) override {
4941
execute_on_all(&ble::GattServer::EventHandler::onAttMtuChange,
5042
connectionHandle, attMtuSize);
5143
}
5244

53-
/**
54-
* Function invoked when the server has sent data to a client as
55-
* part of a notification/indication.
56-
*
57-
* @note params has a temporary scope and should be copied by the
58-
* application if needed later
59-
*/
6045
void onDataSent(const GattDataSentCallbackParams* params) override {
6146
execute_on_all(&ble::GattServer::EventHandler::onDataSent, params);
6247
}
6348

64-
/**
65-
* Function invoked when a client writes an attribute
66-
*
67-
* @note params has a temporary scope and should be copied by the
68-
* application if needed later
69-
*/
7049
void onDataWritten(const GattWriteCallbackParams *params) override {
7150
execute_on_all(&ble::GattServer::EventHandler::onDataWritten, params);
7251
}
7352

74-
/**
75-
* Function invoked when a client reads an attribute
76-
*
77-
* @note This functionality may not be available on all underlying stacks.
78-
* Application code may work around that limitation by monitoring read
79-
* requests instead of read events.
80-
*
81-
* @note params has a temporary scope and should be copied by the
82-
* application if needed later
83-
*
84-
* @see GattCharacteristic::setReadAuthorizationCallback()
85-
* @see isOnDataReadAvailable().
86-
*/
8753
void onDataRead(const GattReadCallbackParams *params) override {
8854
execute_on_all(&ble::GattServer::EventHandler::onDataRead, params);
8955
}
9056

91-
/**
92-
* Function invoked when the GattServer instance is about
93-
* to be shut down. This can result in a call to reset() or BLE::reset().
94-
*/
9557
void onShutdown(const GattServer *server) override {
9658
execute_on_all(&ble::GattServer::EventHandler::onShutdown, server);
9759
}
9860

99-
/**
100-
* Function invoked when the client has subscribed to characteristic updates
101-
*
102-
* @note params has a temporary scope and should be copied by the
103-
* application if needed later
104-
*/
10561
void onUpdatesEnabled(const GattUpdatesEnabledCallbackParams* params) override {
10662
execute_on_all(&ble::GattServer::EventHandler::onUpdatesEnabled, params);
10763
}
10864

109-
/**
110-
* Function invoked when the client has unsubscribed to characteristic updates
111-
*
112-
* @note params has a temporary scope and should be copied by the
113-
* application if needed later
114-
*/
11565
void onUpdatesDisabled(const GattUpdatesDisabledCallbackParams* params) override {
11666
execute_on_all(&ble::GattServer::EventHandler::onUpdatesDisabled, params);
11767
}
11868

119-
/**
120-
* Function invoked when an ACK has been received for an
121-
* indication sent to the client.
122-
*
123-
* @note params has a temporary scope and should be copied by the
124-
* application if needed later
125-
*/
12669
void onConfirmationReceived(const GattConfirmationReceivedCallbackParams* params) override {
12770
execute_on_all(&ble::GattServer::EventHandler::onConfirmationReceived, params);
12871
}

0 commit comments

Comments
 (0)