From c97b970722b926c6f879d998f424e58b738ef2ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Cab=C3=A9?= Date: Sat, 10 Oct 2026 08:37:45 +0000 Subject: [PATCH] feat(zephyr): add the POSIX functions used by MAVLink MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MAVLink and the commander checks use POSIX functions and definitions that Zephyr does not provide: - access(), truncate() and statfs(), implemented on top of the Zephyr file systems. access() only checks that the file exists. - The terminal interface. Zephyr has no terminal devices, so tcgetattr(), tcsetattr(), tcflush() and tcdrain() fail with ENOTTY. - inet_aton(), INADDR_LOOPBACK, O_NOCTTY and the d_type values of directory entries. The Zephyr networking headers also declare struct device, so they are included with it renamed in C++ code, as the kernel headers are. Assisted-by: Claude:opus-5.5 Signed-off-by: Benjamin Cabé --- platforms/zephyr/include/arpa/inet.h | 46 ++++++ platforms/zephyr/include/dirent.h | 41 +++++ platforms/zephyr/include/fcntl.h | 40 +++++ platforms/zephyr/include/netinet/in.h | 40 +++++ platforms/zephyr/include/pthread.h | 9 +- platforms/zephyr/include/sys/statfs.h | 52 ++++++ platforms/zephyr/include/sys/termios.h | 150 ++++++++++++++++++ platforms/zephyr/include/unistd.h | 14 ++ .../zephyr/src/px4/common/px4_zephyr_impl.cpp | 99 ++++++++++++ 9 files changed, 488 insertions(+), 3 deletions(-) create mode 100644 platforms/zephyr/include/arpa/inet.h create mode 100644 platforms/zephyr/include/dirent.h create mode 100644 platforms/zephyr/include/fcntl.h create mode 100644 platforms/zephyr/include/netinet/in.h create mode 100644 platforms/zephyr/include/sys/statfs.h create mode 100644 platforms/zephyr/include/sys/termios.h diff --git a/platforms/zephyr/include/arpa/inet.h b/platforms/zephyr/include/arpa/inet.h new file mode 100644 index 00000000000..98dedee8a80 --- /dev/null +++ b/platforms/zephyr/include/arpa/inet.h @@ -0,0 +1,46 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#pragma once + +#include_next + +#ifdef __cplusplus +extern "C" { +#endif + +int inet_aton(const char *cp, struct in_addr *inp); + +#ifdef __cplusplus +} +#endif diff --git a/platforms/zephyr/include/dirent.h b/platforms/zephyr/include/dirent.h new file mode 100644 index 00000000000..b98b883e9c7 --- /dev/null +++ b/platforms/zephyr/include/dirent.h @@ -0,0 +1,41 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#pragma once + +#include_next + +// struct dirent has no d_type on Zephyr: these are the values of stat() based checks +#define DT_UNKNOWN 0 +#define DT_DIR 4 +#define DT_REG 8 diff --git a/platforms/zephyr/include/fcntl.h b/platforms/zephyr/include/fcntl.h new file mode 100644 index 00000000000..2b00e9f0d5d --- /dev/null +++ b/platforms/zephyr/include/fcntl.h @@ -0,0 +1,40 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#pragma once + +#include_next + +#ifndef O_NOCTTY +#define O_NOCTTY 0 +#endif diff --git a/platforms/zephyr/include/netinet/in.h b/platforms/zephyr/include/netinet/in.h new file mode 100644 index 00000000000..250c10b9aef --- /dev/null +++ b/platforms/zephyr/include/netinet/in.h @@ -0,0 +1,40 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#pragma once + +#include_next + +#ifndef INADDR_LOOPBACK +#define INADDR_LOOPBACK ((in_addr_t)0x7f000001) +#endif diff --git a/platforms/zephyr/include/pthread.h b/platforms/zephyr/include/pthread.h index 442edf527f7..797e6bc398b 100644 --- a/platforms/zephyr/include/pthread.h +++ b/platforms/zephyr/include/pthread.h @@ -33,12 +33,15 @@ #pragma once -// is included here, and its struct device conflicts with namespace device in C++. -// As visibility.h includes this header first, PX4 C++ files always see struct zephyr_device, and can't -// use the Zephyr device drivers: C files can. +// The Zephyr headers that declare struct device, the kernel and networking ones, are included here, as it conflicts +// with namespace device in C++. As visibility.h includes this header first, PX4 C++ files always see struct +// zephyr_device, and can't use the Zephyr device drivers: C files can. #ifdef __cplusplus #define device zephyr_device #include_next +#if defined(CONFIG_NET_SOCKETS) +#include +#endif #undef device #else #include_next diff --git a/platforms/zephyr/include/sys/statfs.h b/platforms/zephyr/include/sys/statfs.h new file mode 100644 index 00000000000..8050dcecb1b --- /dev/null +++ b/platforms/zephyr/include/sys/statfs.h @@ -0,0 +1,52 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#pragma once +#pragma GCC system_header + +#ifdef __cplusplus +extern "C" { +#endif + +struct statfs { + unsigned long f_bsize; + unsigned long f_blocks; + unsigned long f_bfree; + unsigned long f_bavail; +}; + +int statfs(const char *path, struct statfs *buf); + +#ifdef __cplusplus +} +#endif diff --git a/platforms/zephyr/include/sys/termios.h b/platforms/zephyr/include/sys/termios.h new file mode 100644 index 00000000000..01270f8f96e --- /dev/null +++ b/platforms/zephyr/include/sys/termios.h @@ -0,0 +1,150 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file termios.h + * + * Terminal interface. Zephyr has no terminal devices: tcgetattr(), + * tcsetattr(), tcflush() and tcdrain() fail with ENOTTY. + */ + +#pragma once +#pragma GCC system_header + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef uint32_t tcflag_t; +typedef uint8_t cc_t; +typedef uint32_t speed_t; + +#define NCCS 12 + +struct termios { + tcflag_t c_iflag; + tcflag_t c_oflag; + tcflag_t c_cflag; + tcflag_t c_lflag; + cc_t c_cc[NCCS]; + speed_t c_ispeed; + speed_t c_ospeed; +}; + +/* c_cc */ +#define VMIN 0 +#define VTIME 1 + +/* c_iflag */ +#define IGNBRK (1 << 0) +#define BRKINT (1 << 1) +#define IGNPAR (1 << 2) +#define PARMRK (1 << 3) +#define INPCK (1 << 4) +#define ISTRIP (1 << 5) +#define INLCR (1 << 6) +#define IGNCR (1 << 7) +#define ICRNL (1 << 8) +#define IXON (1 << 9) +#define IXOFF (1 << 10) + +/* c_oflag */ +#define OPOST (1 << 0) +#define ONLCR (1 << 1) + +/* c_cflag */ +#define CSIZE (3 << 0) +#define CS5 (0 << 0) +#define CS6 (1 << 0) +#define CS7 (2 << 0) +#define CS8 (3 << 0) +#define CSTOPB (1 << 2) +#define CREAD (1 << 3) +#define PARENB (1 << 4) +#define PARODD (1 << 5) +#define HUPCL (1 << 6) +#define CLOCAL (1 << 7) +#define CRTSCTS (1 << 8) + +/* c_lflag */ +#define ISIG (1 << 0) +#define ICANON (1 << 1) +#define ECHO (1 << 2) +#define ECHONL (1 << 3) +#define IEXTEN (1 << 4) + +/* tcsetattr() */ +#define TCSANOW 0 +#define TCSADRAIN 1 +#define TCSAFLUSH 2 + +/* tcflush() */ +#define TCIFLUSH 0 +#define TCOFLUSH 1 +#define TCIOFLUSH 2 + +/* the speeds are the baud rates */ +#define B0 0 +#define B50 50 +#define B75 75 +#define B110 110 +#define B134 134 +#define B150 150 +#define B200 200 +#define B300 300 +#define B600 600 +#define B1200 1200 +#define B1800 1800 +#define B2400 2400 +#define B4800 4800 +#define B9600 9600 +#define B19200 19200 +#define B38400 38400 +#define B57600 57600 +#define B115200 115200 +#define B230400 230400 + +speed_t cfgetispeed(const struct termios *termios_p); +speed_t cfgetospeed(const struct termios *termios_p); +int cfsetispeed(struct termios *termios_p, speed_t speed); +int cfsetospeed(struct termios *termios_p, speed_t speed); +int tcdrain(int fd); +int tcflush(int fd, int queue_selector); +int tcgetattr(int fd, struct termios *termios_p); +int tcsetattr(int fd, int optional_actions, const struct termios *termios_p); + +#ifdef __cplusplus +} +#endif diff --git a/platforms/zephyr/include/unistd.h b/platforms/zephyr/include/unistd.h index 3d6725c2d2d..0560a42af24 100644 --- a/platforms/zephyr/include/unistd.h +++ b/platforms/zephyr/include/unistd.h @@ -37,4 +37,18 @@ #ifndef F_OK #define F_OK 0 +#define X_OK 1 +#define W_OK 2 +#define R_OK 4 +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +int access(const char *pathname, int mode); +int truncate(const char *path, off_t length); + +#ifdef __cplusplus +} #endif diff --git a/platforms/zephyr/src/px4/common/px4_zephyr_impl.cpp b/platforms/zephyr/src/px4/common/px4_zephyr_impl.cpp index 4fbf54c194d..1b25875ada3 100644 --- a/platforms/zephyr/src/px4/common/px4_zephyr_impl.cpp +++ b/platforms/zephyr/src/px4/common/px4_zephyr_impl.cpp @@ -35,15 +35,24 @@ #include #include +#include +#include #include +#include #include #include #include +#include #include +#include #include #include +#if defined(CONFIG_NET_SOCKETS) +#include +#endif + __BEGIN_DECLS long PX4_TICKS_PER_SEC = CONFIG_SYS_CLOCK_TICKS_PER_SEC; @@ -57,6 +66,96 @@ int boardctl(unsigned int cmd, uintptr_t arg) return -ENOTSUP; } +// only the existence of the file is checked +int access(const char *pathname, int mode) +{ + struct stat st; + return stat(pathname, &st); +} + +int truncate(const char *path, off_t length) +{ + const int fd = open(path, O_WRONLY); + + if (fd < 0) { + return -1; + } + + const int ret = ftruncate(fd, length); + close(fd); + return ret; +} + +#if defined(CONFIG_NET_SOCKETS) +int inet_aton(const char *cp, struct in_addr *inp) +{ + return inet_pton(AF_INET, cp, inp) == 1; +} +#endif + +int statfs(const char *path, struct statfs *buf) +{ + struct fs_statvfs stat; + const int ret = fs_statvfs(path, &stat); + + if (ret != 0) { + errno = -ret; + return -1; + } + + buf->f_bsize = stat.f_frsize; + buf->f_blocks = stat.f_blocks; + buf->f_bfree = stat.f_bfree; + buf->f_bavail = stat.f_bfree; + return 0; +} + +speed_t cfgetispeed(const struct termios *termios_p) +{ + return termios_p->c_ispeed; +} + +speed_t cfgetospeed(const struct termios *termios_p) +{ + return termios_p->c_ospeed; +} + +int cfsetispeed(struct termios *termios_p, speed_t speed) +{ + termios_p->c_ispeed = speed; + return 0; +} + +int cfsetospeed(struct termios *termios_p, speed_t speed) +{ + termios_p->c_ospeed = speed; + return 0; +} + +int tcdrain(int fd) +{ + errno = ENOTTY; + return -1; +} + +int tcflush(int fd, int queue_selector) +{ + errno = ENOTTY; + return -1; +} + +int tcgetattr(int fd, struct termios *termios_p) +{ + errno = ENOTTY; + return -1; +} + +int tcsetattr(int fd, int optional_actions, const struct termios *termios_p) +{ + errno = ENOTTY; + return -1; +} + int dprintf(int fd, const char *format, ...) { va_list args;