From 902f1e6476046d77db84b85c5124537aab7b2178 Mon Sep 17 00:00:00 2001 From: Iampete1 Date: Fri, 27 Sep 2024 18:29:32 +0100 Subject: [PATCH] AP_Scripting: modules: MAVLink: update to latest version of generated code --- .../MAVLink/mavlink_msg_COMMAND_LONG.lua | 1 + .../modules/MAVLink/mavlink_msgs.lua | 94 ++++++++++++++++--- 2 files changed, 81 insertions(+), 14 deletions(-) diff --git a/libraries/AP_Scripting/modules/MAVLink/mavlink_msg_COMMAND_LONG.lua b/libraries/AP_Scripting/modules/MAVLink/mavlink_msg_COMMAND_LONG.lua index ba40100f41ae0b..7302529ea4c308 100644 --- a/libraries/AP_Scripting/modules/MAVLink/mavlink_msg_COMMAND_LONG.lua +++ b/libraries/AP_Scripting/modules/MAVLink/mavlink_msg_COMMAND_LONG.lua @@ -1,5 +1,6 @@ local COMMAND_LONG = {} COMMAND_LONG.id = 76 +COMMAND_LONG.crc_extra = 152 COMMAND_LONG.fields = { { "param1", "> 8) ~ (tmp << 8) ~ (tmp << 3) ~ (tmp >> 4) + crc = crc & 0xFFFF + end + return crc +end + +-- Note that this does not parse the serial data, it parses the MAVLink 2 C structure `mavlink_message_t` +-- This structure is passed in by the ArduPilot bindings as a string +---@param message any -- encoded message +---@param msg_map table -- table containing message objects with keys of the message ID +---@return table|nil -- a table representing the contents of the message, or nill if decode failed function mavlink_msgs.decode(message, msg_map) - local result, offset = mavlink_msgs.decode_header(message) + local result = mavlink_msgs.decode_header(message) local message_map = require("MAVLink/mavlink_msg_" .. msg_map[result.msgid]) if not message_map then -- we don't know how to decode this message, bail on it return nil end + -- If we have a crc extra for this message then check it + -- This ensures compatibility with message definitions generated before the crc check was added + if message_map.crc_extra then + -- crc of payload and header values + local crc_buffer + if result.protocol_version == 2 then + crc_buffer = string.sub(message, 4, 12 + result.payload_length) + + else + -- V1 does not include all fields on the wire + crc_buffer = string.char(result.payload_length) + crc_buffer = crc_buffer .. string.char(result.seq) + crc_buffer = crc_buffer .. string.char(result.sysid) + crc_buffer = crc_buffer .. string.char(result.compid) + crc_buffer = crc_buffer .. string.char(result.msgid) + if result.payload_length > 0 then + crc_buffer = crc_buffer .. string.sub(message, 13, 12 + result.payload_length) + end + + end + + local crc = mavlink_msgs.generateCRC(crc_buffer .. string.char(message_map.crc_extra)) + + if crc ~= result.checksum then + -- crc failed + return nil + end + end + -- map all the fields out + local offset = 13 for _,v in ipairs(message_map.fields) do if v[3] then result[v[1]] = {} @@ -55,14 +113,22 @@ function mavlink_msgs.decode(message, msg_map) end else result[v[1]], offset = string.unpack(v[2], message, offset) + if string.sub(v[2],2,2) == 'c' then + -- Got string, unpack includes 0 values to the set length + -- this is annoying, so remove them + result[v[1]] = string.gsub(result[v[1]], string.char(0), "") + end end end - -- ignore the idea of a checksum - - return result; + return result end +---Encode the payload section of a given message +---@param msgname string -- name of message to encode +---@param message table -- table containing key value pairs representing the data fields in the message +---@return integer -- message id +---@return string -- encoded payload function mavlink_msgs.encode(msgname, message) local message_map = require("MAVLink/mavlink_msg_" .. msgname) if not message_map then