Skip to content

Commit

Permalink
Support the new Zephyr multicast API
Browse files Browse the repository at this point in the history
  • Loading branch information
cguimaraes committed Jul 12, 2023
1 parent d1c4cdb commit 87c7fc5
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/system/zephyr/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,6 @@ size_t _z_send_udp_unicast(const _z_sys_net_socket_t sock, const uint8_t *ptr, s
#endif

#if Z_LINK_UDP_MULTICAST == 1
#if KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR > 3
#error \
"Zenoh UDP Multicast support is not yet supported in this Zephyr version. To continue with this Zephyr version, disable Z_LINK_UDP_MULTICAST"
#else
int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, _z_sys_net_endpoint_t *lep,
uint32_t tout, const char *iface) {
int8_t ret = _Z_RES_OK;
Expand Down Expand Up @@ -410,14 +406,22 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo
if (!mcast) {
ret = _Z_ERR_GENERIC;
}
#if KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR > 3
net_if_ipv4_maddr_join(net_if_get_default(), mcast);
#else
net_if_ipv4_maddr_join(mcast);
#endif
} else if (rep._iptcp->ai_family == AF_INET6) {
struct net_if_mcast_addr *mcast = NULL;
mcast = net_if_ipv6_maddr_add(ifa, &((struct sockaddr_in6 *)rep._iptcp->ai_addr)->sin6_addr);
if (!mcast) {
ret = _Z_ERR_GENERIC;
}
#if KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR > 3
net_if_ipv6_maddr_join(net_if_get_default(), mcast);
#else
net_if_ipv6_maddr_join(mcast);
#endif
} else {
ret = _Z_ERR_GENERIC;
}
Expand Down Expand Up @@ -445,15 +449,23 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *
if (rep._iptcp->ai_family == AF_INET) {
mcast = net_if_ipv4_maddr_add(ifa, &((struct sockaddr_in *)rep._iptcp->ai_addr)->sin_addr);
if (mcast != NULL) {
#if KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR > 3
net_if_ipv4_maddr_leave(net_if_get_default(), mcast);
#else
net_if_ipv4_maddr_leave(mcast);
#endif
net_if_ipv4_maddr_rm(ifa, &((struct sockaddr_in *)rep._iptcp->ai_addr)->sin_addr);
} else {
// Do nothing. The socket will be closed in any case.
}
} else if (rep._iptcp->ai_family == AF_INET6) {
mcast = net_if_ipv6_maddr_add(ifa, &((struct sockaddr_in6 *)rep._iptcp->ai_addr)->sin6_addr);
if (mcast != NULL) {
#if KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR > 3
net_if_ipv6_maddr_leave(net_if_get_default(), mcast);
#else
net_if_ipv6_maddr_leave(mcast);
#endif
net_if_ipv6_maddr_rm(ifa, &((struct sockaddr_in6 *)rep._iptcp->ai_addr)->sin6_addr);
} else {
// Do nothing. The socket will be closed in any case.
Expand Down Expand Up @@ -537,7 +549,6 @@ size_t _z_send_udp_multicast(const _z_sys_net_socket_t sock, const uint8_t *ptr,
_z_sys_net_endpoint_t rep) {
return sendto(sock._fd, ptr, len, 0, rep._iptcp->ai_addr, rep._iptcp->ai_addrlen);
}
#endif // KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR > 3
#endif // Z_LINK_UDP_MULTICAST == 1

#if Z_LINK_SERIAL == 1
Expand Down

0 comments on commit 87c7fc5

Please sign in to comment.