crc16.cpp
Go to the documentation of this file.
00001 // This code came from the AutoPilot Manual
00002 // it should be replaced with the boost version if possible
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 }


asctec_autopilot
Author(s): William Morris, Ivan Dryanovski, Steven Bellens, Patrick Bouffard et al.
autogenerated on Tue Jan 7 2014 11:04:25