FaxAPI.cpp
上传用户:glass0516
上传日期:2010-01-11
资源大小:104k
文件大小:17k
- /*****************************************************************************
- * RelayFax Open Source Project
- * Copyright 1996-2004 Alt-N Technologies, Ltd.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted only as authorized by the RelayFax Open
- * Source License. A copy of this license is available in file LICENSE
- * in the top-level directory of the distribution.
- *
- * RelayFax is a registered trademark of Alt-N Technologies, Ltd.
- *
- * Individual files and/or contributed packages may be copyright by
- * other parties and subject to additional restrictions.
- *****************************************************************************/
- #include "stdafx.h"
- #include "FaxAPI.h"
- #include "ModemDetect.h"
- #include "ClassOne.h"
- #include "ClassOnePointZero.h"
- #include "ClassTwo.h"
- #include "ClassTwoPointZero.h"
- #include "ClassTwoPointOne.h"
- HINSTANCE g_Instance = NULL;
- //////////////////////////////////////////////////////////////////////
- // TiffWarningHandler
- //////////////////////////////////////////////////////////////////////
- static void TiffWarningHandler(const char* module, const char* fmt, va_list ap)
- {
- #ifdef DEBUG
- char buf[256] = {0};
- _vsnprintf( buf, sizeof(buf) - 1, fmt, ap);
- OutputDebugString( "Warning: " );
- OutputDebugString( buf );
- OutputDebugString( "n" );
- #endif
- }
- //////////////////////////////////////////////////////////////////////
- // TiffErrorHandler
- //////////////////////////////////////////////////////////////////////
- static void TiffErrorHandler(const char* module, const char* fmt, va_list ap)
- {
- #ifdef DEBUG
- char buf[256] = {0};
- _vsnprintf( buf, sizeof(buf) - 1, fmt, ap);
- OutputDebugString( "Error: " );
- OutputDebugString( buf );
- OutputDebugString( "n" );
- #endif
- }
- //////////////////////////////////////////////////////////////////////
- // DllMain
- //////////////////////////////////////////////////////////////////////
- BOOL APIENTRY DllMain( HANDLE hModule,
- DWORD dwReason,
- LPVOID lpReserved
- )
- {
- switch (dwReason)
- {
- case DLL_PROCESS_ATTACH:
- g_Instance = (HINSTANCE)hModule;
- CHiddenWindow::RegisterWindowClass( g_Instance );
- // set tiff error handlers
- TIFFSetErrorHandler( TiffErrorHandler );
- TIFFSetWarningHandler( TiffWarningHandler );
- break;
- case DLL_PROCESS_DETACH:
- CHiddenWindow::UnRegisterWindowClass();
- break;
- }
- return TRUE;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiGetModem - internal function
- //////////////////////////////////////////////////////////////////////
- CModem* FaxApiGetModem( FaxApiModem pModem )
- {
- // check the obvious
- if( pModem == NULL )
- return NULL;
- // and the not so obvious
- if( IsBadWritePtr( pModem, sizeof(CModem) ) )
- {
- return NULL;
- }
- // cast it
- return (CModem*) pModem;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiCreateModem
- //////////////////////////////////////////////////////////////////////
- FaxApiModem FAXAPI_CALL FaxApiCreateModem( int nClass )
- {
- FaxApiModem pModem = NULL;
- CModem* pMdm;
- switch( nClass )
- {
- case FAXAPI_DETECT:
- pMdm = new CModemDetect;
- break;
- case FAXAPI_CLASS_1:
- pMdm = new CClassOne;
- break;
- case FAXAPI_CLASS_1_0:
- pMdm = new CClassOnePointZero;
- break;
- case FAXAPI_CLASS_2:
- pMdm = new CClassTwo;
- break;
- case FAXAPI_CLASS_2_0:
- pMdm = new CClassTwoPointZero;
- break;
- case FAXAPI_CLASS_2_1:
- pMdm = new CClassTwoPointOne;
- break;
- default:
- pMdm = NULL;
- break;
- }
- if( pMdm )
- {
- pModem = pMdm;
- }
- return pModem;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiDeleteMessage
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiDeleteMessage( MSG* pMsg )
- {
- // todo: check pMsg->cbm_cbSize
- if( pMsg == NULL )
- {
- return FAXAPI_ERROR_BAD_MSG;
- }
- if( pMsg->lParam == NULL )
- {
- return FAXAPI_ERROR_BAD_MSG;
- }
- switch( pMsg->lParam )
- {
- case FAXAPI_EVENT_DETECT_FINISHED:
- {
- FaxApiModemDetectMsg* pMMsg = (FaxApiModemDetectMsg*)pMsg->wParam;
- if( IsBadWritePtr( pMMsg, sizeof(FaxApiModemDetectMsg) ) )
- {
- return FAXAPI_ERROR_BAD_MSG;
- }
-
- delete pMMsg;
- }
- break;
- default:
- {
- FaxApiModemMsg* pMMsg = (FaxApiModemMsg*)pMsg->wParam;
-
- if( IsBadWritePtr( pMMsg, sizeof(FaxApiModemMsg) ) )
- {
- return FAXAPI_ERROR_BAD_MSG;
- }
-
- delete pMMsg;
- }
- }
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetCommParam
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetCommParam( FaxApiModem pModem, DWORD BaudRate, BYTE ByteSize, BYTE Parity, BYTE StopBits )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetCommParam( BaudRate, ByteSize, Parity, StopBits );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetFlowControl
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetFlowControl( FaxApiModem pModem, bool bDSRFlowControl, bool bCTSFlowControl, bool bSoftFlowControl )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetFlowControl( bDSRFlowControl, bCTSFlowControl, bSoftFlowControl );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetPort
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetPort( FaxApiModem pModem, char* szPort )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetPort( szPort );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetSpkrParams
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetSpkrParams( FaxApiModem pModem, int nSpkrVol, int nSpkrMode )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetSpkrParams( nSpkrVol, nSpkrMode );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetDistinctiveRing
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetDistinctiveRing( FaxApiModem pModem, LPCSTR szRingCodes )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetDistinctiveRing( szRingCodes );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetInitString
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetInitString( FaxApiModem pModem, LPCSTR szString )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetInitString( szString );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetSendEncoding
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetSendEncoding( FaxApiModem pModem, int nEncoding )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetSendEncoding( nEncoding );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetSendECM
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetSendECM( FaxApiModem pModem, bool bECMSupported )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetSendECM( bECMSupported );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetSendFine
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetSendFine( FaxApiModem pModem, bool bFineSupported )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetSendFine( bFineSupported );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetSendUnlimited
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetSendUnlimited( FaxApiModem pModem, bool bUnlimitedSupported )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetSendUnlimited( bUnlimitedSupported );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetPulseDialing
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetPulseDialing( FaxApiModem pModem, bool bPulseDialing )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetPulseDialing( bPulseDialing );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiEnableDebugLog
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiEnableDebugLog( FaxApiModem pModem, bool bEnable, char* LogDirectory )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->EnableDebugLog( bEnable, LogDirectory );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiEnableDebugLog
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetMaxPageRetries( FaxApiModem pModem, int nRetries )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetMaxPageRetries( nRetries );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetCSID
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetCSID( FaxApiModem pModem, LPCSTR szString )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- pMdm->ChangeCSID( szString );
- return FAXAPI_SUCCESS;
- }
- pMdm->SetCSID( szString );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetSendBaud
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetSendBaud( FaxApiModem pModem, int nBaud )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetSendBaud( nBaud );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSetRecvBaud
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiSetRecvBaud( FaxApiModem pModem, int nBaud )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->SetRecvBaud( nBaud );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiStartThread
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiStartThread( FaxApiModem pModem, HANDLE hStop, DWORD faxThreadID )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() )
- {
- // can't change the port once the thread is started.
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- pMdm->Initialize( hStop, faxThreadID );
- return FAXAPI_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiWaitForModemToExit
- //////////////////////////////////////////////////////////////////////
- int FAXAPI_CALL FaxApiWaitForModemToExit( FaxApiModem pModem )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- if( pMdm->ThreadStarted() == false)
- {
- // can't wait for the modem if isn't started
- return FAXAPI_ERROR_THREAD_STARTED;
- }
- DWORD dwResult = WaitForSingleObject( pMdm->GetHandle(), 5000 );
- if( dwResult == WAIT_OBJECT_0 )
- {
- return FAXAPI_SUCCESS;
- }
- else
- {
- return FAXAPI_ERROR_BAD_MODEM;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiSendFax
- //////////////////////////////////////////////////////////////////////
- bool FAXAPI_CALL FaxApiSendFax( FaxApiModem pModem, char* szNumberToDial, char* szFaxFile )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return false;
- }
- if( pMdm->ThreadStarted() == false)
- {
- // can't wait for the modem if isn't started
- return false;
- }
- return pMdm->SendFax( szNumberToDial, szFaxFile );
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiReceiveFax
- //////////////////////////////////////////////////////////////////////
- void FAXAPI_CALL FaxApiReceiveFax( FaxApiModem pModem, char* szFaxFile )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return;
- }
- if( pMdm->ThreadStarted() == false)
- {
- // can't wait for the modem if isn't started
- return;
- }
- pMdm->RecvFax( szFaxFile );
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiAbortFax
- //////////////////////////////////////////////////////////////////////
- void FAXAPI_CALL FaxApiAbortFax( FaxApiModem pModem )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return;
- }
- if( pMdm->ThreadStarted() == false)
- {
- // can't abort fax if the modem isn't started
- return;
- }
- pMdm->AbortFax();
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiDisconnect
- //////////////////////////////////////////////////////////////////////
- void FAXAPI_CALL FaxApiDisconnect( FaxApiModem pModem )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return;
- }
- if( pMdm->ThreadStarted() == false)
- {
- // can't abort fax if the modem isn't started
- return;
- }
- pMdm->Disconnect();
- }
- //////////////////////////////////////////////////////////////////////
- // FaxApiClearRingCount
- //////////////////////////////////////////////////////////////////////
- void FAXAPI_CALL FaxApiClearRingCount( FaxApiModem pModem )
- {
- CModem* pMdm = FaxApiGetModem( pModem );
- if( pMdm == NULL )
- {
- return;
- }
- if( pMdm->ThreadStarted() == false)
- {
- return;
- }
- pMdm->ClearRingCount();
- }