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