From f5cf1a850ca179948b6f12fb08e0e8747fe45c4a Mon Sep 17 00:00:00 2001 From: drowe67 Date: Fri, 1 Jan 2016 02:19:07 +0000 Subject: [PATCH] added ccitt crc16 check git-svn-id: https://svn.code.sf.net/p/freetel/code@2599 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/src/horus_l2.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/codec2-dev/src/horus_l2.c b/codec2-dev/src/horus_l2.c index edaf7bf5..895a5f23 100644 --- a/codec2-dev/src/horus_l2.c +++ b/codec2-dev/src/horus_l2.c @@ -79,6 +79,7 @@ static char uw[] = {'$','$'}; int32_t get_syndrome(int32_t pattern); void golay23_init(void); int golay23_decode(int received_codeword); +unsigned short gen_crc16(unsigned char* data_p, unsigned char length); /* We are using a Golay (23,12) code which has a codeword 23 bits @@ -614,10 +615,19 @@ int main(void) { assert(sizeof(h) == nbytes); memcpy(&h, output_payload, nbytes); - fprintf(stderr, "%d,%d,%02d:%02d:%02d,%f,%f,%d,%d,%d,%d,%d,%04x\n", + uint16_t crc_rx = gen_crc16(output_payload, nbytes-2); + char crc_str[80]; + + if (crc_rx == h.Checksum) { + sprintf(crc_str, "CRC OK"); + } else { + sprintf(crc_str, "CRC BAD"); + } + + fprintf(stderr, "%d,%d,%02d:%02d:%02d,%f,%f,%d,%d,%d,%d,%d,%04x %s\n", h.PayloadID, h.Counter, h.Hours, h.Minutes, h.Seconds, h.Latitude, h.Longitude, h.Altitude, h.Speed, h.Sats, h.Temp, - h.BattVoltage, h.Checksum); + h.BattVoltage, h.Checksum, crc_str); /* Hex ASCII file output */ @@ -917,5 +927,21 @@ int golay23_count_errors(int recd_codeword, int corrected_codeword) return errors; } + + +// from http://stackoverflow.com/questions/10564491/function-to-calculate-a-crc16-checksum + +unsigned short gen_crc16(unsigned char* data_p, unsigned char length){ + unsigned char x; + unsigned short crc = 0xFFFF; + + while (length--){ + x = crc >> 8 ^ *data_p++; + x ^= x>>4; + crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x); + } + return crc; +} + #endif -- 2.25.1