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

fix: better gap state update in gap central #507

Merged
merged 5 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
55 changes: 23 additions & 32 deletions hal_st/middlewares/ble_middleware/GapCentralSt.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "hal_st/middlewares/ble_middleware/GapCentralSt.hpp"
#include "ble_defs.h"
#include "infra/event/EventDispatcherWithWeakPtr.hpp"
#include "infra/util/Function.hpp"
#include "services/ble/Gap.hpp"
#include <algorithm>
#include <chrono>
#include <cmath>
Expand Down Expand Up @@ -136,14 +138,32 @@ namespace hal

void GapCentralSt::HandleHciLeConnectionCompleteEvent(evt_le_meta_event* metaEvent)
{
GapSt::HandleHciLeConnectionCompleteEvent(metaEvent);
UpdateStateOnConnectionComplete(metaEvent);

initiatingStateTimer.Cancel();

GapSt::HandleHciLeConnectionCompleteEvent(metaEvent);
}

void GapCentralSt::HandleHciLeEnhancedConnectionCompleteEvent(evt_le_meta_event* metaEvent)
{
GapSt::HandleHciLeEnhancedConnectionCompleteEvent(metaEvent);
UpdateStateOnConnectionComplete(metaEvent);

initiatingStateTimer.Cancel();

GapSt::HandleHciLeEnhancedConnectionCompleteEvent(metaEvent);
}

void GapCentralSt::UpdateStateOnConnectionComplete(evt_le_meta_event* metaEvent)
{
auto status = reinterpret_cast<hci_le_enhanced_connection_complete_event_rp0*>(metaEvent->data)->Status;

services::GapState state = status == BLE_STATUS_SUCCESS ? services::GapState::connected : services::GapState::standby;

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

void GapCentralSt::HandleGapProcedureCompleteEvent(evt_blecore_aci* vendorEvent)
Expand Down Expand Up @@ -213,17 +233,6 @@ namespace hal
SetDataLength();
});
};
else
onMtuExchangeDone = [this]()
{
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]()
{
SetPhy();
});
};

if (onDataLengthChanged)
onDataLengthChanged();
}

void GapCentralSt::HandleHciLePhyUpdateCompleteEvent(evt_le_meta_event* metaEvent)
Expand All @@ -235,11 +244,6 @@ namespace hal
really_assert(phyUpdateCompleteEvent.Connection_Handle == connectionContext.connectionHandle);

onMtuExchangeDone = nullptr;

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

void GapCentralSt::HandleGapDiscoveryProcedureEvent()
Expand Down Expand Up @@ -269,24 +273,10 @@ namespace hal

void GapCentralSt::SetDataLength()
{
onDataLengthChanged = [this]()
{
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]()
{
SetPhy();
});
};

auto status = hci_le_set_data_length(this->connectionContext.connectionHandle, services::GapConnectionParameters::connectionInitialMaxTxOctets, services::GapConnectionParameters::connectionInitialMaxTxTime);
assert(status == BLE_STATUS_SUCCESS);
}

void GapCentralSt::SetPhy() const
{
auto status = hci_le_set_phy(this->connectionContext.connectionHandle, GapSt::allPhys, GapSt::speed2Mbps, GapSt::speed2Mbps, 0);
assert(status == BLE_STATUS_SUCCESS);
}

void GapCentralSt::HandleAdvertisingReport(const Advertising_Report_t& advertisingReport)
{
services::GapAdvertisingReport discoveredDevice;
Expand Down Expand Up @@ -314,5 +304,6 @@ namespace hal

SetIoCapabilities(services::GapPairing::IoCapabilities::none);
SetSecurityMode(services::GapPairing::SecurityMode::mode1, services::GapPairing::SecurityLevel::level1);
hci_le_set_default_phy(allPhys, speed2Mbps, speed2Mbps);
}
}
3 changes: 1 addition & 2 deletions hal_st/middlewares/ble_middleware/GapCentralSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ namespace hal
void HandleGapDirectConnectionProcedureCompleteEvent();

void HandleAdvertisingReport(const Advertising_Report_t& advertisingReport);
void SetPhy() const;
void SetDataLength();
void MtuExchange() const;
void Initialize(const GapService& gapService);
void UpdateStateOnConnectionComplete(evt_le_meta_event* metaEvent);

private:
static const services::GapConnectionParameters connectionUpdateParameters;
Expand All @@ -63,7 +63,6 @@ namespace hal
bool discovering = false;
services::GapConnectionParameters connectionParameters;
infra::AutoResetFunction<void()> onMtuExchangeDone;
infra::AutoResetFunction<void()> onDataLengthChanged;
infra::TimerSingleShot initiatingStateTimer;
};
}
Expand Down
Loading