diff --git a/features/FEATURE_BLE/ble/GattServer.h b/features/FEATURE_BLE/ble/GattServer.h index dfa9fa3c3e..90c2925867 100644 --- a/features/FEATURE_BLE/ble/GattServer.h +++ b/features/FEATURE_BLE/ble/GattServer.h @@ -17,12 +17,18 @@ #ifndef MBED_GATT_SERVER_H__ #define MBED_GATT_SERVER_H__ -#include "ble/Gap.h" -#include "GattService.h" -#include "GattAttribute.h" -#include "GattServerEvents.h" -#include "GattCallbackParamTypes.h" -#include "CallChainOfFunctionPointersWithContext.h" +#include "ble/common/StaticInterface.h" +#include "ble/GattService.h" +#include "ble/GattAttribute.h" +#include "ble/GattServerEvents.h" +#include "ble/GattCallbackParamTypes.h" +#include "ble/CallChainOfFunctionPointersWithContext.h" +#include "BleImplementationForward.h" + +#if !defined(DOXYGEN_ONLY) +namespace ble { +namespace interface { +#endif /** * @addtogroup ble @@ -85,7 +91,15 @@ * Characteristic Value Notification and Characteristic Value Indication when * the nature of the server initiated is not relevant. */ +#if !defined(DOXYGEN_ONLY) +template +class GattServer : public StaticInterface { +#else class GattServer { +#endif + + using StaticInterface::impl; + public: /** @@ -195,17 +209,7 @@ protected: /** * Construct a GattServer instance. */ - GattServer() : - eventHandler(NULL), - serviceCount(0), - characteristicCount(0), - dataSentCallChain(), - dataWrittenCallChain(), - dataReadCallChain(), - updatesEnabledCallback(NULL), - updatesDisabledCallback(NULL), - confirmationReceivedCallback(NULL) { - } + GattServer(); /* * 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. */ - virtual 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; - } + ble_error_t addService(GattService &service); /** * 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. * - * @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) * 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, uint8_t buffer[], 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. @@ -297,22 +284,12 @@ public: * * @return BLE_ERROR_NONE if a value was read successfully into the buffer. */ - virtual ble_error_t read( - Gap::Handle_t connectionHandle, + ble_error_t read( + ble::connection_handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t *buffer, 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. @@ -329,22 +306,12 @@ public: * @return BLE_ERROR_NONE if the attribute value has been successfully * updated. */ - virtual ble_error_t write( + ble_error_t write( GattAttribute::Handle_t attributeHandle, const uint8_t *value, uint16_t size, 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. @@ -367,24 +334,13 @@ public: * @return BLE_ERROR_NONE if the attribute value has been successfully * updated. */ - virtual ble_error_t write( - Gap::Handle_t connectionHandle, + ble_error_t write( + ble::connection_handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t *value, uint16_t size, 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 @@ -397,18 +353,10 @@ public: * @return BLE_ERROR_NONE if the connection and handle are found. False * otherwise. */ - virtual ble_error_t areUpdatesEnabled( + ble_error_t areUpdatesEnabled( const GattCharacteristic &characteristic, 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 @@ -423,20 +371,11 @@ public: * @return BLE_ERROR_NONE if the connection and handle are found. False * otherwise. */ - virtual ble_error_t areUpdatesEnabled( - Gap::Handle_t connectionHandle, + ble_error_t areUpdatesEnabled( + ble::connection_handle_t connectionHandle, const GattCharacteristic &characteristic, 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 @@ -447,12 +386,7 @@ public: * * @return true if onDataRead is supported; false otherwise. */ - virtual bool isOnDataReadAvailable() const - { - /* Requesting action from porters: override this API if this capability - is supported. */ - return false; - } + bool isOnDataReadAvailable() const; /* * APIs with nonvirtual implementations. @@ -791,24 +725,56 @@ public: * * @return BLE_ERROR_NONE on success. */ - virtual ble_error_t reset(void) - { - /* Notify that the instance is about to shutdown */ - shutdownCallChain.call(this); - shutdownCallChain.clear(); + ble_error_t reset(void); - serviceCount = 0; - characteristicCount = 0; +protected: + /* --- Abstract calls to override --- */ - dataSentCallChain.clear(); - dataWrittenCallChain.clear(); - dataReadCallChain.clear(); - updatesEnabledCallback = NULL; - updatesDisabledCallback = NULL; - confirmationReceivedCallback = NULL; + /* Derived implementation must call the base reset_ */ + ble_error_t reset_(void); - 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: /** @@ -878,4 +844,13 @@ private: * @} */ +#if !defined(DOXYGEN_ONLY) +} // interface +} // ble + +typedef ble::impl::GattServer GattServer; + +#endif + + #endif /* ifndef MBED_GATT_SERVER_H__ */ diff --git a/features/FEATURE_BLE/source/GattServer.tpp b/features/FEATURE_BLE/source/GattServer.tpp new file mode 100644 index 0000000000..3a17c2b3f7 --- /dev/null +++ b/features/FEATURE_BLE/source/GattServer.tpp @@ -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 +GattServer::GattServer() : + serviceCount(0), + characteristicCount(0), + eventHandler(NULL), + dataSentCallChain(), + dataWrittenCallChain(), + dataReadCallChain(), + updatesEnabledCallback(NULL), + updatesDisabledCallback(NULL), + confirmationReceivedCallback(NULL) { +} + +template +ble_error_t GattServer::addService(GattService &service) +{ + return impl()->addService_(service); +} + +template +ble_error_t GattServer::read( + GattAttribute::Handle_t attributeHandle, + uint8_t buffer[], + uint16_t *lengthP +) { + return impl()->read_(attributeHandle, buffer, lengthP); +} + +template +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); +} + +template +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); +} + +template +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 + ); +} + +template +ble_error_t GattServer::areUpdatesEnabled( + const GattCharacteristic &characteristic, + bool *enabledP +) { + return impl()->areUpdatesEnabled_(characteristic, enabledP); +} + +template +ble_error_t GattServer::areUpdatesEnabled( + ble::connection_handle_t connectionHandle, + const GattCharacteristic &characteristic, + bool *enabledP +) { + return impl()->areUpdatesEnabled_(connectionHandle, characteristic, enabledP); +} + +template +bool GattServer::isOnDataReadAvailable() const +{ + return impl()->isOnDataReadAvailable_(); +} + +template +ble_error_t GattServer::reset(void) +{ + return impl()->reset_(); +} + +template +ble_error_t GattServer::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 +ble_error_t GattServer::addService_(GattService &service) +{ + return BLE_ERROR_NOT_IMPLEMENTED; +} + +template +ble_error_t GattServer::read_( + GattAttribute::Handle_t attributeHandle, + uint8_t buffer[], + uint16_t *lengthP +) { + return BLE_ERROR_NOT_IMPLEMENTED; +} + +template +ble_error_t GattServer::read_( + ble::connection_handle_t connectionHandle, + GattAttribute::Handle_t attributeHandle, + uint8_t *buffer, + uint16_t *lengthP +) { + return BLE_ERROR_NOT_IMPLEMENTED; +} + +template +ble_error_t GattServer::write_( + GattAttribute::Handle_t attributeHandle, + const uint8_t *value, + uint16_t size, + bool localOnly +) { + return BLE_ERROR_NOT_IMPLEMENTED; +} + +template +ble_error_t GattServer::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 +ble_error_t GattServer::areUpdatesEnabled_( + const GattCharacteristic &characteristic, + bool *enabledP +) { + return BLE_ERROR_NOT_IMPLEMENTED; +} + +template +ble_error_t GattServer::areUpdatesEnabled_( + ble::connection_handle_t connectionHandle, + const GattCharacteristic &characteristic, + bool *enabledP +) { + return BLE_ERROR_NOT_IMPLEMENTED; +} + +template +bool GattServer::isOnDataReadAvailable_() const +{ + return BLE_ERROR_NOT_IMPLEMENTED; +} + +} // interface +} // ble