protocol.h 12 KB

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