protocol.h 11.6 KB
Newer Older
James Goppert's avatar
James Goppert committed
1 2 3 4 5 6
#ifndef  _MAVLINK_PROTOCOL_H_
#define  _MAVLINK_PROTOCOL_H_

#include "string.h"
#include "mavlink_types.h"

lm's avatar
lm committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 
   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

LM's avatar
LM committed
21 22 23 24
#ifndef MAVLINK_AVOID_GCC_STACK_BUG
# define MAVLINK_AVOID_GCC_STACK_BUG defined(__GNUC__)
#endif

lm's avatar
lm committed
25 26 27 28
#ifndef MAVLINK_ASSERT
#define MAVLINK_ASSERT(x)
#endif

LM's avatar
LM committed
29 30 31 32 33 34 35 36
#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

lm's avatar
lm committed
37 38 39 40 41 42 43 44
#ifdef MAVLINK_SEPARATE_HELPERS
#define MAVLINK_HELPER
#else
#define MAVLINK_HELPER static inline
#include "mavlink_helpers.h"
#endif // MAVLINK_SEPARATE_HELPERS

/* always include the prototypes to ensure we don't get out of sync */
45
#ifndef MAVLINK_GET_CHANNEL_STATUS
lm's avatar
lm committed
46
MAVLINK_HELPER mavlink_status_t* mavlink_get_channel_status(uint8_t chan);
47
#endif
Lorenz Meier's avatar
Lorenz Meier committed
48
MAVLINK_HELPER void mavlink_reset_channel_status(uint8_t chan);
LM's avatar
LM committed
49
#if MAVLINK_CRC_EXTRA
lm's avatar
lm committed
50
MAVLINK_HELPER uint16_t mavlink_finalize_message_chan(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id, 
lm's avatar
lm committed
51
						      uint8_t chan, uint8_t length, uint8_t crc_extra);
lm's avatar
lm committed
52
MAVLINK_HELPER uint16_t mavlink_finalize_message(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id, 
lm's avatar
lm committed
53
						 uint8_t length, uint8_t crc_extra);
54
#ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
LM's avatar
LM committed
55 56
MAVLINK_HELPER void _mav_finalize_message_chan_send(mavlink_channel_t chan, uint8_t msgid, const char *packet, 
						    uint8_t length, uint8_t crc_extra);
57
#endif
LM's avatar
LM committed
58 59
#else
MAVLINK_HELPER uint16_t mavlink_finalize_message_chan(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id, 
lm's avatar
lm committed
60
						      uint8_t chan, uint8_t length);
LM's avatar
LM committed
61
MAVLINK_HELPER uint16_t mavlink_finalize_message(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id, 
lm's avatar
lm committed
62
						 uint8_t length);
Lorenz Meier's avatar
Lorenz Meier committed
63
#ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
LM's avatar
LM committed
64
MAVLINK_HELPER void _mav_finalize_message_chan_send(mavlink_channel_t chan, uint8_t msgid, const char *packet, uint8_t length);
Lorenz Meier's avatar
Lorenz Meier committed
65
#endif
LM's avatar
LM committed
66
#endif // MAVLINK_CRC_EXTRA
lm's avatar
lm committed
67 68 69 70 71 72 73
MAVLINK_HELPER uint16_t mavlink_msg_to_send_buffer(uint8_t *buffer, const mavlink_message_t *msg);
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_parse_char(uint8_t chan, uint8_t c, mavlink_message_t* r_message, mavlink_status_t* r_mavlink_status);
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);
#ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
LM's avatar
LM committed
74
MAVLINK_HELPER void _mavlink_send_uart(mavlink_channel_t chan, const char *buf, uint16_t len);
Lorenz Meier's avatar
Lorenz Meier committed
75
MAVLINK_HELPER void _mavlink_resend_uart(mavlink_channel_t chan, const mavlink_message_t *msg);
lm's avatar
lm committed
76 77 78 79 80 81 82 83 84 85
#endif

/**
 * @brief Get the required buffer size for this message
 */
static inline uint16_t mavlink_msg_get_send_buffer_length(const mavlink_message_t* msg)
{
	return msg->len + MAVLINK_NUM_NON_PAYLOAD_BYTES;
}

LM's avatar
LM committed
86
#if MAVLINK_NEED_BYTE_SWAP
LM's avatar
LM committed
87
static inline void byte_swap_2(char *dst, const char *src)
lm's avatar
lm committed
88
{
LM's avatar
LM committed
89 90
	dst[0] = src[1];
	dst[1] = src[0];
lm's avatar
lm committed
91
}
LM's avatar
LM committed
92
static inline void byte_swap_4(char *dst, const char *src)
lm's avatar
lm committed
93
{
LM's avatar
LM committed
94 95 96 97
	dst[0] = src[3];
	dst[1] = src[2];
	dst[2] = src[1];
	dst[3] = src[0];
lm's avatar
lm committed
98
}
LM's avatar
LM committed
99
static inline void byte_swap_8(char *dst, const char *src)
James Goppert's avatar
James Goppert committed
100
{
LM's avatar
LM committed
101 102 103 104 105 106 107 108
	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];
James Goppert's avatar
James Goppert committed
109
}
LM's avatar
LM committed
110
#elif !MAVLINK_ALIGNED_FIELDS
LM's avatar
LM committed
111
static inline void byte_copy_2(char *dst, const char *src)
James Goppert's avatar
James Goppert committed
112
{
LM's avatar
LM committed
113 114
	dst[0] = src[0];
	dst[1] = src[1];
James Goppert's avatar
James Goppert committed
115
}
LM's avatar
LM committed
116
static inline void byte_copy_4(char *dst, const char *src)
lm's avatar
lm committed
117
{
LM's avatar
LM committed
118 119 120 121
	dst[0] = src[0];
	dst[1] = src[1];
	dst[2] = src[2];
	dst[3] = src[3];
lm's avatar
lm committed
122
}
LM's avatar
LM committed
123
static inline void byte_copy_8(char *dst, const char *src)
James Goppert's avatar
James Goppert committed
124
{
LM's avatar
LM committed
125
	memcpy(dst, src, 8);
James Goppert's avatar
James Goppert committed
126
}
lm's avatar
lm committed
127 128
#endif

LM's avatar
LM committed
129 130 131
#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
James Goppert's avatar
James Goppert committed
132

lm's avatar
lm committed
133
#if MAVLINK_NEED_BYTE_SWAP
LM's avatar
LM committed
134 135 136 137 138 139 140 141
#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)
LM's avatar
LM committed
142
#elif !MAVLINK_ALIGNED_FIELDS
LM's avatar
LM committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
#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
lm's avatar
lm committed
160
#endif
James Goppert's avatar
James Goppert committed
161

162 163
/*
  like memcpy(), but if src is NULL, do a memset to zero
164
*/
Lorenz Meier's avatar
Lorenz Meier committed
165
static inline void mav_array_memcpy(void *dest, const void *src, size_t n)
166 167 168 169 170 171 172
{
	if (src == NULL) {
		memset(dest, 0, n);
	} else {
		memcpy(dest, src, n);
	}
}
James Goppert's avatar
James Goppert committed
173

lm's avatar
lm committed
174 175 176
/*
 * Place a char array into a buffer
 */
LM's avatar
LM committed
177
static inline void _mav_put_char_array(char *buf, uint8_t wire_offset, const char *b, uint8_t array_length)
lm's avatar
lm committed
178
{
179
	mav_array_memcpy(&buf[wire_offset], b, array_length);
180

lm's avatar
lm committed
181
}
James Goppert's avatar
James Goppert committed
182

lm's avatar
lm committed
183 184 185
/*
 * Place a uint8_t array into a buffer
 */
LM's avatar
LM committed
186
static inline void _mav_put_uint8_t_array(char *buf, uint8_t wire_offset, const uint8_t *b, uint8_t array_length)
lm's avatar
lm committed
187
{
188
	mav_array_memcpy(&buf[wire_offset], b, array_length);
189

lm's avatar
lm committed
190
}
James Goppert's avatar
James Goppert committed
191

lm's avatar
lm committed
192 193 194
/*
 * Place a int8_t array into a buffer
 */
LM's avatar
LM committed
195
static inline void _mav_put_int8_t_array(char *buf, uint8_t wire_offset, const int8_t *b, uint8_t array_length)
James Goppert's avatar
James Goppert committed
196
{
197
	mav_array_memcpy(&buf[wire_offset], b, array_length);
198

lm's avatar
lm committed
199 200 201
}

#if MAVLINK_NEED_BYTE_SWAP
LM's avatar
LM committed
202 203
#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) \
lm's avatar
lm committed
204
{ \
LM's avatar
LM committed
205
	if (b == NULL) { \
LM's avatar
LM committed
206
		memset(&buf[wire_offset], 0, array_length*sizeof(TYPE)); \
LM's avatar
LM committed
207 208 209
	} else { \
		uint16_t i; \
		for (i=0; i<array_length; i++) { \
LM's avatar
LM committed
210
			_mav_put_## TYPE (buf, wire_offset+(i*sizeof(TYPE)), b[i]); \
LM's avatar
LM committed
211
		} \
lm's avatar
lm committed
212 213 214
	} \
}
#else
LM's avatar
LM committed
215 216
#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) \
lm's avatar
lm committed
217
{ \
218
	mav_array_memcpy(&buf[wire_offset], b, array_length*sizeof(TYPE)); \
James Goppert's avatar
James Goppert committed
219
}
lm's avatar
lm committed
220 221
#endif

LM's avatar
LM committed
222 223 224 225 226 227 228 229
_MAV_PUT_ARRAY(uint16_t, u16)
_MAV_PUT_ARRAY(uint32_t, u32)
_MAV_PUT_ARRAY(uint64_t, u64)
_MAV_PUT_ARRAY(int16_t,  i16)
_MAV_PUT_ARRAY(int32_t,  i32)
_MAV_PUT_ARRAY(int64_t,  i64)
_MAV_PUT_ARRAY(float,    f)
_MAV_PUT_ARRAY(double,   d)
lm's avatar
lm committed
230

231 232 233
#define _MAV_RETURN_char(msg, wire_offset)             (const char)_MAV_PAYLOAD(msg)[wire_offset]
#define _MAV_RETURN_int8_t(msg, wire_offset)   (const int8_t)_MAV_PAYLOAD(msg)[wire_offset]
#define _MAV_RETURN_uint8_t(msg, wire_offset) (const uint8_t)_MAV_PAYLOAD(msg)[wire_offset]
James Goppert's avatar
James Goppert committed
234

lm's avatar
lm committed
235
#if MAVLINK_NEED_BYTE_SWAP
LM's avatar
LM committed
236 237 238
#define _MAV_MSG_RETURN_TYPE(TYPE, SIZE) \
static inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
{ TYPE r; byte_swap_## SIZE((char*)&r, &_MAV_PAYLOAD(msg)[ofs]); return r; }
LM's avatar
LM committed
239

LM's avatar
LM committed
240 241 242 243 244 245 246 247
_MAV_MSG_RETURN_TYPE(uint16_t, 2)
_MAV_MSG_RETURN_TYPE(int16_t,  2)
_MAV_MSG_RETURN_TYPE(uint32_t, 4)
_MAV_MSG_RETURN_TYPE(int32_t,  4)
_MAV_MSG_RETURN_TYPE(uint64_t, 8)
_MAV_MSG_RETURN_TYPE(int64_t,  8)
_MAV_MSG_RETURN_TYPE(float,    4)
_MAV_MSG_RETURN_TYPE(double,   8)
LM's avatar
LM committed
248 249

#elif !MAVLINK_ALIGNED_FIELDS
LM's avatar
LM committed
250 251 252
#define _MAV_MSG_RETURN_TYPE(TYPE, SIZE) \
static inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
{ TYPE r; byte_copy_## SIZE((char*)&r, &_MAV_PAYLOAD(msg)[ofs]); return r; }
LM's avatar
LM committed
253

LM's avatar
LM committed
254 255 256 257 258 259 260 261 262 263 264 265
_MAV_MSG_RETURN_TYPE(uint16_t, 2)
_MAV_MSG_RETURN_TYPE(int16_t,  2)
_MAV_MSG_RETURN_TYPE(uint32_t, 4)
_MAV_MSG_RETURN_TYPE(int32_t,  4)
_MAV_MSG_RETURN_TYPE(uint64_t, 8)
_MAV_MSG_RETURN_TYPE(int64_t,  8)
_MAV_MSG_RETURN_TYPE(float,    4)
_MAV_MSG_RETURN_TYPE(double,   8)
#else // nicely aligned, no swap
#define _MAV_MSG_RETURN_TYPE(TYPE) \
static inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
{ return *(const TYPE *)(&_MAV_PAYLOAD(msg)[ofs]);}
LM's avatar
LM committed
266

LM's avatar
LM committed
267 268 269 270 271 272 273 274
_MAV_MSG_RETURN_TYPE(uint16_t)
_MAV_MSG_RETURN_TYPE(int16_t)
_MAV_MSG_RETURN_TYPE(uint32_t)
_MAV_MSG_RETURN_TYPE(int32_t)
_MAV_MSG_RETURN_TYPE(uint64_t)
_MAV_MSG_RETURN_TYPE(int64_t)
_MAV_MSG_RETURN_TYPE(float)
_MAV_MSG_RETURN_TYPE(double)
LM's avatar
LM committed
275
#endif // MAVLINK_NEED_BYTE_SWAP
lm's avatar
lm committed
276

LM's avatar
LM committed
277
static inline uint16_t _MAV_RETURN_char_array(const mavlink_message_t *msg, char *value, 
lm's avatar
lm committed
278
						     uint8_t array_length, uint8_t wire_offset)
lm's avatar
lm committed
279
{
LM's avatar
LM committed
280
	memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length);
lm's avatar
lm committed
281
	return array_length;
lm's avatar
lm committed
282 283
}

LM's avatar
LM committed
284
static inline uint16_t _MAV_RETURN_uint8_t_array(const mavlink_message_t *msg, uint8_t *value, 
lm's avatar
lm committed
285
							uint8_t array_length, uint8_t wire_offset)
lm's avatar
lm committed
286
{
LM's avatar
LM committed
287
	memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length);
lm's avatar
lm committed
288 289 290
	return array_length;
}

LM's avatar
LM committed
291
static inline uint16_t _MAV_RETURN_int8_t_array(const mavlink_message_t *msg, int8_t *value, 
lm's avatar
lm committed
292 293
						       uint8_t array_length, uint8_t wire_offset)
{
LM's avatar
LM committed
294
	memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length);
lm's avatar
lm committed
295 296 297 298
	return array_length;
}

#if MAVLINK_NEED_BYTE_SWAP
LM's avatar
LM committed
299 300
#define _MAV_RETURN_ARRAY(TYPE, V) \
static inline uint16_t _MAV_RETURN_## TYPE ##_array(const mavlink_message_t *msg, TYPE *value, \
lm's avatar
lm committed
301 302 303 304
							 uint8_t array_length, uint8_t wire_offset) \
{ \
	uint16_t i; \
	for (i=0; i<array_length; i++) { \
LM's avatar
LM committed
305
		value[i] = _MAV_RETURN_## TYPE (msg, wire_offset+(i*sizeof(value[0]))); \
lm's avatar
lm committed
306 307 308 309
	} \
	return array_length*sizeof(value[0]); \
}
#else
LM's avatar
LM committed
310 311
#define _MAV_RETURN_ARRAY(TYPE, V)					\
static inline uint16_t _MAV_RETURN_## TYPE ##_array(const mavlink_message_t *msg, TYPE *value, \
lm's avatar
lm committed
312 313
							 uint8_t array_length, uint8_t wire_offset) \
{ \
LM's avatar
LM committed
314
	memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length*sizeof(TYPE)); \
LM's avatar
LM committed
315
	return array_length*sizeof(TYPE); \
lm's avatar
lm committed
316 317 318
}
#endif

LM's avatar
LM committed
319 320 321 322 323 324 325 326
_MAV_RETURN_ARRAY(uint16_t, u16)
_MAV_RETURN_ARRAY(uint32_t, u32)
_MAV_RETURN_ARRAY(uint64_t, u64)
_MAV_RETURN_ARRAY(int16_t,  i16)
_MAV_RETURN_ARRAY(int32_t,  i32)
_MAV_RETURN_ARRAY(int64_t,  i64)
_MAV_RETURN_ARRAY(float,    f)
_MAV_RETURN_ARRAY(double,   d)
lm's avatar
lm committed
327 328

#endif // _MAVLINK_PROTOCOL_H_