diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp index 3fdda351..b46c5ca6 100644 --- a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp @@ -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& 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::NotifyObservers([](auto& observer) { observer.StateChanged(services::GapState::standby); }); + infra::Subject::NotifyObservers([](auto& observer) + { + observer.StateChanged(services::GapState::standby); + }); } void GapCentralSt::Connect(hal::MacAddress macAddress, services::GapDeviceAddressType addressType) diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.hpp b/hal_st/middlewares/ble_middleware/GapCentralSt.hpp index d9fa9608..e8999507 100644 --- a/hal_st/middlewares/ble_middleware/GapCentralSt.hpp +++ b/hal_st/middlewares/ble_middleware/GapCentralSt.hpp @@ -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& 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; diff --git a/hal_st/middlewares/ble_middleware/GapPeripheralSt.cpp b/hal_st/middlewares/ble_middleware/GapPeripheralSt.cpp index ca2a381a..e687a544 100644 --- a/hal_st/middlewares/ble_middleware/GapPeripheralSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapPeripheralSt.cpp @@ -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& 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 diff --git a/hal_st/middlewares/ble_middleware/GapPeripheralSt.hpp b/hal_st/middlewares/ble_middleware/GapPeripheralSt.hpp index 57bae2e6..03bf397b 100644 --- a/hal_st/middlewares/ble_middleware/GapPeripheralSt.hpp +++ b/hal_st/middlewares/ble_middleware/GapPeripheralSt.hpp @@ -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& bondStorageSynchronizerCreator, uint32_t* bleBondsStorage); + GapPeripheralSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration); // Implementation of GapPeripheral services::GapAddress GetAddress() const override; diff --git a/hal_st/middlewares/ble_middleware/GapSt.cpp b/hal_st/middlewares/ble_middleware/GapSt.cpp index c6139f9a..aee55cef 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapSt.cpp @@ -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 @@ -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{ @@ -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& 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(&bleBondsStorage), + reinterpret_cast(&bleBondStorage.bleBondsStorage), 0, // ThreadNvmRamAddress static_cast(LL_DBGMCU_GetRevisionID()), static_cast(LL_DBGMCU_GetDeviceID()), @@ -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 @@ -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(); } diff --git a/hal_st/middlewares/ble_middleware/GapSt.hpp b/hal_st/middlewares/ble_middleware/GapSt.hpp index e7ab62ba..f27791ca 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.hpp +++ b/hal_st/middlewares/ble_middleware/GapSt.hpp @@ -8,6 +8,7 @@ #include "services/ble/BondStorageSynchronizer.hpp" #include "services/ble/Gap.hpp" #include "services/ble/Gatt.hpp" +#include "shci.h" namespace hal { @@ -30,6 +31,28 @@ namespace hal std::array 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& bondStorageSynchronizerCreator; + uint32_t& bleBondsStorage; + }; + // Implementation of AttMtuExchange uint16_t EffectiveMaxAttMtuSize() const override; @@ -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& bondStorageSynchronizerCreator, uint32_t& bleBondsStorage); + GapSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration); virtual void HandleHciDisconnectEvent(hci_event_pckt& eventPacket); @@ -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> bondStorageSynchronizer; }; diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp index 98c6a156..51a404ad 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp @@ -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& 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) {} diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp index ae97036c..fce179dd 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp @@ -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& 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; diff --git a/hal_st/middlewares/ble_middleware/TracingGapPeripheralSt.cpp b/hal_st/middlewares/ble_middleware/TracingGapPeripheralSt.cpp index 1e8c8cbe..f1d9b5fc 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapPeripheralSt.cpp +++ b/hal_st/middlewares/ble_middleware/TracingGapPeripheralSt.cpp @@ -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& 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) {} diff --git a/hal_st/middlewares/ble_middleware/TracingGapPeripheralSt.hpp b/hal_st/middlewares/ble_middleware/TracingGapPeripheralSt.hpp index 2eecc7f1..ddb01ad3 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapPeripheralSt.hpp +++ b/hal_st/middlewares/ble_middleware/TracingGapPeripheralSt.hpp @@ -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& bondStorageSynchronizerCreator, uint32_t* bleBondsStorage, services::Tracer& tracer); + TracingGapPeripheralSt(hal::HciEventSource& hciEventSource, BleBondStorage bleBondStorage, const Configuration& configuration, services::Tracer& tracer); protected: // Implementation of GapPeripheralSt