#pragma once #include "string.h" #include "mavlink_types.h" /* If you want MAVLink on a system that is native big-endian, you need to define NATIVE_BIG_ENDIAN */ #ifdef NATIVE_BIG_ENDIAN # define MAVLINK_NEED_BYTE_SWAP (MAVLINK_ENDIAN == MAVLINK_LITTLE_ENDIAN) #else # define MAVLINK_NEED_BYTE_SWAP (MAVLINK_ENDIAN != MAVLINK_LITTLE_ENDIAN) #endif #ifndef MAVLINK_STACK_BUFFER #define MAVLINK_STACK_BUFFER 0 #endif #ifndef MAVLINK_AVOID_GCC_STACK_BUG # define MAVLINK_AVOID_GCC_STACK_BUG defined(__GNUC__) #endif #ifndef MAVLINK_ASSERT #define MAVLINK_ASSERT(x) #endif #ifndef MAVLINK_START_UART_SEND #define MAVLINK_START_UART_SEND(chan, length) #endif #ifndef MAVLINK_END_UART_SEND #define MAVLINK_END_UART_SEND(chan, length) #endif /* to get warnings when any WIP message is used, add this: #define MAVLINK_WIP __attribute__((warning("MAVLink work in progress"))) */ #ifndef MAVLINK_WIP #define MAVLINK_WIP #endif #define MAVLINK_HAVE_GET_TARGET_SYSTEM #define MAVLINK_HAVE_GET_TARGET_SYSID /** * @brief Map a full system target to its legacy uint8_t payload field. * Wide targets use the non-broadcast sentinel; also pass the full target to * the _target() send/finalize helper so it is retained in the header. */ static inline uint8_t mavlink_msg_target_field(uint32_t target_system) { return target_system > UINT8_MAX ? MAVLINK_TARGET_SYSTEM_SENTINEL : (uint8_t)target_system; } /* option to provide alternative implementation of mavlink_helpers.h */ #ifdef MAVLINK_SEPARATE_HELPERS #define MAVLINK_HELPER /* decls in sync with those in mavlink_helpers.h */ #ifndef MAVLINK_GET_CHANNEL_STATUS MAVLINK_HELPER mavlink_status_t* mavlink_get_channel_status(uint8_t chan); #endif MAVLINK_HELPER void mavlink_reset_channel_status(uint8_t chan); MAVLINK_HELPER uint8_t _mav_header_pack(uint8_t *buf, uint8_t magic, uint8_t len, uint8_t incompat_flags, uint8_t compat_flags, uint8_t seq, uint32_t sysid, uint8_t compid, uint32_t msgid, uint32_t target_sysid); MAVLINK_HELPER uint8_t mavlink_header_to_send_buffer(uint8_t *buf, const mavlink_message_t *msg); MAVLINK_HELPER uint16_t mavlink_finalize_message_buffer_target(mavlink_message_t* msg, uint32_t system_id, uint8_t component_id, mavlink_status_t* status, uint8_t min_length, uint8_t length, uint8_t crc_extra, uint32_t target_sysid); MAVLINK_HELPER uint16_t mavlink_finalize_message_buffer(mavlink_message_t* msg, uint32_t system_id, uint8_t component_id, mavlink_status_t* status, uint8_t min_length, uint8_t length, uint8_t crc_extra); MAVLINK_HELPER uint16_t mavlink_finalize_message_chan_target(mavlink_message_t* msg, uint32_t system_id, uint8_t component_id, uint8_t chan, uint8_t min_length, uint8_t length, uint8_t crc_extra, uint32_t target_sysid); MAVLINK_HELPER uint16_t mavlink_finalize_message_chan(mavlink_message_t* msg, uint32_t system_id, uint8_t component_id, uint8_t chan, uint8_t min_length, uint8_t length, uint8_t crc_extra); MAVLINK_HELPER uint16_t mavlink_finalize_message_target(mavlink_message_t* msg, uint32_t system_id, uint8_t component_id, uint8_t min_length, uint8_t length, uint8_t crc_extra, uint32_t target_sysid); MAVLINK_HELPER uint16_t mavlink_finalize_message(mavlink_message_t* msg, uint32_t system_id, uint8_t component_id, uint8_t min_length, uint8_t length, uint8_t crc_extra); #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS MAVLINK_HELPER void _mav_finalize_message_chan_send_target(mavlink_channel_t chan, uint32_t msgid, const char *packet, uint8_t min_length, uint8_t length, uint8_t crc_extra, uint32_t target_sysid); MAVLINK_HELPER void _mav_finalize_message_chan_send(mavlink_channel_t chan, uint32_t msgid, const char *packet, uint8_t min_length, uint8_t length, uint8_t crc_extra); #endif MAVLINK_HELPER uint16_t mavlink_msg_to_send_buffer(uint8_t *buffer, const mavlink_message_t *msg); MAVLINK_HELPER bool mavlink_msg_get_target_system(const mavlink_message_t *msg, const uint8_t *target_system_ptr, uint32_t *target_system); MAVLINK_HELPER uint32_t mavlink_msg_get_target_sysid(const mavlink_message_t *msg, const mavlink_msg_entry_t *entry); MAVLINK_HELPER uint8_t mavlink_msg_get_target_compid(const mavlink_message_t *msg, const mavlink_msg_entry_t *entry); MAVLINK_HELPER void mavlink_start_checksum(mavlink_message_t* msg); MAVLINK_HELPER void mavlink_update_checksum(mavlink_message_t* msg, uint8_t c); MAVLINK_HELPER uint8_t mavlink_frame_char_buffer(mavlink_message_t* rxmsg, mavlink_status_t* status, uint8_t c, mavlink_message_t* r_message, mavlink_status_t* r_mavlink_status); MAVLINK_HELPER uint8_t mavlink_frame_char(uint8_t chan, uint8_t c, mavlink_message_t* r_message, mavlink_status_t* r_mavlink_status); MAVLINK_HELPER uint8_t mavlink_parse_char(uint8_t chan, uint8_t c, mavlink_message_t* r_message, mavlink_status_t* r_mavlink_status); MAVLINK_HELPER const mavlink_msg_entry_t *mavlink_get_msg_entry(uint32_t msgid); #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS MAVLINK_HELPER void _mavlink_send_uart(mavlink_channel_t chan, const char *buf, uint16_t len); MAVLINK_HELPER void _mavlink_resend_uart(mavlink_channel_t chan, const mavlink_message_t *msg); #endif #else #define MAVLINK_HELPER static inline #include "mavlink_helpers.h" #endif // MAVLINK_SEPARATE_HELPERS /** * @brief Get the required buffer size for this message */ static inline uint16_t mavlink_msg_get_send_buffer_length(const mavlink_message_t* msg) { if (msg->magic == MAVLINK_STX_MAVLINK1) { return msg->len + MAVLINK_CORE_HEADER_MAVLINK1_LEN+1 + 2; } uint16_t signature_len = (msg->incompat_flags & MAVLINK_IFLAG_SIGNED)?MAVLINK_SIGNATURE_BLOCK_LEN:0; uint16_t ext_header_len = 0; if (msg->incompat_flags & MAVLINK_IFLAG_SYSID32) { ext_header_len += MAVLINK_SYSID32_HEADER_EXTRA; } if (msg->incompat_flags & MAVLINK_IFLAG_TARGET32) { ext_header_len += MAVLINK_TARGET32_HEADER_EXTRA; } return msg->len + MAVLINK_NUM_NON_PAYLOAD_BYTES + ext_header_len + signature_len; } #if MAVLINK_NEED_BYTE_SWAP static inline void byte_swap_2(char *dst, const char *src) { dst[0] = src[1]; dst[1] = src[0]; } static inline void byte_swap_4(char *dst, const char *src) { dst[0] = src[3]; dst[1] = src[2]; dst[2] = src[1]; dst[3] = src[0]; } static inline void byte_swap_8(char *dst, const char *src) { dst[0] = src[7]; dst[1] = src[6]; dst[2] = src[5]; dst[3] = src[4]; dst[4] = src[3]; dst[5] = src[2]; dst[6] = src[1]; dst[7] = src[0]; } #elif !MAVLINK_ALIGNED_FIELDS static inline void byte_copy_2(char *dst, const char *src) { dst[0] = src[0]; dst[1] = src[1]; } static inline void byte_copy_4(char *dst, const char *src) { dst[0] = src[0]; dst[1] = src[1]; dst[2] = src[2]; dst[3] = src[3]; } static inline void byte_copy_8(char *dst, const char *src) { memcpy(dst, src, 8); } #endif #define _mav_put_uint8_t(buf, wire_offset, b) buf[wire_offset] = (uint8_t)b #define _mav_put_int8_t(buf, wire_offset, b) buf[wire_offset] = (int8_t)b #define _mav_put_char(buf, wire_offset, b) buf[wire_offset] = b #if MAVLINK_NEED_BYTE_SWAP #define _mav_put_uint16_t(buf, wire_offset, b) byte_swap_2(&buf[wire_offset], (const char *)&b) #define _mav_put_int16_t(buf, wire_offset, b) byte_swap_2(&buf[wire_offset], (const char *)&b) #define _mav_put_uint32_t(buf, wire_offset, b) byte_swap_4(&buf[wire_offset], (const char *)&b) #define _mav_put_int32_t(buf, wire_offset, b) byte_swap_4(&buf[wire_offset], (const char *)&b) #define _mav_put_uint64_t(buf, wire_offset, b) byte_swap_8(&buf[wire_offset], (const char *)&b) #define _mav_put_int64_t(buf, wire_offset, b) byte_swap_8(&buf[wire_offset], (const char *)&b) #define _mav_put_float(buf, wire_offset, b) byte_swap_4(&buf[wire_offset], (const char *)&b) #define _mav_put_double(buf, wire_offset, b) byte_swap_8(&buf[wire_offset], (const char *)&b) #elif !MAVLINK_ALIGNED_FIELDS #define _mav_put_uint16_t(buf, wire_offset, b) byte_copy_2(&buf[wire_offset], (const char *)&b) #define _mav_put_int16_t(buf, wire_offset, b) byte_copy_2(&buf[wire_offset], (const char *)&b) #define _mav_put_uint32_t(buf, wire_offset, b) byte_copy_4(&buf[wire_offset], (const char *)&b) #define _mav_put_int32_t(buf, wire_offset, b) byte_copy_4(&buf[wire_offset], (const char *)&b) #define _mav_put_uint64_t(buf, wire_offset, b) byte_copy_8(&buf[wire_offset], (const char *)&b) #define _mav_put_int64_t(buf, wire_offset, b) byte_copy_8(&buf[wire_offset], (const char *)&b) #define _mav_put_float(buf, wire_offset, b) byte_copy_4(&buf[wire_offset], (const char *)&b) #define _mav_put_double(buf, wire_offset, b) byte_copy_8(&buf[wire_offset], (const char *)&b) #else #define _mav_put_uint16_t(buf, wire_offset, b) *(uint16_t *)&buf[wire_offset] = b #define _mav_put_int16_t(buf, wire_offset, b) *(int16_t *)&buf[wire_offset] = b #define _mav_put_uint32_t(buf, wire_offset, b) *(uint32_t *)&buf[wire_offset] = b #define _mav_put_int32_t(buf, wire_offset, b) *(int32_t *)&buf[wire_offset] = b #define _mav_put_uint64_t(buf, wire_offset, b) *(uint64_t *)&buf[wire_offset] = b #define _mav_put_int64_t(buf, wire_offset, b) *(int64_t *)&buf[wire_offset] = b #define _mav_put_float(buf, wire_offset, b) *(float *)&buf[wire_offset] = b #define _mav_put_double(buf, wire_offset, b) *(double *)&buf[wire_offset] = b #endif /* like memcpy(), but if src is NULL, do a memset to zero */ static inline void mav_array_memcpy(void *dest, const void *src, size_t n) { // It would be tempting to do a strcpy/strncpy for the char[] type. Unfortunately, some // existing MAVLink messages such as PARAM_EXT_VALUE.param_value use the char[] type for // arbitrary data (including null), and would break. // // It would be nice to change such char[] types to uint8_t[] but that would change the // CRC_EXTRA. if (src == NULL) { memset(dest, 0, n); } else { memcpy(dest, src, n); } } /* * Place a char array into a buffer */ static inline void _mav_put_char_array(char *buf, uint8_t wire_offset, const char *b, uint8_t array_length) { mav_array_memcpy(&buf[wire_offset], b, array_length); } /* * Place a uint8_t array into a buffer */ static inline void _mav_put_uint8_t_array(char *buf, uint8_t wire_offset, const uint8_t *b, uint8_t array_length) { mav_array_memcpy(&buf[wire_offset], b, array_length); } /* * Place a int8_t array into a buffer */ static inline void _mav_put_int8_t_array(char *buf, uint8_t wire_offset, const int8_t *b, uint8_t array_length) { mav_array_memcpy(&buf[wire_offset], b, array_length); } #if MAVLINK_NEED_BYTE_SWAP #define _MAV_PUT_ARRAY(TYPE, V) \ static inline void _mav_put_ ## TYPE ##_array(char *buf, uint8_t wire_offset, const TYPE *b, uint8_t array_length) \ { \ if (b == NULL) { \ memset(&buf[wire_offset], 0, array_length*sizeof(TYPE)); \ } else { \ uint16_t i; \ for (i=0; i