Skip to content

Commit

Permalink
Fix #2174, Move CRC types and convert to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
thnkslprpt committed Nov 2, 2022
1 parent 7a220ae commit 90437e0
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 73 deletions.
22 changes: 0 additions & 22 deletions cmake/sample_defs/sample_mission_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,28 +258,6 @@
*/
#define CFE_MISSION_EVS_MAX_MESSAGE_LENGTH 122

/** \name Checksum/CRC algorithm identifiers */
/** \{ */
#define CFE_MISSION_ES_CRC_8 1 /**< \brief CRC ( 8 bit additive - returns 32 bit total) (Currently not implemented) */
#define CFE_MISSION_ES_CRC_16 2 /**< \brief CRC (16 bit additive - returns 32 bit total) */
#define CFE_MISSION_ES_CRC_32 \
3 /**< \brief CRC (32 bit additive - returns 32 bit total) (Currently not implemented) \
*/
/** \} */

/**
** \cfeescfg Mission Default CRC algorithm
**
** \par Description:
** Indicates the which CRC algorithm should be used as the default
** for verifying the contents of Critical Data Stores and when calculating
** Table Image data integrity values.
**
** \par Limits
** Currently only CFE_MISSION_ES_CRC_16 is supported (see #CFE_MISSION_ES_CRC_16)
*/
#define CFE_MISSION_ES_DEFAULT_CRC CFE_MISSION_ES_CRC_16

/**
** \cfetblcfg Maximum Table Name Length
**
Expand Down
11 changes: 5 additions & 6 deletions docs/cFE Application Developers Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1303,14 +1303,13 @@ TypeCRC identifies which of the standard CRC polynomials to be used. Currently,
there are the following types available:
```
CFE_MISSION_ES_CRC_8 – an 8-bit additive checksum calculation that returns a 32-bit value
CFE_MISSION_ES_CRC_16 – a 16-bit additive checksum calculation that returns a 32-bit value
CFE_MISSION_ES_CRC_32 – a 32-bit additive checksum calculation that returns a 32-bit value
CFE_MISSION_ES_DEFAULT_CRC – the mission specified default CRC calculation
CFE_ES_CRC_8 – an 8-bit additive checksum calculation that returns a 32-bit value
CFE_ES_CRC_16 – a 16-bit additive checksum calculation that returns a 32-bit value
CFE_ES_CRC_32 – a 32-bit additive checksum calculation that returns a 32-bit value
```
Unless there is a specific interface with a specified CRC calculation,
Applications must use the CFE_MISSION_ES_DEFAULT_CRC type.
Currently only CFE_ES_CRC_16 is supported. CFE_ES_CRC_8 and CFE_ES_CRC_32 are yet
to be implemented.
## 5.11 File System Functions
Expand Down
23 changes: 14 additions & 9 deletions modules/cfe_testcase/src/es_misc_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,37 @@
*/

#include "cfe_test.h"
#include "cfe_es_api_typedefs.h"

void TestCalculateCRC(void)
{
const char *Data = "Random Stuff";
uint32 inputCrc = 345353;
uint32 Result;
const char * Data = "Random Stuff";
uint32 inputCrc = 345353;
uint32 Result;
CFE_ES_CrcType_t TypeCrc = CFE_ES_CRC_16;

UtPrintf("Testing: CFE_ES_CalculateCRC");

/* CRC is implementation specific, functional just checks that a result is produced and reports */
UtAssert_VOIDCALL(Result = CFE_ES_CalculateCRC(Data, sizeof(Data), 0, CFE_MISSION_ES_DEFAULT_CRC));
UtAssert_VOIDCALL(Result = CFE_ES_CalculateCRC(Data, sizeof(Data), 0, TypeCrc));
UtAssert_MIR("Confirm mission default CRC of \"%s\" is %lu", Data, (unsigned long)Result);

UtAssert_VOIDCALL(Result = CFE_ES_CalculateCRC(Data, sizeof(Data), inputCrc, CFE_MISSION_ES_CRC_16));
UtAssert_VOIDCALL(Result = CFE_ES_CalculateCRC(Data, sizeof(Data), inputCrc, TypeCrc));
UtAssert_MIR("Confirm CRC16 of \"%s\" with input CRC of %lu is %lu", Data, (unsigned long)inputCrc,
(unsigned long)Result);

UtAssert_VOIDCALL(Result = CFE_ES_CalculateCRC(Data, sizeof(Data), 0, CFE_MISSION_ES_CRC_8));
TypeCrc = CFE_ES_CRC_8;
UtAssert_VOIDCALL(Result = CFE_ES_CalculateCRC(Data, sizeof(Data), 0, TypeCrc));
UtAssert_MIR("Confirm CRC8 of \"%s\" is %lu", Data, (unsigned long)Result);

UtAssert_VOIDCALL(Result = CFE_ES_CalculateCRC(Data, sizeof(Data), 0, CFE_MISSION_ES_CRC_32));
TypeCrc = CFE_ES_CRC_32;
UtAssert_VOIDCALL(Result = CFE_ES_CalculateCRC(Data, sizeof(Data), 0, TypeCrc));
UtAssert_MIR("Confirm CRC32 of \"%s\" is %lu", Data, (unsigned long)Result);

/* NULL input or 0 size returns input crc */
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(NULL, sizeof(Data), inputCrc, CFE_MISSION_ES_CRC_16), inputCrc);
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(Data, 0, inputCrc, CFE_MISSION_ES_CRC_16), inputCrc);
TypeCrc = CFE_ES_CRC_16;
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(NULL, sizeof(Data), inputCrc, TypeCrc), inputCrc);
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(Data, 0, inputCrc, TypeCrc), inputCrc);
}

void TestWriteToSysLog(void)
Expand Down
6 changes: 3 additions & 3 deletions modules/core_api/fsw/inc/cfe_es.h
Original file line number Diff line number Diff line change
Expand Up @@ -999,13 +999,13 @@ CFE_Status_t CFE_ES_WriteToSysLog(const char *SpecStringPtr, ...) OS_PRINTF(1, 2
** a single value. Nominally, the user should set this value to zero.
**
** \param[in] TypeCRC One of the following CRC algorithm selections:
** \arg \c CFE_MISSION_ES_CRC_8 - (Not currently implemented)
** \arg \c CFE_MISSION_ES_CRC_16 - CRC-16/ARC <BR>
** \arg \c CFE_ES_CRC_8 - (Not currently implemented)
** \arg \c CFE_ES_CRC_16 - CRC-16/ARC <BR>
** Polynomial: 0x8005 <BR>
** Initialization: 0x0000 <BR>
** Reflect Input/Output: true <BR>
** XorOut: 0x0000
** \arg \c CFE_MISSION_ES_CRC_32 - (not currently implemented)
** \arg \c CFE_ES_CRC_32 - (not currently implemented)
**
** \return The result of the CRC calculation on the specified memory block.
** If the TypeCRC is unimplemented will return 0.
Expand Down
12 changes: 12 additions & 0 deletions modules/core_api/fsw/inc/cfe_es_api_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ typedef CFE_ES_TaskEntryFuncPtr_t CFE_ES_ChildTaskMainFuncPtr_t;
*/
typedef void *CFE_ES_StackPointer_t; /* aka osal_stackptr_t in proposed OSAL change */

/**
* \brief Checksum/CRC algorithm identifiers
*
* Currently only CFE_ES_CRC_16 is supported.
*/
typedef enum CFE_ES_CrcType
{
CFE_ES_CRC_8, /**< \brief CRC ( 8 bit additive - returns 32 bit total) (Currently not implemented) */
CFE_ES_CRC_16, /**< \brief CRC (16 bit additive - returns 32 bit total) */
CFE_ES_CRC_32 /**< \brief CRC (32 bit additive - returns 32 bit total) (Currently not implemented) */
} CFE_ES_CrcType_t;

/**
* \brief Pool Alignment
*
Expand Down
8 changes: 4 additions & 4 deletions modules/es/fsw/src/cfe_es_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ CFE_Status_t CFE_ES_WriteToSysLog(const char *SpecStringPtr, ...)
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
uint32 CFE_ES_CalculateCRC(const void *DataPtr, size_t DataLength, uint32 InputCRC, uint32 TypeCRC)
uint32 CFE_ES_CalculateCRC(const void *DataPtr, size_t DataLength, uint32 InputCRC, CFE_ES_CrcType_t TypeCRC)
{
uint32 i;
int16 Index;
Expand Down Expand Up @@ -1610,11 +1610,11 @@ uint32 CFE_ES_CalculateCRC(const void *DataPtr, size_t DataLength, uint32 InputC

switch (TypeCRC)
{
case CFE_MISSION_ES_CRC_32:
case CFE_ES_CRC_32:
CFE_ES_WriteToSysLog("%s: Calculate CRC32 not Implemented\n", __func__);
break;

case CFE_MISSION_ES_CRC_16:
case CFE_ES_CRC_16:
Crc = (int16)(0xFFFF & InputCRC);
BufPtr = (const uint8 *)DataPtr;

Expand All @@ -1631,7 +1631,7 @@ uint32 CFE_ES_CalculateCRC(const void *DataPtr, size_t DataLength, uint32 InputC
}
break;

case CFE_MISSION_ES_CRC_8:
case CFE_ES_CRC_8:
CFE_ES_WriteToSysLog("%s: Calculate CRC8 not Implemented\n", __func__);
break;

Expand Down
13 changes: 7 additions & 6 deletions modules/es/fsw/src/cfe_es_cds_mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <string.h>

#include "cfe_es_module_all.h"
#include "cfe_es_api_typedefs.h"

/*****************************************************************************/
/*
Expand Down Expand Up @@ -176,7 +177,7 @@ int32 CFE_ES_CDSBlockWrite(CFE_ES_CDSHandle_t Handle, const void *DataToWrite)
size_t UserDataSize;
size_t UserDataOffset;
CFE_ES_CDS_RegRec_t * CDSRegRecPtr;

CFE_ES_CrcType_t TypeCrc = CFE_ES_CRC_16;
/* Ensure the log message is an empty string in case it is never written to */
LogMessage[0] = 0;

Expand Down Expand Up @@ -213,10 +214,9 @@ int32 CFE_ES_CDSBlockWrite(CFE_ES_CDSHandle_t Handle, const void *DataToWrite)
UserDataOffset = CDSRegRecPtr->BlockOffset;
UserDataOffset += sizeof(CFE_ES_CDS_BlockHeader_t);

CDS->Cache.Data.BlockHeader.Crc =
CFE_ES_CalculateCRC(DataToWrite, UserDataSize, 0, CFE_MISSION_ES_DEFAULT_CRC);
CDS->Cache.Offset = CDSRegRecPtr->BlockOffset;
CDS->Cache.Size = sizeof(CFE_ES_CDS_BlockHeader_t);
CDS->Cache.Data.BlockHeader.Crc = CFE_ES_CalculateCRC(DataToWrite, UserDataSize, 0, TypeCrc);
CDS->Cache.Offset = CDSRegRecPtr->BlockOffset;
CDS->Cache.Size = sizeof(CFE_ES_CDS_BlockHeader_t);

/* Write the new block descriptor for the data coming from the Application */
Status = CFE_ES_CDS_CacheFlush(&CDS->Cache);
Expand Down Expand Up @@ -272,6 +272,7 @@ int32 CFE_ES_CDSBlockRead(void *DataRead, CFE_ES_CDSHandle_t Handle)
size_t UserDataSize;
size_t UserDataOffset;
CFE_ES_CDS_RegRec_t * CDSRegRecPtr;
CFE_ES_CrcType_t TypeCrc = CFE_ES_CRC_16;

CDSRegRecPtr = CFE_ES_LocateCDSBlockRecordByID(Handle);

Expand Down Expand Up @@ -313,7 +314,7 @@ int32 CFE_ES_CDSBlockRead(void *DataRead, CFE_ES_CDSHandle_t Handle)
if (PspStatus == CFE_PSP_SUCCESS)
{
/* Compute the CRC for the data read from the CDS and determine if the data is still valid */
CrcOfCDSData = CFE_ES_CalculateCRC(DataRead, UserDataSize, 0, CFE_MISSION_ES_DEFAULT_CRC);
CrcOfCDSData = CFE_ES_CalculateCRC(DataRead, UserDataSize, 0, TypeCrc);

/* If the CRCs do not match, report an error */
if (CrcOfCDSData != CDS->Cache.Data.BlockHeader.Crc)
Expand Down
16 changes: 8 additions & 8 deletions modules/es/fsw/src/cfe_es_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include "cfe_version.h"
#include "target_config.h"
#include "cfe_es_verify.h"

#include "cfe_es_api_typedefs.h"
#include "cfe_config.h"

#include <string.h>
Expand Down Expand Up @@ -272,12 +272,12 @@ void CFE_ES_GenerateBuildInfoEvents(void)
*-----------------------------------------------------------------*/
int32 CFE_ES_TaskInit(void)
{
int32 Status;
int32 PspStatus;
uint32 SizeofCfeSegment;
cpuaddr CfeSegmentAddr;
uint8 VersionNumber[4];

int32 Status;
int32 PspStatus;
uint32 SizeofCfeSegment;
cpuaddr CfeSegmentAddr;
uint8 VersionNumber[4];
CFE_ES_CrcType_t TypeCrc = CFE_ES_CRC_16;
/*
** Initialize task command execution counters
*/
Expand Down Expand Up @@ -364,7 +364,7 @@ int32 CFE_ES_TaskInit(void)
if (PspStatus == CFE_PSP_SUCCESS)
{
CFE_ES_Global.TaskData.HkPacket.Payload.CFECoreChecksum =
CFE_ES_CalculateCRC((void *)(CfeSegmentAddr), SizeofCfeSegment, 0, CFE_MISSION_ES_DEFAULT_CRC);
CFE_ES_CalculateCRC((void *)(CfeSegmentAddr), SizeofCfeSegment, 0, TypeCrc);
}
else
{
Expand Down
16 changes: 11 additions & 5 deletions modules/es/ut-coverage/es_UT.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "es_UT.h"
#include "target_config.h"
#include "cfe_config.h"
#include "cfe_es_api_typedefs.h"

#define ES_UT_CDS_BLOCK_SIZE 16

Expand Down Expand Up @@ -3943,6 +3944,7 @@ void TestAPI(void)
CFE_ES_AppInfo_t AppInfo;
CFE_ES_AppRecord_t * UtAppRecPtr;
CFE_ES_TaskRecord_t *UtTaskRecPtr;
CFE_ES_CrcType_t TypeCrc;

UtPrintf("Begin Test API");

Expand Down Expand Up @@ -4384,29 +4386,33 @@ void TestAPI(void)
/* Test calculating a CRC on a range of memory using CRC type 8
* NOTE: This capability is not currently implemented in cFE
*/
TypeCrc = CFE_ES_CRC_8;
memset(Data, 1, sizeof(Data));
ES_ResetUnitTest();
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(&Data, 12, 345353, CFE_MISSION_ES_CRC_8), 0);
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(&Data, 12, 345353, TypeCrc), 0);

/* Test calculating a CRC on a range of memory using CRC type 16 */
TypeCrc = CFE_ES_CRC_16;
ES_ResetUnitTest();
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(&Data, 12, 345353, CFE_MISSION_ES_CRC_16), 2688);
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(&Data, 12, 345353, TypeCrc), 2688);

/* Test calculating a CRC on a range of memory using CRC type 32
* NOTE: This capability is not currently implemented in cFE
*/
TypeCrc = CFE_ES_CRC_32;
ES_ResetUnitTest();
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(&Data, 12, 345353, CFE_MISSION_ES_CRC_32), 0);
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(&Data, 12, 345353, TypeCrc), 0);

/* Test calculating a CRC on a range of memory using an invalid CRC type
*/
ES_ResetUnitTest();
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(&Data, 12, 345353, -1), 0);

/* Test NULL and zero size */
TypeCrc = CFE_ES_CRC_16;
ES_ResetUnitTest();
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(NULL, 12, 345353, CFE_MISSION_ES_CRC_16), 345353);
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(&Data, 0, 345353, CFE_MISSION_ES_CRC_16), 345353);
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(NULL, 12, 345353, TypeCrc), 345353);
UtAssert_UINT32_EQ(CFE_ES_CalculateCRC(&Data, 0, 345353, TypeCrc), 345353);

/* Test shared mutex take with a take error */
ES_ResetUnitTest();
Expand Down
13 changes: 8 additions & 5 deletions modules/tbl/fsw/src/cfe_tbl_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
** Required header files...
*/
#include "cfe_tbl_module_all.h"
#include "cfe_es_api_typedefs.h"

#include <string.h>

Expand Down Expand Up @@ -58,6 +59,7 @@ CFE_Status_t CFE_TBL_Register(CFE_TBL_Handle_t *TblHandlePtr, const char *Name,
char AppName[OS_MAX_API_NAME] = {"UNKNOWN"};
char TblName[CFE_TBL_MAX_FULL_NAME_LEN] = {""};
CFE_TBL_Handle_t AccessIndex;
CFE_ES_CrcType_t TypeCrc = CFE_ES_CRC_16;

if (TblHandlePtr == NULL || Name == NULL)
{
Expand Down Expand Up @@ -406,8 +408,8 @@ CFE_Status_t CFE_TBL_Register(CFE_TBL_Handle_t *TblHandlePtr, const char *Name,
RegRecPtr->TableLoadedOnce = CritRegRecPtr->TableLoadedOnce;

/* Compute the CRC on the specified table buffer */
WorkingBufferPtr->Crc = CFE_ES_CalculateCRC(
WorkingBufferPtr->BufferPtr, RegRecPtr->Size, 0, CFE_MISSION_ES_DEFAULT_CRC);
WorkingBufferPtr->Crc =
CFE_ES_CalculateCRC(WorkingBufferPtr->BufferPtr, RegRecPtr->Size, 0, TypeCrc);

/* Make sure everyone who sees the table knows that it has been updated */
CFE_TBL_NotifyTblUsersOfUpdate(RegRecPtr);
Expand Down Expand Up @@ -676,6 +678,7 @@ CFE_Status_t CFE_TBL_Load(CFE_TBL_Handle_t TblHandle, CFE_TBL_SrcEnum_t SrcType,
CFE_TBL_RegistryRec_t * RegRecPtr;
char AppName[OS_MAX_API_NAME] = {"UNKNOWN"};
bool FirstTime = false;
CFE_ES_CrcType_t TypeCrc = CFE_ES_CRC_16;

if (SrcDataPtr == NULL)
{
Expand Down Expand Up @@ -785,8 +788,7 @@ CFE_Status_t CFE_TBL_Load(CFE_TBL_Handle_t TblHandle, CFE_TBL_SrcEnum_t SrcType,
WorkingBufferPtr->FileCreateTimeSubSecs = 0;

/* Compute the CRC on the specified table buffer */
WorkingBufferPtr->Crc =
CFE_ES_CalculateCRC(WorkingBufferPtr->BufferPtr, RegRecPtr->Size, 0, CFE_MISSION_ES_DEFAULT_CRC);
WorkingBufferPtr->Crc = CFE_ES_CalculateCRC(WorkingBufferPtr->BufferPtr, RegRecPtr->Size, 0, TypeCrc);

break;
default:
Expand Down Expand Up @@ -1491,6 +1493,7 @@ CFE_Status_t CFE_TBL_Modified(CFE_TBL_Handle_t TblHandle)
CFE_TBL_Handle_t AccessIterator;
CFE_ES_AppId_t ThisAppId;
size_t FilenameLen;
CFE_ES_CrcType_t TypeCrc = CFE_ES_CRC_16;

/* Verify that this application has the right to perform operation */
Status = CFE_TBL_ValidateAccess(TblHandle, &ThisAppId);
Expand All @@ -1513,7 +1516,7 @@ CFE_Status_t CFE_TBL_Modified(CFE_TBL_Handle_t TblHandle)

/* Update CRC on contents of table */
RegRecPtr->Buffers[RegRecPtr->ActiveBufferIndex].Crc = CFE_ES_CalculateCRC(
RegRecPtr->Buffers[RegRecPtr->ActiveBufferIndex].BufferPtr, RegRecPtr->Size, 0, CFE_MISSION_ES_DEFAULT_CRC);
RegRecPtr->Buffers[RegRecPtr->ActiveBufferIndex].BufferPtr, RegRecPtr->Size, 0, TypeCrc);

FilenameLen = strlen(RegRecPtr->LastFileLoaded);
if (FilenameLen < (sizeof(RegRecPtr->LastFileLoaded) - 4))
Expand Down
5 changes: 3 additions & 2 deletions modules/tbl/fsw/src/cfe_tbl_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
** Required header files...
*/
#include "cfe_tbl_module_all.h"
#include "cfe_es_api_typedefs.h"

#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -782,6 +783,7 @@ int32 CFE_TBL_LoadFromFile(const char *AppName, CFE_TBL_LoadBuff_t *WorkingBuffe
size_t FilenameLen = strlen(Filename);
uint32 NumBytes;
uint8 ExtraByte;
CFE_ES_CrcType_t TypeCrc = CFE_ES_CRC_16;

if (FilenameLen > (OS_MAX_PATH_LEN - 1))
{
Expand Down Expand Up @@ -899,8 +901,7 @@ int32 CFE_TBL_LoadFromFile(const char *AppName, CFE_TBL_LoadBuff_t *WorkingBuffe
WorkingBufferPtr->FileCreateTimeSubSecs = StdFileHeader.TimeSubSeconds;

/* Compute the CRC on the specified table buffer */
WorkingBufferPtr->Crc =
CFE_ES_CalculateCRC(WorkingBufferPtr->BufferPtr, RegRecPtr->Size, 0, CFE_MISSION_ES_DEFAULT_CRC);
WorkingBufferPtr->Crc = CFE_ES_CalculateCRC(WorkingBufferPtr->BufferPtr, RegRecPtr->Size, 0, TypeCrc);

OS_close(FileDescriptor);

Expand Down
Loading

0 comments on commit 90437e0

Please sign in to comment.