static CellularCommInterfaceError_t _prvCommIntfClose()

in source/cellular/comm_if_windows.c [645:714]


static CellularCommInterfaceError_t _prvCommIntfClose( CellularCommInterfaceHandle_t commInterfaceHandle )
{
    CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS;
    _cellularCommContext_t * pCellularCommContext = ( _cellularCommContext_t * ) commInterfaceHandle;
    HANDLE hComm = NULL;
    BOOL Status = TRUE;
    DWORD dwRes = 0;

    if( pCellularCommContext == NULL )
    {
        commIntRet = IOT_COMM_INTERFACE_FAILURE;
    }
    else if( ( pCellularCommContext->commStatus & CELLULAR_COMM_OPEN_BIT ) == 0 )
    {
        CellularLogError( "Cellular close comm interface is not opened before." );
        commIntRet = IOT_COMM_INTERFACE_FAILURE;
    }
    else
    {
        /* clean the receive callback. */
        pCellularCommContext->commReceiveCallback = NULL;

        /* Close the COM port. */
        hComm = pCellularCommContext->commFileHandle;

        if( hComm != ( HANDLE ) INVALID_HANDLE_VALUE )
        {
            Status = CloseHandle( hComm );

            if( Status == FALSE )
            {
                CellularLogDebug( "Cellular close CloseHandle %p fail", hComm );
                commIntRet = IOT_COMM_INTERFACE_FAILURE;
            }
        }
        else
        {
            commIntRet = IOT_COMM_INTERFACE_FAILURE;
        }

        pCellularCommContext->commFileHandle = NULL;

        /* Wait for the thread exit. */
        if( pCellularCommContext->commReceiveCallbackThread != NULL )
        {
            dwRes = WaitForSingleObject( pCellularCommContext->commReceiveCallbackThread, COMM_RECV_THREAD_TIMEOUT );

            if( dwRes != WAIT_OBJECT_0 )
            {
                CellularLogDebug( "Cellular close wait receiveCallbackThread %p fail %d",
                                  pCellularCommContext->commReceiveCallbackThread, dwRes );
                commIntRet = IOT_COMM_INTERFACE_FAILURE;
            }
            else
            {
                CloseHandle( pCellularCommContext->commReceiveCallbackThread );
            }
        }

        pCellularCommContext->commReceiveCallbackThread = NULL;

        /* Clean the commTaskThread. */
        ( void ) cleanCommTaskThread( pCellularCommContext );

        /* clean the data structure. */
        pCellularCommContext->commStatus &= ~( CELLULAR_COMM_OPEN_BIT );
    }

    return commIntRet;
}