source: flair-src/branches/mavlink/tools/Controller/Mavlink/src/include/checksum.h@ 112

Last change on this file since 112 was 75, checked in by Thomas Fuhrmann, 8 years ago

Change the version of mavlink generated messages and rename it to include

File size: 2.1 KB
Line 
1#ifdef __cplusplus
2extern "C" {
3#endif
4
5#ifndef _CHECKSUM_H_
6#define _CHECKSUM_H_
7
8// Visual Studio versions before 2010 don't have stdint.h, so we just error out.
9#if (defined _MSC_VER) && (_MSC_VER < 1600)
10#error "The C-MAVLink implementation requires Visual Studio 2010 or greater"
11#endif
12
13#include <stdint.h>
14
15/**
16 *
17 * CALCULATE THE CHECKSUM
18 *
19 */
20
21#define X25_INIT_CRC 0xffff
22#define X25_VALIDATE_CRC 0xf0b8
23
24#ifndef HAVE_CRC_ACCUMULATE
25/**
26 * @brief Accumulate the X.25 CRC by adding one char at a time.
27 *
28 * The checksum function adds the hash of one char at a time to the
29 * 16 bit checksum (uint16_t).
30 *
31 * @param data new char to hash
32 * @param crcAccum the already accumulated checksum
33 **/
34static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum)
35{
36 /*Accumulate one byte of data into the CRC*/
37 uint8_t tmp;
38
39 tmp = data ^ (uint8_t)(*crcAccum &0xff);
40 tmp ^= (tmp<<4);
41 *crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4);
42}
43#endif
44
45
46/**
47 * @brief Initiliaze the buffer for the X.25 CRC
48 *
49 * @param crcAccum the 16 bit X.25 CRC
50 */
51static inline void crc_init(uint16_t* crcAccum)
52{
53 *crcAccum = X25_INIT_CRC;
54}
55
56
57/**
58 * @brief Calculates the X.25 checksum on a byte buffer
59 *
60 * @param pBuffer buffer containing the byte array to hash
61 * @param length length of the byte array
62 * @return the checksum over the buffer bytes
63 **/
64static inline uint16_t crc_calculate(const uint8_t* pBuffer, uint16_t length)
65{
66 uint16_t crcTmp;
67 crc_init(&crcTmp);
68 while (length--) {
69 crc_accumulate(*pBuffer++, &crcTmp);
70 }
71 return crcTmp;
72}
73
74
75/**
76 * @brief Accumulate the X.25 CRC by adding an array of bytes
77 *
78 * The checksum function adds the hash of one char at a time to the
79 * 16 bit checksum (uint16_t).
80 *
81 * @param data new bytes to hash
82 * @param crcAccum the already accumulated checksum
83 **/
84static inline void crc_accumulate_buffer(uint16_t *crcAccum, const char *pBuffer, uint16_t length)
85{
86 const uint8_t *p = (const uint8_t *)pBuffer;
87 while (length--) {
88 crc_accumulate(*p++, crcAccum);
89 }
90}
91
92#endif /* _CHECKSUM_H_ */
93
94#ifdef __cplusplus
95}
96#endif
Note: See TracBrowser for help on using the repository browser.