feat(mavlink): build MAVLink on Zephyr

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é <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 c97b970722
commit 240898b79d
6 files changed
+93 -15

No files matched your search

+64
View File
@@ -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 <dirent.h>
#include <limits.h>
#include <stdio.h>
#include <sys/stat.h>
/**
* 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
}
+2 -1
View File
@@ -46,6 +46,7 @@
#include <errno.h>
#include <cstring>
#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: {
+4 -3
View File
@@ -31,6 +31,7 @@
*
****************************************************************************/
#include "mavlink_dirent.h"
#include "mavlink_log_handler.h"
#include "mavlink_main.h"
#include <dirent.h>
@@ -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)) {
+13 -1
View File
@@ -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);
+3 -3
View File
@@ -70,7 +70,7 @@
#include <lib/drivers/device/Device.hpp> // For DeviceId union
#include <containers/LockGuard.hpp>
#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
+7 -7
View File
@@ -51,9 +51,9 @@
#include <nshlib/nshlib.h>
#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 <asm/socket.h>
@@ -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");