You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1133 lines
36 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #pragma once
  2. #include "string.h"
  3. #include "checksum.h"
  4. #include "mavlink_types.h"
  5. #include "mavlink_conversions.h"
  6. #include <stdio.h>
  7. #ifndef MAVLINK_HELPER
  8. #define MAVLINK_HELPER
  9. #endif
  10. #include "mavlink_sha256.h"
  11. #ifdef MAVLINK_USE_CXX_NAMESPACE
  12. namespace mavlink {
  13. #endif
  14. /*
  15. * Internal function to give access to the channel status for each channel
  16. */
  17. #ifndef MAVLINK_GET_CHANNEL_STATUS
  18. MAVLINK_HELPER mavlink_status_t* mavlink_get_channel_status(uint8_t chan)
  19. {
  20. #ifdef MAVLINK_EXTERNAL_RX_STATUS
  21. // No m_mavlink_status array defined in function,
  22. // has to be defined externally
  23. #else
  24. static mavlink_status_t m_mavlink_status[MAVLINK_COMM_NUM_BUFFERS];
  25. #endif
  26. return &m_mavlink_status[chan];
  27. }
  28. #endif
  29. /*
  30. * Internal function to give access to the channel buffer for each channel
  31. */
  32. #ifndef MAVLINK_GET_CHANNEL_BUFFER
  33. MAVLINK_HELPER mavlink_message_t* mavlink_get_channel_buffer(uint8_t chan)
  34. {
  35. #ifdef MAVLINK_EXTERNAL_RX_BUFFER
  36. // No m_mavlink_buffer array defined in function,
  37. // has to be defined externally
  38. #else
  39. static mavlink_message_t m_mavlink_buffer[MAVLINK_COMM_NUM_BUFFERS];
  40. #endif
  41. return &m_mavlink_buffer[chan];
  42. }
  43. #endif // MAVLINK_GET_CHANNEL_BUFFER
  44. /* Enable this option to check the length of each message.
  45. This allows invalid messages to be caught much sooner. Use if the transmission
  46. medium is prone to missing (or extra) characters (e.g. a radio that fades in
  47. and out). Only use if the channel will only contain messages types listed in
  48. the headers.
  49. */
  50. //#define MAVLINK_CHECK_MESSAGE_LENGTH
  51. /**
  52. * @brief Reset the status of a channel.
  53. */
  54. MAVLINK_HELPER void mavlink_reset_channel_status(uint8_t chan)
  55. {
  56. mavlink_status_t *status = mavlink_get_channel_status(chan);
  57. status->parse_state = MAVLINK_PARSE_STATE_IDLE;
  58. }
  59. /**
  60. * @brief create a signature block for a packet
  61. */
  62. MAVLINK_HELPER uint8_t mavlink_sign_packet(mavlink_signing_t *signing,
  63. uint8_t signature[MAVLINK_SIGNATURE_BLOCK_LEN],
  64. const uint8_t *header, uint8_t header_len,
  65. const uint8_t *packet, uint8_t packet_len,
  66. const uint8_t crc[2])
  67. {
  68. mavlink_sha256_ctx ctx;
  69. union {
  70. uint64_t t64;
  71. uint8_t t8[8];
  72. } tstamp;
  73. if (signing == NULL || !(signing->flags & MAVLINK_SIGNING_FLAG_SIGN_OUTGOING)) {
  74. return 0;
  75. }
  76. signature[0] = signing->link_id;
  77. tstamp.t64 = signing->timestamp;
  78. memcpy(&signature[1], tstamp.t8, 6);
  79. signing->timestamp++;
  80. mavlink_sha256_init(&ctx);
  81. mavlink_sha256_update(&ctx, signing->secret_key, sizeof(signing->secret_key));
  82. mavlink_sha256_update(&ctx, header, header_len);
  83. mavlink_sha256_update(&ctx, packet, packet_len);
  84. mavlink_sha256_update(&ctx, crc, 2);
  85. mavlink_sha256_update(&ctx, signature, 7);
  86. mavlink_sha256_final_48(&ctx, &signature[7]);
  87. return MAVLINK_SIGNATURE_BLOCK_LEN;
  88. }
  89. /**
  90. * @brief Trim payload of any trailing zero-populated bytes (MAVLink 2 only).
  91. *
  92. * @param payload Serialised payload buffer.
  93. * @param length Length of full-width payload buffer.
  94. * @return Length of payload after zero-filled bytes are trimmed.
  95. */
  96. MAVLINK_HELPER uint8_t _mav_trim_payload(const char *payload, uint8_t length)
  97. {
  98. while (length > 1 && payload[length-1] == 0) {
  99. length--;
  100. }
  101. return length;
  102. }
  103. /**
  104. * @brief check a signature block for a packet
  105. */
  106. MAVLINK_HELPER bool mavlink_signature_check(mavlink_signing_t *signing,
  107. mavlink_signing_streams_t *signing_streams,
  108. const mavlink_message_t *msg)
  109. {
  110. if (signing == NULL) {
  111. return true;
  112. }
  113. const uint8_t *p = (const uint8_t *)&msg->magic;
  114. const uint8_t *psig = msg->signature;
  115. const uint8_t *incoming_signature = psig+7;
  116. mavlink_sha256_ctx ctx;
  117. uint8_t signature[6];
  118. uint16_t i;
  119. mavlink_sha256_init(&ctx);
  120. mavlink_sha256_update(&ctx, signing->secret_key, sizeof(signing->secret_key));
  121. mavlink_sha256_update(&ctx, p, MAVLINK_CORE_HEADER_LEN+1+msg->len);
  122. mavlink_sha256_update(&ctx, msg->ck, 2);
  123. mavlink_sha256_update(&ctx, psig, 1+6);
  124. mavlink_sha256_final_48(&ctx, signature);
  125. if (memcmp(signature, incoming_signature, 6) != 0) {
  126. return false;
  127. }
  128. // now check timestamp
  129. union tstamp {
  130. uint64_t t64;
  131. uint8_t t8[8];
  132. } tstamp;
  133. uint8_t link_id = psig[0];
  134. tstamp.t64 = 0;
  135. memcpy(tstamp.t8, psig+1, 6);
  136. if (signing_streams == NULL) {
  137. return false;
  138. }
  139. // find stream
  140. for (i=0; i<signing_streams->num_signing_streams; i++) {
  141. if (msg->sysid == signing_streams->stream[i].sysid &&
  142. msg->compid == signing_streams->stream[i].compid &&
  143. link_id == signing_streams->stream[i].link_id) {
  144. break;
  145. }
  146. }
  147. if (i == signing_streams->num_signing_streams) {
  148. if (signing_streams->num_signing_streams >= MAVLINK_MAX_SIGNING_STREAMS) {
  149. // over max number of streams
  150. return false;
  151. }
  152. // new stream. Only accept if timestamp is not more than 1 minute old
  153. if (tstamp.t64 + 6000*1000UL < signing->timestamp) {
  154. return false;
  155. }
  156. // add new stream
  157. signing_streams->stream[i].sysid = msg->sysid;
  158. signing_streams->stream[i].compid = msg->compid;
  159. signing_streams->stream[i].link_id = link_id;
  160. signing_streams->num_signing_streams++;
  161. } else {
  162. union tstamp last_tstamp;
  163. last_tstamp.t64 = 0;
  164. memcpy(last_tstamp.t8, signing_streams->stream[i].timestamp_bytes, 6);
  165. if (tstamp.t64 <= last_tstamp.t64) {
  166. // repeating old timestamp
  167. return false;
  168. }
  169. }
  170. // remember last timestamp
  171. memcpy(signing_streams->stream[i].timestamp_bytes, psig+1, 6);
  172. // our next timestamp must be at least this timestamp
  173. if (tstamp.t64 > signing->timestamp) {
  174. signing->timestamp = tstamp.t64;
  175. }
  176. return true;
  177. }
  178. /**
  179. * @brief Finalize a MAVLink message with channel assignment
  180. *
  181. * This function calculates the checksum and sets length and aircraft id correctly.
  182. * It assumes that the message id and the payload are already correctly set. This function
  183. * can also be used if the message header has already been written before (as in mavlink_msg_xxx_pack
  184. * instead of mavlink_msg_xxx_pack_headerless), it just introduces little extra overhead.
  185. *
  186. * @param msg Message to finalize
  187. * @param system_id Id of the sending (this) system, 1-127
  188. * @param length Message length
  189. */
  190. MAVLINK_HELPER uint16_t mavlink_finalize_message_buffer(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id,
  191. mavlink_status_t* status, uint8_t min_length, uint8_t length, uint8_t crc_extra)
  192. {
  193. bool mavlink1 = (status->flags & MAVLINK_STATUS_FLAG_OUT_MAVLINK1) != 0;
  194. bool signing = (!mavlink1) && status->signing && (status->signing->flags & MAVLINK_SIGNING_FLAG_SIGN_OUTGOING);
  195. uint8_t signature_len = signing? MAVLINK_SIGNATURE_BLOCK_LEN : 0;
  196. uint8_t header_len = MAVLINK_CORE_HEADER_LEN+1;
  197. uint8_t buf[MAVLINK_CORE_HEADER_LEN+1];
  198. if (mavlink1) {
  199. msg->magic = MAVLINK_STX_MAVLINK1;
  200. header_len = MAVLINK_CORE_HEADER_MAVLINK1_LEN+1;
  201. } else {
  202. msg->magic = MAVLINK_STX;
  203. }
  204. msg->len = mavlink1?min_length:_mav_trim_payload(_MAV_PAYLOAD(msg), length);
  205. msg->sysid = system_id;
  206. msg->compid = component_id;
  207. msg->incompat_flags = 0;
  208. if (signing) {
  209. msg->incompat_flags |= MAVLINK_IFLAG_SIGNED;
  210. }
  211. msg->compat_flags = 0;
  212. msg->seq = status->current_tx_seq;
  213. status->current_tx_seq = status->current_tx_seq + 1;
  214. // form the header as a byte array for the crc
  215. buf[0] = msg->magic;
  216. buf[1] = msg->len;
  217. if (mavlink1) {
  218. buf[2] = msg->seq;
  219. buf[3] = msg->sysid;
  220. buf[4] = msg->compid;
  221. buf[5] = msg->msgid & 0xFF;
  222. } else {
  223. buf[2] = msg->incompat_flags;
  224. buf[3] = msg->compat_flags;
  225. buf[4] = msg->seq;
  226. buf[5] = msg->sysid;
  227. buf[6] = msg->compid;
  228. buf[7] = msg->msgid & 0xFF;
  229. buf[8] = (msg->msgid >> 8) & 0xFF;
  230. buf[9] = (msg->msgid >> 16) & 0xFF;
  231. }
  232. uint16_t checksum = crc_calculate(&buf[1], header_len-1);
  233. crc_accumulate_buffer(&checksum, _MAV_PAYLOAD(msg), msg->len);
  234. crc_accumulate(crc_extra, &checksum);
  235. mavlink_ck_a(msg) = (uint8_t)(checksum & 0xFF);
  236. mavlink_ck_b(msg) = (uint8_t)(checksum >> 8);
  237. msg->checksum = checksum;
  238. if (signing) {
  239. mavlink_sign_packet(status->signing,
  240. msg->signature,
  241. (const uint8_t *)buf, header_len,
  242. (const uint8_t *)_MAV_PAYLOAD(msg), msg->len,
  243. (const uint8_t *)_MAV_PAYLOAD(msg)+(uint16_t)msg->len);
  244. }
  245. return msg->len + header_len + 2 + signature_len;
  246. }
  247. MAVLINK_HELPER uint16_t mavlink_finalize_message_chan(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id,
  248. uint8_t chan, uint8_t min_length, uint8_t length, uint8_t crc_extra)
  249. {
  250. mavlink_status_t *status = mavlink_get_channel_status(chan);
  251. return mavlink_finalize_message_buffer(msg, system_id, component_id, status, min_length, length, crc_extra);
  252. }
  253. /**
  254. * @brief Finalize a MAVLink message with MAVLINK_COMM_0 as default channel
  255. */
  256. MAVLINK_HELPER uint16_t mavlink_finalize_message(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id,
  257. uint8_t min_length, uint8_t length, uint8_t crc_extra)
  258. {
  259. return mavlink_finalize_message_chan(msg, system_id, component_id, MAVLINK_COMM_0, min_length, length, crc_extra);
  260. }
  261. static inline void _mav_parse_error(mavlink_status_t *status)
  262. {
  263. status->parse_error++;
  264. }
  265. #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
  266. MAVLINK_HELPER void _mavlink_send_uart(mavlink_channel_t chan, const char *buf, uint16_t len);
  267. /**
  268. * @brief Finalize a MAVLink message with channel assignment and send
  269. */
  270. MAVLINK_HELPER void _mav_finalize_message_chan_send(mavlink_channel_t chan, uint32_t msgid,
  271. const char *packet,
  272. uint8_t min_length, uint8_t length, uint8_t crc_extra)
  273. {
  274. uint16_t checksum;
  275. uint8_t buf[MAVLINK_NUM_HEADER_BYTES];
  276. uint8_t ck[2];
  277. mavlink_status_t *status = mavlink_get_channel_status(chan);
  278. uint8_t header_len = MAVLINK_CORE_HEADER_LEN;
  279. uint8_t signature_len = 0;
  280. uint8_t signature[MAVLINK_SIGNATURE_BLOCK_LEN];
  281. bool mavlink1 = (status->flags & MAVLINK_STATUS_FLAG_OUT_MAVLINK1) != 0;
  282. bool signing = (!mavlink1) && status->signing && (status->signing->flags & MAVLINK_SIGNING_FLAG_SIGN_OUTGOING);
  283. if (mavlink1) {
  284. length = min_length;
  285. if (msgid > 255) {
  286. // can't send 16 bit messages
  287. _mav_parse_error(status);
  288. return;
  289. }
  290. header_len = MAVLINK_CORE_HEADER_MAVLINK1_LEN;
  291. buf[0] = MAVLINK_STX_MAVLINK1;
  292. buf[1] = length;
  293. buf[2] = status->current_tx_seq;
  294. buf[3] = mavlink_system.sysid;
  295. buf[4] = mavlink_system.compid;
  296. buf[5] = msgid & 0xFF;
  297. } else {
  298. uint8_t incompat_flags = 0;
  299. if (signing) {
  300. incompat_flags |= MAVLINK_IFLAG_SIGNED;
  301. }
  302. length = _mav_trim_payload(packet, length);
  303. buf[0] = MAVLINK_STX;
  304. buf[1] = length;
  305. buf[2] = incompat_flags;
  306. buf[3] = 0; // compat_flags
  307. buf[4] = status->current_tx_seq;
  308. buf[5] = mavlink_system.sysid;
  309. buf[6] = mavlink_system.compid;
  310. buf[7] = msgid & 0xFF;
  311. buf[8] = (msgid >> 8) & 0xFF;
  312. buf[9] = (msgid >> 16) & 0xFF;
  313. }
  314. status->current_tx_seq++;
  315. checksum = crc_calculate((const uint8_t*)&buf[1], header_len);
  316. crc_accumulate_buffer(&checksum, packet, length);
  317. crc_accumulate(crc_extra, &checksum);
  318. ck[0] = (uint8_t)(checksum & 0xFF);
  319. ck[1] = (uint8_t)(checksum >> 8);
  320. if (signing) {
  321. // possibly add a signature
  322. signature_len = mavlink_sign_packet(status->signing, signature, buf, header_len+1,
  323. (const uint8_t *)packet, length, ck);
  324. }
  325. MAVLINK_START_UART_SEND(chan, header_len + 3 + (uint16_t)length + (uint16_t)signature_len);
  326. _mavlink_send_uart(chan, (const char *)buf, header_len+1);
  327. _mavlink_send_uart(chan, packet, length);
  328. _mavlink_send_uart(chan, (const char *)ck, 2);
  329. if (signature_len != 0) {
  330. _mavlink_send_uart(chan, (const char *)signature, signature_len);
  331. }
  332. MAVLINK_END_UART_SEND(chan, header_len + 3 + (uint16_t)length + (uint16_t)signature_len);
  333. }
  334. /**
  335. * @brief re-send a message over a uart channel
  336. * this is more stack efficient than re-marshalling the message
  337. * If the message is signed then the original signature is also sent
  338. */
  339. MAVLINK_HELPER void _mavlink_resend_uart(mavlink_channel_t chan, const mavlink_message_t *msg)
  340. {
  341. uint8_t ck[2];
  342. ck[0] = (uint8_t)(msg->checksum & 0xFF);
  343. ck[1] = (uint8_t)(msg->checksum >> 8);
  344. // XXX use the right sequence here
  345. uint8_t header_len;
  346. uint8_t signature_len;
  347. if (msg->magic == MAVLINK_STX_MAVLINK1) {
  348. header_len = MAVLINK_CORE_HEADER_MAVLINK1_LEN + 1;
  349. signature_len = 0;
  350. MAVLINK_START_UART_SEND(chan, header_len + msg->len + 2 + signature_len);
  351. // we can't send the structure directly as it has extra mavlink2 elements in it
  352. uint8_t buf[MAVLINK_CORE_HEADER_MAVLINK1_LEN + 1];
  353. buf[0] = msg->magic;
  354. buf[1] = msg->len;
  355. buf[2] = msg->seq;
  356. buf[3] = msg->sysid;
  357. buf[4] = msg->compid;
  358. buf[5] = msg->msgid & 0xFF;
  359. _mavlink_send_uart(chan, (const char*)buf, header_len);
  360. } else {
  361. header_len = MAVLINK_CORE_HEADER_LEN + 1;
  362. signature_len = (msg->incompat_flags & MAVLINK_IFLAG_SIGNED)?MAVLINK_SIGNATURE_BLOCK_LEN:0;
  363. MAVLINK_START_UART_SEND(chan, header_len + msg->len + 2 + signature_len);
  364. uint8_t buf[MAVLINK_CORE_HEADER_LEN + 1];
  365. buf[0] = msg->magic;
  366. buf[1] = msg->len;
  367. buf[2] = msg->incompat_flags;
  368. buf[3] = msg->compat_flags;
  369. buf[4] = msg->seq;
  370. buf[5] = msg->sysid;
  371. buf[6] = msg->compid;
  372. buf[7] = msg->msgid & 0xFF;
  373. buf[8] = (msg->msgid >> 8) & 0xFF;
  374. buf[9] = (msg->msgid >> 16) & 0xFF;
  375. _mavlink_send_uart(chan, (const char *)buf, header_len);
  376. }
  377. _mavlink_send_uart(chan, _MAV_PAYLOAD(msg), msg->len);
  378. _mavlink_send_uart(chan, (const char *)ck, 2);
  379. if (signature_len != 0) {
  380. _mavlink_send_uart(chan, (const char *)msg->signature, MAVLINK_SIGNATURE_BLOCK_LEN);
  381. }
  382. MAVLINK_END_UART_SEND(chan, header_len + msg->len + 2 + signature_len);
  383. }
  384. #endif // MAVLINK_USE_CONVENIENCE_FUNCTIONS
  385. /**
  386. * @brief Pack a message to send it over a serial byte stream
  387. */
  388. MAVLINK_HELPER uint16_t mavlink_msg_to_send_buffer(uint8_t *buf, const mavlink_message_t *msg)
  389. {
  390. uint8_t signature_len, header_len;
  391. uint8_t *ck;
  392. uint8_t length = msg->len;
  393. if (msg->magic == MAVLINK_STX_MAVLINK1) {
  394. signature_len = 0;
  395. header_len = MAVLINK_CORE_HEADER_MAVLINK1_LEN;
  396. buf[0] = msg->magic;
  397. buf[1] = length;
  398. buf[2] = msg->seq;
  399. buf[3] = msg->sysid;
  400. buf[4] = msg->compid;
  401. buf[5] = msg->msgid & 0xFF;
  402. memcpy(&buf[6], _MAV_PAYLOAD(msg), msg->len);
  403. ck = buf + header_len + 1 + (uint16_t)msg->len;
  404. } else {
  405. length = _mav_trim_payload(_MAV_PAYLOAD(msg), length);
  406. header_len = MAVLINK_CORE_HEADER_LEN;
  407. buf[0] = msg->magic;
  408. buf[1] = length;
  409. buf[2] = msg->incompat_flags;
  410. buf[3] = msg->compat_flags;
  411. buf[4] = msg->seq;
  412. buf[5] = msg->sysid;
  413. buf[6] = msg->compid;
  414. buf[7] = msg->msgid & 0xFF;
  415. buf[8] = (msg->msgid >> 8) & 0xFF;
  416. buf[9] = (msg->msgid >> 16) & 0xFF;
  417. memcpy(&buf[10], _MAV_PAYLOAD(msg), length);
  418. ck = buf + header_len + 1 + (uint16_t)length;
  419. signature_len = (msg->incompat_flags & MAVLINK_IFLAG_SIGNED)?MAVLINK_SIGNATURE_BLOCK_LEN:0;
  420. }
  421. ck[0] = (uint8_t)(msg->checksum & 0xFF);
  422. ck[1] = (uint8_t)(msg->checksum >> 8);
  423. if (signature_len > 0) {
  424. memcpy(&ck[2], msg->signature, signature_len);
  425. }
  426. return header_len + 1 + 2 + (uint16_t)length + (uint16_t)signature_len;
  427. }
  428. union __mavlink_bitfield {
  429. uint8_t uint8;
  430. int8_t int8;
  431. uint16_t uint16;
  432. int16_t int16;
  433. uint32_t uint32;
  434. int32_t int32;
  435. };
  436. MAVLINK_HELPER void mavlink_start_checksum(mavlink_message_t* msg)
  437. {
  438. uint16_t crcTmp = 0;
  439. crc_init(&crcTmp);
  440. msg->checksum = crcTmp;
  441. }
  442. MAVLINK_HELPER void mavlink_update_checksum(mavlink_message_t* msg, uint8_t c)
  443. {
  444. uint16_t checksum = msg->checksum;
  445. crc_accumulate(c, &checksum);
  446. msg->checksum = checksum;
  447. }
  448. /*
  449. return the crc_entry value for a msgid
  450. */
  451. #ifndef MAVLINK_GET_MSG_ENTRY
  452. MAVLINK_HELPER const mavlink_msg_entry_t *mavlink_get_msg_entry(uint32_t msgid)
  453. {
  454. static const mavlink_msg_entry_t mavlink_message_crcs[] = MAVLINK_MESSAGE_CRCS;
  455. /*
  456. use a bisection search to find the right entry. A perfect hash may be better
  457. Note that this assumes the table is sorted by msgid
  458. */
  459. uint32_t low=0, high=sizeof(mavlink_message_crcs)/sizeof(mavlink_message_crcs[0]) - 1;
  460. while (low < high) {
  461. uint32_t mid = (low+1+high)/2;
  462. if (msgid < mavlink_message_crcs[mid].msgid) {
  463. high = mid-1;
  464. continue;
  465. }
  466. if (msgid > mavlink_message_crcs[mid].msgid) {
  467. low = mid;
  468. continue;
  469. }
  470. low = mid;
  471. break;
  472. }
  473. if (mavlink_message_crcs[low].msgid != msgid) {
  474. // msgid is not in the table
  475. return NULL;
  476. }
  477. return &mavlink_message_crcs[low];
  478. }
  479. #endif // MAVLINK_GET_MSG_ENTRY
  480. /*
  481. return the crc_extra value for a message
  482. */
  483. MAVLINK_HELPER uint8_t mavlink_get_crc_extra(const mavlink_message_t *msg)
  484. {
  485. const mavlink_msg_entry_t *e = mavlink_get_msg_entry(msg->msgid);
  486. return e?e->crc_extra:0;
  487. }
  488. /*
  489. return the min message length
  490. */
  491. #define MAVLINK_HAVE_MIN_MESSAGE_LENGTH
  492. MAVLINK_HELPER uint8_t mavlink_min_message_length(const mavlink_message_t *msg)
  493. {
  494. const mavlink_msg_entry_t *e = mavlink_get_msg_entry(msg->msgid);
  495. return e?e->min_msg_len:0;
  496. }
  497. /*
  498. return the max message length (including extensions)
  499. */
  500. #define MAVLINK_HAVE_MAX_MESSAGE_LENGTH
  501. MAVLINK_HELPER uint8_t mavlink_max_message_length(const mavlink_message_t *msg)
  502. {
  503. const mavlink_msg_entry_t *e = mavlink_get_msg_entry(msg->msgid);
  504. return e?e->max_msg_len:0;
  505. }
  506. /**
  507. * This is a variant of mavlink_frame_char() but with caller supplied
  508. * parsing buffers. It is useful when you want to create a MAVLink
  509. * parser in a library that doesn't use any global variables
  510. *
  511. * @param rxmsg parsing message buffer
  512. * @param status parsing status buffer
  513. * @param c The char to parse
  514. *
  515. * @param r_message NULL if no message could be decoded, otherwise the message data
  516. * @param r_mavlink_status if a message was decoded, this is filled with the channel's stats
  517. * @return 0 if no message could be decoded, 1 on good message and CRC, 2 on bad CRC
  518. *
  519. */
  520. MAVLINK_HELPER uint8_t mavlink_frame_char_buffer(mavlink_message_t* rxmsg,
  521. mavlink_status_t* status,
  522. uint8_t c,
  523. mavlink_message_t* r_message,
  524. mavlink_status_t* r_mavlink_status)
  525. {
  526. int bufferIndex = 0;
  527. status->msg_received = MAVLINK_FRAMING_INCOMPLETE;
  528. switch (status->parse_state)
  529. {
  530. case MAVLINK_PARSE_STATE_UNINIT:
  531. case MAVLINK_PARSE_STATE_IDLE:
  532. if (c == MAVLINK_STX)
  533. {
  534. status->parse_state = MAVLINK_PARSE_STATE_GOT_STX;
  535. rxmsg->len = 0;
  536. rxmsg->magic = c;
  537. status->flags &= ~MAVLINK_STATUS_FLAG_IN_MAVLINK1;
  538. mavlink_start_checksum(rxmsg);
  539. } else if (c == MAVLINK_STX_MAVLINK1)
  540. {
  541. status->parse_state = MAVLINK_PARSE_STATE_GOT_STX;
  542. rxmsg->len = 0;
  543. rxmsg->magic = c;
  544. status->flags |= MAVLINK_STATUS_FLAG_IN_MAVLINK1;
  545. mavlink_start_checksum(rxmsg);
  546. }
  547. break;
  548. case MAVLINK_PARSE_STATE_GOT_STX:
  549. if (status->msg_received
  550. /* Support shorter buffers than the
  551. default maximum packet size */
  552. #if (MAVLINK_MAX_PAYLOAD_LEN < 255)
  553. || c > MAVLINK_MAX_PAYLOAD_LEN
  554. #endif
  555. )
  556. {
  557. status->buffer_overrun++;
  558. _mav_parse_error(status);
  559. status->msg_received = 0;
  560. status->parse_state = MAVLINK_PARSE_STATE_IDLE;
  561. }
  562. else
  563. {
  564. // NOT counting STX, LENGTH, SEQ, SYSID, COMPID, MSGID, CRC1 and CRC2
  565. rxmsg->len = c;
  566. status->packet_idx = 0;
  567. mavlink_update_checksum(rxmsg, c);
  568. if (status->flags & MAVLINK_STATUS_FLAG_IN_MAVLINK1) {
  569. rxmsg->incompat_flags = 0;
  570. rxmsg->compat_flags = 0;
  571. status->parse_state = MAVLINK_PARSE_STATE_GOT_COMPAT_FLAGS;
  572. } else {
  573. status->parse_state = MAVLINK_PARSE_STATE_GOT_LENGTH;
  574. }
  575. }
  576. break;
  577. case MAVLINK_PARSE_STATE_GOT_LENGTH:
  578. rxmsg->incompat_flags = c;
  579. if ((rxmsg->incompat_flags & ~MAVLINK_IFLAG_MASK) != 0) {
  580. // message includes an incompatible feature flag
  581. _mav_parse_error(status);
  582. status->msg_received = 0;
  583. status->parse_state = MAVLINK_PARSE_STATE_IDLE;
  584. break;
  585. }
  586. mavlink_update_checksum(rxmsg, c);
  587. status->parse_state = MAVLINK_PARSE_STATE_GOT_INCOMPAT_FLAGS;
  588. break;
  589. case MAVLINK_PARSE_STATE_GOT_INCOMPAT_FLAGS:
  590. rxmsg->compat_flags = c;
  591. mavlink_update_checksum(rxmsg, c);
  592. status->parse_state = MAVLINK_PARSE_STATE_GOT_COMPAT_FLAGS;
  593. break;
  594. case MAVLINK_PARSE_STATE_GOT_COMPAT_FLAGS:
  595. rxmsg->seq = c;
  596. mavlink_update_checksum(rxmsg, c);
  597. status->parse_state = MAVLINK_PARSE_STATE_GOT_SEQ;
  598. break;
  599. case MAVLINK_PARSE_STATE_GOT_SEQ:
  600. rxmsg->sysid = c;
  601. mavlink_update_checksum(rxmsg, c);
  602. status->parse_state = MAVLINK_PARSE_STATE_GOT_SYSID;
  603. break;
  604. case MAVLINK_PARSE_STATE_GOT_SYSID:
  605. rxmsg->compid = c;
  606. mavlink_update_checksum(rxmsg, c);
  607. status->parse_state = MAVLINK_PARSE_STATE_GOT_COMPID;
  608. break;
  609. case MAVLINK_PARSE_STATE_GOT_COMPID:
  610. rxmsg->msgid = c;
  611. mavlink_update_checksum(rxmsg, c);
  612. if (status->flags & MAVLINK_STATUS_FLAG_IN_MAVLINK1) {
  613. if(rxmsg->len > 0) {
  614. status->parse_state = MAVLINK_PARSE_STATE_GOT_MSGID3;
  615. } else {
  616. status->parse_state = MAVLINK_PARSE_STATE_GOT_PAYLOAD;
  617. }
  618. #ifdef MAVLINK_CHECK_MESSAGE_LENGTH
  619. if (rxmsg->len < mavlink_min_message_length(rxmsg) ||
  620. rxmsg->len > mavlink_max_message_length(rxmsg)) {
  621. _mav_parse_error(status);
  622. status->parse_state = MAVLINK_PARSE_STATE_IDLE;
  623. break;
  624. }
  625. #endif
  626. } else {
  627. status->parse_state = MAVLINK_PARSE_STATE_GOT_MSGID1;
  628. }
  629. break;
  630. case MAVLINK_PARSE_STATE_GOT_MSGID1:
  631. rxmsg->msgid |= c<<8;
  632. mavlink_update_checksum(rxmsg, c);
  633. status->parse_state = MAVLINK_PARSE_STATE_GOT_MSGID2;
  634. break;
  635. case MAVLINK_PARSE_STATE_GOT_MSGID2:
  636. rxmsg->msgid |= ((uint32_t)c)<<16;
  637. mavlink_update_checksum(rxmsg, c);
  638. if(rxmsg->len > 0){
  639. status->parse_state = MAVLINK_PARSE_STATE_GOT_MSGID3;
  640. } else {
  641. status->parse_state = MAVLINK_PARSE_STATE_GOT_PAYLOAD;
  642. }
  643. #ifdef MAVLINK_CHECK_MESSAGE_LENGTH
  644. if (rxmsg->len < mavlink_min_message_length(rxmsg) ||
  645. rxmsg->len > mavlink_max_message_length(rxmsg))
  646. {
  647. _mav_parse_error(status);
  648. status->parse_state = MAVLINK_PARSE_STATE_IDLE;
  649. break;
  650. }
  651. #endif
  652. break;
  653. case MAVLINK_PARSE_STATE_GOT_MSGID3:
  654. _MAV_PAYLOAD_NON_CONST(rxmsg)[status->packet_idx++] = (char)c;
  655. mavlink_update_checksum(rxmsg, c);
  656. if (status->packet_idx == rxmsg->len)
  657. {
  658. status->parse_state = MAVLINK_PARSE_STATE_GOT_PAYLOAD;
  659. }
  660. break;
  661. case MAVLINK_PARSE_STATE_GOT_PAYLOAD: {
  662. const mavlink_msg_entry_t *e = mavlink_get_msg_entry(rxmsg->msgid);
  663. uint8_t crc_extra = e?e->crc_extra:0;
  664. mavlink_update_checksum(rxmsg, crc_extra);
  665. if (c != (rxmsg->checksum & 0xFF)) {
  666. status->parse_state = MAVLINK_PARSE_STATE_GOT_BAD_CRC1;
  667. } else {
  668. status->parse_state = MAVLINK_PARSE_STATE_GOT_CRC1;
  669. }
  670. rxmsg->ck[0] = c;
  671. // zero-fill the packet to cope with short incoming packets
  672. if (e && status->packet_idx < e->max_msg_len) {
  673. memset(&_MAV_PAYLOAD_NON_CONST(rxmsg)[status->packet_idx], 0, e->max_msg_len - status->packet_idx);
  674. }
  675. break;
  676. }
  677. case MAVLINK_PARSE_STATE_GOT_CRC1:
  678. case MAVLINK_PARSE_STATE_GOT_BAD_CRC1:
  679. if (status->parse_state == MAVLINK_PARSE_STATE_GOT_BAD_CRC1 || c != (rxmsg->checksum >> 8)) {
  680. // got a bad CRC message
  681. status->msg_received = MAVLINK_FRAMING_BAD_CRC;
  682. } else {
  683. // Successfully got message
  684. status->msg_received = MAVLINK_FRAMING_OK;
  685. }
  686. rxmsg->ck[1] = c;
  687. if (rxmsg->incompat_flags & MAVLINK_IFLAG_SIGNED) {
  688. status->parse_state = MAVLINK_PARSE_STATE_SIGNATURE_WAIT;
  689. status->signature_wait = MAVLINK_SIGNATURE_BLOCK_LEN;
  690. // If the CRC is already wrong, don't overwrite msg_received,
  691. // otherwise we can end up with garbage flagged as valid.
  692. if (status->msg_received != MAVLINK_FRAMING_BAD_CRC) {
  693. status->msg_received = MAVLINK_FRAMING_INCOMPLETE;
  694. }
  695. } else {
  696. if (status->signing &&
  697. (status->signing->accept_unsigned_callback == NULL ||
  698. !status->signing->accept_unsigned_callback(status, rxmsg->msgid))) {
  699. // If the CRC is already wrong, don't overwrite msg_received.
  700. if (status->msg_received != MAVLINK_FRAMING_BAD_CRC) {
  701. status->msg_received = MAVLINK_FRAMING_BAD_SIGNATURE;
  702. }
  703. }
  704. status->parse_state = MAVLINK_PARSE_STATE_IDLE;
  705. if (r_message != NULL) {
  706. memcpy(r_message, rxmsg, sizeof(mavlink_message_t));
  707. }
  708. }
  709. break;
  710. case MAVLINK_PARSE_STATE_SIGNATURE_WAIT:
  711. rxmsg->signature[MAVLINK_SIGNATURE_BLOCK_LEN-status->signature_wait] = c;
  712. status->signature_wait--;
  713. if (status->signature_wait == 0) {
  714. // we have the whole signature, check it is OK
  715. bool sig_ok = mavlink_signature_check(status->signing, status->signing_streams, rxmsg);
  716. if (!sig_ok &&
  717. (status->signing->accept_unsigned_callback &&
  718. status->signing->accept_unsigned_callback(status, rxmsg->msgid))) {
  719. // accepted via application level override
  720. sig_ok = true;
  721. }
  722. if (sig_ok) {
  723. status->msg_received = MAVLINK_FRAMING_OK;
  724. } else {
  725. status->msg_received = MAVLINK_FRAMING_BAD_SIGNATURE;
  726. }
  727. status->parse_state = MAVLINK_PARSE_STATE_IDLE;
  728. if (r_message !=NULL) {
  729. memcpy(r_message, rxmsg, sizeof(mavlink_message_t));
  730. }
  731. }
  732. break;
  733. }
  734. bufferIndex++;
  735. // If a message has been sucessfully decoded, check index
  736. if (status->msg_received == MAVLINK_FRAMING_OK)
  737. {
  738. //while(status->current_seq != rxmsg->seq)
  739. //{
  740. // status->packet_rx_drop_count++;
  741. // status->current_seq++;
  742. //}
  743. status->current_rx_seq = rxmsg->seq;
  744. // Initial condition: If no packet has been received so far, drop count is undefined
  745. if (status->packet_rx_success_count == 0) status->packet_rx_drop_count = 0;
  746. // Count this packet as received
  747. status->packet_rx_success_count++;
  748. }
  749. if (r_message != NULL) {
  750. r_message->len = rxmsg->len; // Provide visibility on how far we are into current msg
  751. }
  752. if (r_mavlink_status != NULL) {
  753. r_mavlink_status->parse_state = status->parse_state;
  754. r_mavlink_status->packet_idx = status->packet_idx;
  755. r_mavlink_status->current_rx_seq = status->current_rx_seq+1;
  756. r_mavlink_status->packet_rx_success_count = status->packet_rx_success_count;
  757. r_mavlink_status->packet_rx_drop_count = status->parse_error;
  758. r_mavlink_status->flags = status->flags;
  759. }
  760. status->parse_error = 0;
  761. if (status->msg_received == MAVLINK_FRAMING_BAD_CRC) {
  762. /*
  763. the CRC came out wrong. We now need to overwrite the
  764. msg CRC with the one on the wire so that if the
  765. caller decides to forward the message anyway that
  766. mavlink_msg_to_send_buffer() won't overwrite the
  767. checksum
  768. */
  769. if (r_message != NULL) {
  770. r_message->checksum = rxmsg->ck[0] | (rxmsg->ck[1]<<8);
  771. }
  772. }
  773. return status->msg_received;
  774. }
  775. /**
  776. * This is a convenience function which handles the complete MAVLink parsing.
  777. * the function will parse one byte at a time and return the complete packet once
  778. * it could be successfully decoded. This function will return 0, 1 or
  779. * 2 (MAVLINK_FRAMING_INCOMPLETE, MAVLINK_FRAMING_OK or MAVLINK_FRAMING_BAD_CRC)
  780. *
  781. * Messages are parsed into an internal buffer (one for each channel). When a complete
  782. * message is received it is copies into *r_message and the channel's status is
  783. * copied into *r_mavlink_status.
  784. *
  785. * @param chan ID of the channel to be parsed.
  786. * A channel is not a physical message channel like a serial port, but a logical partition of
  787. * the communication streams. COMM_NB is the limit for the number of channels
  788. * on MCU (e.g. ARM7), while COMM_NB_HIGH is the limit for the number of channels in Linux/Windows
  789. * @param c The char to parse
  790. *
  791. * @param r_message NULL if no message could be decoded, otherwise the message data
  792. * @param r_mavlink_status if a message was decoded, this is filled with the channel's stats
  793. * @return 0 if no message could be decoded, 1 on good message and CRC, 2 on bad CRC
  794. *
  795. * A typical use scenario of this function call is:
  796. *
  797. * @code
  798. * #include <mavlink.h>
  799. *
  800. * mavlink_status_t status;
  801. * mavlink_message_t msg;
  802. * int chan = 0;
  803. *
  804. *
  805. * while(serial.bytesAvailable > 0)
  806. * {
  807. * uint8_t byte = serial.getNextByte();
  808. * if (mavlink_frame_char(chan, byte, &msg, &status) != MAVLINK_FRAMING_INCOMPLETE)
  809. * {
  810. * printf("Received message with ID %d, sequence: %d from component %d of system %d", msg.msgid, msg.seq, msg.compid, msg.sysid);
  811. * }
  812. * }
  813. *
  814. *
  815. * @endcode
  816. */
  817. MAVLINK_HELPER uint8_t mavlink_frame_char(uint8_t chan, uint8_t c, mavlink_message_t* r_message, mavlink_status_t* r_mavlink_status)
  818. {
  819. return mavlink_frame_char_buffer(mavlink_get_channel_buffer(chan),
  820. mavlink_get_channel_status(chan),
  821. c,
  822. r_message,
  823. r_mavlink_status);
  824. }
  825. /**
  826. * Set the protocol version
  827. */
  828. MAVLINK_HELPER void mavlink_set_proto_version(uint8_t chan, unsigned int version)
  829. {
  830. mavlink_status_t *status = mavlink_get_channel_status(chan);
  831. if (version > 1) {
  832. status->flags &= ~(MAVLINK_STATUS_FLAG_OUT_MAVLINK1);
  833. } else {
  834. status->flags |= MAVLINK_STATUS_FLAG_OUT_MAVLINK1;
  835. }
  836. }
  837. /**
  838. * Get the protocol version
  839. *
  840. * @return 1 for v1, 2 for v2
  841. */
  842. MAVLINK_HELPER unsigned int mavlink_get_proto_version(uint8_t chan)
  843. {
  844. mavlink_status_t *status = mavlink_get_channel_status(chan);
  845. if ((status->flags & MAVLINK_STATUS_FLAG_OUT_MAVLINK1) > 0) {
  846. return 1;
  847. } else {
  848. return 2;
  849. }
  850. }
  851. /**
  852. * This is a convenience function which handles the complete MAVLink parsing.
  853. * the function will parse one byte at a time and return the complete packet once
  854. * it could be successfully decoded. This function will return 0 or 1.
  855. *
  856. * Messages are parsed into an internal buffer (one for each channel). When a complete
  857. * message is received it is copies into *r_message and the channel's status is
  858. * copied into *r_mavlink_status.
  859. *
  860. * @param chan ID of the channel to be parsed.
  861. * A channel is not a physical message channel like a serial port, but a logical partition of
  862. * the communication streams. COMM_NB is the limit for the number of channels
  863. * on MCU (e.g. ARM7), while COMM_NB_HIGH is the limit for the number of channels in Linux/Windows
  864. * @param c The char to parse
  865. *
  866. * @param r_message NULL if no message could be decoded, otherwise the message data
  867. * @param r_mavlink_status if a message was decoded, this is filled with the channel's stats
  868. * @return 0 if no message could be decoded or bad CRC, 1 on good message and CRC
  869. *
  870. * A typical use scenario of this function call is:
  871. *
  872. * @code
  873. * #include <mavlink.h>
  874. *
  875. * mavlink_status_t status;
  876. * mavlink_message_t msg;
  877. * int chan = 0;
  878. *
  879. *
  880. * while(serial.bytesAvailable > 0)
  881. * {
  882. * uint8_t byte = serial.getNextByte();
  883. * if (mavlink_parse_char(chan, byte, &msg, &status))
  884. * {
  885. * printf("Received message with ID %d, sequence: %d from component %d of system %d", msg.msgid, msg.seq, msg.compid, msg.sysid);
  886. * }
  887. * }
  888. *
  889. *
  890. * @endcode
  891. */
  892. MAVLINK_HELPER uint8_t mavlink_parse_char(uint8_t chan, uint8_t c, mavlink_message_t* r_message, mavlink_status_t* r_mavlink_status)
  893. {
  894. uint8_t msg_received = mavlink_frame_char(chan, c, r_message, r_mavlink_status);
  895. if (msg_received == MAVLINK_FRAMING_BAD_CRC ||
  896. msg_received == MAVLINK_FRAMING_BAD_SIGNATURE) {
  897. // we got a bad CRC. Treat as a parse failure
  898. mavlink_message_t* rxmsg = mavlink_get_channel_buffer(chan);
  899. mavlink_status_t* status = mavlink_get_channel_status(chan);
  900. _mav_parse_error(status);
  901. status->msg_received = MAVLINK_FRAMING_INCOMPLETE;
  902. status->parse_state = MAVLINK_PARSE_STATE_IDLE;
  903. if (c == MAVLINK_STX)
  904. {
  905. status->parse_state = MAVLINK_PARSE_STATE_GOT_STX;
  906. rxmsg->len = 0;
  907. mavlink_start_checksum(rxmsg);
  908. }
  909. return 0;
  910. }
  911. return msg_received;
  912. }
  913. /**
  914. * @brief Put a bitfield of length 1-32 bit into the buffer
  915. *
  916. * @param b the value to add, will be encoded in the bitfield
  917. * @param bits number of bits to use to encode b, e.g. 1 for boolean, 2, 3, etc.
  918. * @param packet_index the position in the packet (the index of the first byte to use)
  919. * @param bit_index the position in the byte (the index of the first bit to use)
  920. * @param buffer packet buffer to write into
  921. * @return new position of the last used byte in the buffer
  922. */
  923. MAVLINK_HELPER uint8_t put_bitfield_n_by_index(int32_t b, uint8_t bits, uint8_t packet_index, uint8_t bit_index, uint8_t* r_bit_index, uint8_t* buffer)
  924. {
  925. uint16_t bits_remain = bits;
  926. // Transform number into network order
  927. int32_t v;
  928. uint8_t i_bit_index, i_byte_index, curr_bits_n;
  929. #if MAVLINK_NEED_BYTE_SWAP
  930. union {
  931. int32_t i;
  932. uint8_t b[4];
  933. } bin, bout;
  934. bin.i = b;
  935. bout.b[0] = bin.b[3];
  936. bout.b[1] = bin.b[2];
  937. bout.b[2] = bin.b[1];
  938. bout.b[3] = bin.b[0];
  939. v = bout.i;
  940. #else
  941. v = b;
  942. #endif
  943. // buffer in
  944. // 01100000 01000000 00000000 11110001
  945. // buffer out
  946. // 11110001 00000000 01000000 01100000
  947. // Existing partly filled byte (four free slots)
  948. // 0111xxxx
  949. // Mask n free bits
  950. // 00001111 = 2^0 + 2^1 + 2^2 + 2^3 = 2^n - 1
  951. // = ((uint32_t)(1 << n)) - 1; // = 2^n - 1
  952. // Shift n bits into the right position
  953. // out = in >> n;
  954. // Mask and shift bytes
  955. i_bit_index = bit_index;
  956. i_byte_index = packet_index;
  957. if (bit_index > 0)
  958. {
  959. // If bits were available at start, they were available
  960. // in the byte before the current index
  961. i_byte_index--;
  962. }
  963. // While bits have not been packed yet
  964. while (bits_remain > 0)
  965. {
  966. // Bits still have to be packed
  967. // there can be more than 8 bits, so
  968. // we might have to pack them into more than one byte
  969. // First pack everything we can into the current 'open' byte
  970. //curr_bits_n = bits_remain << 3; // Equals bits_remain mod 8
  971. //FIXME
  972. if (bits_remain <= (uint8_t)(8 - i_bit_index))
  973. {
  974. // Enough space
  975. curr_bits_n = (uint8_t)bits_remain;
  976. }
  977. else
  978. {
  979. curr_bits_n = (8 - i_bit_index);
  980. }
  981. // Pack these n bits into the current byte
  982. // Mask out whatever was at that position with ones (xxx11111)
  983. buffer[i_byte_index] &= (0xFF >> (8 - curr_bits_n));
  984. // Put content to this position, by masking out the non-used part
  985. buffer[i_byte_index] |= ((0x00 << curr_bits_n) & v);
  986. // Increment the bit index
  987. i_bit_index += curr_bits_n;
  988. // Now proceed to the next byte, if necessary
  989. bits_remain -= curr_bits_n;
  990. if (bits_remain > 0)
  991. {
  992. // Offer another 8 bits / one byte
  993. i_byte_index++;
  994. i_bit_index = 0;
  995. }
  996. }
  997. *r_bit_index = i_bit_index;
  998. // If a partly filled byte is present, mark this as consumed
  999. if (i_bit_index != 7) i_byte_index++;
  1000. return i_byte_index - packet_index;
  1001. }
  1002. #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
  1003. // To make MAVLink work on your MCU, define comm_send_ch() if you wish
  1004. // to send 1 byte at a time, or MAVLINK_SEND_UART_BYTES() to send a
  1005. // whole packet at a time
  1006. /*
  1007. #include "mavlink_types.h"
  1008. void comm_send_ch(mavlink_channel_t chan, uint8_t ch)
  1009. {
  1010. if (chan == MAVLINK_COMM_0)
  1011. {
  1012. uart0_transmit(ch);
  1013. }
  1014. if (chan == MAVLINK_COMM_1)
  1015. {
  1016. uart1_transmit(ch);
  1017. }
  1018. }
  1019. */
  1020. MAVLINK_HELPER void _mavlink_send_uart(mavlink_channel_t chan, const char *buf, uint16_t len)
  1021. {
  1022. #ifdef MAVLINK_SEND_UART_BYTES
  1023. /* this is the more efficient approach, if the platform
  1024. defines it */
  1025. MAVLINK_SEND_UART_BYTES(chan, (const uint8_t *)buf, len);
  1026. #else
  1027. /* fallback to one byte at a time */
  1028. uint16_t i;
  1029. for (i = 0; i < len; i++) {
  1030. comm_send_ch(chan, (uint8_t)buf[i]);
  1031. }
  1032. #endif
  1033. }
  1034. #endif // MAVLINK_USE_CONVENIENCE_FUNCTIONS
  1035. #ifdef MAVLINK_USE_CXX_NAMESPACE
  1036. } // namespace mavlink
  1037. #endif