Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support to return resolve MAC address #464

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 9 additions & 6 deletions hal_st/middlewares/ble_middleware/GapCentralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ namespace hal
return static_cast<services::GapAdvertisingEventType>(eventType);
}

services::GapAdvertisingEventAddressType ToAdvertisingAddressType(uint8_t addressType)
{
return static_cast<services::GapAdvertisingEventAddressType>(addressType);
}

bool IsTxDataLengthConfigured(const hci_le_data_length_change_event_rp0& dataLengthChangeEvent)
{
return dataLengthChangeEvent.MaxTxOctets == services::GapConnectionParameters::connectionInitialMaxTxOctets &&
Expand Down Expand Up @@ -110,6 +105,14 @@ namespace hal
aci_gap_terminate_gap_proc(GAP_GENERAL_DISCOVERY_PROC);
}

infra::Optional<hal::MacAddress> 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)
{}

Expand Down Expand Up @@ -293,7 +296,7 @@ namespace hal
auto advertisementData = const_cast<uint8_t*>(&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<services::GapDeviceAddressType>(advertisingReport.Address_Type);
discoveredDevice.data = infra::MemoryRange(advertisementData, advertisementData + advertisingReport.Length_Data);
discoveredDevice.rssi = static_cast<int8_t>(*const_cast<uint8_t*>(advertisementData + advertisingReport.Length_Data));

Expand Down
1 change: 1 addition & 0 deletions hal_st/middlewares/ble_middleware/GapCentralSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace hal
void SetAddress(hal::MacAddress macAddress, services::GapDeviceAddressType addressType) override;
void StartDeviceDiscovery() override;
void StopDeviceDiscovery() override;
infra::Optional<hal::MacAddress> ResolvePrivateAddress(hal::MacAddress address) const override;

// Implementation of GapPairing
void AllowPairing(bool allow) override;
Expand Down
8 changes: 4 additions & 4 deletions hal_st/middlewares/ble_middleware/GapPeripheralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(connectionContext.peerAddressType), connectionContext.peerAddress.data(), address.address.data());

address.type = services::GapDeviceAddressType::publicAddress;
address.type = services::GapDeviceAddressType::randomAddress;

assert(status == BLE_STATUS_SUCCESS);

Expand Down Expand Up @@ -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<services::GapDeviceAddressType>(dummyPeer.Peer_Identity_Address_Type);
}
else
{
aci_gap_add_devices_to_resolving_list(numberOfBondedAddress, reinterpret_cast<const Whitelist_Identity_Entry_t*>(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<services::GapDeviceAddressType>(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);
Expand Down
40 changes: 10 additions & 30 deletions hal_st/middlewares/ble_middleware/GapSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(addressType), address.data()) == BLE_STATUS_SUCCESS;
}

void GapSt::Pair()
{
really_assert(connectionContext.connectionHandle != GapSt::invalidConnection);
Expand Down Expand Up @@ -166,15 +171,15 @@ namespace hal
auto connectionCompleteEvent = *reinterpret_cast<hci_le_connection_complete_event_rp0*>(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<services::GapDeviceAddressType>(connectionCompleteEvent.Peer_Address_Type), &connectionCompleteEvent.Peer_Address[0]);
}

void GapSt::HandleHciLeEnhancedConnectionCompleteEvent(evt_le_meta_event* metaEvent)
{
auto connectionCompleteEvt = *reinterpret_cast<hci_le_enhanced_connection_complete_event_rp0*>(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<services::GapDeviceAddressType>(connectionCompleteEvt.Peer_Address_Type), &connectionCompleteEvt.Peer_Address[0]);
}

void GapSt::HandleBondLostEvent(evt_blecore_aci* vendorEvent)
Expand All @@ -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<uint8_t>(connectionContext.peerAddressType), connectionContext.peerAddress.data()) == BLE_STATUS_SUCCESS)
{
hal::MacAddress address = connectionContext.peerAddress;
aci_gap_resolve_private_addr(connectionContext.peerAddress.data(), address.data());
Expand Down Expand Up @@ -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>(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));
}

Expand Down
15 changes: 8 additions & 7 deletions hal_st/middlewares/ble_middleware/GapSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace hal
: public services::AttMtuExchange
, public services::GapBonding
, public services::GapPairing
, private hal::HciEventSink
, private HciEventSink
{
public:
struct GapService
Expand All @@ -31,7 +31,7 @@ namespace hal

struct Configuration
{
const hal::MacAddress& address;
const MacAddress& address;
const GapService& gapService;
const RootKeys& rootKeys;
uint8_t txPowerLevel;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ namespace hal
GapCentralSt::StopDeviceDiscovery();
}

infra::Optional<hal::MacAddress> 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";
Expand All @@ -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";
Expand Down
2 changes: 2 additions & 0 deletions hal_st/middlewares/ble_middleware/TracingGapCentralSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ namespace hal
void SetAddress(hal::MacAddress macAddress, services::GapDeviceAddressType addressType) override;
void StartDeviceDiscovery() override;
void StopDeviceDiscovery() override;
infra::Optional<hal::MacAddress> 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;
Expand Down
Loading