protocol.h 14 KB

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