From 7cded79b59a1c04c327e11d17e488e475a7ae09b Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Tue, 8 Dec 2015 10:35:54 -0800 Subject: [PATCH] Clean up CRC class/interface, make more like boost::crc_basic. --- firmware/application/app_ais.cpp | 19 +++---- firmware/application/app_ert.cpp | 15 ++--- firmware/common/crc.hpp | 95 ++++++++++++++++---------------- 3 files changed, 62 insertions(+), 67 deletions(-) diff --git a/firmware/application/app_ais.cpp b/firmware/application/app_ais.cpp index 3633b6512..a7711818e 100644 --- a/firmware/application/app_ais.cpp +++ b/firmware/application/app_ais.cpp @@ -132,23 +132,18 @@ struct PacketTooLong { struct CRCCheck { bool operator()(const ::Packet& packet) { + const size_t fcs_length = 16; const size_t data_and_fcs_length = packet.size() - 7; - const size_t data_length = data_and_fcs_length - 16; + const size_t data_length = data_and_fcs_length - fcs_length; CRCFieldReader field_crc { packet }; - CRC ais_fcs { 0x1021 }; + CRC ais_fcs { 0x1021, 0xffff, 0xffff }; - uint16_t crc_calculated = 0xffff; - - for(size_t i=0; i ert_bch { 0x6f63 }; size_t start_bit = 5; - auto crc_calculated = ert_bch.calculate_byte(0x0000, reader_.read(0, start_bit)); + ert_bch.process_byte(reader_.read(0, start_bit)); for(size_t i=start_bit; i ert_crc_ccitt { 0x1021 }; - uint16_t crc_calculated = 0xffff; + CRC ert_crc_ccitt { 0x1021, 0xffff, 0x1d0f }; for(size_t i=0; i #include +/* Inspired by + * http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code + * + * ...then munged into a shape resembling boost::crc_basic. + * http://www.boost.org/doc/libs/release/libs/crc/ + */ + template class CRC { public: constexpr CRC( - const T polynomial/*, - const T initial*/ - ) : polynomial { polynomial }/*, - initial { initial }*/ + const T truncated_polynomial, + const T initial_remainder = 0, + const T final_xor_value = 0 + ) : truncated_polynomial { truncated_polynomial }, + initial_remainder { initial_remainder }, + final_xor_value { final_xor_value }, + remainder { initial_remainder } { - // CRC LSB must always be 1 } -/* - template - T calculate(const U& bits, const size_t length) { - if( length > bits.size() ) { - // Exception. - return 0; - } - T crc = 0; - - for(size_t i=0; i