BLE: Bind interface to private implementation.

pull/13475/head
Vincent Coubard 2020-08-21 16:02:43 +01:00
parent 0929478b7f
commit c773870d56
16 changed files with 108 additions and 39 deletions

View File

@ -1439,6 +1439,17 @@ public:
GapShutdownCallbackChain_t &onShutdown();
#if !defined(DOXYGEN_ONLY)
/*
* Constructor from the private implementation.
*/
Gap(impl::Gap* impl) : impl(impl) {}
/*
* Restrict copy and move.
*/
Gap(const Gap&) = delete;
Gap& operator=(const Gap&) = delete;
/*
* API reserved for the controller driver to set the random static address.
* Setting a new random static address while the controller is operating is

View File

@ -651,7 +651,6 @@ public:
*/
HVXCallbackChain_t& onHVX();
/**
* Reset the state of the GattClient instance.
*
@ -706,6 +705,12 @@ public:
*/
void processHVXEvent(const GattHVXCallbackParams *params);
#if !defined(DOXYGEN_ONLY)
GattClient(impl::GattClient* impl) : impl(impl) { }
GattClient(const GattClient&) = delete;
GattClient& operator=(const GattClient&) = delete;
#endif // !defined(DOXYGEN_ONLY)
private:
impl::GattClient *impl;
};

View File

@ -594,6 +594,12 @@ public:
*/
void onConfirmationReceived(EventCallback_t callback);
#if !defined(DOXYGEN_ONLY)
GattServer(impl::GattServer* impl) : impl(impl) {}
GattServer(const GattServer&) = delete;
GattServer& operator=(const GattServer&) = delete;
#endif // !defined(DOXYGEN_ONLY)
private:
impl::GattServer *impl;
};

View File

@ -897,6 +897,10 @@ public:
/** For backwards compatibility. This enum is now in BLETypes.h
* @deprecated use the enum in ble namespace */
typedef ble::Keypress_t Keypress_t;
SecurityManager(impl::SecurityManager* impl) : impl(impl) {}
SecurityManager(const SecurityManager&) = delete;
SecurityManager& operator=(const SecurityManager&) = delete;
#endif // !defined(DOXYGEN_ONLY)
private:

View File

@ -36,6 +36,11 @@
#include "drivers/LowPowerTimer.h"
#include "ble/internal/PalSecurityManager.h"
#include "ble/internal/GapImpl.h"
#include "ble/internal/GattClientImpl.h"
#include "ble/internal/GattServerImpl.h"
#include "ble/internal/SecurityManagerImpl.h"
namespace ble {
class PalSigningMonitor;
@ -85,6 +90,8 @@ public:
*/
virtual const char *getVersion();
ble::impl::Gap& getGapImpl();
/**
* @see BLEInstanceBase::getGap
*/
@ -96,6 +103,8 @@ public:
virtual const ble::Gap& getGap() const;
#if BLE_FEATURE_GATT_SERVER
ble::impl::GattServer& getGattServerImpl();
/**
* @see BLEInstanceBase::getGattServer
*/
@ -108,6 +117,8 @@ public:
#endif // BLE_FEATURE_GATT_SERVER
#if BLE_FEATURE_GATT_CLIENT
ble::impl::GattClient& getGattClientImpl();
/**
* @see BLEInstanceBase::getGattClient
*/
@ -122,6 +133,8 @@ public:
#endif // BLE_FEATURE_GATT_CLIENT
#if BLE_FEATURE_SECURITY
ble::impl::SecurityManager& getSecurityManagerImpl();
/**
* @see BLEInstanceBase::getSecurityManager
*/

View File

@ -20,10 +20,13 @@
#define IMPL_PAL_GENERIC_ACCESS_SERVICE_
#include "ble/internal/PalGenericAccessService.h"
#include "ble/GattServer.h"
#include "ble/Gap.h"
namespace ble {
namespace impl {
class GattServer;
}
/**
* Implementation of ble::PalGenericAccessService for the Cordio stack.
@ -52,7 +55,7 @@ public:
private:
#if BLE_FEATURE_GATT_SERVER
ble::GattServer& gatt_server();
ble::impl::GattServer& gatt_server();
#endif // BLE_FEATURE_GATT_SERVER
};

View File

@ -23,11 +23,9 @@
namespace ble {
class SecurityManager;
class PalSigningMonitor : public interface::PalSigningMonitor {
public:
void set_signing_event_handler(SecurityManager *handler);
void set_signing_event_handler(PalSigningMonitorEventHandler *handler);
};
} // ble

View File

@ -43,6 +43,7 @@
#include "ble/internal/PalSigningMonitor.h"
#include "ble/internal/BLEInstanceBase.h"
#include "CordioHCIDriver.h"
#include "ble/internal/GattServerImpl.h"
using namespace std::chrono;
@ -184,16 +185,22 @@ const char *BLEInstanceBase::getVersion()
return version;
}
ble::Gap &BLEInstanceBase::getGap()
ble::impl::Gap &BLEInstanceBase::getGapImpl()
{
static ble::PalGenericAccessService cordio_gap_service;
static ble::Gap gap(
static ble::impl::Gap gap(
_event_queue,
ble::PalGap::get_gap(),
cordio_gap_service,
ble::PalSecurityManager::get_security_manager()
);
return gap;
}
ble::Gap &BLEInstanceBase::getGap()
{
auto& impl = getGapImpl();
static ble::Gap gap(&impl);
return gap;
}
@ -205,24 +212,39 @@ const ble::Gap &BLEInstanceBase::getGap() const
#if BLE_FEATURE_GATT_SERVER
ble::impl::GattServer &BLEInstanceBase::getGattServerImpl()
{
return ble::impl::GattServer::getInstance();
}
GattServer &BLEInstanceBase::getGattServer()
{
return GattServer::getInstance();
auto& impl = getGattServerImpl();
static GattServer server(&impl);
return server;
}
const GattServer &BLEInstanceBase::getGattServer() const
{
return GattServer::getInstance();
BLEInstanceBase &self = const_cast<BLEInstanceBase &>(*this);
return const_cast<const ble::GattServer &>(self.getGattServer());
}
#endif // BLE_FEATURE_GATT_SERVER
#if BLE_FEATURE_GATT_CLIENT
ble::impl::GattClient &BLEInstanceBase::getGattClientImpl()
{
static ble::impl::GattClient gatt_client(getPalGattClient());
return gatt_client;
}
ble::GattClient &BLEInstanceBase::getGattClient()
{
static ble::GattClient gatt_client(getPalGattClient());
auto& impl = getGattClientImpl();
static ble::GattClient gatt_client(&impl);
impl.setInterface(&gatt_client);
return gatt_client;
}
@ -236,18 +258,24 @@ PalGattClient &BLEInstanceBase::getPalGattClient()
#if BLE_FEATURE_SECURITY
SecurityManager &BLEInstanceBase::getSecurityManager()
ble::impl::SecurityManager &BLEInstanceBase::getSecurityManagerImpl()
{
static PalSigningMonitor signing_event_monitor;
static ble::SecurityManager m_instance(
static ble::impl::SecurityManager m_instance(
ble::PalSecurityManager::get_security_manager(),
getGap(),
getGapImpl(),
signing_event_monitor
);
return m_instance;
}
SecurityManager &BLEInstanceBase::getSecurityManager()
{
static SecurityManager m_instance(&getSecurityManagerImpl());
return m_instance;
}
const SecurityManager &BLEInstanceBase::getSecurityManager() const
{
const BLEInstanceBase &self = const_cast<BLEInstanceBase &>(*this);
@ -293,7 +321,7 @@ void BLEInstanceBase::stack_handler(wsfEventMask_t event, wsfMsgHdr_t *msg)
switch (msg->event) {
case DM_RESET_CMPL_IND: {
::BLE::InitializationCompleteCallbackContext context = {
::BLE::Instance(::BLE::DEFAULT_INSTANCE),
::BLE::Instance(),
BLE_ERROR_NONE
};
#if BLE_FEATURE_EXTENDED_ADVERTISING
@ -314,7 +342,7 @@ void BLEInstanceBase::stack_handler(wsfEventMask_t event, wsfMsgHdr_t *msg)
}
#endif // BLE_FEATURE_EXTENDED_ADVERTISING
#if BLE_FEATURE_GATT_SERVER
deviceInstance().getGattServer().initialize();
deviceInstance().getGattServerImpl().initialize();
#endif
deviceInstance().initialization_status = INITIALIZED;
_init_callback.call(&context);
@ -499,7 +527,7 @@ void BLEInstanceBase::stack_setup()
AttsInit();
AttsIndInit();
#if BLE_FEATURE_SECURITY
AttsAuthorRegister(GattServer::atts_auth_cb);
AttsAuthorRegister(impl::GattServer::atts_auth_cb);
#endif
#if BLE_FEATURE_SIGNING
AttsSignInit();

View File

@ -17,7 +17,7 @@
*/
#include "ble/internal/PalAttClient.h"
#include "ble/GattServer.h"
#include "ble/internal/GattServerImpl.h"
#include "ble/internal/PalSimpleAttServerMessage.h"
#include "ble/internal/PalGap.h"
#include "ble/internal/PalGattClient.h"
@ -444,7 +444,7 @@ void PalAttClient::att_client_handler(const attEvt_t *event)
#if BLE_FEATURE_GATT_SERVER
// pass events not handled to the server side
ble::GattServer::getInstance().att_cb(event);
ble::impl::GattServer::getInstance().att_cb(event);
#endif // BLE_FEATURE_GATT_SERVER
}

View File

@ -17,6 +17,7 @@
*/
#include "ble/internal/PalGenericAccessService.h"
#include "ble/internal/GattServerImpl.h"
namespace ble {
@ -115,11 +116,12 @@ ble_error_t PalGenericAccessService::set_peripheral_preferred_connection_paramet
#if BLE_FEATURE_GATT_SERVER
ble::GattServer& PalGenericAccessService::gatt_server()
ble::impl::GattServer& PalGenericAccessService::gatt_server()
{
return ble::GattServer::getInstance();
return ble::impl::GattServer::getInstance();
}
#endif // BLE_FEATURE_GATT_SERVER
} // ble
#endif // BLE_FEATURE_GATT_SERVER

View File

@ -18,17 +18,16 @@
#include "ble/internal/PalSigningMonitor.h"
#include "ble/internal/BLEInstanceBase.h"
#include "ble/GattClient.h"
namespace ble {
void PalSigningMonitor::set_signing_event_handler(SecurityManager *handler)
void PalSigningMonitor::set_signing_event_handler(PalSigningMonitorEventHandler *handler)
{
#if BLE_FEATURE_GATT_CLIENT
BLEInstanceBase::deviceInstance().getGattClient().set_signing_event_handler(handler);
BLEInstanceBase::deviceInstance().getGattClientImpl().set_signing_event_handler(handler);
#endif // BLE_FEATURE_GATT_CLIENT
#if BLE_FEATURE_GATT_SERVER
BLEInstanceBase::deviceInstance().getGattServer().set_signing_event_handler(handler);
BLEInstanceBase::deviceInstance().getGattServerImpl().set_signing_event_handler(handler);
#endif // BLE_FEATURE_GATT_SERVER
}

View File

@ -252,8 +252,7 @@ private:
mutable ProcedureControlBlock *control_blocks;
bool _is_reseting;
// TODO initialize
::ble::GattClient *client;
::ble::GattClient *client = nullptr;
private:
/**
@ -264,6 +263,11 @@ private:
~GattClient()
{
}
void setInterface(ble::GattClient *client_interface)
{
client = client_interface;
}
};
} // namespace impl

View File

@ -75,7 +75,7 @@ public:
ble_error_t setDatabaseFilepath(const char *dbFilepath = NULL);
ble_error_t reset(void);
ble_error_t reset(ble::SecurityManager* sm);
ble_error_t preserveBondingStateOnReset(bool enable);

View File

@ -1207,7 +1207,7 @@ ble_error_t GattClient::write(
if (cmd == GATT_OP_SIGNED_WRITE_CMD) {
ble::link_encryption_t encryption(ble::link_encryption_t::NOT_ENCRYPTED);
// FIXME: use security manager or a template if applicable
SecurityManager &sm = createBLEInstance()->getSecurityManager();
ble::SecurityManager &sm = createBLEInstance()->getSecurityManager();
ble_error_t status = sm.getLinkEncryption(connection_handle, &encryption);
if (status == BLE_ERROR_NONE &&
(encryption == link_encryption_t::ENCRYPTED ||

View File

@ -122,9 +122,7 @@ ble_error_t SecurityManager::init(
_connection_monitor.set_connection_event_handler(this);
#endif
#if BLE_FEATURE_SIGNING
// TODO: FIXME
// _signing_monitor.set_signing_event_handler(this);
_signing_monitor.set_signing_event_handler(nullptr);
_signing_monitor.set_signing_event_handler(this);
#endif
_pal.set_event_handler(this);
@ -174,16 +172,14 @@ ble_error_t SecurityManager::setDatabaseFilepath(
}
ble_error_t SecurityManager::reset(void)
ble_error_t SecurityManager::reset(ble::SecurityManager* sm)
{
delete _db;
_db = nullptr;
_pal.reset();
/* Notify that the instance is about to shutdown */
// TODO: FIXME
// shutdownCallChain.call(this);
shutdownCallChain.call(nullptr);
shutdownCallChain.call(sm);
shutdownCallChain.clear();
eventHandler = &defaultEventHandler;

View File

@ -39,7 +39,7 @@ ble_error_t SecurityManager::setDatabaseFilepath(const char *dbFilepath)
ble_error_t SecurityManager::reset()
{
return impl->reset();
return impl->reset(this);
}
ble_error_t SecurityManager::preserveBondingStateOnReset(bool enable)