Skip to content

Commit

Permalink
Improve Zephyr version detection (#229) (#230)
Browse files Browse the repository at this point in the history
* Improve Zephyr version detection

* Support the new Zephyr multicast API

* Reuse ifa variable
  • Loading branch information
cguimaraes committed Jul 26, 2023
1 parent 37397bf commit 87e21ad
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion extra_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"-<system/mbed/>",
"-<system/unix/>",
"-<system/windows/>"]
CPPDEFINES = ["ZENOH_ZEPHYR", "ZENOH_PIO"]
CPPDEFINES = ["ZENOH_ZEPHYR"]

elif FRAMEWORK == 'arduino':
PLATFORM = env.get("PIOPLATFORM")
Expand Down
7 changes: 6 additions & 1 deletion include/zenoh-pico/system/platform/zephyr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
#ifndef ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H
#define ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H

#if defined(ZENOH_PIO)
#include <version.h>

#if KERNEL_VERSION_MAJOR == 2
#include <kernel.h>
#elif KERNEL_VERSION_MAJOR == 3
#include <zephyr/kernel.h>
#else
#pragma "This Zephyr version might not be supported."
#include <zephyr/kernel.h>
#endif

Expand Down
22 changes: 20 additions & 2 deletions src/system/zephyr/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//

#if defined(ZENOH_PIO)
#include <version.h>

#if KERNEL_VERSION_MAJOR == 2
#include <drivers/uart.h>
#else
#include <zephyr/drivers/uart.h>
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -439,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(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.
}
} 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.
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/system/zephyr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//

#if defined(ZENOH_PIO)
#include <kernel.h>
#include <version.h>

#if KERNEL_VERSION_MAJOR == 2
#include <random/rand32.h>
#else
#include <zephyr/kernel.h>
#include <zephyr/random/rand32.h>
#endif

Expand Down

0 comments on commit 87e21ad

Please sign in to comment.