Arduino Mavlink
Loading...
Searching...
No Matches
protocol.h
Go to the documentation of this file.
1#pragma once
2
3#include "string.h"
5
6/*
7 If you want MAVLink on a system that is native big-endian,
8 you need to define NATIVE_BIG_ENDIAN
9*/
10#ifdef NATIVE_BIG_ENDIAN
11# define MAVLINK_NEED_BYTE_SWAP (MAVLINK_ENDIAN == MAVLINK_LITTLE_ENDIAN)
12#else
13# define MAVLINK_NEED_BYTE_SWAP (MAVLINK_ENDIAN != MAVLINK_LITTLE_ENDIAN)
14#endif
15
16#ifndef MAVLINK_STACK_BUFFER
17#define MAVLINK_STACK_BUFFER 0
18#endif
19
20#ifndef MAVLINK_AVOID_GCC_STACK_BUG
21# define MAVLINK_AVOID_GCC_STACK_BUG defined(__GNUC__)
22#endif
23
24#ifndef MAVLINK_ASSERT
25#define MAVLINK_ASSERT(x)
26#endif
27
28#ifndef MAVLINK_START_UART_SEND
29#define MAVLINK_START_UART_SEND(chan, length)
30#endif
31
32#ifndef MAVLINK_END_UART_SEND
33#define MAVLINK_END_UART_SEND(chan, length)
34#endif
35
36/*
37 to get warnings when any WIP message is used, add this:
38 #define MAVLINK_WIP __attribute__((warning("MAVLink work in progress")))
39*/
40#ifndef MAVLINK_WIP
41#define MAVLINK_WIP
42#endif
43
44/* option to provide alternative implementation of mavlink_helpers.h */
45#ifdef MAVLINK_SEPARATE_HELPERS
46
47 #define MAVLINK_HELPER
48
49 /* decls in sync with those in mavlink_helpers.h */
50 #ifndef MAVLINK_GET_CHANNEL_STATUS
51 MAVLINK_HELPER mavlink_status_t* mavlink_get_channel_status(uint8_t chan);
52 #endif
53 MAVLINK_HELPER void mavlink_reset_channel_status(uint8_t chan);
54 MAVLINK_HELPER uint16_t mavlink_finalize_message_buffer(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id,
55 mavlink_status_t* status, uint8_t min_length, uint8_t length, uint8_t crc_extra);
56 MAVLINK_HELPER uint16_t mavlink_finalize_message_chan(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id,
57 uint8_t chan, uint8_t min_length, uint8_t length, uint8_t crc_extra);
58 MAVLINK_HELPER uint16_t mavlink_finalize_message(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id,
59 uint8_t min_length, uint8_t length, uint8_t crc_extra);
60 #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
61 MAVLINK_HELPER void _mav_finalize_message_chan_send(mavlink_channel_t chan, uint32_t msgid, const char *packet,
62 uint8_t min_length, uint8_t length, uint8_t crc_extra);
63 #endif
64 MAVLINK_HELPER uint16_t mavlink_msg_to_send_buffer(uint8_t *buffer, const mavlink_message_t *msg);
65 MAVLINK_HELPER void mavlink_start_checksum(mavlink_message_t* msg);
66 MAVLINK_HELPER void mavlink_update_checksum(mavlink_message_t* msg, uint8_t c);
67 MAVLINK_HELPER uint8_t mavlink_frame_char_buffer(mavlink_message_t* rxmsg,
68 mavlink_status_t* status,
69 uint8_t c,
70 mavlink_message_t* r_message,
71 mavlink_status_t* r_mavlink_status);
72 MAVLINK_HELPER uint8_t mavlink_frame_char(uint8_t chan, uint8_t c, mavlink_message_t* r_message, mavlink_status_t* r_mavlink_status);
73 MAVLINK_HELPER uint8_t mavlink_parse_char(uint8_t chan, uint8_t c, mavlink_message_t* r_message, mavlink_status_t* r_mavlink_status);
74 MAVLINK_HELPER uint8_t put_bitfield_n_by_index(int32_t b, uint8_t bits, uint8_t packet_index, uint8_t bit_index,
75 uint8_t* r_bit_index, uint8_t* buffer);
76 MAVLINK_HELPER const mavlink_msg_entry_t *mavlink_get_msg_entry(uint32_t msgid);
77 #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
78 MAVLINK_HELPER void _mavlink_send_uart(mavlink_channel_t chan, const char *buf, uint16_t len);
79 MAVLINK_HELPER void _mavlink_resend_uart(mavlink_channel_t chan, const mavlink_message_t *msg);
80 #endif
81
82#else
83
84 #define MAVLINK_HELPER static inline
85 #include "mavlink_helpers.h"
86
87#endif // MAVLINK_SEPARATE_HELPERS
88
89
90/**
91 * @brief Get the required buffer size for this message
92 */
93static inline uint16_t mavlink_msg_get_send_buffer_length(const mavlink_message_t* msg)
94{
95 if (msg->magic == MAVLINK_STX_MAVLINK1) {
96 return msg->len + MAVLINK_CORE_HEADER_MAVLINK1_LEN+1 + 2;
97 }
98 uint16_t signature_len = (msg->incompat_flags & MAVLINK_IFLAG_SIGNED)?MAVLINK_SIGNATURE_BLOCK_LEN:0;
99 return msg->len + MAVLINK_NUM_NON_PAYLOAD_BYTES + signature_len;
100}
101
103static inline void byte_swap_2(char *dst, const char *src)
104{
105 dst[0] = src[1];
106 dst[1] = src[0];
107}
108static inline void byte_swap_4(char *dst, const char *src)
109{
110 dst[0] = src[3];
111 dst[1] = src[2];
112 dst[2] = src[1];
113 dst[3] = src[0];
114}
115static inline void byte_swap_8(char *dst, const char *src)
116{
117 dst[0] = src[7];
118 dst[1] = src[6];
119 dst[2] = src[5];
120 dst[3] = src[4];
121 dst[4] = src[3];
122 dst[5] = src[2];
123 dst[6] = src[1];
124 dst[7] = src[0];
125}
126#elif !MAVLINK_ALIGNED_FIELDS
127static inline void byte_copy_2(char *dst, const char *src)
128{
129 dst[0] = src[0];
130 dst[1] = src[1];
131}
132static inline void byte_copy_4(char *dst, const char *src)
133{
134 dst[0] = src[0];
135 dst[1] = src[1];
136 dst[2] = src[2];
137 dst[3] = src[3];
138}
139static inline void byte_copy_8(char *dst, const char *src)
140{
141 memcpy(dst, src, 8);
142}
143#endif
144
145#define _mav_put_uint8_t(buf, wire_offset, b) buf[wire_offset] = (uint8_t)b
146#define _mav_put_int8_t(buf, wire_offset, b) buf[wire_offset] = (int8_t)b
147#define _mav_put_char(buf, wire_offset, b) buf[wire_offset] = b
148
150#define _mav_put_uint16_t(buf, wire_offset, b) byte_swap_2(&buf[wire_offset], (const char *)&b)
151#define _mav_put_int16_t(buf, wire_offset, b) byte_swap_2(&buf[wire_offset], (const char *)&b)
152#define _mav_put_uint32_t(buf, wire_offset, b) byte_swap_4(&buf[wire_offset], (const char *)&b)
153#define _mav_put_int32_t(buf, wire_offset, b) byte_swap_4(&buf[wire_offset], (const char *)&b)
154#define _mav_put_uint64_t(buf, wire_offset, b) byte_swap_8(&buf[wire_offset], (const char *)&b)
155#define _mav_put_int64_t(buf, wire_offset, b) byte_swap_8(&buf[wire_offset], (const char *)&b)
156#define _mav_put_float(buf, wire_offset, b) byte_swap_4(&buf[wire_offset], (const char *)&b)
157#define _mav_put_double(buf, wire_offset, b) byte_swap_8(&buf[wire_offset], (const char *)&b)
158#elif !MAVLINK_ALIGNED_FIELDS
159#define _mav_put_uint16_t(buf, wire_offset, b) byte_copy_2(&buf[wire_offset], (const char *)&b)
160#define _mav_put_int16_t(buf, wire_offset, b) byte_copy_2(&buf[wire_offset], (const char *)&b)
161#define _mav_put_uint32_t(buf, wire_offset, b) byte_copy_4(&buf[wire_offset], (const char *)&b)
162#define _mav_put_int32_t(buf, wire_offset, b) byte_copy_4(&buf[wire_offset], (const char *)&b)
163#define _mav_put_uint64_t(buf, wire_offset, b) byte_copy_8(&buf[wire_offset], (const char *)&b)
164#define _mav_put_int64_t(buf, wire_offset, b) byte_copy_8(&buf[wire_offset], (const char *)&b)
165#define _mav_put_float(buf, wire_offset, b) byte_copy_4(&buf[wire_offset], (const char *)&b)
166#define _mav_put_double(buf, wire_offset, b) byte_copy_8(&buf[wire_offset], (const char *)&b)
167#else
168#define _mav_put_uint16_t(buf, wire_offset, b) *(uint16_t *)&buf[wire_offset] = b
169#define _mav_put_int16_t(buf, wire_offset, b) *(int16_t *)&buf[wire_offset] = b
170#define _mav_put_uint32_t(buf, wire_offset, b) *(uint32_t *)&buf[wire_offset] = b
171#define _mav_put_int32_t(buf, wire_offset, b) *(int32_t *)&buf[wire_offset] = b
172#define _mav_put_uint64_t(buf, wire_offset, b) *(uint64_t *)&buf[wire_offset] = b
173#define _mav_put_int64_t(buf, wire_offset, b) *(int64_t *)&buf[wire_offset] = b
174#define _mav_put_float(buf, wire_offset, b) *(float *)&buf[wire_offset] = b
175#define _mav_put_double(buf, wire_offset, b) *(double *)&buf[wire_offset] = b
176#endif
177
178/*
179 like memcpy(), but if src is NULL, do a memset to zero
180*/
181static inline void mav_array_memcpy(void *dest, const void *src, size_t n)
182{
183 // It would be tempting to do a strcpy/strncpy for the char[] type. Unfortunately, some
184 // existing MAVLink messages such as PARAM_EXT_VALUE.param_value use the char[] type for
185 // arbitrary data (including null), and would break.
186 //
187 // It would be nice to change such char[] types to uint8_t[] but that would change the
188 // CRC_EXTRA.
189
190 if (src == NULL) {
191 memset(dest, 0, n);
192 } else {
193 memcpy(dest, src, n);
194 }
195}
196
197/*
198 * Place a char array into a buffer
199 */
200static inline void _mav_put_char_array(char *buf, uint8_t wire_offset, const char *b, uint8_t array_length)
201{
202 mav_array_memcpy(&buf[wire_offset], b, array_length);
203
204}
205
206/*
207 * Place a uint8_t array into a buffer
208 */
209static inline void _mav_put_uint8_t_array(char *buf, uint8_t wire_offset, const uint8_t *b, uint8_t array_length)
210{
211 mav_array_memcpy(&buf[wire_offset], b, array_length);
212
213}
214
215/*
216 * Place a int8_t array into a buffer
217 */
218static inline void _mav_put_int8_t_array(char *buf, uint8_t wire_offset, const int8_t *b, uint8_t array_length)
219{
220 mav_array_memcpy(&buf[wire_offset], b, array_length);
221
222}
223
225#define _MAV_PUT_ARRAY(TYPE, V) static
226 inline void _mav_put_ ## TYPE ##_array(char *buf, uint8_t wire_offset, const TYPE *b, uint8_t array_length) \
227{
228 if (b == NULL) {
229 memset(&buf[wire_offset], 0, array_length*sizeof(TYPE));
230 } else {
231 uint16_t i;
232 for (i=0; i<array_length; i++) {
233 _mav_put_## TYPE (buf, wire_offset+(i*sizeof(TYPE)), b[i]);
234 }
235 } \
236}
237#else
238#define _MAV_PUT_ARRAY(TYPE, V) static
239 inline void _mav_put_ ## TYPE ##_array(char *buf, uint8_t wire_offset, const TYPE *b, uint8_t array_length) \
240{
241 mav_array_memcpy(&buf[wire_offset], b, array_length*sizeof(TYPE)); \
242}
243#endif
244
245_MAV_PUT_ARRAY(uint16_t, u16)
246_MAV_PUT_ARRAY(uint32_t, u32)
247_MAV_PUT_ARRAY(uint64_t, u64)
248_MAV_PUT_ARRAY(int16_t, i16)
249_MAV_PUT_ARRAY(int32_t, i32)
250_MAV_PUT_ARRAY(int64_t, i64)
251_MAV_PUT_ARRAY(float, f)
252_MAV_PUT_ARRAY(double, d)
253
254#define _MAV_RETURN_char(msg, wire_offset) (char)_MAV_PAYLOAD(msg)[wire_offset]
255#define _MAV_RETURN_int8_t(msg, wire_offset) (int8_t)_MAV_PAYLOAD(msg)[wire_offset]
256#define _MAV_RETURN_uint8_t(msg, wire_offset) (uint8_t)_MAV_PAYLOAD(msg)[wire_offset]
257
259#define _MAV_MSG_RETURN_TYPE(TYPE, SIZE) static
260 inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
261{TYPE r; byte_swap_## SIZE((char*)&r, &_MAV_PAYLOAD(msg)[ofs]); return r; }
262
263_MAV_MSG_RETURN_TYPE(uint16_t, 2)
264_MAV_MSG_RETURN_TYPE(int16_t, 2)
265_MAV_MSG_RETURN_TYPE(uint32_t, 4)
266_MAV_MSG_RETURN_TYPE(int32_t, 4)
267_MAV_MSG_RETURN_TYPE(uint64_t, 8)
268_MAV_MSG_RETURN_TYPE(int64_t, 8)
269_MAV_MSG_RETURN_TYPE(float, 4)
270_MAV_MSG_RETURN_TYPE(double, 8)
271
272#elif !MAVLINK_ALIGNED_FIELDS
273#define _MAV_MSG_RETURN_TYPE(TYPE, SIZE) static
274 inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
275{TYPE r; byte_copy_## SIZE((char*)&r, &_MAV_PAYLOAD(msg)[ofs]); return r; }
276
277_MAV_MSG_RETURN_TYPE(uint16_t, 2)
278_MAV_MSG_RETURN_TYPE(int16_t, 2)
279_MAV_MSG_RETURN_TYPE(uint32_t, 4)
280_MAV_MSG_RETURN_TYPE(int32_t, 4)
281_MAV_MSG_RETURN_TYPE(uint64_t, 8)
282_MAV_MSG_RETURN_TYPE(int64_t, 8)
283_MAV_MSG_RETURN_TYPE(float, 4)
284_MAV_MSG_RETURN_TYPE(double, 8)
285#else // nicely aligned, no swap
286#define _MAV_MSG_RETURN_TYPE(TYPE) static
287 inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
288{return *(const TYPE *)(&_MAV_PAYLOAD(msg)[ofs]);}
289
290_MAV_MSG_RETURN_TYPE(uint16_t)
291_MAV_MSG_RETURN_TYPE(int16_t)
292_MAV_MSG_RETURN_TYPE(uint32_t)
293_MAV_MSG_RETURN_TYPE(int32_t)
294_MAV_MSG_RETURN_TYPE(uint64_t)
295_MAV_MSG_RETURN_TYPE(int64_t)
296_MAV_MSG_RETURN_TYPE(float)
297_MAV_MSG_RETURN_TYPE(double)
298#endif // MAVLINK_NEED_BYTE_SWAP
299
300static inline uint16_t _MAV_RETURN_char_array(const mavlink_message_t *msg, char *value,
301 uint8_t array_length, uint8_t wire_offset)
302{
303 memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length);
304 return array_length;
305}
306
307static inline uint16_t _MAV_RETURN_uint8_t_array(const mavlink_message_t *msg, uint8_t *value,
308 uint8_t array_length, uint8_t wire_offset)
309{
310 memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length);
311 return array_length;
312}
313
314static inline uint16_t _MAV_RETURN_int8_t_array(const mavlink_message_t *msg, int8_t *value,
315 uint8_t array_length, uint8_t wire_offset)
316{
317 memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length);
318 return array_length;
319}
320
322#define _MAV_RETURN_ARRAY(TYPE, V) static
323 inline uint16_t _MAV_RETURN_## TYPE ##_array(const mavlink_message_t *msg, TYPE *value,
324 uint8_t array_length, uint8_t wire_offset) \
325{
326 uint16_t i;
327 for (i=0; i<array_length; i++) {
328 value[i] = _MAV_RETURN_## TYPE (msg, wire_offset+(i*sizeof(value[0])));
329 }
330 return array_length*sizeof(value[0]); \
331}
332#else
333#define _MAV_RETURN_ARRAY(TYPE, V) static
334 inline uint16_t _MAV_RETURN_## TYPE ##_array(const mavlink_message_t *msg, TYPE *value,
335 uint8_t array_length, uint8_t wire_offset) \
336{
337 memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length*sizeof(TYPE));
338 return array_length*sizeof(TYPE); \
339}
340#endif
341
342_MAV_RETURN_ARRAY(uint16_t, u16)
343_MAV_RETURN_ARRAY(uint32_t, u32)
344_MAV_RETURN_ARRAY(uint64_t, u64)
345_MAV_RETURN_ARRAY(int16_t, i16)
346_MAV_RETURN_ARRAY(int32_t, i32)
347_MAV_RETURN_ARRAY(int64_t, i64)
348_MAV_RETURN_ARRAY(float, f)
349_MAV_RETURN_ARRAY(double, d)
#define _MAV_RETURN_ARRAY(TYPE, V)
Definition: protocol.h:322
#define _MAV_MSG_RETURN_TYPE(TYPE, SIZE)
Definition: protocol.h:259
#define _MAV_PUT_ARRAY(TYPE, V)
Definition: protocol.h:225
#define MAVLINK_NEED_BYTE_SWAP
Definition: protocol.h:13