⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faxenginecmd.cpp

📁 fax engine 传真引擎 relay fax 的开源项目 商业软件使用 高质量 高可靠
💻 CPP
字号:
/*****************************************************************************
* 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: %s\n", pMsg->m_szClasses );
	printf( "Manufacturer: %s\n", pMsg->m_szManufacturer );
	printf( "Model: %s\n", pMsg->m_szModel );
	printf( "Product Code: %s\n", pMsg->m_szProductCode );
	printf( "ID: %s\n", pMsg->m_szIDCode );

	if( pMsg->m_bClass2 )
	{
		printf( "Class 2 capabilities: %s,%s,%s\n", 
				 (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,%s\n",
				 (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: %s\n", pMsg->sz );
				break;

			case FAXAPI_EVENT_IDLE:
				printf( "Modem is idle\n" );
				break;

			case FAXAPI_EVENT_RING:
				printf( "RING\n" );
				break;

			case FAXAPI_EVENT_START_RECV:
				printf( "Starting to receive\n" );
				break;

			case FAXAPI_EVENT_START_SEND:
				printf( "Starting to send\n" );
				break;

			case FAXAPI_EVENT_RECV_DIS:
				printf( "Received DIS\n" );
				ShowParameters( pMsg );
				break;

			case FAXAPI_EVENT_SENT_DIS:
				printf( "Sent DIS\n" );
				ShowParameters( pMsg );
				break;

			case FAXAPI_EVENT_RECV_DCS:
				printf( "Received DCS\n" );
				ShowParameters( pMsg );
				break;

			case FAXAPI_EVENT_SENT_DCS:
				printf( "Sent DCS\n" );
				ShowParameters( pMsg );
				break;

			case FAXAPI_EVENT_START_TRAINING:
				printf( "Training\n" );
				break;

			case FAXAPI_EVENT_START_PAGE:
				printf( "Start Page: %d\n", 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: %s\n", pMsg->sz );
				break;

			case FAXAPI_EVENT_TERMINATE:
				if( pMsg->t.nSuccessful == 1 )
				{
					printf( "Fax successful.\n" );
				}
				else
				{
					printf( "Fax failed. %s\n", pMsg->sz );
				}
				break;

			case FAXAPI_EVENT_DISCONNECT:
				printf( "Modem is disconnected\n" );
				break;

			case FAXAPI_EVENT_CALLERID:
				printf( "CallerID: %s \n", pMsg->sz );
				break;

			case FAXAPI_EVENT_INFO:
				printf( "%s\n", pMsg->sz );
				break;

			case FAXAPI_EVENT_DETECT_FINISHED:
				printf( "Detection finished\n" );
				ShowDetectionResults( (FaxApiModemDetectMsg*)msg.wParam );
				break;

			default:
				printf( "received event %d\n", msg.lParam );
				break;
			}

			ret = msg.lParam;

			FaxApiDeleteMessage( &msg );
		}
		else
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
	}

	return ret;
}

void printusage()
{
	printf( "\nNot enough parameters\n" );
	printf( "usage:\tFaxEngineCmd port command faxfile number\n\n" );
	printf( "\tport - com port, e.g.: COM1\n" );
	printf( "\tif command is send, you need both faxfile and number\n" );
	printf( "\tif command is recv, you only need faxfile\n" );
	printf( "\tfaxfile - tiff file to read or create\n" ); 
}


void ModemDetection( char* szPort )
{
 	printf( "\nDetecting modem on %s\n", szPort );

	FaxApiModem pDetect = FaxApiCreateModem( FAXAPI_DETECT );

	if( pDetect == NULL )
	{
		printf( "FaxApiCreateModem failed\n" );
		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=%d\n", 
					  dwResult, GetLastError() );
			bRun = false;
		}
	}
}

int main(int argc, char* argv[])
{
	int nSendCopies = 1;

	SetConsoleCtrlHandler( ControlHandler, TRUE );

	printf( "RelayFax Open Source Project version 1.0.0\n" );
	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 exit\n" );

	ModemDetection( argv[1] );

	if( bSend )
	{
		printf( "Connecting to %s to send %s to %s\n", argv[1], argv[3], argv[4] );
	}
	else
	{
		printf( "Connecting to %s to receive %s\n", 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 failed\n" );
		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=%d\n", 
					  dwResult, GetLastError() );
			bRun = false;
		}
	}

	MessageLoop();

	printf( "RelayFax Open Source Fax Engine exitting\n" );

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -