static CellularCommInterfaceError_t _prvCommIntfSend()

in source/cellular/comm_if_windows.c [718:812]


static CellularCommInterfaceError_t _prvCommIntfSend( CellularCommInterfaceHandle_t commInterfaceHandle,
                                                      const uint8_t * pData,
                                                      uint32_t dataLength,
                                                      uint32_t timeoutMilliseconds,
                                                      uint32_t * pDataSentLength )
{
    CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS;
    _cellularCommContext_t * pCellularCommContext = ( _cellularCommContext_t * ) commInterfaceHandle;
    HANDLE hComm = NULL;
    OVERLAPPED osWrite = { 0 };
    DWORD dwRes = 0;
    DWORD dwWritten = 0;
    BOOL Status = TRUE;

    if( pCellularCommContext == NULL )
    {
        commIntRet = IOT_COMM_INTERFACE_BAD_PARAMETER;
    }
    else if( ( pCellularCommContext->commStatus & CELLULAR_COMM_OPEN_BIT ) == 0 )
    {
        CellularLogError( "Cellular send comm interface is not opened before." );
        commIntRet = IOT_COMM_INTERFACE_FAILURE;
    }
    else
    {
        hComm = pCellularCommContext->commFileHandle;
        osWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

        if( osWrite.hEvent == NULL )
        {
            CellularLogError( "Cellular CreateEvent fail %d", GetLastError() );
            commIntRet = IOT_COMM_INTERFACE_FAILURE;
        }
    }

    if( commIntRet == IOT_COMM_INTERFACE_SUCCESS )
    {
        Status = WriteFile( hComm, pData, dataLength, &dwWritten, &osWrite );

        /* WriteFile fail and error is not the ERROR_IO_PENDING. */
        if( ( Status == FALSE ) && ( GetLastError() != ERROR_IO_PENDING ) )
        {
            CellularLogError( "Cellular WriteFile fail %d", GetLastError() );
            commIntRet = IOT_COMM_INTERFACE_FAILURE;
        }

        if( Status == TRUE )
        {
            *pDataSentLength = ( uint32_t ) dwWritten;
        }
    }

    /* Handle pending I/O. */
    if( ( commIntRet == IOT_COMM_INTERFACE_SUCCESS ) && ( Status == FALSE ) )
    {
        dwRes = WaitForSingleObject( osWrite.hEvent, timeoutMilliseconds );

        switch( dwRes )
        {
            case WAIT_OBJECT_0:

                if( GetOverlappedResult( hComm, &osWrite, &dwWritten, FALSE ) == FALSE )
                {
                    CellularLogError( "Cellular GetOverlappedResult fail %d", GetLastError() );
                    commIntRet = IOT_COMM_INTERFACE_FAILURE;
                }

                break;

            case STATUS_TIMEOUT:
                CellularLogError( "Cellular WaitForSingleObject timeout" );
                commIntRet = IOT_COMM_INTERFACE_TIMEOUT;
                break;

            default:
                CellularLogError( "Cellular WaitForSingleObject fail %d", dwRes );
                commIntRet = IOT_COMM_INTERFACE_FAILURE;
                break;
        }

        *pDataSentLength = ( uint32_t ) dwWritten;
    }

    if( osWrite.hEvent != NULL )
    {
        Status = CloseHandle( osWrite.hEvent );

        if( Status == FALSE )
        {
            CellularLogDebug( "Cellular send CloseHandle fail" );
        }
    }

    return commIntRet;
}