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: improvements according to application note AN5289 #135

Merged
merged 6 commits into from
May 30, 2023
31 changes: 6 additions & 25 deletions hal_st/middlewares/ble_middleware/GapCentralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace hal
{
const services::GapConnectionParameters GapCentralSt::connectionUpdateParameters
{
320, // 400 ms
320, // 400 ms
6, // 7.5 ms
6, // 7.5 ms
0,
500,
};
Expand Down Expand Up @@ -66,7 +66,8 @@ namespace hal
leScanInterval, leScanWindow, peerAddress, macAddress.data(), RESOLVABLE_PRIVATE_ADDR,
connectionUpdateParameters.minConnIntMultiplier, connectionUpdateParameters.maxConnIntMultiplier,
connectionUpdateParameters.slaveLatency, connectionUpdateParameters.supervisorTimeoutMs,
minConnectionEventLength, connectionMaxCeLength);
minConnectionEventLength, maxConnectionEventLength);

}

void GapCentralSt::Disconnect()
Expand All @@ -83,7 +84,7 @@ namespace hal
{
if (!discovering)
{
aci_gap_start_general_discovery_proc(leScanInterval, leScanWindow, GAP_NON_RESOLVABLE_PRIVATE_ADDR, filterDuplicatesEnabled);
aci_gap_start_general_discovery_proc(leScanInterval, leScanWindow, GAP_RESOLVABLE_PRIVATE_ADDR, filterDuplicatesEnabled);
discovering = true;
infra::Subject<services::GapCentralObserver>::NotifyObservers([](auto& observer) { observer.StateChanged(services::GapState::scanning); });
}
Expand All @@ -105,29 +106,10 @@ namespace hal
connectionParameters.slaveLatency, connectionParameters.supervisorTimeoutMs);
}

void GapCentralSt::SetPhy()
{
if (hci_le_set_phy(connectionContext.connectionHandle, allPhys, speed1Mbps | speed2Mbps, speed1Mbps | speed2Mbps, 0) != BLE_STATUS_SUCCESS)
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]() { this->SetPhy(); });
else
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]() { this->DataLengthUpdate(); });
}

void GapCentralSt::DataLengthUpdate()
{
if (hci_le_set_data_length(connectionContext.connectionHandle, transmissionOctets, transmissionTime) != BLE_STATUS_SUCCESS)
oguzcanoguz marked this conversation as resolved.
Show resolved Hide resolved
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]() { this->DataLengthUpdate(); });
else
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]() { this->ConnectionUpdate(); });
}

void GapCentralSt::ConnectionUpdate()
{
if (aci_gap_start_connection_update(connectionContext.connectionHandle,
connectionUpdateParameters.minConnIntMultiplier, connectionUpdateParameters.maxConnIntMultiplier,
connectionUpdateParameters.slaveLatency, connectionUpdateParameters.supervisorTimeoutMs,
minConnectionEventLength, maxConnectionEventLength) != BLE_STATUS_SUCCESS)
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]() { this->ConnectionUpdate(); });
else
infra::Subject<services::GapCentralObserver>::NotifyObservers([](auto& observer) { observer.StateChanged(services::GapState::connected); });
}
Expand Down Expand Up @@ -219,7 +201,7 @@ namespace hal

void GapCentralSt::HandleGapDirectConnectionEstablishmentEvent()
{
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]() { this->SetPhy(); });
infra::EventDispatcherWithWeakPtr::Instance().Schedule([this]() { this->DataLengthUpdate(); });
}

void GapCentralSt::HandleHciLeConnectionUpdateCompleteEvent(evt_le_meta_event* metaEvent)
Expand Down Expand Up @@ -300,6 +282,5 @@ namespace hal

aci_gap_set_io_capability(ioCapability);
aci_gap_set_authentication_requirement(bondingMode, mitmMode, secureConnectionSupport, keypressNotificationSupport, 16, 16, 0, 111111, GAP_PUBLIC_ADDR);
aci_gap_configure_whitelist();
}
}
5 changes: 1 addition & 4 deletions hal_st/middlewares/ble_middleware/GapCentralSt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ namespace hal

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

private:
Expand All @@ -65,11 +63,10 @@ namespace hal
// Create connection parameters
const uint16_t leScanInterval = 0x320;
const uint16_t leScanWindow = 0x320;
const uint16_t connectionMaxCeLength = 0x3e8;

// Connection Interval parameters
const uint16_t minConnectionEventLength = 0;
const uint16_t maxConnectionEventLength = 0x280;
const uint16_t maxConnectionEventLength = 0x280; // 400 ms

// Terminate connection
const uint8_t remoteUserTerminatedConnection = 0x13;
Expand Down
1 change: 1 addition & 0 deletions hal_st/middlewares/ble_middleware/GapSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ namespace hal
SVCCTL_Init();

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

void GapSt::SetAddress(const hal::MacAddress& address, services::GapDeviceAddressType addressType)
Expand Down
4 changes: 4 additions & 0 deletions hal_st/middlewares/ble_middleware/TracingGapCentralSt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ namespace hal
{
const auto dataLengthChangeEvent = *reinterpret_cast<hci_le_data_length_change_event_rp0*>(metaEvent->data);
tracer.Trace() << "TracingGapCentralSt::HandleHciLeDataLengthChangeEvent, Handle - 0x" << infra::hex << dataLengthChangeEvent.Connection_Handle;
tracer.Trace() << "\tMax TX octets : " << dataLengthChangeEvent.MaxTxOctets;
tracer.Trace() << "\tMax TX time : " << dataLengthChangeEvent.MaxTxTime;
tracer.Trace() << "\tMax RX octets : " << dataLengthChangeEvent.MaxRxOctets;
tracer.Trace() << "\tMax RX time : " << dataLengthChangeEvent.MaxRxTime;
GapCentralSt::HandleHciLeDataLengthChangeEvent(metaEvent);
}

Expand Down