From a6e1702191bae0eefe39260d161fb425bc8c530c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Romanin=20de=20Lazaro?= Date: Tue, 10 Dec 2024 16:35:14 +0000 Subject: [PATCH 1/6] Add support to return resolve MAC address Return the resolved MAC address instead of the random one --- hal_st/middlewares/ble_middleware/GapSt.cpp | 7 +++++++ hal_st/middlewares/ble_middleware/GapSt.hpp | 1 + hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp | 6 ++++++ hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp | 1 + 4 files changed, 15 insertions(+) diff --git a/hal_st/middlewares/ble_middleware/GapSt.cpp b/hal_st/middlewares/ble_middleware/GapSt.cpp index 55a72a71..174f93d0 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapSt.cpp @@ -151,6 +151,13 @@ namespace hal std::abort(); } + hal::MacAddress GapSt::ResolveDeviceAddress(hal::MacAddress deviceAddress) const + { + hal::MacAddress address = connectionContext.peerAddress; + aci_gap_resolve_private_addr(deviceAddress.data(), address.data()); + return address; + } + void GapSt::HandleHciDisconnectEvent(hci_event_pckt& eventPacket) { auto disconnectionCompleteEvent = *reinterpret_cast(eventPacket.data); diff --git a/hal_st/middlewares/ble_middleware/GapSt.hpp b/hal_st/middlewares/ble_middleware/GapSt.hpp index 7a41b414..9e359737 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.hpp +++ b/hal_st/middlewares/ble_middleware/GapSt.hpp @@ -52,6 +52,7 @@ namespace hal void SetIoCapabilities(services::GapPairing::IoCapabilities caps) override; void AuthenticateWithPasskey(uint32_t passkey) override; void NumericComparisonConfirm(bool accept) override; + hal::MacAddress ResolveDeviceAddress(hal::MacAddress deviceAddress) const override; protected: GapSt(hal::HciEventSource& hciEventSource, services::BondStorageSynchronizer& bondStorageSynchronizer, const Configuration& configuration); diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp index e3a5584b..48a13468 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp @@ -99,6 +99,12 @@ namespace hal GapCentralSt::NumericComparisonConfirm(accept); } + hal::MacAddress TracingGapCentralSt::ResolveDeviceAddress(hal::MacAddress deviceAddress) const + { + tracer.Trace() << "TracingGapCentralSt::ResolveDeviceAddress"; + return GapCentralSt::ResolveDeviceAddress(deviceAddress); + } + void TracingGapCentralSt::HandleHciDisconnectEvent(hci_event_pckt& eventPacket) { const auto disconnectEvt = reinterpret_cast(eventPacket.data); diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp index 301bef00..c4ddf7f3 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp @@ -31,6 +31,7 @@ namespace hal void SetIoCapabilities(services::GapPairing::IoCapabilities caps) override; void AuthenticateWithPasskey(uint32_t passkey) override; void NumericComparisonConfirm(bool accept) override; + hal::MacAddress ResolveDeviceAddress(hal::MacAddress deviceAddress) const override; protected: // Implementation of GapCentralSt From b3d7ea696a305974a62266518d757772db1bbf57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Romanin=20de=20Lazaro?= Date: Thu, 19 Dec 2024 14:27:59 +0000 Subject: [PATCH 2/6] Changes after sync up with Oguzcan --- .../ble_middleware/GapCentralSt.cpp | 8 +++++++ .../ble_middleware/GapCentralSt.hpp | 1 + hal_st/middlewares/ble_middleware/GapSt.cpp | 21 +++++++------------ hal_st/middlewares/ble_middleware/GapSt.hpp | 20 ++++++++++++------ .../ble_middleware/TracingGapCentralSt.cpp | 20 ++++++++++++------ .../ble_middleware/TracingGapCentralSt.hpp | 3 ++- 6 files changed, 46 insertions(+), 27 deletions(-) diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp index 7cf6ba59..ce86dcb4 100644 --- a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp @@ -113,6 +113,14 @@ namespace hal aci_gap_terminate_gap_proc(GAP_GENERAL_DISCOVERY_PROC); } + infra::Optional GapCentralSt::ResolveDeviceAddress(hal::MacAddress deviceAddress) const + { + hal::MacAddress address; + if (aci_gap_resolve_private_addr(deviceAddress.data(), address.data()) != BLE_STATUS_SUCCESS) + return infra::none; + return infra::MakeOptional(address); + } + void GapCentralSt::AllowPairing(bool) {} diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.hpp b/hal_st/middlewares/ble_middleware/GapCentralSt.hpp index 18bb26ef..84715ae1 100644 --- a/hal_st/middlewares/ble_middleware/GapCentralSt.hpp +++ b/hal_st/middlewares/ble_middleware/GapCentralSt.hpp @@ -22,6 +22,7 @@ namespace hal void SetAddress(hal::MacAddress macAddress, services::GapDeviceAddressType addressType) override; void StartDeviceDiscovery() override; void StopDeviceDiscovery() override; + infra::Optional ResolveDeviceAddress(hal::MacAddress deviceAddress) const override; // Implementation of GapPairing void AllowPairing(bool allow) override; diff --git a/hal_st/middlewares/ble_middleware/GapSt.cpp b/hal_st/middlewares/ble_middleware/GapSt.cpp index 174f93d0..a3f0df66 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapSt.cpp @@ -1,6 +1,8 @@ #include "hal_st/middlewares/ble_middleware/GapSt.hpp" #include "ble_gap_aci.h" #include "services/ble/Gap.hpp" +#include +#include namespace hal { @@ -91,6 +93,11 @@ namespace hal return numberOfBondedAddress; } + bool GapSt::IsDeviceBounded(MacAddress deviceAddress) const + { + return (aci_gap_is_device_bonded(static_cast(PeerAddressType::PUBLIC), deviceAddress.data()) == BLE_STATUS_SUCCESS); + } + void GapSt::Pair() { really_assert(connectionContext.connectionHandle != GapSt::invalidConnection); @@ -151,13 +158,6 @@ namespace hal std::abort(); } - hal::MacAddress GapSt::ResolveDeviceAddress(hal::MacAddress deviceAddress) const - { - hal::MacAddress address = connectionContext.peerAddress; - aci_gap_resolve_private_addr(deviceAddress.data(), address.data()); - return address; - } - void GapSt::HandleHciDisconnectEvent(hci_event_pckt& eventPacket) { auto disconnectionCompleteEvent = *reinterpret_cast(eventPacket.data); @@ -316,13 +316,6 @@ namespace hal { static constexpr auto deducePeerAddressType = [](auto peerAddressType) { - enum class PeerAddressType : uint8_t - { - PUBLIC, - RANDOM, - RESOLVED_PUBLIC_IDENTITY, - RESOLVED_RANDOM_STATIC_IDENTITY - }; switch (static_cast(peerAddressType)) { diff --git a/hal_st/middlewares/ble_middleware/GapSt.hpp b/hal_st/middlewares/ble_middleware/GapSt.hpp index 9e359737..b2b02ef5 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.hpp +++ b/hal_st/middlewares/ble_middleware/GapSt.hpp @@ -14,7 +14,7 @@ namespace hal : public services::AttMtuExchange , public services::GapBonding , public services::GapPairing - , private hal::HciEventSink + , private HciEventSink { public: struct GapService @@ -31,7 +31,7 @@ namespace hal struct Configuration { - const hal::MacAddress& address; + const MacAddress& address; const GapService& gapService; const RootKeys& rootKeys; uint8_t txPowerLevel; @@ -45,6 +45,7 @@ namespace hal void RemoveOldestBond() override; std::size_t GetMaxNumberOfBonds() const override; std::size_t GetNumberOfBonds() const override; + bool IsDeviceBounded(MacAddress deviceAddress) const override; // Implementation of GapPairing void Pair() override; @@ -52,10 +53,9 @@ namespace hal void SetIoCapabilities(services::GapPairing::IoCapabilities caps) override; void AuthenticateWithPasskey(uint32_t passkey) override; void NumericComparisonConfirm(bool accept) override; - hal::MacAddress ResolveDeviceAddress(hal::MacAddress deviceAddress) const override; protected: - GapSt(hal::HciEventSource& hciEventSource, services::BondStorageSynchronizer& bondStorageSynchronizer, const Configuration& configuration); + GapSt(HciEventSource& hciEventSource, services::BondStorageSynchronizer& bondStorageSynchronizer, const Configuration& configuration); virtual void HandleHciDisconnectEvent(hci_event_pckt& eventPacket); @@ -73,7 +73,7 @@ namespace hal virtual void HandleL2capConnectionUpdateRequestEvent(evt_blecore_aci* vendorEvent){}; virtual void HandleMtuExchangeResponseEvent(evt_blecore_aci* vendorEvent); - void SetAddress(const hal::MacAddress& address, services::GapDeviceAddressType addressType); + void SetAddress(const MacAddress& address, services::GapDeviceAddressType addressType); private: // Implementation of HciEventSink @@ -86,11 +86,19 @@ namespace hal void UpdateNrBonds(); protected: + enum class PeerAddressType : uint8_t + { + PUBLIC, + RANDOM, + RESOLVED_PUBLIC_IDENTITY, + RESOLVED_RANDOM_STATIC_IDENTITY + }; + struct ConnectionContext { uint16_t connectionHandle; uint8_t peerAddressType; - hal::MacAddress peerAddress; + MacAddress peerAddress; }; ConnectionContext connectionContext; diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp index 48a13468..6f2b3a96 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp @@ -45,6 +45,13 @@ namespace hal GapCentralSt::StopDeviceDiscovery(); } + infra::Optional TracingGapCentralSt::ResolveDeviceAddress(hal::MacAddress deviceAddress) const + { + auto resolvedMac = GapCentralSt::ResolveDeviceAddress(deviceAddress); + tracer.Trace() << "TracingGapCentralSt::ResolveDeviceAddress, MAC address: " << infra::AsMacAddress(deviceAddress) << " resolved MAC address " << infra::AsMacAddress(*resolvedMac); + return resolvedMac; + } + void TracingGapCentralSt::RemoveAllBonds() { tracer.Trace() << "TracingGapCentralSt::RemoveAllBonds"; @@ -69,6 +76,13 @@ namespace hal return GapCentralSt::GetNumberOfBonds(); } + bool TracingGapCentralSt::IsDeviceBounded(hal::MacAddress deviceAddress) const + { + auto ret = GapCentralSt::IsDeviceBounded(deviceAddress); + tracer.Trace() << "TracingGapCentralSt::IsDeviceBounded " << infra::AsMacAddress(deviceAddress) << " -> " << (ret ? "true" : "false"); + return ret; + } + void TracingGapCentralSt::Pair() { tracer.Trace() << "TracingGapCentralSt::Pair"; @@ -99,12 +113,6 @@ namespace hal GapCentralSt::NumericComparisonConfirm(accept); } - hal::MacAddress TracingGapCentralSt::ResolveDeviceAddress(hal::MacAddress deviceAddress) const - { - tracer.Trace() << "TracingGapCentralSt::ResolveDeviceAddress"; - return GapCentralSt::ResolveDeviceAddress(deviceAddress); - } - void TracingGapCentralSt::HandleHciDisconnectEvent(hci_event_pckt& eventPacket) { const auto disconnectEvt = reinterpret_cast(eventPacket.data); diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp index c4ddf7f3..f6268342 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp @@ -18,12 +18,14 @@ namespace hal void SetAddress(hal::MacAddress macAddress, services::GapDeviceAddressType addressType) override; void StartDeviceDiscovery() override; void StopDeviceDiscovery() override; + infra::Optional ResolveDeviceAddress(hal::MacAddress deviceAddress) const override; // Implementation of GapBonding void RemoveAllBonds() override; void RemoveOldestBond() override; std::size_t GetMaxNumberOfBonds() const override; std::size_t GetNumberOfBonds() const override; + bool IsDeviceBounded(hal::MacAddress deviceAddress) const override; // Implementation of GapPairing void Pair() override; @@ -31,7 +33,6 @@ namespace hal void SetIoCapabilities(services::GapPairing::IoCapabilities caps) override; void AuthenticateWithPasskey(uint32_t passkey) override; void NumericComparisonConfirm(bool accept) override; - hal::MacAddress ResolveDeviceAddress(hal::MacAddress deviceAddress) const override; protected: // Implementation of GapCentralSt From ac4c21981db29cd58cda59ab4460e39dd2114799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Romanin=20de=20Lazaro?= Date: Thu, 19 Dec 2024 14:28:50 +0000 Subject: [PATCH 3/6] New solution --- CMakeLists.txt | 2 +- .../ble_middleware/GapCentralSt.cpp | 18 +++++++++++++++--- hal_st/middlewares/ble_middleware/GapSt.cpp | 18 ++++++++---------- hal_st/middlewares/ble_middleware/GapSt.hpp | 10 +++++----- .../ble_middleware/TracingGapCentralSt.cpp | 12 ++++++++---- .../ble_middleware/TracingGapCentralSt.hpp | 2 +- 6 files changed, 38 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 93ae4266..f4afce08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,7 @@ if (HALST_STANDALONE) FetchContent_Declare( emil GIT_REPOSITORY https://github.com/philips-software/amp-embedded-infra-lib.git - GIT_TAG 6aa8866fa29ea9b503ba6301da6390281ed2a1d1 # unreleased + GIT_TAG a99e673b5fdf4ce471a8cdbb29443f06b9bac6fa # unreleased ) FetchContent_MakeAvailable(emil) diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp index ce86dcb4..fc2430bd 100644 --- a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp @@ -32,9 +32,21 @@ namespace hal return static_cast(eventType); } - services::GapAdvertisingEventAddressType ToAdvertisingAddressType(uint8_t addressType) + services::GapAdvertisingEventAddressType ToAdvertisingAddressType(uint8_t addressType, const uint8_t address[6]) { - return static_cast(addressType); + if (addressType == static_cast(services::GapDeviceAddressType::publicAddress)) + return services::GapAdvertisingEventAddressType::publicDeviceAddress; + else if (addressType == static_cast(services::GapDeviceAddressType::randomAddress)) + { + auto mode = address[5] >> 6; // Address in EUI-48 format + if (mode == 0x3) + return services::GapAdvertisingEventAddressType::randomDeviceAddress; + else if (mode == 0x01) + return services::GapAdvertisingEventAddressType::publicIdentityAddress; + else if (mode == 0x00) + return services::GapAdvertisingEventAddressType::randomIdentityAddress; + } + return services::GapAdvertisingEventAddressType::randomDeviceAddress; } bool IsTxDataLengthConfigured(const hci_le_data_length_change_event_rp0& dataLengthChangeEvent) @@ -292,7 +304,7 @@ namespace hal auto advertisementData = const_cast(&advertisingReport.Length_Data) + 1; std::copy_n(std::begin(advertisingReport.Address), discoveredDevice.address.size(), std::begin(discoveredDevice.address)); discoveredDevice.eventType = ToAdvertisingEventType(advertisingReport.Event_Type); - discoveredDevice.addressType = ToAdvertisingAddressType(advertisingReport.Address_Type); + discoveredDevice.addressType = ToAdvertisingAddressType(advertisingReport.Address_Type, advertisingReport.Address); discoveredDevice.data.assign(advertisementData, advertisementData + advertisingReport.Length_Data); discoveredDevice.rssi = static_cast(*const_cast(advertisementData + advertisingReport.Length_Data)); diff --git a/hal_st/middlewares/ble_middleware/GapSt.cpp b/hal_st/middlewares/ble_middleware/GapSt.cpp index a3f0df66..1525ec06 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapSt.cpp @@ -1,8 +1,6 @@ #include "hal_st/middlewares/ble_middleware/GapSt.hpp" #include "ble_gap_aci.h" #include "services/ble/Gap.hpp" -#include -#include namespace hal { @@ -93,9 +91,9 @@ namespace hal return numberOfBondedAddress; } - bool GapSt::IsDeviceBounded(MacAddress deviceAddress) const + bool GapSt::IsDeviceBonded(MacAddress deviceAddress) const { - return (aci_gap_is_device_bonded(static_cast(PeerAddressType::PUBLIC), deviceAddress.data()) == BLE_STATUS_SUCCESS); + return aci_gap_is_device_bonded(static_cast(PeerAddressType::publicStatic), deviceAddress.data()) == BLE_STATUS_SUCCESS; } void GapSt::Pair() @@ -319,16 +317,16 @@ namespace hal switch (static_cast(peerAddressType)) { - case PeerAddressType::PUBLIC: - case PeerAddressType::RANDOM: + case PeerAddressType::publicStatic: + case PeerAddressType::randomStatic: return peerAddressType; - case PeerAddressType::RESOLVED_PUBLIC_IDENTITY: - return infra::enum_cast(PeerAddressType::PUBLIC); + case PeerAddressType::resolvablePrivate: + return infra::enum_cast(PeerAddressType::publicStatic); - case PeerAddressType::RESOLVED_RANDOM_STATIC_IDENTITY: + case PeerAddressType::nonResolvablePrivate: default: - return infra::enum_cast(PeerAddressType::RANDOM); + return infra::enum_cast(PeerAddressType::randomStatic); } }; diff --git a/hal_st/middlewares/ble_middleware/GapSt.hpp b/hal_st/middlewares/ble_middleware/GapSt.hpp index b2b02ef5..5244e53a 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.hpp +++ b/hal_st/middlewares/ble_middleware/GapSt.hpp @@ -45,7 +45,7 @@ namespace hal void RemoveOldestBond() override; std::size_t GetMaxNumberOfBonds() const override; std::size_t GetNumberOfBonds() const override; - bool IsDeviceBounded(MacAddress deviceAddress) const override; + bool IsDeviceBonded(MacAddress deviceAddress) const override; // Implementation of GapPairing void Pair() override; @@ -88,10 +88,10 @@ namespace hal protected: enum class PeerAddressType : uint8_t { - PUBLIC, - RANDOM, - RESOLVED_PUBLIC_IDENTITY, - RESOLVED_RANDOM_STATIC_IDENTITY + publicStatic, + randomStatic, + resolvablePrivate, + nonResolvablePrivate }; struct ConnectionContext diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp index 6f2b3a96..c3266c03 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp @@ -48,7 +48,11 @@ namespace hal infra::Optional TracingGapCentralSt::ResolveDeviceAddress(hal::MacAddress deviceAddress) const { auto resolvedMac = GapCentralSt::ResolveDeviceAddress(deviceAddress); - tracer.Trace() << "TracingGapCentralSt::ResolveDeviceAddress, MAC address: " << infra::AsMacAddress(deviceAddress) << " resolved MAC address " << infra::AsMacAddress(*resolvedMac); + tracer.Trace() << "TracingGapCentralSt::ResolveDeviceAddress, MAC address: " << infra::AsMacAddress(deviceAddress); + if (resolvedMac) + tracer.Continue() << ", resolved MAC address " << infra::AsMacAddress(*resolvedMac); + else + tracer.Continue() << ", could not resolve MAC address"; return resolvedMac; } @@ -76,10 +80,10 @@ namespace hal return GapCentralSt::GetNumberOfBonds(); } - bool TracingGapCentralSt::IsDeviceBounded(hal::MacAddress deviceAddress) const + bool TracingGapCentralSt::IsDeviceBonded(hal::MacAddress deviceAddress) const { - auto ret = GapCentralSt::IsDeviceBounded(deviceAddress); - tracer.Trace() << "TracingGapCentralSt::IsDeviceBounded " << infra::AsMacAddress(deviceAddress) << " -> " << (ret ? "true" : "false"); + auto ret = GapCentralSt::IsDeviceBonded(deviceAddress); + tracer.Trace() << "TracingGapCentralSt::IsDeviceBonded " << infra::AsMacAddress(deviceAddress) << " -> " << (ret ? "true" : "false"); return ret; } diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp index f6268342..a3b8193c 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp @@ -25,7 +25,7 @@ namespace hal void RemoveOldestBond() override; std::size_t GetMaxNumberOfBonds() const override; std::size_t GetNumberOfBonds() const override; - bool IsDeviceBounded(hal::MacAddress deviceAddress) const override; + bool IsDeviceBonded(hal::MacAddress deviceAddress) const override; // Implementation of GapPairing void Pair() override; From e3c23dfd6656caef423fdede0caf9e2d75951542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Romanin=20de=20Lazaro?= Date: Tue, 14 Jan 2025 10:24:17 +0000 Subject: [PATCH 4/6] fix code review --- hal_st/middlewares/ble_middleware/GapCentralSt.cpp | 8 ++++---- hal_st/middlewares/ble_middleware/GapCentralSt.hpp | 2 +- hal_st/middlewares/ble_middleware/GapSt.cpp | 4 ++-- hal_st/middlewares/ble_middleware/GapSt.hpp | 2 +- .../ble_middleware/TracingGapCentralSt.cpp | 12 ++++++------ .../ble_middleware/TracingGapCentralSt.hpp | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp index fc2430bd..3d78af6a 100644 --- a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp @@ -125,12 +125,12 @@ namespace hal aci_gap_terminate_gap_proc(GAP_GENERAL_DISCOVERY_PROC); } - infra::Optional GapCentralSt::ResolveDeviceAddress(hal::MacAddress deviceAddress) const + infra::Optional GapCentralSt::ResolvePrivateAddress(hal::MacAddress address) const { - hal::MacAddress address; - if (aci_gap_resolve_private_addr(deviceAddress.data(), address.data()) != BLE_STATUS_SUCCESS) + hal::MacAddress identityAddress; + if (aci_gap_resolve_private_addr(address.data(), identityAddress.data()) != BLE_STATUS_SUCCESS) return infra::none; - return infra::MakeOptional(address); + return infra::MakeOptional(identityAddress); } void GapCentralSt::AllowPairing(bool) diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.hpp b/hal_st/middlewares/ble_middleware/GapCentralSt.hpp index 84715ae1..d801e470 100644 --- a/hal_st/middlewares/ble_middleware/GapCentralSt.hpp +++ b/hal_st/middlewares/ble_middleware/GapCentralSt.hpp @@ -22,7 +22,7 @@ namespace hal void SetAddress(hal::MacAddress macAddress, services::GapDeviceAddressType addressType) override; void StartDeviceDiscovery() override; void StopDeviceDiscovery() override; - infra::Optional ResolveDeviceAddress(hal::MacAddress deviceAddress) const override; + infra::Optional ResolvePrivateAddress(hal::MacAddress address) const override; // Implementation of GapPairing void AllowPairing(bool allow) override; diff --git a/hal_st/middlewares/ble_middleware/GapSt.cpp b/hal_st/middlewares/ble_middleware/GapSt.cpp index 1525ec06..ead75d9d 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapSt.cpp @@ -91,9 +91,9 @@ namespace hal return numberOfBondedAddress; } - bool GapSt::IsDeviceBonded(MacAddress deviceAddress) const + bool GapSt::IsDeviceBonded(MacAddress identityAddress) const { - return aci_gap_is_device_bonded(static_cast(PeerAddressType::publicStatic), deviceAddress.data()) == BLE_STATUS_SUCCESS; + return aci_gap_is_device_bonded(static_cast(PeerAddressType::publicStatic), identityAddress.data()) == BLE_STATUS_SUCCESS; } void GapSt::Pair() diff --git a/hal_st/middlewares/ble_middleware/GapSt.hpp b/hal_st/middlewares/ble_middleware/GapSt.hpp index 5244e53a..e2f4ac3e 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.hpp +++ b/hal_st/middlewares/ble_middleware/GapSt.hpp @@ -45,7 +45,7 @@ namespace hal void RemoveOldestBond() override; std::size_t GetMaxNumberOfBonds() const override; std::size_t GetNumberOfBonds() const override; - bool IsDeviceBonded(MacAddress deviceAddress) const override; + bool IsDeviceBonded(MacAddress identityAddress) const override; // Implementation of GapPairing void Pair() override; diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp index c3266c03..8a907630 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp @@ -45,10 +45,10 @@ namespace hal GapCentralSt::StopDeviceDiscovery(); } - infra::Optional TracingGapCentralSt::ResolveDeviceAddress(hal::MacAddress deviceAddress) const + infra::Optional TracingGapCentralSt::ResolvePrivateAddress(hal::MacAddress address) const { - auto resolvedMac = GapCentralSt::ResolveDeviceAddress(deviceAddress); - tracer.Trace() << "TracingGapCentralSt::ResolveDeviceAddress, MAC address: " << infra::AsMacAddress(deviceAddress); + auto resolvedMac = GapCentralSt::ResolvePrivateAddress(address); + tracer.Trace() << "TracingGapCentralSt::ResolvePrivateAddress, MAC address: " << infra::AsMacAddress(address); if (resolvedMac) tracer.Continue() << ", resolved MAC address " << infra::AsMacAddress(*resolvedMac); else @@ -80,10 +80,10 @@ namespace hal return GapCentralSt::GetNumberOfBonds(); } - bool TracingGapCentralSt::IsDeviceBonded(hal::MacAddress deviceAddress) const + bool TracingGapCentralSt::IsDeviceBonded(hal::MacAddress identityAddress) const { - auto ret = GapCentralSt::IsDeviceBonded(deviceAddress); - tracer.Trace() << "TracingGapCentralSt::IsDeviceBonded " << infra::AsMacAddress(deviceAddress) << " -> " << (ret ? "true" : "false"); + auto ret = GapCentralSt::IsDeviceBonded(identityAddress); + tracer.Trace() << "TracingGapCentralSt::IsDeviceBonded " << infra::AsMacAddress(identityAddress) << " -> " << (ret ? "true" : "false"); return ret; } diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp index a3b8193c..35ff5ee8 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp @@ -18,14 +18,14 @@ namespace hal void SetAddress(hal::MacAddress macAddress, services::GapDeviceAddressType addressType) override; void StartDeviceDiscovery() override; void StopDeviceDiscovery() override; - infra::Optional ResolveDeviceAddress(hal::MacAddress deviceAddress) const override; + infra::Optional ResolvePrivateAddress(hal::MacAddress address) const override; // Implementation of GapBonding void RemoveAllBonds() override; void RemoveOldestBond() override; std::size_t GetMaxNumberOfBonds() const override; std::size_t GetNumberOfBonds() const override; - bool IsDeviceBonded(hal::MacAddress deviceAddress) const override; + bool IsDeviceBonded(hal::MacAddress identityAddress) const override; // Implementation of GapPairing void Pair() override; From fcce870e44d6a13b78b9e5be7739fc386b9eb56e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Romanin=20de=20Lazaro?= Date: Thu, 16 Jan 2025 14:16:37 +0000 Subject: [PATCH 5/6] new solution --- .../ble_middleware/GapCentralSt.cpp | 22 +++---------- .../ble_middleware/GapPeripheralSt.cpp | 8 ++--- hal_st/middlewares/ble_middleware/GapSt.cpp | 32 ++++--------------- hal_st/middlewares/ble_middleware/GapSt.hpp | 14 ++------ .../ble_middleware/TracingGapCentralSt.cpp | 6 ++-- .../ble_middleware/TracingGapCentralSt.hpp | 2 +- 6 files changed, 23 insertions(+), 61 deletions(-) diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp index 3d78af6a..9c2cfc8e 100644 --- a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp @@ -31,22 +31,10 @@ namespace hal { return static_cast(eventType); } - - services::GapAdvertisingEventAddressType ToAdvertisingAddressType(uint8_t addressType, const uint8_t address[6]) + + services::GapDeviceAddressType ToAdvertisingAddressType(uint8_t addressType) { - if (addressType == static_cast(services::GapDeviceAddressType::publicAddress)) - return services::GapAdvertisingEventAddressType::publicDeviceAddress; - else if (addressType == static_cast(services::GapDeviceAddressType::randomAddress)) - { - auto mode = address[5] >> 6; // Address in EUI-48 format - if (mode == 0x3) - return services::GapAdvertisingEventAddressType::randomDeviceAddress; - else if (mode == 0x01) - return services::GapAdvertisingEventAddressType::publicIdentityAddress; - else if (mode == 0x00) - return services::GapAdvertisingEventAddressType::randomIdentityAddress; - } - return services::GapAdvertisingEventAddressType::randomDeviceAddress; + return static_cast(addressType); } bool IsTxDataLengthConfigured(const hci_le_data_length_change_event_rp0& dataLengthChangeEvent) @@ -300,11 +288,11 @@ namespace hal void GapCentralSt::HandleAdvertisingReport(const Advertising_Report_t& advertisingReport) { services::GapAdvertisingReport discoveredDevice; - + auto advertisementData = const_cast(&advertisingReport.Length_Data) + 1; std::copy_n(std::begin(advertisingReport.Address), discoveredDevice.address.size(), std::begin(discoveredDevice.address)); discoveredDevice.eventType = ToAdvertisingEventType(advertisingReport.Event_Type); - discoveredDevice.addressType = ToAdvertisingAddressType(advertisingReport.Address_Type, advertisingReport.Address); + discoveredDevice.addressType = ToAdvertisingAddressType(advertisingReport.Address_Type); discoveredDevice.data.assign(advertisementData, advertisementData + advertisingReport.Length_Data); discoveredDevice.rssi = static_cast(*const_cast(advertisementData + advertisingReport.Length_Data)); diff --git a/hal_st/middlewares/ble_middleware/GapPeripheralSt.cpp b/hal_st/middlewares/ble_middleware/GapPeripheralSt.cpp index 56a66623..511bf7a3 100644 --- a/hal_st/middlewares/ble_middleware/GapPeripheralSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapPeripheralSt.cpp @@ -21,9 +21,9 @@ namespace hal { services::GapAddress address; /* Use last peer addres to get current RPA */ - [[maybe_unused]] auto status = hci_le_read_local_resolvable_address(connectionContext.peerAddressType, connectionContext.peerAddress.data(), address.address.data()); + [[maybe_unused]] auto status = hci_le_read_local_resolvable_address(static_cast(connectionContext.peerAddressType), connectionContext.peerAddress.data(), address.address.data()); - address.type = services::GapDeviceAddressType::publicAddress; + address.type = services::GapDeviceAddressType::randomAddress; assert(status == BLE_STATUS_SUCCESS); @@ -133,14 +133,14 @@ namespace hal aci_gap_add_devices_to_resolving_list(1, &dummyPeer, 1); std::copy(std::begin(dummyPeer.Peer_Identity_Address), std::end(dummyPeer.Peer_Identity_Address), connectionContext.peerAddress.begin()); - connectionContext.peerAddressType = dummyPeer.Peer_Identity_Address_Type; + connectionContext.peerAddressType = static_cast(dummyPeer.Peer_Identity_Address_Type); } else { aci_gap_add_devices_to_resolving_list(numberOfBondedAddress, reinterpret_cast(bondedDevices.begin()), 1); std::copy(std::begin(bondedDevices[numberOfBondedAddress - 1].Address), std::end(bondedDevices[numberOfBondedAddress - 1].Address), connectionContext.peerAddress.begin()); - connectionContext.peerAddressType = bondedDevices[numberOfBondedAddress - 1].Address_Type; + connectionContext.peerAddressType = static_cast(bondedDevices[numberOfBondedAddress - 1].Address_Type); for (uint8_t i = 0; i < numberOfBondedAddress; i++) hci_le_set_privacy_mode(bondedDevices[i].Address_Type, bondedDevices[i].Address, HCI_PRIV_MODE_DEVICE); diff --git a/hal_st/middlewares/ble_middleware/GapSt.cpp b/hal_st/middlewares/ble_middleware/GapSt.cpp index ead75d9d..f6a56365 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapSt.cpp @@ -91,9 +91,9 @@ namespace hal return numberOfBondedAddress; } - bool GapSt::IsDeviceBonded(MacAddress identityAddress) const + bool GapSt::IsDeviceBonded(MacAddress address, services::GapDeviceAddressType addressType) const { - return aci_gap_is_device_bonded(static_cast(PeerAddressType::publicStatic), identityAddress.data()) == BLE_STATUS_SUCCESS; + return aci_gap_is_device_bonded(static_cast(addressType), address.data()) == BLE_STATUS_SUCCESS; } void GapSt::Pair() @@ -171,7 +171,7 @@ namespace hal auto connectionCompleteEvent = *reinterpret_cast(metaEvent->data); if (connectionCompleteEvent.Status == BLE_STATUS_SUCCESS) - SetConnectionContext(connectionCompleteEvent.Connection_Handle, connectionCompleteEvent.Peer_Address_Type, &connectionCompleteEvent.Peer_Address[0]); + SetConnectionContext(connectionCompleteEvent.Connection_Handle, static_cast(connectionCompleteEvent.Peer_Address_Type), &connectionCompleteEvent.Peer_Address[0]); } void GapSt::HandleHciLeEnhancedConnectionCompleteEvent(evt_le_meta_event* metaEvent) @@ -179,7 +179,7 @@ namespace hal auto connectionCompleteEvt = *reinterpret_cast(metaEvent->data); if (connectionCompleteEvt.Status == BLE_STATUS_SUCCESS) - SetConnectionContext(connectionCompleteEvt.Connection_Handle, connectionCompleteEvt.Peer_Address_Type, &connectionCompleteEvt.Peer_Address[0]); + SetConnectionContext(connectionCompleteEvt.Connection_Handle, static_cast(connectionCompleteEvt.Peer_Address_Type), &connectionCompleteEvt.Peer_Address[0]); } void GapSt::HandleBondLostEvent(evt_blecore_aci* vendorEvent) @@ -206,7 +206,7 @@ namespace hal really_assert(pairingComplete->Connection_Handle == connectionContext.connectionHandle); - if (aci_gap_is_device_bonded(connectionContext.peerAddressType, connectionContext.peerAddress.data()) == BLE_STATUS_SUCCESS) + if (IsDeviceBonded(connectionContext.peerAddress, connectionContext.peerAddressType)) { hal::MacAddress address = connectionContext.peerAddress; aci_gap_resolve_private_addr(connectionContext.peerAddress.data(), address.data()); @@ -310,29 +310,11 @@ namespace hal } } - void GapSt::SetConnectionContext(uint16_t connectionHandle, uint8_t peerAddressType, uint8_t* peerAddress) + void GapSt::SetConnectionContext(uint16_t connectionHandle, services::GapDeviceAddressType peerAddressType, uint8_t* peerAddress) { - static constexpr auto deducePeerAddressType = [](auto peerAddressType) - { - - switch (static_cast(peerAddressType)) - { - case PeerAddressType::publicStatic: - case PeerAddressType::randomStatic: - return peerAddressType; - - case PeerAddressType::resolvablePrivate: - return infra::enum_cast(PeerAddressType::publicStatic); - - case PeerAddressType::nonResolvablePrivate: - default: - return infra::enum_cast(PeerAddressType::randomStatic); - } - }; - maxAttMtu = defaultMaxAttMtuSize; connectionContext.connectionHandle = connectionHandle; - connectionContext.peerAddressType = deducePeerAddressType(peerAddressType); + connectionContext.peerAddressType = peerAddressType; std::copy_n(peerAddress, connectionContext.peerAddress.size(), std::begin(connectionContext.peerAddress)); } diff --git a/hal_st/middlewares/ble_middleware/GapSt.hpp b/hal_st/middlewares/ble_middleware/GapSt.hpp index e2f4ac3e..701195c0 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.hpp +++ b/hal_st/middlewares/ble_middleware/GapSt.hpp @@ -45,7 +45,7 @@ namespace hal void RemoveOldestBond() override; std::size_t GetMaxNumberOfBonds() const override; std::size_t GetNumberOfBonds() const override; - bool IsDeviceBonded(MacAddress identityAddress) const override; + bool IsDeviceBonded(MacAddress address, services::GapDeviceAddressType addressType) const override; // Implementation of GapPairing void Pair() override; @@ -82,22 +82,14 @@ namespace hal void HandleHciLeMetaEvent(hci_event_pckt& eventPacket); void HandleHciVendorSpecificDebugEvent(hci_event_pckt& eventPacket); - void SetConnectionContext(uint16_t connectionHandle, uint8_t peerAddressType, uint8_t* peerAddress); + void SetConnectionContext(uint16_t connectionHandle, services::GapDeviceAddressType peerAddressType, uint8_t* peerAddress); void UpdateNrBonds(); protected: - enum class PeerAddressType : uint8_t - { - publicStatic, - randomStatic, - resolvablePrivate, - nonResolvablePrivate - }; - struct ConnectionContext { uint16_t connectionHandle; - uint8_t peerAddressType; + services::GapDeviceAddressType peerAddressType; MacAddress peerAddress; }; diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp index 8a907630..0f4b30ea 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp @@ -80,10 +80,10 @@ namespace hal return GapCentralSt::GetNumberOfBonds(); } - bool TracingGapCentralSt::IsDeviceBonded(hal::MacAddress identityAddress) const + bool TracingGapCentralSt::IsDeviceBonded(hal::MacAddress address, services::GapDeviceAddressType addressType) const { - auto ret = GapCentralSt::IsDeviceBonded(identityAddress); - tracer.Trace() << "TracingGapCentralSt::IsDeviceBonded " << infra::AsMacAddress(identityAddress) << " -> " << (ret ? "true" : "false"); + auto ret = GapCentralSt::IsDeviceBonded(address, addressType); + tracer.Trace() << "TracingGapCentralSt::IsDeviceBonded " << infra::AsMacAddress(address) << " -> " << (ret ? "true" : "false"); return ret; } diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp index 35ff5ee8..3ee4afc2 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp @@ -25,7 +25,7 @@ namespace hal void RemoveOldestBond() override; std::size_t GetMaxNumberOfBonds() const override; std::size_t GetNumberOfBonds() const override; - bool IsDeviceBonded(hal::MacAddress identityAddress) const override; + bool IsDeviceBonded(hal::MacAddress address, services::GapDeviceAddressType addressType) const override; // Implementation of GapPairing void Pair() override; From 62b326ca86ede147de89bfd0487a5eb6c1b91c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Romanin=20de=20Lazaro?= <162123637+cassio-lazaro@users.noreply.github.com> Date: Wed, 5 Feb 2025 18:11:01 +0100 Subject: [PATCH 6/6] Update CMakeLists.txt --- CMakeLists.txt | 2 +- hal_st/middlewares/ble_middleware/GapCentralSt.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f4afce08..b873d8af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,7 @@ if (HALST_STANDALONE) FetchContent_Declare( emil GIT_REPOSITORY https://github.com/philips-software/amp-embedded-infra-lib.git - GIT_TAG a99e673b5fdf4ce471a8cdbb29443f06b9bac6fa # unreleased + GIT_TAG 5ace85bd4f163b4c989f0d3f07f96d0571e40011 # unreleased ) FetchContent_MakeAvailable(emil) diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp index 9c2cfc8e..ecf26429 100644 --- a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp @@ -31,7 +31,7 @@ namespace hal { return static_cast(eventType); } - + services::GapDeviceAddressType ToAdvertisingAddressType(uint8_t addressType) { return static_cast(addressType); @@ -288,7 +288,7 @@ namespace hal void GapCentralSt::HandleAdvertisingReport(const Advertising_Report_t& advertisingReport) { services::GapAdvertisingReport discoveredDevice; - + auto advertisementData = const_cast(&advertisingReport.Length_Data) + 1; std::copy_n(std::begin(advertisingReport.Address), discoveredDevice.address.size(), std::begin(discoveredDevice.address)); discoveredDevice.eventType = ToAdvertisingEventType(advertisingReport.Event_Type);