mavlink_types.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #pragma once
  2. // Visual Studio versions before 2010 don't have stdint.h, so we just error out.
  3. #if (defined _MSC_VER) && (_MSC_VER < 1600)
  4. #error "The C-MAVLink implementation requires Visual Studio 2010 or greater"
  5. #endif
  6. #include <stdbool.h>
  7. #include <stdint.h>
  8. #ifdef MAVLINK_USE_CXX_NAMESPACE
  9. namespace mavlink {
  10. #endif
  11. // Macro to define packed structures
  12. #ifdef __GNUC__
  13. #define MAVPACKED( __Declaration__ ) __Declaration__ __attribute__((packed))
  14. #else
  15. #define MAVPACKED( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
  16. #endif
  17. #ifndef MAVLINK_MAX_PAYLOAD_LEN
  18. // it is possible to override this, but be careful!
  19. #define MAVLINK_MAX_PAYLOAD_LEN 255 ///< Maximum payload length
  20. #endif
  21. #define MAVLINK_CORE_HEADER_LEN 9 ///< Length of core header (of the comm. layer)
  22. #define MAVLINK_CORE_HEADER_MAVLINK1_LEN 5 ///< Length of MAVLink1 core header (of the comm. layer)
  23. #define MAVLINK_NUM_HEADER_BYTES (MAVLINK_CORE_HEADER_LEN + 1) ///< Length of all header bytes, including core and stx
  24. #define MAVLINK_NUM_CHECKSUM_BYTES 2
  25. #define MAVLINK_NUM_NON_PAYLOAD_BYTES (MAVLINK_NUM_HEADER_BYTES + MAVLINK_NUM_CHECKSUM_BYTES)
  26. #define MAVLINK_SIGNATURE_BLOCK_LEN 13
  27. #define MAVLINK_MAX_PACKET_LEN (MAVLINK_MAX_PAYLOAD_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES + MAVLINK_SIGNATURE_BLOCK_LEN) ///< Maximum packet length
  28. /**
  29. * Old-style 4 byte param union
  30. *
  31. * This struct is the data format to be used when sending
  32. * parameters. The parameter should be copied to the native
  33. * type (without type conversion)
  34. * and re-instanted on the receiving side using the
  35. * native type as well.
  36. */
  37. MAVPACKED(
  38. typedef struct param_union {
  39. union {
  40. float param_float;
  41. int32_t param_int32;
  42. uint32_t param_uint32;
  43. int16_t param_int16;
  44. uint16_t param_uint16;
  45. int8_t param_int8;
  46. uint8_t param_uint8;
  47. uint8_t bytes[4];
  48. };
  49. uint8_t type;
  50. }) mavlink_param_union_t;
  51. /**
  52. * New-style 8 byte param union
  53. * mavlink_param_union_double_t will be 8 bytes long, and treated as needing 8 byte alignment for the purposes of MAVLink 1.0 field ordering.
  54. * The mavlink_param_union_double_t will be treated as a little-endian structure.
  55. *
  56. * If is_double is 1 then the type is a double, and the remaining 63 bits are the double, with the lowest bit of the mantissa zero.
  57. * The intention is that by replacing the is_double bit with 0 the type can be directly used as a double (as the is_double bit corresponds to the
  58. * lowest mantissa bit of a double). If is_double is 0 then mavlink_type gives the type in the union.
  59. * The mavlink_types.h header will also need to have shifts/masks to define the bit boundaries in the above,
  60. * as bitfield ordering isn't consistent between platforms. The above is intended to be for gcc on x86,
  61. * which should be the same as gcc on little-endian arm. When using shifts/masks the value will be treated as a 64 bit unsigned number,
  62. * and the bits pulled out using the shifts/masks.
  63. */
  64. MAVPACKED(
  65. typedef struct param_union_extended {
  66. union {
  67. struct {
  68. uint8_t is_double:1;
  69. uint8_t mavlink_type:7;
  70. union {
  71. char c;
  72. uint8_t uint8;
  73. int8_t int8;
  74. uint16_t uint16;
  75. int16_t int16;
  76. uint32_t uint32;
  77. int32_t int32;
  78. float f;
  79. uint8_t align[7];
  80. };
  81. };
  82. uint8_t data[8];
  83. };
  84. }) mavlink_param_union_double_t;
  85. /**
  86. * This structure is required to make the mavlink_send_xxx convenience functions
  87. * work, as it tells the library what the current system and component ID are.
  88. */
  89. MAVPACKED(
  90. typedef struct __mavlink_system {
  91. uint8_t sysid; ///< Used by the MAVLink message_xx_send() convenience function
  92. uint8_t compid; ///< Used by the MAVLink message_xx_send() convenience function
  93. }) mavlink_system_t;
  94. MAVPACKED(
  95. typedef struct __mavlink_message {
  96. uint16_t checksum; ///< sent at end of packet
  97. uint8_t magic; ///< protocol magic marker
  98. uint8_t len; ///< Length of payload
  99. uint8_t incompat_flags; ///< flags that must be understood
  100. uint8_t compat_flags; ///< flags that can be ignored if not understood
  101. uint8_t seq; ///< Sequence of packet
  102. uint8_t sysid; ///< ID of message sender system/aircraft
  103. uint8_t compid; ///< ID of the message sender component
  104. uint32_t msgid:24; ///< ID of message in payload
  105. uint64_t payload64[(MAVLINK_MAX_PAYLOAD_LEN+MAVLINK_NUM_CHECKSUM_BYTES+7)/8];
  106. uint8_t ck[2]; ///< incoming checksum bytes
  107. uint8_t signature[MAVLINK_SIGNATURE_BLOCK_LEN];
  108. }) mavlink_message_t;
  109. typedef enum {
  110. MAVLINK_TYPE_CHAR = 0,
  111. MAVLINK_TYPE_UINT8_T = 1,
  112. MAVLINK_TYPE_INT8_T = 2,
  113. MAVLINK_TYPE_UINT16_T = 3,
  114. MAVLINK_TYPE_INT16_T = 4,
  115. MAVLINK_TYPE_UINT32_T = 5,
  116. MAVLINK_TYPE_INT32_T = 6,
  117. MAVLINK_TYPE_UINT64_T = 7,
  118. MAVLINK_TYPE_INT64_T = 8,
  119. MAVLINK_TYPE_FLOAT = 9,
  120. MAVLINK_TYPE_DOUBLE = 10
  121. } mavlink_message_type_t;
  122. #define MAVLINK_MAX_FIELDS 64
  123. typedef struct __mavlink_field_info {
  124. const char *name; // name of this field
  125. const char *print_format; // printing format hint, or NULL
  126. mavlink_message_type_t type; // type of this field
  127. unsigned int array_length; // if non-zero, field is an array
  128. unsigned int wire_offset; // offset of each field in the payload
  129. unsigned int structure_offset; // offset in a C structure
  130. } mavlink_field_info_t;
  131. // note that in this structure the order of fields is the order
  132. // in the XML file, not necessary the wire order
  133. typedef struct __mavlink_message_info {
  134. uint32_t msgid; // message ID
  135. const char *name; // name of the message
  136. unsigned num_fields; // how many fields in this message
  137. mavlink_field_info_t fields[MAVLINK_MAX_FIELDS]; // field information
  138. } mavlink_message_info_t;
  139. #define _MAV_PAYLOAD(msg) ((const char *)(&((msg)->payload64[0])))
  140. #define _MAV_PAYLOAD_NON_CONST(msg) ((char *)(&((msg)->payload64[0])))
  141. // checksum is immediately after the payload bytes
  142. #define mavlink_ck_a(msg) *((msg)->len + (uint8_t *)_MAV_PAYLOAD_NON_CONST(msg))
  143. #define mavlink_ck_b(msg) *(((msg)->len+(uint16_t)1) + (uint8_t *)_MAV_PAYLOAD_NON_CONST(msg))
  144. #ifndef HAVE_MAVLINK_CHANNEL_T
  145. typedef enum {
  146. MAVLINK_COMM_0,
  147. MAVLINK_COMM_1,
  148. MAVLINK_COMM_2,
  149. MAVLINK_COMM_3
  150. } mavlink_channel_t;
  151. #endif
  152. /*
  153. * applications can set MAVLINK_COMM_NUM_BUFFERS to the maximum number
  154. * of buffers they will use. If more are used, then the result will be
  155. * a stack overrun
  156. */
  157. #ifndef MAVLINK_COMM_NUM_BUFFERS
  158. #if (defined linux) | (defined __linux) | (defined __MACH__) | (defined _WIN32)
  159. # define MAVLINK_COMM_NUM_BUFFERS 16
  160. #else
  161. # define MAVLINK_COMM_NUM_BUFFERS 4
  162. #endif
  163. #endif
  164. typedef enum {
  165. MAVLINK_PARSE_STATE_UNINIT=0,
  166. MAVLINK_PARSE_STATE_IDLE,
  167. MAVLINK_PARSE_STATE_GOT_STX,
  168. MAVLINK_PARSE_STATE_GOT_LENGTH,
  169. MAVLINK_PARSE_STATE_GOT_INCOMPAT_FLAGS,
  170. MAVLINK_PARSE_STATE_GOT_COMPAT_FLAGS,
  171. MAVLINK_PARSE_STATE_GOT_SEQ,
  172. MAVLINK_PARSE_STATE_GOT_SYSID,
  173. MAVLINK_PARSE_STATE_GOT_COMPID,
  174. MAVLINK_PARSE_STATE_GOT_MSGID1,
  175. MAVLINK_PARSE_STATE_GOT_MSGID2,
  176. MAVLINK_PARSE_STATE_GOT_MSGID3,
  177. MAVLINK_PARSE_STATE_GOT_PAYLOAD,
  178. MAVLINK_PARSE_STATE_GOT_CRC1,
  179. MAVLINK_PARSE_STATE_GOT_BAD_CRC1,
  180. MAVLINK_PARSE_STATE_SIGNATURE_WAIT
  181. } mavlink_parse_state_t; ///< The state machine for the comm parser
  182. typedef enum {
  183. MAVLINK_FRAMING_INCOMPLETE=0,
  184. MAVLINK_FRAMING_OK=1,
  185. MAVLINK_FRAMING_BAD_CRC=2,
  186. MAVLINK_FRAMING_BAD_SIGNATURE=3
  187. } mavlink_framing_t;
  188. #define MAVLINK_STATUS_FLAG_IN_MAVLINK1 1 // last incoming packet was MAVLink1
  189. #define MAVLINK_STATUS_FLAG_OUT_MAVLINK1 2 // generate MAVLink1 by default
  190. #define MAVLINK_STATUS_FLAG_IN_SIGNED 4 // last incoming packet was signed and validated
  191. #define MAVLINK_STATUS_FLAG_IN_BADSIG 8 // last incoming packet had a bad signature
  192. #define MAVLINK_STX_MAVLINK1 0xFE // marker for old protocol
  193. typedef struct __mavlink_status {
  194. uint8_t msg_received; ///< Number of received messages
  195. uint8_t buffer_overrun; ///< Number of buffer overruns
  196. uint8_t parse_error; ///< Number of parse errors
  197. mavlink_parse_state_t parse_state; ///< Parsing state machine
  198. uint8_t packet_idx; ///< Index in current packet
  199. uint8_t current_rx_seq; ///< Sequence number of last packet received
  200. uint8_t current_tx_seq; ///< Sequence number of last packet sent
  201. uint16_t packet_rx_success_count; ///< Received packets
  202. uint16_t packet_rx_drop_count; ///< Number of packet drops
  203. uint8_t flags; ///< MAVLINK_STATUS_FLAG_*
  204. uint8_t signature_wait; ///< number of signature bytes left to receive
  205. struct __mavlink_signing *signing; ///< optional signing state
  206. struct __mavlink_signing_streams *signing_streams; ///< global record of stream timestamps
  207. } mavlink_status_t;
  208. /*
  209. a callback function to allow for accepting unsigned packets
  210. */
  211. typedef bool (*mavlink_accept_unsigned_t)(const mavlink_status_t *status, uint32_t msgid);
  212. /*
  213. flags controlling signing
  214. */
  215. #define MAVLINK_SIGNING_FLAG_SIGN_OUTGOING 1 ///< Enable outgoing signing
  216. typedef enum {
  217. MAVLINK_SIGNING_STATUS_NONE=0,
  218. MAVLINK_SIGNING_STATUS_OK=1,
  219. MAVLINK_SIGNING_STATUS_BAD_SIGNATURE=2,
  220. MAVLINK_SIGNING_STATUS_NO_STREAMS=3,
  221. MAVLINK_SIGNING_STATUS_TOO_MANY_STREAMS=4,
  222. MAVLINK_SIGNING_STATUS_OLD_TIMESTAMP=5,
  223. MAVLINK_SIGNING_STATUS_REPLAY=6,
  224. } mavlink_signing_status_t;
  225. /*
  226. state of MAVLink signing for this channel
  227. */
  228. typedef struct __mavlink_signing {
  229. uint8_t flags; ///< MAVLINK_SIGNING_FLAG_*
  230. uint8_t link_id; ///< Same as MAVLINK_CHANNEL
  231. uint64_t timestamp; ///< Timestamp, in microseconds since UNIX epoch GMT
  232. uint8_t secret_key[32];
  233. mavlink_accept_unsigned_t accept_unsigned_callback;
  234. mavlink_signing_status_t last_status;
  235. } mavlink_signing_t;
  236. /*
  237. timestamp state of each logical signing stream. This needs to be the same structure for all
  238. connections in order to be secure
  239. */
  240. #ifndef MAVLINK_MAX_SIGNING_STREAMS
  241. #define MAVLINK_MAX_SIGNING_STREAMS 16
  242. #endif
  243. typedef struct __mavlink_signing_streams {
  244. uint16_t num_signing_streams;
  245. struct __mavlink_signing_stream {
  246. uint8_t link_id; ///< ID of the link (MAVLINK_CHANNEL)
  247. uint8_t sysid; ///< Remote system ID
  248. uint8_t compid; ///< Remote component ID
  249. uint8_t timestamp_bytes[6]; ///< Timestamp, in microseconds since UNIX epoch GMT
  250. } stream[MAVLINK_MAX_SIGNING_STREAMS];
  251. } mavlink_signing_streams_t;
  252. #define MAVLINK_BIG_ENDIAN 0
  253. #define MAVLINK_LITTLE_ENDIAN 1
  254. #define MAV_MSG_ENTRY_FLAG_HAVE_TARGET_SYSTEM 1
  255. #define MAV_MSG_ENTRY_FLAG_HAVE_TARGET_COMPONENT 2
  256. /*
  257. entry in table of information about each message type
  258. */
  259. typedef struct __mavlink_msg_entry {
  260. uint32_t msgid;
  261. uint8_t crc_extra;
  262. uint8_t min_msg_len; // minimum message length
  263. uint8_t max_msg_len; // maximum message length (e.g. including mavlink2 extensions)
  264. uint8_t flags; // MAV_MSG_ENTRY_FLAG_*
  265. uint8_t target_system_ofs; // payload offset to target_system, or 0
  266. uint8_t target_component_ofs; // payload offset to target_component, or 0
  267. } mavlink_msg_entry_t;
  268. /*
  269. incompat_flags bits
  270. */
  271. #define MAVLINK_IFLAG_SIGNED 0x01
  272. #define MAVLINK_IFLAG_MASK 0x01 // mask of all understood bits
  273. #ifdef MAVLINK_USE_CXX_NAMESPACE
  274. } // namespace mavlink
  275. #endif