protocol.h 14 KB

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