mavlink_msg_system_time.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #pragma once
  2. // MESSAGE SYSTEM_TIME PACKING
  3. #define MAVLINK_MSG_ID_SYSTEM_TIME 2
  4. typedef struct __mavlink_system_time_t {
  5. uint64_t time_unix_usec; /*< [us] Timestamp (UNIX epoch time).*/
  6. uint32_t time_boot_ms; /*< [ms] Timestamp (time since system boot).*/
  7. } mavlink_system_time_t;
  8. #define MAVLINK_MSG_ID_SYSTEM_TIME_LEN 12
  9. #define MAVLINK_MSG_ID_SYSTEM_TIME_MIN_LEN 12
  10. #define MAVLINK_MSG_ID_2_LEN 12
  11. #define MAVLINK_MSG_ID_2_MIN_LEN 12
  12. #define MAVLINK_MSG_ID_SYSTEM_TIME_CRC 137
  13. #define MAVLINK_MSG_ID_2_CRC 137
  14. #if MAVLINK_COMMAND_24BIT
  15. #define MAVLINK_MESSAGE_INFO_SYSTEM_TIME { \
  16. 2, \
  17. "SYSTEM_TIME", \
  18. 2, \
  19. { { "time_unix_usec", NULL, MAVLINK_TYPE_UINT64_T, 0, 0, offsetof(mavlink_system_time_t, time_unix_usec) }, \
  20. { "time_boot_ms", NULL, MAVLINK_TYPE_UINT32_T, 0, 8, offsetof(mavlink_system_time_t, time_boot_ms) }, \
  21. } \
  22. }
  23. #else
  24. #define MAVLINK_MESSAGE_INFO_SYSTEM_TIME { \
  25. "SYSTEM_TIME", \
  26. 2, \
  27. { { "time_unix_usec", NULL, MAVLINK_TYPE_UINT64_T, 0, 0, offsetof(mavlink_system_time_t, time_unix_usec) }, \
  28. { "time_boot_ms", NULL, MAVLINK_TYPE_UINT32_T, 0, 8, offsetof(mavlink_system_time_t, time_boot_ms) }, \
  29. } \
  30. }
  31. #endif
  32. /**
  33. * @brief Pack a system_time message
  34. * @param system_id ID of this system
  35. * @param component_id ID of this component (e.g. 200 for IMU)
  36. * @param msg The MAVLink message to compress the data into
  37. *
  38. * @param time_unix_usec [us] Timestamp (UNIX epoch time).
  39. * @param time_boot_ms [ms] Timestamp (time since system boot).
  40. * @return length of the message in bytes (excluding serial stream start sign)
  41. */
  42. static inline uint16_t mavlink_msg_system_time_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg,
  43. uint64_t time_unix_usec, uint32_t time_boot_ms)
  44. {
  45. #if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
  46. char buf[MAVLINK_MSG_ID_SYSTEM_TIME_LEN];
  47. _mav_put_uint64_t(buf, 0, time_unix_usec);
  48. _mav_put_uint32_t(buf, 8, time_boot_ms);
  49. memcpy(_MAV_PAYLOAD_NON_CONST(msg), buf, MAVLINK_MSG_ID_SYSTEM_TIME_LEN);
  50. #else
  51. mavlink_system_time_t packet;
  52. packet.time_unix_usec = time_unix_usec;
  53. packet.time_boot_ms = time_boot_ms;
  54. memcpy(_MAV_PAYLOAD_NON_CONST(msg), &packet, MAVLINK_MSG_ID_SYSTEM_TIME_LEN);
  55. #endif
  56. msg->msgid = MAVLINK_MSG_ID_SYSTEM_TIME;
  57. return mavlink_finalize_message(msg, system_id, component_id, MAVLINK_MSG_ID_SYSTEM_TIME_MIN_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_CRC);
  58. }
  59. /**
  60. * @brief Pack a system_time message on a channel
  61. * @param system_id ID of this system
  62. * @param component_id ID of this component (e.g. 200 for IMU)
  63. * @param chan The MAVLink channel this message will be sent over
  64. * @param msg The MAVLink message to compress the data into
  65. * @param time_unix_usec [us] Timestamp (UNIX epoch time).
  66. * @param time_boot_ms [ms] Timestamp (time since system boot).
  67. * @return length of the message in bytes (excluding serial stream start sign)
  68. */
  69. static inline uint16_t mavlink_msg_system_time_pack_chan(uint8_t system_id, uint8_t component_id, uint8_t chan,
  70. mavlink_message_t* msg,
  71. uint64_t time_unix_usec,uint32_t time_boot_ms)
  72. {
  73. #if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
  74. char buf[MAVLINK_MSG_ID_SYSTEM_TIME_LEN];
  75. _mav_put_uint64_t(buf, 0, time_unix_usec);
  76. _mav_put_uint32_t(buf, 8, time_boot_ms);
  77. memcpy(_MAV_PAYLOAD_NON_CONST(msg), buf, MAVLINK_MSG_ID_SYSTEM_TIME_LEN);
  78. #else
  79. mavlink_system_time_t packet;
  80. packet.time_unix_usec = time_unix_usec;
  81. packet.time_boot_ms = time_boot_ms;
  82. memcpy(_MAV_PAYLOAD_NON_CONST(msg), &packet, MAVLINK_MSG_ID_SYSTEM_TIME_LEN);
  83. #endif
  84. msg->msgid = MAVLINK_MSG_ID_SYSTEM_TIME;
  85. return mavlink_finalize_message_chan(msg, system_id, component_id, chan, MAVLINK_MSG_ID_SYSTEM_TIME_MIN_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_CRC);
  86. }
  87. /**
  88. * @brief Encode a system_time struct
  89. *
  90. * @param system_id ID of this system
  91. * @param component_id ID of this component (e.g. 200 for IMU)
  92. * @param msg The MAVLink message to compress the data into
  93. * @param system_time C-struct to read the message contents from
  94. */
  95. static inline uint16_t mavlink_msg_system_time_encode(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, const mavlink_system_time_t* system_time)
  96. {
  97. return mavlink_msg_system_time_pack(system_id, component_id, msg, system_time->time_unix_usec, system_time->time_boot_ms);
  98. }
  99. /**
  100. * @brief Encode a system_time struct on a channel
  101. *
  102. * @param system_id ID of this system
  103. * @param component_id ID of this component (e.g. 200 for IMU)
  104. * @param chan The MAVLink channel this message will be sent over
  105. * @param msg The MAVLink message to compress the data into
  106. * @param system_time C-struct to read the message contents from
  107. */
  108. static inline uint16_t mavlink_msg_system_time_encode_chan(uint8_t system_id, uint8_t component_id, uint8_t chan, mavlink_message_t* msg, const mavlink_system_time_t* system_time)
  109. {
  110. return mavlink_msg_system_time_pack_chan(system_id, component_id, chan, msg, system_time->time_unix_usec, system_time->time_boot_ms);
  111. }
  112. /**
  113. * @brief Send a system_time message
  114. * @param chan MAVLink channel to send the message
  115. *
  116. * @param time_unix_usec [us] Timestamp (UNIX epoch time).
  117. * @param time_boot_ms [ms] Timestamp (time since system boot).
  118. */
  119. #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
  120. static inline void mavlink_msg_system_time_send(mavlink_channel_t chan, uint64_t time_unix_usec, uint32_t time_boot_ms)
  121. {
  122. #if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
  123. char buf[MAVLINK_MSG_ID_SYSTEM_TIME_LEN];
  124. _mav_put_uint64_t(buf, 0, time_unix_usec);
  125. _mav_put_uint32_t(buf, 8, time_boot_ms);
  126. _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_SYSTEM_TIME, buf, MAVLINK_MSG_ID_SYSTEM_TIME_MIN_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_CRC);
  127. #else
  128. mavlink_system_time_t packet;
  129. packet.time_unix_usec = time_unix_usec;
  130. packet.time_boot_ms = time_boot_ms;
  131. _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_SYSTEM_TIME, (const char *)&packet, MAVLINK_MSG_ID_SYSTEM_TIME_MIN_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_CRC);
  132. #endif
  133. }
  134. /**
  135. * @brief Send a system_time message
  136. * @param chan MAVLink channel to send the message
  137. * @param struct The MAVLink struct to serialize
  138. */
  139. static inline void mavlink_msg_system_time_send_struct(mavlink_channel_t chan, const mavlink_system_time_t* system_time)
  140. {
  141. #if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
  142. mavlink_msg_system_time_send(chan, system_time->time_unix_usec, system_time->time_boot_ms);
  143. #else
  144. _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_SYSTEM_TIME, (const char *)system_time, MAVLINK_MSG_ID_SYSTEM_TIME_MIN_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_CRC);
  145. #endif
  146. }
  147. #if MAVLINK_MSG_ID_SYSTEM_TIME_LEN <= MAVLINK_MAX_PAYLOAD_LEN
  148. /*
  149. This variant of _send() can be used to save stack space by re-using
  150. memory from the receive buffer. The caller provides a
  151. mavlink_message_t which is the size of a full mavlink message. This
  152. is usually the receive buffer for the channel, and allows a reply to an
  153. incoming message with minimum stack space usage.
  154. */
  155. static inline void mavlink_msg_system_time_send_buf(mavlink_message_t *msgbuf, mavlink_channel_t chan, uint64_t time_unix_usec, uint32_t time_boot_ms)
  156. {
  157. #if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
  158. char *buf = (char *)msgbuf;
  159. _mav_put_uint64_t(buf, 0, time_unix_usec);
  160. _mav_put_uint32_t(buf, 8, time_boot_ms);
  161. _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_SYSTEM_TIME, buf, MAVLINK_MSG_ID_SYSTEM_TIME_MIN_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_CRC);
  162. #else
  163. mavlink_system_time_t *packet = (mavlink_system_time_t *)msgbuf;
  164. packet->time_unix_usec = time_unix_usec;
  165. packet->time_boot_ms = time_boot_ms;
  166. _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_SYSTEM_TIME, (const char *)packet, MAVLINK_MSG_ID_SYSTEM_TIME_MIN_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_LEN, MAVLINK_MSG_ID_SYSTEM_TIME_CRC);
  167. #endif
  168. }
  169. #endif
  170. #endif
  171. // MESSAGE SYSTEM_TIME UNPACKING
  172. /**
  173. * @brief Get field time_unix_usec from system_time message
  174. *
  175. * @return [us] Timestamp (UNIX epoch time).
  176. */
  177. static inline uint64_t mavlink_msg_system_time_get_time_unix_usec(const mavlink_message_t* msg)
  178. {
  179. return _MAV_RETURN_uint64_t(msg, 0);
  180. }
  181. /**
  182. * @brief Get field time_boot_ms from system_time message
  183. *
  184. * @return [ms] Timestamp (time since system boot).
  185. */
  186. static inline uint32_t mavlink_msg_system_time_get_time_boot_ms(const mavlink_message_t* msg)
  187. {
  188. return _MAV_RETURN_uint32_t(msg, 8);
  189. }
  190. /**
  191. * @brief Decode a system_time message into a struct
  192. *
  193. * @param msg The message to decode
  194. * @param system_time C-struct to decode the message contents into
  195. */
  196. static inline void mavlink_msg_system_time_decode(const mavlink_message_t* msg, mavlink_system_time_t* system_time)
  197. {
  198. #if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
  199. system_time->time_unix_usec = mavlink_msg_system_time_get_time_unix_usec(msg);
  200. system_time->time_boot_ms = mavlink_msg_system_time_get_time_boot_ms(msg);
  201. #else
  202. uint8_t len = msg->len < MAVLINK_MSG_ID_SYSTEM_TIME_LEN? msg->len : MAVLINK_MSG_ID_SYSTEM_TIME_LEN;
  203. memset(system_time, 0, MAVLINK_MSG_ID_SYSTEM_TIME_LEN);
  204. memcpy(system_time, _MAV_PAYLOAD(msg), len);
  205. #endif
  206. }