Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1702 - 1
#ifndef  _MAVLINK_HELPERS_H_
2
#define  _MAVLINK_HELPERS_H_
3
 
4
#include "string.h"
5
#include "checksum.h"
6
#include "mavlink_types.h"
7
 
8
#ifndef MAVLINK_HELPER
9
#define MAVLINK_HELPER
10
#endif
11
 
12
/*
13
  internal function to give access to the channel status for each channel
14
 */
15
MAVLINK_HELPER mavlink_status_t* mavlink_get_channel_status(uint8_t chan)
16
{
17
        static mavlink_status_t m_mavlink_status[MAVLINK_COMM_NUM_BUFFERS];
18
        return &m_mavlink_status[chan];
19
}
20
 
21
/*
22
 internal function to give access to the channel buffer for each channel
23
 */
24
MAVLINK_HELPER mavlink_message_t* mavlink_get_channel_buffer(uint8_t chan)
25
{
26
 
27
#if MAVLINK_EXTERNAL_RX_BUFFER
28
        // No m_mavlink_message array defined in function,
29
        // has to be defined externally
30
#ifndef m_mavlink_message
31
#error ERROR: IF #define MAVLINK_EXTERNAL_RX_BUFFER IS SET, THE BUFFER HAS TO BE ALLOCATED OUTSIDE OF THIS FUNCTION (mavlink_message_t m_mavlink_buffer[MAVLINK_COMM_NUM_BUFFERS];)
32
#endif
33
#else
34
        static mavlink_message_t m_mavlink_buffer[MAVLINK_COMM_NUM_BUFFERS];
35
#endif
36
        return &m_mavlink_buffer[chan];
37
}
38
 
39
/**
40
 * @brief Finalize a MAVLink message with channel assignment
41
 *
42
 * This function calculates the checksum and sets length and aircraft id correctly.
43
 * It assumes that the message id and the payload are already correctly set. This function
44
 * can also be used if the message header has already been written before (as in mavlink_msg_xxx_pack
45
 * instead of mavlink_msg_xxx_pack_headerless), it just introduces little extra overhead.
46
 *
47
 * @param msg Message to finalize
48
 * @param system_id Id of the sending (this) system, 1-127
49
 * @param length Message length
50
 */
51
#if MAVLINK_CRC_EXTRA
52
MAVLINK_HELPER uint16_t mavlink_finalize_message_chan(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id,
53
                                                      uint8_t chan, uint8_t length, uint8_t crc_extra)
54
#else
55
MAVLINK_HELPER uint16_t mavlink_finalize_message_chan(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id,
56
                                                      uint8_t chan, uint8_t length)
57
#endif
58
{
59
        // This code part is the same for all messages;
60
        uint16_t checksum;
61
        msg->magic = MAVLINK_STX;
62
        msg->len = length;
63
        msg->sysid = system_id;
64
        msg->compid = component_id;
65
        // One sequence number per component
66
        msg->seq = mavlink_get_channel_status(chan)->current_tx_seq;
67
        mavlink_get_channel_status(chan)->current_tx_seq = mavlink_get_channel_status(chan)->current_tx_seq+1;
68
        checksum = crc_calculate((uint8_t*)&msg->len, length + MAVLINK_CORE_HEADER_LEN);
69
#if MAVLINK_CRC_EXTRA
70
        crc_accumulate(crc_extra, &checksum);
71
#endif
72
        mavlink_ck_a(msg) = (uint8_t)(checksum & 0xFF);
73
        mavlink_ck_b(msg) = (uint8_t)(checksum >> 8);
74
 
75
        return length + MAVLINK_NUM_NON_PAYLOAD_BYTES;
76
}
77
 
78
 
79
/**
80
 * @brief Finalize a MAVLink message with MAVLINK_COMM_0 as default channel
81
 */
82
#if MAVLINK_CRC_EXTRA
83
MAVLINK_HELPER uint16_t mavlink_finalize_message(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id,
84
                                                 uint8_t length, uint8_t crc_extra)
85
{
86
        return mavlink_finalize_message_chan(msg, system_id, component_id, MAVLINK_COMM_0, length, crc_extra);
87
}
88
#else
89
MAVLINK_HELPER uint16_t mavlink_finalize_message(mavlink_message_t* msg, uint8_t system_id, uint8_t component_id,
90
                                                 uint8_t length)
91
{
92
        return mavlink_finalize_message_chan(msg, system_id, component_id, MAVLINK_COMM_0, length);
93
}
94
#endif
95
 
96
#ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
97
MAVLINK_HELPER void _mavlink_send_uart(mavlink_channel_t chan, const char *buf, uint16_t len);
98
 
99
/**
100
 * @brief Finalize a MAVLink message with channel assignment and send
101
 */
102
#if MAVLINK_CRC_EXTRA
103
MAVLINK_HELPER void _mav_finalize_message_chan_send(mavlink_channel_t chan, uint8_t msgid, const char *packet,
104
                                                    uint8_t length, uint8_t crc_extra)
105
#else
106
MAVLINK_HELPER void _mav_finalize_message_chan_send(mavlink_channel_t chan, uint8_t msgid, const char *packet, uint8_t length)
107
#endif
108
{
109
        uint16_t checksum;
110
        uint8_t buf[MAVLINK_NUM_HEADER_BYTES];
111
        uint8_t ck[2];
112
        mavlink_status_t *status = mavlink_get_channel_status(chan);
113
        buf[0] = MAVLINK_STX;
114
        buf[1] = length;
115
        buf[2] = status->current_tx_seq;
116
        buf[3] = mavlink_system.sysid;
117
        buf[4] = mavlink_system.compid;
118
        buf[5] = msgid;
119
        status->current_tx_seq++;
120
        checksum = crc_calculate((uint8_t*)&buf[1], MAVLINK_CORE_HEADER_LEN);
121
        crc_accumulate_buffer(&checksum, packet, length);
122
#if MAVLINK_CRC_EXTRA
123
        crc_accumulate(crc_extra, &checksum);
124
#endif
125
        ck[0] = (uint8_t)(checksum & 0xFF);
126
        ck[1] = (uint8_t)(checksum >> 8);
127
 
128
        MAVLINK_START_UART_SEND(chan, MAVLINK_NUM_NON_PAYLOAD_BYTES + (uint16_t)length);
129
        _mavlink_send_uart(chan, (const char *)buf, MAVLINK_NUM_HEADER_BYTES);
130
        _mavlink_send_uart(chan, packet, length);
131
        _mavlink_send_uart(chan, (const char *)ck, 2);
132
        MAVLINK_END_UART_SEND(chan, MAVLINK_NUM_NON_PAYLOAD_BYTES + (uint16_t)length);
133
}
134
#endif // MAVLINK_USE_CONVENIENCE_FUNCTIONS
135
 
136
/**
137
 * @brief Pack a message to send it over a serial byte stream
138
 */
139
MAVLINK_HELPER uint16_t mavlink_msg_to_send_buffer(uint8_t *buffer, const mavlink_message_t *msg)
140
{
141
        memcpy(buffer, (const uint8_t *)&msg->magic, MAVLINK_NUM_NON_PAYLOAD_BYTES + (uint16_t)msg->len);
142
        return MAVLINK_NUM_NON_PAYLOAD_BYTES + (uint16_t)msg->len;
143
}
144
 
145
union __mavlink_bitfield {
146
        uint8_t uint8;
147
        int8_t int8;
148
        uint16_t uint16;
149
        int16_t int16;
150
        uint32_t uint32;
151
        int32_t int32;
152
};
153
 
154
 
155
MAVLINK_HELPER void mavlink_start_checksum(mavlink_message_t* msg)
156
{
157
        crc_init(&msg->checksum);
158
}
159
 
160
MAVLINK_HELPER void mavlink_update_checksum(mavlink_message_t* msg, uint8_t c)
161
{
162
        crc_accumulate(c, &msg->checksum);
163
}
164
 
165
/**
166
 * This is a convenience function which handles the complete MAVLink parsing.
167
 * the function will parse one byte at a time and return the complete packet once
168
 * it could be successfully decoded. Checksum and other failures will be silently
169
 * ignored.
170
 *
171
 * @param chan     ID of the current channel. This allows to parse different channels with this function.
172
 *                 a channel is not a physical message channel like a serial port, but a logic partition of
173
 *                 the communication streams in this case. COMM_NB is the limit for the number of channels
174
 *                 on MCU (e.g. ARM7), while COMM_NB_HIGH is the limit for the number of channels in Linux/Windows
175
 * @param c        The char to barse
176
 *
177
 * @param returnMsg NULL if no message could be decoded, the message data else
178
 * @return 0 if no message could be decoded, 1 else
179
 *
180
 * A typical use scenario of this function call is:
181
 *
182
 * @code
183
 * #include <inttypes.h> // For fixed-width uint8_t type
184
 *
185
 * mavlink_message_t msg;
186
 * int chan = 0;
187
 *
188
 *
189
 * while(serial.bytesAvailable > 0)
190
 * {
191
 *   uint8_t byte = serial.getNextByte();
192
 *   if (mavlink_parse_char(chan, byte, &msg))
193
 *     {
194
 *     printf("Received message with ID %d, sequence: %d from component %d of system %d", msg.msgid, msg.seq, msg.compid, msg.sysid);
195
 *     }
196
 * }
197
 *
198
 *
199
 * @endcode
200
 */
201
MAVLINK_HELPER uint8_t mavlink_parse_char(uint8_t chan, uint8_t c, mavlink_message_t* r_message, mavlink_status_t* r_mavlink_status)
202
{
203
        /*
204
          default message crc function. You can override this per-system to
205
          put this data in a different memory segment
206
        */
207
#if MAVLINK_CRC_EXTRA
208
#ifndef MAVLINK_MESSAGE_CRC
209
        static const uint8_t mavlink_message_crcs[256] = MAVLINK_MESSAGE_CRCS;
210
#define MAVLINK_MESSAGE_CRC(msgid) mavlink_message_crcs[msgid]
211
#endif
212
#endif
213
 
214
        mavlink_message_t* rxmsg = mavlink_get_channel_buffer(chan); ///< The currently decoded message
215
        mavlink_status_t* status = mavlink_get_channel_status(chan); ///< The current decode status
216
        int bufferIndex = 0;
217
 
218
        status->msg_received = 0;
219
 
220
        switch (status->parse_state)
221
        {
222
        case MAVLINK_PARSE_STATE_UNINIT:
223
        case MAVLINK_PARSE_STATE_IDLE:
224
                if (c == MAVLINK_STX)
225
                {
226
                        status->parse_state = MAVLINK_PARSE_STATE_GOT_STX;
227
                        rxmsg->len = 0;
228
                        rxmsg->magic = c;
229
                        mavlink_start_checksum(rxmsg);
230
                }
231
                break;
232
 
233
        case MAVLINK_PARSE_STATE_GOT_STX:
234
                        if (status->msg_received
235
/* Support shorter buffers than the
236
   default maximum packet size */
237
#if (MAVLINK_MAX_PAYLOAD_LEN < 255)
238
                                || c > MAVLINK_MAX_PAYLOAD_LEN
239
#endif
240
                                )
241
                {
242
                        status->buffer_overrun++;
243
                        status->parse_error++;
244
                        status->msg_received = 0;
245
                        status->parse_state = MAVLINK_PARSE_STATE_IDLE;
246
                }
247
                else
248
                {
249
                        // NOT counting STX, LENGTH, SEQ, SYSID, COMPID, MSGID, CRC1 and CRC2
250
                        rxmsg->len = c;
251
                        status->packet_idx = 0;
252
                        mavlink_update_checksum(rxmsg, c);
253
                        status->parse_state = MAVLINK_PARSE_STATE_GOT_LENGTH;
254
                }
255
                break;
256
 
257
        case MAVLINK_PARSE_STATE_GOT_LENGTH:
258
                rxmsg->seq = c;
259
                mavlink_update_checksum(rxmsg, c);
260
                status->parse_state = MAVLINK_PARSE_STATE_GOT_SEQ;
261
                break;
262
 
263
        case MAVLINK_PARSE_STATE_GOT_SEQ:
264
                rxmsg->sysid = c;
265
                mavlink_update_checksum(rxmsg, c);
266
                status->parse_state = MAVLINK_PARSE_STATE_GOT_SYSID;
267
                break;
268
 
269
        case MAVLINK_PARSE_STATE_GOT_SYSID:
270
                rxmsg->compid = c;
271
                mavlink_update_checksum(rxmsg, c);
272
                status->parse_state = MAVLINK_PARSE_STATE_GOT_COMPID;
273
                break;
274
 
275
        case MAVLINK_PARSE_STATE_GOT_COMPID:
276
                rxmsg->msgid = c;
277
                mavlink_update_checksum(rxmsg, c);
278
                if (rxmsg->len == 0)
279
                {
280
                        status->parse_state = MAVLINK_PARSE_STATE_GOT_PAYLOAD;
281
                }
282
                else
283
                {
284
                        status->parse_state = MAVLINK_PARSE_STATE_GOT_MSGID;
285
                }
286
                break;
287
 
288
        case MAVLINK_PARSE_STATE_GOT_MSGID:
289
                _MAV_PAYLOAD_NON_CONST(rxmsg)[status->packet_idx++] = (char)c;
290
                mavlink_update_checksum(rxmsg, c);
291
                if (status->packet_idx == rxmsg->len)
292
                {
293
                        status->parse_state = MAVLINK_PARSE_STATE_GOT_PAYLOAD;
294
                }
295
                break;
296
 
297
        case MAVLINK_PARSE_STATE_GOT_PAYLOAD:
298
#if MAVLINK_CRC_EXTRA
299
                mavlink_update_checksum(rxmsg, MAVLINK_MESSAGE_CRC(rxmsg->msgid));
300
#endif
301
                if (c != (rxmsg->checksum & 0xFF)) {
302
                        // Check first checksum byte
303
                        status->parse_error++;
304
                        status->msg_received = 0;
305
                        status->parse_state = MAVLINK_PARSE_STATE_IDLE;
306
                        if (c == MAVLINK_STX)
307
                        {
308
                                status->parse_state = MAVLINK_PARSE_STATE_GOT_STX;
309
                                rxmsg->len = 0;
310
                                mavlink_start_checksum(rxmsg);
311
                        }
312
                }
313
                else
314
                {
315
                        status->parse_state = MAVLINK_PARSE_STATE_GOT_CRC1;
316
                        _MAV_PAYLOAD_NON_CONST(rxmsg)[status->packet_idx] = (char)c;
317
                }
318
                break;
319
 
320
        case MAVLINK_PARSE_STATE_GOT_CRC1:
321
                if (c != (rxmsg->checksum >> 8)) {
322
                        // Check second checksum byte
323
                        status->parse_error++;
324
                        status->msg_received = 0;
325
                        status->parse_state = MAVLINK_PARSE_STATE_IDLE;
326
                        if (c == MAVLINK_STX)
327
                        {
328
                                status->parse_state = MAVLINK_PARSE_STATE_GOT_STX;
329
                                rxmsg->len = 0;
330
                                mavlink_start_checksum(rxmsg);
331
                        }
332
                }
333
                else
334
                {
335
                        // Successfully got message
336
                        status->msg_received = 1;
337
                        status->parse_state = MAVLINK_PARSE_STATE_IDLE;
338
                        _MAV_PAYLOAD_NON_CONST(rxmsg)[status->packet_idx+1] = (char)c;
339
                        memcpy(r_message, rxmsg, sizeof(mavlink_message_t));
340
                }
341
                break;
342
        }
343
 
344
        bufferIndex++;
345
        // If a message has been sucessfully decoded, check index
346
        if (status->msg_received == 1)
347
        {
348
                //while(status->current_seq != rxmsg->seq)
349
                //{
350
                //      status->packet_rx_drop_count++;
351
                //               status->current_seq++;
352
                //}
353
                status->current_rx_seq = rxmsg->seq;
354
                // Initial condition: If no packet has been received so far, drop count is undefined
355
                if (status->packet_rx_success_count == 0) status->packet_rx_drop_count = 0;
356
                // Count this packet as received
357
                status->packet_rx_success_count++;
358
        }
359
 
360
        r_mavlink_status->current_rx_seq = status->current_rx_seq+1;
361
        r_mavlink_status->packet_rx_success_count = status->packet_rx_success_count;
362
        r_mavlink_status->packet_rx_drop_count = status->parse_error;
363
        status->parse_error = 0;
364
        return status->msg_received;
365
}
366
 
367
/**
368
 * @brief Put a bitfield of length 1-32 bit into the buffer
369
 *
370
 * @param b the value to add, will be encoded in the bitfield
371
 * @param bits number of bits to use to encode b, e.g. 1 for boolean, 2, 3, etc.
372
 * @param packet_index the position in the packet (the index of the first byte to use)
373
 * @param bit_index the position in the byte (the index of the first bit to use)
374
 * @param buffer packet buffer to write into
375
 * @return new position of the last used byte in the buffer
376
 */
377
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)
378
{
379
        uint16_t bits_remain = bits;
380
        // Transform number into network order
381
        int32_t v;
382
        uint8_t i_bit_index, i_byte_index, curr_bits_n;
383
#if MAVLINK_NEED_BYTE_SWAP
384
        union {
385
                int32_t i;
386
                uint8_t b[4];
387
        } bin, bout;
388
        bin.i = b;
389
        bout.b[0] = bin.b[3];
390
        bout.b[1] = bin.b[2];
391
        bout.b[2] = bin.b[1];
392
        bout.b[3] = bin.b[0];
393
        v = bout.i;
394
#else
395
        v = b;
396
#endif
397
 
398
        // buffer in
399
        // 01100000 01000000 00000000 11110001
400
        // buffer out
401
        // 11110001 00000000 01000000 01100000
402
 
403
        // Existing partly filled byte (four free slots)
404
        // 0111xxxx
405
 
406
        // Mask n free bits
407
        // 00001111 = 2^0 + 2^1 + 2^2 + 2^3 = 2^n - 1
408
        // = ((uint32_t)(1 << n)) - 1; // = 2^n - 1
409
 
410
        // Shift n bits into the right position
411
        // out = in >> n;
412
 
413
        // Mask and shift bytes
414
        i_bit_index = bit_index;
415
        i_byte_index = packet_index;
416
        if (bit_index > 0)
417
        {
418
                // If bits were available at start, they were available
419
                // in the byte before the current index
420
                i_byte_index--;
421
        }
422
 
423
        // While bits have not been packed yet
424
        while (bits_remain > 0)
425
        {
426
                // Bits still have to be packed
427
                // there can be more than 8 bits, so
428
                // we might have to pack them into more than one byte
429
 
430
                // First pack everything we can into the current 'open' byte
431
                //curr_bits_n = bits_remain << 3; // Equals  bits_remain mod 8
432
                //FIXME
433
                if (bits_remain <= (uint8_t)(8 - i_bit_index))
434
                {
435
                        // Enough space
436
                        curr_bits_n = (uint8_t)bits_remain;
437
                }
438
                else
439
                {
440
                        curr_bits_n = (8 - i_bit_index);
441
                }
442
 
443
                // Pack these n bits into the current byte
444
                // Mask out whatever was at that position with ones (xxx11111)
445
                buffer[i_byte_index] &= (0xFF >> (8 - curr_bits_n));
446
                // Put content to this position, by masking out the non-used part
447
                buffer[i_byte_index] |= ((0x00 << curr_bits_n) & v);
448
 
449
                // Increment the bit index
450
                i_bit_index += curr_bits_n;
451
 
452
                // Now proceed to the next byte, if necessary
453
                bits_remain -= curr_bits_n;
454
                if (bits_remain > 0)
455
                {
456
                        // Offer another 8 bits / one byte
457
                        i_byte_index++;
458
                        i_bit_index = 0;
459
                }
460
        }
461
 
462
        *r_bit_index = i_bit_index;
463
        // If a partly filled byte is present, mark this as consumed
464
        if (i_bit_index != 7) i_byte_index++;
465
        return i_byte_index - packet_index;
466
}
467
 
468
#ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
469
 
470
// To make MAVLink work on your MCU, define comm_send_ch() if you wish
471
// to send 1 byte at a time, or MAVLINK_SEND_UART_BYTES() to send a
472
// whole packet at a time
473
 
474
/*
475
 
476
#include "mavlink_types.h"
477
 
478
void comm_send_ch(mavlink_channel_t chan, uint8_t ch)
479
{
480
    if (chan == MAVLINK_COMM_0)
481
    {
482
        uart0_transmit(ch);
483
    }
484
    if (chan == MAVLINK_COMM_1)
485
    {
486
        uart1_transmit(ch);
487
    }
488
}
489
 */
490
 
491
MAVLINK_HELPER void _mavlink_send_uart(mavlink_channel_t chan, const char *buf, uint16_t len)
492
{
493
#ifdef MAVLINK_SEND_UART_BYTES
494
        /* this is the more efficient approach, if the platform
495
           defines it */
496
        MAVLINK_SEND_UART_BYTES(chan, (uint8_t *)buf, len);
497
#else
498
        /* fallback to one byte at a time */
499
        uint16_t i;
500
        for (i = 0; i < len; i++) {
501
                comm_send_ch(chan, (uint8_t)buf[i]);
502
        }
503
#endif
504
}
505
#endif // MAVLINK_USE_CONVENIENCE_FUNCTIONS
506
 
507
#endif /* _MAVLINK_HELPERS_H_ */