BLE - Devirtualization of ::Gap

The interface definition now lives in ::ble::interface::LegacyGap. Implementation must export the implementation type as ble::impl::LegacyGap.
pull/9727/head
Vincent Coubard 2019-02-25 19:03:22 +00:00
parent 2d007eee42
commit 1ad726322f
2 changed files with 726 additions and 354 deletions

View File

@ -17,6 +17,7 @@
#ifndef MBED_BLE_GAP_H__
#define MBED_BLE_GAP_H__
#include "ble/common/StaticInterface.h"
#include "BLETypes.h"
#include "BLEProtocol.h"
#include "ble/GapAdvertisingData.h"
@ -29,6 +30,7 @@
#include "platform/mbed_toolchain.h"
#include "ble/gap/Gap.h"
#include "BleImplementationForward.h"
/**
* @addtogroup ble
@ -37,19 +39,34 @@
* @{
*/
#if !defined(DOXYGEN_ONLY)
namespace ble {
namespace interface {
#endif
/**
* @copydoc ble::Gap
*/
#if defined(DOXYGEN_ONLY)
class Gap : public ble::Gap {
#else
template<class Impl>
class LegacyGap :
public ble::StaticInterface<Impl, LegacyGap>,
public ble::interface::Gap<Impl>
{
#endif
using ble::StaticInterface<Impl, ::ble::interface::LegacyGap>::impl;
public:
using ble::Gap::setAdvertisingParameters;
using ble::Gap::setAdvertisingPayload;
using ble::Gap::setAdvertisingScanResponse;
using ble::Gap::startAdvertising;
using ble::Gap::stopAdvertising;
using ble::Gap::connect;
using ble::Gap::disconnect;
using ble::Gap::startScan;
using ble::interface::Gap<Impl>::setAdvertisingParameters;
using ble::interface::Gap<Impl>::setAdvertisingPayload;
using ble::interface::Gap<Impl>::setAdvertisingScanResponse;
using ble::interface::Gap<Impl>::startAdvertising;
using ble::interface::Gap<Impl>::stopAdvertising;
using ble::interface::Gap<Impl>::connect;
using ble::interface::Gap<Impl>::disconnect;
using ble::interface::Gap<Impl>::startScan;
/**
* Address-type for BLEProtocol addresses.
@ -738,14 +755,14 @@ public:
*
* @see Gap::onShutdown().
*/
typedef FunctionPointerWithContext<const Gap *> GapShutdownCallback_t;
typedef FunctionPointerWithContext<const LegacyGap *> GapShutdownCallback_t;
/**
* Callchain of gap shutdown event handler.
*
* @see Gap::onShutdown().
*/
typedef CallChainOfFunctionPointersWithContext<const Gap *>
typedef CallChainOfFunctionPointersWithContext<const LegacyGap *>
GapShutdownCallbackChain_t;
/*
@ -790,7 +807,7 @@ public:
"mbed-os-5.9.0",
"Non portable API, use enablePrivacy to enable use of private addresses"
)
virtual ble_error_t setAddress(
ble_error_t setAddress(
BLEProtocol::AddressType_t type,
const BLEProtocol::AddressBytes_t address
);
@ -806,7 +823,7 @@ public:
*
* @return BLE_ERROR_NONE on success.
*/
virtual ble_error_t getAddress(
ble_error_t getAddress(
BLEProtocol::AddressType_t *typeP,
BLEProtocol::AddressBytes_t address
);
@ -834,12 +851,7 @@ public:
* @return Minimum Advertising interval in milliseconds for connectable
* undirected and connectable directed advertising types.
*/
virtual uint16_t getMinAdvertisingInterval(void) const
{
/* Requesting action from porter(s): override this API if this capability
is supported. */
return 0;
}
uint16_t getMinAdvertisingInterval(void) const;
/**
* Get the minimum advertising interval in milliseconds, which can be
@ -848,24 +860,14 @@ public:
* @return Minimum Advertising interval in milliseconds for scannable
* undirected and nonconnectable undirected event types.
*/
virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const
{
/* Requesting action from porter(s): override this API if this capability
is supported. */
return 0;
}
uint16_t getMinNonConnectableAdvertisingInterval(void) const;
/**
* Get the maximum advertising interval in milliseconds.
*
* @return Maximum Advertising interval in milliseconds.
*/
virtual uint16_t getMaxAdvertisingInterval(void) const
{
/* Requesting action from porter(s): override this API if this capability
is supported. */
return 0xFFFF;
}
uint16_t getMaxAdvertisingInterval(void) const;
/**
* Stop the ongoing advertising procedure.
@ -883,7 +885,7 @@ public:
"Deprecated since addition of extended advertising support."
"Use stopAdvertising(advertising_handle_t) instead."
)
virtual ble_error_t stopAdvertising(void);
ble_error_t stopAdvertising(void);
/**
* Initiate a connection to a peer.
@ -909,7 +911,7 @@ public:
"Deprecated since addition of extended advertising support."
"Use connect(target_peer_address_type_t, address_t, ConnectionParameters) instead."
)
virtual ble_error_t connect(
ble_error_t connect(
const BLEProtocol::AddressBytes_t peerAddr,
PeerAddressType_t peerAddrType,
const ConnectionParams_t *connectionParams,
@ -940,7 +942,7 @@ public:
"This function won't work if privacy is enabled; You must use the overload "
"accepting PeerAddressType_t."
)
virtual ble_error_t connect(
ble_error_t connect(
const BLEProtocol::AddressBytes_t peerAddr,
BLEProtocol::AddressType_t peerAddrType,
const ConnectionParams_t *connectionParams,
@ -990,7 +992,7 @@ public:
"Deprecated since addition of extended advertising support."
"Use disconnect(connection_handle_t, local_disconnection_reason_t) instead."
)
virtual ble_error_t disconnect(
ble_error_t disconnect(
Handle_t connectionHandle, DisconnectionReason_t reason
);
@ -1007,7 +1009,7 @@ public:
* @return BLE_ERROR_NONE if disconnection was successful.
*/
MBED_DEPRECATED("Use disconnect(Handle_t, DisconnectionReason_t) instead.")
virtual ble_error_t disconnect(DisconnectionReason_t reason);
ble_error_t disconnect(DisconnectionReason_t reason);
public:
@ -1020,15 +1022,7 @@ public:
* @return BLE_ERROR_NONE if the parameters were successfully filled into
* @p params.
*/
virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params)
{
/* Avoid compiler warnings about unused variables. */
(void) params;
/* Requesting action from porter(s): override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
ble_error_t getPreferredConnectionParams(ConnectionParams_t *params);
/**
* Set the value of the preferred connection parameters exposed in the GATT
@ -1043,17 +1037,9 @@ public:
* @return BLE_ERROR_NONE if the preferred connection params were set
* correctly.
*/
virtual ble_error_t setPreferredConnectionParams(
ble_error_t setPreferredConnectionParams(
const ConnectionParams_t *params
)
{
/* Avoid compiler warnings about unused variables. */
(void) params;
/* Requesting action from porter(s): override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
);
/**
* Update connection parameters of an existing connection.
@ -1079,7 +1065,7 @@ public:
"conn_interval_t, slave_latency_t, supervision_timeout_t, "
"conn_event_length_t, conn_event_length_t) instead."
)
virtual ble_error_t updateConnectionParams(
ble_error_t updateConnectionParams(
Handle_t handle,
const ConnectionParams_t *params
);
@ -1093,15 +1079,7 @@ public:
*
* @return BLE_ERROR_NONE if the device name was set correctly.
*/
virtual ble_error_t setDeviceName(const uint8_t *deviceName)
{
/* Avoid compiler warnings about unused variables. */
(void) deviceName;
/* Requesting action from porter(s): override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
ble_error_t setDeviceName(const uint8_t *deviceName);
/**
* Get the value of the device name characteristic in the Generic Access
@ -1125,16 +1103,7 @@ public:
* bytes actually returned in deviceName. The application may use this
* information to retry with a suitable buffer size.
*/
virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP)
{
/* avoid compiler warnings about unused variables */
(void) deviceName;
(void) lengthP;
/* Requesting action from porter(s): override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
/**
* Set the value of the appearance characteristic in the GAP service.
@ -1143,15 +1112,7 @@ public:
*
* @return BLE_ERROR_NONE if the new appearance was set correctly.
*/
virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance)
{
/* Avoid compiler warnings about unused variables. */
(void) appearance;
/* Requesting action from porter(s): override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
/**
* Get the value of the appearance characteristic in the GAP service.
@ -1161,15 +1122,7 @@ public:
* @return BLE_ERROR_NONE if the device-appearance was fetched correctly
* from the underlying BLE stack.
*/
virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP)
{
/* Avoid compiler warnings about unused variables. */
(void) appearanceP;
/* Requesting action from porter(s): override this API if this capability
is supported. */
return BLE_ERROR_NOT_IMPLEMENTED;
}
ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
/**
* Set the radio's transmit power.
@ -1187,7 +1140,7 @@ public:
"Deprecated since addition of extended advertising support."
"See ble::AdvertisingParameters and setAdvertisingParameters."
)
virtual ble_error_t setTxPower(int8_t txPower);
ble_error_t setTxPower(int8_t txPower);
/**
* Query the underlying stack for allowed Tx power values.
@ -1201,7 +1154,7 @@ public:
"mbed-os-5.11.0",
"Deprecated since addition of extended advertising support."
)
virtual void getPermittedTxPowerValues(
void getPermittedTxPowerValues(
const int8_t **valueArrayPP, size_t *countP
);
@ -1213,7 +1166,7 @@ public:
* @note If using Mbed OS, you can configure the size of the whitelist by
* setting the YOTTA_CFG_WHITELIST_MAX_SIZE macro in your yotta config file.
*/
virtual uint8_t getMaxWhitelistSize(void) const;
uint8_t getMaxWhitelistSize(void) const;
/**
* Get the Link Layer to use the internal whitelist when scanning,
@ -1225,7 +1178,7 @@ public:
* @return BLE_ERROR_NONE if the implementation's whitelist was successfully
* copied into the supplied reference.
*/
virtual ble_error_t getWhitelist(Whitelist_t &whitelist) const;
ble_error_t getWhitelist(Whitelist_t &whitelist) const;
/**
* Set the value of the whitelist to be used during GAP procedures.
@ -1245,7 +1198,7 @@ public:
* @note If the input whitelist is larger than @ref getMaxWhitelistSize(),
* then @ref BLE_ERROR_PARAM_OUT_OF_RANGE is returned.
*/
virtual ble_error_t setWhitelist(const Whitelist_t &whitelist);
ble_error_t setWhitelist(const Whitelist_t &whitelist);
/**
* Set the advertising policy filter mode to be used during the next
@ -1264,7 +1217,7 @@ public:
"Deprecated since addition of extended advertising support."
"This setting is now part of ble::AdvertisingParameters."
)
virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
/**
* Set the scan policy filter mode to be used during the next scan procedure.
@ -1282,7 +1235,7 @@ public:
"Deprecated since addition of extended advertising support."
"This setting is now part of ble::ScanParameters."
)
virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode);
ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode);
/**
* Set the initiator policy filter mode to be used during the next connection
@ -1301,7 +1254,7 @@ public:
"Deprecated since addition of extended advertising support."
"This setting is now part of ble::ConnectionParameters."
)
virtual ble_error_t setInitiatorPolicyMode(InitiatorPolicyMode_t mode);
ble_error_t setInitiatorPolicyMode(InitiatorPolicyMode_t mode);
/**
* Get the current advertising policy filter mode.
@ -1314,7 +1267,7 @@ public:
"mbed-os-5.11.0",
"Deprecated since addition of extended advertising support."
)
virtual AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
/**
* Get the current scan policy filter mode.
@ -1327,7 +1280,7 @@ public:
"mbed-os-5.11.0",
"Deprecated since addition of extended advertising support."
)
virtual ScanningPolicyMode_t getScanningPolicyMode(void) const;
ScanningPolicyMode_t getScanningPolicyMode(void) const;
/**
* Get the current initiator policy filter mode.
@ -1340,7 +1293,7 @@ public:
"mbed-os-5.11.0",
"Deprecated since addition of extended advertising support."
)
virtual InitiatorPolicyMode_t getInitiatorPolicyMode(void) const;
InitiatorPolicyMode_t getInitiatorPolicyMode(void) const;
protected:
/* Override the following in the underlying adaptation layer to provide the
@ -1363,7 +1316,7 @@ protected:
"Vendors should use the cordio hci interface or the ble::pal or "
"implement startScan(duplicates_filter_t, scan_duration_t, period)"
)
virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams);
ble_error_t startRadioScan(const GapScanningParams &scanningParams);
/*
* APIs with nonvirtual implementations.
@ -1933,7 +1886,7 @@ public:
"mbed-os-5.11.0",
"Deprecated since addition of extended advertising support. "
)
virtual ble_error_t initRadioNotification(void);
ble_error_t initRadioNotification(void);
private:
/**
@ -1954,10 +1907,10 @@ private:
"Deprecated since addition of extended advertising support. "
"Implement setAdvertisingPayload() and setAdvertisingScanResponse() instead."
)
virtual ble_error_t setAdvertisingData(
ble_error_t setAdvertisingData(
const GapAdvertisingData &advData,
const GapAdvertisingData &scanResponse
) = 0;
);
/**
* Start the advertising procedure.
@ -1979,7 +1932,7 @@ private:
"Implement startAdvertising(advertising_handle_t, adv_duration_t, uint8_t)"
"instead."
)
virtual ble_error_t startAdvertising(const GapAdvertisingParams &params) = 0;
ble_error_t startAdvertising(const GapAdvertisingParams &params);
public:
/**
@ -2237,7 +2190,7 @@ public:
* @param[in] memberPtr Shutdown event handler to register.
*/
template<typename T>
void onShutdown(T *objPtr, void (T::*memberPtr)(const Gap *))
void onShutdown(T *objPtr, void (T::*memberPtr)(const LegacyGap *))
{
shutdownCallChain.add(objPtr, memberPtr);
}
@ -2272,56 +2225,13 @@ public:
* @note Currently, a call to reset() does not reset the advertising and
* scan parameters to default values.
*/
virtual ble_error_t reset(void)
{
/* Notify that the instance is about to shut down */
shutdownCallChain.call(this);
shutdownCallChain.clear();
/* Clear Gap state */
state.advertising = 0;
state.connected = 0;
connectionCount = 0;
/* Clear scanning state */
scanningActive = false;
/* Clear advertising and scanning data */
_advPayload.clear();
_scanResponse.clear();
/* Clear callbacks */
timeoutCallbackChain.clear();
connectionCallChain.clear();
disconnectionCallChain.clear();
radioNotificationCallback = NULL;
onAdvertisementReport = NULL;
_eventHandler = NULL;
return BLE_ERROR_NONE;
}
ble_error_t reset(void);
protected:
/**
* Construct a Gap instance.
*/
Gap() :
_advParams(),
_advPayload(),
_scanningParams(),
_scanResponse(),
connectionCount(0),
state(),
scanningActive(false),
timeoutCallbackChain(),
radioNotificationCallback(),
onAdvertisementReport(),
connectionCallChain(),
disconnectionCallChain()
{
_advPayload.clear();
_scanResponse.clear();
}
LegacyGap();
/* Entry points for the underlying stack to report events back to the user. */
public:
@ -2418,17 +2328,7 @@ public:
"Deprecated since addition of extended advertising support. "
"Use EventHandler::onDisconnectionComplete() instead"
)
void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason)
{
/* Update Gap state */
--connectionCount;
if (!connectionCount) {
state.connected = 0;
}
DisconnectionCallbackParams_t callbackParams(handle, reason);
disconnectionCallChain.call(&callbackParams);
}
void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason);
/**
* Forward a received advertising packet to all registered event handlers
@ -2517,16 +2417,7 @@ public:
"Deprecated since addition of extended advertising support. "
"Use EventHandler instead"
)
void processTimeoutEvent(TimeoutSource_t source)
{
if (source == TIMEOUT_SRC_ADVERTISING) {
/* Update gap state if the source is an advertising timeout */
state.advertising = 0;
}
if (timeoutCallbackChain) {
timeoutCallbackChain(source);
}
}
void processTimeoutEvent(TimeoutSource_t source);
protected:
/**
@ -2603,9 +2494,114 @@ private:
private:
/* Disallow copy and assignment. */
Gap(const Gap &);
LegacyGap(const LegacyGap &);
Gap &operator=(const Gap &);
LegacyGap &operator=(const LegacyGap &);
protected:
using ble::interface::Gap<Impl>::startAdvertising_;
using ble::interface::Gap<Impl>::stopAdvertising_;
using ble::interface::Gap<Impl>::connect_;
using ble::interface::Gap<Impl>::disconnect_;
/* --- Abstract calls with default implementation --- */
uint16_t getMinAdvertisingInterval_(void) const;
uint16_t getMinNonConnectableAdvertisingInterval_(void) const;
uint16_t getMaxAdvertisingInterval_(void) const;
/* Note: Implementation must call the base class reset_ */
ble_error_t reset_(void);
/* --- Abstract calls to override --- */
uint8_t getMaxWhitelistSize_(void) const;
ble_error_t getWhitelist_(Whitelist_t &whitelist) const;
ble_error_t setWhitelist_(const Whitelist_t &whitelist);
ble_error_t setAddress_(
BLEProtocol::AddressType_t type,
const BLEProtocol::AddressBytes_t address
);
ble_error_t getAddress_(
BLEProtocol::AddressType_t *typeP,
BLEProtocol::AddressBytes_t address
);
ble_error_t stopAdvertising_(void);
ble_error_t connect_(
const BLEProtocol::AddressBytes_t peerAddr,
PeerAddressType_t peerAddrType,
const ConnectionParams_t *connectionParams,
const GapScanningParams *scanParams
);
ble_error_t connect_(
const BLEProtocol::AddressBytes_t peerAddr,
BLEProtocol::AddressType_t peerAddrType,
const ConnectionParams_t *connectionParams,
const GapScanningParams *scanParams
);
ble_error_t disconnect_(
Handle_t connectionHandle, DisconnectionReason_t reason
);
ble_error_t disconnect_(DisconnectionReason_t reason);
ble_error_t updateConnectionParams_(
Handle_t handle,
const ConnectionParams_t *params
);
ble_error_t setTxPower_(int8_t txPower);
void getPermittedTxPowerValues_(
const int8_t **valueArrayPP, size_t *countP
);
ble_error_t setAdvertisingPolicyMode_(AdvertisingPolicyMode_t mode);
ble_error_t setScanningPolicyMode_(ScanningPolicyMode_t mode);
ble_error_t setInitiatorPolicyMode_(InitiatorPolicyMode_t mode);
AdvertisingPolicyMode_t getAdvertisingPolicyMode_(void) const;
ScanningPolicyMode_t getScanningPolicyMode_(void) const;
InitiatorPolicyMode_t getInitiatorPolicyMode_(void) const;
ble_error_t startRadioScan_(const GapScanningParams &scanningParams);
ble_error_t initRadioNotification_(void);
ble_error_t getPreferredConnectionParams_(ConnectionParams_t *params);
ble_error_t setPreferredConnectionParams_(
const ConnectionParams_t *params
);
ble_error_t setDeviceName_(const uint8_t *deviceName);
ble_error_t getDeviceName_(uint8_t *deviceName, unsigned *lengthP);
ble_error_t setAppearance_(GapAdvertisingData::Appearance appearance);
ble_error_t getAppearance_(GapAdvertisingData::Appearance *appearanceP);
ble_error_t setAdvertisingData_(
const GapAdvertisingData &advData,
const GapAdvertisingData &scanResponse
);
ble_error_t startAdvertising_(const GapAdvertisingParams &params);
};
/**
@ -2623,8 +2619,9 @@ private:
#pragma diag_suppress 1361
#endif
template<class Impl>
template<typename T>
ble_error_t Gap::startScan(
ble_error_t LegacyGap<Impl>::startScan(
T *object,
void (T::*callbackMember)(const AdvertisementCallbackParams_t *params)
)
@ -2640,21 +2637,23 @@ ble_error_t Gap::startScan(
return err;
}
template<class Impl>
template<typename T>
void Gap::onConnection(T *tptr, void (T::*mptr)(const ConnectionCallbackParams_t *))
void LegacyGap<Impl>::onConnection(T *tptr, void (T::*mptr)(const ConnectionCallbackParams_t *))
{
connectionCallChain.add(tptr, mptr);
}
template<class Impl>
template<typename T>
void Gap::onDisconnection(T *tptr, void (T::*mptr)(const DisconnectionCallbackParams_t *))
void LegacyGap<Impl>::onDisconnection(T *tptr, void (T::*mptr)(const DisconnectionCallbackParams_t *))
{
disconnectionCallChain.add(tptr, mptr);
}
template<class Impl>
template<typename T>
void Gap::onRadioNotification(T *tptr, void (T::*mptr)(bool))
void LegacyGap<Impl>::onRadioNotification(T *tptr, void (T::*mptr)(bool))
{
radioNotificationCallback.attach(tptr, mptr);
}
@ -2665,4 +2664,16 @@ void Gap::onRadioNotification(T *tptr, void (T::*mptr)(bool))
#pragma pop
#endif
} // interface
} // ble
// import LegacyGap implementation into global namespace
typedef ble::impl::LegacyGap Gap;
// import Gap implementation into ble namespace
namespace ble {
typedef impl::Gap Gap;
}
#endif // ifndef MBED_BLE_GAP_H__