Skip to content

Commit

Permalink
Use Enum to indicate an operation instead of bool in MulticastGroupHa…
Browse files Browse the repository at this point in the history
…ndler
  • Loading branch information
ArekBalysNordic committed Mar 27, 2024
1 parent 72e7235 commit 44ecb4e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/inet/UDPEndPointImplSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion src/inet/UDPEndPointImplSockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
10 changes: 7 additions & 3 deletions src/platform/Zephyr/wifi/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
});
Expand Down
49 changes: 28 additions & 21 deletions src/platform/nrfconnect/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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;
Expand Down
24 changes: 13 additions & 11 deletions src/platform/telink/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack()

ReturnErrorOnFailure(GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>::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);
Expand Down

0 comments on commit 44ecb4e

Please sign in to comment.