Skip to content

Commit

Permalink
[nrfconnect][zephyr] Improvements and Fixes for WiFi according to NCS…
Browse files Browse the repository at this point in the history
… 2.6.0 (project-chip#32711)

* [nrfconnect] wifi: avoid unwanted connect request

It's pointless to issue a connect request in case
no valid SSID has been found.

Signed-off-by: Marcin Kajor <[email protected]>

* [nrfconnect] wifi: Fix 5GHz association

Wi-Fi stack recently introduced a check for valid band value and the
default value of 0 (memset) means only 2.4GHz, so, 5GHz Wi-Fi
associations will fail.

Fix the default to Unknown to scan all supported bands.

* [zephyr][nrfconnect] Make Wi-Fi manager use Wi-Fi interface only

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 <[email protected]>

* [nrfconnect] fix handling of LastNetworkID in Wi-Fi driver

This commit makes sure that correct Network ID is provided to the
Network Commissioning cluster from the platform's Wi-Fi driver.

Signed-off-by: Łukasz Duda <[email protected]>

* [inet] Combine platform handlers for joining/leaving mcast group

Instead, use a single handler for both joining and leaving
a multicast group to reduce the code duplication.

Signed-off-by: Damian Krolik <[email protected]>

* [zephyr][nrfconnect] Move handler for joining/leaving mcast group

Move the platform handler for joining and leaving a multicast
group to ConnectivityManagerImpl to support Matter stack on
a system with multiple network interfaces (Thread + Wi-Fi).

Signed-off-by: Damian Krolik <[email protected]>

* [nrfconnect] Added DNS server refresh after adding new IPv6 address

The Wi-Fi device does not update mDNS queries after obtaining
new IPv6 GUA address, so for some time after assigning prefix,
the Thread Border Routers still use cached link-local address,
which is not routable.

Signed-off-by: Kamil Kasperczyk <[email protected]>

* [nrfconnect] [zephyr] Disable synchronous printk

Disable synchronous printk to avoid blocking IRQs which
may affect time sensitive components (like 15.4 radio).

Signed-off-by: Marcin Kajor <[email protected]>

* [nrfconnect] Fix various Wi-Fi issues with error code handling

This commit handles a few issues with Wi-Fi connection or scanning:
 - Use wifi_status structure instead of incompatible WiFiRequestStatus
 - On connect error value > 2 do not report success
 - On scan error value > 1 do not report success
 - Provide value of mandatory LastConnectErrorValue attribute

Signed-off-by: Łukasz Duda <[email protected]>

* [nrfconnect] Minor Wi-Fi refinements

* error code handling unification
* added GetWantedNetwork getter
  and use it when handling network status change
* minor refactoring

Signed-off-by: Marcin Kajor <[email protected]>

* Restyled by clang-format

* [nrfconnect] Provide a workaround for nrfconnect Posix unit tests.

We need to disable all dependencies to the Zephyr net_if module until
we switch unit tests to it.

* Restyled by gn

* Use Enum to indicate an operation instead of bool in MulticastGroupHandler

---------

Signed-off-by: Marcin Kajor <[email protected]>
Signed-off-by: Damian Krolik <[email protected]>
Signed-off-by: Łukasz Duda <[email protected]>
Signed-off-by: Kamil Kasperczyk <[email protected]>
Co-authored-by: Marcin Kajor <[email protected]>
Co-authored-by: Damian Krolik <[email protected]>
Co-authored-by: Łukasz Duda <[email protected]>
Co-authored-by: Kamil Kasperczyk <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
6 people authored Apr 2, 2024
1 parent 6c89640 commit 78160d1
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 206 deletions.
4 changes: 3 additions & 1 deletion config/zephyr/chip-module/Kconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ config LOG_DEFAULT_LEVEL

endif

# disable synchronous printk to avoid blocking IRQs which
# may affect time sensitive components
config PRINTK_SYNC
bool
default y
default n

config ASSERT
bool
Expand Down
8 changes: 3 additions & 5 deletions src/inet/UDPEndPointImplSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ CHIP_ERROR IPv4Bind(int socket, const IPAddress & address, uint16_t port)
} // anonymous namespace

#if CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API
UDPEndPointImplSockets::MulticastGroupHandler UDPEndPointImplSockets::sJoinMulticastGroupHandler;
UDPEndPointImplSockets::MulticastGroupHandler UDPEndPointImplSockets::sLeaveMulticastGroupHandler;
UDPEndPointImplSockets::MulticastGroupHandler UDPEndPointImplSockets::sMulticastGroupHandler;
#endif // CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API

CHIP_ERROR UDPEndPointImplSockets::BindImpl(IPAddressType addressType, const IPAddress & addr, uint16_t port, InterfaceId interface)
Expand Down Expand Up @@ -801,10 +800,9 @@ CHIP_ERROR UDPEndPointImplSockets::IPv4JoinLeaveMulticastGroupImpl(InterfaceId a
CHIP_ERROR UDPEndPointImplSockets::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join)
{
#if CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API
MulticastGroupHandler handler = join ? sJoinMulticastGroupHandler : sLeaveMulticastGroupHandler;
if (handler != nullptr)
if (sMulticastGroupHandler != nullptr)
{
return handler(aInterfaceId, aAddress);
return sMulticastGroupHandler(aInterfaceId, aAddress, MulticastOperation::kJoin);
}
#endif // CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API

Expand Down
15 changes: 10 additions & 5 deletions src/inet/UDPEndPointImplSockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,18 @@ class UDPEndPointImplSockets : public UDPEndPoint, public EndPointStateSockets

#if CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API
public:
using MulticastGroupHandler = CHIP_ERROR (*)(InterfaceId, const IPAddress &);
static void SetJoinMulticastGroupHandler(MulticastGroupHandler handler) { sJoinMulticastGroupHandler = handler; }
static void SetLeaveMulticastGroupHandler(MulticastGroupHandler handler) { sLeaveMulticastGroupHandler = handler; }
enum class MulticastOperation
{
kJoin,
kLeave
};

using MulticastGroupHandler = CHIP_ERROR (*)(InterfaceId, const IPAddress &, MulticastOperation operation);

static void SetMulticastGroupHandler(MulticastGroupHandler handler) { sMulticastGroupHandler = handler; }

private:
static MulticastGroupHandler sJoinMulticastGroupHandler;
static MulticastGroupHandler sLeaveMulticastGroupHandler;
static MulticastGroupHandler sMulticastGroupHandler;
#endif // CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API
};

Expand Down
4 changes: 2 additions & 2 deletions src/platform/Zephyr/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,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 = Inet::InterfaceId::Null());
net_if * GetWiFiInterface();

} // namespace InetUtils
} // namespace DeviceLayer
Expand Down
23 changes: 0 additions & 23 deletions src/platform/Zephyr/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@
#include <platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp>
#include <platform/Zephyr/ThreadStackManagerImpl.h>

#include <inet/UDPEndPointImpl.h>
#include <lib/support/CodeUtils.h>
#include <platform/OpenThread/OpenThreadUtils.h>
#include <platform/ThreadStackManager.h>

namespace chip {
namespace DeviceLayer {

using namespace ::chip::DeviceLayer::Internal;
using namespace ::chip::Inet;

ThreadStackManagerImpl ThreadStackManagerImpl::sInstance;

Expand All @@ -46,26 +43,6 @@ CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack()

ReturnErrorOnFailure(GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>::DoInit(instance));

UDPEndPointImplSockets::SetJoinMulticastGroupHandler([](InterfaceId, const IPAddress & address) {
const otIp6Address otAddress = ToOpenThreadIP6Address(address);

ThreadStackMgr().LockThreadStack();
const auto otError = otIp6SubscribeMulticastAddress(openthread_get_default_instance(), &otAddress);
ThreadStackMgr().UnlockThreadStack();

return MapOpenThreadError(otError);
});

UDPEndPointImplSockets::SetLeaveMulticastGroupHandler([](InterfaceId, const IPAddress & address) {
const otIp6Address otAddress = ToOpenThreadIP6Address(address);

ThreadStackMgr().LockThreadStack();
const auto otError = otIp6UnsubscribeMulticastAddress(openthread_get_default_instance(), &otAddress);
ThreadStackMgr().UnlockThreadStack();

return MapOpenThreadError(otError);
});

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
k_sem_init(&mSrpClearAllSemaphore, 0, 1);
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
Expand Down
48 changes: 24 additions & 24 deletions src/platform/Zephyr/wifi/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,33 +159,33 @@ void WiFiManager::WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mg
CHIP_ERROR WiFiManager::Init()
{
// 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);
net_if * iface = InetUtils::GetInterface(interfaceId);
VerifyOrReturnError(iface != nullptr, INET_ERROR_UNKNOWN_INTERFACE);
Inet::UDPEndPointImplSockets::SetMulticastGroupHandler(
[](Inet::InterfaceId interfaceId, const Inet::IPAddress & address, UDPEndPointImplSockets::MulticastOperation operation) {
const in6_addr addr = InetUtils::ToZephyrAddr(address);
net_if * iface = InetUtils::GetInterface(interfaceId);
VerifyOrReturnError(iface != nullptr, INET_ERROR_UNKNOWN_INTERFACE);

net_if_mcast_addr * maddr = net_if_ipv6_maddr_add(iface, &addr);

if (maddr && !net_if_ipv6_maddr_is_joined(maddr) && !net_ipv6_is_addr_mcast_link_all_nodes(&addr))
{
net_if_ipv6_maddr_join(iface, maddr);
}

return CHIP_NO_ERROR;
});

Inet::UDPEndPointImplSockets::SetLeaveMulticastGroupHandler([](Inet::InterfaceId interfaceId, const Inet::IPAddress & address) {
const in6_addr addr = InetUtils::ToZephyrAddr(address);
net_if * iface = InetUtils::GetInterface(interfaceId);
VerifyOrReturnError(iface != nullptr, INET_ERROR_UNKNOWN_INTERFACE);
if (operation == UDPEndPointImplSockets::MulticastOperation::kJoin)
{
net_if_mcast_addr * maddr = net_if_ipv6_maddr_add(iface, &addr);

if (!net_ipv6_is_addr_mcast_link_all_nodes(&addr) && !net_if_ipv6_maddr_rm(iface, &addr))
{
return CHIP_ERROR_INVALID_ADDRESS;
}
if (maddr && !net_if_ipv6_maddr_is_joined(maddr) && !net_ipv6_is_addr_mcast_link_all_nodes(&addr))
{
net_if_ipv6_maddr_join(iface, maddr);
}
}
else if (operation == UDPEndPointImplSockets::MulticastOperation::kLeave)
{
VerifyOrReturnError(net_ipv6_is_addr_mcast_link_all_nodes(&addr) || net_if_ipv6_maddr_rm(iface, &addr),
CHIP_ERROR_INVALID_ADDRESS);
}
else
{
return CHIP_ERROR_INCORRECT_STATE;
}

return CHIP_NO_ERROR;
});
return CHIP_NO_ERROR;
});

net_mgmt_init_event_callback(&mWiFiMgmtClbk, WifiMgmtEventHandler, kWifiManagementEvents);
net_mgmt_add_event_callback(&mWiFiMgmtClbk);
Expand Down
9 changes: 7 additions & 2 deletions src/platform/nrfconnect/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ static_library("nrfconnect") {
]
}

if (chip_enable_openthread || chip_enable_wifi) {
sources += [
"../Zephyr/InetUtils.cpp",
"../Zephyr/InetUtils.h",
]
}

if (chip_enable_openthread) {
sources += [
"../OpenThread/OpenThreadUtils.cpp",
Expand All @@ -96,8 +103,6 @@ static_library("nrfconnect") {

if (chip_enable_wifi) {
sources += [
"../Zephyr/InetUtils.cpp",
"../Zephyr/InetUtils.h",
"OTAImageProcessorImplWiFi.h",
"wifi/ConnectivityManagerImplWiFi.cpp",
"wifi/ConnectivityManagerImplWiFi.h",
Expand Down
81 changes: 76 additions & 5 deletions src/platform/nrfconnect/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <platform/ConnectivityManager.h>
#include <platform/internal/BLEManager.h>

#include <inet/UDPEndPointImplSockets.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/ConnectivityManager.h>
#include <platform/Zephyr/InetUtils.h>
#include <platform/internal/BLEManager.h>

#include <platform/internal/GenericConnectivityManagerImpl_UDP.ipp>

Expand All @@ -34,16 +35,64 @@
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <platform/OpenThread/OpenThreadUtils.h>
#include <platform/internal/GenericConnectivityManagerImpl_Thread.ipp>
#endif

using namespace ::chip;
using namespace ::chip::TLV;
using namespace ::chip::Inet;
using namespace ::chip::DeviceLayer::Internal;

namespace chip {
namespace DeviceLayer {

namespace {
CHIP_ERROR JoinLeaveMulticastGroup(net_if * iface, const Inet::IPAddress & address,
UDPEndPointImplSockets::MulticastOperation operation)
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
if (net_if_l2(iface) == &NET_L2_GET_NAME(OPENTHREAD))
{
const otIp6Address otAddress = ToOpenThreadIP6Address(address);
const auto handler = operation == UDPEndPointImplSockets::MulticastOperation::kJoin ? otIp6SubscribeMulticastAddress
: otIp6UnsubscribeMulticastAddress;
otError error;

ThreadStackMgr().LockThreadStack();
error = handler(openthread_get_default_instance(), &otAddress);
ThreadStackMgr().UnlockThreadStack();

return MapOpenThreadError(error);
}
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
// The following code should also be valid for other interface types, such as Ethernet,
// but they are not officially supported, so for now enable it for Wi-Fi only.
const in6_addr in6Addr = InetUtils::ToZephyrAddr(address);

if (operation == UDPEndPointImplSockets::MulticastOperation::kJoin)
{
net_if_mcast_addr * maddr = net_if_ipv6_maddr_add(iface, &in6Addr);

if (maddr && !net_if_ipv6_maddr_is_joined(maddr))
{
net_if_ipv6_maddr_join(iface, maddr);
}
}
else if (operation == UDPEndPointImplSockets::MulticastOperation::kLeave)
{
VerifyOrReturnError(net_if_ipv6_maddr_rm(iface, &in6Addr), CHIP_ERROR_INVALID_ADDRESS);
}
else
{
return CHIP_ERROR_INCORRECT_STATE;
}
#endif

return CHIP_NO_ERROR;
}
} // namespace

ConnectivityManagerImpl ConnectivityManagerImpl::sInstance;

CHIP_ERROR ConnectivityManagerImpl::_Init()
Expand All @@ -54,6 +103,28 @@ CHIP_ERROR ConnectivityManagerImpl::_Init()
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
ReturnErrorOnFailure(InitWiFi());
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_WIFI
UDPEndPointImplSockets::SetMulticastGroupHandler(
[](InterfaceId interfaceId, const IPAddress & address, UDPEndPointImplSockets::MulticastOperation operation) {
if (interfaceId.IsPresent())
{
net_if * iface = InetUtils::GetInterface(interfaceId);
VerifyOrReturnError(iface != nullptr, INET_ERROR_UNKNOWN_INTERFACE);

return JoinLeaveMulticastGroup(iface, address, operation);
}

// If the interface is not specified, join or leave the multicast group on all interfaces.
for (int i = 1; net_if * iface = net_if_get_by_index(i); i++)
{
ReturnErrorOnFailure(JoinLeaveMulticastGroup(iface, address, operation));
}

return CHIP_NO_ERROR;
});
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_WIFI

return CHIP_NO_ERROR;
}

Expand Down
Loading

0 comments on commit 78160d1

Please sign in to comment.