00001
00002
00003
00004 #include "asctec_autopilot/crc16.h"
00005
00006 namespace asctec
00007 {
00008
00009 bool crc_valid (unsigned short packet_crc, void *data, unsigned short cnt)
00010 {
00011 unsigned short checksum = crc16 (data, cnt);
00012
00013 if (checksum == packet_crc)
00014 {
00015 return true;
00016 }
00017 return false;
00018 }
00019
00020 unsigned short crc_update (unsigned short crc, unsigned char data)
00021 {
00022 data ^= (crc & 0xff);
00023 data ^= data << 4;
00024
00025 return ((((unsigned short) data << 8) | ((crc >> 8) & 0xff)) ^ (unsigned char) (data >> 4)
00026 ^ ((unsigned short) data << 3));
00027 }
00028
00029 unsigned short crc16 (void *data, unsigned short cnt)
00030 {
00031 unsigned short crc = 0xff;
00032 unsigned char *ptr = (unsigned char *) data;
00033 int i;
00034
00035 for (i = 0; i < cnt; i++)
00036 {
00037 crc = crc_update (crc, *ptr);
00038 ptr++;
00039 }
00040 return crc;
00041 }
00042 }