Skip to content

Commit

Permalink
feat!: Update WB55 GAP constructor (#227)
Browse files Browse the repository at this point in the history
* WB55 gap constructor updated to be shorter (less than 7 elements) and to contain rf wake up clock source

* Update hal_st/middlewares/ble_middleware/GapCentralSt.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update hal_st/middlewares/ble_middleware/GapSt.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: EkelmansPh <[email protected]>

* include schi in the header file

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: EkelmansPh <[email protected]>
  • Loading branch information
3 people authored Dec 8, 2023
1 parent ee43490 commit 0c8e2e4
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 32 deletions.
11 changes: 7 additions & 4 deletions hal_st/middlewares/ble_middleware/GapCentralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ namespace hal
}
}

GapCentralSt::GapCentralSt(hal::HciEventSource& hciEventSource, hal::MacAddress address, const RootKeys& rootKeys, uint16_t maxAttMtuSize, uint8_t txPowerLevel, const GapService gapService, infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator, uint32_t* bleBondsStorage)
: GapSt(hciEventSource, address, rootKeys, maxAttMtuSize, txPowerLevel, bondStorageSynchronizerCreator, *bleBondsStorage)
GapCentralSt::GapCentralSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration)
: GapSt(hciEventSource, bleBondStorage, configuration)
{
Initialize(gapService);
Initialize(configuration.gapService);

infra::Subject<services::GapCentralObserver>::NotifyObservers([](auto& observer) { observer.StateChanged(services::GapState::standby); });
infra::Subject<services::GapCentralObserver>::NotifyObservers([](auto& observer)
{
observer.StateChanged(services::GapState::standby);
});
}

void GapCentralSt::Connect(hal::MacAddress macAddress, services::GapDeviceAddressType addressType)
Expand Down
2 changes: 1 addition & 1 deletion hal_st/middlewares/ble_middleware/GapCentralSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace hal
, public GapSt
{
public:
GapCentralSt(hal::HciEventSource& hciEventSource, hal::MacAddress address, const RootKeys& rootKeys, uint16_t maxAttMtuSize, uint8_t txPowerLevel, const GapService gapService, infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator, uint32_t* bleBondsStorage);
GapCentralSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration);

// Implementation of services::GapCentral
void Connect(hal::MacAddress macAddress, services::GapDeviceAddressType addressType) override;
Expand Down
6 changes: 3 additions & 3 deletions hal_st/middlewares/ble_middleware/GapPeripheralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace

namespace hal
{
GapPeripheralSt::GapPeripheralSt(hal::HciEventSource& hciEventSource, hal::MacAddress address, const hal::GapSt::RootKeys& rootKeys, uint16_t maxAttMtuSize, uint8_t txPowerLevel, const GapService gapService, infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator, uint32_t* bleBondsStorage)
: GapSt(hciEventSource, address, rootKeys, maxAttMtuSize, txPowerLevel, bondStorageSynchronizerCreator, *bleBondsStorage)
GapPeripheralSt::GapPeripheralSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration)
: GapSt(hciEventSource, bleBondStorage, configuration)
{
Initialize(gapService);
Initialize(configuration.gapService);
}

services::GapAddress GapPeripheralSt::GetAddress() const
Expand Down
2 changes: 1 addition & 1 deletion hal_st/middlewares/ble_middleware/GapPeripheralSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace hal
, public GapSt
{
public:
GapPeripheralSt(hal::HciEventSource& hciEventSource, hal::MacAddress address, const hal::GapSt::RootKeys& rootKeys, uint16_t maxAttMtuSize, uint8_t txPowerLevel, const GapService gapService, infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator, uint32_t* bleBondsStorage);
GapPeripheralSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration);

// Implementation of GapPeripheral
services::GapAddress GetAddress() const override;
Expand Down
34 changes: 19 additions & 15 deletions hal_st/middlewares/ble_middleware/GapSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "ble_gap_aci.h"
#include "infra/event/EventDispatcherWithWeakPtr.hpp"
#include "services/ble/Gap.hpp"
#include "shci.h"
#include "stm32wbxx_ll_system.h"

namespace hal
Expand Down Expand Up @@ -32,6 +31,11 @@ namespace hal
return services::GapPairingObserver::PairingErrorType::unknown;
}
}

constexpr auto RfWakeupClockSelection(const hal::GapSt::RfWakeupClock& rfWakeupClock)
{
return infra::enum_cast(rfWakeupClock);
}
}

const services::GapConnectionParameters GapSt::connectionParameters{
Expand All @@ -41,27 +45,27 @@ namespace hal
500,
};

GapSt::GapSt(hal::HciEventSource& hciEventSource, hal::MacAddress& address, const RootKeys& rootKeys, uint16_t& maxAttMtuSize, uint8_t& txPowerLevel, infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator, uint32_t& bleBondsStorage)
GapSt::GapSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration)
: HciEventSink(hciEventSource)
, txPowerLevel(txPowerLevel)
, txPowerLevel(configuration.txPowerLevel)
{
really_assert(maxAttMtuSize >= BLE_DEFAULT_ATT_MTU && maxAttMtuSize <= 251);
really_assert(configuration.maxAttMtuSize >= BLE_DEFAULT_ATT_MTU && configuration.maxAttMtuSize <= 251);
// BLE middleware supported maxAttMtuSize = 512. Current usage of library limits maxAttMtuSize to 251 (max HCI buffer size)

connectionContext.connectionHandle = GapSt::invalidConnection;

const uint8_t maxNumberOfBleLinks = 0x01;
const uint8_t prepareWriteListSize = BLE_PREP_WRITE_X_ATT(maxAttMtuSize);
const uint8_t numberOfBleMemoryBlocks = BLE_MBLOCKS_CALC(prepareWriteListSize, maxAttMtuSize, maxNumberOfBleLinks);
const uint8_t prepareWriteListSize = BLE_PREP_WRITE_X_ATT(configuration.maxAttMtuSize);
const uint8_t numberOfBleMemoryBlocks = BLE_MBLOCKS_CALC(prepareWriteListSize, configuration.maxAttMtuSize, maxNumberOfBleLinks);
const uint8_t bleStackOptions = (SHCI_C2_BLE_INIT_OPTIONS_LL_HOST | SHCI_C2_BLE_INIT_OPTIONS_WITH_SVC_CHANGE_DESC | SHCI_C2_BLE_INIT_OPTIONS_DEVICE_NAME_RO | SHCI_C2_BLE_INIT_OPTIONS_NO_EXT_ADV | SHCI_C2_BLE_INIT_OPTIONS_NO_CS_ALGO2 |
SHCI_C2_BLE_INIT_OPTIONS_FULL_GATTDB_NVM | SHCI_C2_BLE_INIT_OPTIONS_GATT_CACHING_NOTUSED | SHCI_C2_BLE_INIT_OPTIONS_POWER_CLASS_2_3 | SHCI_C2_BLE_INIT_OPTIONS_APPEARANCE_READONLY | SHCI_C2_BLE_INIT_OPTIONS_ENHANCED_ATT_NOTSUPPORTED);
SHCI_C2_BLE_INIT_OPTIONS_FULL_GATTDB_NVM | SHCI_C2_BLE_INIT_OPTIONS_GATT_CACHING_NOTUSED | SHCI_C2_BLE_INIT_OPTIONS_POWER_CLASS_2_3 | SHCI_C2_BLE_INIT_OPTIONS_APPEARANCE_READONLY | SHCI_C2_BLE_INIT_OPTIONS_ENHANCED_ATT_NOTSUPPORTED);

SHCI_C2_CONFIG_Cmd_Param_t configParam = {
SHCI_C2_CONFIG_PAYLOAD_CMD_SIZE,
SHCI_C2_CONFIG_CONFIG1_BIT0_BLE_NVM_DATA_TO_SRAM,
SHCI_C2_CONFIG_EVTMASK1_BIT1_BLE_NVM_RAM_UPDATE_ENABLE,
0, // Spare
reinterpret_cast<uint32_t>(&bleBondsStorage),
reinterpret_cast<uint32_t>(&bleBondStorage.bleBondsStorage),
0, // ThreadNvmRamAddress
static_cast<uint16_t>(LL_DBGMCU_GetRevisionID()),
static_cast<uint16_t>(LL_DBGMCU_GetDeviceID()),
Expand All @@ -79,13 +83,13 @@ namespace hal
0x08, // Maximum number of Services that can be stored in the GATT database
0x540, // Size of the storage area for Attribute values
maxNumberOfBleLinks,
0x01, // Enable or disable the Extended Packet length feature
0x01, // Enable or disable the Extended Packet length feature
prepareWriteListSize,
numberOfBleMemoryBlocks,
maxAttMtuSize,
configuration.maxAttMtuSize,
0x1FA, // Sleep clock accuracy in Slave mode
0x00, // Sleep clock accuracy in Master mode
0x00, // Source for the low speed clock for RF wake-up
RfWakeupClockSelection(configuration.rfWakeupClock), // Source for the low speed clock for RF wake-up
0xFFFFFFFF, // Maximum duration of the connection event when the device is in Slave mode in units of 625/256 us (~2.44 us)
0x148, // Start up time of the high speed (16 or 32 MHz) crystal oscillator in units of 625/256 us (~2.44 us)
0x01, // Viterbi Mode
Expand All @@ -110,17 +114,17 @@ namespace hal
hci_reset();

// Write Identity root key used to derive LTK and CSRK
aci_hal_write_config_data(CONFIG_DATA_IR_OFFSET, CONFIG_DATA_IR_LEN, rootKeys.identity.data());
aci_hal_write_config_data(CONFIG_DATA_IR_OFFSET, CONFIG_DATA_IR_LEN, configuration.rootKeys.identity.data());

// Write Encryption root key used to derive LTK and CSRK
aci_hal_write_config_data(CONFIG_DATA_ER_OFFSET, CONFIG_DATA_ER_LEN, rootKeys.encryption.data());
aci_hal_write_config_data(CONFIG_DATA_ER_OFFSET, CONFIG_DATA_ER_LEN, configuration.rootKeys.encryption.data());

bondStorageSynchronizer.Emplace(bondStorageSynchronizerCreator);
bondStorageSynchronizer.Emplace(bleBondStorage.bondStorageSynchronizerCreator);

aci_hal_set_tx_power_level(1, txPowerLevel);
aci_gatt_init();

aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, address.data());
aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, configuration.address.data());

SVCCTL_Init();
}
Expand Down
27 changes: 25 additions & 2 deletions hal_st/middlewares/ble_middleware/GapSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "services/ble/BondStorageSynchronizer.hpp"
#include "services/ble/Gap.hpp"
#include "services/ble/Gatt.hpp"
#include "shci.h"

namespace hal
{
Expand All @@ -30,6 +31,28 @@ namespace hal
std::array<uint8_t, 16> encryption;
};

enum class RfWakeupClock : uint8_t
{
highSpeedExternal = SHCI_C2_BLE_INIT_CFG_BLE_LS_CLK_HSE_1024,
lowSpeedExternal = SHCI_C2_BLE_INIT_CFG_BLE_LS_CLK_LSE,
};

struct Configuration
{
const hal::MacAddress& address;
const GapService& gapService;
const RootKeys& rootKeys;
uint16_t maxAttMtuSize;
uint8_t txPowerLevel;
RfWakeupClock rfWakeupClock;
};

struct BleBondStorage
{
infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator;
uint32_t& bleBondsStorage;
};

// Implementation of AttMtuExchange
uint16_t EffectiveMaxAttMtuSize() const override;

Expand All @@ -47,7 +70,7 @@ namespace hal
void NumericComparisonConfirm(bool accept) override;

protected:
GapSt(hal::HciEventSource& hciEventSource, hal::MacAddress& address, const RootKeys& rootKeys, uint16_t& maxAttMtuSize, uint8_t& txPowerLevel, infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator, uint32_t& bleBondsStorage);
GapSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration);

virtual void HandleHciDisconnectEvent(hci_event_pckt& eventPacket);

Expand Down Expand Up @@ -103,7 +126,7 @@ namespace hal
static constexpr uint8_t maxNumberOfBonds = 10;

private:
uint8_t& txPowerLevel;
const uint8_t& txPowerLevel;
uint16_t maxAttMtu = defaultMaxAttMtuSize;
infra::Optional<infra::ProxyCreator<services::BondStorageSynchronizer, void()>> bondStorageSynchronizer;
};
Expand Down
4 changes: 2 additions & 2 deletions hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace hal
{
TracingGapCentralSt::TracingGapCentralSt(hal::HciEventSource& hciEventSource, hal::MacAddress address, const RootKeys& rootKeys, uint16_t maxAttMtuSize, uint8_t txPowerLevel, const GapService gapService, infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator, uint32_t* bleBondsStorage, services::Tracer& tracer)
: GapCentralSt(hciEventSource, address, rootKeys, maxAttMtuSize, txPowerLevel, gapService, bondStorageSynchronizerCreator, bleBondsStorage)
TracingGapCentralSt::TracingGapCentralSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration, services::Tracer& tracer)
: GapCentralSt(hciEventSource, bleBondStorage, configuration)
, tracer(tracer)
{}

Expand Down
2 changes: 1 addition & 1 deletion hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace hal
: public GapCentralSt
{
public:
TracingGapCentralSt(hal::HciEventSource& hciEventSource, hal::MacAddress address, const RootKeys& rootKeys, uint16_t maxAttMtuSize, uint8_t txPowerLevel, const GapService gapService, infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator, uint32_t* bleBondsStorage, services::Tracer& tracer);
TracingGapCentralSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration, services::Tracer& tracer);

// Implementation of services::GapCentral
void Connect(hal::MacAddress macAddress, services::GapDeviceAddressType addressType) override;
Expand Down
4 changes: 2 additions & 2 deletions hal_st/middlewares/ble_middleware/TracingGapPeripheralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace hal
{
TracingGapPeripheralSt::TracingGapPeripheralSt(hal::HciEventSource& hciEventSource, hal::MacAddress address, const RootKeys& rootKeys, uint16_t maxAttMtuSize, uint8_t txPowerLevel, const GapService& gapService, infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator, uint32_t* bleBondsStorage, services::Tracer& tracer)
: GapPeripheralSt(hciEventSource, address, rootKeys, maxAttMtuSize, txPowerLevel, gapService, bondStorageSynchronizerCreator, bleBondsStorage)
TracingGapPeripheralSt::TracingGapPeripheralSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration, services::Tracer& tracer)
: GapPeripheralSt(hciEventSource, bleBondStorage, configuration)
, tracer(tracer)
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace hal
: public GapPeripheralSt
{
public:
TracingGapPeripheralSt(hal::HciEventSource& hciEventSource, hal::MacAddress address, const RootKeys& rootKeys, uint16_t maxAttMtuSize, uint8_t txPowerLevel, const GapService& gapService, infra::CreatorBase<services::BondStorageSynchronizer, void()>& bondStorageSynchronizerCreator, uint32_t* bleBondsStorage, services::Tracer& tracer);
TracingGapPeripheralSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration, services::Tracer& tracer);

protected:
// Implementation of GapPeripheralSt
Expand Down

0 comments on commit 0c8e2e4

Please sign in to comment.