From f28886e970e13fd20c65d852c4729473a71c4c99 Mon Sep 17 00:00:00 2001 From: Ulrich Mohr Date: Mon, 6 Apr 2020 13:22:42 +0200 Subject: [PATCH] Moved crc calculation into its own class, renamed function to show which crc is calculated. --- container/IndexedRingMemoryArray.h | 2 +- datalinklayer/DataLinkLayer.cpp | 4 ++-- datalinklayer/TcTransferFrameLocal.cpp | 6 +++--- devicehandlers/DeviceHandlerBase.cpp | 2 +- globalfunctions/{crc_ccitt.cpp => CRC.cpp} | 8 ++++---- globalfunctions/CRC.h | 16 ++++++++++++++++ globalfunctions/crc_ccitt.h | 9 --------- memory/MemoryHelper.cpp | 4 ++-- tcdistribution/TcPacketCheck.cpp | 4 ++-- tmtcpacket/pus/TcPacketBase.cpp | 4 ++-- tmtcpacket/pus/TmPacketBase.cpp | 4 ++-- 11 files changed, 35 insertions(+), 28 deletions(-) rename globalfunctions/{crc_ccitt.cpp => CRC.cpp} (94%) create mode 100644 globalfunctions/CRC.h delete mode 100644 globalfunctions/crc_ccitt.h diff --git a/container/IndexedRingMemoryArray.h b/container/IndexedRingMemoryArray.h index eacfe3e5..992b7489 100644 --- a/container/IndexedRingMemoryArray.h +++ b/container/IndexedRingMemoryArray.h @@ -2,10 +2,10 @@ #define FRAMEWORK_CONTAINER_INDEXEDRINGMEMORY_H_ #include +#include #include #include #include -#include #include template diff --git a/datalinklayer/DataLinkLayer.cpp b/datalinklayer/DataLinkLayer.cpp index 1ee546c1..70999403 100644 --- a/datalinklayer/DataLinkLayer.cpp +++ b/datalinklayer/DataLinkLayer.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include DataLinkLayer::DataLinkLayer(uint8_t* set_frame_buffer, ClcwIF* setClcw, @@ -60,7 +60,7 @@ ReturnValue_t DataLinkLayer::frameValidationCheck() { } ReturnValue_t DataLinkLayer::frameCheckCRC() { - uint16_t checkValue = ::Calculate_CRC(this->currentFrame.getFullFrame(), + uint16_t checkValue = CRC::crc16ccitt(this->currentFrame.getFullFrame(), this->currentFrame.getFullSize()); if (checkValue == 0) { return RETURN_OK; diff --git a/datalinklayer/TcTransferFrameLocal.cpp b/datalinklayer/TcTransferFrameLocal.cpp index a53c5b28..7362a6ae 100644 --- a/datalinklayer/TcTransferFrameLocal.cpp +++ b/datalinklayer/TcTransferFrameLocal.cpp @@ -6,7 +6,7 @@ */ #include -#include +#include #include #include @@ -25,7 +25,7 @@ TcTransferFrameLocal::TcTransferFrameLocal(bool bypass, bool controlCommand, uin uint16_t totalSize = sizeof(TcTransferFramePrimaryHeader) + dataSize + FRAME_CRC_SIZE -1; frame->header.vcidAndLength_h |= (totalSize & 0x0300) >> 8; frame->header.length_l = (totalSize & 0x00FF); - uint16_t crc = ::Calculate_CRC(getFullFrame(), getFullSize() -2); + uint16_t crc = CRC::crc16ccitt(getFullFrame(), getFullSize() -2); this->getFullFrame()[getFullSize()-2] = (crc & 0xFF00) >> 8; this->getFullFrame()[getFullSize()-1] = (crc & 0x00FF); } else if (dataSize <= 1016) { @@ -33,7 +33,7 @@ TcTransferFrameLocal::TcTransferFrameLocal(bool bypass, bool controlCommand, uin uint16_t dataCrcSize = sizeof(TcTransferFramePrimaryHeader) + 1 + dataSize + FRAME_CRC_SIZE -1; frame->header.vcidAndLength_h |= (dataCrcSize & 0x0300) >> 8; frame->header.length_l = (dataCrcSize & 0x00FF); - uint16_t crc = ::Calculate_CRC(getFullFrame(), getFullSize() -2); + uint16_t crc = CRC::crc16ccitt(getFullFrame(), getFullSize() -2); this->getFullFrame()[getFullSize()-2] = (crc & 0xFF00) >> 8; this->getFullFrame()[getFullSize()-1] = (crc & 0x00FF); } else { diff --git a/devicehandlers/DeviceHandlerBase.cpp b/devicehandlers/DeviceHandlerBase.cpp index ae9199f2..22d49d37 100644 --- a/devicehandlers/DeviceHandlerBase.cpp +++ b/devicehandlers/DeviceHandlerBase.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/globalfunctions/crc_ccitt.cpp b/globalfunctions/CRC.cpp similarity index 94% rename from globalfunctions/crc_ccitt.cpp rename to globalfunctions/CRC.cpp index 74a93465..ecd0b675 100644 --- a/globalfunctions/crc_ccitt.cpp +++ b/globalfunctions/CRC.cpp @@ -1,7 +1,7 @@ -#include +#include #include -static const uint16_t crc_table[256] = { +const uint16_t CRC::crc16ccitt_table[256] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, @@ -38,14 +38,14 @@ static const uint16_t crc_table[256] = { // CRC implementation -uint16_t Calculate_CRC(uint8_t const input[], uint32_t length, uint16_t startingCrc) +uint16_t CRC::crc16ccitt(uint8_t const input[], uint32_t length, uint16_t startingCrc) { uint8_t *data = (uint8_t *)input; unsigned int tbl_idx; while (length--) { tbl_idx = ((startingCrc >> 8) ^ *data) & 0xff; - startingCrc = (crc_table[tbl_idx] ^ (startingCrc << 8)) & 0xffff; + startingCrc = (crc16ccitt_table[tbl_idx] ^ (startingCrc << 8)) & 0xffff; data++; } diff --git a/globalfunctions/CRC.h b/globalfunctions/CRC.h new file mode 100644 index 00000000..4200f94f --- /dev/null +++ b/globalfunctions/CRC.h @@ -0,0 +1,16 @@ +#ifndef CRC_CCITT_H_ +#define CRC_CCITT_H_ + +#include + +class CRC { +public: + static uint16_t crc16ccitt(uint8_t const input[], uint32_t length, + uint16_t startingCrc = 0xffff); +private: + CRC(); + + static const uint16_t crc16ccitt_table[256]; +}; + +#endif /* CRC_H_ */ diff --git a/globalfunctions/crc_ccitt.h b/globalfunctions/crc_ccitt.h deleted file mode 100644 index 60077d0b..00000000 --- a/globalfunctions/crc_ccitt.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef CRC_CCITT_H_ -#define CRC_CCITT_H_ - -#include - -uint16_t Calculate_CRC(uint8_t const input[], uint32_t length, uint16_t startingCrc = 0xffff); - - -#endif /* CRC_H_ */ diff --git a/memory/MemoryHelper.cpp b/memory/MemoryHelper.cpp index 4905875e..69830084 100644 --- a/memory/MemoryHelper.cpp +++ b/memory/MemoryHelper.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -98,7 +98,7 @@ void MemoryHelper::completeDump(ReturnValue_t errorCode, break; } case MemoryMessage::CMD_MEMORY_CHECK: { - uint16_t crc = ::Calculate_CRC(reservedSpaceInIPC, size); + uint16_t crc = CRC::crc16ccitt(reservedSpaceInIPC, size); //Delete data immediately, was temporary. ipcStore->deleteData(ipcAddress); MemoryMessage::setMemoryCheckReply(&reply, crc); diff --git a/tcdistribution/TcPacketCheck.cpp b/tcdistribution/TcPacketCheck.cpp index b942e6b6..90a1167f 100644 --- a/tcdistribution/TcPacketCheck.cpp +++ b/tcdistribution/TcPacketCheck.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -8,7 +8,7 @@ TcPacketCheck::TcPacketCheck( uint16_t set_apid ) : apid(set_apid) { } ReturnValue_t TcPacketCheck::checkPacket( TcPacketStored* current_packet ) { - uint16_t calculated_crc = ::Calculate_CRC ( current_packet->getWholeData(), current_packet->getFullSize() ); + uint16_t calculated_crc = CRC::crc16ccitt( current_packet->getWholeData(), current_packet->getFullSize() ); if ( calculated_crc != 0 ) { return INCORRECT_CHECKSUM; } diff --git a/tmtcpacket/pus/TcPacketBase.cpp b/tmtcpacket/pus/TcPacketBase.cpp index 630394f8..c81e06b7 100644 --- a/tmtcpacket/pus/TcPacketBase.cpp +++ b/tmtcpacket/pus/TcPacketBase.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -40,7 +40,7 @@ uint16_t TcPacketBase::getErrorControl() { void TcPacketBase::setErrorControl() { uint32_t full_size = getFullSize(); - uint16_t crc = ::Calculate_CRC(getWholeData(), full_size - CRC_SIZE); + uint16_t crc = CRC::crc16ccitt(getWholeData(), full_size - CRC_SIZE); uint32_t size = getApplicationDataSize(); (&tcData->data)[size] = (crc & 0XFF00) >> 8; // CRCH (&tcData->data)[size + 1] = (crc) & 0X00FF; // CRCL diff --git a/tmtcpacket/pus/TmPacketBase.cpp b/tmtcpacket/pus/TmPacketBase.cpp index 1599f79e..74bc9b8a 100644 --- a/tmtcpacket/pus/TmPacketBase.cpp +++ b/tmtcpacket/pus/TmPacketBase.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -39,7 +39,7 @@ uint16_t TmPacketBase::getErrorControl() { void TmPacketBase::setErrorControl() { uint32_t full_size = getFullSize(); - uint16_t crc = ::Calculate_CRC(getWholeData(), full_size - CRC_SIZE); + uint16_t crc = CRC::crc16ccitt(getWholeData(), full_size - CRC_SIZE); uint32_t size = getSourceDataSize(); getSourceData()[size] = (crc & 0XFF00) >> 8; // CRCH getSourceData()[size + 1] = (crc) & 0X00FF; // CRCL