BLE: Cleanup BLEInstanceBas implementation

pull/13475/head
Vincent Coubard 2020-08-24 17:09:50 +01:00
parent 2af0f88fa6
commit 029ed3a0c5
10 changed files with 114 additions and 104 deletions

View File

@ -49,10 +49,6 @@ namespace ble {
* *
* @see BLE * @see BLE
*/ */
#if !defined(DOXYGEN_ONLY)
namespace interface {
#endif // !defined(DOXYGEN_ONLY)
class BLEInstanceBase { class BLEInstanceBase {
public: public:
/** /**
@ -116,7 +112,7 @@ public:
* *
* @see BLE::init() BLE::hasInitialized() * @see BLE::init() BLE::hasInitialized()
*/ */
virtual bool hasInitialized(void) const = 0; virtual bool hasInitialized() const = 0;
/** /**
* Shutdown the vendor BLE subsystem. * Shutdown the vendor BLE subsystem.
@ -139,7 +135,7 @@ public:
* @see BLE::shutdown() BLE::init() BLE::hasInitialized() Gap::reset() * @see BLE::shutdown() BLE::init() BLE::hasInitialized() Gap::reset()
* GattClient::reset() GattServer::reset() SecurityManager::reset() . * GattClient::reset() GattServer::reset() SecurityManager::reset() .
*/ */
virtual ble_error_t shutdown(void) = 0; virtual ble_error_t shutdown() = 0;
/** /**
* Fetches a NULL terminated string representation of the underlying BLE * Fetches a NULL terminated string representation of the underlying BLE
@ -150,7 +146,7 @@ public:
* *
* @see BLE::getVersion() * @see BLE::getVersion()
*/ */
virtual const char *getVersion(void) = 0; virtual const char *getVersion() = 0;
/** /**
* Accessor to the vendor implementation of the Gap interface. * Accessor to the vendor implementation of the Gap interface.
@ -160,7 +156,7 @@ public:
* *
* @see BLE::gap() Gap * @see BLE::gap() Gap
*/ */
virtual ble::Gap &getGap(void) = 0; virtual ble::Gap &getGap() = 0;
/** /**
* Const alternative to getGap(). * Const alternative to getGap().
@ -170,7 +166,7 @@ public:
* *
* @see BLE::gap() Gap * @see BLE::gap() Gap
*/ */
virtual const ble::Gap &getGap(void) const = 0; virtual const ble::Gap &getGap() const = 0;
#if BLE_FEATURE_GATT_SERVER #if BLE_FEATURE_GATT_SERVER
@ -183,7 +179,7 @@ public:
* *
* @see BLE::gattServer() GattServer * @see BLE::gattServer() GattServer
*/ */
virtual ble::GattServer &getGattServer(void) = 0; virtual ble::GattServer &getGattServer() = 0;
/** /**
* A const alternative to getGattServer(). * A const alternative to getGattServer().
@ -193,7 +189,7 @@ public:
* *
* @see BLE::gattServer() GattServer * @see BLE::gattServer() GattServer
*/ */
virtual const ble::GattServer &getGattServer(void) const = 0; virtual const ble::GattServer &getGattServer() const = 0;
#endif // BLE_FEATURE_GATT_SERVER #endif // BLE_FEATURE_GATT_SERVER
@ -207,7 +203,7 @@ public:
* *
* @see BLE::gattClient() GattClient * @see BLE::gattClient() GattClient
*/ */
virtual ble::GattClient &getGattClient(void) = 0; virtual ble::GattClient &getGattClient() = 0;
#endif #endif
@ -221,7 +217,7 @@ public:
* *
* @see BLE::securityManager() SecurityManager * @see BLE::securityManager() SecurityManager
*/ */
virtual ble::SecurityManager &getSecurityManager(void) = 0; virtual ble::SecurityManager &getSecurityManager() = 0;
/** /**
* A const alternative to getSecurityManager(). * A const alternative to getSecurityManager().
@ -231,16 +227,11 @@ public:
* *
* @see BLE::securityManager() SecurityManager * @see BLE::securityManager() SecurityManager
*/ */
virtual const ble::SecurityManager &getSecurityManager(void) const = 0; virtual const ble::SecurityManager &getSecurityManager() const = 0;
#endif // BLE_FEATURE_SECURITY #endif // BLE_FEATURE_SECURITY
}; };
#if !defined(DOXYGEN_ONLY)
} // namespace interface
#endif // !defined(DOXYGEN_ONLY)
} // namespace ble
/** /**
* Return the instance of the vendor implementation of BLEInstanceBase. * Return the instance of the vendor implementation of BLEInstanceBase.
* *
@ -250,15 +241,13 @@ public:
* @attention The vendor library must provide an implementation for this function * @attention The vendor library must provide an implementation for this function
* library. Otherwise, there will be a linker error. * library. Otherwise, there will be a linker error.
*/ */
extern ble::BLEInstanceBase *createBLEInstance(void); extern ble::BLEInstanceBase *createBLEInstance();
/* This includes the concrete class implementation, to provide a an alternative BLE PAL implementation
* disable Cordio and place your header in a path with the same structure */
#include "ble/internal/BLEInstanceBaseImpl.h"
/** /**
* @} * @}
* @} * @}
*/ */
} // namespace ble
#endif // ifndef MBED_BLE_DEVICE_INSTANCE_BASE__ #endif // ifndef MBED_BLE_DEVICE_INSTANCE_BASE__

View File

@ -46,15 +46,18 @@ namespace ble {
class PalSigningMonitor; class PalSigningMonitor;
namespace impl {
/** /**
* @see BLEInstanceBase * @see BLEInstanceBase
*/ */
class BLEInstanceBase : public interface::BLEInstanceBase { class BLEInstanceBase : public ble::BLEInstanceBase {
friend PalSigningMonitor; friend PalSigningMonitor;
/** /**
* Construction with an HCI driver. * Construction with an HCI driver.
*/ */
BLEInstanceBase(CordioHCIDriver& hci_driver); BLEInstanceBase(CordioHCIDriver &hci_driver);
/** /**
* Destructor * Destructor
@ -67,13 +70,13 @@ public:
* Access to the singleton containing the implementation of BLEInstanceBase * Access to the singleton containing the implementation of BLEInstanceBase
* for the Cordio stack. * for the Cordio stack.
*/ */
static BLEInstanceBase& deviceInstance(); static BLEInstanceBase &deviceInstance();
/** /**
* @see BLEInstanceBase::init * @see BLEInstanceBase::init
*/ */
virtual ble_error_t init( virtual ble_error_t init(
FunctionPointerWithContext< ::BLE::InitializationCompleteCallbackContext*> initCallback FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> initCallback
); );
/** /**
@ -91,34 +94,37 @@ public:
*/ */
virtual const char *getVersion(); virtual const char *getVersion();
ble::impl::Gap& getGapImpl(); ble::impl::Gap &getGapImpl();
/** /**
* @see BLEInstanceBase::getGap * @see BLEInstanceBase::getGap
*/ */
virtual ble::Gap& getGap(); virtual ble::Gap &getGap();
/** /**
* @see BLEInstanceBase::getGap * @see BLEInstanceBase::getGap
*/ */
virtual const ble::Gap& getGap() const; virtual const ble::Gap &getGap() const;
#if BLE_FEATURE_GATT_SERVER #if BLE_FEATURE_GATT_SERVER
ble::impl::GattServer& getGattServerImpl();
ble::impl::GattServer &getGattServerImpl();
/** /**
* @see BLEInstanceBase::getGattServer * @see BLEInstanceBase::getGattServer
*/ */
virtual GattServer &getGattServer(); virtual ble::GattServer &getGattServer();
/** /**
* @see BLEInstanceBase::getGattServer * @see BLEInstanceBase::getGattServer
*/ */
virtual const GattServer &getGattServer() const; virtual const ble::GattServer &getGattServer() const;
#endif // BLE_FEATURE_GATT_SERVER #endif // BLE_FEATURE_GATT_SERVER
#if BLE_FEATURE_GATT_CLIENT #if BLE_FEATURE_GATT_CLIENT
ble::impl::GattClient& getGattClientImpl();
ble::impl::GattClient &getGattClientImpl();
/** /**
* @see BLEInstanceBase::getGattClient * @see BLEInstanceBase::getGattClient
@ -131,10 +137,12 @@ public:
* @return PAL Gatt Client. * @return PAL Gatt Client.
*/ */
PalGattClient &getPalGattClient(); PalGattClient &getPalGattClient();
#endif // BLE_FEATURE_GATT_CLIENT #endif // BLE_FEATURE_GATT_CLIENT
#if BLE_FEATURE_SECURITY #if BLE_FEATURE_SECURITY
ble::impl::SecurityManager& getSecurityManagerImpl();
ble::impl::SecurityManager &getSecurityManagerImpl();
/** /**
* @see BLEInstanceBase::getSecurityManager * @see BLEInstanceBase::getSecurityManager
@ -159,17 +167,22 @@ public:
virtual void processEvents(); virtual void processEvents();
private: private:
static void stack_handler(wsfEventMask_t event, wsfMsgHdr_t* msg); static void stack_handler(wsfEventMask_t event, wsfMsgHdr_t *msg);
static void device_manager_cb(dmEvt_t* dm_event);
static void connection_handler(dmEvt_t* dm_event); static void device_manager_cb(dmEvt_t *dm_event);
static void connection_handler(dmEvt_t *dm_event);
static void timeoutCallback(); static void timeoutCallback();
void stack_setup(); void stack_setup();
void start_stack_reset(); void start_stack_reset();
void callDispatcher(); void callDispatcher();
static CordioHCIDriver* _hci_driver; static CordioHCIDriver *_hci_driver;
static FunctionPointerWithContext< ::BLE::InitializationCompleteCallbackContext*> _init_callback; static FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> _init_callback;
enum { enum {
NOT_INITIALIZED, NOT_INITIALIZED,
@ -182,6 +195,7 @@ private:
uint64_t _last_update_us; uint64_t _last_update_us;
}; };
} // namespace impl
} // namespace ble } // namespace ble

View File

@ -20,9 +20,10 @@
#define IMPL_PAL_ATT_CLIENT_ #define IMPL_PAL_ATT_CLIENT_
#include "source/pal/PalSimpleAttServerMessage.h" #include "source/pal/PalSimpleAttServerMessage.h"
#include "att_api.h"
#include "source/pal/PalAttClient.h" #include "source/pal/PalAttClient.h"
#include "att_api.h"
namespace ble { namespace ble {
namespace impl { namespace impl {

View File

@ -50,6 +50,7 @@
#include "internal/PalAttClientImpl.h" #include "internal/PalAttClientImpl.h"
#include "internal/PalGenericAccessServiceImpl.h" #include "internal/PalGenericAccessServiceImpl.h"
#include "PalGapImpl.h" #include "PalGapImpl.h"
#include "internal/BLEInstanceBaseImpl.h"
using namespace std::chrono; using namespace std::chrono;
@ -105,12 +106,13 @@ extern "C" MBED_WEAK void wsf_mbed_ble_signal_event(void)
* BLE-API requires an implementation of the following function in order to * BLE-API requires an implementation of the following function in order to
* obtain its transport handle. * obtain its transport handle.
*/ */
ble::BLEInstanceBase *createBLEInstance() ble::BLEInstanceBase *ble::createBLEInstance()
{ {
return (&(ble::BLEInstanceBase::deviceInstance())); return (&(ble::impl::BLEInstanceBase::deviceInstance()));
} }
namespace ble { namespace ble {
namespace impl {
BLEInstanceBase::BLEInstanceBase(CordioHCIDriver &hci_driver) : BLEInstanceBase::BLEInstanceBase(CordioHCIDriver &hci_driver) :
initialization_status(NOT_INITIALIZED), initialization_status(NOT_INITIALIZED),
@ -122,7 +124,8 @@ BLEInstanceBase::BLEInstanceBase(CordioHCIDriver &hci_driver) :
} }
BLEInstanceBase::~BLEInstanceBase() BLEInstanceBase::~BLEInstanceBase()
{} {
}
/** /**
* The singleton which represents the BLE transport for the BLE. * The singleton which represents the BLE transport for the BLE.
@ -136,7 +139,8 @@ BLEInstanceBase &BLEInstanceBase::deviceInstance()
} }
ble_error_t BLEInstanceBase::init( ble_error_t BLEInstanceBase::init(
FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> initCallback) FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> initCallback
)
{ {
switch (initialization_status) { switch (initialization_status) {
case NOT_INITIALIZED: case NOT_INITIALIZED:
@ -206,7 +210,7 @@ ble::impl::Gap &BLEInstanceBase::getGapImpl()
ble::Gap &BLEInstanceBase::getGap() ble::Gap &BLEInstanceBase::getGap()
{ {
auto& impl = getGapImpl(); auto &impl = getGapImpl();
static ble::Gap gap(&impl); static ble::Gap gap(&impl);
return gap; return gap;
} }
@ -224,14 +228,14 @@ ble::impl::GattServer &BLEInstanceBase::getGattServerImpl()
return ble::impl::GattServer::getInstance(); return ble::impl::GattServer::getInstance();
} }
GattServer &BLEInstanceBase::getGattServer() ble::GattServer &BLEInstanceBase::getGattServer()
{ {
auto& impl = getGattServerImpl(); auto &impl = getGattServerImpl();
static GattServer server(&impl); static ble::GattServer server(&impl);
return server; return server;
} }
const GattServer &BLEInstanceBase::getGattServer() const const ble::GattServer &BLEInstanceBase::getGattServer() const
{ {
BLEInstanceBase &self = const_cast<BLEInstanceBase &>(*this); BLEInstanceBase &self = const_cast<BLEInstanceBase &>(*this);
return const_cast<const ble::GattServer &>(self.getGattServer()); return const_cast<const ble::GattServer &>(self.getGattServer());
@ -249,7 +253,7 @@ ble::impl::GattClient &BLEInstanceBase::getGattClientImpl()
ble::GattClient &BLEInstanceBase::getGattClient() ble::GattClient &BLEInstanceBase::getGattClient()
{ {
auto& impl = getGattClientImpl(); auto &impl = getGattClientImpl();
static ble::GattClient gatt_client(&impl); static ble::GattClient gatt_client(&impl);
impl.setInterface(&gatt_client); impl.setInterface(&gatt_client);
return gatt_client; return gatt_client;
@ -290,16 +294,16 @@ ble::impl::SecurityManager &BLEInstanceBase::getSecurityManagerImpl()
return m_instance; return m_instance;
} }
SecurityManager &BLEInstanceBase::getSecurityManager() ble::SecurityManager &BLEInstanceBase::getSecurityManager()
{ {
static SecurityManager m_instance(&getSecurityManagerImpl()); static ble::SecurityManager m_instance(&getSecurityManagerImpl());
return m_instance; return m_instance;
} }
const SecurityManager &BLEInstanceBase::getSecurityManager() const const ble::SecurityManager &BLEInstanceBase::getSecurityManager() const
{ {
const BLEInstanceBase &self = const_cast<BLEInstanceBase &>(*this); const BLEInstanceBase &self = const_cast<BLEInstanceBase &>(*this);
return const_cast<const SecurityManager &>(self.getSecurityManager()); return const_cast<const ble::SecurityManager &>(self.getSecurityManager());
} }
#endif // BLE_FEATURE_SECURITY #endif // BLE_FEATURE_SECURITY
@ -650,4 +654,5 @@ CordioHCIDriver *BLEInstanceBase::_hci_driver = nullptr;
FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> BLEInstanceBase::_init_callback; FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> BLEInstanceBase::_init_callback;
} // namespace impl
} // namespace ble } // namespace ble

View File

@ -19,7 +19,7 @@
#include "ble/common/BLERoles.h" #include "ble/common/BLERoles.h"
#include <algorithm> #include <algorithm>
#include "GattServerImpl.h" #include "GattServerImpl.h"
#include "source/BLEInstanceBase.h" #include "BLEInstanceBaseImpl.h"
#include "wsf_types.h" #include "wsf_types.h"
#include "att_api.h" #include "att_api.h"
@ -1080,7 +1080,7 @@ uint8_t GattServer::atts_auth_cb(dmConnId_t connId, uint8_t permit, uint16_t han
#if BLE_FEATURE_SECURITY #if BLE_FEATURE_SECURITY
// this CB is triggered when read or write of an attribute (either a value // this CB is triggered when read or write of an attribute (either a value
// handle or a descriptor) requires secure connection security. // handle or a descriptor) requires secure connection security.
ble::SecurityManager &security_manager = BLEInstanceBase::deviceInstance().getSecurityManager(); ble::SecurityManager &security_manager = impl::BLEInstanceBase::deviceInstance().getSecurityManager();
link_encryption_t encryption(link_encryption_t::NOT_ENCRYPTED); link_encryption_t encryption(link_encryption_t::NOT_ENCRYPTED);
ble_error_t err = security_manager.getLinkEncryption(connId, &encryption); ble_error_t err = security_manager.getLinkEncryption(connId, &encryption);
@ -1349,7 +1349,7 @@ bool GattServer::is_update_authorized(
} }
#if BLE_FEATURE_SECURITY #if BLE_FEATURE_SECURITY
ble::SecurityManager &security_manager = BLEInstanceBase::deviceInstance().getSecurityManager(); ble::SecurityManager &security_manager = impl::BLEInstanceBase::deviceInstance().getSecurityManager();
link_encryption_t encryption(link_encryption_t::NOT_ENCRYPTED); link_encryption_t encryption(link_encryption_t::NOT_ENCRYPTED);
ble_error_t err = security_manager.getLinkEncryption(connection, &encryption); ble_error_t err = security_manager.getLinkEncryption(connection, &encryption);
if (err) { if (err) {

View File

@ -45,8 +45,9 @@
namespace ble { namespace ble {
// fwd declaration of PalAttClient and BLE // fwd declaration of PalAttClient, PalGenericAccessService and BLE
class PalAttClient; class PalAttClient;
class PalGenericAccessService;
class BLE; class BLE;
namespace impl { namespace impl {
@ -54,7 +55,6 @@ class GattServer : public PalSigningMonitor {
friend ble::BLE; friend ble::BLE;
friend ble::PalAttClient; friend ble::PalAttClient;
friend PalSigningMonitor; friend PalSigningMonitor;
friend BLEInstanceBase;
friend PalGenericAccessService; friend PalGenericAccessService;
using EventHandler = ble::GattServer::EventHandler; using EventHandler = ble::GattServer::EventHandler;
@ -195,6 +195,18 @@ public:
static void att_cb(const attEvt_t *pEvt); static void att_cb(const attEvt_t *pEvt);
/**
* Initialize the GattServer and add mandatory services (generic access and
* generic attribute service).
*/
void initialize();
static uint8_t atts_auth_cb(dmConnId_t connId, uint8_t permit, uint16_t handle);
void set_signing_event_handler(
PalSigningMonitorEventHandler *signing_event_handler
) override;
private: private:
GattServer(); GattServer();
@ -203,16 +215,6 @@ private:
const GattServer &operator=(const GattServer &); const GattServer &operator=(const GattServer &);
/**
* Initialize the GattServer and add mandatory services (generic access and
* generic attribute service).
*/
void initialize();
void set_signing_event_handler(
PalSigningMonitorEventHandler *signing_event_handler
) override;
EventHandler *getEventHandler(); EventHandler *getEventHandler();
void add_default_services(); void add_default_services();
@ -267,8 +269,6 @@ private:
attsAttr_t *pAttr attsAttr_t *pAttr
); );
static uint8_t atts_auth_cb(dmConnId_t connId, uint8_t permit, uint16_t handle);
void add_generic_access_service(); void add_generic_access_service();
void add_generic_attribute_service(); void add_generic_attribute_service();

View File

@ -20,7 +20,7 @@
#include "GattServerImpl.h" #include "GattServerImpl.h"
#include "source/pal/PalSimpleAttServerMessage.h" #include "source/pal/PalSimpleAttServerMessage.h"
#include "source/pal/PalGattClient.h" #include "source/pal/PalGattClient.h"
#include "source/BLEInstanceBase.h" #include "BLEInstanceBaseImpl.h"
#include "internal/PalAttClientImpl.h" #include "internal/PalAttClientImpl.h"
#include "att_api.h" #include "att_api.h"
@ -395,13 +395,13 @@ void PalAttClient::att_client_handler(const attEvt_t *event)
{ {
#if BLE_FEATURE_GATT_CLIENT #if BLE_FEATURE_GATT_CLIENT
if (event->hdr.status == ATT_SUCCESS && event->hdr.event == ATT_MTU_UPDATE_IND) { if (event->hdr.status == ATT_SUCCESS && event->hdr.event == ATT_MTU_UPDATE_IND) {
ble::BLEInstanceBase &ble = ble::BLEInstanceBase::deviceInstance(); ble::impl::BLEInstanceBase &ble = BLEInstanceBase::deviceInstance();
PalGattClientEventHandler *handler = ble.getPalGattClient().get_event_handler(); PalGattClientEventHandler *handler = ble.getPalGattClient().get_event_handler();
if (handler) { if (handler) {
handler->on_att_mtu_change(event->hdr.param, event->mtu); handler->on_att_mtu_change(event->hdr.param, event->mtu);
} }
} else if (event->hdr.event == ATTC_WRITE_CMD_RSP) { } else if (event->hdr.event == ATTC_WRITE_CMD_RSP) {
ble::BLEInstanceBase &ble = ble::BLEInstanceBase::deviceInstance(); ble::impl::BLEInstanceBase &ble = BLEInstanceBase::deviceInstance();
PalGattClientEventHandler *handler = ble.getPalGattClient().get_event_handler(); PalGattClientEventHandler *handler = ble.getPalGattClient().get_event_handler();
if (handler) { if (handler) {
handler->on_write_command_sent( handler->on_write_command_sent(

View File

@ -30,6 +30,7 @@
#include "source/pal/GapTypes.h" #include "source/pal/GapTypes.h"
#include "source/pal/PalGenericAccessService.h" #include "source/pal/PalGenericAccessService.h"
#include "source/pal/PalEventQueue.h" #include "source/pal/PalEventQueue.h"
#include "source/pal/PalSecurityManager.h"
using namespace std::chrono; using namespace std::chrono;
@ -319,9 +320,9 @@ const central_privacy_configuration_t Gap::default_central_privacy_configuration
Gap::Gap( Gap::Gap(
ble::PalEventQueue &event_queue, ble::PalEventQueue &event_queue,
PalGap &pal_gap, ble::PalGap &pal_gap,
PalGenericAccessService &generic_access_service, ble::PalGenericAccessService &generic_access_service,
PalSecurityManager &pal_sm ble::PalSecurityManager &pal_sm
) : _event_queue(event_queue), ) : _event_queue(event_queue),
_pal_gap(pal_gap), _pal_gap(pal_gap),
_gap_service(generic_access_service), _gap_service(generic_access_service),

View File

@ -358,9 +358,9 @@ private:
Gap( Gap(
ble::PalEventQueue &event_queue, ble::PalEventQueue &event_queue,
PalGap &pal_gap, ble::PalGap &pal_gap,
PalGenericAccessService &generic_access_service, ble::PalGenericAccessService &generic_access_service,
PalSecurityManager &pal_sm ble::PalSecurityManager &pal_sm
); );
~Gap(); ~Gap();

View File

@ -51,7 +51,6 @@ class SecurityManager :
public ble::PalSigningMonitorEventHandler { public ble::PalSigningMonitorEventHandler {
friend class ble::PalConnectionMonitorEventHandler; friend class ble::PalConnectionMonitorEventHandler;
friend ::ble::BLEInstanceBase;
friend PalGenericAccessService; friend PalGenericAccessService;
friend PalSecurityManager; friend PalSecurityManager;
@ -449,12 +448,7 @@ private:
/* end implements PalSecurityManager::EventHandler */ /* end implements PalSecurityManager::EventHandler */
private: public:
/* Disallow copy and assignment. */
SecurityManager(const SecurityManager &);
SecurityManager &operator=(const SecurityManager &);
SecurityManager( SecurityManager(
PalSecurityManager &palImpl, PalSecurityManager &palImpl,
PalConnectionMonitor &connMonitorImpl, PalConnectionMonitor &connMonitorImpl,
@ -484,6 +478,12 @@ private:
delete _db; delete _db;
} }
private:
/* Disallow copy and assignment. */
SecurityManager(const SecurityManager &);
SecurityManager &operator=(const SecurityManager &);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Helper functions // Helper functions
// //