Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Cellular_CommonInit error handling #163

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/doxygen/include/size_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<tr>
<td>cellular_common_api.c</td>
<td><center>0.7K</center></td>
<td><center>0.6K</center></td>
<td><center>0.7K</center></td>
</tr>
<tr>
<td>cellular_common.c</td>
Expand All @@ -45,6 +45,6 @@
<tr>
<td><b>Total estimates</b></td>
<td><b><center>15.1K</center></b></td>
<td><b><center>13.6K</center></b></td>
<td><b><center>13.7K</center></b></td>
</tr>
</table>
61 changes: 47 additions & 14 deletions source/cellular_common_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,58 @@ CellularError_t Cellular_CommonInit( CellularHandle_t * pCellularHandle,
CellularError_t cellularStatus = CELLULAR_SUCCESS;
CellularContext_t * pContext = NULL;

/* Init the common library. */
cellularStatus = _Cellular_LibInit( pCellularHandle, pCommInterface, pTokenTable );

/* Init the module. */
if( cellularStatus == CELLULAR_SUCCESS )
if( pCellularHandle == NULL )
{
pContext = *pCellularHandle;
cellularStatus = Cellular_ModuleInit( pContext, &pContext->pModuleContext );
LogError( ( "Cellular_CommonInit pCellularHandle is NULL." ) );
cellularStatus = CELLULAR_INVALID_HANDLE;
}

/* Setup UE, URC and query register status. */
if( cellularStatus == CELLULAR_SUCCESS )
else if( pCommInterface == NULL )
{
cellularStatus = Cellular_ModuleEnableUE( pContext );
LogError( ( "Cellular_CommonInit pCommInterface is NULL." ) );
cellularStatus = CELLULAR_BAD_PARAMETER;
}

if( cellularStatus == CELLULAR_SUCCESS )
else if( pTokenTable == NULL )
{
LogError( ( "Cellular_CommonInit pTokenTable is NULL." ) );
cellularStatus = CELLULAR_BAD_PARAMETER;
}
else
{
cellularStatus = Cellular_ModuleEnableUrc( pContext );
/* Init the common library. */
cellularStatus = _Cellular_LibInit( pCellularHandle, pCommInterface, pTokenTable );

if( cellularStatus == CELLULAR_SUCCESS )
{
pContext = ( CellularContext_t * ) ( *pCellularHandle );

cellularStatus = Cellular_ModuleInit( pContext, &pContext->pModuleContext );

if( cellularStatus == CELLULAR_SUCCESS )
{
cellularStatus = Cellular_ModuleEnableUE( pContext );

if( cellularStatus == CELLULAR_SUCCESS )
{
cellularStatus = Cellular_ModuleEnableUrc( pContext );
}

if( cellularStatus != CELLULAR_SUCCESS )
{
/* Clean up the resource allocated by cellular module here if
* Cellular_ModuleEnableUE or Cellular_ModuleEnableUrc returns
* error. */
( void ) Cellular_ModuleCleanUp( pContext );
}
}

if( cellularStatus != CELLULAR_SUCCESS )
{
/* Clean up the resource in cellular common library if any of the
* module port function returns error. Error returned by _Cellular_LibInit
* is already handled in the implementation. */
( void ) _Cellular_LibCleanup( pContext );
}
}
}

return cellularStatus;
Expand Down
207 changes: 204 additions & 3 deletions test/unit-test/cellular_common_api_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,220 @@ static CellularCommInterface_t cellularCommInterface =
/* ========================================================================== */

/**
* @brief Test that null handler case for Cellular_CommonInit.
* @brief Cellular_CommonInit - Test the null pCommInterface parameter.
*
* <b>Coverage</b>
* @code{c}
* if( pCellularHandle == NULL )
* {
* LogError( ( "Cellular_CommonInit pCellularHandle is NULL." ) );
* cellularStatus = CELLULAR_INVALID_HANDLE;
* }
* @endcode
* ( pCellularHandle == NULL ) is true.
*/
void test_Cellular_CommonInit_Null_Handler( void )
{
CellularError_t cellularStatus = CELLULAR_SUCCESS;

_Cellular_LibInit_IgnoreAndReturn( CELLULAR_INVALID_HANDLE );

/* API call. */
cellularStatus = Cellular_CommonInit( NULL, &cellularCommInterface, &tokenTable );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_INVALID_HANDLE, cellularStatus );
}

/**
* @brief Cellular_CommonInit - Test the null pCommInterface parameter.
*
* <b>Coverage</b>
* @code{c}
* else if( pCommInterface == NULL )
* {
* LogError( ( "Cellular_CommonInit pCommInterface is NULL." ) );
* cellularStatus = CELLULAR_BAD_PARAMETER;
* }
* @endcode
* ( pCommInterface == NULL ) is true.
*/
void test_Cellular_CommonInit_Null_Comm_Interface( void )
{
struct CellularContext * pHandler;
CellularError_t cellularStatus = CELLULAR_SUCCESS;

/* API call. */
cellularStatus = Cellular_CommonInit( &pHandler, NULL, &tokenTable );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_BAD_PARAMETER, cellularStatus );
}

/**
* @brief Cellular_CommonInit - Test the null pTokenTable parameter.
*
* <b>Coverage</b>
* @code{c}
* else if( pTokenTable == NULL )
* {
* LogError( ( "Cellular_CommonInit pTokenTable is NULL." ) );
* cellularStatus = CELLULAR_BAD_PARAMETER;
* }
* @endcode
* ( pTokenTable == NULL ) is true.
*/
void test_Cellular_CommonInit_Null_Token_Table( void )
{
struct CellularContext * pHandler;
CellularError_t cellularStatus = CELLULAR_SUCCESS;

/* API call. */
cellularStatus = Cellular_CommonInit( &pHandler, &cellularCommInterface, NULL );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_BAD_PARAMETER, cellularStatus );
}

/**
* @brief Cellular_CommonInit - Test that _Cellular_LibInit returns error.
*
* <b>Coverage</b>
* @code{c}
* cellularStatus = _Cellular_LibInit( pCellularHandle, pCommInterface, pTokenTable );
* ...
* if( cellularStatus == CELLULAR_SUCCESS )
* {
* ...
* }
* @endcode
* ( cellularStatus == CELLULAR_SUCCESS ) is false.
*/
void test_Cellular_CommonInit_Lib_Init_error( void )
{
struct CellularContext * pHandler;
CellularError_t cellularStatus = CELLULAR_SUCCESS;

/* Expectation. */
_Cellular_LibInit_IgnoreAndReturn( CELLULAR_RESOURCE_CREATION_FAIL );

/* API call. */
cellularStatus = Cellular_CommonInit( &pHandler, &cellularCommInterface, &tokenTable );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_RESOURCE_CREATION_FAIL, cellularStatus );
}

/**
* @brief Cellular_CommonInit - Test that Cellular_ModuleInit returns error.
*
* <b>Coverage</b>
* @code{c}
* cellularStatus = Cellular_ModuleInit( pContext, &pContext->pModuleContext );
* ...
* if( cellularStatus != CELLULAR_SUCCESS )
* {
* ( void ) _Cellular_LibCleanup( pContext );
* }
* @endcode
* ( cellularStatus != CELLULAR_SUCCESS ) is true.
*/
void test_Cellular_CommonInit_Module_Init_error( void )
{
struct CellularContext xCellularContext = { 0 };
struct CellularContext * pContext;
CellularError_t cellularStatus = CELLULAR_SUCCESS;

/* Setup. */
pContext = &xCellularContext;

/* Expectation. */
_Cellular_LibInit_IgnoreAndReturn( CELLULAR_SUCCESS );
Cellular_ModuleInit_ExpectAndReturn( &xCellularContext, &xCellularContext.pModuleContext, CELLULAR_INTERNAL_FAILURE );
_Cellular_LibCleanup_ExpectAndReturn( &xCellularContext, CELLULAR_SUCCESS );

/* API call. */
cellularStatus = Cellular_CommonInit( &pContext, &cellularCommInterface, &tokenTable );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_INTERNAL_FAILURE, cellularStatus );
}

/**
* @brief Cellular_CommonInit - Test that Cellular_ModuleEnableUE returns error.
*
* <b>Coverage</b>
* @code{c}
* cellularStatus = Cellular_ModuleEnableUE( pContext );
* if( cellularStatus == CELLULAR_SUCCESS )
* {
* cellularStatus = Cellular_ModuleEnableUrc( pContext );
* }
* @endcode
* ( cellularStatus == CELLULAR_SUCCESS ) is false.
*/
void test_Cellular_CommonInit_Module_Enable_UE_error( void )
{
struct CellularContext xCellularContext = { 0 };
struct CellularContext * pContext;
CellularError_t cellularStatus = CELLULAR_SUCCESS;

/* Setup. */
pContext = &xCellularContext;

/* Expectation. */
_Cellular_LibInit_IgnoreAndReturn( CELLULAR_SUCCESS );
Cellular_ModuleInit_ExpectAndReturn( &xCellularContext, &xCellularContext.pModuleContext, CELLULAR_SUCCESS );
Cellular_ModuleEnableUE_ExpectAndReturn( &xCellularContext, CELLULAR_INTERNAL_FAILURE );

Cellular_ModuleCleanUp_ExpectAndReturn( &xCellularContext, CELLULAR_SUCCESS );
_Cellular_LibCleanup_ExpectAndReturn( &xCellularContext, CELLULAR_SUCCESS );

/* API call. */
cellularStatus = Cellular_CommonInit( &pContext, &cellularCommInterface, &tokenTable );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_INTERNAL_FAILURE, cellularStatus );
}

/**
* @brief Cellular_CommonInit - Test that Cellular_ModuleEnableUrc returns error.
*
* <b>Coverage</b>
* @code{c}
* ...
* cellularStatus = Cellular_ModuleEnableUrc( pContext );
* ...
* if( cellularStatus != CELLULAR_SUCCESS )
* {
* cellularStatus = Cellular_ModuleCleanUp( pContext );
* }
* @endcode
* ( cellularStatus != CELLULAR_SUCCESS ) is true.
*/
void test_Cellular_CommonInit_Module_Enable_URC_error( void )
{
struct CellularContext xCellularContext = { 0 };
struct CellularContext * pContext;
CellularError_t cellularStatus = CELLULAR_SUCCESS;

/* Setup. */
pContext = &xCellularContext;

/* Expectation. */
_Cellular_LibInit_IgnoreAndReturn( CELLULAR_SUCCESS );
Cellular_ModuleInit_ExpectAndReturn( &xCellularContext, &xCellularContext.pModuleContext, CELLULAR_SUCCESS );
Cellular_ModuleEnableUE_ExpectAndReturn( &xCellularContext, CELLULAR_SUCCESS );
Cellular_ModuleEnableUrc_ExpectAndReturn( &xCellularContext, CELLULAR_INTERNAL_FAILURE );

Cellular_ModuleCleanUp_ExpectAndReturn( &xCellularContext, CELLULAR_SUCCESS );
_Cellular_LibCleanup_ExpectAndReturn( &xCellularContext, CELLULAR_SUCCESS );

/* API call. */
cellularStatus = Cellular_CommonInit( &pContext, &cellularCommInterface, &tokenTable );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_INTERNAL_FAILURE, cellularStatus );
}

/**
* @brief Test that happy path case for Cellular_CommonInit.
*/
Expand Down
Loading