Skip to content

Commit

Permalink
[nrf noup] Directed network scan implementation
Browse files Browse the repository at this point in the history
Implementation of directed scaning, to adjust
to newer matter version specification.

Signed-off-by: Patryk Lipinski <patryk.lipinski@nordicsemi.no>
  • Loading branch information
LipinskiPNordicSemi committed Mar 12, 2024
1 parent 7653e6c commit b54e8c5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/app/clusters/network-commissioning/network-commissioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class Instance : public CommandHandlerInterface,

Optional<uint64_t> 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();
Expand Down
25 changes: 22 additions & 3 deletions src/platform/nrfconnect/wifi/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -334,7 +341,14 @@ void WiFiManager::ScanResultHandler(Platform::UniquePtr<uint8_t> 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));
}
}
}

Expand All @@ -345,6 +359,11 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr<uint8_t> data)
uint8_t * rawData = safePtr.get();
const wifi_status * status = reinterpret_cast<const wifi_status *>(rawData);

if (Instance().mDirectScanNetwork.GetSsidSpan().size() > 0)
{
Instance().mDirectScanNetwork.Erase();
}

if (status->status)
{
ChipLogError(DeviceLayer, "Wi-Fi scan finalization failure (%d)", status->status);
Expand Down Expand Up @@ -420,8 +439,8 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr<uint8_t> data)
{
CHIP_ERROR err = SystemLayer().ScheduleLambda([capturedData = data.get()] {
Platform::UniquePtr<uint8_t> safePtr(capturedData);
uint8_t * rawData = safePtr.get();
const wifi_status * status = reinterpret_cast<const wifi_status *>(rawData);
uint8_t * rawData = safePtr.get();
const wifi_status * status = reinterpret_cast<const wifi_status *>(rawData);

if (status->status)
{
Expand Down
1 change: 1 addition & 0 deletions src/platform/nrfconnect/wifi/WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit b54e8c5

Please sign in to comment.