FaxEngineCmd.cpp
上传用户:glass0516
上传日期:2010-01-11
资源大小:104k
文件大小:10k
- /*****************************************************************************
- * 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 "../src/FaxApi.h"
- HANDLE hStop = NULL;
- bool bUseClass2 = false;
- bool bUseClass20 = false;
- BOOL WINAPI ControlHandler ( DWORD dwCtrlType )
- {
- switch( dwCtrlType )
- {
- case CTRL_BREAK_EVENT: // use Ctrl+C or Ctrl+Break to simulate
- case CTRL_C_EVENT: // SERVICE_CONTROL_STOP in debug mode
- SetEvent( hStop );
- return TRUE;
- break;
- }
- return FALSE;
- }
- WORD Cls2ScanTimes_normal[8] = { 0, 5, 10, 10, 20, 20, 40, 40 };
- WORD Cls2ScanTimes_fine[8] = { 0, 5, 5, 10, 10, 20, 20, 40 };
- char* Cls2FaxParamResolutions[2] = { "Normal","Fine" };
- WORD Cls2FaxParamBitRates[6] = { 2400, 4800, 7200, 9600, 12000, 14400 };
- WORD Cls2FaxParamPageWidths[5] = { 1728, 2048, 2432, 1216, 864 };
- char* Cls2FaxParamPageLengths[3] = { "A4", "B4", "Unlimited" };
- char* Cls2FaxParamDataFormats[4] = { "1D", "2D", "Uncompressed", "T.6" };
- char* Cls2FaxParamErrorCheckings[3] = { "No", "Yes", "Yes" };
- void ShowParameters( FaxApiModemMsg* pMMsg )
- {
- printf( "(%d baud, w=%d, l=%s, %s, %s, ECM=%s, scan=%dms)n",
- Cls2FaxParamBitRates[pMMsg->p.BitRate],
- Cls2FaxParamPageWidths[pMMsg->p.PageWidth],
- Cls2FaxParamPageLengths[pMMsg->p.PageLength],
- Cls2FaxParamResolutions[pMMsg->p.VertRes],
- Cls2FaxParamDataFormats[pMMsg->p.DataFormat],
- Cls2FaxParamErrorCheckings[pMMsg->p.ECM],
- (pMMsg->p.VertRes == 0) ?
- Cls2ScanTimes_normal[pMMsg->p.ScanTime] :
- Cls2ScanTimes_fine[pMMsg->p.ScanTime] );
- }
- void ShowDetectionResults( FaxApiModemDetectMsg* pMsg )
- {
- printf( "Modem supports classes: %sn", pMsg->m_szClasses );
- printf( "Manufacturer: %sn", pMsg->m_szManufacturer );
- printf( "Model: %sn", pMsg->m_szModel );
- printf( "Product Code: %sn", pMsg->m_szProductCode );
- printf( "ID: %sn", pMsg->m_szIDCode );
- if( pMsg->m_bClass2 )
- {
- printf( "Class 2 capabilities: %s,%s,%sn",
- (pMsg->m_Class2Matrix[0][1]) ? "Fine" : "Normal",
- (pMsg->m_Class2Matrix[4][3]) ? "2D Group 4" : ( pMsg->m_Class2Matrix[4][1] ? "2D Group 3" : "1D Group 3" ),
- (pMsg->m_Class2Matrix[5][2]) ? "ECM" : "no ECM" );
- bUseClass2 = true;
- }
- if( pMsg->m_bClass2_0 )
- {
- printf( "Class 2.0 capabilities: %s,%s,%sn",
- (pMsg->m_Class20Matrix[0][1]) ? "Fine" : "Normal",
- (pMsg->m_Class20Matrix[4][3]) ? "2D Group 4" : ( pMsg->m_Class20Matrix[4][1] ? "2D Group 3" : "1D Group 3" ),
- (pMsg->m_Class20Matrix[5][1]) ? "ECM" : "no ECM" );
- bUseClass20 = true;
- }
- }
- int MessageLoop( void )
- {
- int ret = -1;
- MSG msg;
- while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
- {
- if( msg.message == FAXAPI_MESSAGE )
- {
- FaxApiModemMsg* pMsg = (FaxApiModemMsg*) msg.wParam;
-
- switch( msg.lParam )
- {
- case FAXAPI_EVENT_ERROR:
- printf( "Modem Error: %sn", pMsg->sz );
- break;
- case FAXAPI_EVENT_IDLE:
- printf( "Modem is idlen" );
- break;
- case FAXAPI_EVENT_RING:
- printf( "RINGn" );
- break;
- case FAXAPI_EVENT_START_RECV:
- printf( "Starting to receiven" );
- break;
- case FAXAPI_EVENT_START_SEND:
- printf( "Starting to sendn" );
- break;
- case FAXAPI_EVENT_RECV_DIS:
- printf( "Received DISn" );
- ShowParameters( pMsg );
- break;
- case FAXAPI_EVENT_SENT_DIS:
- printf( "Sent DISn" );
- ShowParameters( pMsg );
- break;
- case FAXAPI_EVENT_RECV_DCS:
- printf( "Received DCSn" );
- ShowParameters( pMsg );
- break;
- case FAXAPI_EVENT_SENT_DCS:
- printf( "Sent DCSn" );
- ShowParameters( pMsg );
- break;
- case FAXAPI_EVENT_START_TRAINING:
- printf( "Trainingn" );
- break;
- case FAXAPI_EVENT_START_PAGE:
- printf( "Start Page: %dn", pMsg->t.nPages + 1 );
- break;
- case FAXAPI_EVENT_PAGE_DATA:
- printf( "%d%%n", pMsg->sz[0] );
- break;
- case FAXAPI_EVENT_GOT_REMOTEID:
- printf( "Received ID: %sn", pMsg->sz );
- break;
- case FAXAPI_EVENT_TERMINATE:
- if( pMsg->t.nSuccessful == 1 )
- {
- printf( "Fax successful.n" );
- }
- else
- {
- printf( "Fax failed. %sn", pMsg->sz );
- }
- break;
- case FAXAPI_EVENT_DISCONNECT:
- printf( "Modem is disconnectedn" );
- break;
- case FAXAPI_EVENT_CALLERID:
- printf( "CallerID: %s n", pMsg->sz );
- break;
- case FAXAPI_EVENT_INFO:
- printf( "%sn", pMsg->sz );
- break;
- case FAXAPI_EVENT_DETECT_FINISHED:
- printf( "Detection finishedn" );
- ShowDetectionResults( (FaxApiModemDetectMsg*)msg.wParam );
- break;
- default:
- printf( "received event %dn", msg.lParam );
- break;
- }
- ret = msg.lParam;
- FaxApiDeleteMessage( &msg );
- }
- else
- {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- }
- return ret;
- }
- void printusage()
- {
- printf( "nNot enough parametersn" );
- printf( "usage:tFaxEngineCmd port command faxfile numbernn" );
- printf( "tport - com port, e.g.: COM1n" );
- printf( "tif command is send, you need both faxfile and numbern" );
- printf( "tif command is recv, you only need faxfilen" );
- printf( "tfaxfile - tiff file to read or createn" );
- }
- void ModemDetection( char* szPort )
- {
- printf( "nDetecting modem on %sn", szPort );
- FaxApiModem pDetect = FaxApiCreateModem( FAXAPI_DETECT );
- if( pDetect == NULL )
- {
- printf( "FaxApiCreateModem failedn" );
- return;
- }
- FaxApiEnableDebugLog( pDetect, true, "" );
- FaxApiSetCommParam( pDetect, 19200, 8, NOPARITY, ONESTOPBIT );
- FaxApiSetFlowControl( pDetect, false, true, true );
- FaxApiSetPort( pDetect, szPort );
- hStop = CreateEvent( NULL, TRUE, FALSE, NULL );
- FaxApiStartThread( pDetect, hStop, GetCurrentThreadId() );
- bool bRun = true;
- while( bRun )
- {
- DWORD dwResult = MsgWaitForMultipleObjects( 1, &hStop, FALSE, INFINITE, QS_ALLINPUT );
- if( dwResult == WAIT_OBJECT_0 )
- {
- // we don't need to tell the modem to exit, since hStop is the exit signal
- FaxApiWaitForModemToExit( pDetect );
- bRun = false;
- }
- else if( dwResult == WAIT_OBJECT_0 + 1 )
- {
- int nEvent = MessageLoop();
- while( nEvent >= 0 )
- {
- if( nEvent == FAXAPI_EVENT_DISCONNECT )
- {
- FaxApiWaitForModemToExit( pDetect );
- bRun = false;
- pDetect = NULL; // the modem will delete itself
- }
- nEvent = MessageLoop();
- }
- }
- else
- {
- printf( "MsgWaitForMultipleObjects returned %d LastError=%dn",
- dwResult, GetLastError() );
- bRun = false;
- }
- }
- }
- int main(int argc, char* argv[])
- {
- int nSendCopies = 1;
- SetConsoleCtrlHandler( ControlHandler, TRUE );
- printf( "RelayFax Open Source Project version 1.0.0n" );
- printf( "Copyright 1996-2004 Alt-N Technologies, Ltd.n" );
- printf( "All rights reserved.n" );
- if( argc < 4 )
- {
- printusage();
- return -1;
- }
- // get command-line parameters
- bool bSend = (strcmp( "send", argv[2] ) == 0);
- if( bSend && argc < 5 )
- {
- printusage();
- return -1;
- }
- printf( "nHit Ctrl-C to exitn" );
- ModemDetection( argv[1] );
- if( bSend )
- {
- printf( "Connecting to %s to send %s to %sn", argv[1], argv[3], argv[4] );
- }
- else
- {
- printf( "Connecting to %s to receive %sn", argv[1], argv[3] );
- }
- ResetEvent( hStop );
- FaxApiModem pModem;
-
- if( bUseClass20 )
- {
- pModem = FaxApiCreateModem( FAXAPI_CLASS_2_0 );
- }
- else if ( bUseClass2 )
- {
- pModem = FaxApiCreateModem( FAXAPI_CLASS_2 );
- }
- else
- {
- pModem = FaxApiCreateModem( FAXAPI_CLASS_1 );
- }
- if( pModem == NULL )
- {
- printf( "FaxApiCreateModem failedn" );
- return -1;
- }
- FaxApiEnableDebugLog( pModem, true, "" );
- FaxApiSetCommParam( pModem, 19200, 8, NOPARITY, ONESTOPBIT );
- FaxApiSetFlowControl( pModem, false, true, true );
- FaxApiSetPort( pModem, argv[1] );
- FaxApiSetInitString( pModem, "AT&F&C1&D2S7=55S0=0" );
- FaxApiSetSpkrParams( pModem, FAXAPI_SPKRVOL_LOW, FAXAPI_SPKRMODE_ON );
- FaxApiSetDistinctiveRing( pModem, "" );
- FaxApiSetSendEncoding( pModem, FAXAPI_ENC_CCITT_T6 );
- FaxApiSetSendECM( pModem, true );
- FaxApiSetSendFine( pModem, true );
- FaxApiSetSendUnlimited( pModem, true );
- FaxApiSetPulseDialing( pModem, false );
- FaxApiSetCSID( pModem, "FAXENGTEST" );
- FaxApiSetSendBaud( pModem, FAXAPI_BAUD_14400 );
- FaxApiSetRecvBaud( pModem, FAXAPI_BAUD_14400 );
- FaxApiStartThread( pModem, hStop, GetCurrentThreadId() );
- bool bRun = true;
- while( bRun )
- {
- DWORD dwResult = MsgWaitForMultipleObjects( 1, &hStop, FALSE, INFINITE, QS_ALLINPUT );
- if( dwResult == WAIT_OBJECT_0 )
- {
- // we don't need to tell the modem to exit, since hStop is the exit signal
- FaxApiWaitForModemToExit( pModem );
- bRun = false;
- }
- else if( dwResult == WAIT_OBJECT_0 + 1 )
- {
- int nEvent = MessageLoop();
- while( nEvent >= 0 )
- {
- if( nEvent == FAXAPI_EVENT_IDLE )
- {
- if( bSend && nSendCopies > 0 )
- {
- FaxApiSendFax( pModem, argv[4], argv[3] );
- nSendCopies--;
- }
- }
- if( nEvent == FAXAPI_EVENT_RING )
- {
- if( !bSend )
- {
- FaxApiReceiveFax( pModem, argv[3] );
- }
- }
- if( nEvent == FAXAPI_EVENT_DISCONNECT )
- {
- FaxApiWaitForModemToExit( pModem );
- bRun = false;
- pModem = NULL; // the modem will delete itself
- }
- nEvent = MessageLoop();
- }
- }
- else
- {
- printf( "MsgWaitForMultipleObjects returned %d LastError=%dn",
- dwResult, GetLastError() );
- bRun = false;
- }
- }
- MessageLoop();
- printf( "RelayFax Open Source Fax Engine exittingn" );
- return 0;
- }