CellularError_t Cellular_ActivatePdn()

in sim70x0/cellular_sim70x0_api.c [2096:2186]


CellularError_t Cellular_ActivatePdn( CellularHandle_t cellularHandle,
                                      uint8_t contextId )
{
    CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle;
    CellularError_t cellularStatus = CELLULAR_BAD_PARAMETER;
    CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;

    char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE * 2 ] = { '\0' }; /* APN & auth info is too long ...*/

    CellularAtReq_t atReqActPdn =
    {
        cmdBuf,
        CELLULAR_AT_NO_RESULT,
        NULL,
        NULL,
        NULL,
        0,
    };

    if( !IsValidPDP( contextId ) )
    {
        LogError( ( "ActivatePdn: contexId err: %d", contextId ) );
        goto err;
    }

    /* Make sure the library is open. */
    cellularStatus = _Cellular_CheckLibraryStatus( pContext );

    if( cellularStatus != CELLULAR_SUCCESS )
    {
        goto err;
    }

    cellularModuleContext_t * pSimContex = ( cellularModuleContext_t * ) pContext->pModueContext;

    if( pSimContex == NULL )
    {
        goto err;
    }

    if( pSimContex->pdnCfg.password && ( strlen( pSimContex->pdnCfg.password ) > 0 ) &&
        pSimContex->pdnCfg.username && ( strlen( pSimContex->pdnCfg.username ) > 0 ) &&
        ( pSimContex->pdnCfg.pdnAuthType > 0 ) )
    {
        ( void ) snprintf( cmdBuf, sizeof( cmdBuf ), "AT+CNCFG=%d,%d,\"%s\",\"%s\",\"%s\",%d",
                           contextId,
                           0, /* 0=Dual Stack, 1=IPV4, 2=IPV6*/
                           pSimContex->pdnCfg.apnName, pSimContex->pdnCfg.username, pSimContex->pdnCfg.password, pSimContex->pdnCfg.pdnAuthType );
    }
    else
    {
        ( void ) snprintf( cmdBuf, sizeof( cmdBuf ), "AT+CNCFG=%d,%d,\"%s\"",
                           contextId,
                           0, /* 0=Dual Stack, 1=IPV4, 2=IPV6*/
                           pSimContex->pdnCfg.apnName );
    }

    pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqActPdn );

    if( pktStatus != CELLULAR_PKT_STATUS_OK )
    {
        LogError( ( "can't set PDN, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) );
        cellularStatus = _Cellular_TranslatePktStatus( pktStatus );
    }

    if( cellularStatus == CELLULAR_SUCCESS )
    {
        xEventGroupClearBits( pSimContex->pdnEvent, CNACT_EVENT_BIT_ACT );
        ( void ) snprintf( cmdBuf, sizeof( cmdBuf ), "AT+CNACT=%d,1", contextId );

        pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqActPdn, PDN_ACTIVATION_PACKET_REQ_TIMEOUT_MS );

        if( pktStatus == CELLULAR_PKT_STATUS_OK )
        {
            LogDebug( ( "Cellular module wait Pdn-%d ...", contextId ) );

            xEventGroupWaitBits( pSimContex->pdnEvent, CNACT_EVENT_BIT_ACT, true, false,
                                 pdMS_TO_TICKS( PDN_ACTIVATION_PACKET_REQ_TIMEOUT_MS ) );

            LogInfo( ( "Cellular module wait Pdn-%d ready", contextId ) );
        }
        else
        {
            LogError( ( "Can't activate PDN, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) );
            cellularStatus = _Cellular_TranslatePktStatus( pktStatus );
        }
    }

err:
    return cellularStatus;
}