diff --git a/src/inet/UDPEndPointImplSockets.cpp b/src/inet/UDPEndPointImplSockets.cpp index 963d360d8e96c3..23190c15846e88 100644 --- a/src/inet/UDPEndPointImplSockets.cpp +++ b/src/inet/UDPEndPointImplSockets.cpp @@ -802,7 +802,7 @@ CHIP_ERROR UDPEndPointImplSockets::IPv6JoinLeaveMulticastGroupImpl(InterfaceId a #if CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API if (sMulticastGroupHandler != nullptr) { - return sMulticastGroupHandler(aInterfaceId, aAddress, join); + return sMulticastGroupHandler(aInterfaceId, aAddress, MulticastOperation::kJoin); } #endif // CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API diff --git a/src/inet/UDPEndPointImplSockets.h b/src/inet/UDPEndPointImplSockets.h index 2080afd4790b5a..b4e16e859b04ed 100644 --- a/src/inet/UDPEndPointImplSockets.h +++ b/src/inet/UDPEndPointImplSockets.h @@ -63,7 +63,13 @@ class UDPEndPointImplSockets : public UDPEndPoint, public EndPointStateSockets #if CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API public: - using MulticastGroupHandler = CHIP_ERROR (*)(InterfaceId, const IPAddress &, bool /* join */); + enum class MulticastOperation + { + kJoin, + kLeave + }; + + using MulticastGroupHandler = CHIP_ERROR (*)(InterfaceId, const IPAddress &, MulticastOperation operation); static void SetMulticastGroupHandler(MulticastGroupHandler handler) { sMulticastGroupHandler = handler; } diff --git a/src/platform/Zephyr/wifi/WiFiManager.cpp b/src/platform/Zephyr/wifi/WiFiManager.cpp index 29238a26e4f639..69d2943235cdbd 100644 --- a/src/platform/Zephyr/wifi/WiFiManager.cpp +++ b/src/platform/Zephyr/wifi/WiFiManager.cpp @@ -160,12 +160,12 @@ CHIP_ERROR WiFiManager::Init() { // TODO: consider moving these to ConnectivityManagerImpl to be prepared for handling multiple interfaces on a single device. Inet::UDPEndPointImplSockets::SetMulticastGroupHandler( - [](Inet::InterfaceId interfaceId, const Inet::IPAddress & address, bool join) { + [](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); - if (join) + if (operation == UDPEndPointImplSockets::MulticastOperation::kJoin) { net_if_mcast_addr * maddr = net_if_ipv6_maddr_add(iface, &addr); @@ -174,11 +174,15 @@ CHIP_ERROR WiFiManager::Init() net_if_ipv6_maddr_join(iface, maddr); } } - else + 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; }); diff --git a/src/platform/nrfconnect/ConnectivityManagerImpl.cpp b/src/platform/nrfconnect/ConnectivityManagerImpl.cpp index a73c47d3bf25fe..ecd185200c78f7 100644 --- a/src/platform/nrfconnect/ConnectivityManagerImpl.cpp +++ b/src/platform/nrfconnect/ConnectivityManagerImpl.cpp @@ -46,13 +46,15 @@ namespace chip { namespace DeviceLayer { namespace { -CHIP_ERROR JoinLeaveMulticastGroup(net_if * iface, const Inet::IPAddress & address, bool join) +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 = join ? otIp6SubscribeMulticastAddress : otIp6UnsubscribeMulticastAddress; + const auto handler = operation == UDPEndPointImplSockets::MulticastOperation::kJoin ? otIp6SubscribeMulticastAddress + : otIp6UnsubscribeMulticastAddress; otError error; ThreadStackMgr().LockThreadStack(); @@ -68,7 +70,7 @@ CHIP_ERROR JoinLeaveMulticastGroup(net_if * iface, const Inet::IPAddress & addre // but they are not officially supported, so for now enable it for Wi-Fi only. const in6_addr in6Addr = InetUtils::ToZephyrAddr(address); - if (join) + if (operation == UDPEndPointImplSockets::MulticastOperation::kJoin) { net_if_mcast_addr * maddr = net_if_ipv6_maddr_add(iface, &in6Addr); @@ -77,10 +79,14 @@ CHIP_ERROR JoinLeaveMulticastGroup(net_if * iface, const Inet::IPAddress & addre net_if_ipv6_maddr_join(iface, maddr); } } - else + 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; @@ -99,23 +105,24 @@ CHIP_ERROR ConnectivityManagerImpl::_Init() #endif #if CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_WIFI - UDPEndPointImplSockets::SetMulticastGroupHandler([](InterfaceId interfaceId, const IPAddress & address, bool join) { - if (interfaceId.IsPresent()) - { - net_if * iface = InetUtils::GetInterface(interfaceId); - VerifyOrReturnError(iface != nullptr, INET_ERROR_UNKNOWN_INTERFACE); - - return JoinLeaveMulticastGroup(iface, address, join); - } - - // 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, join)); - } - - return CHIP_NO_ERROR; - }); + 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; diff --git a/src/platform/telink/ThreadStackManagerImpl.cpp b/src/platform/telink/ThreadStackManagerImpl.cpp index 79e30c39601521..2ec9b02ca0b67c 100644 --- a/src/platform/telink/ThreadStackManagerImpl.cpp +++ b/src/platform/telink/ThreadStackManagerImpl.cpp @@ -48,17 +48,19 @@ CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack() ReturnErrorOnFailure(GenericThreadStackManagerImpl_OpenThread::DoInit(instance)); - UDPEndPointImplSockets::SetMulticastGroupHandler([](InterfaceId, const IPAddress & address, bool join) { - const otIp6Address otAddress = ToOpenThreadIP6Address(address); - const auto handler = join ? otIp6SubscribeMulticastAddress : otIp6UnsubscribeMulticastAddress; - otError error; - - ThreadStackMgr().LockThreadStack(); - error = handler(openthread_get_default_instance(), &otAddress); - ThreadStackMgr().UnlockThreadStack(); - - return MapOpenThreadError(error); - }); + UDPEndPointImplSockets::SetMulticastGroupHandler( + [](InterfaceId, const IPAddress & address, UDPEndPointImplSockets::MulticastOperation operation) { + 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); + }); #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT k_sem_init(&mSrpClearAllSemaphore, 0, 1);