Skip to content

Commit 41a24c3

Browse files
committed
Introduce new GattServer::EventHandler callback functions
1 parent 11171c6 commit 41a24c3

File tree

4 files changed

+173
-8
lines changed

4 files changed

+173
-8
lines changed

connectivity/FEATURE_BLE/include/ble/GattServer.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#ifndef MBED_GATT_SERVER_H__
2121
#define MBED_GATT_SERVER_H__
2222

23+
#include "platform/mbed_toolchain.h"
24+
2325
#include "ble/common/CallChainOfFunctionPointersWithContext.h"
2426
#include "ble/common/blecommon.h"
2527

@@ -118,6 +120,84 @@ class GattServer {
118120
(void)connectionHandle;
119121
(void)attMtuSize;
120122
}
123+
124+
/**
125+
* Function invoked when the server has sent data to a client as
126+
* part of a notification/indication.
127+
*
128+
* @note params has a temporary scope and should be copied by the
129+
* application if needed later
130+
*/
131+
virtual void onDataSent(const GattDataSentCallbackParams &params) {
132+
(void)params;
133+
}
134+
135+
/**
136+
* Function invoked when a client writes an attribute
137+
*
138+
* @note params has a temporary scope and should be copied by the
139+
* application if needed later
140+
*/
141+
virtual void onDataWritten(const GattWriteCallbackParams &params) {
142+
(void)params;
143+
}
144+
145+
/**
146+
* Function invoked when a client reads an attribute
147+
*
148+
* @note This functionality may not be available on all underlying stacks.
149+
* Application code may work around that limitation by monitoring read
150+
* requests instead of read events.
151+
*
152+
* @note params has a temporary scope and should be copied by the
153+
* application if needed later
154+
*
155+
* @see GattCharacteristic::setReadAuthorizationCallback()
156+
* @see isOnDataReadAvailable().
157+
*/
158+
virtual void onDataRead(const GattReadCallbackParams &params) {
159+
(void)params;
160+
}
161+
162+
/**
163+
* Function invoked when the GattServer instance is about
164+
* to be shut down. This can result in a call to reset() or BLE::reset().
165+
*/
166+
virtual void onShutdown(const GattServer &server) {
167+
(void)server;
168+
}
169+
170+
/**
171+
* Function invoked when the client has subscribed to characteristic updates
172+
*
173+
* @note params has a temporary scope and should be copied by the
174+
* application if needed later
175+
*/
176+
virtual void onUpdatesEnabled(const GattUpdatesEnabledCallbackParams &params) {
177+
(void)params;
178+
}
179+
180+
/**
181+
* Function invoked when the client has unsubscribed to characteristic updates
182+
*
183+
* @note params has a temporary scope and should be copied by the
184+
* application if needed later
185+
*/
186+
virtual void onUpdatesDisabled(const GattUpdatesDisabledCallbackParams &params) {
187+
(void)params;
188+
}
189+
190+
/**
191+
* Function invoked when an ACK has been received for an
192+
* indication sent to the client.
193+
*
194+
* @note params has a temporary scope and should be copied by the
195+
* application if needed later
196+
*/
197+
virtual void onConfirmationReceived(const GattConfirmationReceivedCallbackParams &params) {
198+
(void)params;
199+
}
200+
121201
protected:
122202
/**
123203
* Prevent polymorphic deletion and avoid unnecessary virtual destructor
@@ -407,6 +487,8 @@ class GattServer {
407487
* @note It is possible to chain together multiple onDataSent callbacks
408488
* (potentially from different modules of an application).
409489
*/
490+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
491+
"been replaced by GattServer::setEventHandler. Use that function instead.")
410492
void onDataSent(const DataSentCallback_t &callback);
411493

412494
/**
@@ -419,6 +501,8 @@ class GattServer {
419501
* function.
420502
*/
421503
template <typename T>
504+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
505+
"been replaced by GattServer::setEventHandler. Use that function instead.")
422506
void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count))
423507
{
424508
onDataSent({objPtr, memberPtr});
@@ -429,6 +513,8 @@ class GattServer {
429513
*
430514
* @return A reference to the DATA_SENT event callback chain.
431515
*/
516+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
517+
"been replaced by GattServer::setEventHandler. Use that function instead.")
432518
DataSentCallbackChain_t &onDataSent();
433519

434520
/**
@@ -440,6 +526,8 @@ class GattServer {
440526
* @attention It is possible to set multiple event handlers. Registered
441527
* handlers may be removed with onDataWritten().detach(callback).
442528
*/
529+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
530+
"been replaced by GattServer::setEventHandler. Use that function instead.")
443531
void onDataWritten(const DataWrittenCallback_t &callback);
444532

445533
/**
@@ -452,6 +540,8 @@ class GattServer {
452540
* function.
453541
*/
454542
template <typename T>
543+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
544+
"been replaced by GattServer::setEventHandler. Use that function instead.")
455545
void onDataWritten(
456546
T *objPtr,
457547
void (T::*memberPtr)(const GattWriteCallbackParams *context)
@@ -471,6 +561,8 @@ class GattServer {
471561
* @note It is possible to unregister callbacks using
472562
* onDataWritten().detach(callback).
473563
*/
564+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
565+
"been replaced by GattServer::setEventHandler. Use that function instead.")
474566
DataWrittenCallbackChain_t &onDataWritten();
475567

476568
/**
@@ -491,6 +583,8 @@ class GattServer {
491583
* @attention It is possible to set multiple event handlers. Registered
492584
* handlers may be removed with onDataRead().detach(callback).
493585
*/
586+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
587+
"been replaced by GattServer::setEventHandler. Use that function instead.")
494588
ble_error_t onDataRead(const DataReadCallback_t &callback);
495589

496590
/**
@@ -502,6 +596,8 @@ class GattServer {
502596
* function.
503597
*/
504598
template <typename T>
599+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
600+
"been replaced by GattServer::setEventHandler. Use that function instead.")
505601
ble_error_t onDataRead(
506602
T *objPtr,
507603
void (T::*memberPtr)(const GattReadCallbackParams *context)
@@ -521,6 +617,8 @@ class GattServer {
521617
* @note It is possible to unregister callbacks using
522618
* onDataRead().detach(callback).
523619
*/
620+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
621+
"been replaced by GattServer::setEventHandler. Use that function instead.")
524622
DataReadCallbackChain_t &onDataRead();
525623

526624
/**
@@ -536,6 +634,8 @@ class GattServer {
536634
* @note It is possible to unregister a callback using
537635
* onShutdown().detach(callback)
538636
*/
637+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
638+
"been replaced by GattServer::setEventHandler. Use that function instead.")
539639
void onShutdown(const GattServerShutdownCallback_t &callback);
540640

541641
/**
@@ -550,6 +650,8 @@ class GattServer {
550650
* function.
551651
*/
552652
template <typename T>
653+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
654+
"been replaced by GattServer::setEventHandler. Use that function instead.")
553655
void onShutdown(T *objPtr, void (T::*memberPtr)(const GattServer *))
554656
{
555657
onShutdown({objPtr, memberPtr});
@@ -566,6 +668,8 @@ class GattServer {
566668
* @note It is possible to unregister callbacks using
567669
* onShutdown().detach(callback).
568670
*/
671+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
672+
"been replaced by GattServer::setEventHandler. Use that function instead.")
569673
GattServerShutdownCallbackChain_t& onShutdown();
570674

571675
/**
@@ -574,6 +678,8 @@ class GattServer {
574678
*
575679
* @param[in] callback Event handler being registered.
576680
*/
681+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
682+
"been replaced by GattServer::setEventHandler. Use that function instead.")
577683
void onUpdatesEnabled(EventCallback_t callback);
578684

579685
/**
@@ -582,6 +688,8 @@ class GattServer {
582688
*
583689
* @param[in] callback Event handler being registered.
584690
*/
691+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
692+
"been replaced by GattServer::setEventHandler. Use that function instead.")
585693
void onUpdatesDisabled(EventCallback_t callback);
586694

587695
/**
@@ -592,6 +700,8 @@ class GattServer {
592700
*
593701
* @param[in] callback Event handler being registered.
594702
*/
703+
MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have"
704+
"been replaced by GattServer::setEventHandler. Use that function instead.")
595705
void onConfirmationReceived(EventCallback_t callback);
596706

597707
#if !defined(DOXYGEN_ONLY)

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,31 @@ class ChainableGattServerEventHandler : public ble::GattServer::EventHandler,
4242
connectionHandle, attMtuSize);
4343
}
4444

45-
void onDataSent(const GattDataSentCallbackParams* params) override {
45+
void onDataSent(const GattDataSentCallbackParams &params) override {
4646
execute_on_all(&ble::GattServer::EventHandler::onDataSent, params);
4747
}
4848

49-
void onDataWritten(const GattWriteCallbackParams *params) override {
49+
void onDataWritten(const GattWriteCallbackParams &params) override {
5050
execute_on_all(&ble::GattServer::EventHandler::onDataWritten, params);
5151
}
5252

53-
void onDataRead(const GattReadCallbackParams *params) override {
53+
void onDataRead(const GattReadCallbackParams &params) override {
5454
execute_on_all(&ble::GattServer::EventHandler::onDataRead, params);
5555
}
5656

57-
void onShutdown(const GattServer *server) override {
57+
void onShutdown(const GattServer &server) override {
5858
execute_on_all(&ble::GattServer::EventHandler::onShutdown, server);
5959
}
6060

61-
void onUpdatesEnabled(const GattUpdatesEnabledCallbackParams* params) override {
61+
void onUpdatesEnabled(const GattUpdatesEnabledCallbackParams &params) override {
6262
execute_on_all(&ble::GattServer::EventHandler::onUpdatesEnabled, params);
6363
}
6464

65-
void onUpdatesDisabled(const GattUpdatesDisabledCallbackParams* params) override {
65+
void onUpdatesDisabled(const GattUpdatesDisabledCallbackParams &params) override {
6666
execute_on_all(&ble::GattServer::EventHandler::onUpdatesDisabled, params);
6767
}
6868

69-
void onConfirmationReceived(const GattConfirmationReceivedCallbackParams* params) override {
69+
void onConfirmationReceived(const GattConfirmationReceivedCallbackParams &params) override {
7070
execute_on_all(&ble::GattServer::EventHandler::onConfirmationReceived, params);
7171
}
7272

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ struct GattDataSentCallbackParams {
399399
/**
400400
* Attribute Handle to which the event applies
401401
*/
402-
GattAttribute::Handle_t handle;
402+
GattAttribute::Handle_t attHandle;
403403

404404
};
405405

connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,11 @@ GapAdvertisingData::Appearance GattServer::getAppearance()
902902
ble_error_t GattServer::reset(ble::GattServer* server)
903903
{
904904
/* Notify that the instance is about to shutdown */
905+
if(eventHandler) {
906+
eventHandler->onShutdown(*server);
907+
}
908+
909+
// Execute callbacks added with deprecated API
905910
shutdownCallChain.call(server);
906911
shutdownCallChain.clear();
907912

@@ -1506,11 +1511,21 @@ GattServer::EventHandler *GattServer::getEventHandler()
15061511

15071512
void GattServer::handleDataWrittenEvent(const GattWriteCallbackParams *params)
15081513
{
1514+
if(eventHandler) {
1515+
eventHandler->onDataWritten(*params);
1516+
}
1517+
1518+
// Execute callbacks added with deprecated API
15091519
dataWrittenCallChain.call(params);
15101520
}
15111521

15121522
void GattServer::handleDataReadEvent(const GattReadCallbackParams *params)
15131523
{
1524+
if(eventHandler) {
1525+
eventHandler->onDataRead(*params);
1526+
}
1527+
1528+
// Execute callbacks added with deprecated API
15141529
dataReadCallChain.call(params);
15151530
}
15161531

@@ -1522,22 +1537,62 @@ void GattServer::handleEvent(
15221537
{
15231538
switch (type) {
15241539
case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
1540+
1541+
if(eventHandler) {
1542+
GattUpdatesEnabledCallbackParams params({
1543+
.connHandle = connHandle,
1544+
.attHandle = attributeHandle
1545+
});
1546+
eventHandler->onUpdatesEnabled(params);
1547+
}
1548+
1549+
// Execute deprecated callback
15251550
if (updatesEnabledCallback) {
15261551
updatesEnabledCallback(attributeHandle);
15271552
}
15281553
break;
15291554
case GattServerEvents::GATT_EVENT_UPDATES_DISABLED:
1555+
1556+
if(eventHandler) {
1557+
GattUpdatesDisabledCallbackParams params({
1558+
.connHandle = connHandle,
1559+
.attHandle = attributeHandle
1560+
});
1561+
eventHandler->onUpdatesDisabled(params);
1562+
}
1563+
1564+
// Execute deprecated callback
15301565
if (updatesDisabledCallback) {
15311566
updatesDisabledCallback(attributeHandle);
15321567
}
15331568
break;
15341569
case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED:
1570+
1571+
if(eventHandler) {
1572+
GattConfirmationReceivedCallbackParams params({
1573+
.connHandle = connHandle,
1574+
.attHandle = attributeHandle
1575+
});
1576+
eventHandler->onConfirmationReceived(params);
1577+
}
1578+
1579+
// Execute deprecated callback
15351580
if (confirmationReceivedCallback) {
15361581
confirmationReceivedCallback(attributeHandle);
15371582
}
15381583
break;
15391584

15401585
case GattServerEvents::GATT_EVENT_DATA_SENT:
1586+
1587+
if(eventHandler) {
1588+
GattDataSentCallbackParams params({
1589+
.connHandle = connHandle,
1590+
.attHandle = attributeHandle
1591+
});
1592+
eventHandler->onDataSent(params);
1593+
}
1594+
1595+
// Execute deprecated callback
15411596
// Called every time a notification or indication has been sent
15421597
handleDataSentEvent(1);
15431598
break;

0 commit comments

Comments
 (0)