Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
checksum.h
00001 #ifdef __cplusplus 00002 extern "C" { 00003 #endif 00004 00005 #ifndef _CHECKSUM_H_ 00006 #define _CHECKSUM_H_ 00007 00008 // Visual Studio versions before 2010 don't have stdint.h, so we just error out. 00009 #if (defined _MSC_VER) && (_MSC_VER < 1600) 00010 #error "The C-MAVLink implementation requires Visual Studio 2010 or greater" 00011 #endif 00012 00013 #include <stdint.h> 00014 00015 /** 00016 * 00017 * CALCULATE THE CHECKSUM 00018 * 00019 */ 00020 00021 #define X25_INIT_CRC 0xffff 00022 #define X25_VALIDATE_CRC 0xf0b8 00023 00024 #ifndef HAVE_CRC_ACCUMULATE 00025 /** 00026 * @brief Accumulate the X.25 CRC by adding one char at a time. 00027 * 00028 * The checksum function adds the hash of one char at a time to the 00029 * 16 bit checksum (uint16_t). 00030 * 00031 * @param data new char to hash 00032 * @param crcAccum the already accumulated checksum 00033 **/ 00034 static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum) 00035 { 00036 /*Accumulate one byte of data into the CRC*/ 00037 uint8_t tmp; 00038 00039 tmp = data ^ (uint8_t)(*crcAccum &0xff); 00040 tmp ^= (tmp<<4); 00041 *crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4); 00042 } 00043 #endif 00044 00045 00046 /** 00047 * @brief Initiliaze the buffer for the X.25 CRC 00048 * 00049 * @param crcAccum the 16 bit X.25 CRC 00050 */ 00051 static inline void crc_init(uint16_t* crcAccum) 00052 { 00053 *crcAccum = X25_INIT_CRC; 00054 } 00055 00056 00057 /** 00058 * @brief Calculates the X.25 checksum on a byte buffer 00059 * 00060 * @param pBuffer buffer containing the byte array to hash 00061 * @param length length of the byte array 00062 * @return the checksum over the buffer bytes 00063 **/ 00064 static inline uint16_t crc_calculate(const uint8_t* pBuffer, uint16_t length) 00065 { 00066 uint16_t crcTmp; 00067 crc_init(&crcTmp); 00068 while (length--) { 00069 crc_accumulate(*pBuffer++, &crcTmp); 00070 } 00071 return crcTmp; 00072 } 00073 00074 00075 /** 00076 * @brief Accumulate the X.25 CRC by adding an array of bytes 00077 * 00078 * The checksum function adds the hash of one char at a time to the 00079 * 16 bit checksum (uint16_t). 00080 * 00081 * @param data new bytes to hash 00082 * @param crcAccum the already accumulated checksum 00083 **/ 00084 static inline void crc_accumulate_buffer(uint16_t *crcAccum, const char *pBuffer, uint16_t length) 00085 { 00086 const uint8_t *p = (const uint8_t *)pBuffer; 00087 while (length--) { 00088 crc_accumulate(*p++, crcAccum); 00089 } 00090 } 00091 00092 #endif /* _CHECKSUM_H_ */ 00093 00094 #ifdef __cplusplus 00095 } 00096 #endif 00097
Generated on Wed Jul 13 2022 05:12:03 by
