BLE: GattServer PIMPL

pull/13475/head
Vincent Coubard 2020-08-21 12:15:13 +01:00
parent 2b48d4826e
commit cf3ede0191
4 changed files with 342 additions and 246 deletions

View File

@ -95,6 +95,10 @@ namespace ble {
* the nature of the server initiated is not relevant.
*/
#if !defined(DOXYGEN_ONLY)
namespace impl {
class GattServer;
}
namespace interface {
#endif // !defined(DOXYGEN_ONLY)
class GattServer {
@ -415,7 +419,10 @@ public:
* function.
*/
template <typename T>
void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count));
void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count))
{
onDataSent({objPtr, memberPtr});
}
/**
* Access the callchain of data sent event handlers.
@ -448,7 +455,10 @@ public:
void onDataWritten(
T *objPtr,
void (T::*memberPtr)(const GattWriteCallbackParams *context)
);
)
{
onDataWritten({objPtr, memberPtr});
}
/**
* Access the callchain of data written event handlers.
@ -495,7 +505,10 @@ public:
ble_error_t onDataRead(
T *objPtr,
void (T::*memberPtr)(const GattReadCallbackParams *context)
);
)
{
onDataRead({objPtr, memberPtr});
}
/**
* Access the callchain of data read event handlers.
@ -537,7 +550,10 @@ public:
* function.
*/
template <typename T>
void onShutdown(T *objPtr, void (T::*memberPtr)(const GattServer *));
void onShutdown(T *objPtr, void (T::*memberPtr)(const GattServer *))
{
onShutdown({objPtr, memberPtr});
}
/**
* Access the callchain of shutdown event handlers.
@ -578,72 +594,8 @@ public:
*/
void onConfirmationReceived(EventCallback_t callback);
/* Entry points for the underlying stack to report events back to the user. */
protected:
/**
* Helper function that notifies all registered handlers of an occurrence
* of a data written event.
*
* @attention Vendor implementation must invoke this function after one of
* the GattServer attributes has been written.
*
* @param[in] params The data written parameters passed to the registered
* handlers.
*/
void handleDataWrittenEvent(const GattWriteCallbackParams *params);
/**
* Helper function that notifies all registered handlers of an occurrence
* of a data read event.
*
* @attention Vendor implementation must invoke this function after one of
* the GattServer attributes has been read.
*
* @param[in] params The data read parameters passed to the registered
* handlers.
*/
void handleDataReadEvent(const GattReadCallbackParams *params);
/**
* Helper function that notifies the registered handler of an occurrence
* of updates enabled, updates disabled or confirmation received events.
*
* @attention Vendor implementation must invoke this function when a client
* subscribes to characteristic updates, unsubscribes from characteristic
* updates or a notification confirmation has been received.
*
* @param[in] type The type of event that occurred.
* @param[in] attributeHandle The handle of the attribute concerned by the
* event.
*/
void handleEvent(
GattServerEvents::gattEvent_e type,
GattAttribute::Handle_t attributeHandle
);
/**
* Helper function that notifies all registered handlers of an occurrence
* of a data sent event.
*
* @attention Vendor implementation must invoke this function after the
* emission of a notification or an indication.
*
* @param[in] count Number of packets sent.
*/
void handleDataSentEvent(unsigned count);
/**
* Get preferred connection paramters.
*
*/
Gap::PreferredConnectionParams_t getPreferredConnectionParams();
/**
* Set preferred connection parameters.
*
* @param[in] params Preferred connection parameter values to set.
*/
void setPreferredConnectionParams(const Gap::PreferredConnectionParams_t& params);
private:
impl::GattServer *impl;
};
/**
@ -655,13 +607,16 @@ protected:
#if !defined(DOXYGEN_ONLY)
} // namespace interface
#endif // !defined(DOXYGEN_ONLY)
using ble::interface::GattServer;
} // ble
/* This includes the concrete class implementation, to provide a an alternative API implementation
* disable ble-api-implementation and place your header in a path with the same structure */
#include "ble/internal/GattServerImpl.h"
//#include "ble/internal/GattServerImpl.h"
/** @deprecated Use the namespaced ble::GattServer instead of the global GattServer. */
using ble::GattServer;
using ble::interface::GattServer;
#endif /* ifndef MBED_GATT_SERVER_H__ */

View File

@ -27,6 +27,7 @@
#include "ble/common/ble/GattCallbackParamTypes.h"
#include <stddef.h>
#include <ble/GattServer.h>
#include "ble/common/ble/blecommon.h"
#include "ble/Gap.h"
#include "wsf_types.h"
@ -42,29 +43,31 @@ namespace ble {
class PalAttClient;
class BLE;
class GattServer :
public ble::interface::GattServer,
public PalSigningMonitor
{
namespace impl {
class GattServer : public PalSigningMonitor {
friend ble::BLE;
friend ble::PalAttClient;
friend PalSigningMonitor;
friend BLEInstanceBase;
friend PalGenericAccessService;
using EventHandler = ble::GattServer::EventHandler;
using DataSentCallback_t = ble::GattServer::DataSentCallback_t ;
using DataSentCallbackChain_t = ble::GattServer::DataSentCallbackChain_t ;
using DataWrittenCallback_t = ble::GattServer::DataWrittenCallback_t ;
using DataWrittenCallbackChain_t = ble::GattServer::DataWrittenCallbackChain_t ;
using DataReadCallback_t = ble::GattServer::DataReadCallback_t;
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;
// inherited typedefs have the wrong types so we have to redefine them
public:
typedef FunctionPointerWithContext<const GattServer *>
GattServerShutdownCallback_t;
typedef CallChainOfFunctionPointersWithContext<const GattServer*>
GattServerShutdownCallbackChain_t;
public:
void setEventHandler(EventHandler *handler);
ble_error_t reset(void);
ble_error_t reset(ble::GattServer* server);
ble_error_t addService(GattService &service);
@ -107,48 +110,29 @@ public:
bool *enabledP
);
Gap::PreferredConnectionParams_t getPreferredConnectionParams();
ble::Gap::PreferredConnectionParams_t getPreferredConnectionParams();
void setPreferredConnectionParams(const Gap::PreferredConnectionParams_t& params);
void setPreferredConnectionParams(const ble::Gap::PreferredConnectionParams_t &params);
bool isOnDataReadAvailable() const;
void onDataSent(const DataSentCallback_t &callback);
template <typename T>
void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count));
DataSentCallbackChain_t &onDataSent();
void onDataWritten(const DataWrittenCallback_t &callback) {
void onDataWritten(const DataWrittenCallback_t &callback)
{
dataWrittenCallChain.add(callback);
}
template <typename T>
void onDataWritten(
T *objPtr,
void (T::*memberPtr)(const GattWriteCallbackParams *context)
) {
dataWrittenCallChain.add(objPtr, memberPtr);
};
DataWrittenCallbackChain_t &onDataWritten();
ble_error_t onDataRead(const DataReadCallback_t &callback);
template <typename T>
ble_error_t onDataRead(
T *objPtr,
void (T::*memberPtr)(const GattReadCallbackParams *context)
);
DataReadCallbackChain_t &onDataRead();
void onShutdown(const GattServerShutdownCallback_t &callback);
template <typename T>
void onShutdown(T *objPtr, void (T::*memberPtr)(const GattServer *));
GattServerShutdownCallbackChain_t &onShutdown();
void onUpdatesEnabled(EventCallback_t callback);
@ -199,9 +183,11 @@ public:
#endif // Disabled until reworked and reintroduced to GattServer API
private:
GattServer();
GattServer(const GattServer &);
const GattServer &operator=(const GattServer &);
/**
@ -260,16 +246,35 @@ private:
);
static void cccd_cb(attsCccEvt_t *pEvt);
static void att_cb(const attEvt_t *pEvt);
static uint8_t atts_read_cb(dmConnId_t connId, uint16_t handle, uint8_t operation, uint16_t offset, attsAttr_t *pAttr);
static uint8_t atts_write_cb(dmConnId_t connId, uint16_t handle, uint8_t operation, uint16_t offset, uint16_t len, uint8_t *pValue, attsAttr_t *pAttr);
static uint8_t atts_write_cb(
dmConnId_t connId,
uint16_t handle,
uint8_t operation,
uint16_t offset,
uint16_t len,
uint8_t *pValue,
attsAttr_t *pAttr
);
static uint8_t atts_auth_cb(dmConnId_t connId, uint8_t permit, uint16_t handle);
void add_generic_access_service();
void add_generic_attribute_service();
void *alloc_block(size_t block_size);
GattCharacteristic *get_auth_char(uint16_t value_handle);
bool get_cccd_index_by_cccd_handle(GattAttribute::Handle_t cccd_handle, uint8_t &idx) const;
bool get_cccd_index_by_value_handle(GattAttribute::Handle_t char_handle, uint8_t &idx) const;
bool is_update_authorized(connection_handle_t connection, GattAttribute::Handle_t value_handle);
struct alloc_block_t {
@ -357,7 +362,8 @@ private:
uint8_t ppcp_declaration_value[5];
uint8_t ppcp[8];
uint8_t*& device_name_value() {
uint8_t *&device_name_value()
{
return attributes[2].pValue;
}
} generic_access_service;
@ -376,6 +382,7 @@ private:
bool default_services_added;
};
} // namespace impl
} // ble
#endif /* ifndef MBED_CORDIO_GATT_SERVER_H__ */

View File

@ -18,12 +18,13 @@
#include "BLERoles.h"
#include <algorithm>
#include "ble/GattServer.h"
#include "ble/internal/GattServerImpl.h"
#include "ble/internal/BLEInstanceBase.h"
#include "wsf_types.h"
#include "att_api.h"
namespace ble {
namespace impl {
namespace {
@ -259,7 +260,8 @@ bool GattServer::is_characteristic_valid(GattCharacteristic *characteristic)
#if BLE_FEATURE_SIGNING
// check for invalid permissions
if ((properties == SIGNED_WRITE_PROPERTY) &&
(characteristic->getWriteSecurityRequirement() == att_security_requirement_t::NONE
(
characteristic->getWriteSecurityRequirement() == att_security_requirement_t::NONE
#if BLE_FEATURE_SECURE_CONNECTIONS
|| characteristic->getWriteSecurityRequirement() == att_security_requirement_t::SC_AUTHENTICATED
@ -810,9 +812,9 @@ bool GattServer::isOnDataReadAvailable() const
return true;
}
Gap::PreferredConnectionParams_t GattServer::getPreferredConnectionParams()
ble::Gap::PreferredConnectionParams_t GattServer::getPreferredConnectionParams()
{
Gap::PreferredConnectionParams_t params = { 0 };
ble::Gap::PreferredConnectionParams_t params = {0};
memcpy(&params.minConnectionInterval, generic_access_service.ppcp, 2);
memcpy(&params.maxConnectionInterval, generic_access_service.ppcp + 2, 2);
memcpy(&params.slaveLatency, generic_access_service.ppcp + 4, 2);
@ -820,7 +822,7 @@ Gap::PreferredConnectionParams_t GattServer::getPreferredConnectionParams()
return params;
}
void GattServer::setPreferredConnectionParams(const Gap::PreferredConnectionParams_t& params)
void GattServer::setPreferredConnectionParams(const ble::Gap::PreferredConnectionParams_t &params)
{
memcpy(generic_access_service.ppcp, &params.minConnectionInterval, 2);
memcpy(generic_access_service.ppcp + 2, &params.maxConnectionInterval, 2);
@ -873,10 +875,10 @@ GapAdvertisingData::Appearance GattServer::getAppearance()
#endif // Disabled until reworked and reintroduced to GattServer API
ble_error_t GattServer::reset(void)
ble_error_t GattServer::reset(ble::GattServer* server)
{
/* Notify that the instance is about to shutdown */
shutdownCallChain.call(this);
shutdownCallChain.call(server);
shutdownCallChain.clear();
serviceCount = 0;
@ -1078,7 +1080,7 @@ uint8_t GattServer::atts_auth_cb(dmConnId_t connId, uint8_t permit, uint16_t han
#if BLE_FEATURE_SECURITY
// this CB is triggered when read or write of an attribute (either a value
// handle or a descriptor) requires secure connection security.
SecurityManager& security_manager = BLEInstanceBase::deviceInstance().getSecurityManager();
ble::SecurityManager &security_manager = BLEInstanceBase::deviceInstance().getSecurityManager();
link_encryption_t encryption(link_encryption_t::NOT_ENCRYPTED);
ble_error_t err = security_manager.getLinkEncryption(connId, &encryption);
@ -1347,7 +1349,7 @@ bool GattServer::is_update_authorized(
}
#if BLE_FEATURE_SECURITY
SecurityManager& security_manager = BLEInstanceBase::deviceInstance().getSecurityManager();
ble::SecurityManager &security_manager = BLEInstanceBase::deviceInstance().getSecurityManager();
link_encryption_t encryption(link_encryption_t::NOT_ENCRYPTED);
ble_error_t err = security_manager.getLinkEncryption(connection, &encryption);
if (err) {
@ -1419,12 +1421,6 @@ void GattServer::onDataSent(const DataSentCallback_t &callback)
dataSentCallChain.add(callback);
}
template <typename T>
void GattServer::onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count))
{
dataSentCallChain.add(objPtr, memberPtr);
}
ble::GattServer::DataSentCallbackChain_t &GattServer::onDataSent()
{
return dataSentCallChain;
@ -1445,20 +1441,6 @@ ble_error_t GattServer::onDataRead(const DataReadCallback_t &callback)
return BLE_ERROR_NONE;
}
template <typename T>
ble_error_t GattServer::onDataRead(
T *objPtr,
void (T::*memberPtr)(const GattReadCallbackParams *context)
)
{
if (!isOnDataReadAvailable()) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
dataReadCallChain.add(objPtr, memberPtr);
return BLE_ERROR_NONE;
}
ble::GattServer::DataReadCallbackChain_t &GattServer::onDataRead()
{
return dataReadCallChain;
@ -1469,13 +1451,7 @@ void GattServer::onShutdown(const GattServerShutdownCallback_t &callback)
shutdownCallChain.add(callback);
}
template <typename T>
void GattServer::onShutdown(T *objPtr, void (T::*memberPtr)(const ble::GattServer *))
{
shutdownCallChain.add(objPtr, memberPtr);
}
ble::GattServer::GattServerShutdownCallbackChain_t& GattServer::onShutdown()
GattServer::GattServerShutdownCallbackChain_t &GattServer::onShutdown()
{
return shutdownCallChain;
}
@ -1552,4 +1528,5 @@ void GattServer::handleDataSentEvent(unsigned count)
dataSentCallChain.call(count);
}
} // namespace impl
} // namespace ble

View File

@ -0,0 +1,157 @@
/* mbed Microcontroller Library
* Copyright (c) 2020 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ble/GattServer.h"
#include "ble/internal/GattServerImpl.h"
namespace ble {
namespace interface {
void GattServer::setEventHandler(EventHandler *handler)
{
return impl->setEventHandler(handler);
}
ble_error_t GattServer::reset()
{
return impl->reset(this);
}
ble_error_t GattServer::addService(GattService &service)
{
return impl->addService(service);
}
ble_error_t GattServer::read(
GattAttribute::Handle_t attributeHandle,
uint8_t buffer[],
uint16_t *lengthP
)
{
return impl->read(attributeHandle, buffer, lengthP);
}
ble_error_t GattServer::read(
ble::connection_handle_t connectionHandle,
GattAttribute::Handle_t attributeHandle,
uint8_t *buffer,
uint16_t *lengthP
)
{
return impl->read(connectionHandle, attributeHandle, buffer, lengthP);
}
ble_error_t GattServer::write(
GattAttribute::Handle_t attributeHandle,
const uint8_t *value,
uint16_t size,
bool localOnly
)
{
return impl->write(attributeHandle, value, size, localOnly);
}
ble_error_t GattServer::write(
ble::connection_handle_t connectionHandle,
GattAttribute::Handle_t attributeHandle,
const uint8_t *value,
uint16_t size,
bool localOnly
)
{
return impl->write(connectionHandle, attributeHandle, value, size, localOnly);
}
ble_error_t GattServer::areUpdatesEnabled(
const GattCharacteristic &characteristic,
bool *enabledP
)
{
return impl->areUpdatesEnabled(characteristic, enabledP);
}
ble_error_t GattServer::areUpdatesEnabled(
ble::connection_handle_t connectionHandle,
const GattCharacteristic &characteristic,
bool *enabledP
)
{
return impl->areUpdatesEnabled(connectionHandle, characteristic, enabledP);
}
bool GattServer::isOnDataReadAvailable() const
{
return impl->isOnDataReadAvailable();
}
void GattServer::onDataSent(const DataSentCallback_t &callback)
{
return impl->onDataSent(callback);
}
GattServer::DataSentCallbackChain_t &GattServer::onDataSent()
{
return impl->onDataSent();
}
void GattServer::onDataWritten(const DataWrittenCallback_t &callback)
{
return impl->onDataWritten(callback);
}
GattServer::DataWrittenCallbackChain_t &GattServer::onDataWritten()
{
return impl->onDataWritten();
}
ble_error_t GattServer::onDataRead(const DataReadCallback_t &callback)
{
return impl->onDataRead(callback);
}
GattServer::DataReadCallbackChain_t &GattServer::onDataRead()
{
return impl->onDataRead();
}
void GattServer::onShutdown(const GattServerShutdownCallback_t &callback)
{
return impl->onShutdown(callback);
}
GattServer::GattServerShutdownCallbackChain_t& GattServer::onShutdown()
{
return impl->onShutdown();
}
void GattServer::onUpdatesEnabled(EventCallback_t callback)
{
return impl->onUpdatesEnabled(callback);
}
void GattServer::onUpdatesDisabled(EventCallback_t callback)
{
return impl->onUpdatesDisabled(callback);
}
void GattServer::onConfirmationReceived(EventCallback_t callback)
{
return impl->onConfirmationReceived(callback);
}
} // namespace interface
} // ble