ModemDetect.cpp
上传用户:glass0516
上传日期:2010-01-11
资源大小:104k
文件大小:11k
- /*****************************************************************************
- * 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 "ModemDetect.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CModemDetect::CModemDetect()
- {
- pInfo = new FaxApiModemDetectMsg;
- pInfo->m_cbSize = sizeof(FaxApiModemDetectMsg);
- pInfo->m_bSuccessful = false;
- pInfo->m_bClass1 = false;
- pInfo->m_bClass1_0 = false;
- pInfo->m_bClass2 = false;
- pInfo->m_bClass2_0 = false;
- pInfo->m_bClass2_1 = false;
- pInfo->m_szClasses[0] = ' ';
- pInfo->m_szProductCode[0] = ' ';
- pInfo->m_szIDCode[0] = ' ';
- pInfo->m_szManufacturer[0] = ' ';
- pInfo->m_szModel[0] = ' ';
- ZeroMemory( pInfo->m_Class2Matrix, sizeof(bool)*FAXAPI_MAXPARAMETERS*FAXAPI_MAXPARAMVALUE );
- ZeroMemory( pInfo->m_Class20Matrix, sizeof(bool)*FAXAPI_MAXPARAMETERS*FAXAPI_MAXPARAMVALUE );
- m_nLoopCtr = 0;
- }
- CModemDetect::~CModemDetect()
- {
- }
- //////////////////////////////////////////////////////////////////////
- // OnConnect
- //////////////////////////////////////////////////////////////////////
- void CModemDetect::OnConnect(void)
- {
- SetState( STATE_INIT );
- m_bGotOK = false;
- SendCommand( COMMAND_INIT );
- m_nLoopCtr = 0;
- }
- //////////////////////////////////////////////////////////////////////
- // OnDisconnect
- //////////////////////////////////////////////////////////////////////
- bool CModemDetect::OnDisconnect(void)
- {
- return true;
- }
- //////////////////////////////////////////////////////////////////////
- // OnReadLine
- //////////////////////////////////////////////////////////////////////
- void CModemDetect::OnReadLine(void)
- {
- switch( m_nState )
- {
- case STATE_INIT:
- PhaseInit();
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // OnTimer
- //////////////////////////////////////////////////////////////////////
- void CModemDetect::OnTimer( UINT nID )
- {
- if( (nID == TIMER_COMMAND) && (m_nState == STATE_INIT) )
- {
- if( m_nLastCommand == COMMAND_INIT )
- {
- if( m_bGotOK )
- {
- m_nLoopCtr = 0;
- SendCommand( COMMAND_DISABLE_ECHO );
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- KillTimer( TIMER_COMMAND );
- m_sLastError.assign( (char*)LoadString(GEN_NO_RESPONSE_FROM_MODEM).c_str() );
- SignalEvent( EVENT_ERROR );
- OnDisconnectMsg();
- }
- else
- {
- SendCommand( m_nLastCommand, (char*) m_LastCommandString.c_str() );
- }
- }
- }
- else
- {
- m_sLastError.assign( (char*)LoadString(GEN_TIMEOUT_RESPONDING_TO_COMMAND).c_str() );
- SignalEvent( EVENT_ERROR );
- KillTimer( TIMER_COMMAND );
- OnDisconnectMsg();
- }
- }
- }
- //////////////////////////////////////////////////////////////////////
- // PhaseInit
- //////////////////////////////////////////////////////////////////////
- void CModemDetect::PhaseInit(void)
- {
- if( stricmp( m_szLineBuff, m_LastCommandString.c_str() ) == 0 )
- {
- // Echo - ignore
- return;
- }
- switch( m_nLastCommand )
- {
- case COMMAND_INIT:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- m_bGotOK = true;
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- ErrorUnexpectedResponse();
- KillTimer( TIMER_COMMAND );
- Quit( false );
- }
- }
- break;
- case COMMAND_DISABLE_ECHO:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- // send next command
- SendCommand( COMMAND_QUERY_PRODUCT_CODE );
- }
- else
- {
- ErrorUnexpectedResponse();
- Quit( false );
- }
- break;
- // most general stuff first
- case COMMAND_QUERY_PRODUCT_CODE:
- if( ( stricmp( m_szLineBuff, "OK" ) == 0 ) || ( stricmp( m_szLineBuff, "ERROR" ) == 0 ) )
- {
- // send next command
- SendCommand( COMMAND_QUERY_MANUFACTURER );
- }
- else
- {
- strncpy( pInfo->m_szProductCode, m_szLineBuff, FAXAPI_DETECTMSG_INFOLEN );
- }
- break;
- case COMMAND_QUERY_MANUFACTURER:
- if( ( stricmp( m_szLineBuff, "OK" ) == 0 ) || ( stricmp( m_szLineBuff, "ERROR" ) == 0 ) )
- {
- // send next command
- SendCommand( COMMAND_QUERY_FCLASS );
- }
- else
- {
- strncpy( pInfo->m_szIDCode, m_szLineBuff, FAXAPI_DETECTMSG_INFOLEN );
- }
- break;
- case COMMAND_QUERY_FCLASS:
- if( stricmp( m_szLineBuff, "ERROR" ) == 0 )
- {
- ErrorUnexpectedResponse();
- Quit( false );
- }
- else if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( pInfo->m_bClass1 )
- {
- SendCommand( COMMAND_SET_FCLASS_1 );
- }
- else if( pInfo->m_bClass2 )
- {
- SendCommand( COMMAND_SET_FCLASS_2 );
- }
- else if( pInfo->m_bClass2_0 )
- {
- SendCommand( COMMAND_SET_FCLASS_2_0 );
- }
- else
- {
- Quit( false );
- }
- }
- else
- {
- strncpy( pInfo->m_szClasses, m_szLineBuff, FAXAPI_DETECTMSG_INFOLEN );
- char WorkBuffer[512];
- WorkBuffer[0] = ' ';
- strncpy( WorkBuffer, pInfo->m_szClasses, FAXAPI_DETECTMSG_INFOLEN );
- WorkBuffer[511] = ' ';
- LPSTR lpTok = strtok( WorkBuffer, ",");
- while( lpTok )
- {
- if( strcmp( lpTok, "1" ) == 0 )
- {
- pInfo->m_bClass1 = true;
- }
- else if( strcmp( lpTok, "1.0" ) == 0 )
- {
- pInfo->m_bClass1_0 = true;
- }
- else if( strcmp( lpTok, "2" ) == 0 )
- {
- pInfo->m_bClass2 = true;
- }
- else if( strcmp( lpTok, "2.0" ) == 0 )
- {
- pInfo->m_bClass2_0 = true;
- }
- else if( strcmp( lpTok, "2.1" ) == 0 )
- {
- pInfo->m_bClass2_1 = true;
- }
- lpTok = strtok( NULL, "," );
- }
- }
- break;
- case COMMAND_SET_FCLASS_1:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- //OutputDebugString( "Supports class 1n" );
- }
- else
- {
- pInfo->m_bClass1 = false;
- }
- SendCommand( COMMAND_QUERY_SEND_SPEEDS );
- break;
- case COMMAND_QUERY_SEND_SPEEDS:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- SendCommand( COMMAND_QUERY_RECEIVE_SPEEDS );
- }
- else
- {
- ProcSupportedSpeeds( m_szLineBuff, true );
- pInfo->m_nSendBaudRate = m_nMaxSendBaud;
- }
- break;
- case COMMAND_QUERY_RECEIVE_SPEEDS:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- pInfo->m_bSuccessful = true;
- if( pInfo->m_bClass2 )
- {
- SendCommand( COMMAND_SET_FCLASS_2 );
- }
- else if( pInfo->m_bClass2_0 )
- {
- SendCommand( COMMAND_SET_FCLASS_2_0 );
- }
- else
- {
- Quit( true );
- }
- }
- else
- {
- ProcSupportedSpeeds( m_szLineBuff, false );
- pInfo->m_nRecvBaudRate = m_nMaxRecvBaud;
- }
- break;
- case COMMAND_SET_FCLASS_2:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- SendCommand( COMMAND_QUERY_CLASS2_CAP );
- }
- else
- {
- pInfo->m_bClass2 = false;
- if( pInfo->m_bClass2_0 )
- {
- SendCommand( COMMAND_SET_FCLASS_2_0 );
- }
- else
- {
- Quit( pInfo->m_bSuccessful );
- }
- }
- break;
- case COMMAND_QUERY_CLASS2_CAP:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- // send next command
- SendCommand( COMMAND_QUERY_CLASS2_MANUFACT );
- }
- else
- {
- ProcCapabilities( m_szLineBuff );
- memcpy( pInfo->m_Class2Matrix, m_ParamMatrix, sizeof(bool)*FAXAPI_MAXPARAMETERS*FAXAPI_MAXPARAMVALUE );
- }
- break;
- case COMMAND_QUERY_CLASS2_MANUFACT:
- if( ( stricmp( m_szLineBuff, "OK" ) == 0 ) || ( stricmp( m_szLineBuff, "ERROR" ) == 0 ) )
- {
- // send next command
- SendCommand( COMMAND_QUERY_CLASS2_MODEL );
- }
- else
- {
- if( pInfo->m_szManufacturer[0] == ' ' )
- {
- strncpy( pInfo->m_szManufacturer, m_szLineBuff, FAXAPI_DETECTMSG_INFOLEN );
- }
- }
- break;
- case COMMAND_QUERY_CLASS2_MODEL:
- if( ( stricmp( m_szLineBuff, "OK" ) == 0 ) || ( stricmp( m_szLineBuff, "ERROR" ) == 0 ) )
- {
- if( pInfo->m_bClass2_0 )
- {
- SendCommand( COMMAND_SET_FCLASS_2_0 );
- }
- else
- {
- Quit( pInfo->m_bSuccessful );
- }
- }
- else
- {
- if( pInfo->m_szModel[0] == ' ' )
- {
- strncpy( pInfo->m_szModel, m_szLineBuff, FAXAPI_DETECTMSG_INFOLEN );
- }
- }
- break;
- case COMMAND_SET_FCLASS_2_0:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- // class 2.0 supported
- SendCommand( COMMAND_QUERY_CLASS2_0_CAP );
- }
- else
- {
- pInfo->m_bClass2_0 = false;
- Quit( pInfo->m_bSuccessful );
- }
- break;
- case COMMAND_QUERY_CLASS2_0_CAP:
- if( ( stricmp( m_szLineBuff, "OK" ) == 0 ) || ( stricmp( m_szLineBuff, "ERROR" ) == 0 ) )
- {
- SendCommand( COMMAND_QUERY_CLASS2_0_MANUFACT );
- }
- else
- {
- ProcCapabilities( m_szLineBuff );
- memcpy( pInfo->m_Class20Matrix, m_ParamMatrix, sizeof(bool)*FAXAPI_MAXPARAMETERS*FAXAPI_MAXPARAMVALUE );
- }
- break;
- case COMMAND_QUERY_CLASS2_0_MANUFACT:
- if( ( stricmp( m_szLineBuff, "OK" ) == 0 ) || ( stricmp( m_szLineBuff, "ERROR" ) == 0 ) )
- {
- // send next command
- SendCommand( COMMAND_QUERY_CLASS2_0_MODEL );
- }
- else
- {
- if( pInfo->m_szManufacturer[0] == ' ' )
- {
- strncpy( pInfo->m_szManufacturer, m_szLineBuff, FAXAPI_DETECTMSG_INFOLEN );
- }
- }
- break;
- case COMMAND_QUERY_CLASS2_0_MODEL:
- if( ( stricmp( m_szLineBuff, "OK" ) == 0 ) || ( stricmp( m_szLineBuff, "ERROR" ) == 0 ) )
- {
- Quit( true );
- }
- else
- {
- if( pInfo->m_szModel[0] == ' ' )
- {
- strncpy( pInfo->m_szModel, m_szLineBuff, FAXAPI_DETECTMSG_INFOLEN );
- }
- }
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // Quit
- //////////////////////////////////////////////////////////////////////
- void CModemDetect::Quit( bool bSuccessful )
- {
- strncpy( pInfo->szID, m_sPort.c_str(), FAXAPI_MODEMMSG_IDLEN );
- m_bSuccessful = bSuccessful;
- if( bSuccessful )
- {
- if( pInfo->m_szModel[0] == ' ' )
- {
- if( pInfo->m_szProductCode[0] )
- {
- strncpy( pInfo->m_szModel, pInfo->m_szProductCode, FAXAPI_DETECTMSG_INFOLEN );
- }
- else if( pInfo->m_szIDCode[0] )
- {
- strncpy( pInfo->m_szModel, pInfo->m_szIDCode, FAXAPI_DETECTMSG_INFOLEN );
- }
- }
- if( pInfo->m_szManufacturer[0] == ' ' )
- {
- if( pInfo->m_szIDCode[0] )
- {
- strncpy( pInfo->m_szManufacturer, pInfo->m_szIDCode, FAXAPI_DETECTMSG_INFOLEN );
- }
- else if( pInfo->m_szProductCode[0] )
- {
- strncpy( pInfo->m_szManufacturer, pInfo->m_szProductCode, FAXAPI_DETECTMSG_INFOLEN );
- }
- }
- }
- // zero terminate since strncpy doesn't do this for us
- pInfo->m_szClasses[FAXAPI_DETECTMSG_INFOLEN-1] = ' ';
- pInfo->m_szProductCode[FAXAPI_DETECTMSG_INFOLEN-1] = ' ';
- pInfo->m_szIDCode[FAXAPI_DETECTMSG_INFOLEN-1] = ' ';
- pInfo->m_szManufacturer[FAXAPI_DETECTMSG_INFOLEN-1] = ' ';
- pInfo->m_szModel[FAXAPI_DETECTMSG_INFOLEN-1] = ' ';
- PostThreadMessage( m_FaxThreadID, WM_MODEM_MESSAGE, (WPARAM)pInfo, FAXAPI_EVENT_DETECT_FINISHED );
- PhaseDisconnect();
- }