Skip to content

Commit

Permalink
Fix MISRA C 2012 deviations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Mar 5, 2024
1 parent df553bd commit 74f06fb
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 140 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
cmake -S test -B build/ \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Debug \
-DUNITTEST=ON \
-DBUILD_CLONE_SUBMODULES=ON \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Werror -DNDEBUG'
Expand Down
19 changes: 8 additions & 11 deletions source/cellular_3gpp_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1419,8 +1419,8 @@ static CellularError_t atcmdUpdateMccMnc( CellularContext_t * pContext,
CellularAtReq_t atCopsRequest = { 0 };

/* Set the response to numeric format. */
atCopsRequest.pAtCmd = "AT+COPS=3,2",
atCopsRequest.atCmdType = CELLULAR_AT_NO_RESULT,
atCopsRequest.pAtCmd = "AT+COPS=3,2";
atCopsRequest.atCmdType = CELLULAR_AT_NO_RESULT;
pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atCopsRequest );

if( pktStatus == CELLULAR_PKT_STATUS_OK )
Expand Down Expand Up @@ -1808,7 +1808,7 @@ CellularError_t Cellular_CommonGetRegisteredNetwork( CellularHandle_t cellularHa
}
else
{
memset( pOperatorInfo, 0, sizeof( cellularOperatorInfo_t ) );
( void ) memset( pOperatorInfo, 0, sizeof( cellularOperatorInfo_t ) );
cellularStatus = atcmdUpdateMccMnc( pContext, pOperatorInfo );
}

Expand Down Expand Up @@ -2667,14 +2667,7 @@ CellularError_t Cellular_CommonGetSimCardLockStatus( CellularHandle_t cellularHa
CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle;
CellularError_t cellularStatus = CELLULAR_SUCCESS;
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
CellularAtReq_t atReqGetSimLockStatus = { 0 };

atReqGetSimLockStatus.pAtCmd = "AT+CPIN?";
atReqGetSimLockStatus.atCmdType = CELLULAR_AT_WITH_PREFIX;
atReqGetSimLockStatus.pAtRspPrefix = "+CPIN";
atReqGetSimLockStatus.respCallback = _Cellular_RecvFuncGetSimLockStatus;
atReqGetSimLockStatus.pData = NULL;
atReqGetSimLockStatus.dataLen = 0;
CellularAtReq_t atReqGetSimLockStatus;

/* pContext is checked in _Cellular_CheckLibraryStatus function. */
cellularStatus = _Cellular_CheckLibraryStatus( pContext );
Expand All @@ -2693,6 +2686,10 @@ CellularError_t Cellular_CommonGetSimCardLockStatus( CellularHandle_t cellularHa
/* Initialize the sim state and the sim lock state. */
pSimCardStatus->simCardLockState = CELLULAR_SIM_CARD_LOCK_UNKNOWN;

atReqGetSimLockStatus.pAtCmd = "AT+CPIN?";
atReqGetSimLockStatus.atCmdType = CELLULAR_AT_WITH_PREFIX;
atReqGetSimLockStatus.pAtRspPrefix = "+CPIN";
atReqGetSimLockStatus.respCallback = _Cellular_RecvFuncGetSimLockStatus;
atReqGetSimLockStatus.pData = &pSimCardStatus->simCardLockState;
atReqGetSimLockStatus.dataLen = ( uint16_t ) sizeof( CellularSimCardLockState_t );

Expand Down
16 changes: 7 additions & 9 deletions source/cellular_pkthandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ static CellularPktStatus_t _Cellular_DataSendWithTimeoutDelayRaw( CellularContex
uint32_t timeoutMs );
static void _Cellular_PktHandlerAcquirePktRequestMutex( CellularContext_t * pContext );
static void _Cellular_PktHandlerReleasePktRequestMutex( CellularContext_t * pContext );
static int _searchCompareFunc( const void * pInputToken,
const void * pBase );
static int32_t _searchCompareFunc( const void * pInputToken,
const void * pBase );
static int32_t _sortCompareFunc( const void * pElem1Ptr,
const void * pElem2Ptr );
static CellularPktStatus_t _atParseGetHandler( CellularContext_t * pContext,
Expand Down Expand Up @@ -152,7 +152,7 @@ static CellularPktStatus_t _processUrcPacket( CellularContext_t * pContext,
* leading char. ":" is also checked in Cellular_ATIsPrefixPresent. Remove
* the leading char and split the string into the following substrings :
* pInputLine = "+" pTokenPtr + ":" + pSavePtr. */
pSavePtr = pInputLine + 1;
pSavePtr = &( pInputLine[ 1 ] );
pTokenPtr = strtok_r( pSavePtr, ":", &pSavePtr );

if( pTokenPtr == NULL )
Expand Down Expand Up @@ -182,7 +182,7 @@ static CellularPktStatus_t _processUrcPacket( CellularContext_t * pContext,
{
/* inputWithPrefix is true means the string starts with '+'.
* Restore string to "+pTokenPtr:pSavePtr" for callback function. */
*( pSavePtr - 1 ) = ':';
pSavePtr[ -1 ] = ':';
}

_Cellular_GenericCallback( pContext, pInputLine );
Expand Down Expand Up @@ -360,10 +360,10 @@ static void _Cellular_PktHandlerReleasePktRequestMutex( CellularContext_t * pCon

/*-----------------------------------------------------------*/

static int _searchCompareFunc( const void * pInputToken,
const void * pBase )
static int32_t _searchCompareFunc( const void * pInputToken,
const void * pBase )
{
int compareValue = 0;
int32_t compareValue = 0;
const char * pToken = ( const char * ) pInputToken;
const CellularAtParseTokenMap_t * pBasePtr = ( const CellularAtParseTokenMap_t * ) pBase;
uint32_t tokenLen = ( uint32_t ) strlen( pInputToken );
Expand Down Expand Up @@ -833,8 +833,6 @@ CellularPktStatus_t _Cellular_AtParseInit( const CellularContext_t * pContext )
LogError( ( "AtParseFail URC token table is not sorted" ) );
}

configASSERT( finit == true );

for( i = 0; i < tokenMapSize; i++ )
{
LogDebug( ( "Callbacks setup for %u : %s", ( unsigned int ) i, pTokenMap[ i ].pStrValue ) );
Expand Down
19 changes: 8 additions & 11 deletions source/cellular_pktio.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ static void _handleAllReceived( CellularContext_t * pContext,
CellularATCommandResponse_t ** ppAtResp,
char * pData,
uint32_t bytesInBuffer );
static uint32_t _handleRxDataEvent( CellularContext_t * pContext,
CellularATCommandResponse_t ** ppAtResp );
static uint32_t _handleRxDataEvent( CellularContext_t * pContext );
static void _pktioReadThread( void * pUserData );
static void _PktioInitProcessReadThreadStatus( CellularContext_t * pContext );
static bool _getNextLine( CellularContext_t * pContext,
Expand Down Expand Up @@ -424,7 +423,7 @@ static bool _checkUrcTokenWoPrefix( const CellularContext_t * pContext,
uint32_t urcTokenTableSize = pContext->tokenTable.cellularUrcTokenWoPrefixTableSize;
const char * const * const pUrcTokenTable = pContext->tokenTable.pCellularUrcTokenWoPrefixTable;

if( ( pUrcTokenTable == NULL ) || ( urcTokenTableSize == 0 ) )
if( ( pUrcTokenTable == NULL ) || ( urcTokenTableSize == 0U ) )
{
ret = false;
}
Expand Down Expand Up @@ -1164,22 +1163,21 @@ static void _handleAllReceived( CellularContext_t * pContext,

/*-----------------------------------------------------------*/

static uint32_t _handleRxDataEvent( CellularContext_t * pContext,
CellularATCommandResponse_t ** ppAtResp )
static uint32_t _handleRxDataEvent( CellularContext_t * pContext )
{
char * pLine = NULL;
uint32_t bytesRead = 0;
uint32_t bytesLeft = 0;

/* Return the first line, may be more lines in buffer. */
/* Start from pLine there are bytesRead bytes. */
pLine = _Cellular_ReadLine( pContext, &bytesRead, *ppAtResp );
pLine = _Cellular_ReadLine( pContext, &bytesRead, pContext->pAtCmdResp );

if( bytesRead > 0U )
{
if( pContext->dataLength != 0U )
{
( void ) _handleData( pLine, pContext, *ppAtResp, &pLine, bytesRead, &bytesLeft );
( void ) _handleData( pLine, pContext, pContext->pAtCmdResp, &pLine, bytesRead, &bytesLeft );
}
else
{
Expand All @@ -1191,7 +1189,7 @@ static uint32_t _handleRxDataEvent( CellularContext_t * pContext,
{
/* Add the null terminated char to the end of pLine. */
pLine[ bytesLeft ] = '\0';
_handleAllReceived( pContext, ppAtResp, pLine, bytesLeft );
_handleAllReceived( pContext, &pContext->pAtCmdResp, pLine, bytesLeft );
}
}

Expand All @@ -1203,7 +1201,6 @@ static uint32_t _handleRxDataEvent( CellularContext_t * pContext,
static void _pktioReadThread( void * pUserData )
{
CellularContext_t * pContext = ( CellularContext_t * ) pUserData;
CellularATCommandResponse_t * pAtResp = NULL;
PlatformEventGroup_EventBits uxBits = 0;
uint32_t bytesRead = 0U;

Expand All @@ -1227,15 +1224,15 @@ static void _pktioReadThread( void * pUserData )
if( ( uxBits & ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_ABORT ) != 0U )
{
LogDebug( ( "Abort received, cleaning up!" ) );
FREE_AT_RESPONSE_AND_SET_NULL( pAtResp );
FREE_AT_RESPONSE_AND_SET_NULL( pContext->pAtCmdResp );
break;
}
else if( ( uxBits & ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_RX_DATA ) != 0U )
{
/* Keep Reading until there is no more bytes in comm interface. */
do
{
bytesRead = _handleRxDataEvent( pContext, &pAtResp );
bytesRead = _handleRxDataEvent( pContext );
} while( ( bytesRead != 0U ) );
}
else
Expand Down
19 changes: 10 additions & 9 deletions source/include/private/cellular_common_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,16 @@ struct CellularContext
const CellularCommInterface_t * pCommIntf; /**< Communication interface for target specific. */

/* Common library. */
bool bLibOpened; /**< CellularLib is currently open. */
bool bLibShutdown; /**< CellularLib prematurely shut down. */
bool bLibClosing; /**< Graceful shutdown in progress. */
PlatformMutex_t libStatusMutex; /**< The mutex for changing lib status. */
PlatformMutex_t libAtDataMutex; /**< The mutex for AT data in cellular context. */
_callbackEvents_t cbEvents; /**< Call back functions registered to report events. */
cellularAtData_t libAtData; /**< Global variables. */

CellularTokenTable_t tokenTable; /**< Token table to config pkthandler and pktio. */
CellularATCommandResponse_t * pAtCmdResp; /**< Solicited response pointer. */
bool bLibOpened; /**< CellularLib is currently open. */
bool bLibShutdown; /**< CellularLib prematurely shut down. */
bool bLibClosing; /**< Graceful shutdown in progress. */
PlatformMutex_t libStatusMutex; /**< The mutex for changing lib status. */
PlatformMutex_t libAtDataMutex; /**< The mutex for AT data in cellular context. */
_callbackEvents_t cbEvents; /**< Call back functions registered to report events. */
cellularAtData_t libAtData; /**< Global variables. */

CellularTokenTable_t tokenTable; /**< Token table to config pkthandler and pktio. */

/* Packet handler. */
PlatformMutex_t pktRequestMutex; /**< The mutex for sending request. */
Expand Down
120 changes: 69 additions & 51 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ set_property( GLOBAL PROPERTY USE_FOLDERS ON )
set( CMAKE_C_STANDARD 90 )
set( CMAKE_C_STANDARD_REQUIRED ON )

# If no configuration is defined, turn everything on.
if( NOT DEFINED COV_ANALYSIS AND NOT DEFINED UNITTEST )
set( COV_ANALYSIS ON )
set( UNITTEST ON )
endif()

# Do not allow in-source build.
if( ${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR} )
message( FATAL_ERROR "In-source build is not allowed. Please build in a separate directory, such as ${PROJECT_SOURCE_DIR}/build." )
Expand All @@ -32,74 +38,86 @@ set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
# ===================================== Coverity Analysis Configuration =================================================

# Set the source path and include path
set( CELLULAR_COMMON_SOURCE ${MODULE_ROOT_DIR}/source )
set( CELLULAR_INCLUDE_DIRS ${CELLULAR_COMMON_SOURCE}/include )
set( CELLULAR_COMMON_SOURCE_DIRS ${MODULE_ROOT_DIR}/source )
set( CELLULAR_INCLUDE_DIRS ${CELLULAR_COMMON_SOURCE_DIRS}/include )
set( CELLULAR_COMMON_INCLUDE_DIRS ${CELLULAR_INCLUDE_DIRS}/common )
set( CELLULAR_COMMON_INCLUDE_PRIVATE_DIRS ${CELLULAR_INCLUDE_DIRS}/private )
set( CELLULAR_INTERFACE_INCLUDE_DIRS ${CELLULAR_COMMON_SOURCE}/interface )
set( CELLULAR_INTERFACE_INCLUDE_DIRS ${CELLULAR_COMMON_SOURCE_DIRS}/interface )
set( CELLULAR_TEST_DIRS ${MODULE_ROOT_DIR}/test/unit-test )

# Target for Coverity analysis that builds the library.
add_library( coverity_analysis
${CELLULAR_COMMON_SOURCE}/cellular_at_core.c
${CELLULAR_COMMON_SOURCE}/cellular_common.c
${CELLULAR_COMMON_SOURCE}/cellular_common_api.c
${CELLULAR_COMMON_SOURCE}/cellular_3gpp_urc_handler.c
${CELLULAR_COMMON_SOURCE}/cellular_3gpp_api.c
${CELLULAR_COMMON_SOURCE}/cellular_pkthandler.c
${CELLULAR_COMMON_SOURCE}/cellular_pktio.c )
if( COV_ANALYSIS )
# Target for Coverity analysis that builds the library.
add_library( coverity_analysis
${CELLULAR_COMMON_SOURCE_DIRS}/cellular_at_core.c
${CELLULAR_COMMON_SOURCE_DIRS}/cellular_common.c
${CELLULAR_COMMON_SOURCE_DIRS}/cellular_common_api.c
${CELLULAR_COMMON_SOURCE_DIRS}/cellular_3gpp_urc_handler.c
${CELLULAR_COMMON_SOURCE_DIRS}/cellular_3gpp_api.c
${CELLULAR_COMMON_SOURCE_DIRS}/cellular_pkthandler.c
${CELLULAR_COMMON_SOURCE_DIRS}/cellular_pktio.c )

# Build Cellular library target without custom config dependency.
target_compile_definitions( coverity_analysis PUBLIC CELLULAR_DO_NOT_USE_CUSTOM_CONFIG=1 )
# Build Cellular library target without custom config dependency.
target_compile_definitions( coverity_analysis PUBLIC CELLULAR_DO_NOT_USE_CUSTOM_CONFIG=1 )

# Cellular include path.
target_include_directories( coverity_analysis PUBLIC ${CELLULAR_COMMON_INCLUDE_DIRS} ${CELLULAR_INCLUDE_DIRS} ${CELLULAR_INTERFACE_INCLUDE_DIRS} ${CELLULAR_TEST_DIRS})
# Cellular include path.
target_include_directories( coverity_analysis PUBLIC ${CELLULAR_COMMON_INCLUDE_DIRS} ${CELLULAR_INCLUDE_DIRS} ${CELLULAR_INTERFACE_INCLUDE_DIRS} ${CELLULAR_TEST_DIRS} )

# Cellular private include path.
target_include_directories( coverity_analysis PRIVATE ${CELLULAR_COMMON_INCLUDE_PRIVATE_DIRS} )
# Cellular private include path.
target_include_directories( coverity_analysis PRIVATE ${CELLULAR_COMMON_INCLUDE_PRIVATE_DIRS} )

# Build without debug enabled when performing static analysis
target_compile_options(coverity_analysis PUBLIC -DNDEBUG )
# Build without debug enabled when performing static analysis
target_compile_options( coverity_analysis PUBLIC -DNDEBUG )

# ==================================== Test Configuration ========================================
file( GLOB CELLULAR_COMMON_SOURCE ${CELLULAR_COMMON_SOURCE_DIRS}/*.c )

# Define a CMock resource path.
set( CMOCK_DIR ${MODULE_ROOT_DIR}/test/unit-test/CMock CACHE INTERNAL "CMock library source directory." )
add_custom_target( fix_source ALL
COMMAND sed -i -b -e 's/pdFALSE/pdFAIL/g' -e 's/pdTRUE/pdPASS/g' ${CELLULAR_COMMON_SOURCE})

# Include CMock build configuration.
include( unit-test/cmock_build.cmake )
add_dependencies( coverity_analysis fix_source )
endif()

# Check if the CMock source directory exists, and if not present, clone the submodule
# if BUILD_CLONE_SUBMODULES configuration is enabled.
if( NOT EXISTS ${CMOCK_DIR}/src )
# Attempt to clone CMock.
if( ${BUILD_CLONE_SUBMODULES} )
clone_cmock()
else()
message( FATAL_ERROR "The required submodule CMock does not exist. Either clone it manually, or set BUILD_CLONE_SUBMODULES to 1 to automatically clone it during build." )
# ==================================== Test Configuration ========================================

if( UNITTEST )
# Define a CMock resource path.
set( CMOCK_DIR ${MODULE_ROOT_DIR}/test/unit-test/CMock CACHE INTERNAL "CMock library source directory." )

# Include CMock build configuration.
include( unit-test/cmock_build.cmake )

# Check if the CMock source directory exists, and if not present, clone the submodule
# if BUILD_CLONE_SUBMODULES configuration is enabled.
if( NOT EXISTS ${CMOCK_DIR}/src )
# Attempt to clone CMock.
if( ${BUILD_CLONE_SUBMODULES} )
clone_cmock()
else()
message( FATAL_ERROR "The required submodule CMock does not exist. Either clone it manually, or set BUILD_CLONE_SUBMODULES to 1 to automatically clone it during build." )
endif()
endif()
endif()

# Add unit test and coverage configuration.
# Add unit test and coverage configuration.

# Use CTest utility for managing test runs. This has to be added BEFORE
# defining test targets with add_test()
enable_testing()
# Use CTest utility for managing test runs. This has to be added BEFORE
# defining test targets with add_test()
enable_testing()

# Add build targets for CMock and Unit, required for unit testing.
add_cmock_targets()
# Add build targets for CMock and Unit, required for unit testing.
add_cmock_targets()

# Add function to enable CMock based tests and coverage.
include( ${MODULE_ROOT_DIR}/tools/cmock/create_test.cmake )
# Add function to enable CMock based tests and coverage.
include( ${MODULE_ROOT_DIR}/tools/cmock/create_test.cmake )

# Include build configuration for unit tests.
add_subdirectory( unit-test )
# Include build configuration for unit tests.
add_subdirectory( unit-test )

# ==================================== Coverage Analysis configuration ========================================
# ==================================== Coverage Analysis configuration ========================================

# Add a target for running coverage on tests.
add_custom_target( coverage
COMMAND ${CMAKE_COMMAND} -P ${MODULE_ROOT_DIR}/tools/cmock/coverage.cmake
DEPENDS cmock unity cellular_at_core_utest cellular_pktio_utest cellular_pkthandler_utest cellular_common_api_utest cellular_common_utest cellular_3gpp_api_utest cellular_3gpp_urc_handler_utest
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
# Add a target for running coverage on tests.
add_custom_target( coverage
COMMAND ${CMAKE_COMMAND} -DCMOCK_DIR=${CMOCK_DIR} -P ${MODULE_ROOT_DIR}/tools/cmock/coverage.cmake
DEPENDS cmock unity cellular_at_core_utest cellular_pktio_utest cellular_pkthandler_utest cellular_common_api_utest cellular_common_utest cellular_3gpp_api_utest cellular_3gpp_urc_handler_utest
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

endif()
Loading

0 comments on commit 74f06fb

Please sign in to comment.