From 240898b79d33ff57b41cf573a20b6e13dc67bcea 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(mavlink): build MAVLink on Zephyr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zephyr is a POSIX platform for PX4, but MAVLink uses parts of POSIX that only Linux and macOS have: - The MAVLink shell runs the pxh shell, which Zephyr does not have. - The network interfaces cannot be listed with the socket API, so there is no broadcast address. - struct dirent has no d_type, so the type of a directory entry comes from stat(). - There is no dup2() or /dev/null to reopen the standard file descriptors with. The receiver thread kept an 8 kB buffer on its stack on POSIX, which is sized for Linux. Zephyr gets the buffer and the network stack margins of NuttX instead. Assisted-by: Claude:opus-5.5 Signed-off-by: Benjamin Cabé --- src/modules/mavlink/mavlink_dirent.h | 64 +++++++++++++++++++++ src/modules/mavlink/mavlink_ftp.cpp | 3 +- src/modules/mavlink/mavlink_log_handler.cpp | 7 ++- src/modules/mavlink/mavlink_main.cpp | 14 ++++- src/modules/mavlink/mavlink_receiver.cpp | 6 +- src/modules/mavlink/mavlink_shell.cpp | 14 ++--- 6 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 src/modules/mavlink/mavlink_dirent.h diff --git a/src/modules/mavlink/mavlink_dirent.h b/src/modules/mavlink/mavlink_dirent.h new file mode 100644 index 00000000000..61c7730e029 --- /dev/null +++ b/src/modules/mavlink/mavlink_dirent.h @@ -0,0 +1,64 @@ +/**************************************************************************** + * + * 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 mavlink_dirent.h + */ + +#pragma once + +#include +#include +#include +#include + +/** + * The type of a directory entry, as in d_type. + */ +static inline int mavlink_dirent_type(const char *dir, const struct dirent *entry) +{ +#if defined(__PX4_ZEPHYR) + // struct dirent has no d_type on Zephyr + char path[PATH_MAX]; + struct stat st; + const int ret = snprintf(path, sizeof(path), "%s/%s", dir, entry->d_name); + + if ((ret <= 0) || (ret >= (int)sizeof(path)) || (stat(path, &st) != 0)) { + return DT_UNKNOWN; + } + + return S_ISDIR(st.st_mode) ? DT_DIR : DT_REG; +#else + return entry->d_type; +#endif +} diff --git a/src/modules/mavlink/mavlink_ftp.cpp b/src/modules/mavlink/mavlink_ftp.cpp index 2fd5501486f..972ae4b1e26 100644 --- a/src/modules/mavlink/mavlink_ftp.cpp +++ b/src/modules/mavlink/mavlink_ftp.cpp @@ -46,6 +46,7 @@ #include #include +#include "mavlink_dirent.h" #include "mavlink_ftp.h" #include "mavlink_main.h" @@ -562,7 +563,7 @@ MavlinkFTP::_workList(PayloadHeader *payload, bool include_time) char direntType; // Determine the directory entry type - switch (result->d_type) { + switch (mavlink_dirent_type(_work_buffer1, result)) { #ifdef __PX4_NUTTX case DTYPE_FILE: { diff --git a/src/modules/mavlink/mavlink_log_handler.cpp b/src/modules/mavlink/mavlink_log_handler.cpp index 0689fb6ad29..f97d281b984 100644 --- a/src/modules/mavlink/mavlink_log_handler.cpp +++ b/src/modules/mavlink/mavlink_log_handler.cpp @@ -31,6 +31,7 @@ * ****************************************************************************/ +#include "mavlink_dirent.h" #include "mavlink_log_handler.h" #include "mavlink_main.h" #include @@ -451,7 +452,7 @@ bool MavlinkLogHandler::create_log_list_file() break; } - if (result->d_type != PX4LOG_DIRECTORY) { + if (mavlink_dirent_type(kLogDir, result) != PX4LOG_DIRECTORY) { // Skip stray files continue; } @@ -503,7 +504,7 @@ void MavlinkLogHandler::write_entries_to_file(FILE *fp, const char *dir) break; } - if (result->d_type != PX4LOG_REGULAR_FILE) { + if (mavlink_dirent_type(dir, result) != PX4LOG_REGULAR_FILE) { // Skip non files continue; } @@ -612,7 +613,7 @@ void MavlinkLogHandler::delete_all_logs(const char *dir, unsigned depth) continue; } - if (result->d_type == PX4LOG_DIRECTORY) { + if (mavlink_dirent_type(dir, result) == PX4LOG_DIRECTORY) { delete_all_logs(filepath, depth + 1); if (rmdir(filepath)) { diff --git a/src/modules/mavlink/mavlink_main.cpp b/src/modules/mavlink/mavlink_main.cpp index a1812168a0e..082c96c652a 100644 --- a/src/modules/mavlink/mavlink_main.cpp +++ b/src/modules/mavlink/mavlink_main.cpp @@ -81,7 +81,7 @@ #error The non-standard CRTSCTS define is incorrect. Fix this in the OS or replace with (CRTS_IFLOW | CCTS_OFLOW) #endif -#ifdef CONFIG_NET +#if defined(CONFIG_NET) || defined(CONFIG_NETWORKING) #define MAVLINK_NET_ADDED_STACK PX4_STACK_ADJUSTED(350) #else #define MAVLINK_NET_ADDED_STACK 0 @@ -1026,6 +1026,13 @@ void Mavlink::send_bytes(const uint8_t *buf, unsigned packet_len) } #ifdef MAVLINK_UDP +#if defined(__PX4_ZEPHYR) +void Mavlink::find_broadcast_address() +{ + // the network interfaces cannot be listed through the socket API + PX4_WARN("no broadcast address"); +} +#else void Mavlink::find_broadcast_address() { struct ifconf ifconf; @@ -1167,6 +1174,7 @@ const in_addr Mavlink::query_netmask_addr(const int socket_fd, const ifreq &ifre return ((struct sockaddr_in *)&netmask_ifreq.ifr_addr)->sin_addr; } +#endif const in_addr Mavlink::compute_broadcast_addr(const in_addr &host_addr, const in_addr &netmask_addr) { @@ -2378,6 +2386,8 @@ Mavlink::configure_streams_to_default(const char *configure_single_stream) int Mavlink::task_main(int argc, char *argv[]) { +#if !defined(__PX4_ZEPHYR) + // If stdin, stdout and/or stderr file descriptors (0, 1, 2) // are not open when mavlink module starts (as might be the case for USB auto-start), // use default /dev/null so that these numbers are not used by other other files. @@ -2409,6 +2419,8 @@ Mavlink::task_main(int argc, char *argv[]) } } +#endif + pthread_mutex_init(&_tstatus_mutex, nullptr); pthread_mutex_init(&_radio_status_mutex, nullptr); pthread_mutex_init(&_message_buffer_mutex, nullptr); diff --git a/src/modules/mavlink/mavlink_receiver.cpp b/src/modules/mavlink/mavlink_receiver.cpp index 22d648cfc31..e4e97292224 100644 --- a/src/modules/mavlink/mavlink_receiver.cpp +++ b/src/modules/mavlink/mavlink_receiver.cpp @@ -70,7 +70,7 @@ #include // For DeviceId union #include -#ifdef CONFIG_NET +#if defined(CONFIG_NET) || defined(CONFIG_NETWORKING) #define MAVLINK_RECEIVER_NET_ADDED_STACK 1360 #else #define MAVLINK_RECEIVER_NET_ADDED_STACK 0 @@ -3998,10 +3998,10 @@ MavlinkReceiver::run() // poll timeout in ms. Also defines the max update frequency of the mission & param manager, etc. const int timeout = 10; -#if defined(__PX4_POSIX) +#if defined(__PX4_POSIX) && !defined(__PX4_ZEPHYR) /* 1500 is the Wifi MTU, so we make sure to fit a full packet */ uint8_t buf[1600 * 5]; -#elif defined(CONFIG_NET) +#elif defined(CONFIG_NET) || defined(CONFIG_NETWORKING) /* 1500 is the Wifi MTU, so we make sure to fit a full packet */ uint8_t buf[1000]; #else diff --git a/src/modules/mavlink/mavlink_shell.cpp b/src/modules/mavlink/mavlink_shell.cpp index f3800627e89..14a13ed3899 100644 --- a/src/modules/mavlink/mavlink_shell.cpp +++ b/src/modules/mavlink/mavlink_shell.cpp @@ -51,9 +51,9 @@ #include #endif /* __PX4_NUTTX */ -#ifdef __PX4_POSIX +#if defined(__PX4_POSIX) && !defined(__PX4_ZEPHYR) #include "../../../platforms/posix/src/px4/common/px4_daemon/pxh.h" -#endif /* __PX4_POSIX */ +#endif #ifdef __PX4_CYGWIN #include @@ -79,10 +79,7 @@ MavlinkShell::~MavlinkShell() int MavlinkShell::start() { //this currently only works for NuttX & POSIX -#if !defined(__PX4_NUTTX) && !defined(__PX4_POSIX) - return -1; -#endif - +#if defined(__PX4_NUTTX) || (defined(__PX4_POSIX) && !defined(__PX4_ZEPHYR)) PX4_INFO("Starting mavlink shell"); int p1[2], p2[2]; @@ -182,6 +179,9 @@ int MavlinkShell::start() #endif /* __PX4_NUTTX */ return ret; +#else + return -1; +#endif } int MavlinkShell::shell_start_thread(int argc, char *argv[]) @@ -198,7 +198,7 @@ int MavlinkShell::shell_start_thread(int argc, char *argv[]) #endif /* __PX4_NUTTX */ -#ifdef __PX4_POSIX +#if defined(__PX4_POSIX) && !defined(__PX4_ZEPHYR) if (argc != 3) { PX4_ERR("Mavlink shell bug");