Skip to content

Commit

Permalink
new solution
Browse files Browse the repository at this point in the history
  • Loading branch information
cassio-lazaro committed Jan 16, 2025
1 parent b05fb88 commit c572283
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 62 deletions.
19 changes: 1 addition & 18 deletions hal_st/middlewares/ble_middleware/GapCentralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,6 @@ namespace hal
return static_cast<services::GapAdvertisingEventType>(eventType);
}

services::GapAdvertisingEventAddressType ToAdvertisingAddressType(uint8_t addressType, const uint8_t address[6])
{
if (addressType == static_cast<uint8_t>(services::GapDeviceAddressType::publicAddress))
return services::GapAdvertisingEventAddressType::publicDeviceAddress;
else if (addressType == static_cast<uint8_t>(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)
{
return dataLengthChangeEvent.MaxTxOctets == services::GapConnectionParameters::connectionInitialMaxTxOctets &&
Expand Down Expand Up @@ -313,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, advertisingReport.Address);
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
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
32 changes: 7 additions & 25 deletions hal_st/middlewares/ble_middleware/GapSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(PeerAddressType::publicStatic), identityAddress.data()) == BLE_STATUS_SUCCESS;
return aci_gap_is_device_bonded(static_cast<uint8_t>(addressType), address.data()) == BLE_STATUS_SUCCESS;
}

void GapSt::Pair()
Expand Down Expand Up @@ -171,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 @@ -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 (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 @@ -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>(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));
}

Expand Down
14 changes: 3 additions & 11 deletions hal_st/middlewares/ble_middleware/GapSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};

Expand Down
6 changes: 3 additions & 3 deletions hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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 @@ -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;
Expand Down

0 comments on commit c572283

Please sign in to comment.