feat(zephyr): add the POSIX functions used by MAVLink

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é <benjamin@zephyrproject.org>
This commit is contained in:
Benjamin Cabé authored and Ramon Roche committed 2026-11-30 11:57:50 -08:00
1 parent 250b9b7e28
commit c97b970722
9 files changed
+488 -3

No files matched your search

+46
View File
@@ -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 <arpa/inet.h>
#ifdef __cplusplus
extern "C" {
#endif
int inet_aton(const char *cp, struct in_addr *inp);
#ifdef __cplusplus
}
#endif
+41
View File
@@ -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 <dirent.h>
// 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
+40
View File
@@ -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 <fcntl.h>
#ifndef O_NOCTTY
#define O_NOCTTY 0
#endif
+40
View File
@@ -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 <netinet/in.h>
#ifndef INADDR_LOOPBACK
#define INADDR_LOOPBACK ((in_addr_t)0x7f000001)
#endif
+6 -3
View File
@@ -33,12 +33,15 @@
#pragma once
// <zephyr/kernel.h> 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 <pthread.h>
#if defined(CONFIG_NET_SOCKETS)
#include <zephyr/net/socket.h>
#endif
#undef device
#else
#include_next <pthread.h>
+52
View File
@@ -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
+150
View File
@@ -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 <stdint.h>
#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
+14
View File
@@ -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
@@ -35,15 +35,24 @@
#include <px4_daemon/server_io.h>
#include <sys/boardctl.h>
#include <sys/statfs.h>
#include <sys/termios.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <zephyr/fs/fs.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/reboot.h>
#if defined(CONFIG_NET_SOCKETS)
#include <arpa/inet.h>
#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;