BLE - Devirtualize ::GattServer.

The interface is defined in ::ble::interface::GattServer and an implementation must export the implementation type ::ble::impl::GattServer.
pull/9727/head
Vincent Coubard 2019-02-25 19:10:16 +00:00
parent e56fd682a1
commit 6d2592a137
2 changed files with 304 additions and 120 deletions

View File

@ -17,12 +17,18 @@
#ifndef MBED_GATT_SERVER_H__ #ifndef MBED_GATT_SERVER_H__
#define MBED_GATT_SERVER_H__ #define MBED_GATT_SERVER_H__
#include "ble/Gap.h" #include "ble/common/StaticInterface.h"
#include "GattService.h" #include "ble/GattService.h"
#include "GattAttribute.h" #include "ble/GattAttribute.h"
#include "GattServerEvents.h" #include "ble/GattServerEvents.h"
#include "GattCallbackParamTypes.h" #include "ble/GattCallbackParamTypes.h"
#include "CallChainOfFunctionPointersWithContext.h" #include "ble/CallChainOfFunctionPointersWithContext.h"
#include "BleImplementationForward.h"
#if !defined(DOXYGEN_ONLY)
namespace ble {
namespace interface {
#endif
/** /**
* @addtogroup ble * @addtogroup ble
@ -85,7 +91,15 @@
* Characteristic Value Notification and Characteristic Value Indication when * Characteristic Value Notification and Characteristic Value Indication when
* the nature of the server initiated is not relevant. * the nature of the server initiated is not relevant.
*/ */
#if !defined(DOXYGEN_ONLY)
template <class Impl>
class GattServer : public StaticInterface<Impl, GattServer> {
#else
class GattServer { class GattServer {
#endif
using StaticInterface<Impl, ::ble::interface::GattServer>::impl;
public: public:
/** /**
@ -195,17 +209,7 @@ protected:
/** /**
* Construct a GattServer instance. * Construct a GattServer instance.
*/ */
GattServer() : GattServer();
eventHandler(NULL),
serviceCount(0),
characteristicCount(0),
dataSentCallChain(),
dataWrittenCallChain(),
dataReadCallChain(),
updatesEnabledCallback(NULL),
updatesDisabledCallback(NULL),
confirmationReceivedCallback(NULL) {
}
/* /*
* The following functions are meant to be overridden in the platform * The following functions are meant to be overridden in the platform
@ -237,15 +241,7 @@ public:
* *
* @return BLE_ERROR_NONE if the service was successfully added. * @return BLE_ERROR_NONE if the service was successfully added.
*/ */
virtual ble_error_t addService(GattService &service) ble_error_t addService(GattService &service);
{
/* Avoid compiler warnings about unused variables. */
(void)service;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
/** /**
* Read the value of an attribute present in the local GATT server. * Read the value of an attribute present in the local GATT server.
@ -260,24 +256,15 @@ public:
* *
* @return BLE_ERROR_NONE if a value was read successfully into the buffer. * @return BLE_ERROR_NONE if a value was read successfully into the buffer.
* *
* @attention read(Gap::Handle_t, GattAttribute::Handle_t, uint8_t *, uint16_t *) * @attention read(ble::connection_handle_t, GattAttribute::Handle_t, uint8_t *, uint16_t *)
* must be used to read Client Characteristic Configuration Descriptor (CCCD) * must be used to read Client Characteristic Configuration Descriptor (CCCD)
* because the value of this type of attribute depends on the connection. * because the value of this type of attribute depends on the connection.
*/ */
virtual ble_error_t read( ble_error_t read(
GattAttribute::Handle_t attributeHandle, GattAttribute::Handle_t attributeHandle,
uint8_t buffer[], uint8_t buffer[],
uint16_t *lengthP uint16_t *lengthP
) { );
/* Avoid compiler warnings about unused variables. */
(void)attributeHandle;
(void)buffer;
(void)lengthP;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
/** /**
* Read the value of an attribute present in the local GATT server. * Read the value of an attribute present in the local GATT server.
@ -297,22 +284,12 @@ public:
* *
* @return BLE_ERROR_NONE if a value was read successfully into the buffer. * @return BLE_ERROR_NONE if a value was read successfully into the buffer.
*/ */
virtual ble_error_t read( ble_error_t read(
Gap::Handle_t connectionHandle, ble::connection_handle_t connectionHandle,
GattAttribute::Handle_t attributeHandle, GattAttribute::Handle_t attributeHandle,
uint8_t *buffer, uint8_t *buffer,
uint16_t *lengthP uint16_t *lengthP
) { );
/* Avoid compiler warnings about unused variables. */
(void)connectionHandle;
(void)attributeHandle;
(void)buffer;
(void)lengthP;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
/** /**
* Update the value of an attribute present in the local GATT server. * Update the value of an attribute present in the local GATT server.
@ -329,22 +306,12 @@ public:
* @return BLE_ERROR_NONE if the attribute value has been successfully * @return BLE_ERROR_NONE if the attribute value has been successfully
* updated. * updated.
*/ */
virtual ble_error_t write( ble_error_t write(
GattAttribute::Handle_t attributeHandle, GattAttribute::Handle_t attributeHandle,
const uint8_t *value, const uint8_t *value,
uint16_t size, uint16_t size,
bool localOnly = false bool localOnly = false
) { );
/* Avoid compiler warnings about unused variables. */
(void)attributeHandle;
(void)value;
(void)size;
(void)localOnly;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
/** /**
* Update the value of an attribute present in the local GATT server. * Update the value of an attribute present in the local GATT server.
@ -367,24 +334,13 @@ public:
* @return BLE_ERROR_NONE if the attribute value has been successfully * @return BLE_ERROR_NONE if the attribute value has been successfully
* updated. * updated.
*/ */
virtual ble_error_t write( ble_error_t write(
Gap::Handle_t connectionHandle, ble::connection_handle_t connectionHandle,
GattAttribute::Handle_t attributeHandle, GattAttribute::Handle_t attributeHandle,
const uint8_t *value, const uint8_t *value,
uint16_t size, uint16_t size,
bool localOnly = false bool localOnly = false
) { );
/* Avoid compiler warnings about unused variables. */
(void)connectionHandle;
(void)attributeHandle;
(void)value;
(void)size;
(void)localOnly;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
/** /**
* Determine if one of the connected clients has subscribed to notifications * Determine if one of the connected clients has subscribed to notifications
@ -397,18 +353,10 @@ public:
* @return BLE_ERROR_NONE if the connection and handle are found. False * @return BLE_ERROR_NONE if the connection and handle are found. False
* otherwise. * otherwise.
*/ */
virtual ble_error_t areUpdatesEnabled( ble_error_t areUpdatesEnabled(
const GattCharacteristic &characteristic, const GattCharacteristic &characteristic,
bool *enabledP bool *enabledP
) { );
/* Avoid compiler warnings about unused variables. */
(void)characteristic;
(void)enabledP;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
/** /**
* Determine if an identified client has subscribed to notifications or * Determine if an identified client has subscribed to notifications or
@ -423,20 +371,11 @@ public:
* @return BLE_ERROR_NONE if the connection and handle are found. False * @return BLE_ERROR_NONE if the connection and handle are found. False
* otherwise. * otherwise.
*/ */
virtual ble_error_t areUpdatesEnabled( ble_error_t areUpdatesEnabled(
Gap::Handle_t connectionHandle, ble::connection_handle_t connectionHandle,
const GattCharacteristic &characteristic, const GattCharacteristic &characteristic,
bool *enabledP bool *enabledP
) { );
/* Avoid compiler warnings about unused variables. */
(void)connectionHandle;
(void)characteristic;
(void)enabledP;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
/** /**
* Indicate if the underlying stack emit events when an attribute is read by * Indicate if the underlying stack emit events when an attribute is read by
@ -447,12 +386,7 @@ public:
* *
* @return true if onDataRead is supported; false otherwise. * @return true if onDataRead is supported; false otherwise.
*/ */
virtual bool isOnDataReadAvailable() const bool isOnDataReadAvailable() const;
{
/* Requesting action from porters: override this API if this capability
is supported. */
return false;
}
/* /*
* APIs with nonvirtual implementations. * APIs with nonvirtual implementations.
@ -791,24 +725,56 @@ public:
* *
* @return BLE_ERROR_NONE on success. * @return BLE_ERROR_NONE on success.
*/ */
virtual ble_error_t reset(void) ble_error_t reset(void);
{
/* Notify that the instance is about to shutdown */
shutdownCallChain.call(this);
shutdownCallChain.clear();
serviceCount = 0; protected:
characteristicCount = 0; /* --- Abstract calls to override --- */
dataSentCallChain.clear(); /* Derived implementation must call the base reset_ */
dataWrittenCallChain.clear(); ble_error_t reset_(void);
dataReadCallChain.clear();
updatesEnabledCallback = NULL;
updatesDisabledCallback = NULL;
confirmationReceivedCallback = NULL;
return BLE_ERROR_NONE; ble_error_t addService_(GattService &service);
}
ble_error_t read_(
GattAttribute::Handle_t attributeHandle,
uint8_t buffer[],
uint16_t *lengthP
);
ble_error_t read_(
ble::connection_handle_t connectionHandle,
GattAttribute::Handle_t attributeHandle,
uint8_t *buffer,
uint16_t *lengthP
);
ble_error_t write_(
GattAttribute::Handle_t attributeHandle,
const uint8_t *value,
uint16_t size,
bool localOnly
);
ble_error_t write_(
ble::connection_handle_t connectionHandle,
GattAttribute::Handle_t attributeHandle,
const uint8_t *value,
uint16_t size,
bool localOnly
);
ble_error_t areUpdatesEnabled_(
const GattCharacteristic &characteristic,
bool *enabledP
);
ble_error_t areUpdatesEnabled_(
ble::connection_handle_t connectionHandle,
const GattCharacteristic &characteristic,
bool *enabledP
);
bool isOnDataReadAvailable_() const;
protected: protected:
/** /**
@ -878,4 +844,13 @@ private:
* @} * @}
*/ */
#if !defined(DOXYGEN_ONLY)
} // interface
} // ble
typedef ble::impl::GattServer GattServer;
#endif
#endif /* ifndef MBED_GATT_SERVER_H__ */ #endif /* ifndef MBED_GATT_SERVER_H__ */

View File

@ -0,0 +1,209 @@
/* mbed Microcontroller Library
* Copyright (c) 2019 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"
namespace ble {
namespace interface {
template<class Impl>
GattServer<Impl>::GattServer() :
serviceCount(0),
characteristicCount(0),
eventHandler(NULL),
dataSentCallChain(),
dataWrittenCallChain(),
dataReadCallChain(),
updatesEnabledCallback(NULL),
updatesDisabledCallback(NULL),
confirmationReceivedCallback(NULL) {
}
template<class Impl>
ble_error_t GattServer<Impl>::addService(GattService &service)
{
return impl()->addService_(service);
}
template<class Impl>
ble_error_t GattServer<Impl>::read(
GattAttribute::Handle_t attributeHandle,
uint8_t buffer[],
uint16_t *lengthP
) {
return impl()->read_(attributeHandle, buffer, lengthP);
}
template<class Impl>
ble_error_t GattServer<Impl>::read(
ble::connection_handle_t connectionHandle,
GattAttribute::Handle_t attributeHandle,
uint8_t *buffer,
uint16_t *lengthP
) {
return impl()->read_(connectionHandle, attributeHandle, buffer, lengthP);
}
template<class Impl>
ble_error_t GattServer<Impl>::write(
GattAttribute::Handle_t attributeHandle,
const uint8_t *value,
uint16_t size,
bool localOnly
) {
return impl()->write_(attributeHandle, value, size, localOnly);
}
template<class Impl>
ble_error_t GattServer<Impl>::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
);
}
template<class Impl>
ble_error_t GattServer<Impl>::areUpdatesEnabled(
const GattCharacteristic &characteristic,
bool *enabledP
) {
return impl()->areUpdatesEnabled_(characteristic, enabledP);
}
template<class Impl>
ble_error_t GattServer<Impl>::areUpdatesEnabled(
ble::connection_handle_t connectionHandle,
const GattCharacteristic &characteristic,
bool *enabledP
) {
return impl()->areUpdatesEnabled_(connectionHandle, characteristic, enabledP);
}
template<class Impl>
bool GattServer<Impl>::isOnDataReadAvailable() const
{
return impl()->isOnDataReadAvailable_();
}
template<class Impl>
ble_error_t GattServer<Impl>::reset(void)
{
return impl()->reset_();
}
template<class Impl>
ble_error_t GattServer<Impl>::reset_(void)
{
/* Notify that the instance is about to shutdown */
shutdownCallChain.call(this);
shutdownCallChain.clear();
serviceCount = 0;
characteristicCount = 0;
dataSentCallChain.clear();
dataWrittenCallChain.clear();
dataReadCallChain.clear();
updatesEnabledCallback = NULL;
updatesDisabledCallback = NULL;
confirmationReceivedCallback = NULL;
return BLE_ERROR_NONE;
}
/* -------------------------- Dummy implementation -------------------------- */
template<class Impl>
ble_error_t GattServer<Impl>::addService_(GattService &service)
{
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
ble_error_t GattServer<Impl>::read_(
GattAttribute::Handle_t attributeHandle,
uint8_t buffer[],
uint16_t *lengthP
) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
ble_error_t GattServer<Impl>::read_(
ble::connection_handle_t connectionHandle,
GattAttribute::Handle_t attributeHandle,
uint8_t *buffer,
uint16_t *lengthP
) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
ble_error_t GattServer<Impl>::write_(
GattAttribute::Handle_t attributeHandle,
const uint8_t *value,
uint16_t size,
bool localOnly
) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
ble_error_t GattServer<Impl>::write_(
ble::connection_handle_t connectionHandle,
GattAttribute::Handle_t attributeHandle,
const uint8_t *value,
uint16_t size,
bool localOnly
) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
ble_error_t GattServer<Impl>::areUpdatesEnabled_(
const GattCharacteristic &characteristic,
bool *enabledP
) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
ble_error_t GattServer<Impl>::areUpdatesEnabled_(
ble::connection_handle_t connectionHandle,
const GattCharacteristic &characteristic,
bool *enabledP
) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
bool GattServer<Impl>::isOnDataReadAvailable_() const
{
return BLE_ERROR_NOT_IMPLEMENTED;
}
} // interface
} // ble