From e5fea5a37032f4e080911733658ab13fd6767d84 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 5 Feb 2009 10:05:15 +0000 Subject: [PATCH] Added a checksum function for the Oceanic VT Pro version string. --- src/checksum.c | 14 ++++++++++++++ src/checksum.h | 3 +++ src/oceanic_vtpro.c | 18 ++++++++++++++---- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/checksum.c b/src/checksum.c index 759294d..aa06edd 100644 --- a/src/checksum.c +++ b/src/checksum.c @@ -21,6 +21,20 @@ #include "checksum.h" + +unsigned char +checksum_add_uint4 (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] & 0xF0) >> 4; + crc += (data[i] & 0x0F); + } + + return crc; +} + + unsigned char checksum_add_uint8 (const unsigned char data[], unsigned int size, unsigned char init) { diff --git a/src/checksum.h b/src/checksum.h index 576fbe7..f078ef8 100644 --- a/src/checksum.h +++ b/src/checksum.h @@ -26,6 +26,9 @@ extern "C" { #endif /* __cplusplus */ +unsigned char +checksum_add_uint4 (const unsigned char data[], unsigned int size, unsigned char init); + unsigned char checksum_add_uint8 (const unsigned char data[], unsigned int size, unsigned char init); diff --git a/src/oceanic_vtpro.c b/src/oceanic_vtpro.c index 1a72b9e..2b5addd 100644 --- a/src/oceanic_vtpro.c +++ b/src/oceanic_vtpro.c @@ -396,8 +396,13 @@ oceanic_vtpro_device_version (device_t *abstract, unsigned char data[], unsigned if (rc != DEVICE_STATUS_SUCCESS) return rc; - // FIXME: The answer seems to contain a checksum byte, - // but the correct checksum function is not known. + // Verify the checksum of the answer. + unsigned char crc = ans[OCEANIC_VTPRO_PACKET_SIZE / 2]; + unsigned char ccrc = checksum_add_uint4 (ans, OCEANIC_VTPRO_PACKET_SIZE / 2, 0x00); + if (crc != ccrc) { + WARNING ("Unexpected answer CRC."); + return DEVICE_STATUS_PROTOCOL; + } #ifndef NDEBUG ans[OCEANIC_VTPRO_PACKET_SIZE / 2] = 0; @@ -414,8 +419,13 @@ oceanic_vtpro_device_version (device_t *abstract, unsigned char data[], unsigned if (rc != DEVICE_STATUS_SUCCESS) return rc; - // FIXME: The answer seems to contain a checksum byte, - // but the correct checksum function is not known. + // Verify the checksum of the answer. + unsigned char crc = answer[OCEANIC_VTPRO_PACKET_SIZE / 2]; + unsigned char ccrc = checksum_add_uint4 (answer, OCEANIC_VTPRO_PACKET_SIZE / 2, 0x00); + if (crc != ccrc) { + WARNING ("Unexpected answer CRC."); + return DEVICE_STATUS_PROTOCOL; + } // Verify the last byte of the answer. if (answer[OCEANIC_VTPRO_PACKET_SIZE / 2 + 1] != 0x51) {