protocol.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. * Place a char array into a buffer
  164. */
  165. static inline void _mav_put_char_array(char *buf, uint8_t wire_offset, const char *b, uint8_t array_length)
  166. {
  167. mav_array_memcpy(&buf[wire_offset], b, array_length);
  168. }
  169. /*
  170. * Place a uint8_t array into a buffer
  171. */
  172. static inline void _mav_put_uint8_t_array(char *buf, uint8_t wire_offset, const uint8_t *b, uint8_t array_length)
  173. {
  174. mav_array_memcpy(&buf[wire_offset], b, array_length);
  175. }
  176. /*
  177. * Place a int8_t array into a buffer
  178. */
  179. static inline void _mav_put_int8_t_array(char *buf, uint8_t wire_offset, const int8_t *b, uint8_t array_length)
  180. {
  181. mav_array_memcpy(&buf[wire_offset], b, array_length);
  182. }
  183. #if MAVLINK_NEED_BYTE_SWAP
  184. #define _MAV_PUT_ARRAY(TYPE, V) \
  185. static inline void _mav_put_ ## TYPE ##_array(char *buf, uint8_t wire_offset, const TYPE *b, uint8_t array_length) \
  186. { \
  187. if (b == NULL) { \
  188. memset(&buf[wire_offset], 0, array_length*sizeof(TYPE)); \
  189. } else { \
  190. uint16_t i; \
  191. for (i=0; i<array_length; i++) { \
  192. _mav_put_## TYPE (buf, wire_offset+(i*sizeof(TYPE)), b[i]); \
  193. } \
  194. } \
  195. }
  196. #else
  197. #define _MAV_PUT_ARRAY(TYPE, V) \
  198. static inline void _mav_put_ ## TYPE ##_array(char *buf, uint8_t wire_offset, const TYPE *b, uint8_t array_length) \
  199. { \
  200. mav_array_memcpy(&buf[wire_offset], b, array_length*sizeof(TYPE)); \
  201. }
  202. #endif
  203. _MAV_PUT_ARRAY(uint16_t, u16)
  204. _MAV_PUT_ARRAY(uint32_t, u32)
  205. _MAV_PUT_ARRAY(uint64_t, u64)
  206. _MAV_PUT_ARRAY(int16_t, i16)
  207. _MAV_PUT_ARRAY(int32_t, i32)
  208. _MAV_PUT_ARRAY(int64_t, i64)
  209. _MAV_PUT_ARRAY(float, f)
  210. _MAV_PUT_ARRAY(double, d)
  211. #define _MAV_RETURN_char(msg, wire_offset) (char)_MAV_PAYLOAD(msg)[wire_offset]
  212. #define _MAV_RETURN_int8_t(msg, wire_offset) (int8_t)_MAV_PAYLOAD(msg)[wire_offset]
  213. #define _MAV_RETURN_uint8_t(msg, wire_offset) (uint8_t)_MAV_PAYLOAD(msg)[wire_offset]
  214. #if MAVLINK_NEED_BYTE_SWAP
  215. #define _MAV_MSG_RETURN_TYPE(TYPE, SIZE) \
  216. static inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
  217. { TYPE r; byte_swap_## SIZE((char*)&r, &_MAV_PAYLOAD(msg)[ofs]); return r; }
  218. _MAV_MSG_RETURN_TYPE(uint16_t, 2)
  219. _MAV_MSG_RETURN_TYPE(int16_t, 2)
  220. _MAV_MSG_RETURN_TYPE(uint32_t, 4)
  221. _MAV_MSG_RETURN_TYPE(int32_t, 4)
  222. _MAV_MSG_RETURN_TYPE(uint64_t, 8)
  223. _MAV_MSG_RETURN_TYPE(int64_t, 8)
  224. _MAV_MSG_RETURN_TYPE(float, 4)
  225. _MAV_MSG_RETURN_TYPE(double, 8)
  226. #elif !MAVLINK_ALIGNED_FIELDS
  227. #define _MAV_MSG_RETURN_TYPE(TYPE, SIZE) \
  228. static inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
  229. { TYPE r; byte_copy_## SIZE((char*)&r, &_MAV_PAYLOAD(msg)[ofs]); return r; }
  230. _MAV_MSG_RETURN_TYPE(uint16_t, 2)
  231. _MAV_MSG_RETURN_TYPE(int16_t, 2)
  232. _MAV_MSG_RETURN_TYPE(uint32_t, 4)
  233. _MAV_MSG_RETURN_TYPE(int32_t, 4)
  234. _MAV_MSG_RETURN_TYPE(uint64_t, 8)
  235. _MAV_MSG_RETURN_TYPE(int64_t, 8)
  236. _MAV_MSG_RETURN_TYPE(float, 4)
  237. _MAV_MSG_RETURN_TYPE(double, 8)
  238. #else // nicely aligned, no swap
  239. #define _MAV_MSG_RETURN_TYPE(TYPE) \
  240. static inline TYPE _MAV_RETURN_## TYPE(const mavlink_message_t *msg, uint8_t ofs) \
  241. { return *(const TYPE *)(&_MAV_PAYLOAD(msg)[ofs]);}
  242. _MAV_MSG_RETURN_TYPE(uint16_t)
  243. _MAV_MSG_RETURN_TYPE(int16_t)
  244. _MAV_MSG_RETURN_TYPE(uint32_t)
  245. _MAV_MSG_RETURN_TYPE(int32_t)
  246. _MAV_MSG_RETURN_TYPE(uint64_t)
  247. _MAV_MSG_RETURN_TYPE(int64_t)
  248. _MAV_MSG_RETURN_TYPE(float)
  249. _MAV_MSG_RETURN_TYPE(double)
  250. #endif // MAVLINK_NEED_BYTE_SWAP
  251. static inline uint16_t _MAV_RETURN_char_array(const mavlink_message_t *msg, char *value,
  252. uint8_t array_length, uint8_t wire_offset)
  253. {
  254. memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length);
  255. return array_length;
  256. }
  257. static inline uint16_t _MAV_RETURN_uint8_t_array(const mavlink_message_t *msg, uint8_t *value,
  258. uint8_t array_length, uint8_t wire_offset)
  259. {
  260. memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length);
  261. return array_length;
  262. }
  263. static inline uint16_t _MAV_RETURN_int8_t_array(const mavlink_message_t *msg, int8_t *value,
  264. uint8_t array_length, uint8_t wire_offset)
  265. {
  266. memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length);
  267. return array_length;
  268. }
  269. #if MAVLINK_NEED_BYTE_SWAP
  270. #define _MAV_RETURN_ARRAY(TYPE, V) \
  271. static inline uint16_t _MAV_RETURN_## TYPE ##_array(const mavlink_message_t *msg, TYPE *value, \
  272. uint8_t array_length, uint8_t wire_offset) \
  273. { \
  274. uint16_t i; \
  275. for (i=0; i<array_length; i++) { \
  276. value[i] = _MAV_RETURN_## TYPE (msg, wire_offset+(i*sizeof(value[0]))); \
  277. } \
  278. return array_length*sizeof(value[0]); \
  279. }
  280. #else
  281. #define _MAV_RETURN_ARRAY(TYPE, V) \
  282. static inline uint16_t _MAV_RETURN_## TYPE ##_array(const mavlink_message_t *msg, TYPE *value, \
  283. uint8_t array_length, uint8_t wire_offset) \
  284. { \
  285. memcpy(value, &_MAV_PAYLOAD(msg)[wire_offset], array_length*sizeof(TYPE)); \
  286. return array_length*sizeof(TYPE); \
  287. }
  288. #endif
  289. _MAV_RETURN_ARRAY(uint16_t, u16)
  290. _MAV_RETURN_ARRAY(uint32_t, u32)
  291. _MAV_RETURN_ARRAY(uint64_t, u64)
  292. _MAV_RETURN_ARRAY(int16_t, i16)
  293. _MAV_RETURN_ARRAY(int32_t, i32)
  294. _MAV_RETURN_ARRAY(int64_t, i64)
  295. _MAV_RETURN_ARRAY(float, f)
  296. _MAV_RETURN_ARRAY(double, d)