CellularError_t _Cellular_LibInit()

in source/cellular_common.c [929:1057]


CellularError_t _Cellular_LibInit( CellularHandle_t * pCellularHandle,
                                   const CellularCommInterface_t * pCommInterface,
                                   const CellularTokenTable_t * pTokenTable )
{
    CellularError_t cellularStatus = CELLULAR_SUCCESS;
    CellularContext_t * pContext = NULL;
    bool bLibStatusMutexCreateSuccess = false;
    bool bAtDataMutexCreateSuccess = false;
    bool bPktRequestMutexCreateSuccess = false;
    bool bPktResponseMutexCreateSuccess = false;

    cellularStatus = checkInitParameter( pCellularHandle, pCommInterface, pTokenTable );

    if( cellularStatus != CELLULAR_SUCCESS )
    {
        LogError( ( "_Cellular_CommonInit checkInitParameter failed" ) );
    }
    else
    {
        pContext = _Cellular_AllocContext();

        if( pContext == NULL )
        {
            LogError( ( "CellularContext_t allocation failed" ) );
            cellularStatus = CELLULAR_NO_MEMORY;
        }
        else
        {
            /* Assign the comm interface to pContext. */
            pContext->pCommIntf = pCommInterface;

            /* copy the token table. */
            ( void ) memcpy( &pContext->tokenTable, pTokenTable, sizeof( CellularTokenTable_t ) );
        }
    }

    /* Defines the Mutexes and Semophores. */
    if( cellularStatus == CELLULAR_SUCCESS )
    {
        if( _Cellular_CreateLibStatusMutex( pContext ) != true )
        {
            LogError( ( "Could not create CellularLib status mutex" ) );
            cellularStatus = CELLULAR_RESOURCE_CREATION_FAIL;
        }
        else
        {
            bLibStatusMutexCreateSuccess = true;
        }
    }

    if( cellularStatus == CELLULAR_SUCCESS )
    {
        if( _Cellular_CreateAtDataMutex( pContext ) != true )
        {
            LogError( ( "Could not create CELLULAR AT Data mutex " ) );
            cellularStatus = CELLULAR_RESOURCE_CREATION_FAIL;
        }
        else
        {
            bAtDataMutexCreateSuccess = true;
        }
    }

    if( cellularStatus == CELLULAR_SUCCESS )
    {
        if( _Cellular_CreatePktRequestMutex( pContext ) != true )
        {
            LogError( ( "Could not create CELLULAR Pkt Req mutex " ) );
            cellularStatus = CELLULAR_RESOURCE_CREATION_FAIL;
        }
        else
        {
            bPktRequestMutexCreateSuccess = true;
        }
    }

    if( cellularStatus == CELLULAR_SUCCESS )
    {
        if( _Cellular_CreatePktResponseMutex( pContext ) != true )
        {
            LogError( ( "Could not create CELLULAR Pkt Resp mutex " ) );
            cellularStatus = CELLULAR_RESOURCE_CREATION_FAIL;
        }
        else
        {
            bPktResponseMutexCreateSuccess = true;
        }
    }

    /* Configure the library. */
    if( cellularStatus == CELLULAR_SUCCESS )
    {
        cellularStatus = libOpen( pContext );
    }

    if( cellularStatus != CELLULAR_SUCCESS )
    {
        if( bPktResponseMutexCreateSuccess == true )
        {
            _Cellular_DestroyPktResponseMutex( pContext );
        }

        if( bPktRequestMutexCreateSuccess == true )
        {
            _Cellular_DestroyPktRequestMutex( pContext );
        }

        if( bAtDataMutexCreateSuccess == true )
        {
            _Cellular_DestroyAtDataMutex( pContext );
        }

        if( bLibStatusMutexCreateSuccess == true )
        {
            _Cellular_DestroyLibStatusMutex( pContext );
        }

        if( pContext != NULL )
        {
            _Cellular_FreeContext( pContext );
        }
    }
    else
    {
        *pCellularHandle = pContext;
    }

    return cellularStatus;
}