Skip to content

Commit

Permalink
Fix #2110, Add CFE_ES_StatusToString and CFE_STATUS_C
Browse files Browse the repository at this point in the history
  • Loading branch information
skliper committed Jun 6, 2022
1 parent 10898c9 commit 87bde9e
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/src/cfe_api.dox
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<LI> #CFE_ES_CalculateCRC - \copybrief CFE_ES_CalculateCRC
<LI> #CFE_ES_WriteToSysLog - \copybrief CFE_ES_WriteToSysLog
<LI> #CFE_ES_ProcessAsyncEvent - \copybrief CFE_ES_ProcessAsyncEvent
<LI> #CFE_ES_StatusToString - \copybrief CFE_ES_StatusToString
</UL>
<LI> \ref CFEAPIESResourceID
<UL>
Expand Down
1 change: 1 addition & 0 deletions modules/cfe_testcase/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_cfe_app(cfe_testcase
src/cfe_test_table.c
src/es_application_control_test.c
src/es_behavior_test.c
src/es_error_test.c
src/es_info_test.c
src/es_task_test.c
src/es_cds_test.c
Expand Down
1 change: 1 addition & 0 deletions modules/cfe_testcase/src/cfe_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void CFE_TestMain(void)
ESBehaviorestSetup();
ESCDSTestSetup();
ESCounterTestSetup();
ESErrorTestSetup();
ESInfoTestSetup();
ESMemPoolTestSetup();
ESMiscTestSetup();
Expand Down
1 change: 1 addition & 0 deletions modules/cfe_testcase/src/cfe_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void ESApplicationControlTestSetup(void);
void ESBehaviorestSetup(void);
void ESCDSTestSetup(void);
void ESCounterTestSetup(void);
void ESErrorTestSetup(void);
void ESInfoTestSetup(void);
void ESMemPoolTestSetup(void);
void ESMiscTestSetup(void);
Expand Down
58 changes: 58 additions & 0 deletions modules/cfe_testcase/src/es_error_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/************************************************************************
* NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes”
*
* Copyright (c) 2020 United States Government as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
************************************************************************/

/**
* \file
* Functional test of basic ES Error APIs
*/

/*
* Includes
*/

#include "cfe_test.h"

void TestStatusToString_Helper(CFE_Status_t status)
{
CFE_StatusString_t status_string;
CFE_StatusString_t *rtn_addr;
char expected[CFE_STATUS_STRING_LENGTH + 1];

/* Used oversized string to test for truncation */
snprintf(expected, sizeof(expected), "0x%08x", (unsigned int)status);
rtn_addr = CFE_ES_StatusToString(status, &status_string);
UtAssert_ADDRESS_EQ(rtn_addr, &status_string);
UtAssert_STRINGBUF_EQ(status_string, sizeof(status_string), expected, sizeof(expected));
}

void TestStatusToString(void)
{
/* NULL test */
UtAssert_ADDRESS_EQ(CFE_ES_StatusToString(CFE_SUCCESS, NULL), NULL);

/* Status value tests */
TestStatusToString_Helper(CFE_SUCCESS);
TestStatusToString_Helper(CFE_SEVERITY_ERROR);
TestStatusToString_Helper(CFE_STATUS_C(INT32_MAX));
TestStatusToString_Helper(CFE_STATUS_C(INT32_MIN));
}

void ESErrorTestSetup(void)
{
UtTest_Add(TestStatusToString, NULL, NULL, "TestStatusToString");
}
33 changes: 31 additions & 2 deletions modules/core_api/fsw/inc/cfe_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,40 @@
/* Include Files */
#include "osapi.h"

/*
* Define a type for readability.
/**
* \brief cFE Status type for readability and eventually type safety
*/
typedef int32 CFE_Status_t;

/**
* \brief cFE Status macro for literal
*/
#define CFE_STATUS_C(X) ((CFE_Status_t)(X))

/**
* \brief cFE Status converted to string length limit
*
* Used for sizing CFE_StatusString_t intended for use in printing CFE_Status_t values
* Sized for 0x%08x and NULL
*/
#define CFE_STATUS_STRING_LENGTH 11

/**
* @brief For the @ref CFE_ES_StatusToString() function, to ensure
* everyone is making an array of the same length.
*/
typedef char CFE_StatusString_t[CFE_STATUS_STRING_LENGTH];

/**
* @brief Convert status to a string
*
* @param[in] status Status value to convert
* @param[out] status_string Buffer to store status converted to string
*
* @return Passed in string pointer
*/
CFE_StatusString_t *CFE_ES_StatusToString(CFE_Status_t status, CFE_StatusString_t *status_string);

/*
** Status Codes are 32 bit values formatted as follows:
**
Expand Down
17 changes: 17 additions & 0 deletions modules/core_api/ut-stubs/src/cfe_es_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -954,3 +954,20 @@ CFE_Status_t CFE_ES_WriteToSysLog(const char *SpecStringPtr, ...)

return UT_GenStub_GetReturnValue(CFE_ES_WriteToSysLog, CFE_Status_t);
}

/*
* ----------------------------------------------------
* Generated stub function for CFE_ES_StatusToString()
* ----------------------------------------------------
*/
CFE_StatusString_t *CFE_ES_StatusToString(CFE_Status_t status, CFE_StatusString_t *status_string)
{
UT_GenStub_SetupReturnBuffer(CFE_ES_StatusToString, CFE_StatusString_t *);

UT_GenStub_AddParam(CFE_ES_StatusToString, CFE_Status_t, status);
UT_GenStub_AddParam(CFE_ES_StatusToString, CFE_StatusString_t *, status_string);

UT_GenStub_Execute(CFE_ES_StatusToString, Basic, NULL);

return UT_GenStub_GetReturnValue(CFE_ES_StatusToString, CFE_StatusString_t *);
}
17 changes: 17 additions & 0 deletions modules/es/fsw/src/cfe_es_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -2299,3 +2299,20 @@ void CFE_ES_ProcessAsyncEvent(void)
/* This just wakes up the background task to log/handle the event. */
CFE_ES_BackgroundWakeup();
}

/*----------------------------------------------------------------
*
* Function: CFE_ES_StatusToString
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_StatusString_t *CFE_ES_StatusToString(CFE_Status_t status, CFE_StatusString_t *status_string)
{
if (status_string != NULL)
{
snprintf(*status_string, sizeof(*status_string), "0x%08x", (unsigned int)status);
}
return status_string;
}
32 changes: 32 additions & 0 deletions modules/es/ut-coverage/es_UT.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ void UtTest_Setup(void)
UT_ADD_TEST(TestESMempool);
UT_ADD_TEST(TestSysLog);
UT_ADD_TEST(TestBackground);
UT_ADD_TEST(TestStatusToString);
}

/*
Expand Down Expand Up @@ -5631,3 +5632,34 @@ void TestBackground(void)
/* The number of jobs running should be 1 (perf log dump) */
UtAssert_UINT32_EQ(CFE_ES_Global.BackgroundTask.NumJobsRunning, 1);
}

/*--------------------------------------------------------------------------------*
* TestStatusToString test helper function to avoid repeating logic
*--------------------------------------------------------------------------------*/
void TestStatusToString_Helper(CFE_Status_t status)
{
CFE_StatusString_t status_string;
CFE_StatusString_t *rtn_addr;
char expected[CFE_STATUS_STRING_LENGTH + 1];

/* Used oversized string to test for truncation */
snprintf(expected, sizeof(expected), "0x%08x", (unsigned int)status);
rtn_addr = CFE_ES_StatusToString(status, &status_string);
UtAssert_ADDRESS_EQ(rtn_addr, &status_string);
UtAssert_STRINGBUF_EQ(status_string, sizeof(status_string), expected, sizeof(expected));
}

/*--------------------------------------------------------------------------------*
* Functional CFE_ES_StatusToString test
*--------------------------------------------------------------------------------*/
void TestStatusToString(void)
{
/* NULL test */
UtAssert_ADDRESS_EQ(CFE_ES_StatusToString(CFE_SUCCESS, NULL), NULL);

/* Status value tests */
TestStatusToString_Helper(CFE_SUCCESS);
TestStatusToString_Helper(CFE_SEVERITY_ERROR);
TestStatusToString_Helper(CFE_STATUS_C(INT32_MAX));
TestStatusToString_Helper(CFE_STATUS_C(INT32_MIN));
}
1 change: 1 addition & 0 deletions modules/es/ut-coverage/es_UT.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,6 @@ void TestResourceID(void);
void TestGenericCounterAPI(void);
void TestGenericPool(void);
void TestLibs(void);
void TestStatusToString(void);

#endif /* ES_UT_H */

0 comments on commit 87bde9e

Please sign in to comment.