From 87e21ad6005199a213bf0e597d01199b75c43b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 26 Jul 2023 11:56:35 +0200 Subject: [PATCH] Improve Zephyr version detection (#229) (#230) * Improve Zephyr version detection * Support the new Zephyr multicast API * Reuse ifa variable --- extra_script.py | 2 +- include/zenoh-pico/system/platform/zephyr.h | 7 ++++++- src/system/zephyr/network.c | 22 +++++++++++++++++++-- src/system/zephyr/system.c | 6 +++--- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/extra_script.py b/extra_script.py index c454607cc..391eb8047 100644 --- a/extra_script.py +++ b/extra_script.py @@ -28,7 +28,7 @@ "-", "-", "-"] - CPPDEFINES = ["ZENOH_ZEPHYR", "ZENOH_PIO"] + CPPDEFINES = ["ZENOH_ZEPHYR"] elif FRAMEWORK == 'arduino': PLATFORM = env.get("PIOPLATFORM") diff --git a/include/zenoh-pico/system/platform/zephyr.h b/include/zenoh-pico/system/platform/zephyr.h index ca3924e11..4b318c9d9 100644 --- a/include/zenoh-pico/system/platform/zephyr.h +++ b/include/zenoh-pico/system/platform/zephyr.h @@ -15,9 +15,14 @@ #ifndef ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H #define ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H -#if defined(ZENOH_PIO) +#include + +#if KERNEL_VERSION_MAJOR == 2 #include +#elif KERNEL_VERSION_MAJOR == 3 +#include #else +#pragma "This Zephyr version might not be supported." #include #endif diff --git a/src/system/zephyr/network.c b/src/system/zephyr/network.c index 0f34432d8..f0842dc08 100644 --- a/src/system/zephyr/network.c +++ b/src/system/zephyr/network.c @@ -12,7 +12,9 @@ // ZettaScale Zenoh Team, // -#if defined(ZENOH_PIO) +#include + +#if KERNEL_VERSION_MAJOR == 2 #include #else #include @@ -404,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(ifa, 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(ifa, mcast); +#else net_if_ipv6_maddr_join(mcast); +#endif } else { ret = _Z_ERR_GENERIC; } @@ -439,7 +449,11 @@ 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(ifa, 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. @@ -447,7 +461,11 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * } 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(ifa, 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. @@ -531,7 +549,7 @@ 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 +#endif // Z_LINK_UDP_MULTICAST == 1 #if Z_LINK_SERIAL == 1 int8_t _z_open_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, uint32_t rxpin, uint32_t baudrate) { diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index 37c2333f1..2670ae9cb 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -12,11 +12,11 @@ // ZettaScale Zenoh Team, // -#if defined(ZENOH_PIO) -#include +#include + +#if KERNEL_VERSION_MAJOR == 2 #include #else -#include #include #endif