1 #ifndef _MAVLINK_PROTOCOL_H_
2 #define _MAVLINK_PROTOCOL_H_
11 #ifdef NATIVE_BIG_ENDIAN
12 # define MAVLINK_NEED_BYTE_SWAP (MAVLINK_ENDIAN == MAVLINK_LITTLE_ENDIAN)
14 # define MAVLINK_NEED_BYTE_SWAP (MAVLINK_ENDIAN != MAVLINK_LITTLE_ENDIAN)
17 #ifndef MAVLINK_STACK_BUFFER
18 #define MAVLINK_STACK_BUFFER 0
21 #ifndef MAVLINK_AVOID_GCC_STACK_BUG
22 # define MAVLINK_AVOID_GCC_STACK_BUG defined(__GNUC__)
25 #ifndef MAVLINK_ASSERT
26 #define MAVLINK_ASSERT(x)
29 #ifndef MAVLINK_START_UART_SEND
30 #define MAVLINK_START_UART_SEND(chan, length)
33 #ifndef MAVLINK_END_UART_SEND
34 #define MAVLINK_END_UART_SEND(chan, length)
38 #ifdef MAVLINK_SEPARATE_HELPERS
40 #define MAVLINK_HELPER
43 #ifndef MAVLINK_GET_CHANNEL_STATUS
49 uint8_t chan, uint8_t length, uint8_t crc_extra);
51 uint8_t length, uint8_t crc_extra);
52 #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
54 uint8_t length, uint8_t crc_extra);
58 uint8_t chan, uint8_t length);
61 #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
64 #endif // MAVLINK_CRC_EXTRA
70 uint8_t* r_bit_index, uint8_t* buffer);
71 #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
78 #define MAVLINK_HELPER static inline
81 #endif // MAVLINK_SEPARATE_HELPERS
86 static inline uint16_t mavlink_msg_get_send_buffer_length(
const mavlink_message_t* msg)
91 #if MAVLINK_NEED_BYTE_SWAP
92 static inline void byte_swap_2(
char *dst,
const char *src)
97 static inline void byte_swap_4(
char *dst,
const char *src)
104 static inline void byte_swap_8(
char *dst,
const char *src)
115 #elif !MAVLINK_ALIGNED_FIELDS
116 static inline void byte_copy_2(
char *dst,
const char *src)
121 static inline void byte_copy_4(
char *dst,
const char *src)
128 static inline void byte_copy_8(
char *dst,
const char *src)
134 #define _mav_put_uint8_t(buf, wire_offset, b) buf[wire_offset] = (uint8_t)b
135 #define _mav_put_int8_t(buf, wire_offset, b) buf[wire_offset] = (int8_t)b
136 #define _mav_put_char(buf, wire_offset, b) buf[wire_offset] = b
138 #if MAVLINK_NEED_BYTE_SWAP
139 #define _mav_put_uint16_t(buf, wire_offset, b) byte_swap_2(&buf[wire_offset], (const char *)&b)
140 #define _mav_put_int16_t(buf, wire_offset, b) byte_swap_2(&buf[wire_offset], (const char *)&b)
141 #define _mav_put_uint32_t(buf, wire_offset, b) byte_swap_4(&buf[wire_offset], (const char *)&b)
142 #define _mav_put_int32_t(buf, wire_offset, b) byte_swap_4(&buf[wire_offset], (const char *)&b)
143 #define _mav_put_uint64_t(buf, wire_offset, b) byte_swap_8(&buf[wire_offset], (const char *)&b)
144 #define _mav_put_int64_t(buf, wire_offset, b) byte_swap_8(&buf[wire_offset], (const char *)&b)
145 #define _mav_put_float(buf, wire_offset, b) byte_swap_4(&buf[wire_offset], (const char *)&b)
146 #define _mav_put_double(buf, wire_offset, b) byte_swap_8(&buf[wire_offset], (const char *)&b)
147 #elif !MAVLINK_ALIGNED_FIELDS
148 #define _mav_put_uint16_t(buf, wire_offset, b) byte_copy_2(&buf[wire_offset], (const char *)&b)
149 #define _mav_put_int16_t(buf, wire_offset, b) byte_copy_2(&buf[wire_offset], (const char *)&b)
150 #define _mav_put_uint32_t(buf, wire_offset, b) byte_copy_4(&buf[wire_offset], (const char *)&b)
151 #define _mav_put_int32_t(buf, wire_offset, b) byte_copy_4(&buf[wire_offset], (const char *)&b)
152 #define _mav_put_uint64_t(buf, wire_offset, b) byte_copy_8(&buf[wire_offset], (const char *)&b)
153 #define _mav_put_int64_t(buf, wire_offset, b) byte_copy_8(&buf[wire_offset], (const char *)&b)
154 #define _mav_put_float(buf, wire_offset, b) byte_copy_4(&buf[wire_offset], (const char *)&b)
155 #define _mav_put_double(buf, wire_offset, b) byte_copy_8(&buf[wire_offset], (const char *)&b)
157 #define _mav_put_uint16_t(buf, wire_offset, b) *(uint16_t *)&buf[wire_offset] = b
158 #define _mav_put_int16_t(buf, wire_offset, b) *(int16_t *)&buf[wire_offset] = b
159 #define _mav_put_uint32_t(buf, wire_offset, b) *(uint32_t *)&buf[wire_offset] = b
160 #define _mav_put_int32_t(buf, wire_offset, b) *(int32_t *)&buf[wire_offset] = b
161 #define _mav_put_uint64_t(buf, wire_offset, b) *(uint64_t *)&buf[wire_offset] = b
162 #define _mav_put_int64_t(buf, wire_offset, b) *(int64_t *)&buf[wire_offset] = b
163 #define _mav_put_float(buf, wire_offset, b) *(float *)&buf[wire_offset] = b
164 #define _mav_put_double(buf, wire_offset, b) *(double *)&buf[wire_offset] = b
170 static inline void mav_array_memcpy(
void *dest,
const void *src,
size_t n)
175 memcpy(dest, src, n);
182 static inline void _mav_put_char_array(
char *buf, uint8_t wire_offset,
const char *b, uint8_t array_length)
184 mav_array_memcpy(&buf[wire_offset], b, array_length);
191 static inline void _mav_put_uint8_t_array(
char *buf, uint8_t wire_offset,
const uint8_t *b, uint8_t array_length)
193 mav_array_memcpy(&buf[wire_offset], b, array_length);
200 static inline void _mav_put_int8_t_array(
char *buf, uint8_t wire_offset,
const int8_t *b, uint8_t array_length)
202 mav_array_memcpy(&buf[wire_offset], b, array_length);
206 #if MAVLINK_NEED_BYTE_SWAP
207 #define _MAV_PUT_ARRAY(TYPE, V) \
208 static inline void _mav_put_ ## TYPE ##_array(char *buf, uint8_t wire_offset, const TYPE *b, uint8_t array_length) \
211 memset(&buf[wire_offset], 0, array_length*sizeof(TYPE)); \
214 for (i=0; i<array_length; i++) { \
215 _mav_put_## TYPE (buf, wire_offset+(i*sizeof(TYPE)), b[i]); \
220 #define _MAV_PUT_ARRAY(TYPE, V) \
221 static inline void _mav_put_ ## TYPE ##_array(char *buf, uint8_t wire_offset, const TYPE *b, uint8_t array_length) \
223 mav_array_memcpy(&buf[wire_offset], b, array_length*sizeof(TYPE)); \
236 #define _MAV_RETURN_char(msg, wire_offset) (const char)_MAV_PAYLOAD(msg)[wire_offset]
237 #define _MAV_RETURN_int8_t(msg, wire_offset) (const int8_t)_MAV_PAYLOAD(msg)[wire_offset]
238 #define _MAV_RETURN_uint8_t(msg, wire_offset) (const uint8_t)_MAV_PAYLOAD(msg)[wire_offset]
240 #if MAVLINK_NEED_BYTE_SWAP
241 #define _MAV_MSG_RETURN_TYPE(TYPE, SIZE) \
242 static inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
243 { TYPE r; byte_swap_## SIZE((char*)&r, &_MAV_PAYLOAD(msg)[ofs]); return r; }
254 #elif !MAVLINK_ALIGNED_FIELDS
255 #define _MAV_MSG_RETURN_TYPE(TYPE, SIZE) \
256 static inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
257 { TYPE r; byte_copy_## SIZE((char*)&r, &_MAV_PAYLOAD(msg)[ofs]); return r; }
267 #else // nicely aligned, no swap
268 #define _MAV_MSG_RETURN_TYPE(TYPE) \
269 static inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
270 { return *(const TYPE *)(&_MAV_PAYLOAD(msg)[ofs]);}
280 #endif // MAVLINK_NEED_BYTE_SWAP
282 static inline uint16_t _MAV_RETURN_char_array(
const mavlink_message_t *msg,
char *value,
283 uint8_t array_length, uint8_t wire_offset)
285 memcpy(value, &
_MAV_PAYLOAD(msg)[wire_offset], array_length);
289 static inline uint16_t _MAV_RETURN_uint8_t_array(
const mavlink_message_t *msg, uint8_t *value,
290 uint8_t array_length, uint8_t wire_offset)
292 memcpy(value, &
_MAV_PAYLOAD(msg)[wire_offset], array_length);
296 static inline uint16_t _MAV_RETURN_int8_t_array(
const mavlink_message_t *msg, int8_t *value,
297 uint8_t array_length, uint8_t wire_offset)
299 memcpy(value, &
_MAV_PAYLOAD(msg)[wire_offset], array_length);
303 #if MAVLINK_NEED_BYTE_SWAP
304 #define _MAV_RETURN_ARRAY(TYPE, V) \
305 static inline uint16_t _MAV_RETURN_## TYPE ##_array(const mavlink_message_t *msg, TYPE *value, \
306 uint8_t array_length, uint8_t wire_offset) \
309 for (i=0; i<array_length; i++) { \
310 value[i] = _MAV_RETURN_## TYPE (msg, wire_offset+(i*sizeof(value[0]))); \
312 return array_length*sizeof(value[0]); \
315 #define _MAV_RETURN_ARRAY(TYPE, V) \
316 static inline uint16_t _MAV_RETURN_## TYPE ##_array(const mavlink_message_t *msg, TYPE *value, \
317 uint8_t array_length, uint8_t wire_offset) \
319 memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length*sizeof(TYPE)); \
320 return array_length*sizeof(TYPE); \
333 #endif // _MAVLINK_PROTOCOL_H_
MAVLINK_HELPER void mavlink_reset_channel_status(uint8_t chan)
Reset the status of a channel.
Definition: mavlink_helpers.h:49
MAVLINK_HELPER uint8_t mavlink_parse_char(uint8_t chan, uint8_t c, mavlink_message_t *r_message, mavlink_status_t *r_mavlink_status)
Definition: mavlink_helpers.h:247
Definition: mavlink_types.h:211
MAVLINK_HELPER uint8_t put_bitfield_n_by_index(int32_t b, uint8_t bits, uint8_t packet_index, uint8_t bit_index, uint8_t *r_bit_index, uint8_t *buffer)
Put a bitfield of length 1-32 bit into the buffer.
Definition: mavlink_helpers.h:447
#define _MAV_MSG_RETURN_TYPE(TYPE, SIZE)
Definition: protocol.h:241
MAVLINK_HELPER mavlink_status_t * mavlink_get_channel_status(uint8_t chan)
Definition: mavlink_helpers.h:17
#define MAVLINK_NUM_NON_PAYLOAD_BYTES
Definition: mavlink_types.h:31
MAVLINK_HELPER uint16_t mavlink_finalize_message(mavlink_message_t *msg, uint8_t system_id, uint8_t component_id, uint8_t length)
Finalize a MAVLink message with MAVLINK_COMM_0 as default channel.
Definition: mavlink_helpers.h:105
#define _MAV_RETURN_ARRAY(TYPE, V)
Definition: protocol.h:304
mavlink_channel_t
Definition: mavlink_types.h:178
#define _MAV_PAYLOAD(msg)
Definition: mavlink_types.h:171
MAVLINK_HELPER void mavlink_update_checksum(mavlink_message_t *msg, uint8_t c)
Definition: mavlink_helpers.h:201
#define _MAV_PUT_ARRAY(TYPE, V)
Definition: protocol.h:207
MAVLINK_HELPER uint16_t mavlink_msg_to_send_buffer(uint8_t *buffer, const mavlink_message_t *msg)
Pack a message to send it over a serial byte stream.
Definition: mavlink_helpers.h:174
#define MAVLINK_HELPER
Definition: protocol.h:78
MAVLINK_HELPER void mavlink_start_checksum(mavlink_message_t *msg)
Definition: mavlink_helpers.h:196
MAVLINK_HELPER uint16_t mavlink_finalize_message_chan(mavlink_message_t *msg, uint8_t system_id, uint8_t component_id, uint8_t chan, uint8_t length)
Finalize a MAVLink message with channel assignment.
Definition: mavlink_helpers.h:71