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: gap and gatt refactored/improved due to embedded-infra changes #136

Merged
merged 24 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a9a0ed3
feat: gap and gatt st implementation simplified due to embedded-infra…
gabrielsantosphilips May 24, 2023
b678c19
feat: amp-embedded-infra hash updated on CMakeLists.txt
gabrielsantosphilips May 25, 2023
e2310dc
Merge branch 'main' into feature/gap_gatt_st_refactor
gabrielsantosphilips Jun 7, 2023
bab79ba
feat: exchange MTU moved from GAP to GATT. Unnecessary includes remov…
gabrielsantosphilips Jun 9, 2023
43fb765
Merge branch 'main' into feature/gap_gatt_st_refactor
gabrielsantosphilips Jun 9, 2023
70018f9
feat: amp-embedded-infra-lib hash updated
gabrielsantosphilips Jun 9, 2023
a4fdc06
feat: gatt client st, exchange mtu print message improved
gabrielsantosphilips Jun 9, 2023
45391cb
Revert "feat: gatt client st, exchange mtu print message improved"
gabrielsantosphilips Jun 12, 2023
cd2f777
Revert "feat: exchange MTU moved from GAP to GATT. Unnecessary includ…
gabrielsantosphilips Jun 12, 2023
272621e
feat: add mtu exchange to gap central implementation
gabrielsantosphilips Jun 12, 2023
2d60d33
fix: amp-embedded-infra-lib version fixed
gabrielsantosphilips Jun 12, 2023
879e89e
Gap central tracing improved
gabrielsantosphilips Jun 23, 2023
877c9c2
add function to set phy to 2 mbps
gabrielsantosphilips Jun 23, 2023
10001ef
fix disonnection trace function
gabrielsantosphilips Jun 23, 2023
b3aafd5
add sequence of configuration after connection is established
gabrielsantosphilips Jun 26, 2023
c9ae74b
apply suggestions from code review
gabrielsantosphilips Jun 26, 2023
e98d037
Merge branch 'main' into feature/gap_gatt_st_refactor
gabrielsantosphilips Jun 26, 2023
db8e0f7
error handing on connection failure improved
gabrielsantosphilips Jul 6, 2023
f7b7b11
Merge branch 'main' into feature/gap_gatt_st_refactor
gabrielsantosphilips Jul 6, 2023
e07150d
Merge branch 'main' into feature/gap_gatt_st_refactor
gabrielsantosphilips Jul 7, 2023
09ca7d1
cmakelists updated with the latests embedded-infra-lib commit
gabrielsantosphilips Jul 7, 2023
98ed2f0
only asserts if GATT complete response has no error
gabrielsantosphilips Jul 11, 2023
5f0adf6
PR comments fixed
gabrielsantosphilips Jul 11, 2023
03facd8
Merge branch 'main' into feature/gap_gatt_st_refactor
gabrielsantosphilips Jul 12, 2023
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 @@ -19,7 +19,7 @@ if (HALST_STANDALONE)
FetchContent_Declare(
emil
GIT_REPOSITORY https://github.com/philips-software/amp-embedded-infra-lib.git
GIT_TAG 77eb4170285a16e31cae5aee6a96e792baea2aa7 # Unreleased
GIT_TAG 88a4689e8f08e2afe856170aa5b5036f69d1610c # Unreleased
)

FetchContent_MakeAvailable(emil)
Expand Down
94 changes: 56 additions & 38 deletions hal_st/middlewares/ble_middleware/GapCentralSt.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "hal_st/middlewares/ble_middleware/GapCentralSt.hpp"
#include "ble_defs.h"
#include "infra/event/EventDispatcherWithWeakPtr.hpp"
#include "infra/stream/ByteInputStream.hpp"

Expand All @@ -14,31 +15,6 @@ namespace hal

namespace
{
infra::ConstByteRange GetLocalNameFromAdvertising(infra::ConstByteRange advertisingIndication)
{
infra::ByteInputStream stream(advertisingIndication, infra::noFail);

while (!stream.Empty())
{
uint8_t length = 0;
uint8_t adType = 0;

stream >> length;

if (length > 0)
{
stream >> adType;

if (adType == AD_TYPE_COMPLETE_LOCAL_NAME || adType == AD_TYPE_SHORTENED_LOCAL_NAME)
return stream.ContiguousRange(length - 1);

stream.Consume(length - 1);
}
}

return infra::ConstByteRange();
}

services::GapAdvertisingEventType ToAdvertisingEventType(uint8_t eventType)
{
return static_cast<services::GapAdvertisingEventType>(eventType);
Expand Down Expand Up @@ -165,6 +141,17 @@ namespace hal
}
}

void GapCentralSt::HandleGattCompleteEvent(evt_blecore_aci* vendorEvent)
{
gabrielsantosphilips marked this conversation as resolved.
Show resolved Hide resolved
auto gattCompleteEvent = *reinterpret_cast<aci_gatt_proc_complete_event_rp0*>(vendorEvent->data);

really_assert(gattCompleteEvent.Connection_Handle == connectionContext.connectionHandle);
really_assert(gattCompleteEvent.Error_Code == BLE_STATUS_SUCCESS);

if (onConnection)
infra::Subject<services::GapCentralObserver>::NotifyObservers(std::exchange(onConnection, nullptr));
}

void GapCentralSt::HandleL2capConnectionUpdateRequestEvent(evt_blecore_aci* vendorEvent)
{
GapSt::HandleL2capConnectionUpdateRequestEvent(vendorEvent);
Expand All @@ -185,7 +172,44 @@ namespace hal

void GapCentralSt::HandleGapDirectConnectionEstablishmentEvent()
{
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]() { this->DataLengthUpdate(); });
onConnection = [](services::GapCentralObserver& observer)
{
observer.StateChanged(services::GapState::connected);
};

SetPhy();
SetDataLength();
MtuExchange();
}

void GapCentralSt::SetPhy()
{
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]()
{
if (hci_le_set_phy(this->connectionContext.connectionHandle, GapSt::allPhys, GapSt::speed2Mbps, GapSt::speed2Mbps, 0) == commandDisallowed)
gabrielsantosphilips marked this conversation as resolved.
Show resolved Hide resolved
{
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]()
{
this->SetPhy();
});
}
});
}

void GapCentralSt::SetDataLength() const
{
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]()
{
hci_le_set_data_length(this->connectionContext.connectionHandle, services::GapConnectionParameters::connectionInitialMaxTxOctets, services::GapConnectionParameters::connectionInitialMaxTxTime);
});
}

void GapCentralSt::MtuExchange() const
{
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]()
{
aci_gatt_exchange_config(this->connectionContext.connectionHandle);
});
}

void GapCentralSt::HandleAdvertisingReport(const Advertising_Report_t& advertisingReport)
Expand All @@ -199,27 +223,21 @@ namespace hal

auto advertisementData = reinterpret_cast<const uint8_t*>(&advertisingReport.Length_Data) + 1;

discoveredDevice.data = GetLocalNameFromAdvertising(infra::MemoryRange(advertisementData, advertisementData + advertisingReport.Length_Data));
discoveredDevice.data = infra::MemoryRange(advertisementData, advertisementData + advertisingReport.Length_Data);

infra::Subject<services::GapCentralObserver>::NotifyObservers([&discoveredDevice](auto& observer) { observer.DeviceDiscovered(discoveredDevice); });
infra::Subject<services::GapCentralObserver>::NotifyObservers([&discoveredDevice](auto& observer)
{
observer.DeviceDiscovered(discoveredDevice);
});
}

void GapCentralSt::SetConnectionInterval(uint16_t connectionInterval, uint16_t slaveLatency, uint16_t timeoutMultiplier)
void GapCentralSt::SetConnectionInterval() const
{
aci_l2cap_connection_parameter_update_req(connectionContext.connectionHandle,
connectionParameters.minConnIntMultiplier, connectionParameters.maxConnIntMultiplier,
connectionParameters.slaveLatency, connectionParameters.supervisorTimeoutMs);
}

void GapCentralSt::DataLengthUpdate()
{
[[maybe_unused]] auto status = hci_le_set_data_length(connectionContext.connectionHandle, transmissionOctets, transmissionTime);

assert(status == BLE_STATUS_SUCCESS);

infra::Subject<services::GapCentralObserver>::NotifyObservers([](auto& observer) { observer.StateChanged(services::GapState::connected); });
}

void GapCentralSt::Initialize(const GapService& gapService)
{
uint16_t gapServiceHandle, gapDevNameCharHandle, gapAppearanceCharHandle;
Expand Down
15 changes: 10 additions & 5 deletions hal_st/middlewares/ble_middleware/GapCentralSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "hci_tl.h"
#include "infra/util/BoundedString.hpp"
#include "infra/util/BoundedVector.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/ProxyCreator.hpp"
#include "services/ble/BondStorageSynchronizer.hpp"
#include "services/ble/Gap.hpp"
Expand Down Expand Up @@ -36,23 +37,23 @@ namespace hal
virtual void HandleHciLeDataLengthChangeEvent(evt_le_meta_event* metaEvent) override;
virtual void HandleHciLePhyUpdateCompleteEvent(evt_le_meta_event* metaEvent) override;
virtual void HandleGapProcedureCompleteEvent(evt_blecore_aci* vendorEvent) override;
virtual void HandleGattCompleteEvent(evt_blecore_aci* vendorEvent) override;
virtual void HandleL2capConnectionUpdateRequestEvent(evt_blecore_aci* vendorEvent) override;

private:
void HandleGapDiscoveryProcedureEvent();
void HandleGapDirectConnectionEstablishmentEvent();

void HandleAdvertisingReport(const Advertising_Report_t& advertisingReport);
void SetConnectionInterval(uint16_t connectionInterval, uint16_t slaveLatency, uint16_t timeoutMultiplier);
void DataLengthUpdate();
void SetConnectionInterval() const;
void SetPhy();
void SetDataLength() const;
void MtuExchange() const;
void Initialize(const GapService& gapService);

private:
static const services::GapConnectionParameters connectionUpdateParameters;

const uint16_t transmissionOctets = 251;
const uint16_t transmissionTime = 2120;

// Create connection parameters
const uint16_t leScanInterval = 0x320;
const uint16_t leScanWindow = 0x320;
Expand All @@ -68,8 +69,12 @@ namespace hal
const uint8_t filterDuplicatesEnabled = 1;
const uint8_t acceptAllParameters = 1;

// HCI status
const uint8_t commandDisallowed = 0x0c;

bool discovering = false;
services::GapConnectionParameters connectionParameters;
infra::Function<void(services::GapCentralObserver&)> onConnection;
};
}

Expand Down
16 changes: 13 additions & 3 deletions hal_st/middlewares/ble_middleware/GapSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@

SVCCTL_Init();

hci_le_write_suggested_default_data_length(services::GapPeripheral::connectionInitialMaxTxOctets, services::GapPeripheral::connectionInitialMaxTxTime);
hci_le_write_suggested_default_data_length(services::GapConnectionParameters::connectionInitialMaxTxOctets, services::GapConnectionParameters::connectionInitialMaxTxTime);
hci_le_set_default_phy(allPhys, speed2Mbps, speed2Mbps);
}

Expand Down Expand Up @@ -137,8 +137,15 @@

void GapSt::HandleMtuExchangeResponseEvent(evt_blecore_aci* vendorEvent)
{
maxAttMtu = reinterpret_cast<aci_att_exchange_mtu_resp_event_rp0*>(vendorEvent->data)->Server_RX_MTU;
AttMtuExchange::NotifyObservers([](auto& observer) { observer.ExchangedMaxAttMtuSize(); });
auto attExchangeMtuResponse = *reinterpret_cast<aci_att_exchange_mtu_resp_event_rp0*>(vendorEvent->data);

really_assert(attExchangeMtuResponse.Connection_Handle == connectionContext.connectionHandle);
maxAttMtu = attExchangeMtuResponse.Server_RX_MTU;

AttMtuExchange::NotifyObservers([](auto& observer)
{
observer.ExchangedMaxAttMtuSize();
});
}

void GapSt::SetAddress(const hal::MacAddress& address, services::GapDeviceAddressType addressType)
Expand Down Expand Up @@ -202,7 +209,7 @@

switch (vendorEvent->ecode)
{
case ACI_GAP_PAIRING_COMPLETE_VSEVT_CODE:

Check warning on line 212 in hal_st/middlewares/ble_middleware/GapSt.cpp

View workflow job for this annotation

GitHub Actions / Formatting

[clang-format] reported by reviewdog 🐶 Raw Output: hal_st/middlewares/ble_middleware/GapSt.cpp:212:- case ACI_GAP_PAIRING_COMPLETE_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:213:- HandlePairingCompleteEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:214:- break; hal_st/middlewares/ble_middleware/GapSt.cpp:215:- case ACI_GAP_BOND_LOST_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:216:- HandleBondLostEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:217:- break; hal_st/middlewares/ble_middleware/GapSt.cpp:218:- case ACI_GAP_PROC_COMPLETE_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:219:- HandleGapProcedureCompleteEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:220:- break; hal_st/middlewares/ble_middleware/GapSt.cpp:221:- case ACI_GATT_PROC_COMPLETE_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:222:- HandleGattCompleteEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:223:- break; hal_st/middlewares/ble_middleware/GapSt.cpp:224:- case ACI_L2CAP_CONNECTION_UPDATE_REQ_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:225:- HandleL2capConnectionUpdateRequestEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:226:- break; hal_st/middlewares/ble_middleware/GapSt.cpp:227:- case ACI_ATT_EXCHANGE_MTU_RESP_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:228:- HandleMtuExchangeResponseEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:229:- break; hal_st/middlewares/ble_middleware/GapSt.cpp:230:- default: hal_st/middlewares/ble_middleware/GapSt.cpp:231:- break; hal_st/middlewares/ble_middleware/GapSt.cpp:210:+ case ACI_GAP_PAIRING_COMPLETE_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:211:+ HandlePairingCompleteEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:212:+ break; hal_st/middlewares/ble_middleware/GapSt.cpp:213:+ case ACI_GAP_BOND_LOST_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:214:+ HandleBondLostEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:215:+ break; hal_st/middlewares/ble_middleware/GapSt.cpp:216:+ case ACI_GAP_PROC_COMPLETE_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:217:+ HandleGapProcedureCompleteEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:218:+ break; hal_st/middlewares/ble_middleware/GapSt.cpp:219:+ case ACI_GATT_PROC_COMPLETE_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:220:+ HandleGattCompleteEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:221:+ break; hal_st/middlewares/ble_middleware/GapSt.cpp:222:+ case ACI_L2CAP_CONNECTION_UPDATE_REQ_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:223:+ HandleL2capConnectionUpdateRequestEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:224:+ break; hal_st/middlewares/ble_middleware/GapSt.cpp:225:+ case ACI_ATT_EXCHANGE_MTU_RESP_VSEVT_CODE: hal_st/middlewares/ble_middleware/GapSt.cpp:226:+ HandleMtuExchangeResponseEvent(vendorEvent); hal_st/middlewares/ble_middleware/GapSt.cpp:227:+ break; hal_st/middlewares/ble_middleware/GapSt.cpp:228:+ default: hal_st/middlewares/ble_middleware/GapSt.cpp:229:+ break;
HandlePairingCompleteEvent(vendorEvent);
break;
case ACI_GAP_BOND_LOST_VSEVT_CODE:
Expand All @@ -211,6 +218,9 @@
case ACI_GAP_PROC_COMPLETE_VSEVT_CODE:
HandleGapProcedureCompleteEvent(vendorEvent);
break;
case ACI_GATT_PROC_COMPLETE_VSEVT_CODE:
HandleGattCompleteEvent(vendorEvent);
break;
case ACI_L2CAP_CONNECTION_UPDATE_REQ_VSEVT_CODE:
HandleL2capConnectionUpdateRequestEvent(vendorEvent);
break;
Expand Down
9 changes: 5 additions & 4 deletions hal_st/middlewares/ble_middleware/GapSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,24 @@
virtual void HandleHciDisconnectEvent(hci_event_pckt& eventPacket);

virtual void HandleHciLeConnectionCompleteEvent(evt_le_meta_event* metaEvent);
virtual void HandleHciLeAdvertisingReportEvent(evt_le_meta_event* metaEvent) {};

Check warning on line 43 in hal_st/middlewares/ble_middleware/GapSt.hpp

View workflow job for this annotation

GitHub Actions / Formatting

[clang-format] reported by reviewdog 🐶 Raw Output: hal_st/middlewares/ble_middleware/GapSt.hpp:43:- virtual void HandleHciLeAdvertisingReportEvent(evt_le_meta_event* metaEvent) {}; hal_st/middlewares/ble_middleware/GapSt.hpp:44:- virtual void HandleHciLeConnectionUpdateCompleteEvent(evt_le_meta_event* metaEvent) {}; hal_st/middlewares/ble_middleware/GapSt.hpp:45:- virtual void HandleHciLeDataLengthChangeEvent(evt_le_meta_event* metaEvent) {}; hal_st/middlewares/ble_middleware/GapSt.hpp:46:- virtual void HandleHciLePhyUpdateCompleteEvent(evt_le_meta_event* metaEvent) {}; hal_st/middlewares/ble_middleware/GapSt.hpp:43:+ virtual void HandleHciLeAdvertisingReportEvent(evt_le_meta_event* metaEvent){}; hal_st/middlewares/ble_middleware/GapSt.hpp:44:+ virtual void HandleHciLeConnectionUpdateCompleteEvent(evt_le_meta_event* metaEvent){}; hal_st/middlewares/ble_middleware/GapSt.hpp:45:+ virtual void HandleHciLeDataLengthChangeEvent(evt_le_meta_event* metaEvent){}; hal_st/middlewares/ble_middleware/GapSt.hpp:46:+ virtual void HandleHciLePhyUpdateCompleteEvent(evt_le_meta_event* metaEvent){};
virtual void HandleHciLeConnectionUpdateCompleteEvent(evt_le_meta_event* metaEvent) {};
virtual void HandleHciLeDataLengthChangeEvent(evt_le_meta_event* metaEvent) {};
virtual void HandleHciLePhyUpdateCompleteEvent(evt_le_meta_event* metaEvent) {};
virtual void HandleHciLeEnhancedConnectionCompleteEvent(evt_le_meta_event* metaEvent);

virtual void HandlePairingCompleteEvent(evt_blecore_aci* vendorEvent) {};
virtual void HandlePairingCompleteEvent(evt_blecore_aci* vendorEvent){};
virtual void HandleBondLostEvent(evt_blecore_aci* vendorEvent);
virtual void HandleGapProcedureCompleteEvent(evt_blecore_aci* vendorEvent) {};
virtual void HandleL2capConnectionUpdateRequestEvent(evt_blecore_aci* vendorEvent) {};
virtual void HandleGapProcedureCompleteEvent(evt_blecore_aci* vendorEvent){};
virtual void HandleGattCompleteEvent(evt_blecore_aci* vendorEvent){};
virtual void HandleL2capConnectionUpdateRequestEvent(evt_blecore_aci* vendorEvent){};
virtual void HandleMtuExchangeResponseEvent(evt_blecore_aci* vendorEvent);

void SetAddress(const hal::MacAddress& address, services::GapDeviceAddressType addressType);

private:
// Implementation of HciEventSink
virtual void HciEvent(hci_event_pckt& event);
void HciEvent(hci_event_pckt& event) override;

void HandleHciLeMetaEvent(hci_event_pckt& eventPacket);
void HandleHciVendorSpecificDebugEvent(hci_event_pckt& eventPacket);
Expand Down
12 changes: 7 additions & 5 deletions hal_st/middlewares/ble_middleware/GattClientSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ namespace hal
auto gattProcedureEvent = *reinterpret_cast<aci_gatt_proc_complete_event_rp0*>(vendorEvent->data);

really_assert(gattProcedureEvent.Connection_Handle == connectionHandle);
gabrielsantosphilips marked this conversation as resolved.
Show resolved Hide resolved
really_assert(gattProcedureEvent.Error_Code == BLE_STATUS_SUCCESS);

if (onDiscoveryCompletion)
infra::Subject<services::GattClientDiscoveryObserver>::NotifyObservers(std::exchange(onDiscoveryCompletion, nullptr));
else if (onDone)
std::exchange(onDone, nullptr)();
if (gattProcedureEvent.Error_Code == BLE_STATUS_SUCCESS)
{
if (onDiscoveryCompletion)
infra::Subject<services::GattClientDiscoveryObserver>::NotifyObservers(std::exchange(onDiscoveryCompletion, nullptr));
else if (onDone)
std::exchange(onDone, nullptr)();
}
}

void GattClientSt::HandleHciLeConnectionCompleteEvent(evt_le_meta_event* metaEvent)
Expand Down
1 change: 0 additions & 1 deletion hal_st/middlewares/ble_middleware/GattClientSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace hal
class GattClientSt
: public services::GattClientDiscovery
, public services::GattClientCharacteristicOperations
, public services::GattClientStackUpdate
EkelmansPh marked this conversation as resolved.
Show resolved Hide resolved
, public hal::HciEventSink
{
public:
Expand Down
Loading