From ffaae36e5f7c3a48eaf6745f076fc9c6167991eb Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Wed, 21 Oct 2026 14:38:56 +1300 Subject: [PATCH] feat(mavlink): support 32-bit system IDs Update the MAVLink submodule to a version with 32-bit system IDs and allow MAV_SYS_ID up to 2^31-1. An ID above 255 forces MAVLink 2. Targets above 255 travel in a header extension while the 8-bit payload field only holds 255. Every target check now goes through mavlink_target_system(), and messages addressed to a system are sent with mavlink_msg_send_struct_target() so a wide target is not truncated. System IDs inside message payloads, such as for the gimbal protocol, are still 8 bit, so ground stations should stay at 255 or below. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Julian Oes --- docs/en/mavlink/receiving_messages.md | 7 ++ .../serialpassthrough/serialpassthrough.cpp | 4 +- .../serialpassthrough/serialpassthrough.hpp | 6 +- src/modules/mavlink/mavlink | 2 +- src/modules/mavlink/mavlink_bridge_header.h | 31 +++++++ .../mavlink/mavlink_command_sender.cpp | 14 ++-- src/modules/mavlink/mavlink_command_sender.h | 3 +- src/modules/mavlink/mavlink_events.cpp | 3 +- src/modules/mavlink/mavlink_ftp.cpp | 29 +++---- src/modules/mavlink/mavlink_ftp.h | 11 +-- src/modules/mavlink/mavlink_log_handler.cpp | 10 +-- src/modules/mavlink/mavlink_main.cpp | 75 +++++++---------- src/modules/mavlink/mavlink_main.h | 6 +- src/modules/mavlink/mavlink_mission.cpp | 68 +++++++-------- src/modules/mavlink/mavlink_mission.h | 10 +-- src/modules/mavlink/mavlink_parameters.cpp | 23 ++--- src/modules/mavlink/mavlink_parameters.h | 2 +- src/modules/mavlink/mavlink_params.yaml | 11 ++- src/modules/mavlink/mavlink_receiver.cpp | 84 ++++++++++++------- src/modules/mavlink/mavlink_receiver.h | 10 +-- src/modules/mavlink/mavlink_shell.h | 6 +- src/modules/mavlink/mavlink_timesync.cpp | 5 +- src/modules/mavlink/mavlink_ulog.cpp | 13 ++- src/modules/mavlink/mavlink_ulog.h | 6 +- .../mavlink/streams/CAMERA_TRIGGER.hpp | 4 +- src/modules/mavlink/streams/COMMAND_LONG.hpp | 4 +- .../mavlink/streams/MANUAL_CONTROL.hpp | 7 +- 27 files changed, 250 insertions(+), 204 deletions(-) diff --git a/docs/en/mavlink/receiving_messages.md b/docs/en/mavlink/receiving_messages.md index cc1315b28f7..1b76e445827 100644 --- a/docs/en/mavlink/receiving_messages.md +++ b/docs/en/mavlink/receiving_messages.md @@ -70,6 +70,13 @@ Above we only write to the battery fields that are defined in the topic. In practice you'd update all fields with either valid or invalid values: this has been cut back for brevity. ::: +::: tip +If the message has a `target_system` field, don't compare the decoded field with `mavlink_system.sysid` directly. +MAVLink system IDs can be 32 bit: a target above 255 is sent in a header extension and the 8-bit payload field only holds 255. +Use `mavlink_target_system(msg, decoded.target_system)` to get the full target instead. +When sending a message to a system, use `mavlink_msg_send_struct_target()` instead of `mavlink_msg__send_struct()` so a target above 255 is not truncated. +::: + and finally make sure it is called in [MavlinkReceiver::handle_message()](https://github.com/PX4/PX4-Autopilot/blob/main/src/modules/mavlink/mavlink_receiver.cpp#L228) ```cpp diff --git a/src/drivers/serialpassthrough/serialpassthrough.cpp b/src/drivers/serialpassthrough/serialpassthrough.cpp index eb0c4d7a314..28e67069bb4 100644 --- a/src/drivers/serialpassthrough/serialpassthrough.cpp +++ b/src/drivers/serialpassthrough/serialpassthrough.cpp @@ -267,7 +267,7 @@ int SerialPassthrough::run_trampoline(int argc, char *argv[]) } void SerialPassthrough::pushFromMavlink(const uint8_t *data, size_t len, - uint8_t sysid, uint8_t compid, uint8_t device, + uint32_t sysid, uint8_t compid, uint8_t device, uint8_t channel) { // Store reply metadata @@ -296,7 +296,7 @@ void SerialPassthrough::pushFromMavlink(const uint8_t *data, size_t len, } size_t SerialPassthrough::popToMavlink(uint8_t *buf, size_t max_len, - uint8_t *out_sysid, uint8_t *out_compid, uint8_t *out_device, + uint32_t *out_sysid, uint8_t *out_compid, uint8_t *out_device, uint8_t channel) { pthread_mutex_lock(&_meta_mutex); diff --git a/src/drivers/serialpassthrough/serialpassthrough.hpp b/src/drivers/serialpassthrough/serialpassthrough.hpp index 15af295cc4c..bb5920f1d7d 100644 --- a/src/drivers/serialpassthrough/serialpassthrough.hpp +++ b/src/drivers/serialpassthrough/serialpassthrough.hpp @@ -110,7 +110,7 @@ public: * channel: the MAVLink channel index (0-based) this arrived on. */ void pushFromMavlink(const uint8_t *data, size_t len, - uint8_t sysid, uint8_t compid, uint8_t device, + uint32_t sysid, uint8_t compid, uint8_t device, uint8_t channel); /** @@ -119,7 +119,7 @@ public: * Only returns data if channel matches the channel that sent the request. */ size_t popToMavlink(uint8_t *buf, size_t max_len, - uint8_t *out_sysid, uint8_t *out_compid, uint8_t *out_device, + uint32_t *out_sysid, uint8_t *out_compid, uint8_t *out_device, uint8_t channel); bool should_exit() const { return _should_exit.load(); } @@ -154,7 +154,7 @@ private: pthread_mutex_t _tx_mutex; // Reply metadata (sysid/compid/device/channel of last received message) - uint8_t _target_sysid{0}; + uint32_t _target_sysid{0}; uint8_t _target_compid{0}; uint8_t _device_id{0}; uint8_t _tx_channel{255}; // MAVLink channel index to reply on (255 = not set) diff --git a/src/modules/mavlink/mavlink b/src/modules/mavlink/mavlink index 1b1e896aa3b..2e2b6af9908 160000 --- a/src/modules/mavlink/mavlink +++ b/src/modules/mavlink/mavlink @@ -1 +1 @@ -Subproject commit 1b1e896aa3be0d591166e086e6b9d8613bd03716 +Subproject commit 2e2b6af99084e4fde9bb304c84bb9253a1a98b6f diff --git a/src/modules/mavlink/mavlink_bridge_header.h b/src/modules/mavlink/mavlink_bridge_header.h index 01f21d99824..f80f3b88bff 100644 --- a/src/modules/mavlink/mavlink_bridge_header.h +++ b/src/modules/mavlink/mavlink_bridge_header.h @@ -104,6 +104,37 @@ extern mavlink_message_t *mavlink_get_channel_buffer(uint8_t chan); #include #include +/** + * Full target system of a received message. + * + * A target above 255 travels in the TARGET32 header extension and the 8-bit + * payload field only holds a placeholder (255). Always check the target with + * this instead of the decoded payload field. + */ +static inline uint32_t mavlink_target_system(const mavlink_message_t *msg, uint8_t payload_target_system) +{ + return (msg->incompat_flags & MAVLINK_IFLAG_TARGET32) ? msg->target_sysid : payload_target_system; +} + __END_DECLS +#if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS +#error "mavlink_msg_send_struct_target() sends the struct as is" +#endif + +/** + * Like mavlink_msg__send_struct(), but keeps a target system above 255. + * + * send_struct() only has the 8-bit payload field, so a wider target would be + * truncated. This puts the target into the TARGET32 header extension instead. + */ +#define mavlink_msg_send_struct_target(chan, NAME, packet, target) \ + do { \ + const uint32_t _target = (target); \ + (packet)->target_system = mavlink_msg_target_field(_target); \ + _mav_finalize_message_chan_send_target((chan), MAVLINK_MSG_ID_##NAME, (const char *)(packet), \ + MAVLINK_MSG_ID_##NAME##_MIN_LEN, MAVLINK_MSG_ID_##NAME##_LEN, \ + MAVLINK_MSG_ID_##NAME##_CRC, _target); \ + } while (0) + #endif /* MAVLINK_BRIDGE_HEADER_H */ diff --git a/src/modules/mavlink/mavlink_command_sender.cpp b/src/modules/mavlink/mavlink_command_sender.cpp index e9916b72f41..0c608043559 100644 --- a/src/modules/mavlink/mavlink_command_sender.cpp +++ b/src/modules/mavlink/mavlink_command_sender.cpp @@ -95,7 +95,6 @@ int MavlinkCommandSender::handle_vehicle_command(const vehicle_command_s &comman CMD_DEBUG("new command: %" PRIu32 " (channel: %d)", command.command, channel); mavlink_command_long_t msg = {}; - msg.target_system = command.target_system; msg.target_component = command.target_component; msg.command = command.command; msg.confirmation = command.confirmation; @@ -106,7 +105,7 @@ int MavlinkCommandSender::handle_vehicle_command(const vehicle_command_s &comman msg.param5 = command.param5; msg.param6 = command.param6; msg.param7 = command.param7; - mavlink_msg_command_long_send_struct(channel, &msg); + mavlink_msg_send_struct_target(channel, COMMAND_LONG, &msg, command.target_system); bool already_existing = false; _commands.reset_to_start(); @@ -125,6 +124,7 @@ int MavlinkCommandSender::handle_vehicle_command(const vehicle_command_s &comman command_item_s new_item; new_item.command = msg; + new_item.target_system = command.target_system; new_item.timestamp_us = command.timestamp; new_item.num_sent_per_channel[channel] = 0; new_item.last_time_sent_us = hrt_absolute_time(); @@ -136,9 +136,9 @@ int MavlinkCommandSender::handle_vehicle_command(const vehicle_command_s &comman } void MavlinkCommandSender::handle_mavlink_command_ack(const mavlink_command_ack_t &ack, - uint8_t from_sysid, uint8_t from_compid, uint8_t channel) + uint32_t from_sysid, uint8_t from_compid, uint8_t channel) { - CMD_DEBUG("handling result %" PRIu8 " for command %" PRIu16 " (from %" PRIu8 ":%" PRIu8 ")", + CMD_DEBUG("handling result %" PRIu8 " for command %" PRIu16 " (from %" PRIu32 ":%" PRIu8 ")", ack.result, ack.command, from_sysid, from_compid); lock(); @@ -147,7 +147,7 @@ void MavlinkCommandSender::handle_mavlink_command_ack(const mavlink_command_ack_ while (command_item_s *item = _commands.get_next()) { // Check if the incoming ack matches any of the commands that we have sent. if (item->command.command == ack.command && - (item->command.target_system == 0 || from_sysid == item->command.target_system) && + (item->target_system == 0 || from_sysid == item->target_system) && (item->command.target_component == 0 || from_compid == item->command.target_component) && item->num_sent_per_channel[channel] >= 0) { item->num_sent_per_channel[channel] = -2; // mark this as acknowledged @@ -221,7 +221,7 @@ void MavlinkCommandSender::check_timeout(mavlink_channel_t channel) if (item->num_sent_per_channel[channel] < max_sent && item->num_sent_per_channel[channel] != -1) { // We are behind and need to do a retransmission. item->command.confirmation = ++item->num_sent_per_channel[channel]; - mavlink_msg_command_long_send_struct(channel, &item->command); + mavlink_msg_send_struct_target(channel, COMMAND_LONG, &item->command, item->target_system); CMD_DEBUG("command %" PRIu16 " sent (not first, retries: %" PRIu8 "/%" PRIi8 ", channel: %d)", item->command.command, @@ -242,7 +242,7 @@ void MavlinkCommandSender::check_timeout(mavlink_channel_t channel) // We are the first of a new retransmission series. item->command.confirmation = ++item->num_sent_per_channel[channel]; - mavlink_msg_command_long_send_struct(channel, &item->command); + mavlink_msg_send_struct_target(channel, COMMAND_LONG, &item->command, item->target_system); // Therefore, we are the ones setting the timestamp of this retry round. item->last_time_sent_us = hrt_absolute_time(); diff --git a/src/modules/mavlink/mavlink_command_sender.h b/src/modules/mavlink/mavlink_command_sender.h index f8f8ebfa953..3c8166eaa5c 100644 --- a/src/modules/mavlink/mavlink_command_sender.h +++ b/src/modules/mavlink/mavlink_command_sender.h @@ -80,7 +80,7 @@ public: * Handle mavlink command_ack. * thread-safe */ - void handle_mavlink_command_ack(const mavlink_command_ack_t &ack, uint8_t from_sysid, uint8_t from_compid, + void handle_mavlink_command_ack(const mavlink_command_ack_t &ack, uint32_t from_sysid, uint8_t from_compid, uint8_t channel); private: @@ -110,6 +110,7 @@ private: struct command_item_s { mavlink_command_long_t command = {}; + uint32_t target_system = 0; ///< command only holds the 8-bit payload field hrt_abstime timestamp_us = 0; hrt_abstime last_time_sent_us = 0; // -1: channel did not request this command to be sent, -2: channel got an ack for this command diff --git a/src/modules/mavlink/mavlink_events.cpp b/src/modules/mavlink/mavlink_events.cpp index 135cf154d2e..9cfb5cbb918 100644 --- a/src/modules/mavlink/mavlink_events.cpp +++ b/src/modules/mavlink/mavlink_events.cpp @@ -195,13 +195,12 @@ void SendProtocol::handle_request_event(const mavlink_message_t &msg) const } else { mavlink_response_event_error_t event_error{}; - event_error.target_system = msg.sysid; event_error.target_component = msg.compid; event_error.sequence = sequence; event_error.sequence_oldest_available = _buffer.get_oldest_sequence_after(sequence); event_error.reason = MAV_EVENT_ERROR_REASON_UNAVAILABLE; PX4_DEBUG("Event unavailable (seq=%i oldest=%i)", sequence, event_error.sequence_oldest_available); - mavlink_msg_response_event_error_send_struct(_mavlink.get_channel(), &event_error); + mavlink_msg_send_struct_target(_mavlink.get_channel(), RESPONSE_EVENT_ERROR, &event_error, msg.sysid); } } } diff --git a/src/modules/mavlink/mavlink_ftp.cpp b/src/modules/mavlink/mavlink_ftp.cpp index 66989fb381c..218042a81d3 100644 --- a/src/modules/mavlink/mavlink_ftp.cpp +++ b/src/modules/mavlink/mavlink_ftp.cpp @@ -198,7 +198,7 @@ MavlinkFTP::get_size() } } -uint8_t +uint32_t MavlinkFTP::_getServerSystemId() { return _mavlink.get_system_id(); @@ -225,10 +225,12 @@ MavlinkFTP::handle_message(const mavlink_message_t *msg) mavlink_file_transfer_protocol_t ftp_request; mavlink_msg_file_transfer_protocol_decode(msg, &ftp_request); - PX4_DEBUG("FTP: received ftp protocol message target_system: %d target_component: %d, seq: %d", - ftp_request.target_system, ftp_request.target_component, msg->seq); + const uint32_t target_system = mavlink_target_system(msg, ftp_request.target_system); - if ((ftp_request.target_system == _getServerSystemId() || ftp_request.target_system == 0) && + PX4_DEBUG("FTP: received ftp protocol message target_system: %" PRIu32 " target_component: %d, seq: %d", + target_system, ftp_request.target_component, msg->seq); + + if ((target_system == _getServerSystemId() || target_system == 0) && (ftp_request.target_component == _getServerComponentId() || ftp_request.target_component == 0)) { _process_request(&ftp_request, msg->sysid, msg->compid); } @@ -239,7 +241,7 @@ MavlinkFTP::handle_message(const mavlink_message_t *msg) void MavlinkFTP::_process_request( mavlink_file_transfer_protocol_t *ftp_req, - uint8_t target_system_id, + uint32_t target_system_id, uint8_t target_comp_id) { bool stream_send = false; @@ -263,7 +265,7 @@ MavlinkFTP::_process_request( const PayloadHeader *last_payload = reinterpret_cast(&last_reply->payload[0]); if (payload->seq_number + 1 == last_payload->seq_number - && last_reply->target_system == target_system_id + && _last_reply_target_system == target_system_id && last_reply->target_component == target_comp_id) { // this is the same request as the one we replied to last. It means the (n)ack got lost, and the GCS // resent the request. The request buffer is no longer needed, so expand the compact cache into it; @@ -271,7 +273,7 @@ MavlinkFTP::_process_request( memset(ftp_req, 0, sizeof(*ftp_req)); memcpy(ftp_req, _last_reply, sizeof(_last_reply)); _mavlink.lock_send(); - mavlink_msg_file_transfer_protocol_send_struct(_mavlink.get_channel(), ftp_req); + mavlink_msg_send_struct_target(_mavlink.get_channel(), FILE_TRANSFER_PROTOCOL, ftp_req, target_system_id); _mavlink.unlock_send(); return; } @@ -395,10 +397,9 @@ out: // Stream download replies are sent through mavlink stream mechanism. Unless we need to Nack. if (!stream_send || errorCode != kErrNone) { // respond to the request - ftp_req->target_system = target_system_id; ftp_req->target_network = 0; ftp_req->target_component = target_comp_id; - _reply(ftp_req); + _reply(ftp_req, target_system_id); } } @@ -409,7 +410,7 @@ void MavlinkFTP::_ensure_buffers_exist() /// @brief Sends the specified FTP response message out through mavlink void -MavlinkFTP::_reply(mavlink_file_transfer_protocol_t *ftp_req) +MavlinkFTP::_reply(mavlink_file_transfer_protocol_t *ftp_req, uint32_t target_system_id) { PayloadHeader *payload = reinterpret_cast(&ftp_req->payload[0]); @@ -431,6 +432,7 @@ MavlinkFTP::_reply(mavlink_file_transfer_protocol_t *ftp_req) && !(payload->opcode == kRspNak && payload->data[0] == kErrNoSessionsAvailable)) { _last_reply_valid = true; memcpy(_last_reply, ftp_req, sizeof(_last_reply)); + _last_reply_target_system = target_system_id; } PX4_DEBUG("FTP: %s seq_number: %" PRIu16, payload->opcode == kRspAck ? "Ack" : "Nak", payload->seq_number); @@ -438,7 +440,7 @@ MavlinkFTP::_reply(mavlink_file_transfer_protocol_t *ftp_req) // Called from the receiver thread; the per-channel mavlink_status global is // also written by the sending task_main thread, so serialize via lock_send(). _mavlink.lock_send(); - mavlink_msg_file_transfer_protocol_send_struct(_mavlink.get_channel(), ftp_req); + mavlink_msg_send_struct_target(_mavlink.get_channel(), FILE_TRANSFER_PROTOCOL, ftp_req, target_system_id); _mavlink.unlock_send(); } void MavlinkFTP::_constructPath(char *dst, int dst_len, const char *path) const @@ -754,7 +756,7 @@ MavlinkFTP::_workRead(PayloadHeader *payload) /// @brief Responds to a Stream command MavlinkFTP::ErrorCode -MavlinkFTP::_workBurst(PayloadHeader *payload, uint8_t target_system_id, uint8_t target_component_id) +MavlinkFTP::_workBurst(PayloadHeader *payload, uint32_t target_system_id, uint8_t target_component_id) { if ((payload->session != 0) || !_session_open()) { PX4_DEBUG("_workBurst: no session or no fd"); @@ -1323,10 +1325,9 @@ void MavlinkFTP::send() } } - ftp_msg.target_system = _session_info.stream_target_system_id; ftp_msg.target_network = 0; ftp_msg.target_component = _session_info.stream_target_component_id; - _reply(&ftp_msg); + _reply(&ftp_msg, _session_info.stream_target_system_id); // Streaming a burst is activity: without this the session is torn down // from under a burst that runs longer than the inactivity timeout, which diff --git a/src/modules/mavlink/mavlink_ftp.h b/src/modules/mavlink/mavlink_ftp.h index a07408b657b..a69c9a74a57 100644 --- a/src/modules/mavlink/mavlink_ftp.h +++ b/src/modules/mavlink/mavlink_ftp.h @@ -138,15 +138,15 @@ public: private: char *_data_as_cstring(PayloadHeader *payload); - void _process_request(mavlink_file_transfer_protocol_t *ftp_req, uint8_t target_system_id, uint8_t target_comp_id); - void _reply(mavlink_file_transfer_protocol_t *ftp_req); + void _process_request(mavlink_file_transfer_protocol_t *ftp_req, uint32_t target_system_id, uint8_t target_comp_id); + void _reply(mavlink_file_transfer_protocol_t *ftp_req, uint32_t target_system_id); int _copy_file(const char *src_path, const char *dst_path, size_t length); ErrorCode _workList(PayloadHeader *payload, bool include_time = false); void _statDirent(const char *name, uint32_t &size, uint32_t &mtime); ErrorCode _workOpen(PayloadHeader *payload, int oflag); ErrorCode _workRead(PayloadHeader *payload); - ErrorCode _workBurst(PayloadHeader *payload, uint8_t target_system_id, uint8_t target_component_id); + ErrorCode _workBurst(PayloadHeader *payload, uint32_t target_system_id, uint8_t target_component_id); ErrorCode _workWrite(PayloadHeader *payload); ErrorCode _workTerminate(PayloadHeader *payload); ErrorCode _workReset(PayloadHeader *payload); @@ -157,7 +157,7 @@ private: ErrorCode _workRename(PayloadHeader *payload); ErrorCode _workCalcFileCRC32(PayloadHeader *payload); - uint8_t _getServerSystemId(void); + uint32_t _getServerSystemId(void); uint8_t _getServerComponentId(void); uint8_t _getServerChannel(void); @@ -204,7 +204,7 @@ private: bool stream_download; uint32_t stream_offset; uint16_t stream_seq_number; - uint8_t stream_target_system_id; + uint32_t stream_target_system_id; uint8_t stream_target_component_id; unsigned stream_chunk_transmitted; uint8_t stream_size; ///< payload size the client asked for, 0 = as much as fits @@ -240,6 +240,7 @@ private: static constexpr const int _mav_log_dir_len = sizeof(_mav_log_dir) - 1; bool _last_reply_valid = false; + uint32_t _last_reply_target_system{0}; ///< _last_reply only holds the 8-bit payload field uint8_t _last_reply[MAVLINK_MSG_ID_FILE_TRANSFER_PROTOCOL_LEN - MAVLINK_MSG_FILE_TRANSFER_PROTOCOL_FIELD_PAYLOAD_LEN + sizeof(PayloadHeader) + sizeof(uint32_t)]; diff --git a/src/modules/mavlink/mavlink_log_handler.cpp b/src/modules/mavlink/mavlink_log_handler.cpp index 219c7afa6a0..7e35453feae 100644 --- a/src/modules/mavlink/mavlink_log_handler.cpp +++ b/src/modules/mavlink/mavlink_log_handler.cpp @@ -49,7 +49,7 @@ static const char *kLogDir = PX4_STORAGEDIR "/log"; * LOG_ERASE deletes this vehicle's logs too, and a listing request for one vehicle is * answered by all of them. Zero is the broadcast value for both fields. */ -static bool targeted_at_us(uint8_t target_system, uint8_t target_component) +static bool targeted_at_us(uint32_t target_system, uint8_t target_component) { return (target_system == 0 || target_system == mavlink_system.sysid) && (target_component == 0 @@ -295,7 +295,7 @@ void MavlinkLogHandler::handle_log_request_list(const mavlink_message_t *msg) mavlink_log_request_list_t request; mavlink_msg_log_request_list_decode(msg, &request); - if (!targeted_at_us(request.target_system, request.target_component)) { + if (!targeted_at_us(mavlink_target_system(msg, request.target_system), request.target_component)) { return; } @@ -322,7 +322,7 @@ void MavlinkLogHandler::handle_log_request_data(const mavlink_message_t *msg) mavlink_log_request_data_t request; mavlink_msg_log_request_data_decode(msg, &request); - if (!targeted_at_us(request.target_system, request.target_component)) { + if (!targeted_at_us(mavlink_target_system(msg, request.target_system), request.target_component)) { return; } @@ -382,7 +382,7 @@ void MavlinkLogHandler::handle_log_request_end(const mavlink_message_t *msg) mavlink_log_request_end_t request; mavlink_msg_log_request_end_decode(msg, &request); - if (!targeted_at_us(request.target_system, request.target_component)) { + if (!targeted_at_us(mavlink_target_system(msg, request.target_system), request.target_component)) { return; } @@ -394,7 +394,7 @@ void MavlinkLogHandler::handle_log_erase(const mavlink_message_t *msg) mavlink_log_erase_t request; mavlink_msg_log_erase_decode(msg, &request); - if (!targeted_at_us(request.target_system, request.target_component)) { + if (!targeted_at_us(mavlink_target_system(msg, request.target_system), request.target_component)) { return; } diff --git a/src/modules/mavlink/mavlink_main.cpp b/src/modules/mavlink/mavlink_main.cpp index f656f412258..b8b06755ba0 100644 --- a/src/modules/mavlink/mavlink_main.cpp +++ b/src/modules/mavlink/mavlink_main.cpp @@ -214,7 +214,7 @@ Mavlink::Mavlink() : static px4::atomic_bool s_sysid_written{false}; static px4::atomic_bool s_compid_written{false}; - if (sys_id > 0 && sys_id < 255) { + if (sys_id > 0) { bool expected = false; if (s_sysid_written.compare_exchange(&expected, true)) { @@ -394,6 +394,11 @@ Mavlink::set_instance_id() void Mavlink::setProtocolVersion(uint8_t version) { + // MAVLink 1 can't carry a system ID above 255, the library would drop every message. + if (mavlink_system.sysid > UINT8_MAX) { + version = 2; + } + _protocol_version.store((version == 1) ? 1 : 2); // The version flag lives in the per-channel status, which only exists once we @@ -577,7 +582,7 @@ Mavlink::serial_instance_exists(const char *device_name, Mavlink *self) } bool -Mavlink::component_was_seen(int system_id, int component_id, Mavlink &self) +Mavlink::component_was_seen(uint32_t system_id, int component_id, Mavlink &self) { LockGuard lg{mavlink_module_mutex}; @@ -590,7 +595,7 @@ Mavlink::component_was_seen(int system_id, int component_id, Mavlink &self) return false; } -static bool target_is_us_or_broadcast(int target_system_id, int target_component_id) +static bool target_is_us_or_broadcast(uint32_t target_system_id, int target_component_id) { return (target_system_id == 0 || target_system_id == mavlink_system.sysid) && (target_component_id == 0 || target_component_id == mavlink_system.compid); @@ -601,25 +606,14 @@ Mavlink::forward_message(const mavlink_message_t *msg, Mavlink *self) { const mavlink_msg_entry_t *meta = mavlink_get_msg_entry(msg->msgid); - int target_system_id = 0; - int target_component_id = 0; - - // might be nullptr if message is unknown - if (meta) { - // Extract target system and target component if set - if (meta->flags & MAV_MSG_ENTRY_FLAG_HAVE_TARGET_SYSTEM) { - target_system_id = static_cast((_MAV_PAYLOAD(msg))[meta->target_system_ofs]); - } - - if (meta->flags & MAV_MSG_ENTRY_FLAG_HAVE_TARGET_COMPONENT) { - target_component_id = static_cast((_MAV_PAYLOAD(msg))[meta->target_component_ofs]); - } - } + // Takes the TARGET32 header extension into account, meta might be nullptr if message is unknown + const uint32_t target_system_id = mavlink_msg_get_target_sysid(msg, meta); + const int target_component_id = mavlink_msg_get_target_compid(msg, meta); // SETUP_SIGNING must never be forwarded (MAVLink spec requirement). if (msg->msgid == MAVLINK_MSG_ID_SETUP_SIGNING) { if (!target_is_us_or_broadcast(target_system_id, target_component_id)) { - mavlink_log_warning(&self->_mavlink_log_pub, "MAVLink signing: SETUP_SIGNING for %d/%d not forwarded", + mavlink_log_warning(&self->_mavlink_log_pub, "MAVLink signing: SETUP_SIGNING for %" PRIu32 "/%d not forwarded", target_system_id, target_component_id); } @@ -990,7 +984,8 @@ void Mavlink::send_finish() void Mavlink::send_bytes(const uint8_t *buf, unsigned packet_len) { if (!_tx_buffer_low) { - // _buf holds exactly one maximum-length frame (280 bytes for a signed frame with a 255-byte payload) + // _buf holds exactly one maximum-length frame (287 bytes for a signed frame with a 255-byte payload + // and both header extensions) if (_buf_fill + packet_len <= sizeof(_buf)) { memcpy(&_buf[_buf_fill], buf, packet_len); _buf_fill += packet_len; @@ -1217,7 +1212,7 @@ Mavlink::handle_message(const mavlink_message_t *msg) mavlink_setup_signing_t setup_signing; mavlink_msg_setup_signing_decode(msg, &setup_signing); - if (target_is_us_or_broadcast(setup_signing.target_system, setup_signing.target_component)) { + if (target_is_us_or_broadcast(mavlink_target_system(msg, setup_signing.target_system), setup_signing.target_component)) { // Reject signing changes while armed. This runs on the receiver // thread, so use a local subscription instead of _vehicle_status_sub. uORB::Subscription vehicle_status_sub{ORB_ID(vehicle_status)}; @@ -1506,26 +1501,14 @@ Mavlink::pass_message(const mavlink_message_t *msg) // mavlink_msg_to_send_buffer() can't be used because it trims the payload // again, which would break the checksum of an untrimmed frame. uint8_t buf[MAVLINK_MAX_PACKET_LEN]; - size_t size = 0; - buf[size++] = msg->magic; - buf[size++] = msg->len; + // Includes the SYSID32 and TARGET32 header extensions. + size_t size = mavlink_header_to_send_buffer(buf, msg); - if (msg->magic == MAVLINK_STX_MAVLINK1) { - buf[size++] = msg->seq; - buf[size++] = msg->sysid; - buf[size++] = msg->compid; - buf[size++] = msg->msgid & 0xFF; - - } else { - buf[size++] = msg->incompat_flags; - buf[size++] = msg->compat_flags; - buf[size++] = msg->seq; - buf[size++] = msg->sysid; - buf[size++] = msg->compid; - buf[size++] = msg->msgid & 0xFF; - buf[size++] = (msg->msgid >> 8) & 0xFF; - buf[size++] = (msg->msgid >> 16) & 0xFF; + if (size == 0) { + // Can't happen for a received frame, the parser only accepts representable headers. + perf_count(_forwarding_error_perf); + return; } memcpy(&buf[size], _MAV_PAYLOAD(msg), msg->len); @@ -3068,7 +3051,8 @@ void Mavlink::handleSerialPassthroughOutput() msg.timeout = 0; msg.flags = SERIAL_CONTROL_FLAG_REPLY; - uint8_t sysid, compid, device; + uint32_t sysid; + uint8_t compid, device; for (int i = 0; i < SP_MAX_INSTANCES; i++) { SerialPassthrough *sp = SerialPassthrough::get_instance_by_index(i); @@ -3096,6 +3080,7 @@ void Mavlink::handleMavlinkShellOutput() if (_mavlink_shell.load()) { // First do a fast check before taking the lock mavlink_serial_control_t msg; msg.count = 0; + uint32_t target_system = 0; { const LockGuard lg{_mavlink_shell_mutex}; @@ -3108,7 +3093,7 @@ void Mavlink::handleMavlinkShellOutput() msg.timeout = 0; msg.device = SERIAL_CONTROL_DEV_SHELL; msg.count = shell->read(msg.data, sizeof(msg.data)); - msg.target_system = shell->targetSysid(); + target_system = shell->targetSysid(); msg.target_component = shell->targetCompid(); } } @@ -3117,7 +3102,7 @@ void Mavlink::handleMavlinkShellOutput() // Send message without the shell lock (but under the channel send lock). if (msg.count > 0) { lock_send(); - mavlink_msg_serial_control_send_struct(get_channel(), &msg); + mavlink_msg_send_struct_target(get_channel(), SERIAL_CONTROL, &msg, target_system); unlock_send(); } } @@ -3195,12 +3180,11 @@ void Mavlink::handleCommands() msg.param6 = cmd.param6; msg.param7 = cmd.param7; msg.command = cmd.command; - msg.target_system = cmd.target_system; msg.target_component = cmd.target_component; msg.confirmation = 0; lock_send(); - mavlink_msg_command_long_send_struct(get_channel(), &msg); + mavlink_msg_send_struct_target(get_channel(), COMMAND_LONG, &msg, cmd.target_system); unlock_send(); } } @@ -3232,7 +3216,6 @@ void Mavlink::handleAndGetCurrentCommandAck() msg.command = command_ack.command; msg.progress = command_ack.result_param1; msg.result_param2 = command_ack.result_param2; - msg.target_system = command_ack.target_system; msg.target_component = command_ack.target_component; // Handle logging acks before sending out the mavlink message to prevent a race condition @@ -3253,13 +3236,13 @@ void Mavlink::handleAndGetCurrentCommandAck() if (command_ack.from_external) { // for MAVLINK_MODE_IRIDIUM send only if external lock_send(); - mavlink_msg_command_ack_send_struct(get_channel(), &msg); + mavlink_msg_send_struct_target(get_channel(), COMMAND_ACK, &msg, command_ack.target_system); unlock_send(); } } else { lock_send(); - mavlink_msg_command_ack_send_struct(get_channel(), &msg); + mavlink_msg_send_struct_target(get_channel(), COMMAND_ACK, &msg, command_ack.target_system); unlock_send(); } diff --git a/src/modules/mavlink/mavlink_main.h b/src/modules/mavlink/mavlink_main.h index 9ee9cc4e993..784ec8df96d 100644 --- a/src/modules/mavlink/mavlink_main.h +++ b/src/modules/mavlink/mavlink_main.h @@ -145,7 +145,7 @@ public: static bool serial_instance_exists(const char *device_name, Mavlink *self); static void forward_message(const mavlink_message_t *msg, Mavlink *self); - static bool component_was_seen(int system_id, int component_id, Mavlink &self); + static bool component_was_seen(uint32_t system_id, int component_id, Mavlink &self); bool check_events() const { return _should_check_events.load(); } void check_events_enable() { _should_check_events.store(true); } @@ -159,7 +159,7 @@ public: int get_uart_fd() const { return _uart_fd; } - int get_system_id() const { return mavlink_system.sysid; } + uint32_t get_system_id() const { return mavlink_system.sysid; } int get_component_id() const { return mavlink_system.compid; } const char *_device_name{DEFAULT_DEVICE_NAME}; @@ -496,7 +496,7 @@ public: /** get ulog streaming if active, nullptr otherwise */ MavlinkULog *get_ulog_streaming() { return _mavlink_ulog; } - void try_start_ulog_streaming(uint8_t target_system, uint8_t target_component) + void try_start_ulog_streaming(uint32_t target_system, uint8_t target_component) { if (_mavlink_ulog) { return; } diff --git a/src/modules/mavlink/mavlink_mission.cpp b/src/modules/mavlink/mavlink_mission.cpp index b21b5062533..213d4a43644 100644 --- a/src/modules/mavlink/mavlink_mission.cpp +++ b/src/modules/mavlink/mavlink_mission.cpp @@ -76,7 +76,7 @@ bool MavlinkMissionManager::_transfer_in_progress = false; pthread_mutex_t MavlinkMissionManager::_shared_state_mutex = PTHREAD_MUTEX_INITIALIZER; constexpr uint16_t MavlinkMissionManager::MAX_COUNT[]; -#define CHECK_SYSID_COMPID_MISSION(_msg) (_msg.target_system == mavlink_system.sysid && \ +#define CHECK_SYSID_COMPID_MISSION(_mavlink_msg, _msg) (mavlink_target_system(_mavlink_msg, _msg.target_system) == mavlink_system.sysid && \ ((_msg.target_component == mavlink_system.compid) || \ (_msg.target_component == MAV_COMP_ID_MISSIONPLANNER) || \ (_msg.target_component == MAV_COMP_ID_ALL))) @@ -287,21 +287,20 @@ MavlinkMissionManager::update_safepoint_count(dm_item_t safepoint_dataman_id, un } void -MavlinkMissionManager::send_mission_ack(uint8_t sysid, uint8_t compid, uint8_t type, uint32_t opaque_id) +MavlinkMissionManager::send_mission_ack(uint32_t sysid, uint8_t compid, uint8_t type, uint32_t opaque_id) { mavlink_mission_ack_t wpa{}; - wpa.target_system = sysid; wpa.target_component = compid; wpa.type = type; wpa.mission_type = _mission_type; wpa.opaque_id = opaque_id; _mavlink.lock_send(); - mavlink_msg_mission_ack_send_struct(_mavlink.get_channel(), &wpa); + mavlink_msg_send_struct_target(_mavlink.get_channel(), MISSION_ACK, &wpa, sysid); _mavlink.unlock_send(); - PX4_DEBUG("WPM: Send MISSION_ACK type %u to ID %u", wpa.type, wpa.target_system); + PX4_DEBUG("WPM: Send MISSION_ACK type %u to ID %" PRIu32, wpa.type, sysid); } void @@ -326,28 +325,27 @@ MavlinkMissionManager::send_mission_current(uint16_t seq) } void -MavlinkMissionManager::send_mission_count(uint8_t sysid, uint8_t compid, uint16_t count, MAV_MISSION_TYPE mission_type, +MavlinkMissionManager::send_mission_count(uint32_t sysid, uint8_t compid, uint16_t count, MAV_MISSION_TYPE mission_type, uint32_t opaque_id) { _time_last_sent = hrt_absolute_time(); mavlink_mission_count_t wpc{}; - wpc.target_system = sysid; wpc.target_component = compid; wpc.count = count; wpc.mission_type = mission_type; wpc.opaque_id = opaque_id; _mavlink.lock_send(); - mavlink_msg_mission_count_send_struct(_mavlink.get_channel(), &wpc); + mavlink_msg_send_struct_target(_mavlink.get_channel(), MISSION_COUNT, &wpc, sysid); _mavlink.unlock_send(); - PX4_DEBUG("WPM: Send MISSION_COUNT %u to ID %u, mission type=%i", wpc.count, wpc.target_system, mission_type); + PX4_DEBUG("WPM: Send MISSION_COUNT %u to ID %" PRIu32 ", mission type=%i", wpc.count, sysid, mission_type); } void -MavlinkMissionManager::send_mission_item(uint8_t sysid, uint8_t compid, uint16_t seq) +MavlinkMissionManager::send_mission_item(uint32_t sysid, uint8_t compid, uint16_t seq) { mission_item_s mission_item{}; bool read_success = false; @@ -401,31 +399,29 @@ MavlinkMissionManager::send_mission_item(uint8_t sysid, uint8_t compid, uint16_t mavlink_mission_item_int_t wp{}; format_mavlink_mission_item(&mission_item, reinterpret_cast(&wp)); - wp.target_system = sysid; wp.target_component = compid; wp.seq = seq; wp.current = (_current_seq == seq) ? 1 : 0; _mavlink.lock_send(); - mavlink_msg_mission_item_int_send_struct(_mavlink.get_channel(), &wp); + mavlink_msg_send_struct_target(_mavlink.get_channel(), MISSION_ITEM_INT, &wp, sysid); _mavlink.unlock_send(); - PX4_DEBUG("WPM: Send MISSION_ITEM_INT seq %u to ID %u", wp.seq, wp.target_system); + PX4_DEBUG("WPM: Send MISSION_ITEM_INT seq %u to ID %" PRIu32, wp.seq, sysid); } else { mavlink_mission_item_t wp{}; format_mavlink_mission_item(&mission_item, &wp); - wp.target_system = sysid; wp.target_component = compid; wp.seq = seq; wp.current = (_current_seq == seq) ? 1 : 0; _mavlink.lock_send(); - mavlink_msg_mission_item_send_struct(_mavlink.get_channel(), &wp); + mavlink_msg_send_struct_target(_mavlink.get_channel(), MISSION_ITEM, &wp, sysid); _mavlink.unlock_send(); - PX4_DEBUG("WPM: Send MISSION_ITEM seq %u to ID %u", wp.seq, wp.target_system); + PX4_DEBUG("WPM: Send MISSION_ITEM seq %u to ID %" PRIu32, wp.seq, sysid); } } else { @@ -479,36 +475,34 @@ MavlinkMissionManager::get_current_mission_type_crc() } void -MavlinkMissionManager::send_mission_request(uint8_t sysid, uint8_t compid, uint16_t seq) +MavlinkMissionManager::send_mission_request(uint32_t sysid, uint8_t compid, uint16_t seq) { if (seq < current_max_item_count()) { _time_last_sent = hrt_absolute_time(); if (_int_mode) { mavlink_mission_request_int_t wpr{}; - wpr.target_system = sysid; wpr.target_component = compid; wpr.seq = seq; wpr.mission_type = _mission_type; _mavlink.lock_send(); - mavlink_msg_mission_request_int_send_struct(_mavlink.get_channel(), &wpr); + mavlink_msg_send_struct_target(_mavlink.get_channel(), MISSION_REQUEST_INT, &wpr, sysid); _mavlink.unlock_send(); - PX4_DEBUG("WPM: Send MISSION_REQUEST_INT seq %u to ID %u", wpr.seq, wpr.target_system); + PX4_DEBUG("WPM: Send MISSION_REQUEST_INT seq %u to ID %" PRIu32, wpr.seq, sysid); } else { mavlink_mission_request_t wpr{}; - wpr.target_system = sysid; wpr.target_component = compid; wpr.seq = seq; wpr.mission_type = _mission_type; _mavlink.lock_send(); - mavlink_msg_mission_request_send_struct(_mavlink.get_channel(), &wpr); + mavlink_msg_send_struct_target(_mavlink.get_channel(), MISSION_REQUEST, &wpr, sysid); _mavlink.unlock_send(); - PX4_DEBUG("WPM: Send MISSION_REQUEST seq %u to ID %u", wpr.seq, wpr.target_system); + PX4_DEBUG("WPM: Send MISSION_REQUEST seq %u to ID %" PRIu32, wpr.seq, sysid); } } else { @@ -706,7 +700,7 @@ MavlinkMissionManager::handle_mission_ack(const mavlink_message_t *msg) mavlink_mission_ack_t wpa{}; mavlink_msg_mission_ack_decode(msg, &wpa); - if (CHECK_SYSID_COMPID_MISSION(wpa)) { + if (CHECK_SYSID_COMPID_MISSION(msg, wpa)) { if ((msg->sysid == _transfer_partner_sysid && msg->compid == _transfer_partner_compid)) { if (_state == MAVLINK_WPM_STATE_SENDLIST && _mission_type == wpa.mission_type) { @@ -764,7 +758,7 @@ MavlinkMissionManager::handle_mission_set_current(const mavlink_message_t *msg) mavlink_mission_set_current_t wpc; mavlink_msg_mission_set_current_decode(msg, &wpc); - if (CHECK_SYSID_COMPID_MISSION(wpc)) { + if (CHECK_SYSID_COMPID_MISSION(msg, wpc)) { if (_state == MAVLINK_WPM_STATE_IDLE) { _time_last_recv = hrt_absolute_time(); @@ -796,7 +790,7 @@ MavlinkMissionManager::handle_mission_request_list(const mavlink_message_t *msg) mavlink_mission_request_list_t wprl; mavlink_msg_mission_request_list_decode(msg, &wprl); - if (CHECK_SYSID_COMPID_MISSION(wprl)) { + if (CHECK_SYSID_COMPID_MISSION(msg, wprl)) { const bool maybe_completed = (_state == MAVLINK_WPM_STATE_SENDLIST) && (_transfer_seq == current_item_count()); // If all mission items have been sent and a new mission request list comes in, we can proceed even if MISSION_ACK was @@ -882,7 +876,7 @@ MavlinkMissionManager::handle_mission_request_both(const mavlink_message_t *msg) mavlink_mission_request_t wpr; mavlink_msg_mission_request_decode(msg, &wpr); - if (CHECK_SYSID_COMPID_MISSION(wpr)) { + if (CHECK_SYSID_COMPID_MISSION(msg, wpr)) { if (msg->sysid == _transfer_partner_sysid && msg->compid == _transfer_partner_compid) { if (_state == MAVLINK_WPM_STATE_SENDLIST) { @@ -895,24 +889,24 @@ MavlinkMissionManager::handle_mission_request_both(const mavlink_message_t *msg) /* _transfer_seq contains sequence of expected request */ if (wpr.seq == _transfer_seq && _transfer_seq < _transfer_count) { - PX4_DEBUG("WPM: MISSION_ITEM_REQUEST(_INT) seq %u from ID %u", wpr.seq, msg->sysid); + PX4_DEBUG("WPM: MISSION_ITEM_REQUEST(_INT) seq %u from ID %" PRIu32, wpr.seq, msg->sysid); _transfer_seq++; } else if (wpr.seq == _transfer_seq - 1) { - PX4_DEBUG("WPM: MISSION_ITEM_REQUEST(_INT) seq %u from ID %u (again)", wpr.seq, msg->sysid); + PX4_DEBUG("WPM: MISSION_ITEM_REQUEST(_INT) seq %u from ID %" PRIu32 " (again)", wpr.seq, msg->sysid); } else { if (_transfer_seq > 0 && _transfer_seq < _transfer_count) { - PX4_DEBUG("WPM: MISSION_ITEM_REQUEST(_INT) ERROR: seq %u from ID %u unexpected, must be %i or %i", wpr.seq, msg->sysid, + PX4_DEBUG("WPM: MISSION_ITEM_REQUEST(_INT) ERROR: seq %u from ID %" PRIu32 " unexpected, must be %i or %i", wpr.seq, msg->sysid, _transfer_seq - 1, _transfer_seq); } else if (_transfer_seq <= 0) { - PX4_DEBUG("WPM: MISSION_ITEM_REQUEST(_INT) ERROR: seq %u from ID %u unexpected, must be %i", wpr.seq, msg->sysid, + PX4_DEBUG("WPM: MISSION_ITEM_REQUEST(_INT) ERROR: seq %u from ID %" PRIu32 " unexpected, must be %i", wpr.seq, msg->sysid, _transfer_seq); } else { - PX4_DEBUG("WPM: MISSION_ITEM_REQUEST(_INT) ERROR: seq %u from ID %u unexpected, must be %i", wpr.seq, msg->sysid, + PX4_DEBUG("WPM: MISSION_ITEM_REQUEST(_INT) ERROR: seq %u from ID %" PRIu32 " unexpected, must be %i", wpr.seq, msg->sysid, _transfer_seq - 1); } @@ -972,7 +966,7 @@ MavlinkMissionManager::handle_mission_count(const mavlink_message_t *msg) mavlink_mission_count_t wpc; mavlink_msg_mission_count_decode(msg, &wpc); - if (CHECK_SYSID_COMPID_MISSION(wpc)) { + if (CHECK_SYSID_COMPID_MISSION(msg, wpc)) { if (_state == MAVLINK_WPM_STATE_IDLE) { _time_last_recv = hrt_absolute_time(); @@ -1035,7 +1029,7 @@ MavlinkMissionManager::handle_mission_count(const mavlink_message_t *msg) return; } - PX4_DEBUG("WPM: MISSION_COUNT %u from ID %u, changing state to MAVLINK_WPM_STATE_GETLIST", wpc.count, msg->sysid); + PX4_DEBUG("WPM: MISSION_COUNT %u from ID %" PRIu32 ", changing state to MAVLINK_WPM_STATE_GETLIST", wpc.count, msg->sysid); switch (_mission_type) { case MAV_MISSION_TYPE_MISSION: @@ -1079,7 +1073,7 @@ MavlinkMissionManager::handle_mission_count(const mavlink_message_t *msg) if (_transfer_seq == 0) { /* looks like our MISSION_REQUEST was lost, try again */ - PX4_DEBUG("WPM: MISSION_COUNT %u from ID %u (again)", wpc.count, msg->sysid); + PX4_DEBUG("WPM: MISSION_COUNT %u from ID %" PRIu32 " (again)", wpc.count, msg->sysid); } else { PX4_DEBUG("WPM: MISSION_COUNT ERROR: busy, already receiving seq %u", _transfer_seq); @@ -1145,7 +1139,7 @@ MavlinkMissionManager::handle_mission_item_both(const mavlink_message_t *msg) mavlink_mission_item_t wp; mavlink_msg_mission_item_decode(msg, &wp); - if (CHECK_SYSID_COMPID_MISSION(wp)) { + if (CHECK_SYSID_COMPID_MISSION(msg, wp)) { if ((msg->sysid == _transfer_partner_sysid && msg->compid == _transfer_partner_compid)) { if (wp.mission_type != _mission_type) { PX4_WARN("WPM: Unexpected mission type (%d %d)", (int)wp.mission_type, (int)_mission_type); @@ -1414,7 +1408,7 @@ MavlinkMissionManager::handle_mission_clear_all(const mavlink_message_t *msg) mavlink_mission_clear_all_t wpca; mavlink_msg_mission_clear_all_decode(msg, &wpca); - if (CHECK_SYSID_COMPID_MISSION(wpca)) { + if (CHECK_SYSID_COMPID_MISSION(msg, wpca)) { if (_state == MAVLINK_WPM_STATE_IDLE) { /* don't touch mission items storage itself, but only items count in mission state */ diff --git a/src/modules/mavlink/mavlink_mission.h b/src/modules/mavlink/mavlink_mission.h index 76debb373d9..fa7b04ff68c 100644 --- a/src/modules/mavlink/mavlink_mission.h +++ b/src/modules/mavlink/mavlink_mission.h @@ -148,7 +148,7 @@ private: int32_t _transfer_current_seq{-1}; ///< Current item ID for current transmission (-1 means not initialized) - uint8_t _transfer_partner_sysid{0}; ///< Partner system ID for current transmission + uint32_t _transfer_partner_sysid{0}; ///< Partner system ID for current transmission uint8_t _transfer_partner_compid{0}; ///< Partner component ID for current transmission int32_t _transfer_land_start_marker{-1}; ///< index of land start mission item in current transmission (if unavailable, index of land mission item, -1 otherwise) int32_t _transfer_land_marker{-1}; ///< index of land mission item in current transmission (-1 if unavailable) @@ -215,7 +215,7 @@ private: /** * @brief Sends an waypoint ack message */ - void send_mission_ack(uint8_t sysid, uint8_t compid, uint8_t type, uint32_t opaque_id = 0U); + void send_mission_ack(uint32_t sysid, uint8_t compid, uint8_t type, uint32_t opaque_id = 0U); /** * @brief Broadcasts the new target waypoint and directs the MAV to fly there @@ -228,12 +228,12 @@ private: */ void send_mission_current(uint16_t seq); - void send_mission_count(uint8_t sysid, uint8_t compid, uint16_t count, MAV_MISSION_TYPE mission_type, + void send_mission_count(uint32_t sysid, uint8_t compid, uint16_t count, MAV_MISSION_TYPE mission_type, uint32_t opaque_id); - void send_mission_item(uint8_t sysid, uint8_t compid, uint16_t seq); + void send_mission_item(uint32_t sysid, uint8_t compid, uint16_t seq); - void send_mission_request(uint8_t sysid, uint8_t compid, uint16_t seq); + void send_mission_request(uint32_t sysid, uint8_t compid, uint16_t seq); /** * @brief emits a message that a waypoint reached diff --git a/src/modules/mavlink/mavlink_parameters.cpp b/src/modules/mavlink/mavlink_parameters.cpp index 154a4a5ae8a..5960a59f6ba 100644 --- a/src/modules/mavlink/mavlink_parameters.cpp +++ b/src/modules/mavlink/mavlink_parameters.cpp @@ -196,7 +196,7 @@ MavlinkParametersManager::handle_message(const mavlink_message_t *msg) mavlink_param_request_list_t req_list; mavlink_msg_param_request_list_decode(msg, &req_list); - if (req_list.target_system == mavlink_system.sysid && + if (mavlink_target_system(msg, req_list.target_system) == mavlink_system.sysid && (req_list.target_component == mavlink_system.compid || req_list.target_component == MAV_COMP_ID_ALL)) { if (_send_all_index < 0) { _send_all_index = PARAM_HASH; @@ -209,7 +209,7 @@ MavlinkParametersManager::handle_message(const mavlink_message_t *msg) #if defined(CONFIG_MAVLINK_UAVCAN_PARAMETERS) - if (req_list.target_system == mavlink_system.sysid && req_list.target_component < 127 && + if (mavlink_target_system(msg, req_list.target_system) == mavlink_system.sysid && req_list.target_component < 127 && !is_observed_mavlink_camera(req_list.target_component) && (req_list.target_component != mavlink_system.compid || req_list.target_component == MAV_COMP_ID_ALL)) { // publish list request to UAVCAN driver via uORB. @@ -230,7 +230,7 @@ MavlinkParametersManager::handle_message(const mavlink_message_t *msg) mavlink_param_set_t set; mavlink_msg_param_set_decode(msg, &set); - if (set.target_system == mavlink_system.sysid && + if (mavlink_target_system(msg, set.target_system) == mavlink_system.sysid && (set.target_component == mavlink_system.compid || set.target_component == MAV_COMP_ID_ALL)) { /* local name buffer to enforce null-terminated string */ @@ -282,7 +282,7 @@ MavlinkParametersManager::handle_message(const mavlink_message_t *msg) #if defined(CONFIG_MAVLINK_UAVCAN_PARAMETERS) - if (set.target_system == mavlink_system.sysid && set.target_component < 127 && + if (mavlink_target_system(msg, set.target_system) == mavlink_system.sysid && set.target_component < 127 && !is_observed_mavlink_camera(set.target_component) && (set.target_component != mavlink_system.compid || set.target_component == MAV_COMP_ID_ALL)) { // publish set request to UAVCAN driver via uORB. @@ -317,7 +317,7 @@ MavlinkParametersManager::handle_message(const mavlink_message_t *msg) mavlink_param_request_read_t req_read; mavlink_msg_param_request_read_decode(msg, &req_read); - if (req_read.target_system == mavlink_system.sysid && + if (mavlink_target_system(msg, req_read.target_system) == mavlink_system.sysid && (req_read.target_component == mavlink_system.compid || req_read.target_component == MAV_COMP_ID_ALL)) { /* when no index is given, loop through string ids and compare them */ @@ -391,7 +391,7 @@ MavlinkParametersManager::handle_message(const mavlink_message_t *msg) #if defined(CONFIG_MAVLINK_UAVCAN_PARAMETERS) - if (req_read.target_system == mavlink_system.sysid && req_read.target_component < 127 && + if (mavlink_target_system(msg, req_read.target_system) == mavlink_system.sysid && req_read.target_component < 127 && !is_observed_mavlink_camera(req_read.target_component) && (req_read.target_component != mavlink_system.compid || req_read.target_component == MAV_COMP_ID_ALL)) { // publish set request to UAVCAN driver via uORB. @@ -417,7 +417,7 @@ MavlinkParametersManager::handle_message(const mavlink_message_t *msg) mavlink_param_map_rc_t map_rc; mavlink_msg_param_map_rc_decode(msg, &map_rc); - if (map_rc.target_system == mavlink_system.sysid && + if (mavlink_target_system(msg, map_rc.target_system) == mavlink_system.sysid && (map_rc.target_component == mavlink_system.compid || map_rc.target_component == MAV_COMP_ID_ALL)) { @@ -754,7 +754,7 @@ MavlinkParametersManager::send_param(param_t param, int component_id) int MavlinkParametersManager:: send_error(MAV_PARAM_ERROR error, const char *param_id, const int param_index, - const int target_sysid, const int target_compid, + const uint32_t target_sysid, const int target_compid, int component_id) { /* no free TX buf to send this param error message */ @@ -763,7 +763,6 @@ int MavlinkParametersManager:: send_error(MAV_PARAM_ERROR error, const char *par } mavlink_param_error_t msg; - msg.target_system = target_sysid; msg.target_component = target_compid; msg.error = error; @@ -793,12 +792,14 @@ int MavlinkParametersManager:: send_error(MAV_PARAM_ERROR error, const char *par /* default component ID */ if (component_id < 0) { - mavlink_msg_param_error_send_struct(_mavlink.get_channel(), &msg); + mavlink_msg_send_struct_target(_mavlink.get_channel(), PARAM_ERROR, &msg, target_sysid); } else { // Re-pack the message with a different component ID mavlink_message_t mavlink_packet; - mavlink_msg_param_error_encode_chan(mavlink_system.sysid, component_id, _mavlink.get_channel(), &mavlink_packet, &msg); + // pack_chan() rather than encode_chan(), only it keeps a target system above 255 + mavlink_msg_param_error_pack_chan(mavlink_system.sysid, component_id, _mavlink.get_channel(), &mavlink_packet, + target_sysid, msg.target_component, msg.param_id, msg.param_index, msg.error); _mavlink_resend_uart(_mavlink.get_channel(), &mavlink_packet); } diff --git a/src/modules/mavlink/mavlink_parameters.h b/src/modules/mavlink/mavlink_parameters.h index 846de238bf0..e7a4550ef7f 100644 --- a/src/modules/mavlink/mavlink_parameters.h +++ b/src/modules/mavlink/mavlink_parameters.h @@ -143,7 +143,7 @@ protected: * /// @return true if a error message was sent */ int send_error(MAV_PARAM_ERROR error, const char *param_id = nullptr, - const int param_index = -1, int target_system = -1, + const int param_index = -1, uint32_t target_system = 0, int target_component = -1, int component_id = -1); #if defined(CONFIG_MAVLINK_UAVCAN_PARAMETERS) diff --git a/src/modules/mavlink/mavlink_params.yaml b/src/modules/mavlink/mavlink_params.yaml index 66425051279..96798baef3b 100644 --- a/src/modules/mavlink/mavlink_params.yaml +++ b/src/modules/mavlink/mavlink_params.yaml @@ -5,10 +5,19 @@ parameters: MAV_SYS_ID: description: short: MAVLink system ID + long: |- + IDs above 255 use the 32-bit system ID extension of MAVLink 2, + which forces MAV_PROTO_VER to 2. Every system on the network needs + to support the extension to talk to such a system. + + Only use IDs above 255 for vehicles, and keep ground stations and + other controlling systems at 255 or below. The gimbal protocol and + other messages that carry a system ID inside the payload, such as + the ID of the system in control, still only support IDs up to 255. type: int32 default: 1 min: 1 - max: 250 + max: 2147483647 reboot_required: true MAV_COMP_ID: description: diff --git a/src/modules/mavlink/mavlink_receiver.cpp b/src/modules/mavlink/mavlink_receiver.cpp index 7815121fa81..6364d2f6c93 100644 --- a/src/modules/mavlink/mavlink_receiver.cpp +++ b/src/modules/mavlink/mavlink_receiver.cpp @@ -128,7 +128,7 @@ MavlinkReceiver::MavlinkReceiver(Mavlink &parent) : } void -MavlinkReceiver::acknowledge(uint8_t sysid, uint8_t compid, uint16_t command, uint8_t result, uint8_t progress) +MavlinkReceiver::acknowledge(uint32_t sysid, uint8_t compid, uint16_t command, uint8_t result, uint8_t progress) { vehicle_command_ack_s command_ack{}; @@ -538,7 +538,7 @@ void MavlinkReceiver::handle_messages_in_gimbal_mode(mavlink_message_t &msg) } bool -MavlinkReceiver::evaluate_target_ok(int command, int target_system, int target_component) +MavlinkReceiver::evaluate_target_ok(int command, uint32_t target_system, int target_component) { return ((target_system == 0) || (target_system == mavlink_system.sysid)) && ((target_component == mavlink_system.compid) || (target_component == MAV_COMP_ID_ALL)); @@ -576,7 +576,7 @@ MavlinkReceiver::handle_message_command_long(mavlink_message_t *msg) vcmd.param6 = (double)cmd_mavlink.param6; vcmd.param7 = cmd_mavlink.param7; vcmd.command = cmd_mavlink.command; - vcmd.target_system = cmd_mavlink.target_system; + vcmd.target_system = mavlink_target_system(msg, cmd_mavlink.target_system); vcmd.target_component = cmd_mavlink.target_component; vcmd.source_system = msg->sysid; vcmd.source_component = msg->compid; @@ -701,7 +701,7 @@ MavlinkReceiver::handle_message_command_int(mavlink_message_t *msg) vcmd.param7 = cmd_mavlink.z; vcmd.command = cmd_mavlink.command; - vcmd.target_system = cmd_mavlink.target_system; + vcmd.target_system = mavlink_target_system(msg, cmd_mavlink.target_system); vcmd.target_component = cmd_mavlink.target_component; vcmd.source_system = msg->sysid; vcmd.source_component = msg->compid; @@ -721,7 +721,7 @@ void MavlinkReceiver::handle_message_command_both(mavlink_message_t *msg, const if (!target_ok) { if (!_mavlink.get_forwarding_on() || !_mavlink.component_was_seen(vehicle_command.target_system, vehicle_command.target_component, _mavlink)) { - PX4_INFO("Ignore command %d from %d/%d to %d/%d", + PX4_INFO("Ignore command %d from %" PRIu32 "/%d to %" PRIu32 "/%d", (int)vehicle_command.command, msg->sysid, msg->compid, vehicle_command.target_system, vehicle_command.target_component); } @@ -899,7 +899,7 @@ void MavlinkReceiver::handle_message_command_both(mavlink_message_t *msg, const send_ack = false; if (msg->sysid == mavlink_system.sysid && msg->compid == mavlink_system.compid) { - PX4_WARN("ignoring CMD with same SYS/COMP (%" PRIu8 "/%" PRIu8 ") ID", mavlink_system.sysid, mavlink_system.compid); + PX4_WARN("ignoring CMD with same SYS/COMP (%" PRIu32 "/%" PRIu8 ") ID", mavlink_system.sysid, mavlink_system.compid); return; } @@ -987,11 +987,12 @@ MavlinkReceiver::handle_message_command_ack(mavlink_message_t *msg) // We should not clog the command_ack queue with acks that are not for us. // Therefore, we drop them early and move on. - bool target_ok = evaluate_target_ok(0, ack.target_system, ack.target_component); + const uint32_t ack_target_system = mavlink_target_system(msg, ack.target_system); + bool target_ok = evaluate_target_ok(0, ack_target_system, ack.target_component); if (!target_ok) { - PX4_DEBUG("Drop ack %d for %d from %d/%d to %d/%d\n", - ack.result, ack.command, msg->sysid, msg->compid, ack.target_system, ack.target_component); + PX4_DEBUG("Drop ack %d for %d from %" PRIu32 "/%d to %" PRIu32 "/%d\n", + ack.result, ack.command, msg->sysid, msg->compid, ack_target_system, ack.target_component); return; } @@ -1005,7 +1006,7 @@ MavlinkReceiver::handle_message_command_ack(mavlink_message_t *msg) command_ack.result = ack.result; command_ack.from_external = true; command_ack.result_param1 = ack.progress; - command_ack.target_system = ack.target_system; + command_ack.target_system = ack_target_system; command_ack.target_component = ack.target_component; _cmd_ack_pub.publish(command_ack); @@ -1129,7 +1130,7 @@ MavlinkReceiver::handle_message_set_mode(mavlink_message_t *msg) vcmd.param3 = (float)custom_mode.sub_mode; vcmd.command = vehicle_command_s::VEHICLE_CMD_DO_SET_MODE; - vcmd.target_system = new_mode.target_system; + vcmd.target_system = mavlink_target_system(msg, new_mode.target_system); vcmd.target_component = MAV_COMP_ID_ALL; vcmd.source_system = msg->sysid; vcmd.source_component = msg->compid; @@ -1232,9 +1233,11 @@ MavlinkReceiver::handle_message_set_position_target_local_ned(mavlink_message_t mavlink_set_position_target_local_ned_t target_local_ned; mavlink_msg_set_position_target_local_ned_decode(msg, &target_local_ned); + const uint32_t target_system = mavlink_target_system(msg, target_local_ned.target_system); + /* Only accept messages which are intended for this system */ if (_mavlink.get_forward_externalsp() && - (mavlink_system.sysid == target_local_ned.target_system || target_local_ned.target_system == 0) && + (mavlink_system.sysid == target_system || target_system == 0) && (mavlink_system.compid == target_local_ned.target_component || target_local_ned.target_component == 0)) { trajectory_setpoint_s setpoint{}; @@ -1349,9 +1352,11 @@ MavlinkReceiver::handle_message_set_position_target_global_int(mavlink_message_t mavlink_set_position_target_global_int_t target_global_int; mavlink_msg_set_position_target_global_int_decode(msg, &target_global_int); + const uint32_t target_system = mavlink_target_system(msg, target_global_int.target_system); + /* Only accept messages which are intended for this system */ if (_mavlink.get_forward_externalsp() && - (mavlink_system.sysid == target_global_int.target_system || target_global_int.target_system == 0) && + (mavlink_system.sysid == target_system || target_system == 0) && (mavlink_system.compid == target_global_int.target_component || target_global_int.target_component == 0)) { trajectory_setpoint_s setpoint{}; @@ -1463,7 +1468,7 @@ MavlinkReceiver::handle_message_set_gps_global_origin(mavlink_message_t *msg) mavlink_set_gps_global_origin_t gps_global_origin; mavlink_msg_set_gps_global_origin_decode(msg, &gps_global_origin); - if (gps_global_origin.target_system == _mavlink.get_system_id()) { + if (mavlink_target_system(msg, gps_global_origin.target_system) == _mavlink.get_system_id()) { vehicle_command_s vcmd{}; vcmd.param5 = (double)gps_global_origin.latitude * 1.e-7; vcmd.param6 = (double)gps_global_origin.longitude * 1.e-7; @@ -1848,9 +1853,11 @@ MavlinkReceiver::handle_message_set_attitude_target(mavlink_message_t *msg) mavlink_set_attitude_target_t attitude_target; mavlink_msg_set_attitude_target_decode(msg, &attitude_target); + const uint32_t target_system = mavlink_target_system(msg, attitude_target.target_system); + /* Only accept messages which are intended for this system */ if (_mavlink.get_forward_externalsp() && - (mavlink_system.sysid == attitude_target.target_system || attitude_target.target_system == 0) && + (mavlink_system.sysid == target_system || target_system == 0) && (mavlink_system.compid == attitude_target.target_component || attitude_target.target_component == 0)) { const uint8_t type_mask = attitude_target.type_mask; @@ -1976,16 +1983,17 @@ MavlinkReceiver::handle_message_ping(mavlink_message_t *msg) mavlink_ping_t ping; mavlink_msg_ping_decode(msg, &ping); - if ((ping.target_system == 0) && + const uint32_t ping_target_system = mavlink_target_system(msg, ping.target_system); + + if ((ping_target_system == 0) && (ping.target_component == 0)) { // This is a ping request. Return it to the system which requested the ping. - ping.target_system = msg->sysid; ping.target_component = msg->compid; _mavlink.lock_send(); - mavlink_msg_ping_send_struct(_mavlink.get_channel(), &ping); + mavlink_msg_send_struct_target(_mavlink.get_channel(), PING, &ping, msg->sysid); _mavlink.unlock_send(); - } else if ((ping.target_system == mavlink_system.sysid) && + } else if ((ping_target_system == mavlink_system.sysid) && (ping.target_component == mavlink_system.compid)) { // This is a returned ping message from this system. Calculate latency from it. @@ -2103,8 +2111,10 @@ MavlinkReceiver::handle_message_serial_control(mavlink_message_t *msg) mavlink_msg_serial_control_decode(msg, &serial_control_mavlink); // Check if the message is targeted at us. - if ((serial_control_mavlink.target_system != 0 && - mavlink_system.sysid != serial_control_mavlink.target_system) || + const uint32_t serial_control_target_system = mavlink_target_system(msg, serial_control_mavlink.target_system); + + if ((serial_control_target_system != 0 && + mavlink_system.sysid != serial_control_target_system) || (serial_control_mavlink.target_component != 0 && mavlink_system.compid != serial_control_mavlink.target_component) || (serial_control_mavlink.flags & SERIAL_CONTROL_FLAG_REPLY)) { @@ -2183,7 +2193,9 @@ MavlinkReceiver::handle_message_play_tune(mavlink_message_t *msg) mavlink_play_tune_t play_tune; mavlink_msg_play_tune_decode(msg, &play_tune); - if ((mavlink_system.sysid == play_tune.target_system || play_tune.target_system == 0) && + const uint32_t target_system = mavlink_target_system(msg, play_tune.target_system); + + if ((mavlink_system.sysid == target_system || target_system == 0) && (mavlink_system.compid == play_tune.target_component || play_tune.target_component == 0)) { // Let's make sure the input is 0 terminated, so we don't ever overrun it. @@ -2199,7 +2211,9 @@ MavlinkReceiver::handle_message_play_tune_v2(mavlink_message_t *msg) mavlink_play_tune_v2_t play_tune_v2; mavlink_msg_play_tune_v2_decode(msg, &play_tune_v2); - if ((mavlink_system.sysid == play_tune_v2.target_system || play_tune_v2.target_system == 0) && + const uint32_t target_system = mavlink_target_system(msg, play_tune_v2.target_system); + + if ((mavlink_system.sysid == target_system || target_system == 0) && (mavlink_system.compid == play_tune_v2.target_component || play_tune_v2.target_component == 0)) { if (play_tune_v2.format != TUNE_FORMAT_QBASIC1_1) { @@ -2270,7 +2284,9 @@ MavlinkReceiver::handle_message_tunnel(mavlink_message_t *msg) // The payload is forwarded to a device, so a message meant for another vehicle on the // same link must not be written to this one's bus. - if (!evaluate_target_ok(0, mavlink_tunnel.target_system, mavlink_tunnel.target_component)) { + const uint32_t tunnel_target_system = mavlink_target_system(msg, mavlink_tunnel.target_system); + + if (!evaluate_target_ok(0, tunnel_target_system, mavlink_tunnel.target_component)) { return; } @@ -2286,7 +2302,7 @@ MavlinkReceiver::handle_message_tunnel(mavlink_message_t *msg) tunnel.timestamp = hrt_absolute_time(); tunnel.payload_type = mavlink_tunnel.payload_type; - tunnel.target_system = mavlink_tunnel.target_system; + tunnel.target_system = tunnel_target_system; tunnel.target_component = mavlink_tunnel.target_component; tunnel.payload_length = mavlink_tunnel.payload_length; memcpy(tunnel.payload, mavlink_tunnel.payload, sizeof(tunnel.payload)); @@ -2316,7 +2332,9 @@ MavlinkReceiver::handle_message_rc_channels_override(mavlink_message_t *msg) mavlink_msg_rc_channels_override_decode(msg, &man); // Check target - if (man.target_system != 0 && man.target_system != _mavlink.get_system_id()) { + const uint32_t man_target_system = mavlink_target_system(msg, man.target_system); + + if (man_target_system != 0 && man_target_system != _mavlink.get_system_id()) { return; } @@ -2403,7 +2421,9 @@ MavlinkReceiver::handle_message_manual_control(mavlink_message_t *msg) mavlink_msg_manual_control_decode(msg, &mavlink_manual_control); // Check target - if (mavlink_manual_control.target != 0 && mavlink_manual_control.target != _mavlink.get_system_id()) { + const uint32_t manual_control_target_system = mavlink_target_system(msg, mavlink_manual_control.target); + + if (manual_control_target_system != 0 && manual_control_target_system != _mavlink.get_system_id()) { return; } @@ -2512,7 +2532,7 @@ MavlinkReceiver::handle_message_heartbeat(mavlink_message_t *msg) break; default: - PX4_DEBUG("unhandled HEARTBEAT MAV_TYPE: %" PRIu8 " from SYSID: %" PRIu8 ", COMPID: %" PRIu8, hb.type, msg->sysid, + PX4_DEBUG("unhandled HEARTBEAT MAV_TYPE: %" PRIu8 " from SYSID: %" PRIu32 ", COMPID: %" PRIu8, hb.type, msg->sysid, msg->compid); } @@ -2547,7 +2567,7 @@ MavlinkReceiver::handle_message_heartbeat(mavlink_message_t *msg) break; default: - PX4_DEBUG("unhandled HEARTBEAT MAV_TYPE: %" PRIu8 " from SYSID: %" PRIu8 ", COMPID: %" PRIu8, hb.type, msg->sysid, + PX4_DEBUG("unhandled HEARTBEAT MAV_TYPE: %" PRIu8 " from SYSID: %" PRIu32 ", COMPID: %" PRIu8, hb.type, msg->sysid, msg->compid); } @@ -3778,7 +3798,7 @@ void MavlinkReceiver::handle_message_open_drone_id_basic_id(mavlink_message_t *m mavlink_open_drone_id_basic_id_t odid_module {}; mavlink_msg_open_drone_id_basic_id_decode(msg, &odid_module); - if (odid_module.target_system != mavlink_system.sysid || + if (mavlink_target_system(msg, odid_module.target_system) != mavlink_system.sysid || (odid_module.target_component != mavlink_system.compid && odid_module.target_component != MAV_COMP_ID_ALL)) { return; } @@ -4222,7 +4242,7 @@ void MavlinkReceiver::reset_parser_after_rejected_frame(uint8_t c) } } -bool MavlinkReceiver::component_was_seen(int system_id, int component_id) +bool MavlinkReceiver::component_was_seen(uint32_t system_id, int component_id) { // Called from other threads, read the count once const unsigned component_states_count = _component_states_count.load(); @@ -4373,7 +4393,7 @@ void MavlinkReceiver::print_detailed_rx_stats() const for (const auto &comp_stat : _component_states) { if (comp_stat.received_messages > 0) { - printf("\t sysid:%3" PRIu8 ", compid:%3" PRIu8 ", Total: %" PRIu32 " (lost: %" PRIu32 ")\n", + printf("\t sysid:%3" PRIu32 ", compid:%3" PRIu8 ", Total: %" PRIu32 " (lost: %" PRIu32 ")\n", comp_stat.system_id, comp_stat.component_id, comp_stat.received_messages, comp_stat.missed_messages); diff --git a/src/modules/mavlink/mavlink_receiver.h b/src/modules/mavlink/mavlink_receiver.h index 1a4ec315932..679736a8fa2 100644 --- a/src/modules/mavlink/mavlink_receiver.h +++ b/src/modules/mavlink/mavlink_receiver.h @@ -163,7 +163,7 @@ public: void start(); void stop(); - bool component_was_seen(int system_id, int component_id); + bool component_was_seen(uint32_t system_id, int component_id); void enable_message_statistics() { _message_statistics_enabled = true; } void print_detailed_rx_stats() const; @@ -173,7 +173,7 @@ private: static void *start_trampoline(void *context); void run(); - void acknowledge(uint8_t sysid, uint8_t compid, uint16_t command, uint8_t result, uint8_t progress = 0); + void acknowledge(uint32_t sysid, uint8_t compid, uint16_t command, uint8_t result, uint8_t progress = 0); /** * Common method to handle both mavlink command types, operating on the @@ -277,7 +277,7 @@ private: int set_message_interval(int msgId, float interval, float param3, float param4, float param7); void get_message_interval(int msgId); - bool evaluate_target_ok(int command, int target_system, int target_component); + bool evaluate_target_ok(int command, uint32_t target_system, int target_component); void fill_thrust(float *thrust_body_array, uint8_t vehicle_type, float thrust); @@ -333,7 +333,7 @@ private: struct ComponentState { uint32_t received_messages{0}; uint32_t missed_messages{0}; - uint8_t system_id{0}; + uint32_t system_id{0}; uint8_t component_id{0}; uint8_t last_sequence{0}; }; @@ -350,7 +350,7 @@ private: float avg_rate_hz{0.f}; // average rate uint32_t last_time_received_ms{0}; uint16_t msg_id{0}; - uint8_t system_id{0}; + uint32_t system_id{0}; uint8_t component_id{0}; }; ReceivedMessageStats *_received_msg_stats{nullptr}; diff --git a/src/modules/mavlink/mavlink_shell.h b/src/modules/mavlink/mavlink_shell.h index abb581f751d..e039288b447 100644 --- a/src/modules/mavlink/mavlink_shell.h +++ b/src/modules/mavlink/mavlink_shell.h @@ -78,13 +78,13 @@ public: */ size_t available(); - void setTargetID(uint8_t sysid, uint8_t compid) { _target_sysid.store(sysid); _target_compid.store(compid); } + void setTargetID(uint32_t sysid, uint8_t compid) { _target_sysid.store(sysid); _target_compid.store(compid); } - uint8_t targetSysid() const { return _target_sysid.load(); } + uint32_t targetSysid() const { return _target_sysid.load(); } uint8_t targetCompid() const { return _target_compid.load(); } private: - px4::atomic _target_sysid{}; + px4::atomic _target_sysid{}; px4::atomic _target_compid{}; int _to_shell_fd = -1; /** fd to write to the shell */ diff --git a/src/modules/mavlink/mavlink_timesync.cpp b/src/modules/mavlink/mavlink_timesync.cpp index 820a4b7dacd..a127d27bc13 100644 --- a/src/modules/mavlink/mavlink_timesync.cpp +++ b/src/modules/mavlink/mavlink_timesync.cpp @@ -69,9 +69,8 @@ MavlinkTimesync::handle_message(const mavlink_message_t *msg) rsync.tc1 = now * 1000ULL; rsync.ts1 = tsync.ts1; rsync.target_component = msg->compid; - rsync.target_system = msg->sysid; - mavlink_msg_timesync_send_struct(_mavlink.get_channel(), &rsync); + mavlink_msg_send_struct_target(_mavlink.get_channel(), TIMESYNC, &rsync, msg->sysid); return; @@ -101,7 +100,7 @@ MavlinkTimesync::handle_message(const mavlink_message_t *msg) param_get(param_find("SYS_TIME_SRC"), &sys_time_src); if (update && (sys_time_src & SYS_TIME_SRC_MAVLINK)) { - PX4_INFO("Setting system clock from SYSTEM_TIME sent by %d/%d", msg->sysid, msg->compid); + PX4_INFO("Setting system clock from SYSTEM_TIME sent by %" PRIu32 "/%d", msg->sysid, msg->compid); tv.tv_sec = time.time_unix_usec / 1000000ULL; tv.tv_nsec = (time.time_unix_usec % 1000000ULL) * 1000ULL; diff --git a/src/modules/mavlink/mavlink_ulog.cpp b/src/modules/mavlink/mavlink_ulog.cpp index 64b9ec981d2..ec6563e9860 100644 --- a/src/modules/mavlink/mavlink_ulog.cpp +++ b/src/modules/mavlink/mavlink_ulog.cpp @@ -48,7 +48,7 @@ MavlinkULog *MavlinkULog::_instance = nullptr; px4_sem_t MavlinkULog::_lock; -MavlinkULog::MavlinkULog(int datarate, float max_rate_factor, uint8_t target_system, uint8_t target_component) +MavlinkULog::MavlinkULog(int datarate, float max_rate_factor, uint32_t target_system, uint8_t target_component) : _target_system(target_system), _target_component(target_component), _max_rate_factor(max_rate_factor), _max_num_messages(math::max(1, (int)ceilf((_rate_calculation_delta_t / 1e6f) * _max_rate_factor * datarate / @@ -119,10 +119,9 @@ int MavlinkULog::handle_update(mavlink_channel_t channel) msg.sequence = ulog_data.msg_sequence; msg.length = ulog_data.length; msg.first_message_offset = ulog_data.first_message_offset; - msg.target_system = _target_system; msg.target_component = _target_component; memcpy(msg.data, ulog_data.data, sizeof(msg.data)); - mavlink_msg_logging_data_acked_send_struct(channel, &msg); + mavlink_msg_send_struct_target(channel, LOGGING_DATA_ACKED, &msg, _target_system); } } } @@ -156,20 +155,18 @@ int MavlinkULog::handle_update(mavlink_channel_t channel) msg.sequence = ulog_data.msg_sequence; msg.length = ulog_data.length; msg.first_message_offset = ulog_data.first_message_offset; - msg.target_system = _target_system; msg.target_component = _target_component; memcpy(msg.data, ulog_data.data, sizeof(msg.data)); - mavlink_msg_logging_data_acked_send_struct(channel, &msg); + mavlink_msg_send_struct_target(channel, LOGGING_DATA_ACKED, &msg, _target_system); } else { mavlink_logging_data_t msg; msg.sequence = ulog_data.msg_sequence; msg.length = ulog_data.length; msg.first_message_offset = ulog_data.first_message_offset; - msg.target_system = _target_system; msg.target_component = _target_component; memcpy(msg.data, ulog_data.data, sizeof(msg.data)); - mavlink_msg_logging_data_send_struct(channel, &msg); + mavlink_msg_send_struct_target(channel, LOGGING_DATA, &msg, _target_system); } } @@ -206,7 +203,7 @@ void MavlinkULog::initialize() _init = true; } -MavlinkULog *MavlinkULog::try_start(int datarate, float max_rate_factor, uint8_t target_system, +MavlinkULog *MavlinkULog::try_start(int datarate, float max_rate_factor, uint32_t target_system, uint8_t target_component) { MavlinkULog *ret = nullptr; diff --git a/src/modules/mavlink/mavlink_ulog.h b/src/modules/mavlink/mavlink_ulog.h index 7c96553c6d4..38ce76475da 100644 --- a/src/modules/mavlink/mavlink_ulog.h +++ b/src/modules/mavlink/mavlink_ulog.h @@ -77,7 +77,7 @@ public: * @param target_component ID for mavlink message * @return instance, or nullptr */ - static MavlinkULog *try_start(int datarate, float max_rate_factor, uint8_t target_system, uint8_t target_component); + static MavlinkULog *try_start(int datarate, float max_rate_factor, uint32_t target_system, uint8_t target_component); /** * stop the stream. It also deletes the singleton object, so make sure cleanup @@ -103,7 +103,7 @@ public: private: - MavlinkULog(int datarate, float max_rate_factor, uint8_t target_system, uint8_t target_component); + MavlinkULog(int datarate, float max_rate_factor, uint32_t target_system, uint8_t target_component); ~MavlinkULog(); @@ -131,7 +131,7 @@ private: volatile bool _ack_received = false; ///< set to true if a matching ack received hrt_abstime _last_sent_time = 0; ///< last time we sent a message that requires an ack bool _waiting_for_initial_ack = false; - const uint8_t _target_system; + const uint32_t _target_system; const uint8_t _target_component; const float _max_rate_factor; ///< maximum rate percentage at which we're allowed to push data diff --git a/src/modules/mavlink/streams/CAMERA_TRIGGER.hpp b/src/modules/mavlink/streams/CAMERA_TRIGGER.hpp index 3d475932bf6..6011ea0559c 100644 --- a/src/modules/mavlink/streams/CAMERA_TRIGGER.hpp +++ b/src/modules/mavlink/streams/CAMERA_TRIGGER.hpp @@ -109,7 +109,6 @@ private: /* send MAV_CMD_DO_DIGICAM_CONTROL*/ mavlink_command_long_t command_long_msg{}; - command_long_msg.target_system = _camera_status.active_sys_id; command_long_msg.target_component = i_camera + MAV_COMP_ID_CAMERA; command_long_msg.command = MAV_CMD_DO_DIGICAM_CONTROL; command_long_msg.confirmation = 0; @@ -121,7 +120,8 @@ private: command_long_msg.param6 = NAN; command_long_msg.param7 = NAN; - mavlink_msg_command_long_send_struct(_mavlink->get_channel(), &command_long_msg); + mavlink_msg_send_struct_target(_mavlink->get_channel(), COMMAND_LONG, &command_long_msg, + _camera_status.active_sys_id); } return true; diff --git a/src/modules/mavlink/streams/COMMAND_LONG.hpp b/src/modules/mavlink/streams/COMMAND_LONG.hpp index 89f4a77dbe4..202232293dc 100644 --- a/src/modules/mavlink/streams/COMMAND_LONG.hpp +++ b/src/modules/mavlink/streams/COMMAND_LONG.hpp @@ -88,13 +88,13 @@ private: && (cmd.source_component == cmd.target_component); if (!cmd.from_external && !px4_internal_cmd && !target_system_internal) { - PX4_DEBUG("sending command %ld to %d/%d", cmd.command, cmd.target_system, cmd.target_component); + PX4_DEBUG("sending command %ld to %" PRIu32 "/%d", cmd.command, cmd.target_system, cmd.target_component); MavlinkCommandSender::instance().handle_vehicle_command(cmd, _mavlink->get_channel()); sent = true; } else { - PX4_DEBUG("not forwarding command %ld to %d/%d", cmd.command, cmd.target_system, cmd.target_component); + PX4_DEBUG("not forwarding command %ld to %" PRIu32 "/%d", cmd.command, cmd.target_system, cmd.target_component); } } } diff --git a/src/modules/mavlink/streams/MANUAL_CONTROL.hpp b/src/modules/mavlink/streams/MANUAL_CONTROL.hpp index d62aec1b0a3..6e251fd7b95 100644 --- a/src/modules/mavlink/streams/MANUAL_CONTROL.hpp +++ b/src/modules/mavlink/streams/MANUAL_CONTROL.hpp @@ -67,7 +67,7 @@ private: if (_manual_control_setpoint_sub.update(&manual_control_setpoint)) { mavlink_manual_control_t msg{}; - msg.target = mavlink_system.sysid; + msg.target = mavlink_msg_target_field(mavlink_system.sysid); msg.x = manual_control_setpoint.pitch * 1000.f; msg.y = manual_control_setpoint.roll * 1000.f; // Convert from internal range [-1, 1] to [0, 1000] @@ -116,7 +116,10 @@ private: msg.aux6 = manual_control_setpoint.aux6 * 1000.f; } - mavlink_msg_manual_control_send_struct(_mavlink->get_channel(), &msg); + // The target field is called "target" here, so mavlink_msg_send_struct_target() doesn't fit. + _mav_finalize_message_chan_send_target(_mavlink->get_channel(), MAVLINK_MSG_ID_MANUAL_CONTROL, (const char *)&msg, + MAVLINK_MSG_ID_MANUAL_CONTROL_MIN_LEN, MAVLINK_MSG_ID_MANUAL_CONTROL_LEN, + MAVLINK_MSG_ID_MANUAL_CONTROL_CRC, mavlink_system.sysid); return true; }