Skip to content

Commit

Permalink
[nrf noup] Make Wi-Fi manager use Wi-Fi interface only
Browse files Browse the repository at this point in the history
Find the Wi-Fi interface at the Wi-Fi manager initialization
and use that interface instead of the default interface when
calling Wi-Fi management functions.

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
  • Loading branch information
Damian-Nordic committed Nov 24, 2023
1 parent 3a3405f commit e3c0f65
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/platform/Zephyr/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
const net_if * const iface = InetUtils::GetInterface();
VerifyOrReturnError(iface != nullptr && iface->if_dev != nullptr, CHIP_ERROR_INTERNAL);
const net_if * const iface = InetUtils::GetWiFiInterface();
VerifyOrReturnError(iface != nullptr, CHIP_ERROR_INTERNAL);

const auto linkAddrStruct = iface->if_dev->link_addr;
memcpy(buf, linkAddrStruct.addr, linkAddrStruct.len);
Expand Down
11 changes: 9 additions & 2 deletions src/platform/Zephyr/InetUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

#include "InetUtils.h"

#include <zephyr/net/net_if.h>

namespace chip {
namespace DeviceLayer {
namespace InetUtils {

in6_addr ToZephyrAddr(const chip::Inet::IPAddress & address)
in6_addr ToZephyrAddr(const Inet::IPAddress & address)
{
in6_addr zephyrAddr;

Expand All @@ -31,11 +33,16 @@ in6_addr ToZephyrAddr(const chip::Inet::IPAddress & address)
return zephyrAddr;
}

net_if * GetInterface(chip::Inet::InterfaceId ifaceId)
net_if * GetInterface(Inet::InterfaceId ifaceId)
{
return ifaceId.IsPresent() ? net_if_get_by_index(ifaceId.GetPlatformInterface()) : net_if_get_default();
}

net_if * GetWiFiInterface()
{
return net_if_get_first_wifi();
}

} // namespace InetUtils
} // namespace DeviceLayer
} // namespace chip
5 changes: 3 additions & 2 deletions src/platform/Zephyr/InetUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ namespace chip {
namespace DeviceLayer {
namespace InetUtils {

in6_addr ToZephyrAddr(const chip::Inet::IPAddress & address);
net_if * GetInterface(chip::Inet::InterfaceId ifaceId = chip::Inet::InterfaceId::Null());
in6_addr ToZephyrAddr(const Inet::IPAddress & address);
net_if * GetInterface(Inet::InterfaceId ifaceId);
net_if * GetWiFiInterface();

} // namespace InetUtils
} // namespace DeviceLayer
Expand Down
59 changes: 24 additions & 35 deletions src/platform/nrfconnect/wifi/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ const Map<uint32_t, WiFiManager::NetEventHandler, 5>

void WiFiManager::WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface)
{
if (0 == strcmp(iface->if_dev->dev->name, "wlan0"))
if (iface == Instance().mNetIf)
{
Platform::UniquePtr<uint8_t> eventData(new uint8_t[cb->info_length]);
VerifyOrReturn(eventData);
Expand All @@ -158,6 +158,9 @@ void WiFiManager::WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mg

CHIP_ERROR WiFiManager::Init()
{
mNetIf = InetUtils::GetWiFiInterface();
VerifyOrReturnError(mNetIf != nullptr, INET_ERROR_UNKNOWN_INTERFACE);

// TODO: consider moving these to ConnectivityManagerImpl to be prepared for handling multiple interfaces on a single device.
Inet::UDPEndPointImplSockets::SetJoinMulticastGroupHandler([](Inet::InterfaceId interfaceId, const Inet::IPAddress & address) {
const in6_addr addr = InetUtils::ToZephyrAddr(address);
Expand Down Expand Up @@ -197,17 +200,14 @@ CHIP_ERROR WiFiManager::Init()
CHIP_ERROR WiFiManager::Scan(const ByteSpan & ssid, ScanResultCallback resultCallback, ScanDoneCallback doneCallback,
bool internalScan)
{
net_if * iface = InetUtils::GetInterface();
VerifyOrReturnError(nullptr != iface, CHIP_ERROR_INTERNAL);

mInternalScan = internalScan;
mScanResultCallback = resultCallback;
mScanDoneCallback = doneCallback;
mCachedWiFiState = mWiFiState;
mWiFiState = WIFI_STATE_SCANNING;
mSsidFound = false;

if (0 != net_mgmt(NET_REQUEST_WIFI_SCAN, iface, NULL, 0))
if (0 != net_mgmt(NET_REQUEST_WIFI_SCAN, mNetIf, NULL, 0))
{
ChipLogError(DeviceLayer, "Scan request failed");
return CHIP_ERROR_INTERNAL;
Expand Down Expand Up @@ -248,11 +248,8 @@ CHIP_ERROR WiFiManager::Connect(const ByteSpan & ssid, const ByteSpan & credenti

CHIP_ERROR WiFiManager::Disconnect()
{
net_if * iface = InetUtils::GetInterface();
VerifyOrReturnError(nullptr != iface, CHIP_ERROR_INTERNAL);

mApplicationDisconnectRequested = true;
int status = net_mgmt(NET_REQUEST_WIFI_DISCONNECT, iface, NULL, 0);
int status = net_mgmt(NET_REQUEST_WIFI_DISCONNECT, mNetIf, NULL, 0);

if (status)
{
Expand All @@ -277,11 +274,9 @@ CHIP_ERROR WiFiManager::Disconnect()

CHIP_ERROR WiFiManager::GetWiFiInfo(WiFiInfo & info) const
{
net_if * iface = InetUtils::GetInterface();
VerifyOrReturnError(nullptr != iface, CHIP_ERROR_INTERNAL);
struct wifi_iface_status status = { 0 };
wifi_iface_status status = { 0 };

if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, iface, &status, sizeof(struct wifi_iface_status)))
if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, mNetIf, &status, sizeof(wifi_iface_status)))
{
ChipLogError(DeviceLayer, "Status request failed");
return CHIP_ERROR_INTERNAL;
Expand All @@ -306,7 +301,7 @@ CHIP_ERROR WiFiManager::GetWiFiInfo(WiFiInfo & info) const
CHIP_ERROR WiFiManager::GetNetworkStatistics(NetworkStatistics & stats) const
{
net_stats_wifi data{};
net_mgmt(NET_REQUEST_STATS_GET_WIFI, InetUtils::GetInterface(), &data, sizeof(data));
net_mgmt(NET_REQUEST_STATS_GET_WIFI, mNetIf, &data, sizeof(data));

stats.mPacketMulticastRxCount = data.multicast.rx;
stats.mPacketMulticastTxCount = data.multicast.tx;
Expand All @@ -321,7 +316,7 @@ CHIP_ERROR WiFiManager::GetNetworkStatistics(NetworkStatistics & stats) const
void WiFiManager::ScanResultHandler(Platform::UniquePtr<uint8_t> data)
{
// Contrary to other handlers, offload accumulating of the scan results from the CHIP thread to the caller's thread
const struct wifi_scan_result * scanResult = reinterpret_cast<const struct wifi_scan_result *>(data.get());
const wifi_scan_result * scanResult = reinterpret_cast<const wifi_scan_result *>(data.get());

if (Instance().mInternalScan &&
Instance().mWantedNetwork.GetSsidSpan().data_equal(ByteSpan(scanResult->ssid, scanResult->ssid_length)))
Expand Down Expand Up @@ -402,10 +397,9 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr<uint8_t> data)
}

Instance().mWiFiState = WIFI_STATE_ASSOCIATING;
net_if * iface = InetUtils::GetInterface();
VerifyOrReturn(nullptr != iface, CHIP_ERROR_INTERNAL);

if (net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &(Instance().mWiFiParams.mParams), sizeof(wifi_connect_req_params)))
if (net_mgmt(NET_REQUEST_WIFI_CONNECT, Instance().mNetIf, &(Instance().mWiFiParams.mParams),
sizeof(wifi_connect_req_params)))
{
ChipLogError(DeviceLayer, "Connection request failed");
if (Instance().mHandling.mOnConnectionFailed)
Expand All @@ -430,20 +424,16 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr<uint8_t> data)

void WiFiManager::SendRouterSolicitation(System::Layer * layer, void * param)
{
net_if * iface = InetUtils::GetInterface();
if (iface && iface->if_dev->link_addr.type == NET_LINK_ETHERNET)
net_if_start_rs(Instance().mNetIf);
Instance().mRouterSolicitationCounter++;
if (Instance().mRouterSolicitationCounter < kRouterSolicitationMaxCount)
{
net_if_start_rs(iface);
Instance().mRouterSolicitationCounter++;
if (Instance().mRouterSolicitationCounter < kRouterSolicitationMaxCount)
{
DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(kRouterSolicitationIntervalMs),
SendRouterSolicitation, nullptr);
}
else
{
Instance().mRouterSolicitationCounter = 0;
}
DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(kRouterSolicitationIntervalMs), SendRouterSolicitation,
nullptr);
}
else
{
Instance().mRouterSolicitationCounter = 0;
}
}

Expand Down Expand Up @@ -572,11 +562,10 @@ System::Clock::Milliseconds32 WiFiManager::CalculateNextRecoveryTime()

CHIP_ERROR WiFiManager::SetLowPowerMode(bool onoff)
{
net_if * iface = InetUtils::GetInterface();
VerifyOrReturnError(nullptr != iface, CHIP_ERROR_INTERNAL);
VerifyOrReturnError(nullptr != mNetIf, CHIP_ERROR_INTERNAL);

wifi_ps_config currentConfig{};
if (net_mgmt(NET_REQUEST_WIFI_PS_CONFIG, iface, &currentConfig, sizeof(currentConfig)))
if (net_mgmt(NET_REQUEST_WIFI_PS_CONFIG, mNetIf, &currentConfig, sizeof(currentConfig)))
{
ChipLogError(DeviceLayer, "Get current low power mode config request failed");
return CHIP_ERROR_INTERNAL;
Expand All @@ -586,7 +575,7 @@ CHIP_ERROR WiFiManager::SetLowPowerMode(bool onoff)
(currentConfig.ps_params.enabled == WIFI_PS_DISABLED && onoff == true))
{
wifi_ps_params params{ .enabled = onoff ? WIFI_PS_ENABLED : WIFI_PS_DISABLED };
if (net_mgmt(NET_REQUEST_WIFI_PS, iface, &params, sizeof(params)))
if (net_mgmt(NET_REQUEST_WIFI_PS, mNetIf, &params, sizeof(params)))
{
ChipLogError(DeviceLayer, "Set low power mode request failed");
return CHIP_ERROR_INTERNAL;
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 @@ -220,6 +220,7 @@ class WiFiManager
void ResetRecoveryTime();
System::Clock::Milliseconds32 CalculateNextRecoveryTime();

net_if * mNetIf{ nullptr };
ConnectionParams mWiFiParams{};
ConnectionHandling mHandling;
wifi_iface_state mWiFiState;
Expand Down

0 comments on commit e3c0f65

Please sign in to comment.