Skip to content

Commit e56fd68

Browse files
committed
BLE - Devirtualize ::GattClient
Interface definition now lives in ble::interface::GattClient. An implementation must export the implementation type in ::ble::impl::GattClient.
1 parent 43e87fb commit e56fd68

File tree

2 files changed

+344
-122
lines changed

2 files changed

+344
-122
lines changed

features/FEATURE_BLE/ble/GattClient.h

Lines changed: 113 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717
#ifndef MBED_GATT_CLIENT_H__
1818
#define MBED_GATT_CLIENT_H__
1919

20-
#include "ble/Gap.h"
21-
#include "GattAttribute.h"
22-
#include "ServiceDiscovery.h"
23-
#include "CharacteristicDescriptorDiscovery.h"
24-
25-
#include "GattCallbackParamTypes.h"
26-
27-
#include "CallChainOfFunctionPointersWithContext.h"
20+
#include "ble/common/StaticInterface.h"
21+
#include "ble/GattAttribute.h"
22+
#include "ble/ServiceDiscovery.h"
23+
#include "ble/CharacteristicDescriptorDiscovery.h"
24+
#include "ble/GattCallbackParamTypes.h"
25+
#include "ble/CallChainOfFunctionPointersWithContext.h"
26+
#include "BleImplementationForward.h"
27+
28+
#if !defined(DOXYGEN_ONLY)
29+
namespace ble {
30+
namespace interface {
31+
#endif
2832

2933
/**
3034
* @addtogroup ble
@@ -81,7 +85,15 @@
8185
* indicate properties are set. The client discovers that descriptor
8286
* if it intends to register to server initiated events.
8387
*/
88+
#if defined(DOXYGEN_ONLY)
8489
class GattClient {
90+
#else
91+
template <class Impl>
92+
class GattClient : public StaticInterface<Impl, GattClient> {
93+
#endif
94+
95+
using StaticInterface<Impl, ::ble::interface::GattClient>::impl;
96+
8597
public:
8698

8799
/**
@@ -214,7 +226,7 @@ class GattClient {
214226
*/
215227
public:
216228

217-
virtual ~GattClient() { }
229+
~GattClient() { }
218230

219231
/**
220232
* Launch the service and characteristic discovery procedure of a GATT server
@@ -266,24 +278,13 @@ class GattClient {
266278
* @return BLE_ERROR_NONE if the discovery procedure has been successfully
267279
* started and an appropriate error otherwise.
268280
*/
269-
virtual ble_error_t launchServiceDiscovery(
270-
Gap::Handle_t connectionHandle,
281+
ble_error_t launchServiceDiscovery(
282+
ble::connection_handle_t connectionHandle,
271283
ServiceDiscovery::ServiceCallback_t sc = NULL,
272284
ServiceDiscovery::CharacteristicCallback_t cc = NULL,
273285
const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
274286
const UUID &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)
275-
) {
276-
/* Avoid compiler warnings about unused variables. */
277-
(void)connectionHandle;
278-
(void)sc;
279-
(void)cc;
280-
(void)matchingServiceUUID;
281-
(void)matchingCharacteristicUUIDIn;
282-
283-
/* Requesting action from porters: override this API if this capability
284-
is supported. */
285-
return BLE_ERROR_NOT_IMPLEMENTED;
286-
}
287+
);
287288

288289
/**
289290
* Launch the service discovery procedure of a GATT server peer.
@@ -314,8 +315,8 @@ class GattClient {
314315
* @return BLE_ERROR_NONE if the discovery procedure has been successfully
315316
* started and an appropriate error otherwise.
316317
*/
317-
virtual ble_error_t discoverServices(
318-
Gap::Handle_t connectionHandle,
318+
ble_error_t discoverServices(
319+
ble::connection_handle_t connectionHandle,
319320
ServiceDiscovery::ServiceCallback_t callback,
320321
const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)
321322
) {
@@ -355,46 +356,27 @@ class GattClient {
355356
* @return BLE_ERROR_NONE if the discovery procedure has been successfully
356357
* started and an appropriate error otherwise.
357358
*/
358-
virtual ble_error_t discoverServices(
359-
Gap::Handle_t connectionHandle,
359+
ble_error_t discoverServices(
360+
ble::connection_handle_t connectionHandle,
360361
ServiceDiscovery::ServiceCallback_t callback,
361362
GattAttribute::Handle_t startHandle,
362363
GattAttribute::Handle_t endHandle
363-
) {
364-
/* Avoid compiler warnings about unused variables. */
365-
(void)connectionHandle;
366-
(void)callback;
367-
(void)startHandle;
368-
(void)endHandle;
369-
370-
/* Requesting action from porters: override this API if this capability
371-
is supported. */
372-
return BLE_ERROR_NOT_IMPLEMENTED;
373-
}
364+
);
374365

375366
/**
376367
* Check if the service discovery procedure is currently active.
377368
*
378369
* @return true if service discovery procedure is active and false otherwise.
379370
*/
380-
virtual bool isServiceDiscoveryActive(void) const
381-
{
382-
/* Requesting action from porters: override this API if this capability
383-
is supported. */
384-
return false;
385-
}
371+
bool isServiceDiscoveryActive(void) const;
386372

387373
/**
388374
* Terminate all ongoing service discovery procedures.
389375
*
390376
* It results in an invocation of the service discovery termination handler
391377
* registered with onServiceDiscoveryTermination().
392378
*/
393-
virtual void terminateServiceDiscovery(void)
394-
{
395-
/* Requesting action from porters: override this API if this capability
396-
is supported. */
397-
}
379+
void terminateServiceDiscovery(void);
398380

399381
/**
400382
* Initiate the read procedure of an attribute handle.
@@ -428,20 +410,11 @@ class GattClient {
428410
* Finally, concat the last response with the value containing all the
429411
* previous responses and forward that value to the event handlers.
430412
*/
431-
virtual ble_error_t read(
432-
Gap::Handle_t connHandle,
413+
ble_error_t read(
414+
ble::connection_handle_t connHandle,
433415
GattAttribute::Handle_t attributeHandle,
434416
uint16_t offset
435-
) const {
436-
/* Avoid compiler warnings about unused variables. */
437-
(void)connHandle;
438-
(void)attributeHandle;
439-
(void)offset;
440-
441-
/* Requesting action from porters: override this API if this capability
442-
is supported. */
443-
return BLE_ERROR_NOT_IMPLEMENTED;
444-
}
417+
) const;
445418

446419
/**
447420
* Initiate a write procedure on an attribute value.
@@ -480,24 +453,13 @@ class GattClient {
480453
* by sending an execute write request to the peer. The peer response is
481454
* forwarded to the application handlers.
482455
*/
483-
virtual ble_error_t write(
456+
ble_error_t write(
484457
GattClient::WriteOp_t cmd,
485-
Gap::Handle_t connHandle,
458+
ble::connection_handle_t connHandle,
486459
GattAttribute::Handle_t attributeHandle,
487460
size_t length,
488461
const uint8_t *value
489-
) const {
490-
/* Avoid compiler warnings about unused variables. */
491-
(void)cmd;
492-
(void)connHandle;
493-
(void)attributeHandle;
494-
(void)length;
495-
(void)value;
496-
497-
/* Requesting action from porters: override this API if this capability
498-
is supported. */
499-
return BLE_ERROR_NOT_IMPLEMENTED;
500-
}
462+
) const;
501463

502464
/* Event callback handlers. */
503465
public:
@@ -587,14 +549,9 @@ class GattClient {
587549
*
588550
* @param[in] callback Event handler being registered.
589551
*/
590-
virtual void onServiceDiscoveryTermination(
552+
void onServiceDiscoveryTermination(
591553
ServiceDiscovery::TerminationCallback_t callback
592-
) {
593-
(void)callback; /* Avoid compiler warnings about ununsed variables. */
594-
595-
/* Requesting action from porters: override this API if this capability
596-
is supported. */
597-
}
554+
);
598555

599556
/**
600557
* Initiate the descriptor discovery procedure for a given characteristic.
@@ -618,18 +575,11 @@ class GattClient {
618575
* @return BLE_ERROR_NONE if the characteristic descriptor discovery
619576
* procedure has been launched successfully otherwise an appropriate error.
620577
*/
621-
virtual ble_error_t discoverCharacteristicDescriptors(
578+
ble_error_t discoverCharacteristicDescriptors(
622579
const DiscoveredCharacteristic& characteristic,
623580
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
624581
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
625-
) {
626-
(void) characteristic;
627-
(void) discoveryCallback;
628-
(void) terminationCallback;
629-
/* Requesting action from porter(s): override this API if this
630-
capability is supported. */
631-
return BLE_ERROR_NOT_IMPLEMENTED;
632-
}
582+
);
633583

634584
/**
635585
* Query status of the descriptor discovery procedure for a given
@@ -641,14 +591,9 @@ class GattClient {
641591
* @return true if a descriptors discovery is active for the characteristic
642592
* in input otherwise false.
643593
*/
644-
virtual bool isCharacteristicDescriptorDiscoveryActive(
594+
bool isCharacteristicDescriptorDiscoveryActive(
645595
const DiscoveredCharacteristic& characteristic
646-
) const {
647-
(void) characteristic;
648-
/* Requesting action from porter(s): override this API if this
649-
capability is supported. */
650-
return false;
651-
}
596+
) const;
652597

653598
/**
654599
* @brief Terminate an ongoing characteristic descriptor discovery procedure.
@@ -659,13 +604,9 @@ class GattClient {
659604
* @param[in] characteristic The characteristic containing the descriptors
660605
* being discovered.
661606
*/
662-
virtual void terminateCharacteristicDescriptorDiscovery(
607+
void terminateCharacteristicDescriptorDiscovery(
663608
const DiscoveredCharacteristic& characteristic
664-
) {
665-
/* Requesting action from porter(s): override this API if this
666-
capability is supported. */
667-
(void) characteristic;
668-
}
609+
);
669610

670611
/**
671612
* Trigger MTU negotiation. This might result in a Gap event onAttMtuChange
@@ -679,14 +620,7 @@ class GattClient {
679620
* @return BLE_ERROR_NONE if the procedure has been launched successfully
680621
* otherwise an appropriate error.
681622
*/
682-
virtual ble_error_t negotiateAttMtu(
683-
ble::connection_handle_t connection
684-
) {
685-
/* Requesting action from porter(s): override this API if this
686-
capability is supported. */
687-
(void) connection;
688-
return BLE_ERROR_NOT_IMPLEMENTED;
689-
}
623+
ble_error_t negotiateAttMtu(ble::connection_handle_t connection);
690624

691625
/**
692626
* Register an handler for Handle Value Notification/Indication events.
@@ -781,18 +715,67 @@ class GattClient {
781715
*
782716
* @return BLE_ERROR_NONE on success.
783717
*/
784-
virtual ble_error_t reset(void)
785-
{
786-
/* Notify that the instance is about to shut down. */
787-
shutdownCallChain.call(this);
788-
shutdownCallChain.clear();
718+
ble_error_t reset(void);
789719

790-
onDataReadCallbackChain.clear();
791-
onDataWriteCallbackChain.clear();
792-
onHVXCallbackChain.clear();
720+
protected:
793721

794-
return BLE_ERROR_NONE;
795-
}
722+
/* --- Abstract calls to override --- */
723+
724+
/* Derived implementation must call the base class implementation */
725+
ble_error_t reset_(void);
726+
727+
ble_error_t discoverServices_(
728+
ble::connection_handle_t connectionHandle,
729+
ServiceDiscovery::ServiceCallback_t callback,
730+
GattAttribute::Handle_t startHandle,
731+
GattAttribute::Handle_t endHandle
732+
);
733+
734+
ble_error_t launchServiceDiscovery_(
735+
ble::connection_handle_t connectionHandle,
736+
ServiceDiscovery::ServiceCallback_t sc,
737+
ServiceDiscovery::CharacteristicCallback_t cc,
738+
const UUID &matchingServiceUUID,
739+
const UUID &matchingCharacteristicUUIDIn
740+
);
741+
742+
bool isServiceDiscoveryActive_(void) const;
743+
744+
void terminateServiceDiscovery_(void);
745+
746+
ble_error_t negotiateAttMtu_(ble::connection_handle_t connection);
747+
748+
ble_error_t read_(
749+
ble::connection_handle_t connHandle,
750+
GattAttribute::Handle_t attributeHandle,
751+
uint16_t offset
752+
) const;
753+
754+
ble_error_t write_(
755+
GattClient::WriteOp_t cmd,
756+
ble::connection_handle_t connHandle,
757+
GattAttribute::Handle_t attributeHandle,
758+
size_t length,
759+
const uint8_t *value
760+
) const;
761+
762+
void onServiceDiscoveryTermination_(
763+
ServiceDiscovery::TerminationCallback_t callback
764+
);
765+
766+
ble_error_t discoverCharacteristicDescriptors_(
767+
const DiscoveredCharacteristic& characteristic,
768+
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
769+
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
770+
);
771+
772+
bool isCharacteristicDescriptorDiscoveryActive_(
773+
const DiscoveredCharacteristic& characteristic
774+
) const;
775+
776+
void terminateCharacteristicDescriptorDiscovery_(
777+
const DiscoveredCharacteristic& characteristic
778+
);
796779

797780
protected:
798781
GattClient() : eventHandler(NULL)
@@ -888,4 +871,12 @@ class GattClient {
888871
* @}
889872
*/
890873

874+
#if !defined(DOXYGEN_ONLY)
875+
} // namespace interface
876+
} // namespace ble
877+
878+
using ble::impl::GattClient;
879+
#endif
880+
881+
891882
#endif /* ifndef MBED_GATT_CLIENT_H__ */

0 commit comments

Comments
 (0)