BLE - Devirtualize ::GattClient

Interface definition now lives in ble::interface::GattClient. An implementation must export the implementation type in ::ble::impl::GattClient.
pull/9727/head
Vincent Coubard 2019-02-25 19:08:37 +00:00
parent 43e87fb04d
commit e56fd682a1
2 changed files with 343 additions and 121 deletions

View File

@ -17,14 +17,18 @@
#ifndef MBED_GATT_CLIENT_H__
#define MBED_GATT_CLIENT_H__
#include "ble/Gap.h"
#include "GattAttribute.h"
#include "ServiceDiscovery.h"
#include "CharacteristicDescriptorDiscovery.h"
#include "ble/common/StaticInterface.h"
#include "ble/GattAttribute.h"
#include "ble/ServiceDiscovery.h"
#include "ble/CharacteristicDescriptorDiscovery.h"
#include "ble/GattCallbackParamTypes.h"
#include "ble/CallChainOfFunctionPointersWithContext.h"
#include "BleImplementationForward.h"
#include "GattCallbackParamTypes.h"
#include "CallChainOfFunctionPointersWithContext.h"
#if !defined(DOXYGEN_ONLY)
namespace ble {
namespace interface {
#endif
/**
* @addtogroup ble
@ -81,7 +85,15 @@
* indicate properties are set. The client discovers that descriptor
* if it intends to register to server initiated events.
*/
#if defined(DOXYGEN_ONLY)
class GattClient {
#else
template <class Impl>
class GattClient : public StaticInterface<Impl, GattClient> {
#endif
using StaticInterface<Impl, ::ble::interface::GattClient>::impl;
public:
/**
@ -214,7 +226,7 @@ public:
*/
public:
virtual ~GattClient() { }
~GattClient() { }
/**
* Launch the service and characteristic discovery procedure of a GATT server
@ -266,24 +278,13 @@ public:
* @return BLE_ERROR_NONE if the discovery procedure has been successfully
* started and an appropriate error otherwise.
*/
virtual ble_error_t launchServiceDiscovery(
Gap::Handle_t connectionHandle,
ble_error_t launchServiceDiscovery(
ble::connection_handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t sc = NULL,
ServiceDiscovery::CharacteristicCallback_t cc = NULL,
const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
const UUID &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)
) {
/* Avoid compiler warnings about unused variables. */
(void)connectionHandle;
(void)sc;
(void)cc;
(void)matchingServiceUUID;
(void)matchingCharacteristicUUIDIn;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
);
/**
* Launch the service discovery procedure of a GATT server peer.
@ -314,8 +315,8 @@ public:
* @return BLE_ERROR_NONE if the discovery procedure has been successfully
* started and an appropriate error otherwise.
*/
virtual ble_error_t discoverServices(
Gap::Handle_t connectionHandle,
ble_error_t discoverServices(
ble::connection_handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t callback,
const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)
) {
@ -355,34 +356,19 @@ public:
* @return BLE_ERROR_NONE if the discovery procedure has been successfully
* started and an appropriate error otherwise.
*/
virtual ble_error_t discoverServices(
Gap::Handle_t connectionHandle,
ble_error_t discoverServices(
ble::connection_handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t callback,
GattAttribute::Handle_t startHandle,
GattAttribute::Handle_t endHandle
) {
/* Avoid compiler warnings about unused variables. */
(void)connectionHandle;
(void)callback;
(void)startHandle;
(void)endHandle;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
);
/**
* Check if the service discovery procedure is currently active.
*
* @return true if service discovery procedure is active and false otherwise.
*/
virtual bool isServiceDiscoveryActive(void) const
{
/* Requesting action from porters: override this API if this capability
is supported. */
return false;
}
bool isServiceDiscoveryActive(void) const;
/**
* Terminate all ongoing service discovery procedures.
@ -390,11 +376,7 @@ public:
* It results in an invocation of the service discovery termination handler
* registered with onServiceDiscoveryTermination().
*/
virtual void terminateServiceDiscovery(void)
{
/* Requesting action from porters: override this API if this capability
is supported. */
}
void terminateServiceDiscovery(void);
/**
* Initiate the read procedure of an attribute handle.
@ -428,20 +410,11 @@ public:
* Finally, concat the last response with the value containing all the
* previous responses and forward that value to the event handlers.
*/
virtual ble_error_t read(
Gap::Handle_t connHandle,
ble_error_t read(
ble::connection_handle_t connHandle,
GattAttribute::Handle_t attributeHandle,
uint16_t offset
) const {
/* Avoid compiler warnings about unused variables. */
(void)connHandle;
(void)attributeHandle;
(void)offset;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
) const;
/**
* Initiate a write procedure on an attribute value.
@ -480,24 +453,13 @@ public:
* by sending an execute write request to the peer. The peer response is
* forwarded to the application handlers.
*/
virtual ble_error_t write(
ble_error_t write(
GattClient::WriteOp_t cmd,
Gap::Handle_t connHandle,
ble::connection_handle_t connHandle,
GattAttribute::Handle_t attributeHandle,
size_t length,
const uint8_t *value
) const {
/* Avoid compiler warnings about unused variables. */
(void)cmd;
(void)connHandle;
(void)attributeHandle;
(void)length;
(void)value;
/* Requesting action from porters: override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
) const;
/* Event callback handlers. */
public:
@ -587,14 +549,9 @@ public:
*
* @param[in] callback Event handler being registered.
*/
virtual void onServiceDiscoveryTermination(
void onServiceDiscoveryTermination(
ServiceDiscovery::TerminationCallback_t callback
) {
(void)callback; /* Avoid compiler warnings about ununsed variables. */
/* Requesting action from porters: override this API if this capability
is supported. */
}
);
/**
* Initiate the descriptor discovery procedure for a given characteristic.
@ -618,18 +575,11 @@ public:
* @return BLE_ERROR_NONE if the characteristic descriptor discovery
* procedure has been launched successfully otherwise an appropriate error.
*/
virtual ble_error_t discoverCharacteristicDescriptors(
ble_error_t discoverCharacteristicDescriptors(
const DiscoveredCharacteristic& characteristic,
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
) {
(void) characteristic;
(void) discoveryCallback;
(void) terminationCallback;
/* Requesting action from porter(s): override this API if this
capability is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
);
/**
* Query status of the descriptor discovery procedure for a given
@ -641,14 +591,9 @@ public:
* @return true if a descriptors discovery is active for the characteristic
* in input otherwise false.
*/
virtual bool isCharacteristicDescriptorDiscoveryActive(
bool isCharacteristicDescriptorDiscoveryActive(
const DiscoveredCharacteristic& characteristic
) const {
(void) characteristic;
/* Requesting action from porter(s): override this API if this
capability is supported. */
return false;
}
) const;
/**
* @brief Terminate an ongoing characteristic descriptor discovery procedure.
@ -659,13 +604,9 @@ public:
* @param[in] characteristic The characteristic containing the descriptors
* being discovered.
*/
virtual void terminateCharacteristicDescriptorDiscovery(
void terminateCharacteristicDescriptorDiscovery(
const DiscoveredCharacteristic& characteristic
) {
/* Requesting action from porter(s): override this API if this
capability is supported. */
(void) characteristic;
}
);
/**
* Trigger MTU negotiation. This might result in a Gap event onAttMtuChange
@ -679,14 +620,7 @@ public:
* @return BLE_ERROR_NONE if the procedure has been launched successfully
* otherwise an appropriate error.
*/
virtual ble_error_t negotiateAttMtu(
ble::connection_handle_t connection
) {
/* Requesting action from porter(s): override this API if this
capability is supported. */
(void) connection;
return BLE_ERROR_NOT_IMPLEMENTED;
}
ble_error_t negotiateAttMtu(ble::connection_handle_t connection);
/**
* Register an handler for Handle Value Notification/Indication events.
@ -781,18 +715,67 @@ public:
*
* @return BLE_ERROR_NONE on success.
*/
virtual ble_error_t reset(void)
{
/* Notify that the instance is about to shut down. */
shutdownCallChain.call(this);
shutdownCallChain.clear();
ble_error_t reset(void);
onDataReadCallbackChain.clear();
onDataWriteCallbackChain.clear();
onHVXCallbackChain.clear();
protected:
return BLE_ERROR_NONE;
}
/* --- Abstract calls to override --- */
/* Derived implementation must call the base class implementation */
ble_error_t reset_(void);
ble_error_t discoverServices_(
ble::connection_handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t callback,
GattAttribute::Handle_t startHandle,
GattAttribute::Handle_t endHandle
);
ble_error_t launchServiceDiscovery_(
ble::connection_handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t sc,
ServiceDiscovery::CharacteristicCallback_t cc,
const UUID &matchingServiceUUID,
const UUID &matchingCharacteristicUUIDIn
);
bool isServiceDiscoveryActive_(void) const;
void terminateServiceDiscovery_(void);
ble_error_t negotiateAttMtu_(ble::connection_handle_t connection);
ble_error_t read_(
ble::connection_handle_t connHandle,
GattAttribute::Handle_t attributeHandle,
uint16_t offset
) const;
ble_error_t write_(
GattClient::WriteOp_t cmd,
ble::connection_handle_t connHandle,
GattAttribute::Handle_t attributeHandle,
size_t length,
const uint8_t *value
) const;
void onServiceDiscoveryTermination_(
ServiceDiscovery::TerminationCallback_t callback
);
ble_error_t discoverCharacteristicDescriptors_(
const DiscoveredCharacteristic& characteristic,
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
);
bool isCharacteristicDescriptorDiscoveryActive_(
const DiscoveredCharacteristic& characteristic
) const;
void terminateCharacteristicDescriptorDiscovery_(
const DiscoveredCharacteristic& characteristic
);
protected:
GattClient() : eventHandler(NULL)
@ -888,4 +871,12 @@ private:
* @}
*/
#if !defined(DOXYGEN_ONLY)
} // namespace interface
} // namespace ble
using ble::impl::GattClient;
#endif
#endif /* ifndef MBED_GATT_CLIENT_H__ */

View File

@ -0,0 +1,231 @@
/* 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/GattClient.h"
namespace ble {
namespace interface {
template<class Impl>
ble_error_t GattClient<Impl>::launchServiceDiscovery(
ble::connection_handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t sc,
ServiceDiscovery::CharacteristicCallback_t cc,
const UUID &matchingServiceUUID,
const UUID &matchingCharacteristicUUIDIn
) {
return impl()->launchServiceDiscovery_(
connectionHandle,
sc,
cc,
matchingServiceUUID,
matchingCharacteristicUUIDIn
);
}
template<class Impl>
ble_error_t GattClient<Impl>::discoverServices(
ble::connection_handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t callback,
GattAttribute::Handle_t startHandle,
GattAttribute::Handle_t endHandle
) {
return impl()->discoverServices_(
connectionHandle,
callback,
startHandle,
endHandle
);
}
template<class Impl>
bool GattClient<Impl>::isServiceDiscoveryActive(void) const
{
return impl()->isServiceDiscoveryActive_();
}
template<class Impl>
void GattClient<Impl>::terminateServiceDiscovery(void)
{
return impl()->terminateServiceDiscovery_();
}
template<class Impl>
ble_error_t GattClient<Impl>::read(
ble::connection_handle_t connHandle,
GattAttribute::Handle_t attributeHandle,
uint16_t offset
) const {
return impl()->read_(connHandle, attributeHandle, offset);
}
template<class Impl>
ble_error_t GattClient<Impl>::write(
GattClient::WriteOp_t cmd,
ble::connection_handle_t connHandle,
GattAttribute::Handle_t attributeHandle,
size_t length,
const uint8_t *value
) const {
return impl()->write_(
cmd,
connHandle,
attributeHandle,
length,
value
);
}
template<class Impl>
void GattClient<Impl>::onServiceDiscoveryTermination(
ServiceDiscovery::TerminationCallback_t callback
) {
return impl()->onServiceDiscoveryTermination_(callback);
}
template<class Impl>
ble_error_t GattClient<Impl>::discoverCharacteristicDescriptors(
const DiscoveredCharacteristic& characteristic,
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
) {
return impl()->discoverCharacteristicDescriptors_(
characteristic,
discoveryCallback,
terminationCallback
);
}
template<class Impl>
bool GattClient<Impl>::isCharacteristicDescriptorDiscoveryActive(
const DiscoveredCharacteristic& characteristic
) const {
return impl()->isCharacteristicDescriptorDiscoveryActive_(characteristic);
}
template<class Impl>
void GattClient<Impl>::terminateCharacteristicDescriptorDiscovery(
const DiscoveredCharacteristic& characteristic
) {
return impl()->terminateCharacteristicDescriptorDiscovery_(characteristic);
}
template<class Impl>
ble_error_t GattClient<Impl>::reset(void)
{
return impl()->reset_();
}
/* ------------------------ Default implementations ------------------------- */
template<class Impl>
ble_error_t GattClient<Impl>::reset_(void)
{
/* Notify that the instance is about to shut down. */
shutdownCallChain.call(this);
shutdownCallChain.clear();
onDataReadCallbackChain.clear();
onDataWriteCallbackChain.clear();
onHVXCallbackChain.clear();
return BLE_ERROR_NONE;
}
template<class Impl>
ble_error_t GattClient<Impl>::discoverServices_(
ble::connection_handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t callback,
GattAttribute::Handle_t startHandle,
GattAttribute::Handle_t endHandle
) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
ble_error_t GattClient<Impl>::launchServiceDiscovery_(
ble::connection_handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t sc,
ServiceDiscovery::CharacteristicCallback_t cc,
const UUID &matchingServiceUUID,
const UUID &matchingCharacteristicUUIDIn
) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
bool GattClient<Impl>::isServiceDiscoveryActive_(void) const
{
return false;
}
template<class Impl>
void GattClient<Impl>::terminateServiceDiscovery_(void)
{
}
template<class Impl>
ble_error_t GattClient<Impl>::read_(
ble::connection_handle_t connHandle,
GattAttribute::Handle_t attributeHandle,
uint16_t offset
) const {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
ble_error_t GattClient<Impl>::write_(
GattClient::WriteOp_t cmd,
ble::connection_handle_t connHandle,
GattAttribute::Handle_t attributeHandle,
size_t length,
const uint8_t *value
) const {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
void GattClient<Impl>::onServiceDiscoveryTermination_(
ServiceDiscovery::TerminationCallback_t callback
) {
}
template<class Impl>
ble_error_t GattClient<Impl>::discoverCharacteristicDescriptors_(
const DiscoveredCharacteristic& characteristic,
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
template<class Impl>
bool GattClient<Impl>::isCharacteristicDescriptorDiscoveryActive_(
const DiscoveredCharacteristic& characteristic
) const {
return false;
}
template<class Impl>
void GattClient<Impl>::terminateCharacteristicDescriptorDiscovery_(
const DiscoveredCharacteristic& characteristic
) {
}
} // interface
} // ble