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
*/
#if !defined(DOXYGEN_ONLY)
namespace interface {
#endif // !defined(DOXYGEN_ONLY)
class BLEInstanceBase {
public:
/**
@ -116,7 +112,7 @@ public:
*
* @see BLE::init() BLE::hasInitialized()
*/
virtual bool hasInitialized(void) const = 0;
virtual bool hasInitialized() const = 0;
/**
* Shutdown the vendor BLE subsystem.
@ -139,7 +135,7 @@ public:
* @see BLE::shutdown() BLE::init() BLE::hasInitialized() Gap::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
@ -150,7 +146,7 @@ public:
*
* @see BLE::getVersion()
*/
virtual const char *getVersion(void) = 0;
virtual const char *getVersion() = 0;
/**
* Accessor to the vendor implementation of the Gap interface.
@ -160,7 +156,7 @@ public:
*
* @see BLE::gap() Gap
*/
virtual ble::Gap &getGap(void) = 0;
virtual ble::Gap &getGap() = 0;
/**
* Const alternative to getGap().
@ -170,7 +166,7 @@ public:
*
* @see BLE::gap() Gap
*/
virtual const ble::Gap &getGap(void) const = 0;
virtual const ble::Gap &getGap() const = 0;
#if BLE_FEATURE_GATT_SERVER
@ -183,7 +179,7 @@ public:
*
* @see BLE::gattServer() GattServer
*/
virtual ble::GattServer &getGattServer(void) = 0;
virtual ble::GattServer &getGattServer() = 0;
/**
* A const alternative to getGattServer().
@ -193,7 +189,7 @@ public:
*
* @see BLE::gattServer() GattServer
*/
virtual const ble::GattServer &getGattServer(void) const = 0;
virtual const ble::GattServer &getGattServer() const = 0;
#endif // BLE_FEATURE_GATT_SERVER
@ -207,7 +203,7 @@ public:
*
* @see BLE::gattClient() GattClient
*/
virtual ble::GattClient &getGattClient(void) = 0;
virtual ble::GattClient &getGattClient() = 0;
#endif
@ -221,7 +217,7 @@ public:
*
* @see BLE::securityManager() SecurityManager
*/
virtual ble::SecurityManager &getSecurityManager(void) = 0;
virtual ble::SecurityManager &getSecurityManager() = 0;
/**
* A const alternative to getSecurityManager().
@ -231,16 +227,11 @@ public:
*
* @see BLE::securityManager() SecurityManager
*/
virtual const ble::SecurityManager &getSecurityManager(void) const = 0;
virtual const ble::SecurityManager &getSecurityManager() const = 0;
#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.
*
@ -250,15 +241,13 @@ public:
* @attention The vendor library must provide an implementation for this function
* library. Otherwise, there will be a linker error.
*/
extern ble::BLEInstanceBase *createBLEInstance(void);
/* 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"
extern ble::BLEInstanceBase *createBLEInstance();
/**
* @}
* @}
*/
} // namespace ble
#endif // ifndef MBED_BLE_DEVICE_INSTANCE_BASE__

View File

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

View File

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

View File

@ -50,6 +50,7 @@
#include "internal/PalAttClientImpl.h"
#include "internal/PalGenericAccessServiceImpl.h"
#include "PalGapImpl.h"
#include "internal/BLEInstanceBaseImpl.h"
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
* obtain its transport handle.
*/
ble::BLEInstanceBase *createBLEInstance()
ble::BLEInstanceBase *ble::createBLEInstance()
{
return (&(ble::BLEInstanceBase::deviceInstance()));
return (&(ble::impl::BLEInstanceBase::deviceInstance()));
}
namespace ble {
namespace impl {
BLEInstanceBase::BLEInstanceBase(CordioHCIDriver &hci_driver) :
initialization_status(NOT_INITIALIZED),
@ -122,7 +124,8 @@ BLEInstanceBase::BLEInstanceBase(CordioHCIDriver &hci_driver) :
}
BLEInstanceBase::~BLEInstanceBase()
{}
{
}
/**
* The singleton which represents the BLE transport for the BLE.
@ -136,7 +139,8 @@ BLEInstanceBase &BLEInstanceBase::deviceInstance()
}
ble_error_t BLEInstanceBase::init(
FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> initCallback)
FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> initCallback
)
{
switch (initialization_status) {
case NOT_INITIALIZED:
@ -206,7 +210,7 @@ ble::impl::Gap &BLEInstanceBase::getGapImpl()
ble::Gap &BLEInstanceBase::getGap()
{
auto& impl = getGapImpl();
auto &impl = getGapImpl();
static ble::Gap gap(&impl);
return gap;
}
@ -224,14 +228,14 @@ ble::impl::GattServer &BLEInstanceBase::getGattServerImpl()
return ble::impl::GattServer::getInstance();
}
GattServer &BLEInstanceBase::getGattServer()
ble::GattServer &BLEInstanceBase::getGattServer()
{
auto& impl = getGattServerImpl();
static GattServer server(&impl);
auto &impl = getGattServerImpl();
static ble::GattServer server(&impl);
return server;
}
const GattServer &BLEInstanceBase::getGattServer() const
const ble::GattServer &BLEInstanceBase::getGattServer() const
{
BLEInstanceBase &self = const_cast<BLEInstanceBase &>(*this);
return const_cast<const ble::GattServer &>(self.getGattServer());
@ -249,7 +253,7 @@ ble::impl::GattClient &BLEInstanceBase::getGattClientImpl()
ble::GattClient &BLEInstanceBase::getGattClient()
{
auto& impl = getGattClientImpl();
auto &impl = getGattClientImpl();
static ble::GattClient gatt_client(&impl);
impl.setInterface(&gatt_client);
return gatt_client;
@ -290,16 +294,16 @@ ble::impl::SecurityManager &BLEInstanceBase::getSecurityManagerImpl()
return m_instance;
}
SecurityManager &BLEInstanceBase::getSecurityManager()
ble::SecurityManager &BLEInstanceBase::getSecurityManager()
{
static SecurityManager m_instance(&getSecurityManagerImpl());
static ble::SecurityManager m_instance(&getSecurityManagerImpl());
return m_instance;
}
const SecurityManager &BLEInstanceBase::getSecurityManager() const
const ble::SecurityManager &BLEInstanceBase::getSecurityManager() const
{
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
@ -369,21 +373,21 @@ void BLEInstanceBase::stack_handler(wsfEventMask_t event, wsfMsgHdr_t *msg)
}
break;
#if MBED_CONF_CORDIO_ROUTE_UNHANDLED_COMMAND_COMPLETE_EVENTS
case DM_UNHANDLED_CMD_CMPL_EVT_IND: {
// upcast to unhandled command complete event to access the payload
hciUnhandledCmdCmplEvt_t *unhandled = (hciUnhandledCmdCmplEvt_t *) msg;
if (unhandled->hdr.status == HCI_SUCCESS && unhandled->hdr.param == HCI_OPCODE_LE_TEST_END) {
// unhandled events are not parsed so we need to parse the payload ourselves
uint8_t status;
uint16_t packet_number;
status = unhandled->param[0];
BYTES_TO_UINT16(packet_number, unhandled->param + 1);
case DM_UNHANDLED_CMD_CMPL_EVT_IND: {
// upcast to unhandled command complete event to access the payload
hciUnhandledCmdCmplEvt_t *unhandled = (hciUnhandledCmdCmplEvt_t *) msg;
if (unhandled->hdr.status == HCI_SUCCESS && unhandled->hdr.param == HCI_OPCODE_LE_TEST_END) {
// unhandled events are not parsed so we need to parse the payload ourselves
uint8_t status;
uint16_t packet_number;
status = unhandled->param[0];
BYTES_TO_UINT16(packet_number, unhandled->param + 1);
_hci_driver->handle_test_end(status == 0, packet_number);
return;
_hci_driver->handle_test_end(status == 0, packet_number);
return;
}
}
}
break;
break;
#endif // MBED_CONF_CORDIO_ROUTE_UNHANDLED_COMMAND_COMPLETE_EVENTS
default:
@ -463,8 +467,8 @@ void BLEInstanceBase::stack_setup()
// This warning will be raised if we've allocated too much memory
if (bytes_used < buf_pool_desc.buffer_size) {
MBED_WARNING1(MBED_MAKE_ERROR(MBED_MODULE_BLE, MBED_ERROR_CODE_INVALID_SIZE),
"Too much memory allocated for Cordio memory pool, reduce buf_pool_desc.buffer_size by value below.",
buf_pool_desc.buffer_size - bytes_used);
"Too much memory allocated for Cordio memory pool, reduce buf_pool_desc.buffer_size by value below.",
buf_pool_desc.buffer_size - bytes_used);
}
WsfTimerInit();
@ -650,4 +654,5 @@ CordioHCIDriver *BLEInstanceBase::_hci_driver = nullptr;
FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> BLEInstanceBase::_init_callback;
} // namespace impl
} // namespace ble

View File

@ -19,7 +19,7 @@
#include "ble/common/BLERoles.h"
#include <algorithm>
#include "GattServerImpl.h"
#include "source/BLEInstanceBase.h"
#include "BLEInstanceBaseImpl.h"
#include "wsf_types.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
// this CB is triggered when read or write of an attribute (either a value
// 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);
ble_error_t err = security_manager.getLinkEncryption(connId, &encryption);
@ -1349,7 +1349,7 @@ bool GattServer::is_update_authorized(
}
#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);
ble_error_t err = security_manager.getLinkEncryption(connection, &encryption);
if (err) {

View File

@ -45,8 +45,9 @@
namespace ble {
// fwd declaration of PalAttClient and BLE
// fwd declaration of PalAttClient, PalGenericAccessService and BLE
class PalAttClient;
class PalGenericAccessService;
class BLE;
namespace impl {
@ -54,7 +55,6 @@ class GattServer : public PalSigningMonitor {
friend ble::BLE;
friend ble::PalAttClient;
friend PalSigningMonitor;
friend BLEInstanceBase;
friend PalGenericAccessService;
using EventHandler = ble::GattServer::EventHandler;
@ -195,6 +195,18 @@ public:
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:
GattServer();
@ -203,16 +215,6 @@ private:
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();
void add_default_services();
@ -267,8 +269,6 @@ private:
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_attribute_service();

View File

@ -20,7 +20,7 @@
#include "GattServerImpl.h"
#include "source/pal/PalSimpleAttServerMessage.h"
#include "source/pal/PalGattClient.h"
#include "source/BLEInstanceBase.h"
#include "BLEInstanceBaseImpl.h"
#include "internal/PalAttClientImpl.h"
#include "att_api.h"
@ -395,13 +395,13 @@ void PalAttClient::att_client_handler(const attEvt_t *event)
{
#if BLE_FEATURE_GATT_CLIENT
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();
if (handler) {
handler->on_att_mtu_change(event->hdr.param, event->mtu);
}
} 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();
if (handler) {
handler->on_write_command_sent(

View File

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

View File

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

View File

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