From 7c03ddcb156f292ee1b301d0e850317e356cbb32 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Wed, 30 Jul 2008 08:56:08 +0000 Subject: [PATCH] Moved all the checksum functions to a common file. --- src/Makefile.am | 1 + src/checksum.c | 79 +++++++++++++++++++++++++++++++++++++++ src/checksum.h | 24 ++++++++++++ src/oceanic_atom2.c | 14 +------ src/reefnet_sensuspro.c | 51 ++----------------------- src/reefnet_sensusultra.c | 50 ++----------------------- src/suunto_d9.c | 18 ++------- src/suunto_eon.c | 14 +------ src/suunto_vyper.c | 22 +++-------- src/suunto_vyper2.c | 18 ++------- src/uwatec_aladin.c | 14 +------ src/uwatec_memomouse.c | 18 ++------- 12 files changed, 134 insertions(+), 189 deletions(-) create mode 100644 src/checksum.c create mode 100644 src/checksum.h diff --git a/src/Makefile.am b/src/Makefile.am index afb00fd..acbf533 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -46,6 +46,7 @@ libdivecomputer_la_SOURCES = \ oceanic.h \ oceanic_atom2.h oceanic_atom2.c \ ringbuffer.h ringbuffer.c \ + checksum.h checksum.c \ utils.h utils.c if OS_WIN32 diff --git a/src/checksum.c b/src/checksum.c new file mode 100644 index 0000000..c5a2c82 --- /dev/null +++ b/src/checksum.c @@ -0,0 +1,79 @@ +#include "checksum.h" + +unsigned char +checksum_add_uint8 (const unsigned char data[], unsigned int size, unsigned char init) +{ + unsigned char crc = init; + for (unsigned int i = 0; i < size; ++i) + crc += data[i]; + + return crc; +} + + +unsigned short +checksum_add_uint16 (const unsigned char data[], unsigned int size, unsigned short init) +{ + unsigned short crc = init; + for (unsigned int i = 0; i < size; ++i) + crc += data[i]; + + return crc; +} + + +unsigned char +checksum_xor_uint8 (const unsigned char data[], unsigned int size, unsigned char init) +{ + unsigned char crc = init; + for (unsigned int i = 0; i < size; ++i) + crc ^= data[i]; + + return crc; +} + + +unsigned short +checksum_crc_ccitt_uint16 (const unsigned char data[], unsigned int size) +{ + static const unsigned short crc_ccitt_table[] = { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 + }; + + unsigned short crc = 0xffff; + for (unsigned int i = 0; i < size; ++i) + crc = (crc << 8) ^ crc_ccitt_table[(crc >> 8) ^ data[i]]; + + return crc; +} diff --git a/src/checksum.h b/src/checksum.h new file mode 100644 index 0000000..dedc5f6 --- /dev/null +++ b/src/checksum.h @@ -0,0 +1,24 @@ +#ifndef CHECKSUM_H +#define CHECKSUM_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +unsigned char +checksum_add_uint8 (const unsigned char data[], unsigned int size, unsigned char init); + +unsigned short +checksum_add_uint16 (const unsigned char data[], unsigned int size, unsigned short init); + +unsigned char +checksum_xor_uint8 (const unsigned char data[], unsigned int size, unsigned char init); + +unsigned short +checksum_crc_ccitt_uint16 (const unsigned char data[], unsigned int size); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CHECKSUM_H */ diff --git a/src/oceanic_atom2.c b/src/oceanic_atom2.c index c8ac34c..d974e90 100644 --- a/src/oceanic_atom2.c +++ b/src/oceanic_atom2.c @@ -7,6 +7,7 @@ #include "serial.h" #include "utils.h" #include "ringbuffer.h" +#include "checksum.h" #define MAXRETRIES 2 @@ -59,17 +60,6 @@ device_is_oceanic_atom2 (device_t *abstract) } -static unsigned char -oceanic_atom2_checksum (const unsigned char data[], unsigned int size, unsigned char init) -{ - unsigned char crc = init; - for (unsigned int i = 0; i < size; ++i) - crc += data[i]; - - return crc; -} - - static device_status_t oceanic_atom2_send (oceanic_atom2_device_t *device, const unsigned char command[], unsigned int csize) { @@ -134,7 +124,7 @@ oceanic_atom2_transfer (oceanic_atom2_device_t *device, const unsigned char comm // Verify the checksum of the answer. unsigned char crc = answer[asize - 1]; - unsigned char ccrc = oceanic_atom2_checksum (answer, asize - 1, 0x00); + unsigned char ccrc = checksum_add_uint8 (answer, asize - 1, 0x00); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; diff --git a/src/reefnet_sensuspro.c b/src/reefnet_sensuspro.c index 1af93e7..a3086ce 100644 --- a/src/reefnet_sensuspro.c +++ b/src/reefnet_sensuspro.c @@ -4,6 +4,7 @@ #include "device-private.h" #include "reefnet_sensuspro.h" #include "serial.h" +#include "checksum.h" #include "utils.h" #define WARNING(expr) \ @@ -110,52 +111,6 @@ reefnet_sensuspro_device_close (device_t *abstract) } -static unsigned short -reefnet_sensuspro_checksum (unsigned char *data, unsigned int size) -{ - static unsigned short crc_ccitt_table[] = { - 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, - 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, - 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, - 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, - 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, - 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, - 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, - 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, - 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, - 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, - 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, - 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, - 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, - 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, - 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, - 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, - 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, - 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, - 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, - 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, - 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, - 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, - 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, - 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, - 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, - 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, - 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, - 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, - 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, - 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, - 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, - 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 - }; - - unsigned short crc = 0xffff; - for (unsigned int i = 0; i < size; ++i) - crc = (crc << 8) ^ crc_ccitt_table[(crc >> 8) ^ data[i]]; - - return crc; -} - - static device_status_t reefnet_sensuspro_device_handshake (device_t *abstract, unsigned char *data, unsigned int size) { @@ -182,7 +137,7 @@ reefnet_sensuspro_device_handshake (device_t *abstract, unsigned char *data, uns unsigned short crc = handshake[REEFNET_SENSUSPRO_HANDSHAKE_SIZE + 0] + (handshake[REEFNET_SENSUSPRO_HANDSHAKE_SIZE + 1] << 8); - unsigned short ccrc = reefnet_sensuspro_checksum (handshake, REEFNET_SENSUSPRO_HANDSHAKE_SIZE); + unsigned short ccrc = checksum_crc_ccitt_uint16 (handshake, REEFNET_SENSUSPRO_HANDSHAKE_SIZE); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; @@ -249,7 +204,7 @@ reefnet_sensuspro_device_dump (device_t *abstract, unsigned char *data, unsigned unsigned short crc = answer[REEFNET_SENSUSPRO_MEMORY_SIZE + 0] + (answer[REEFNET_SENSUSPRO_MEMORY_SIZE + 1] << 8); - unsigned short ccrc = reefnet_sensuspro_checksum (answer, REEFNET_SENSUSPRO_MEMORY_SIZE); + unsigned short ccrc = checksum_crc_ccitt_uint16 (answer, REEFNET_SENSUSPRO_MEMORY_SIZE); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; diff --git a/src/reefnet_sensusultra.c b/src/reefnet_sensusultra.c index 541a5b1..a887a73 100644 --- a/src/reefnet_sensusultra.c +++ b/src/reefnet_sensusultra.c @@ -5,6 +5,7 @@ #include "device-private.h" #include "reefnet_sensusultra.h" #include "serial.h" +#include "checksum.h" #include "utils.h" #define WARNING(expr) \ @@ -129,51 +130,6 @@ reefnet_sensusultra_device_set_maxretries (device_t *abstract, unsigned int maxr return DEVICE_STATUS_SUCCESS; } -static unsigned short -reefnet_sensusultra_checksum (const unsigned char *data, unsigned int size) -{ - static unsigned short crc_ccitt_table[] = { - 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, - 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, - 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, - 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, - 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, - 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, - 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, - 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, - 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, - 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, - 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, - 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, - 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, - 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, - 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, - 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, - 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, - 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, - 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, - 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, - 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, - 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, - 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, - 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, - 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, - 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, - 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, - 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, - 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, - 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, - 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, - 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 - }; - - unsigned short crc = 0xffff; - for (unsigned int i = 0; i < size; ++i) - crc = (crc << 8) ^ crc_ccitt_table[(crc >> 8) ^ data[i]]; - - return crc; -} - static int reefnet_sensusultra_isempty (const unsigned char *data, unsigned int size) @@ -248,7 +204,7 @@ reefnet_sensusultra_packet (reefnet_sensusultra_device_t *device, unsigned char // Verify the checksum of the packet. unsigned short crc = data[size - 2] + (data[size - 1] << 8); - unsigned short ccrc = reefnet_sensusultra_checksum (data + header, size - header - 2); + unsigned short ccrc = checksum_crc_ccitt_uint16 (data + header, size - header - 2); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; @@ -468,7 +424,7 @@ reefnet_sensusultra_device_write_user (device_t *abstract, const unsigned char * } // Send the checksum to the device. - unsigned short crc = reefnet_sensusultra_checksum (data, REEFNET_SENSUSULTRA_MEMORY_USER_SIZE); + unsigned short crc = checksum_crc_ccitt_uint16 (data, REEFNET_SENSUSULTRA_MEMORY_USER_SIZE); rc = reefnet_sensusultra_send_ushort (device, crc); if (rc != DEVICE_STATUS_SUCCESS) return rc; diff --git a/src/suunto_d9.c b/src/suunto_d9.c index 3f039a2..f374823 100644 --- a/src/suunto_d9.c +++ b/src/suunto_d9.c @@ -7,6 +7,7 @@ #include "serial.h" #include "utils.h" #include "ringbuffer.h" +#include "checksum.h" #define MAXRETRIES 2 @@ -127,17 +128,6 @@ suunto_d9_device_close (device_t *abstract) } -static unsigned char -suunto_d9_checksum (const unsigned char data[], unsigned int size, unsigned char init) -{ - unsigned char crc = init; - for (unsigned int i = 0; i < size; ++i) - crc ^= data[i]; - - return crc; -} - - static device_status_t suunto_d9_send (suunto_d9_device_t *device, const unsigned char command[], unsigned int csize) { @@ -212,7 +202,7 @@ suunto_d9_transfer (suunto_d9_device_t *device, const unsigned char command[], u // Verify the checksum of the package. unsigned char crc = answer[asize - 1]; - unsigned char ccrc = suunto_d9_checksum (answer, asize - 1, 0x00); + unsigned char ccrc = checksum_xor_uint8 (answer, asize - 1, 0x00); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; @@ -295,7 +285,7 @@ suunto_d9_device_read (device_t *abstract, unsigned int address, unsigned char d (address ) & 0xFF, // low len, // count 0}; // CRC - command[6] = suunto_d9_checksum (command, 6, 0x00); + command[6] = checksum_xor_uint8 (command, 6, 0x00); int rc = suunto_d9_transfer (device, command, sizeof (command), answer, len + 7, len); if (rc != DEVICE_STATUS_SUCCESS) return rc; @@ -343,7 +333,7 @@ suunto_d9_device_write (device_t *abstract, unsigned int address, const unsigned len, // count 0}; // data + CRC memcpy (command + 6, data, len); - command[len + 6] = suunto_d9_checksum (command, len + 6, 0x00); + command[len + 6] = checksum_xor_uint8 (command, len + 6, 0x00); int rc = suunto_d9_transfer (device, command, len + 7, answer, sizeof (answer), 0); if (rc != DEVICE_STATUS_SUCCESS) return rc; diff --git a/src/suunto_eon.c b/src/suunto_eon.c index 0d2ce46..f9c166b 100644 --- a/src/suunto_eon.c +++ b/src/suunto_eon.c @@ -6,6 +6,7 @@ #include "suunto_eon.h" #include "suunto_common.h" #include "serial.h" +#include "checksum.h" #include "utils.h" #define WARNING(expr) \ @@ -116,17 +117,6 @@ suunto_eon_device_close (device_t *abstract) } -static unsigned char -suunto_eon_checksum (unsigned char data[], unsigned int size) -{ - unsigned char crc = 0x00; - for (unsigned int i = 0; i < size; ++i) - crc += data[i]; - - return crc; -} - - static device_status_t suunto_eon_device_dump (device_t *abstract, unsigned char data[], unsigned int size) { @@ -153,7 +143,7 @@ suunto_eon_device_dump (device_t *abstract, unsigned char data[], unsigned int s // Verify the checksum of the package. unsigned char crc = answer[sizeof (answer) - 1]; - unsigned char ccrc = suunto_eon_checksum (answer, sizeof (answer) - 1); + unsigned char ccrc = checksum_add_uint8 (answer, sizeof (answer) - 1, 0x00); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; diff --git a/src/suunto_vyper.c b/src/suunto_vyper.c index 0a9606e..e54b2fe 100644 --- a/src/suunto_vyper.c +++ b/src/suunto_vyper.c @@ -6,6 +6,7 @@ #include "suunto_vyper.h" #include "suunto_common.h" #include "serial.h" +#include "checksum.h" #include "utils.h" #define MIN(a,b) (((a) < (b)) ? (a) : (b)) @@ -128,17 +129,6 @@ suunto_vyper_device_close (device_t *abstract) } -static unsigned char -suunto_vyper_checksum (const unsigned char data[], unsigned int size, unsigned char init) -{ - unsigned char crc = init; - for (unsigned int i = 0; i < size; ++i) - crc ^= data[i]; - - return crc; -} - - static void suunto_vyper_reverse (unsigned char data[], unsigned int size) { @@ -301,7 +291,7 @@ suunto_vyper_transfer (suunto_vyper_device_t *device, const unsigned char comman // Verify the checksum of the package. unsigned char crc = answer[asize - 1]; - unsigned char ccrc = suunto_vyper_checksum (answer, asize - 1, 0x00); + unsigned char ccrc = checksum_xor_uint8 (answer, asize - 1, 0x00); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; @@ -334,7 +324,7 @@ suunto_vyper_device_read (device_t *abstract, unsigned int address, unsigned cha (address ) & 0xFF, // low len, // count 0}; // CRC - command[4] = suunto_vyper_checksum (command, 4, 0x00); + command[4] = checksum_xor_uint8 (command, 4, 0x00); int rc = suunto_vyper_transfer (device, command, sizeof (command), answer, len + 5, len); if (rc != DEVICE_STATUS_SUCCESS) return rc; @@ -393,7 +383,7 @@ suunto_vyper_device_write (device_t *abstract, unsigned int address, const unsig len, // count 0}; // data + CRC memcpy (wcommand + 4, data, len); - wcommand[len + 4] = suunto_vyper_checksum (wcommand, len + 4, 0x00); + wcommand[len + 4] = checksum_xor_uint8 (wcommand, len + 4, 0x00); rc = suunto_vyper_transfer (device, wcommand, len + 5, wanswer, sizeof (wanswer), 0); if (rc != DEVICE_STATUS_SUCCESS) return rc; @@ -425,7 +415,7 @@ suunto_vyper_device_read_dive (device_t *abstract, unsigned char data[], unsigne // Send the command to the dive computer. unsigned char command[3] = {init ? 0x08 : 0x09, 0xA5, 0x00}; - command[2] = suunto_vyper_checksum (command, 2, 0x00); + command[2] = checksum_xor_uint8 (command, 2, 0x00); int rc = suunto_vyper_send (device, command, 3); if (rc != DEVICE_STATUS_SUCCESS) { WARNING ("Failed to send the command."); @@ -476,7 +466,7 @@ suunto_vyper_device_read_dive (device_t *abstract, unsigned char data[], unsigne // Verify the checksum of the package. unsigned char crc = answer[len + 2]; - unsigned char ccrc = suunto_vyper_checksum (answer, len + 2, 0x00); + unsigned char ccrc = checksum_xor_uint8 (answer, len + 2, 0x00); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; diff --git a/src/suunto_vyper2.c b/src/suunto_vyper2.c index 6950790..a3e38af 100644 --- a/src/suunto_vyper2.c +++ b/src/suunto_vyper2.c @@ -7,6 +7,7 @@ #include "serial.h" #include "utils.h" #include "ringbuffer.h" +#include "checksum.h" #define MAXRETRIES 2 @@ -127,17 +128,6 @@ suunto_vyper2_device_close (device_t *abstract) } -static unsigned char -suunto_vyper2_checksum (const unsigned char data[], unsigned int size, unsigned char init) -{ - unsigned char crc = init; - for (unsigned int i = 0; i < size; ++i) - crc ^= data[i]; - - return crc; -} - - static device_status_t suunto_vyper2_send (suunto_vyper2_device_t *device, const unsigned char command[], unsigned int csize) { @@ -199,7 +189,7 @@ suunto_vyper2_transfer (suunto_vyper2_device_t *device, const unsigned char comm // Verify the checksum of the package. unsigned char crc = answer[asize - 1]; - unsigned char ccrc = suunto_vyper2_checksum (answer, asize - 1, 0x00); + unsigned char ccrc = checksum_xor_uint8 (answer, asize - 1, 0x00); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; @@ -282,7 +272,7 @@ suunto_vyper2_device_read (device_t *abstract, unsigned int address, unsigned ch (address ) & 0xFF, // low len, // count 0}; // CRC - command[6] = suunto_vyper2_checksum (command, 6, 0x00); + command[6] = checksum_xor_uint8 (command, 6, 0x00); int rc = suunto_vyper2_transfer (device, command, sizeof (command), answer, len + 7, len); if (rc != DEVICE_STATUS_SUCCESS) return rc; @@ -330,7 +320,7 @@ suunto_vyper2_device_write (device_t *abstract, unsigned int address, const unsi len, // count 0}; // data + CRC memcpy (command + 6, data, len); - command[len + 6] = suunto_vyper2_checksum (command, len + 6, 0x00); + command[len + 6] = checksum_xor_uint8 (command, len + 6, 0x00); int rc = suunto_vyper2_transfer (device, command, len + 7, answer, sizeof (answer), 0); if (rc != DEVICE_STATUS_SUCCESS) return rc; diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index 7e74c01..6dd0fa2 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -6,6 +6,7 @@ #include "serial.h" #include "utils.h" #include "ringbuffer.h" +#include "checksum.h" #define WARNING(expr) \ { \ @@ -148,17 +149,6 @@ uwatec_aladin_reverse_bytes (unsigned char data[], unsigned int size) } -static unsigned short -uwatec_aladin_checksum (unsigned char data[], unsigned int size) -{ - unsigned short crc = 0x00; - for (unsigned int i = 0; i < size; ++i) - crc += data[i]; - - return crc; -} - - static device_status_t uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned int size) { @@ -197,7 +187,7 @@ uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned in unsigned short crc = answer[UWATEC_ALADIN_MEMORY_SIZE + 0] + (answer[UWATEC_ALADIN_MEMORY_SIZE + 1] << 8); - unsigned short ccrc = uwatec_aladin_checksum (answer, UWATEC_ALADIN_MEMORY_SIZE); + unsigned short ccrc = checksum_add_uint16 (answer, UWATEC_ALADIN_MEMORY_SIZE, 0x0000); if (ccrc != crc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; diff --git a/src/uwatec_memomouse.c b/src/uwatec_memomouse.c index 67d1bcc..f24b20e 100644 --- a/src/uwatec_memomouse.c +++ b/src/uwatec_memomouse.c @@ -5,6 +5,7 @@ #include "device-private.h" #include "uwatec_memomouse.h" #include "serial.h" +#include "checksum.h" #include "utils.h" #define WARNING(expr) \ @@ -157,17 +158,6 @@ uwatec_memomouse_reverse (unsigned char data[], unsigned int size) } -static unsigned char -uwatec_memomouse_checksum (unsigned char data[], unsigned int size, unsigned char init) -{ - unsigned char crc = init; - for (unsigned int i = 0; i < size; ++i) - crc ^= data[i]; - - return crc; -} - - static device_status_t uwatec_memomouse_confirm (uwatec_memomouse_device_t *device, unsigned char value) { @@ -218,7 +208,7 @@ uwatec_memomouse_read_packet (uwatec_memomouse_device_t *device, unsigned char d // Verify the checksum of the package. unsigned char crc = data[len + 1]; - unsigned char ccrc = uwatec_memomouse_checksum (data, len + 1, 0x00); + unsigned char ccrc = checksum_xor_uint8 (data, len + 1, 0x00); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; @@ -323,7 +313,7 @@ uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, unsigned // Verify the checksum. unsigned char crc = buffer[total - 1]; - unsigned char ccrc = uwatec_memomouse_checksum (buffer, total - 1, 0x00); + unsigned char ccrc = checksum_xor_uint8 (buffer, total - 1, 0x00); if (crc != ccrc) { free (buffer); return DEVICE_STATUS_PROTOCOL; @@ -371,7 +361,7 @@ uwatec_memomouse_dump (uwatec_memomouse_device_t *device, unsigned char *data[], (device->timestamp >> 16) & 0xFF, (device->timestamp >> 24) & 0xFF, 0x00}; // Outer packet checksum. - command[8] = uwatec_memomouse_checksum (command, 8, 0x00); + command[8] = checksum_xor_uint8 (command, 8, 0x00); uwatec_memomouse_reverse (command, sizeof (command)); // Wait a small amount of time before sending the command.