Skip to content

Commit 88156e0

Browse files
authored
fix: better gap state update in gap central (#507)
* clean up GapCentralSt - remove dynamic phy update, use default setting - change state to connected without waiting for all update ops * GapCentralSt: add missing connected state update * GapCentralSt: Handle failed connection complete better * update ci amp-devcontainer-cpp
1 parent 101bc13 commit 88156e0

File tree

3 files changed

+25
-35
lines changed

3 files changed

+25
-35
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
host_build_clang_msvc:
6868
name: Windows Host Build from Devcontainer
6969
runs-on: [ubuntu-latest]
70-
container: ghcr.io/philips-software/amp-devcontainer-cpp:5.2.0@sha256:c47fcc83b59fb08f3a3a6e591b18bad49b3862acc35770fca6cec9ad0adb9cb2 # v5.2.0
70+
container: ghcr.io/philips-software/amp-devcontainer-cpp:v5.6.0@sha256:884732270a353e8446f813e649f2918ac53ce446a6c0dd4f7f84a6d58fb26bce # v5.6.0
7171
steps:
7272
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
7373
- uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0

hal_st/middlewares/ble_middleware/GapCentralSt.cpp

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "hal_st/middlewares/ble_middleware/GapCentralSt.hpp"
22
#include "ble_defs.h"
33
#include "infra/event/EventDispatcherWithWeakPtr.hpp"
4+
#include "infra/util/Function.hpp"
5+
#include "services/ble/Gap.hpp"
46
#include <algorithm>
57
#include <chrono>
68
#include <cmath>
@@ -136,14 +138,32 @@ namespace hal
136138

137139
void GapCentralSt::HandleHciLeConnectionCompleteEvent(evt_le_meta_event* metaEvent)
138140
{
139-
GapSt::HandleHciLeConnectionCompleteEvent(metaEvent);
141+
UpdateStateOnConnectionComplete(metaEvent);
142+
140143
initiatingStateTimer.Cancel();
144+
145+
GapSt::HandleHciLeConnectionCompleteEvent(metaEvent);
141146
}
142147

143148
void GapCentralSt::HandleHciLeEnhancedConnectionCompleteEvent(evt_le_meta_event* metaEvent)
144149
{
145-
GapSt::HandleHciLeEnhancedConnectionCompleteEvent(metaEvent);
150+
UpdateStateOnConnectionComplete(metaEvent);
151+
146152
initiatingStateTimer.Cancel();
153+
154+
GapSt::HandleHciLeEnhancedConnectionCompleteEvent(metaEvent);
155+
}
156+
157+
void GapCentralSt::UpdateStateOnConnectionComplete(evt_le_meta_event* metaEvent)
158+
{
159+
auto status = reinterpret_cast<hci_le_enhanced_connection_complete_event_rp0*>(metaEvent->data)->Status;
160+
161+
services::GapState state = status == BLE_STATUS_SUCCESS ? services::GapState::connected : services::GapState::standby;
162+
163+
infra::Subject<services::GapCentralObserver>::NotifyObservers([state](services::GapCentralObserver& observer)
164+
{
165+
observer.StateChanged(state);
166+
});
147167
}
148168

149169
void GapCentralSt::HandleGapProcedureCompleteEvent(evt_blecore_aci* vendorEvent)
@@ -213,17 +233,6 @@ namespace hal
213233
SetDataLength();
214234
});
215235
};
216-
else
217-
onMtuExchangeDone = [this]()
218-
{
219-
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]()
220-
{
221-
SetPhy();
222-
});
223-
};
224-
225-
if (onDataLengthChanged)
226-
onDataLengthChanged();
227236
}
228237

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

237246
onMtuExchangeDone = nullptr;
238-
239-
infra::Subject<services::GapCentralObserver>::NotifyObservers([](services::GapCentralObserver& observer)
240-
{
241-
observer.StateChanged(services::GapState::connected);
242-
});
243247
}
244248

245249
void GapCentralSt::HandleGapDiscoveryProcedureEvent()
@@ -269,24 +273,10 @@ namespace hal
269273

270274
void GapCentralSt::SetDataLength()
271275
{
272-
onDataLengthChanged = [this]()
273-
{
274-
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]()
275-
{
276-
SetPhy();
277-
});
278-
};
279-
280276
auto status = hci_le_set_data_length(this->connectionContext.connectionHandle, services::GapConnectionParameters::connectionInitialMaxTxOctets, services::GapConnectionParameters::connectionInitialMaxTxTime);
281277
assert(status == BLE_STATUS_SUCCESS);
282278
}
283279

284-
void GapCentralSt::SetPhy() const
285-
{
286-
auto status = hci_le_set_phy(this->connectionContext.connectionHandle, GapSt::allPhys, GapSt::speed2Mbps, GapSt::speed2Mbps, 0);
287-
assert(status == BLE_STATUS_SUCCESS);
288-
}
289-
290280
void GapCentralSt::HandleAdvertisingReport(const Advertising_Report_t& advertisingReport)
291281
{
292282
services::GapAdvertisingReport discoveredDevice;
@@ -314,5 +304,6 @@ namespace hal
314304

315305
SetIoCapabilities(services::GapPairing::IoCapabilities::none);
316306
SetSecurityMode(services::GapPairing::SecurityMode::mode1, services::GapPairing::SecurityLevel::level1);
307+
hci_le_set_default_phy(allPhys, speed2Mbps, speed2Mbps);
317308
}
318309
}

hal_st/middlewares/ble_middleware/GapCentralSt.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ namespace hal
4242
void HandleGapDirectConnectionProcedureCompleteEvent();
4343

4444
void HandleAdvertisingReport(const Advertising_Report_t& advertisingReport);
45-
void SetPhy() const;
4645
void SetDataLength();
4746
void MtuExchange() const;
4847
void Initialize(const GapService& gapService);
48+
void UpdateStateOnConnectionComplete(evt_le_meta_event* metaEvent);
4949

5050
private:
5151
static const services::GapConnectionParameters connectionUpdateParameters;
@@ -63,7 +63,6 @@ namespace hal
6363
bool discovering = false;
6464
services::GapConnectionParameters connectionParameters;
6565
infra::AutoResetFunction<void()> onMtuExchangeDone;
66-
infra::AutoResetFunction<void()> onDataLengthChanged;
6766
infra::TimerSingleShot initiatingStateTimer;
6867
};
6968
}

0 commit comments

Comments
 (0)