2011-10-16 03:58:14 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _CHECKSUM_H_
|
|
|
|
#define _CHECKSUM_H_
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* CALCULATE THE CHECKSUM
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define X25_INIT_CRC 0xffff
|
|
|
|
#define X25_VALIDATE_CRC 0xf0b8
|
|
|
|
|
2013-01-08 00:58:18 -04:00
|
|
|
#ifndef HAVE_CRC_ACCUMULATE
|
2011-10-16 03:58:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Accumulate the X.25 CRC by adding one char at a time.
|
|
|
|
*
|
|
|
|
* The checksum function adds the hash of one char at a time to the
|
|
|
|
* 16 bit checksum (uint16_t).
|
|
|
|
*
|
|
|
|
* @param data new char to hash
|
|
|
|
* @param crcAccum the already accumulated checksum
|
|
|
|
**/
|
|
|
|
static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum)
|
|
|
|
{
|
|
|
|
/*Accumulate one byte of data into the CRC*/
|
|
|
|
uint8_t tmp;
|
|
|
|
|
|
|
|
tmp = data ^ (uint8_t)(*crcAccum &0xff);
|
|
|
|
tmp ^= (tmp<<4);
|
|
|
|
*crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4);
|
|
|
|
}
|
2013-01-08 00:58:18 -04:00
|
|
|
#endif
|
2011-10-16 03:58:14 -03:00
|
|
|
|
2014-07-24 08:49:05 -03:00
|
|
|
|
2011-10-16 03:58:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Initiliaze the buffer for the X.25 CRC
|
|
|
|
*
|
|
|
|
* @param crcAccum the 16 bit X.25 CRC
|
|
|
|
*/
|
|
|
|
static inline void crc_init(uint16_t* crcAccum)
|
|
|
|
{
|
|
|
|
*crcAccum = X25_INIT_CRC;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Calculates the X.25 checksum on a byte buffer
|
|
|
|
*
|
|
|
|
* @param pBuffer buffer containing the byte array to hash
|
|
|
|
* @param length length of the byte array
|
|
|
|
* @return the checksum over the buffer bytes
|
|
|
|
**/
|
2012-04-19 16:46:29 -03:00
|
|
|
static inline uint16_t crc_calculate(const uint8_t* pBuffer, uint16_t length)
|
2011-10-16 03:58:14 -03:00
|
|
|
{
|
|
|
|
uint16_t crcTmp;
|
|
|
|
crc_init(&crcTmp);
|
|
|
|
while (length--) {
|
|
|
|
crc_accumulate(*pBuffer++, &crcTmp);
|
|
|
|
}
|
|
|
|
return crcTmp;
|
|
|
|
}
|
|
|
|
|
2014-07-24 08:49:05 -03:00
|
|
|
|
2011-10-16 03:58:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Accumulate the X.25 CRC by adding an array of bytes
|
|
|
|
*
|
|
|
|
* The checksum function adds the hash of one char at a time to the
|
|
|
|
* 16 bit checksum (uint16_t).
|
|
|
|
*
|
|
|
|
* @param data new bytes to hash
|
|
|
|
* @param crcAccum the already accumulated checksum
|
|
|
|
**/
|
2014-07-24 08:37:41 -03:00
|
|
|
static inline void crc_accumulate_buffer(uint16_t *crcAccum, const char *pBuffer, uint16_t length)
|
2011-10-16 03:58:14 -03:00
|
|
|
{
|
|
|
|
const uint8_t *p = (const uint8_t *)pBuffer;
|
|
|
|
while (length--) {
|
|
|
|
crc_accumulate(*p++, crcAccum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _CHECKSUM_H_ */
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|