diff --git a/CMakeLists.txt b/CMakeLists.txt index 73e2abea..edf9dece 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 4239adfcec976c47fbdc2b8ada6c6577d7a3c2b9 # unreleased + GIT_TAG ba76e725024c3458a2962703cf596531e658e0b3 # unreleased ) FetchContent_MakeAvailable(emil) diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp index f7505f96..f530a490 100644 --- a/hal_st/middlewares/ble_middleware/GapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapCentralSt.cpp @@ -29,11 +29,6 @@ namespace hal return static_cast(eventType); } - services::GapAdvertisingEventAddressType ToAdvertisingAddressType(uint8_t addressType) - { - return static_cast(addressType); - } - bool IsTxDataLengthConfigured(const hci_le_data_length_change_event_rp0& dataLengthChangeEvent) { return dataLengthChangeEvent.MaxTxOctets == services::GapConnectionParameters::connectionInitialMaxTxOctets && @@ -110,6 +105,14 @@ namespace hal aci_gap_terminate_gap_proc(GAP_GENERAL_DISCOVERY_PROC); } + infra::Optional GapCentralSt::ResolvePrivateAddress(hal::MacAddress address) const + { + hal::MacAddress identityAddress; + if (aci_gap_resolve_private_addr(address.data(), identityAddress.data()) != BLE_STATUS_SUCCESS) + return infra::none; + return infra::MakeOptional(identityAddress); + } + void GapCentralSt::AllowPairing(bool) {} @@ -293,7 +296,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 = static_cast(advertisingReport.Address_Type); discoveredDevice.data = infra::MemoryRange(advertisementData, advertisementData + advertisingReport.Length_Data); discoveredDevice.rssi = static_cast(*const_cast(advertisementData + advertisingReport.Length_Data)); diff --git a/hal_st/middlewares/ble_middleware/GapCentralSt.hpp b/hal_st/middlewares/ble_middleware/GapCentralSt.hpp index 27260b28..e6450665 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 ResolvePrivateAddress(hal::MacAddress address) const override; // Implementation of GapPairing void AllowPairing(bool allow) override; 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 55a72a71..ce0c7105 100644 --- a/hal_st/middlewares/ble_middleware/GapSt.cpp +++ b/hal_st/middlewares/ble_middleware/GapSt.cpp @@ -91,6 +91,11 @@ namespace hal return numberOfBondedAddress; } + bool GapSt::IsDeviceBonded(MacAddress address, services::GapDeviceAddressType addressType) const + { + return aci_gap_is_device_bonded(static_cast(addressType), address.data()) == BLE_STATUS_SUCCESS; + } + void GapSt::Pair() { really_assert(connectionContext.connectionHandle != GapSt::invalidConnection); @@ -166,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) @@ -174,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) @@ -201,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 (aci_gap_is_device_bonded(static_cast(connectionContext.peerAddressType), connectionContext.peerAddress.data()) == BLE_STATUS_SUCCESS) { hal::MacAddress address = connectionContext.peerAddress; aci_gap_resolve_private_addr(connectionContext.peerAddress.data(), address.data()); @@ -305,36 +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) - { - enum class PeerAddressType : uint8_t - { - PUBLIC, - RANDOM, - RESOLVED_PUBLIC_IDENTITY, - RESOLVED_RANDOM_STATIC_IDENTITY - }; - - switch (static_cast(peerAddressType)) - { - case PeerAddressType::PUBLIC: - case PeerAddressType::RANDOM: - return peerAddressType; - - case PeerAddressType::RESOLVED_PUBLIC_IDENTITY: - return infra::enum_cast(PeerAddressType::PUBLIC); - - case PeerAddressType::RESOLVED_RANDOM_STATIC_IDENTITY: - default: - return infra::enum_cast(PeerAddressType::RANDOM); - } - }; - 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 7a41b414..701195c0 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 IsDeviceBonded(MacAddress address, services::GapDeviceAddressType addressType) const override; // Implementation of GapPairing void Pair() override; @@ -54,7 +55,7 @@ namespace hal void NumericComparisonConfirm(bool accept) 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); @@ -72,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 @@ -81,15 +82,15 @@ 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: struct ConnectionContext { uint16_t connectionHandle; - uint8_t peerAddressType; - hal::MacAddress peerAddress; + services::GapDeviceAddressType peerAddressType; + MacAddress peerAddress; }; ConnectionContext connectionContext; diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp index e3a5584b..0f4b30ea 100644 --- a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp +++ b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp @@ -45,6 +45,17 @@ namespace hal GapCentralSt::StopDeviceDiscovery(); } + infra::Optional TracingGapCentralSt::ResolvePrivateAddress(hal::MacAddress address) const + { + 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 + tracer.Continue() << ", could not resolve MAC address"; + return resolvedMac; + } + void TracingGapCentralSt::RemoveAllBonds() { tracer.Trace() << "TracingGapCentralSt::RemoveAllBonds"; @@ -69,6 +80,13 @@ namespace hal return GapCentralSt::GetNumberOfBonds(); } + bool TracingGapCentralSt::IsDeviceBonded(hal::MacAddress address, services::GapDeviceAddressType addressType) const + { + auto ret = GapCentralSt::IsDeviceBonded(address, addressType); + tracer.Trace() << "TracingGapCentralSt::IsDeviceBonded " << infra::AsMacAddress(address) << " -> " << (ret ? "true" : "false"); + return ret; + } + void TracingGapCentralSt::Pair() { tracer.Trace() << "TracingGapCentralSt::Pair"; diff --git a/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp b/hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp index 301bef00..3ee4afc2 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 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 address, services::GapDeviceAddressType addressType) const override; // Implementation of GapPairing void Pair() override;