Skip to content

[BLE] Change GattServer callbacks to CallChains #13728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 94 additions & 13 deletions connectivity/FEATURE_BLE/include/ble/GattServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,66 @@ class GattServer {
*
* @see onDataSent().
*/
typedef FunctionPointerWithContext<unsigned> DataSentCallback_t;
typedef FunctionPointerWithContext<GattDataSentCallbackParams>
DataSentCallback_t;

/**
* Callchain of DataSentCallback_t objects.
*
* @see onDataSent().
*/
typedef CallChainOfFunctionPointersWithContext<unsigned>
typedef CallChainOfFunctionPointersWithContext<GattDataSentCallbackParams>
DataSentCallbackChain_t;

/**
* Event handler invoked when the client has subscribed to characteristic updates
*
* @see onUpdatesEnabled().
*/
typedef FunctionPointerWithContext<GattUpdatesEnabledCallbackParams>
UpdatesEnabledCallback_t;

/**
* Callchain of UpdatesEnabledCallback_t objects.
*
* @see onUpdatesEnabled().
*/
typedef CallChainOfFunctionPointersWithContext<GattUpdatesEnabledCallbackParams>
UpdatesEnabledCallbackChain_t;

/**
* Event handler invoked when the client has unsubscribed from characteristic updates
*
* @see onUpdatesDisabled().
*/
typedef FunctionPointerWithContext<GattUpdatesDisabledCallbackParams>
UpdatesDisabledCallback_t;

/**
* Callchain of UpdatesDisabledCallback_t objects.
*
* @see onUpdatesDisabled().
*/
typedef CallChainOfFunctionPointersWithContext<GattUpdatesDisabledCallbackParams>
UpdatesDisabledCallbackChain_t;

/**
* Event handler invoked when the an ACK has been received for an
* indication sent to the client.
*
* @see onConfirmationReceived().
*/
typedef FunctionPointerWithContext<GattConfirmationReceivedCallbackParams>
ConfirmationReceivedCallback_t;

/**
* Callchain of ConfirmationReceivedCallback_t objects.
*
* @see onConfirmationReceived().
*/
typedef CallChainOfFunctionPointersWithContext<GattUpdatesDisabledCallbackParams>
ConfirmationReceivedCallbackChain_t;

/**
* Event handler invoked when the client has written an attribute of the
* server.
Expand Down Expand Up @@ -190,14 +240,6 @@ class GattServer {
typedef CallChainOfFunctionPointersWithContext<const GattServer*>
GattServerShutdownCallbackChain_t;

/**
* Event handler that handles subscription to characteristic updates,
* unsubscription from characteristic updates and notification confirmation.
*
* @see onUpdatesEnabled() onUpdateDisabled() onConfirmationReceived()
*/
typedef FunctionPointerWithContext<GattAttribute::Handle_t> EventCallback_t;

public:
/**
* Assign the event handler implementation that will be used by the
Expand Down Expand Up @@ -568,15 +610,41 @@ class GattServer {
*
* @param[in] callback Event handler being registered.
*/
void onUpdatesEnabled(EventCallback_t callback);
void onUpdatesEnabled(UpdatesEnabledCallback_t callback);

/**
* Access the callchain of updates enabled event handlers.
*
* @return A reference to the updates enabled event callbacks chain.
*
* @note It is possible to register callbacks using
* onUpdatesEnabled().add(callback).
*
* @note It is possible to unregister callbacks using
* onUpdatesEnabled().detach(callback).
*/
UpdatesEnabledCallbackChain_t& onUpdatesEnabled();

/**
* Set up an event handler that monitors unsubscription from characteristic
* updates.
*
* @param[in] callback Event handler being registered.
*/
void onUpdatesDisabled(EventCallback_t callback);
void onUpdatesDisabled(UpdatesDisabledCallback_t callback);

/**
* Access the callchain of updates disabled event handlers.
*
* @return A reference to the updates disabled event callbacks chain.
*
* @note It is possible to register callbacks using
* onUpdatesDisabled().add(callback).
*
* @note It is possible to unregister callbacks using
* onUpdatesDisabled().detach(callback).
*/
UpdatesEnabledCallbackChain_t& onUpdatesDisabled();

/**
* Set up an event handler that monitors notification acknowledgment.
Expand All @@ -586,7 +654,20 @@ class GattServer {
*
* @param[in] callback Event handler being registered.
*/
void onConfirmationReceived(EventCallback_t callback);
void onConfirmationReceived(ConfirmationReceivedCallback_t callback);

/**
* Access the callchain of confirmation received event handlers.
*
* @return A reference to the confirmation received event callbacks chain.
*
* @note It is possible to register callbacks using
* onConfirmationReceived().add(callback).
*
* @note It is possible to unregister callbacks using
* onConfirmationReceived().detach(callback).
*/
UpdatesEnabledCallbackChain_t& onConfirmationReceived();

#if !defined(DOXYGEN_ONLY)
GattServer(impl::GattServer* impl) : impl(impl) {}
Expand Down
23 changes: 23 additions & 0 deletions connectivity/FEATURE_BLE/include/ble/gatt/GattCallbackParamTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
15 changes: 8 additions & 7 deletions connectivity/FEATURE_BLE/source/GattServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
} // ble

51 changes: 25 additions & 26 deletions connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,9 +887,9 @@ ble_error_t GattServer::reset(ble::GattServer* server)
dataSentCallChain.clear();
dataWrittenCallChain.clear();
dataReadCallChain.clear();
updatesEnabledCallback = nullptr;
updatesDisabledCallback = nullptr;
confirmationReceivedCallback = nullptr;
updatesEnabledCallChain.clear();
updatesDisabledCallChain.clear();
confirmationReceivedCallChain.clear();

while (registered_service) {
internal_service_t *s = registered_service;
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -1388,9 +1388,9 @@ GattServer::GattServer() :
dataSentCallChain(),
dataWrittenCallChain(),
dataReadCallChain(),
updatesEnabledCallback(nullptr),
updatesDisabledCallback(nullptr),
confirmationReceivedCallback(nullptr),
updatesEnabledCallChain(),
updatesDisabledCallChain(),
confirmationReceivedCallChain(),
_signing_event_handler(nullptr),
cccds(),
cccd_values(),
Expand Down Expand Up @@ -1454,19 +1454,20 @@ GattServer::GattServerShutdownCallbackChain_t &GattServer::onShutdown()
return shutdownCallChain;
}

void GattServer::onUpdatesEnabled(EventCallback_t callback)
void GattServer::onUpdatesEnabled(UpdatesEnabledCallback_t callback)
{
updatesEnabledCallback = callback;

updatesEnabledCallChain.add(callback);
}

void GattServer::onUpdatesDisabled(EventCallback_t callback)
void GattServer::onUpdatesDisabled(UpdatesDisabledCallback_t callback)
{
updatesDisabledCallback = callback;
updatesDisabledCallChain.add(callback);
}

void GattServer::onConfirmationReceived(EventCallback_t callback)
void GattServer::onConfirmationReceived(ConfirmationReceivedCallback_t callback)
{
confirmationReceivedCallback = callback;
confirmationReceivedCallChain.add(callback);
}

void GattServer::setEventHandler(EventHandler *handler)
Expand All @@ -1491,39 +1492,37 @@ 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);
}
updatesEnabledCallChain.call(GattUpdatesEnabledCallbackParams(
{ .connHandle = connHandle, .handle = attributeHandle }));
break;
case GattServerEvents::GATT_EVENT_UPDATES_DISABLED:
if (updatesDisabledCallback) {
updatesDisabledCallback(attributeHandle);
}
updatesDisabledCallChain.call(GattUpdatesDisabledCallbackParams(
{ .connHandle = connHandle, .handle = attributeHandle }));
break;
case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED:
if (confirmationReceivedCallback) {
confirmationReceivedCallback(attributeHandle);
}
confirmationReceivedCallChain.call(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:
break;
}
}

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
Expand Down
Loading