From eb1c64eb2dbf74e0b14d50f1856cc278a925c4ad Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Tue, 6 Oct 2020 16:48:19 -0400 Subject: [PATCH 1/6] Update parameters passed to onDataSent, onUpdatesEnabled/Disabled, and onConfirmationReceived callbacks --- .../FEATURE_BLE/include/ble/GattServer.h | 44 +++++++++++++------ .../include/ble/gatt/GattCallbackParamTypes.h | 23 ++++++++++ .../FEATURE_BLE/source/GattServer.cpp | 15 ++++--- .../source/cordio/source/GattServerImpl.cpp | 26 ++++++----- .../source/cordio/source/GattServerImpl.h | 19 ++++---- 5 files changed, 88 insertions(+), 39 deletions(-) diff --git a/connectivity/FEATURE_BLE/include/ble/GattServer.h b/connectivity/FEATURE_BLE/include/ble/GattServer.h index 6b1261f1f51..db4fa1e9a98 100644 --- a/connectivity/FEATURE_BLE/include/ble/GattServer.h +++ b/connectivity/FEATURE_BLE/include/ble/GattServer.h @@ -131,16 +131,42 @@ class GattServer { * * @see onDataSent(). */ - typedef FunctionPointerWithContext DataSentCallback_t; + typedef FunctionPointerWithContext + DataSentCallback_t; /** * Callchain of DataSentCallback_t objects. * * @see onDataSent(). */ - typedef CallChainOfFunctionPointersWithContext + typedef CallChainOfFunctionPointersWithContext DataSentCallbackChain_t; + /** + * Event handler invoked when the client has subscribed to characteristic updates + * + * @see onUpdatesEnabled(). + */ + typedef FunctionPointerWithContext + UpdatesEnabledCallback_t; + + /** + * Event handler invoked when the client has unsubscribed from characteristic updates + * + * @see onUpdatesDisabled(). + */ + typedef FunctionPointerWithContext + UpdatesDisabledCallback_t; + + /** + * Event handler invoked when the an ACK has been received for an + * indication sent to the client. + * + * @see onConfirmationReceived(). + */ + typedef FunctionPointerWithContext + ConfirmationReceivedCallback_t; + /** * Event handler invoked when the client has written an attribute of the * server. @@ -190,14 +216,6 @@ class GattServer { typedef CallChainOfFunctionPointersWithContext GattServerShutdownCallbackChain_t; - /** - * Event handler that handles subscription to characteristic updates, - * unsubscription from characteristic updates and notification confirmation. - * - * @see onUpdatesEnabled() onUpdateDisabled() onConfirmationReceived() - */ - typedef FunctionPointerWithContext EventCallback_t; - public: /** * Assign the event handler implementation that will be used by the @@ -568,7 +586,7 @@ class GattServer { * * @param[in] callback Event handler being registered. */ - void onUpdatesEnabled(EventCallback_t callback); + void onUpdatesEnabled(UpdatesEnabledCallback_t callback); /** * Set up an event handler that monitors unsubscription from characteristic @@ -576,7 +594,7 @@ class GattServer { * * @param[in] callback Event handler being registered. */ - void onUpdatesDisabled(EventCallback_t callback); + void onUpdatesDisabled(UpdatesDisabledCallback_t callback); /** * Set up an event handler that monitors notification acknowledgment. @@ -586,7 +604,7 @@ class GattServer { * * @param[in] callback Event handler being registered. */ - void onConfirmationReceived(EventCallback_t callback); + void onConfirmationReceived(ConfirmationReceivedCallback_t callback); #if !defined(DOXYGEN_ONLY) GattServer(impl::GattServer* impl) : impl(impl) {} diff --git a/connectivity/FEATURE_BLE/include/ble/gatt/GattCallbackParamTypes.h b/connectivity/FEATURE_BLE/include/ble/gatt/GattCallbackParamTypes.h index a425c94f7c4..670c59d57fb 100644 --- a/connectivity/FEATURE_BLE/include/ble/gatt/GattCallbackParamTypes.h +++ b/connectivity/FEATURE_BLE/include/ble/gatt/GattCallbackParamTypes.h @@ -384,6 +384,29 @@ struct GattHVXCallbackParams { }; +/** + * Gatt Data Sent Attribute related events + * + * Used by `onDataSent` + */ +struct GattDataSentCallbackParams { + + /** + * The handle of the connection that triggered the event. + */ + ble::connection_handle_t connHandle; + + /** + * Attribute Handle to which the event applies + */ + GattAttribute::Handle_t handle; + +}; + +using GattUpdatesEnabledCallbackParams = GattDataSentCallbackParams; +using GattUpdatesDisabledCallbackParams = GattDataSentCallbackParams; +using GattConfirmationReceivedCallbackParams = GattDataSentCallbackParams; + namespace ble { /** diff --git a/connectivity/FEATURE_BLE/source/GattServer.cpp b/connectivity/FEATURE_BLE/source/GattServer.cpp index 583903023e5..d8761d31599 100644 --- a/connectivity/FEATURE_BLE/source/GattServer.cpp +++ b/connectivity/FEATURE_BLE/source/GattServer.cpp @@ -137,19 +137,20 @@ GattServer::GattServerShutdownCallbackChain_t& GattServer::onShutdown() return impl->onShutdown(); } -void GattServer::onUpdatesEnabled(EventCallback_t callback) +void GattServer::onUpdatesEnabled(UpdatesEnabledCallback_t callback) { - return impl->onUpdatesEnabled(callback); + impl->onUpdatesEnabled(callback); } -void GattServer::onUpdatesDisabled(EventCallback_t callback) +void GattServer::onUpdatesDisabled(UpdatesDisabledCallback_t callback) { - return impl->onUpdatesDisabled(callback); + impl->onUpdatesDisabled(callback); } -void GattServer::onConfirmationReceived(EventCallback_t callback) +void GattServer::onConfirmationReceived(ConfirmationReceivedCallback_t callback) { - return impl->onConfirmationReceived(callback); + impl->onConfirmationReceived(callback); } -} // ble \ No newline at end of file +} // ble + diff --git a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp index a7ca14d5674..b53f52ca8d2 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp @@ -925,7 +925,7 @@ void GattServer::cccd_cb(attsCccEvt_t *evt) GattServerEvents::GATT_EVENT_UPDATES_ENABLED : GattServerEvents::GATT_EVENT_UPDATES_DISABLED; - getInstance().handleEvent(evt_type, evt->handle); + getInstance().handleEvent(evt_type, evt->hdr.param, evt->handle); } void GattServer::att_cb(const attEvt_t *evt) @@ -936,7 +936,7 @@ void GattServer::att_cb(const attEvt_t *evt) handler->onAttMtuChange(evt->hdr.param, evt->mtu); } } else if (evt->hdr.status == ATT_SUCCESS && evt->hdr.event == ATTS_HANDLE_VALUE_CNF) { - getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT, evt->handle); + getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT, evt->hdr.param, evt->handle); } } @@ -1454,17 +1454,17 @@ GattServer::GattServerShutdownCallbackChain_t &GattServer::onShutdown() return shutdownCallChain; } -void GattServer::onUpdatesEnabled(EventCallback_t callback) +void GattServer::onUpdatesEnabled(UpdatesEnabledCallback_t callback) { updatesEnabledCallback = callback; } -void GattServer::onUpdatesDisabled(EventCallback_t callback) +void GattServer::onUpdatesDisabled(UpdatesDisabledCallback_t callback) { updatesDisabledCallback = callback; } -void GattServer::onConfirmationReceived(EventCallback_t callback) +void GattServer::onConfirmationReceived(ConfirmationReceivedCallback_t callback) { confirmationReceivedCallback = callback; } @@ -1491,29 +1491,33 @@ void GattServer::handleDataReadEvent(const GattReadCallbackParams *params) void GattServer::handleEvent( GattServerEvents::gattEvent_e type, + ble::connection_handle_t connHandle, GattAttribute::Handle_t attributeHandle ) { switch (type) { case GattServerEvents::GATT_EVENT_UPDATES_ENABLED: if (updatesEnabledCallback) { - updatesEnabledCallback(attributeHandle); + updatesEnabledCallback(GattUpdatesEnabledCallbackParams( + { .connHandle = connHandle, .handle = attributeHandle })); } break; case GattServerEvents::GATT_EVENT_UPDATES_DISABLED: if (updatesDisabledCallback) { - updatesDisabledCallback(attributeHandle); + updatesDisabledCallback(GattUpdatesDisabledCallbackParams( + { .connHandle = connHandle, .handle = attributeHandle })); } break; case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED: if (confirmationReceivedCallback) { - confirmationReceivedCallback(attributeHandle); + confirmationReceivedCallback(GattConfirmationReceivedCallbackParams( + { .connHandle = connHandle, .handle = attributeHandle })); } break; case GattServerEvents::GATT_EVENT_DATA_SENT: // Called every time a notification or indication has been sent - handleDataSentEvent(1); + handleDataSentEvent(connHandle, attributeHandle); break; default: @@ -1521,9 +1525,9 @@ void GattServer::handleEvent( } } -void GattServer::handleDataSentEvent(unsigned count) +void GattServer::handleDataSentEvent(ble::connection_handle_t connHandle, GattAttribute::Handle_t attHandle) { - dataSentCallChain.call(count); + dataSentCallChain.call(GattDataSentCallbackParams({.connHandle = connHandle, .handle = attHandle})); } } // namespace impl diff --git a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h index 32d44005e8f..945ade095c9 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h +++ b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h @@ -64,7 +64,9 @@ class GattServer : public PalSigningMonitor { using DataReadCallbackChain_t = ble::GattServer::DataReadCallbackChain_t; using GattServerShutdownCallback_t = ble::GattServer::GattServerShutdownCallback_t; using GattServerShutdownCallbackChain_t = ble::GattServer::GattServerShutdownCallbackChain_t; - using EventCallback_t = ble::GattServer::EventCallback_t; + using UpdatesEnabledCallback_t = ble::GattServer::UpdatesEnabledCallback_t; + using UpdatesDisabledCallback_t = ble::GattServer::UpdatesDisabledCallback_t; + using ConfirmationReceivedCallback_t = ble::GattServer::ConfirmationReceivedCallback_t; // inherited typedefs have the wrong types so we have to redefine them public: @@ -138,11 +140,11 @@ class GattServer : public PalSigningMonitor { GattServerShutdownCallbackChain_t &onShutdown(); - void onUpdatesEnabled(EventCallback_t callback); + void onUpdatesEnabled(UpdatesEnabledCallback_t callback); - void onUpdatesDisabled(EventCallback_t callback); + void onUpdatesDisabled(UpdatesDisabledCallback_t callback); - void onConfirmationReceived(EventCallback_t callback); + void onConfirmationReceived(ConfirmationReceivedCallback_t callback); /* Entry points for the underlying stack to report events back to the user. */ protected: @@ -153,10 +155,11 @@ class GattServer : public PalSigningMonitor { void handleEvent( GattServerEvents::gattEvent_e type, + ble::connection_handle_t connHandle, GattAttribute::Handle_t attributeHandle ); - void handleDataSentEvent(unsigned count); + void handleDataSentEvent(ble::connection_handle_t connHandle, GattAttribute::Handle_t attHandle); /* ===================================================================== */ /* private implementation follows */ @@ -334,17 +337,17 @@ class GattServer : public PalSigningMonitor { /** * The registered callback handler for updates enabled events. */ - EventCallback_t updatesEnabledCallback; + UpdatesEnabledCallback_t updatesEnabledCallback; /** * The registered callback handler for updates disabled events. */ - EventCallback_t updatesDisabledCallback; + UpdatesDisabledCallback_t updatesDisabledCallback; /** * The registered callback handler for confirmation received events. */ - EventCallback_t confirmationReceivedCallback; + ConfirmationReceivedCallback_t confirmationReceivedCallback; PalSigningMonitorEventHandler *_signing_event_handler; From ff9b40b77273eb0b3d4a1babacbd3337a3a180ab Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 7 Oct 2020 16:43:35 -0400 Subject: [PATCH 2/6] Revert changes to CallChains --- .../FEATURE_BLE/include/ble/GattServer.h | 44 ++++++------------- .../FEATURE_BLE/source/GattServer.cpp | 7 ++- .../source/cordio/source/GattServerImpl.cpp | 21 ++++----- .../source/cordio/source/GattServerImpl.h | 18 ++++---- 4 files changed, 33 insertions(+), 57 deletions(-) diff --git a/connectivity/FEATURE_BLE/include/ble/GattServer.h b/connectivity/FEATURE_BLE/include/ble/GattServer.h index db4fa1e9a98..6b1261f1f51 100644 --- a/connectivity/FEATURE_BLE/include/ble/GattServer.h +++ b/connectivity/FEATURE_BLE/include/ble/GattServer.h @@ -131,42 +131,16 @@ class GattServer { * * @see onDataSent(). */ - typedef FunctionPointerWithContext - DataSentCallback_t; + typedef FunctionPointerWithContext DataSentCallback_t; /** * Callchain of DataSentCallback_t objects. * * @see onDataSent(). */ - typedef CallChainOfFunctionPointersWithContext + typedef CallChainOfFunctionPointersWithContext DataSentCallbackChain_t; - /** - * Event handler invoked when the client has subscribed to characteristic updates - * - * @see onUpdatesEnabled(). - */ - typedef FunctionPointerWithContext - UpdatesEnabledCallback_t; - - /** - * Event handler invoked when the client has unsubscribed from characteristic updates - * - * @see onUpdatesDisabled(). - */ - typedef FunctionPointerWithContext - UpdatesDisabledCallback_t; - - /** - * Event handler invoked when the an ACK has been received for an - * indication sent to the client. - * - * @see onConfirmationReceived(). - */ - typedef FunctionPointerWithContext - ConfirmationReceivedCallback_t; - /** * Event handler invoked when the client has written an attribute of the * server. @@ -216,6 +190,14 @@ class GattServer { typedef CallChainOfFunctionPointersWithContext GattServerShutdownCallbackChain_t; + /** + * Event handler that handles subscription to characteristic updates, + * unsubscription from characteristic updates and notification confirmation. + * + * @see onUpdatesEnabled() onUpdateDisabled() onConfirmationReceived() + */ + typedef FunctionPointerWithContext EventCallback_t; + public: /** * Assign the event handler implementation that will be used by the @@ -586,7 +568,7 @@ class GattServer { * * @param[in] callback Event handler being registered. */ - void onUpdatesEnabled(UpdatesEnabledCallback_t callback); + void onUpdatesEnabled(EventCallback_t callback); /** * Set up an event handler that monitors unsubscription from characteristic @@ -594,7 +576,7 @@ class GattServer { * * @param[in] callback Event handler being registered. */ - void onUpdatesDisabled(UpdatesDisabledCallback_t callback); + void onUpdatesDisabled(EventCallback_t callback); /** * Set up an event handler that monitors notification acknowledgment. @@ -604,7 +586,7 @@ class GattServer { * * @param[in] callback Event handler being registered. */ - void onConfirmationReceived(ConfirmationReceivedCallback_t callback); + void onConfirmationReceived(EventCallback_t callback); #if !defined(DOXYGEN_ONLY) GattServer(impl::GattServer* impl) : impl(impl) {} diff --git a/connectivity/FEATURE_BLE/source/GattServer.cpp b/connectivity/FEATURE_BLE/source/GattServer.cpp index d8761d31599..7e169ea6848 100644 --- a/connectivity/FEATURE_BLE/source/GattServer.cpp +++ b/connectivity/FEATURE_BLE/source/GattServer.cpp @@ -137,20 +137,19 @@ GattServer::GattServerShutdownCallbackChain_t& GattServer::onShutdown() return impl->onShutdown(); } -void GattServer::onUpdatesEnabled(UpdatesEnabledCallback_t callback) +void GattServer::onUpdatesEnabled(EventCallback_t callback) { impl->onUpdatesEnabled(callback); } -void GattServer::onUpdatesDisabled(UpdatesDisabledCallback_t callback) +void GattServer::onUpdatesDisabled(EventCallback_t callback) { impl->onUpdatesDisabled(callback); } -void GattServer::onConfirmationReceived(ConfirmationReceivedCallback_t callback) +void GattServer::onConfirmationReceived(EventCallback_t callback) { impl->onConfirmationReceived(callback); } } // ble - diff --git a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp index b53f52ca8d2..0967018286c 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp @@ -1454,17 +1454,17 @@ GattServer::GattServerShutdownCallbackChain_t &GattServer::onShutdown() return shutdownCallChain; } -void GattServer::onUpdatesEnabled(UpdatesEnabledCallback_t callback) +void GattServer::onUpdatesEnabled(EventCallback_t callback) { updatesEnabledCallback = callback; } -void GattServer::onUpdatesDisabled(UpdatesDisabledCallback_t callback) +void GattServer::onUpdatesDisabled(EventCallback_t callback) { updatesDisabledCallback = callback; } -void GattServer::onConfirmationReceived(ConfirmationReceivedCallback_t callback) +void GattServer::onConfirmationReceived(EventCallback_t callback) { confirmationReceivedCallback = callback; } @@ -1498,26 +1498,23 @@ void GattServer::handleEvent( switch (type) { case GattServerEvents::GATT_EVENT_UPDATES_ENABLED: if (updatesEnabledCallback) { - updatesEnabledCallback(GattUpdatesEnabledCallbackParams( - { .connHandle = connHandle, .handle = attributeHandle })); + updatesEnabledCallback(attributeHandle); } break; case GattServerEvents::GATT_EVENT_UPDATES_DISABLED: if (updatesDisabledCallback) { - updatesDisabledCallback(GattUpdatesDisabledCallbackParams( - { .connHandle = connHandle, .handle = attributeHandle })); + updatesDisabledCallback(attributeHandle); } break; case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED: if (confirmationReceivedCallback) { - confirmationReceivedCallback(GattConfirmationReceivedCallbackParams( - { .connHandle = connHandle, .handle = attributeHandle })); + confirmationReceivedCallback(attributeHandle); } break; case GattServerEvents::GATT_EVENT_DATA_SENT: // Called every time a notification or indication has been sent - handleDataSentEvent(connHandle, attributeHandle); + handleDataSentEvent(1); break; default: @@ -1525,9 +1522,9 @@ void GattServer::handleEvent( } } -void GattServer::handleDataSentEvent(ble::connection_handle_t connHandle, GattAttribute::Handle_t attHandle) +void GattServer::handleDataSentEvent(unsigned count) { - dataSentCallChain.call(GattDataSentCallbackParams({.connHandle = connHandle, .handle = attHandle})); + dataSentCallChain.call(count); } } // namespace impl diff --git a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h index 945ade095c9..0d60cb6f039 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h +++ b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h @@ -64,9 +64,7 @@ class GattServer : public PalSigningMonitor { using DataReadCallbackChain_t = ble::GattServer::DataReadCallbackChain_t; using GattServerShutdownCallback_t = ble::GattServer::GattServerShutdownCallback_t; using GattServerShutdownCallbackChain_t = ble::GattServer::GattServerShutdownCallbackChain_t; - using UpdatesEnabledCallback_t = ble::GattServer::UpdatesEnabledCallback_t; - using UpdatesDisabledCallback_t = ble::GattServer::UpdatesDisabledCallback_t; - using ConfirmationReceivedCallback_t = ble::GattServer::ConfirmationReceivedCallback_t; + using EventCallback_t = ble::GattServer::EventCallback_t; // inherited typedefs have the wrong types so we have to redefine them public: @@ -140,11 +138,11 @@ class GattServer : public PalSigningMonitor { GattServerShutdownCallbackChain_t &onShutdown(); - void onUpdatesEnabled(UpdatesEnabledCallback_t callback); + void onUpdatesEnabled(EventCallback_t callback); - void onUpdatesDisabled(UpdatesDisabledCallback_t callback); + void onUpdatesDisabled(EventCallback_t callback); - void onConfirmationReceived(ConfirmationReceivedCallback_t callback); + void onConfirmationReceived(EventCallback_t callback); /* Entry points for the underlying stack to report events back to the user. */ protected: @@ -159,7 +157,7 @@ class GattServer : public PalSigningMonitor { GattAttribute::Handle_t attributeHandle ); - void handleDataSentEvent(ble::connection_handle_t connHandle, GattAttribute::Handle_t attHandle); + void handleDataSentEvent(unsigned count); /* ===================================================================== */ /* private implementation follows */ @@ -337,17 +335,17 @@ class GattServer : public PalSigningMonitor { /** * The registered callback handler for updates enabled events. */ - UpdatesEnabledCallback_t updatesEnabledCallback; + EventCallback_t updatesEnabledCallback; /** * The registered callback handler for updates disabled events. */ - UpdatesDisabledCallback_t updatesDisabledCallback; + EventCallback_t updatesDisabledCallback; /** * The registered callback handler for confirmation received events. */ - ConfirmationReceivedCallback_t confirmationReceivedCallback; + EventCallback_t confirmationReceivedCallback; PalSigningMonitorEventHandler *_signing_event_handler; From fe906c1b7974ce86f1e7bdbacdc2b35c0dcc15dd Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 7 Oct 2020 17:00:34 -0400 Subject: [PATCH 3/6] Deprecate individual callback-registering functions in GattServer --- .../FEATURE_BLE/include/ble/GattServer.h | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/connectivity/FEATURE_BLE/include/ble/GattServer.h b/connectivity/FEATURE_BLE/include/ble/GattServer.h index 6b1261f1f51..b58205ad15a 100644 --- a/connectivity/FEATURE_BLE/include/ble/GattServer.h +++ b/connectivity/FEATURE_BLE/include/ble/GattServer.h @@ -20,6 +20,8 @@ #ifndef MBED_GATT_SERVER_H__ #define MBED_GATT_SERVER_H__ +#include "platform/mbed_toolchain.h" + #include "ble/common/CallChainOfFunctionPointersWithContext.h" #include "ble/common/blecommon.h" @@ -401,6 +403,8 @@ class GattServer { * @note It is possible to chain together multiple onDataSent callbacks * (potentially from different modules of an application). */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") void onDataSent(const DataSentCallback_t &callback); /** @@ -413,6 +417,8 @@ class GattServer { * function. */ template + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) { onDataSent({objPtr, memberPtr}); @@ -423,6 +429,8 @@ class GattServer { * * @return A reference to the DATA_SENT event callback chain. */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") DataSentCallbackChain_t &onDataSent(); /** @@ -434,6 +442,8 @@ class GattServer { * @attention It is possible to set multiple event handlers. Registered * handlers may be removed with onDataWritten().detach(callback). */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") void onDataWritten(const DataWrittenCallback_t &callback); /** @@ -446,6 +456,8 @@ class GattServer { * function. */ template + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") void onDataWritten( T *objPtr, void (T::*memberPtr)(const GattWriteCallbackParams *context) @@ -465,6 +477,8 @@ class GattServer { * @note It is possible to unregister callbacks using * onDataWritten().detach(callback). */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") DataWrittenCallbackChain_t &onDataWritten(); /** @@ -485,6 +499,8 @@ class GattServer { * @attention It is possible to set multiple event handlers. Registered * handlers may be removed with onDataRead().detach(callback). */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") ble_error_t onDataRead(const DataReadCallback_t &callback); /** @@ -496,6 +512,8 @@ class GattServer { * function. */ template + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") ble_error_t onDataRead( T *objPtr, void (T::*memberPtr)(const GattReadCallbackParams *context) @@ -515,6 +533,8 @@ class GattServer { * @note It is possible to unregister callbacks using * onDataRead().detach(callback). */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") DataReadCallbackChain_t &onDataRead(); /** @@ -530,6 +550,8 @@ class GattServer { * @note It is possible to unregister a callback using * onShutdown().detach(callback) */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") void onShutdown(const GattServerShutdownCallback_t &callback); /** @@ -544,6 +566,8 @@ class GattServer { * function. */ template + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") void onShutdown(T *objPtr, void (T::*memberPtr)(const GattServer *)) { onShutdown({objPtr, memberPtr}); @@ -560,6 +584,8 @@ class GattServer { * @note It is possible to unregister callbacks using * onShutdown().detach(callback). */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") GattServerShutdownCallbackChain_t& onShutdown(); /** @@ -568,6 +594,8 @@ class GattServer { * * @param[in] callback Event handler being registered. */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") void onUpdatesEnabled(EventCallback_t callback); /** @@ -576,6 +604,8 @@ class GattServer { * * @param[in] callback Event handler being registered. */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") void onUpdatesDisabled(EventCallback_t callback); /** @@ -586,6 +616,8 @@ class GattServer { * * @param[in] callback Event handler being registered. */ + MBED_DEPRECATED_SINCE("mbed-os-6.3.0", "Individual callback-registering functions have" + "been replaced by GattServer::setEventHandler. Use that function instead.") void onConfirmationReceived(EventCallback_t callback); #if !defined(DOXYGEN_ONLY) From e73db5a716426fa331236de57dc7fd67a624869b Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 7 Oct 2020 17:12:34 -0400 Subject: [PATCH 4/6] Introduce new GattServer::EventHandler callback functions for those previously deprecated --- .../FEATURE_BLE/include/ble/GattServer.h | 43 +++++++++++++++++++ .../include/ble/gatt/GattCallbackParamTypes.h | 2 +- .../source/cordio/source/GattServerImpl.cpp | 40 +++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/connectivity/FEATURE_BLE/include/ble/GattServer.h b/connectivity/FEATURE_BLE/include/ble/GattServer.h index b58205ad15a..b6d551cc221 100644 --- a/connectivity/FEATURE_BLE/include/ble/GattServer.h +++ b/connectivity/FEATURE_BLE/include/ble/GattServer.h @@ -120,6 +120,49 @@ class GattServer { (void)connectionHandle; (void)attMtuSize; } + + /** + * Function invoked when the server has sent data to a client as + * part of a notification/indication. + * + * @note params has a temporary scope and should be copied by the + * application if needed later + */ + virtual void onDataSent(const GattDataSentCallbackParams* params) { + (void)params; + } + + /** + * Function invoked when the client has subscribed to characteristic updates + * + * @note params has a temporary scope and should be copied by the + * application if needed later + */ + virtual void onUpdatesEnabled(const GattUpdatesEnabledCallbackParams* params) { + (void)params; + } + + /** + * Function invoked when the client has unsubscribed to characteristic updates + * + * @note params has a temporary scope and should be copied by the + * application if needed later + */ + virtual void onUpdatesDisabled(const GattUpdatesDisabledCallbackParams* params) { + (void)params; + } + + /** + * Function invoked when an ACK has been received for an + * indication sent to the client. + * + * @note params has a temporary scope and should be copied by the + * application if needed later + */ + virtual void onConfirmationReceived(const GattConfirmationReceivedCallbackParams* params) { + (void)params; + } + protected: /** * Prevent polymorphic deletion and avoid unnecessary virtual destructor diff --git a/connectivity/FEATURE_BLE/include/ble/gatt/GattCallbackParamTypes.h b/connectivity/FEATURE_BLE/include/ble/gatt/GattCallbackParamTypes.h index 670c59d57fb..ee7740d15af 100644 --- a/connectivity/FEATURE_BLE/include/ble/gatt/GattCallbackParamTypes.h +++ b/connectivity/FEATURE_BLE/include/ble/gatt/GattCallbackParamTypes.h @@ -399,7 +399,7 @@ struct GattDataSentCallbackParams { /** * Attribute Handle to which the event applies */ - GattAttribute::Handle_t handle; + GattAttribute::Handle_t attHandle; }; diff --git a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp index 0967018286c..127de7c98fd 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp @@ -1497,22 +1497,62 @@ void GattServer::handleEvent( { switch (type) { case GattServerEvents::GATT_EVENT_UPDATES_ENABLED: + + if(eventHandler) { + GattUpdatesEnabledCallbackParams params({ + .connHandle = connHandle, + .attHandle = attributeHandle + }); + eventHandler->onUpdatesEnabled(¶ms); + } + + // Execute deprecated callback if (updatesEnabledCallback) { updatesEnabledCallback(attributeHandle); } break; case GattServerEvents::GATT_EVENT_UPDATES_DISABLED: + + if(eventHandler) { + GattUpdatesDisabledCallbackParams params({ + .connHandle = connHandle, + .attHandle = attributeHandle + }); + eventHandler->onUpdatesDisabled(¶ms); + } + + // Execute deprecated callback if (updatesDisabledCallback) { updatesDisabledCallback(attributeHandle); } break; case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED: + + if(eventHandler) { + GattConfirmationReceivedCallbackParams params({ + .connHandle = connHandle, + .attHandle = attributeHandle + }); + eventHandler->onConfirmationReceived(¶ms); + } + + // Execute deprecated callback if (confirmationReceivedCallback) { confirmationReceivedCallback(attributeHandle); } break; case GattServerEvents::GATT_EVENT_DATA_SENT: + + if(eventHandler) { + GattDataSentCallbackParams params({ + .connHandle = connHandle, + .attHandle = attributeHandle + }); + eventHandler->onDataSent(¶ms); + } + + // Execute deprecated callback // Called every time a notification or indication has been sent handleDataSentEvent(1); break; From 452a815ece83e0cfb86a744624b3de7b4e7cec9b Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 7 Oct 2020 18:25:30 -0400 Subject: [PATCH 5/6] Add remaining deprecated onXXX handlers to GattServer::EventHandler --- .../FEATURE_BLE/include/ble/GattServer.h | 35 +++++++++++++++++++ .../source/cordio/source/GattServerImpl.cpp | 15 ++++++++ 2 files changed, 50 insertions(+) diff --git a/connectivity/FEATURE_BLE/include/ble/GattServer.h b/connectivity/FEATURE_BLE/include/ble/GattServer.h index b6d551cc221..4a44a056248 100644 --- a/connectivity/FEATURE_BLE/include/ble/GattServer.h +++ b/connectivity/FEATURE_BLE/include/ble/GattServer.h @@ -132,6 +132,41 @@ class GattServer { (void)params; } + /** + * Function invoked when a client writes an attribute + * + * @note params has a temporary scope and should be copied by the + * application if needed later + */ + virtual void onDataWritten(const GattWriteCallbackParams *params) { + (void)params; + } + + /** + * Function invoked when a client reads an attribute + * + * @note This functionality may not be available on all underlying stacks. + * Application code may work around that limitation by monitoring read + * requests instead of read events. + * + * @note params has a temporary scope and should be copied by the + * application if needed later + * + * @see GattCharacteristic::setReadAuthorizationCallback() + * @see isOnDataReadAvailable(). + */ + virtual void onDataRead(const GattReadCallbackParams *params) { + (void)params; + } + + /** + * Function invoked when the GattServer instance is about + * to be shut down. This can result in a call to reset() or BLE::reset(). + */ + virtual void onShutdown(const GattServer *server) { + (void)server; + } + /** * Function invoked when the client has subscribed to characteristic updates * diff --git a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp index 127de7c98fd..9952c39ccb4 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp @@ -878,6 +878,11 @@ GapAdvertisingData::Appearance GattServer::getAppearance() ble_error_t GattServer::reset(ble::GattServer* server) { /* Notify that the instance is about to shutdown */ + if(eventHandler) { + eventHandler->onShutdown(server); + } + + // Execute callbacks added with deprecated API shutdownCallChain.call(server); shutdownCallChain.clear(); @@ -1481,11 +1486,21 @@ GattServer::EventHandler *GattServer::getEventHandler() void GattServer::handleDataWrittenEvent(const GattWriteCallbackParams *params) { + if(eventHandler) { + eventHandler->onDataWritten(params); + } + + // Execute callbacks added with deprecated API dataWrittenCallChain.call(params); } void GattServer::handleDataReadEvent(const GattReadCallbackParams *params) { + if(eventHandler) { + eventHandler->onDataRead(params); + } + + // Execute callbacks added with deprecated API dataReadCallChain.call(params); } From 862669c1cc27b80a0f4eec6f39c5851aa148911b Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Fri, 9 Oct 2020 11:20:54 -0400 Subject: [PATCH 6/6] Change const pointers to const references to be consistent with Gap EventHandler --- connectivity/FEATURE_BLE/include/ble/GattServer.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/connectivity/FEATURE_BLE/include/ble/GattServer.h b/connectivity/FEATURE_BLE/include/ble/GattServer.h index 4a44a056248..e3f2b7792ca 100644 --- a/connectivity/FEATURE_BLE/include/ble/GattServer.h +++ b/connectivity/FEATURE_BLE/include/ble/GattServer.h @@ -128,7 +128,7 @@ class GattServer { * @note params has a temporary scope and should be copied by the * application if needed later */ - virtual void onDataSent(const GattDataSentCallbackParams* params) { + virtual void onDataSent(const GattDataSentCallbackParams ¶ms) { (void)params; } @@ -138,7 +138,7 @@ class GattServer { * @note params has a temporary scope and should be copied by the * application if needed later */ - virtual void onDataWritten(const GattWriteCallbackParams *params) { + virtual void onDataWritten(const GattWriteCallbackParams ¶ms) { (void)params; } @@ -155,7 +155,7 @@ class GattServer { * @see GattCharacteristic::setReadAuthorizationCallback() * @see isOnDataReadAvailable(). */ - virtual void onDataRead(const GattReadCallbackParams *params) { + virtual void onDataRead(const GattReadCallbackParams ¶ms) { (void)params; } @@ -163,7 +163,7 @@ class GattServer { * Function invoked when the GattServer instance is about * to be shut down. This can result in a call to reset() or BLE::reset(). */ - virtual void onShutdown(const GattServer *server) { + virtual void onShutdown(const GattServer &server) { (void)server; } @@ -173,7 +173,7 @@ class GattServer { * @note params has a temporary scope and should be copied by the * application if needed later */ - virtual void onUpdatesEnabled(const GattUpdatesEnabledCallbackParams* params) { + virtual void onUpdatesEnabled(const GattUpdatesEnabledCallbackParams ¶ms) { (void)params; } @@ -183,7 +183,7 @@ class GattServer { * @note params has a temporary scope and should be copied by the * application if needed later */ - virtual void onUpdatesDisabled(const GattUpdatesDisabledCallbackParams* params) { + virtual void onUpdatesDisabled(const GattUpdatesDisabledCallbackParams ¶ms) { (void)params; } @@ -194,7 +194,7 @@ class GattServer { * @note params has a temporary scope and should be copied by the * application if needed later */ - virtual void onConfirmationReceived(const GattConfirmationReceivedCallbackParams* params) { + virtual void onConfirmationReceived(const GattConfirmationReceivedCallbackParams ¶ms) { (void)params; }