Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / libraries / mavlink / v1.0 / checksum.h
blob73f6738af403fa6403d7a397c8a9892e300b4e82
1 #ifdef __cplusplus
2 extern "C" {
3 #endif
5 #ifndef _CHECKSUM_H_
6 #define _CHECKSUM_H_
9 /**
11 * CALCULATE THE CHECKSUM
15 #define X25_INIT_CRC 0xffff
16 #define X25_VALIDATE_CRC 0xf0b8
18 /**
19 * @brief Accumulate the X.25 CRC by adding one char at a time.
21 * The checksum function adds the hash of one char at a time to the
22 * 16 bit checksum (uint16_t).
24 * @param data new char to hash
25 * @param crcAccum the already accumulated checksum
26 **/
27 static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum)
29 /*Accumulate one byte of data into the CRC*/
30 uint8_t tmp;
32 tmp = data ^ (uint8_t)(*crcAccum & 0xff);
33 tmp ^= (tmp << 4);
34 *crcAccum = (*crcAccum >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4);
37 /**
38 * @brief Initiliaze the buffer for the X.25 CRC
40 * @param crcAccum the 16 bit X.25 CRC
42 static inline void crc_init(uint16_t *crcAccum)
44 *crcAccum = X25_INIT_CRC;
48 /**
49 * @brief Calculates the X.25 checksum on a byte buffer
51 * @param pBuffer buffer containing the byte array to hash
52 * @param length length of the byte array
53 * @return the checksum over the buffer bytes
54 **/
55 static inline uint16_t crc_calculate(const uint8_t *pBuffer, uint16_t length)
57 uint16_t crcTmp;
59 crc_init(&crcTmp);
60 while (length--) {
61 crc_accumulate(*pBuffer++, &crcTmp);
63 return crcTmp;
66 /**
67 * @brief Accumulate the X.25 CRC by adding an array of bytes
69 * The checksum function adds the hash of one char at a time to the
70 * 16 bit checksum (uint16_t).
72 * @param data new bytes to hash
73 * @param crcAccum the already accumulated checksum
74 **/
75 static inline void crc_accumulate_buffer(uint16_t *crcAccum, const char *pBuffer, uint8_t length)
77 const uint8_t *p = (const uint8_t *)pBuffer;
79 while (length--) {
80 crc_accumulate(*p++, crcAccum);
85 #endif /* _CHECKSUM_H_ */
87 #ifdef __cplusplus
89 #endif