diff --git a/CMakeLists.txt b/CMakeLists.txt index c6633f070e9..01d24125b32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -179,6 +179,7 @@ define_property(GLOBAL PROPERTY PX4_SRC_FILES # include(px4_add_module) +include(px4_add_mavlink_extension) set(config_module_list) set(config_kernel_list) diff --git a/cmake/px4_add_mavlink_extension.cmake b/cmake/px4_add_mavlink_extension.cmake new file mode 100644 index 00000000000..7b3edbc5349 --- /dev/null +++ b/cmake/px4_add_mavlink_extension.cmake @@ -0,0 +1,146 @@ +############################################################################ +# +# 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. +# +############################################################################ + +include(px4_list_make_absolute) + +#============================================================================= +# +# px4_add_mavlink_extension +# +# Adds MAVLink message handlers and streams to the mavlink module from +# another module, without changing the mavlink module's sources. +# +# Each handler class gets one instance per MAVLink instance, constructed +# from that instance's Mavlink object, and its handle_message() receives +# every message the instance receives, after the mavlink module's own +# handlers. Each stream class joins the list of streams after the +# mavlink module's own, so the mavlink stream command and +# MAV_CMD_SET_MESSAGE_INTERVAL reach it by name or message ID like any +# other stream. +# +# The mavlink module reads the registrations when it is configured. A +# module configured after it can't register and stops the configuration; +# out-of-tree modules (EXTERNAL_MODULES_LOCATION) are configured first. +# +# SRCS compile as part of the mavlink module, which on NuttX joins its +# sources into one file in its build directory. Each source's own +# directory and the headers' directories join the module's include +# directories, so a source's quoted includes resolve there too; file-static +# and anonymous-namespace names in SRCS, and header names, must not collide +# with the module's own. +# +# Usage: +# px4_add_mavlink_extension( +# [HANDLER_HEADER ] +# [HANDLERS ...] +# [STREAM_HEADER ] +# [STREAMS ...] +# [SRCS ...] +# [INCLUDES ...] +# ) +# +# Input: +# HANDLER_HEADER : header declaring HANDLERS; it is included where +# Mavlink is only declared, so the handlers' +# methods are defined in SRCS +# HANDLERS : classes with a constructor taking Mavlink & and a +# void handle_message(const mavlink_message_t *msg) +# STREAM_HEADER : header defining STREAMS, included after the +# mavlink module's own streams +# STREAMS : MavlinkStream classes with get_name_static(), +# get_id_static() and new_instance(), like the +# streams in src/modules/mavlink/streams +# SRCS : sources compiled into the mavlink module +# INCLUDES : further include directories for those sources and +# headers, beyond their own directories +# +# Example: +# px4_add_mavlink_extension( +# HANDLER_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/MyHandler.hpp +# HANDLERS MyHandler +# STREAM_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/MY_MESSAGE.hpp +# STREAMS MavlinkStreamMyMessage +# SRCS ${CMAKE_CURRENT_SOURCE_DIR}/MyHandler.cpp +# ) +# +function(px4_add_mavlink_extension) + + px4_parse_function_args( + NAME px4_add_mavlink_extension + ONE_VALUE HANDLER_HEADER STREAM_HEADER + MULTI_VALUE HANDLERS STREAMS SRCS INCLUDES + ARGN ${ARGN}) + + get_property(configured GLOBAL PROPERTY PX4_MAVLINK_EXTENSIONS_CONFIGURED) + if(configured) + message(FATAL_ERROR "px4_add_mavlink_extension: the mavlink module is configured already, so it can't see this registration") + endif() + + if((HANDLERS AND NOT HANDLER_HEADER) OR (STREAMS AND NOT STREAM_HEADER)) + message(FATAL_ERROR "px4_add_mavlink_extension: HANDLERS need a HANDLER_HEADER and STREAMS a STREAM_HEADER") + endif() + + if(NOT HANDLERS AND NOT STREAMS) + message(FATAL_ERROR "px4_add_mavlink_extension: name at least one of HANDLERS or STREAMS") + endif() + + px4_list_make_absolute(SRCS ${CMAKE_CURRENT_SOURCE_DIR} ${SRCS}) + px4_list_make_absolute(INCLUDES ${CMAKE_CURRENT_SOURCE_DIR} ${INCLUDES}) + set(headers) + + if(HANDLERS) + get_filename_component(HANDLER_HEADER ${HANDLER_HEADER} ABSOLUTE) + set_property(GLOBAL APPEND PROPERTY PX4_MAVLINK_EXTENSION_HANDLER_HEADERS ${HANDLER_HEADER}) + set_property(GLOBAL APPEND PROPERTY PX4_MAVLINK_EXTENSION_HANDLERS ${HANDLERS}) + list(APPEND headers ${HANDLER_HEADER}) + endif() + + if(STREAMS) + get_filename_component(STREAM_HEADER ${STREAM_HEADER} ABSOLUTE) + set_property(GLOBAL APPEND PROPERTY PX4_MAVLINK_EXTENSION_STREAM_HEADERS ${STREAM_HEADER}) + set_property(GLOBAL APPEND PROPERTY PX4_MAVLINK_EXTENSION_STREAMS ${STREAMS}) + list(APPEND headers ${STREAM_HEADER}) + endif() + + # A unity build compiles the sources from the module's build directory, where their quoted + # includes would not find the files next to them + foreach(file ${SRCS} ${headers}) + get_filename_component(directory ${file} DIRECTORY) + list(APPEND INCLUDES ${directory}) + endforeach() + list(REMOVE_DUPLICATES INCLUDES) + + set_property(GLOBAL APPEND PROPERTY PX4_MAVLINK_EXTENSION_SRCS ${SRCS}) + set_property(GLOBAL APPEND PROPERTY PX4_MAVLINK_EXTENSION_INCLUDES ${INCLUDES}) + +endfunction() diff --git a/src/modules/mavlink/CMakeLists.txt b/src/modules/mavlink/CMakeLists.txt index a6d313fe456..0384fb0f14b 100644 --- a/src/modules/mavlink/CMakeLists.txt +++ b/src/modules/mavlink/CMakeLists.txt @@ -97,6 +97,30 @@ target_include_directories(mavlink_c ${MAVLINK_LIBRARY_DIR}/${MAVLINK_DIALECT_UAVIONIX} ) +# The handlers and streams modules add with px4_add_mavlink_extension(), as two headers the +# receiver and the stream list include; a later registration would go unseen, so it fails. +get_property(mavlink_extension_handler_headers GLOBAL PROPERTY PX4_MAVLINK_EXTENSION_HANDLER_HEADERS) +get_property(mavlink_extension_handlers GLOBAL PROPERTY PX4_MAVLINK_EXTENSION_HANDLERS) +get_property(mavlink_extension_stream_headers GLOBAL PROPERTY PX4_MAVLINK_EXTENSION_STREAM_HEADERS) +get_property(mavlink_extension_streams GLOBAL PROPERTY PX4_MAVLINK_EXTENSION_STREAMS) +get_property(mavlink_extension_srcs GLOBAL PROPERTY PX4_MAVLINK_EXTENSION_SRCS) +get_property(mavlink_extension_includes GLOBAL PROPERTY PX4_MAVLINK_EXTENSION_INCLUDES) +set_property(GLOBAL PROPERTY PX4_MAVLINK_EXTENSIONS_CONFIGURED TRUE) + +foreach(kind handler stream) + set(MAVLINK_EXTENSION_INCLUDES "") + foreach(header ${mavlink_extension_${kind}_headers}) + string(APPEND MAVLINK_EXTENSION_INCLUDES "#include \"${header}\"\n") + endforeach() + set(MAVLINK_EXTENSION_LIST "") + set(index 0) + foreach(class ${mavlink_extension_${kind}s}) + string(APPEND MAVLINK_EXTENSION_LIST " X(${class}, ${index})") + math(EXPR index "${index} + 1") + endforeach() + configure_file(mavlink_extension_${kind}s.h.in ${CMAKE_CURRENT_BINARY_DIR}/mavlink_extension_${kind}s.h @ONLY) +endforeach() + px4_add_module( MODULE modules__mavlink MAIN mavlink @@ -125,6 +149,11 @@ px4_add_module( MavlinkStatustextHandler.cpp open_drone_id_translations.cpp tune_publisher.cpp + ${mavlink_extension_srcs} + INCLUDES + ${CMAKE_CURRENT_SOURCE_DIR} # for the sources of extensions + ${CMAKE_CURRENT_BINARY_DIR} + ${mavlink_extension_includes} MODULE_CONFIG module.yaml mavlink_params.yaml @@ -147,6 +176,10 @@ px4_add_module( UNITY_BUILD ) +# mavlink_receiver.h includes the generated list of handlers, so whatever includes it, like the +# tests below, needs the directory that list is generated in, and the extensions' own directories +target_include_directories(modules__mavlink INTERFACE ${CMAKE_CURRENT_BINARY_DIR} ${mavlink_extension_includes}) + if(CONFIG_BOARD_HAS_RID_EEPROM) target_link_libraries(modules__mavlink PRIVATE open_drone_id) endif() diff --git a/src/modules/mavlink/mavlink_extension_handlers.h.in b/src/modules/mavlink/mavlink_extension_handlers.h.in new file mode 100644 index 00000000000..051b3f15b9b --- /dev/null +++ b/src/modules/mavlink/mavlink_extension_handlers.h.in @@ -0,0 +1,10 @@ +/* + * The MAVLink message handlers other modules add with px4_add_mavlink_extension(), generated by + * the mavlink module's CMakeLists.txt. MAVLINK_EXTENSION_HANDLERS(X) expands X(class, index) for + * each, with index counting the classes from 0. + */ + +#pragma once + +@MAVLINK_EXTENSION_INCLUDES@ +#define MAVLINK_EXTENSION_HANDLERS(X)@MAVLINK_EXTENSION_LIST@ diff --git a/src/modules/mavlink/mavlink_extension_streams.h.in b/src/modules/mavlink/mavlink_extension_streams.h.in new file mode 100644 index 00000000000..e644ddc49d3 --- /dev/null +++ b/src/modules/mavlink/mavlink_extension_streams.h.in @@ -0,0 +1,10 @@ +/* + * The MAVLink streams other modules add with px4_add_mavlink_extension(), generated by the mavlink + * module's CMakeLists.txt. MAVLINK_EXTENSION_STREAMS(X) expands X(class, index) for each, with + * index counting the classes from 0. + */ + +#pragma once + +@MAVLINK_EXTENSION_INCLUDES@ +#define MAVLINK_EXTENSION_STREAMS(X)@MAVLINK_EXTENSION_LIST@ diff --git a/src/modules/mavlink/mavlink_messages.cpp b/src/modules/mavlink/mavlink_messages.cpp index 399974798e1..50e94cde4ae 100644 --- a/src/modules/mavlink/mavlink_messages.cpp +++ b/src/modules/mavlink/mavlink_messages.cpp @@ -172,6 +172,9 @@ # include "streams/WHEEL_DISTANCE.hpp" #endif // !CONSTRAINED_FLASH +// The streams other modules add with px4_add_mavlink_extension() +#include "mavlink_extension_streams.h" + // ensure PX4 rotation enum and MAV_SENSOR_ROTATION align static_assert(MAV_SENSOR_ROTATION_NONE == static_cast(ROTATION_NONE), "Roll: 0, Pitch: 0, Yaw: 0"); @@ -547,6 +550,9 @@ static constexpr StreamListItem streams_list[] = { #if defined(WHEEL_DISTANCE_HPP) create_stream_list_item(), #endif // WHEEL_DISTANCE_HPP +#define MAVLINK_EXTENSION_STREAM_ITEM(stream, index) create_stream_list_item(), + MAVLINK_EXTENSION_STREAMS(MAVLINK_EXTENSION_STREAM_ITEM) +#undef MAVLINK_EXTENSION_STREAM_ITEM }; diff --git a/src/modules/mavlink/mavlink_receiver.cpp b/src/modules/mavlink/mavlink_receiver.cpp index 22d648cfc31..7b6c732d9a6 100644 --- a/src/modules/mavlink/mavlink_receiver.cpp +++ b/src/modules/mavlink/mavlink_receiver.cpp @@ -470,6 +470,11 @@ MavlinkReceiver::handle_message(mavlink_message_t *msg) /* handle packet with parent object */ _mavlink.handle_message(msg); + + /* handle packet with the handlers other modules add */ +#define MAVLINK_EXTENSION_HANDLER_CALL(handler, index) _extension_handler_##index.handle_message(msg); + MAVLINK_EXTENSION_HANDLERS(MAVLINK_EXTENSION_HANDLER_CALL) +#undef MAVLINK_EXTENSION_HANDLER_CALL } static bool gimbal_mode_command_allowed(uint16_t command) diff --git a/src/modules/mavlink/mavlink_receiver.h b/src/modules/mavlink/mavlink_receiver.h index 3ff788b6ed1..5affc0c9855 100644 --- a/src/modules/mavlink/mavlink_receiver.h +++ b/src/modules/mavlink/mavlink_receiver.h @@ -50,6 +50,7 @@ #include "MavlinkStatustextHandler.hpp" #include "mavlink_timesync.h" #include "tune_publisher.h" +#include "mavlink_extension_handlers.h" #include #include @@ -326,6 +327,12 @@ private: MavlinkTimesync _mavlink_timesync; MavlinkStatustextHandler _mavlink_statustext_handler; + // The message handlers other modules add with px4_add_mavlink_extension(), one of each per instance. + // They are named by index, as a class name can carry a namespace. +#define MAVLINK_EXTENSION_HANDLER_MEMBER(handler, index) handler _extension_handler_##index{_mavlink}; + MAVLINK_EXTENSION_HANDLERS(MAVLINK_EXTENSION_HANDLER_MEMBER) +#undef MAVLINK_EXTENSION_HANDLER_MEMBER + mavlink_status_t _status{}; ///< receiver status, used for mavlink_parse_char() orb_advert_t _mavlink_log_pub{nullptr};