From b54e8c58f38734de61d62217893ce71431207d19 Mon Sep 17 00:00:00 2001 From: Patryk Lipinski Date: Tue, 12 Mar 2024 11:50:38 +0100 Subject: [PATCH] [nrf noup] Directed network scan implementation Implementation of directed scaning, to adjust to newer matter version specification. Signed-off-by: Patryk Lipinski --- .../network-commissioning.cpp | 10 ++++++++ .../network-commissioning.h | 2 ++ src/platform/nrfconnect/wifi/WiFiManager.cpp | 25 ++++++++++++++++--- src/platform/nrfconnect/wifi/WiFiManager.h | 1 + 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/app/clusters/network-commissioning/network-commissioning.cpp b/src/app/clusters/network-commissioning/network-commissioning.cpp index d86a3183b2..5946b9fa4e 100644 --- a/src/app/clusters/network-commissioning/network-commissioning.cpp +++ b/src/app/clusters/network-commissioning/network-commissioning.cpp @@ -244,6 +244,8 @@ void Instance::OnNetworkingStatusChange(NetworkCommissioning::Status aCommission void Instance::HandleScanNetworks(HandlerContext & ctx, const Commands::ScanNetworks::DecodableType & req) { MATTER_TRACE_SCOPE("HandleScanNetwork", "NetworkCommissioning"); + + mScanningWasDirected = false; if (mFeatureFlags.Has(Feature::kWiFiNetworkInterface)) { ByteSpan ssid; @@ -266,6 +268,7 @@ void Instance::HandleScanNetworks(HandlerContext & ctx, const Commands::ScanNetw ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Protocols::InteractionModel::Status::InvalidCommand); return; } + mScanningWasDirected = !ssid.empty(); mCurrentOperationBreadcrumb = req.breadcrumb; mAsyncCommandHandle = CommandHandler::Handle(&ctx.mCommandHandler); ctx.mCommandHandler.FlushAcksRightAwayOnSlowCommand(); @@ -518,6 +521,13 @@ void Instance::OnFinished(Status status, CharSpan debugText, ThreadScanResponseI return; } + // If drivers are failing to respond NetworkNotFound on empty results, force it for them. + bool resultsMissing = !networks || (networks->Count() == 0); + if ((status == Status::kSuccess) && mScanningWasDirected && resultsMissing) + { + status = Status::kNetworkNotFound; + } + mLastNetworkingStatusValue.SetNonNull(status); TLV::TLVWriter * writer; diff --git a/src/app/clusters/network-commissioning/network-commissioning.h b/src/app/clusters/network-commissioning/network-commissioning.h index c822a3f888..43496db1a9 100644 --- a/src/app/clusters/network-commissioning/network-commissioning.h +++ b/src/app/clusters/network-commissioning/network-commissioning.h @@ -102,6 +102,8 @@ class Instance : public CommandHandlerInterface, Optional mCurrentOperationBreadcrumb; + bool mScanningWasDirected = false; + // Commits the breadcrumb value saved in mCurrentOperationBreadcrumb to the breadcrumb attribute in GeneralCommissioning // cluster. Will set mCurrentOperationBreadcrumb to NullOptional. void CommitSavedBreadcrumb(); diff --git a/src/platform/nrfconnect/wifi/WiFiManager.cpp b/src/platform/nrfconnect/wifi/WiFiManager.cpp index ce17308bf4..3684429f3e 100644 --- a/src/platform/nrfconnect/wifi/WiFiManager.cpp +++ b/src/platform/nrfconnect/wifi/WiFiManager.cpp @@ -188,6 +188,13 @@ CHIP_ERROR WiFiManager::Scan(const ByteSpan & ssid, ScanResultCallback resultCal mWiFiState = WIFI_STATE_SCANNING; mSsidFound = false; + if ((ssid.size() > 0) && (!mInternalScan)) + { + mDirectScanNetwork.Erase(); + memcpy(mDirectScanNetwork.ssid, ssid.data(), ssid.size()); + mDirectScanNetwork.ssidLen = ssid.size(); + } + if (0 != net_mgmt(NET_REQUEST_WIFI_SCAN, mNetIf, NULL, 0)) { ChipLogError(DeviceLayer, "Scan request failed"); @@ -334,7 +341,14 @@ void WiFiManager::ScanResultHandler(Platform::UniquePtr data) if (Instance().mScanResultCallback && !Instance().mInternalScan) { - Instance().mScanResultCallback(ToScanResponse(scanResult)); + if (Instance().mDirectScanNetwork.GetSsidSpan().size() == 0) + { + Instance().mScanResultCallback(ToScanResponse(scanResult)); + } + else if (Instance().mDirectScanNetwork.GetSsidSpan().data_equal(ByteSpan(scanResult->ssid, scanResult->ssid_length))) + { + Instance().mScanResultCallback(ToScanResponse(scanResult)); + } } } @@ -345,6 +359,11 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr data) uint8_t * rawData = safePtr.get(); const wifi_status * status = reinterpret_cast(rawData); + if (Instance().mDirectScanNetwork.GetSsidSpan().size() > 0) + { + Instance().mDirectScanNetwork.Erase(); + } + if (status->status) { ChipLogError(DeviceLayer, "Wi-Fi scan finalization failure (%d)", status->status); @@ -420,8 +439,8 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr data) { CHIP_ERROR err = SystemLayer().ScheduleLambda([capturedData = data.get()] { Platform::UniquePtr safePtr(capturedData); - uint8_t * rawData = safePtr.get(); - const wifi_status * status = reinterpret_cast(rawData); + uint8_t * rawData = safePtr.get(); + const wifi_status * status = reinterpret_cast(rawData); if (status->status) { diff --git a/src/platform/nrfconnect/wifi/WiFiManager.h b/src/platform/nrfconnect/wifi/WiFiManager.h index 9ddb5dea27..fdd5d11c55 100644 --- a/src/platform/nrfconnect/wifi/WiFiManager.h +++ b/src/platform/nrfconnect/wifi/WiFiManager.h @@ -226,6 +226,7 @@ class WiFiManager ScanResultCallback mScanResultCallback{ nullptr }; ScanDoneCallback mScanDoneCallback{ nullptr }; WiFiNetwork mWantedNetwork{}; + WiFiNetwork mDirectScanNetwork{}; bool mInternalScan{ false }; uint8_t mRouterSolicitationCounter = 0; bool mSsidFound{ false };