ClassOne.cpp
上传用户:glass0516
上传日期:2010-01-11
资源大小:104k
文件大小:80k
- /*****************************************************************************
- * 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.
- *****************************************************************************/
- ////////////////////////////////////////////////////////////////////////////////
- //
- // The purpose of CClassOne is contain the protocol specifics of EIA
- // fax modem class 1
- //
- ////////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "ClassOne.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CClassOne::CClassOne()
- {
- m_sEIAClass = "1";
- }
- CClassOne::~CClassOne()
- {
- }
- void CClassOne::OnConnect(void)
- {
- SetState( STATE_INIT );
- SendCommand( COMMAND_INIT );
- m_nLoopCtr = 0;
- }
- bool CClassOne::OnDisconnect(void)
- {
- switch( m_nState )
- {
- case STATE_INIT:
- SignalEvent( EVENT_ERROR );
- return true;
- case STATE_PHASE_A:
- case STATE_PHASE_B:
- case STATE_PHASE_C:
- case STATE_PHASE_D:
- case STATE_PHASE_E:
- Abort( true );
- return false;
- }
- return true;
- }
- //////////////////////////////////////////////////////////////////////
- // OnWrite
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnWrite(void)
- {
- m_dwActivityTimer = GetTickCount();
- if( m_nState == STATE_PHASE_C && !m_bReceiving )
- {
- m_nPageBytes += m_BytesWritten;
- int nPercent = 100 * m_nPageBytes / m_FaxFile.GetPageBytes();
- if( nPercent > 100 )
- nPercent = 100;
- SignalEvent( EVENT_PAGE_DATA, nPercent );
- }
- }
- //////////////////////////////////////////////////////////////////////
- // OnReadLine
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnReadLine(void)
- {
- //char szMsg[256];
- //wsprintf( szMsg, "CClassOne::OnReadLine: %s %dn", m_szLineBuff, m_nLineBuffPtr );
- //OutputDebugString( szMsg );
- switch( m_nState )
- {
- case STATE_IDLE:
- PhaseIdle();
- break;
- case STATE_INIT:
- PhaseInit();
- break;
- case STATE_PHASE_A:
- PhaseA();
- break;
- case STATE_PHASE_B:
- PhaseB();
- break;
- case STATE_PHASE_C:
- PhaseC();
- break;
- case STATE_PHASE_D:
- PhaseD();
- break;
- case STATE_PHASE_E:
- PhaseE();
- break;
- case STATE_RINGING:
- PhaseRinging();
- break;
- case STATE_DISCONNECT:
- PhaseDisconnect();
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // PhaseInit
- //////////////////////////////////////////////////////////////////////
- void CClassOne::PhaseInit(void)
- {
- if( stricmp( m_szLineBuff, m_LastCommandString.c_str() ) == 0 )
- {
- // Echo - ignore
- return;
- }
- if( IsRing() )
- {
- // RING
- m_nRingCount++;
- return;
- }
- switch( m_nLastCommand )
- {
- case COMMAND_INIT:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- m_bGotOK = true;
- // m_nLoopCtr = 0;
- // SendCommand( COMMAND_SETUP_STRING );
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- ErrorUnexpectedResponse();
- KillTimer( TIMER_COMMAND );
- OnDisconnectMsg();
- }
- }
- break;
- case COMMAND_SETUP_STRING:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- SendCommand( COMMAND_DISABLE_ECHO );
- }
- else
- {
- ErrorUnexpectedResponse();
- //SetState( STATE_IDLE );
- SendCommand( COMMAND_DISABLE_ECHO );
- }
- break;
- case COMMAND_DISABLE_ECHO:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- SendCommand( COMMAND_SET_SPKR_VOL );
- }
- else
- {
- ErrorUnexpectedResponse();
- SendCommand( COMMAND_SET_SPKR_VOL );
- }
- break;
- case COMMAND_SET_SPKR_VOL:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- SendCommand( COMMAND_SET_SPKR_MODE );
- }
- else
- {
- ErrorUnexpectedResponse();
- SendCommand( COMMAND_SET_SPKR_MODE );
- }
- break;
- case COMMAND_SET_SPKR_MODE:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- SendCommand( COMMAND_SET_FCLASS_1 );
- }
- else
- {
- ErrorUnexpectedResponse();
- SendCommand( COMMAND_SET_FCLASS_1 );
- }
- break;
- case COMMAND_SET_FCLASS_1:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- SendCommand( COMMAND_QUERY_SEND_SPEEDS );
- }
- else
- {
- ErrorUnexpectedResponse();
- KillTimer( TIMER_COMMAND );
- OnDisconnectMsg();
- }
- break;
- case COMMAND_QUERY_SEND_SPEEDS:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- SendCommand( COMMAND_QUERY_RECEIVE_SPEEDS );
- }
- else
- {
- ProcSupportedSpeeds( m_szLineBuff, true );
- }
- break;
- case COMMAND_QUERY_RECEIVE_SPEEDS:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- KillTimer( TIMER_COMMAND );
- SetState( STATE_IDLE );
- }
- else
- {
- ProcSupportedSpeeds( m_szLineBuff, false );
- }
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // PhaseIdle
- //////////////////////////////////////////////////////////////////////
- void CClassOne::PhaseIdle(void)
- {
- if( IsRing() )
- {
- m_nRingCount = 1;
- SetState( STATE_RINGING );
- SignalEvent( EVENT_RING, m_nRingCount );
- }
- }
- //////////////////////////////////////////////////////////////////////
- // Phase Ringing
- //////////////////////////////////////////////////////////////////////
- void CClassOne::PhaseRinging(void)
- {
- if( IsRing() )
- {
- m_nRingCount++;
- SignalEvent( EVENT_RING, m_nRingCount );
- /*
- if( OkToAnswer() )
- {
- m_bReceiving = true;
- m_bSuccessful = false;
- SendCommand( COMMAND_ANSWER );
- SetState( STATE_PHASE_A );
- }
- */
- }
- else if( strnicmp( m_szLineBuff, "CONNECT", 7 ) == 0 )
- {
- m_FaxFile.SetSendEncoding( m_nSendEncoding );
- m_bReceiving = true;
- m_bSuccessful = false;
- m_nLoopCtr = 0;
- m_bGotDCN = false;
- SignalEvent( EVENT_START_RECV );
- SendID(CSI);
- SetState( STATE_PHASE_B, STATE_SEND_DIS );
- }
- else
- {
- SignalEvent( EVENT_CALLERID );
- }
- }
- //////////////////////////////////////////////////////////////////////
- // Phase A - call setup
- //////////////////////////////////////////////////////////////////////
- void CClassOne::PhaseA(void)
- {
- if( strnicmp( m_szLineBuff, "CONNECT", 7 ) == 0 )
- {
- m_nLoopCtr = 0;
- m_bGotDCN = false;
- if( m_bReceiving )
- {
- SignalEvent( EVENT_START_RECV );
- SendID(CSI);
- SetState( STATE_PHASE_B, STATE_SEND_DIS );
- }
- else
- {
- SignalEvent( EVENT_START_SEND );
- m_bGotDIS = false;
- m_bGotConnect = false;
- InitHDLC();
- SetState( STATE_PHASE_B, STATE_WAIT_FOR_DIS );
- }
- return;
- }
- else
- {
- ErrorConnectResponse();
- Terminate();
- PhaseDisconnect();
- }
- }
- //////////////////////////////////////////////////////////////////////
- // Phase B - negotiation and training
- //////////////////////////////////////////////////////////////////////
- void CClassOne::PhaseB(void)
- {
- switch( m_nPhaseState )
- {
- // begin sending states
- case STATE_WAIT_FOR_DIS:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotDCN )
- {
- m_sLastError.assign( (char*)LoadString(GEN_UNEXPECTED_DCN_RECEIVED).c_str() );
- Terminate();
- DoHangup();
- }
- else if( m_bFinalHDLCFrame && m_bGotDIS )
- {
- m_nLoopCtr = 0;
- EnableSoftFlowControl( true );
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_TIS;
- }
- else
- {
- // ask for another
- if( ++m_nLoopCtr >= 6 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_DIS_NOT_RECEIVED).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- RecvHDLCFrame();
- }
- }
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- // get ready for another HDLC frame
- m_bGotConnect = true;
- InitHDLC();
- KillTimer( TIMER_HDLC_RECV );
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_DIS_NOT_RECEIVED).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- RecvHDLCFrame();
- }
- }
- break;
- case STATE_SEND_TIS:
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- SendID(TSI);
- m_nPhaseState = STATE_SEND_DCS;
- m_bGotConnect = false;
- }
- else
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_SENDING_TSI).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- break;
- case STATE_SEND_DCS:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- // Send 75ms of silence
- SendFormattedCommand( "FTS", 8 );
- m_nPhaseState = STATE_SEND_SILENCE_BEFORE_TRAINING;
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- m_bGotConnect = true;
- SendDCS();
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_SENDING_DCS).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SendHDLCFrame();
- }
- }
- break;
- case STATE_SEND_SILENCE_BEFORE_TRAINING:
- //if( stricmp( m_szLineBuff, "OK" ) != 0 )
- //{
- // OutputDebugString( "Error sending silencen" );
- //}
- SendLongTraining();
- // change state
- m_nPhaseState = STATE_SEND_TRAINING_DATA;
- SignalEvent( EVENT_START_TRAINING );
- break;
- case STATE_SEND_TRAINING_DATA:
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- SendTraining();
- m_nPhaseState = STATE_SEND_TRAINING;
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_SENDING_TRAINING_DATA).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_DCS;
- m_bGotConnect = false;
- }
- }
- break;
- case STATE_SEND_TRAINING:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- m_bGotCFR = false;
- m_bGotCRP = false;
- //OutputDebugString( "Requesting CFRn" );
- EnableSoftFlowControl( false );
- RecvHDLCFrame();
- // change state
- m_nPhaseState = STATE_WAIT_FOR_CFR;
- m_bFinalHDLCFrame = false;
- char szEvMsg[FAXAPI_MODEMMSG_INFOLEN];
- wsprintf( szEvMsg, (char*)LoadString(GEN_SENT_TRAINING_AT_BAUD).c_str(), cls1Speeds[m_nClass1Speed].dwSpeed );
- SignalEventString( EVENT_INFO, szEvMsg );
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- if( m_WriteQueue.size() == 0 )
- {
- // Send another end of data in case it got lost
- //OutputDebugString( "Sending extra <DLE><ETX>n" );
- unsigned char Frame[2];
- Frame[0] = DLE; // <dle>
- Frame[1] = ETX; // <etx>
- DoWrite( (char*)Frame, 2, false );
- }
- }
- else if( m_szLineBuff[0] == XOFF )
- {
- // Ignore
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_IN_TRAINING).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_DCS;
- m_bGotConnect = false;
- }
- }
- break;
- case STATE_WAIT_FOR_CFR:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotDCN )
- {
- Terminate();
- DoHangup();
- }
- else if( m_bGotCRP )
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_DCS;
- m_bGotConnect = false;
- }
- else if( m_bFinalHDLCFrame )
- {
- m_nLoopCtr = 0;
- EnableSoftFlowControl( true );
- if( m_bGotCFR )
- {
- // Send 75ms of silence
- SendFormattedCommand( "FTS", 8 );
- m_nPhaseState = STATE_SEND_SILENCE_BEFORE_DATA;
- m_nPPRCtr = 0;
- if( m_FaxFile.ReadPage( m_bECM, m_nClass1Speed, m_wMinLineChars ) == false )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_READING_TIFF_FILE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- }
- else
- {
- if( m_nClass1Speed == 0 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_IN_TRAINING).c_str() );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- // decrement speed and retrain
- DecrementSpeed();
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_DCS;
- m_bGotConnect = false;
- }
- }
- }
- else if( m_bRecvHDLCError )
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_DCS_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_DCS;
- m_bGotConnect = false;
- }
- }
- else
- {
- RecvHDLCFrame();
- }
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- m_bGotConnect = true;
- // get ready for another HDLC frame
- InitHDLC();
- KillTimer( TIMER_HDLC_RECV );
- }
- else if ( m_szLineBuff[0] == DLE && m_szLineBuff[1] == ETX )
- {
- m_bRecvHDLCError = true;
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_DCS_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_DCS;
- m_bGotConnect = false;
- }
- }
- break;
- case STATE_SEND_SILENCE_BEFORE_DATA:
- //if( stricmp( m_szLineBuff, "OK" ) != 0 )
- //{
- // OutputDebugString( "Error sending silencen" );
- //}
- SendShortTraining();
- // change state
- SetState( STATE_PHASE_C );
- break;
- // begin receiving states
- case STATE_SEND_CSI:
- if( strnicmp( m_szLineBuff, "CONNECT", 7 ) == 0 )
- {
- SendID(CSI);
- m_nPhaseState = STATE_SEND_DIS;
- }
- else if ( m_szLineBuff[0] == DLE && m_szLineBuff[1] == ETX )
- {
- // ignore
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_SENDING_CSI).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SendHDLCFrame();
- }
- }
- break;
- case STATE_SEND_DIS:
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- //OutputDebugString( "Sending DISn" );
- SendDIS();
- m_bFinalHDLCFrame = false;
- m_bGotDCS = false;
- m_bGotCRP = false;
- m_nPhaseState = STATE_DIS_SENT;
- EnableSoftFlowControl( false );
- }
- else if ( m_szLineBuff[0] == DLE && m_szLineBuff[1] == ETX )
- {
- // ignore
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_SENDING_DIS).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_CSI;
- }
- }
- break;
- case STATE_DIS_SENT:
- //OutputDebugString( "Waiting for DCSn" );
- RecvHDLCFrame();
- m_nPhaseState = STATE_WAIT_FOR_DCS;
- m_bGotConnect = false;
- break;
- case STATE_WAIT_FOR_DCS:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotDCN )
- {
- m_sLastError.assign( (char*)LoadString(GEN_UNEXPECTED_DCN_RECEIVED).c_str() );
- Terminate();
- DoHangup();
- }
- else if( m_bFinalHDLCFrame )
- {
- if( m_bGotDCS )
- {
- m_dwTrainingStart = GetTickCount();
- SetTimer( TIMER_TRAINING, 4000 );
- m_nPhaseState = STATE_RECEIVE_TRAINING_DATA;
- RecvLongTraining();
- SignalEvent( EVENT_START_TRAINING );
- }
- else if( m_bGotCRP )
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_CSI;
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_DIS_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- m_nPhaseState = STATE_SEND_CSI;
- SendHDLCFrame();
- }
- }
- }
- else if( m_bRecvHDLCError )
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_DIS_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- m_nPhaseState = STATE_SEND_CSI;
- SendHDLCFrame();
- }
- }
- else
- {
- // ask for another
- //OutputDebugString( "Waiting for DCSn" );
- RecvHDLCFrame();
- }
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- // get ready for another HDLC frame
- m_bGotConnect = true;
- InitHDLC();
- KillTimer( TIMER_HDLC_RECV );
- }
- else if ( m_szLineBuff[0] == DLE && m_szLineBuff[1] == ETX )
- {
- m_bRecvHDLCError = true;
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_DIS_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- m_nPhaseState = STATE_SEND_CSI;
- SendHDLCFrame();
- }
- }
- break;
- case STATE_RECEIVE_TRAINING_DATA:
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- KillTimer( TIMER_TRAINING );
- InitHDLC();
- m_nPhaseState = STATE_RECEIVE_TRAINING;
- m_nCorrect = 0;
- }
- else
- {
- // Wait 1.5 seconds so that the transmitter gets our FTT
- int nTimeLeft = (1500 - (GetTickCount() - m_dwTrainingStart));
- if( nTimeLeft > 0 )
- {
- // try the receive command again
- RecvLongTraining();
- }
- else
- {
- KillTimer( TIMER_TRAINING );
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_CFR;
- }
- }
- break;
- case STATE_RECEIVE_TRAINING:
- if( stricmp( m_szLineBuff, "OK" ) == 0 ||
- stricmp( m_szLineBuff, "NO CARRIER" ) == 0 )
- {
- // send CFR
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_CFR;
- }
- else
- {
- // Wait 1.5 seconds so that the transmitter gets our FTT
- int nDelay = (1500 - (GetTickCount() - m_dwTrainingStart));
- if( nDelay > 0 )
- {
- Sleep( nDelay );
- }
- //OutputDebugString( "Error receiving training datan" );
- // send CFR
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_CFR;
- }
- break;
- case STATE_SEND_CFR:
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- // send CFR
- if( m_nCorrect > 70 )
- {
- SendCFR();
- m_nPhaseState = STATE_SEND_RECV_DATA;
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_CFR).c_str() );
- }
- else
- {
- SendFTT();
- m_bFinalHDLCFrame = false;
- m_bRecvHDLCError = false;
- m_nPhaseState = STATE_SENT_FTT;
- EnableSoftFlowControl( false );
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_FTT).c_str() );
- }
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_DIS_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- m_nPhaseState = STATE_SEND_CSI;
- SendHDLCFrame();
- }
- }
- break;
- case STATE_SEND_RECV_DATA:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- //OutputDebugString( "Going to phase Cn" );
- RecvShortTraining();
- SetState( STATE_PHASE_C );
- m_FaxFile.WriteFileHeader();
- m_nPPRCtr = 0;
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_DIS_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- m_nPhaseState = STATE_SEND_CSI;
- SendHDLCFrame();
- }
- }
- break;
- case STATE_SENT_FTT:
- RecvHDLCFrame();
- m_nPhaseState = STATE_SENT_FTT_WAIT_FOR_DCS;
- break;
- case STATE_SENT_FTT_WAIT_FOR_DCS:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotDCN )
- {
- m_sLastError.assign( (char*)LoadString(GEN_UNEXPECTED_DCN_RECEIVED).c_str() );
- Terminate();
- DoHangup();
- }
- else if( m_bFinalHDLCFrame )
- {
- if( m_bGotDCS )
- {
- m_nLoopCtr = 0;
- SetTimer( TIMER_TRAINING, 4000 );
- m_nPhaseState = STATE_RECEIVE_TRAINING_DATA;
- RecvLongTraining();
- SignalEvent( EVENT_START_TRAINING );
- }
- else if( m_bGotCRP )
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_CFR;
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_FTT_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- m_nPhaseState = STATE_SEND_CFR;
- SendHDLCFrame();
- }
- }
- }
- else if( m_bRecvHDLCError )
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_FTT_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- m_nPhaseState = STATE_SEND_CFR;
- SendHDLCFrame();
- }
- }
- else
- {
- // ask for another
- //OutputDebugString( "Waiting for DCSn" );
- RecvHDLCFrame();
- }
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- // get ready for another HDLC frame
- m_bGotConnect = true;
- InitHDLC();
- KillTimer( TIMER_HDLC_RECV );
- }
- else if ( m_szLineBuff[0] == DLE && m_szLineBuff[1] == ETX )
- {
- m_bRecvHDLCError = true;
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_FTT_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- m_nPhaseState = STATE_SEND_CFR;
- SendHDLCFrame();
- }
- }
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // Phase C - transmit/receive data
- //////////////////////////////////////////////////////////////////////
- void CClassOne::PhaseC(void)
- {
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- if( m_bReceiving )
- {
- InitHDLC();
- }
- else
- {
- m_nPageBytes = 0;
- DoWrite( m_FaxFile.GetPageData(), m_FaxFile.GetPageBytes(), false );
- SetMaxPageRetriesTimer();
- }
- SignalEvent( EVENT_START_PAGE );
- }
- else if( stricmp( m_szLineBuff, "+FCERROR" ) == 0 )
- {
- if( m_bReceiving )
- {
- RecvShortTraining();
- }
- else
- {
- SendShortTraining();
- }
- }
- else /*if( stricmp( m_szLineBuff, "OK" ) == 0 ||
- stricmp( m_szLineBuff, "NO CARRIER" ) == 0) */
- {
- m_nLoopCtr = 0;
- if( m_bReceiving )
- {
- if( !m_bECM )
- {
- m_bLastPageGood = m_FaxFile.WriteIFD();
- }
- m_bGotEOP = false;
- m_bGotMPS = false;
- m_bGotEOM = false;
- m_bFinalHDLCFrame = false;
- RecvHDLCFrame();
- m_dwEOPWaitStart = GetTickCount();
- SetState( STATE_PHASE_D, STATE_WAIT_FOR_EOP );
- }
- else
- {
- // Send 75ms of silence
- SendFormattedCommand( "FTS", 8 );
- SetState( STATE_PHASE_D, STATE_SEND_SILENCE_BEFORE_PAGE_FINISHED );
- }
- }
- }
- //////////////////////////////////////////////////////////////////////
- // Phase D - end of page
- //////////////////////////////////////////////////////////////////////
- void CClassOne::PhaseD(void)
- {
- switch( m_nPhaseState )
- {
- // begin sending states
- case STATE_SEND_SILENCE_BEFORE_PAGE_FINISHED:
- //if( stricmp( m_szLineBuff, "OK" ) != 0 )
- //{
- // OutputDebugString( "Error sending silencen" );
- //}
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_PAGE_FINISHED;
- break;
- case STATE_SEND_PAGE_FINISHED:
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- SendEOP();
- m_nPhaseState = STATE_PAGE_FINISHED_SENT;
- m_bGotPPR = false;
- m_bFinalHDLCFrame = false;
- m_bGotMCF = false;
- m_bGotRNR = false;
- m_bGotCRP = false;
- m_bGotRTP = false;
- m_bGotRTN = false;
- m_dwRNRStart = 0;
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_PPS_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- //OutputDebugString( "Error sending EOPn" );
- SendHDLCFrame();
- }
- }
- break;
- case STATE_PAGE_FINISHED_SENT:
- m_nPhaseState = STATE_WAIT_FOR_MCF;
- EnableSoftFlowControl( false );
- RecvHDLCFrame();
- break;
- case STATE_WAIT_FOR_MCF:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotDCN )
- {
- m_sLastError.assign( (char*)LoadString(GEN_UNEXPECTED_DCN_RECEIVED).c_str() );
- Terminate();
- DoHangup();
- }
- else if( m_bFinalHDLCFrame && (m_bGotMCF || m_bGotPPR) )
- {
- EnableSoftFlowControl( true );
- if( m_FaxFile.MorePages() == false && m_bGotPPR == false && m_FaxFile.GetPartialPage() == false )
- {
- m_FaxFile.IncrementPageCount();
- m_bSuccessful = true;
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- KillTimer( TIMER_MAXPAGERETRIES );
- }
- else
- {
- if( m_bGotPPR )
- {
- if( m_nPPRCtr > 3 )
- {
- m_nPPRCtr = 0;
- m_nPhaseState = STATE_SEND_CTC;
- DecrementSpeed();
- SendHDLCFrame();
- }
- else
- {
- //OutputDebugString( "Sending bad framesn" );
- m_FaxFile.ReadBadFrames( m_nClass1Speed );
- // Send 75ms of silence
- SendFormattedCommand( "FTS", 8 );
- m_nPhaseState = STATE_SEND_SILENCE_BEFORE_PHASE_C;
- }
- }
- else if ( m_FaxFile.GetPartialPage() )
- {
- // Send 75ms of silence
- SendFormattedCommand( "FTS", 8 );
- m_nPhaseState = STATE_SEND_SILENCE_BEFORE_PHASE_C;
- //OutputDebugString( "Sending next blockn" );
- m_nPPRCtr = 0;
- if( m_FaxFile.ReadPage( m_bECM, m_nClass1Speed, m_wMinLineChars ) == false )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_READING_TIFF_FILE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- }
- else
- {
- // Send 75ms of silence
- SendFormattedCommand( "FTS", 8 );
- m_nPhaseState = STATE_SEND_SILENCE_BEFORE_PHASE_C;
- m_nPPRCtr = 0;
- if( m_FaxFile.ReadNextHeader() == false )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_READING_TIFF_FILE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- if( m_FaxFile.ReadPage( m_bECM, m_nClass1Speed, m_wMinLineChars ) == false )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_READING_TIFF_FILE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- }
- }
- }
- }
- else if( m_bFinalHDLCFrame && m_bGotCRP )
- {
- // repeat last command
- m_nPhaseState = STATE_SEND_PAGE_FINISHED;
- SendHDLCFrame();
- }
- else if( m_bFinalHDLCFrame && m_bGotRNR )
- {
- if( (GetTickCount() - m_dwRNRStart) > 60000 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_T5_TIMEOUT).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- // receiver not ready
- m_nPhaseState = STATE_SEND_RR;
- SendHDLCFrame();
- }
- }
- else if( m_bFinalHDLCFrame && m_bGotRTP )
- {
- EnableSoftFlowControl( true );
- if( m_FaxFile.MorePages() == false && m_FaxFile.GetPartialPage() == false )
- {
- m_FaxFile.IncrementPageCount();
- m_bSuccessful = true;
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- KillTimer( TIMER_MAXPAGERETRIES );
- }
- else
- {
- if ( m_FaxFile.GetPartialPage() )
- {
- //OutputDebugString( "Sending next blockn" );
- m_nPPRCtr = 0;
- if( m_FaxFile.ReadPage( m_bECM, m_nClass1Speed, m_wMinLineChars ) == false )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_READING_TIFF_FILE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- }
- else
- {
- //OutputDebugString( "Sending next pagen" );
- m_nPPRCtr = 0;
- if( m_FaxFile.ReadNextHeader() == false )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_READING_TIFF_FILE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- if( m_FaxFile.ReadPage( m_bECM, m_nClass1Speed, m_wMinLineChars ) == false )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_READING_TIFF_FILE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- }
- }
- SendFormattedCommand( "FTS", 8 );
- SetState( STATE_PHASE_B, STATE_SEND_SILENCE_BEFORE_TRAINING );
- // decrement speed and retrain
- //DecrementSpeed();
- //SendHDLCFrame();
- //SetState( STATE_PHASE_B, STATE_SEND_DCS );
- //m_bGotConnect = false;
- }
- }
- else if( m_bFinalHDLCFrame && m_bGotRTN )
- {
- SendFormattedCommand( "FTS", 8 );
- SetState( STATE_PHASE_B, STATE_SEND_SILENCE_BEFORE_TRAINING );
- //DecrementSpeed();
- //SendHDLCFrame();
- //SetState( STATE_PHASE_B, STATE_SEND_DCS );
- //m_bGotConnect = false;
- }
- else if( m_bRecvHDLCError )
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_PPS_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_PAGE_FINISHED;
- }
- }
- else
- {
- RecvHDLCFrame();
- }
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- m_bGotConnect = true;
- // get ready for another HDLC frame
- InitHDLC();
- KillTimer( TIMER_HDLC_RECV );
- }
- else if ( m_szLineBuff[0] == DLE && m_szLineBuff[1] == ETX )
- {
- m_bRecvHDLCError = true;
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_SENT_PPS_3_TIMES_WITHOUT_RESPONSE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_PAGE_FINISHED;
- }
- }
- break;
- case STATE_SEND_RR:
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- SendSimpleHDLCFrame( RR );
- m_nPhaseState = STATE_PAGE_FINISHED_SENT;
- m_bGotPPR = false;
- m_bFinalHDLCFrame = false;
- m_bGotMCF = false;
- m_bGotRNR = false;
- m_bGotCRP = false;
- }
- else
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_PAGE_FINISHED;
- }
- break;
- case STATE_SEND_CTC:
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- // This is not a simple frame
- SendCTC();
- m_nPhaseState = STATE_CTC_SENT;
- m_bGotCRP = false;
- m_bFinalHDLCFrame = false;
- }
- else
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_PAGE_FINISHED;
- }
- break;
- case STATE_CTC_SENT:
- m_nPhaseState = STATE_WAIT_FOR_CTR;
- RecvHDLCFrame();
- break;
- case STATE_WAIT_FOR_CTR:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotDCN )
- {
- m_sLastError.assign( (char*)LoadString(GEN_UNEXPECTED_DCN_RECEIVED).c_str() );
- Terminate();
- DoHangup();
- }
- else if( m_bGotCRP )
- {
- m_nPhaseState = STATE_SEND_CTC;
- SendHDLCFrame();
- }
- else if( m_bFinalHDLCFrame )
- {
- //OutputDebugString( "Sending bad framesn" );
- m_FaxFile.ReadBadFrames( m_nClass1Speed );
- // Send 75ms of silence
- SendFormattedCommand( "FTS", 8 );
- m_nPhaseState = STATE_SEND_SILENCE_BEFORE_PHASE_C;
- }
- else if( m_bRecvHDLCError )
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_PAGE_FINISHED;
- }
- else
- {
- // ask for
- //OutputDebugString( "Waiting for CTRn" );
- EnableSoftFlowControl( false );
- RecvHDLCFrame();
- }
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- m_bGotConnect = true;
- // get ready for another HDLC frame
- InitHDLC();
- KillTimer( TIMER_HDLC_RECV );
- }
- else if ( m_szLineBuff[0] == DLE && m_szLineBuff[1] == ETX )
- {
- m_bRecvHDLCError = true;
- }
- else
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_PAGE_FINISHED;
- }
- break;
- case STATE_SEND_SILENCE_BEFORE_PHASE_C:
- //if( stricmp( m_szLineBuff, "OK" ) != 0 )
- //{
- // OutputDebugString( "Error sending silencen" );
- //}
- SendShortTraining();
- // change state
- SetState( STATE_PHASE_C );
- //OutputDebugString( "Going to Phase Cn" );
- break;
- // begin receiving states
- case STATE_WAIT_FOR_EOP:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotDCN )
- {
- m_sLastError.assign( (char*)LoadString(GEN_UNEXPECTED_DCN_RECEIVED).c_str() );
- Terminate();
- DoHangup();
- }
- else if( m_bFinalHDLCFrame )
- {
- m_nLoopCtr = 0;
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_MCF;
- m_bSendPPR = false;
- if( m_bECM )
- {
- if( m_FaxFile.IsECMBlockGood( m_ECMFrameCount ) )
- {
- m_nPPRCtr = 0;
- m_FaxFile.NextECMBlock();
- if( m_bGotEOP || m_bGotEOM || m_bGotMPS )
- {
- m_FaxFile.WriteIFD();
- }
-
- // Can't send RTN in ECM mode
- m_bLastPageGood = true;
- }
- else
- {
- m_FaxFile.RedoECMBlock();
- m_bSendPPR = true;
- m_nPPRCtr++;
- }
- }
- }
- else
- {
- if( GetTickCount() - m_dwEOPWaitStart > 30000 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_PPS_NOT_RECEIVED).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- // ask for
- //OutputDebugString( "Waiting for EOPn" );
- //EnableSoftFlowControl( false );
- RecvHDLCFrame();
- }
- }
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- m_bGotConnect = true;
- // get ready for another HDLC frame
- InitHDLC();
- KillTimer( TIMER_HDLC_RECV );
- }
- else if ( m_szLineBuff[0] == DLE && m_szLineBuff[1] == ETX )
- {
- m_bRecvHDLCError = true;
- }
- else
- {
- if( GetTickCount() - m_dwEOPWaitStart > 30000 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_PPS_NOT_RECEIVED).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- //OutputDebugString( "Waiting for post-page signaln" );
- RecvHDLCFrame();
- }
- }
- break;
- case STATE_SEND_MCF:
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- SendMCF();
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_SENDING_MCF).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- //OutputDebugString( "Error sending MCFn" );
- SendHDLCFrame();
- }
- }
- break;
- case STATE_MCF_SENT:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotEOP && !m_bSendPPR )
- {
- m_bSuccessful = true;
- //OutputDebugString( "Going to phase En" );
- m_bFinalHDLCFrame = false;
- RecvHDLCFrame();
- SetState( STATE_PHASE_E, STATE_RECEIVE_DCN );
- //SignalEvent( EVENT_END_PAGE );
- }
- else if( m_bGotEOM && !m_bSendPPR )
- {
- SendHDLCFrame();
- SetState( STATE_PHASE_B, STATE_SEND_CSI );
- m_nLoopCtr = 0;
- //SignalEvent( EVENT_END_PAGE );
- }
- else if( m_bSendPPR && m_nPPRCtr > 3 )
- {
- //OutputDebugString( "Waiting for CTCn" );
- m_bFinalHDLCFrame = false;
- RecvHDLCFrame();
- m_nPhaseState = STATE_WAIT_FOR_CTC;
- m_bGotConnect = false;
- m_bGotCTC = false;
- m_bGotEOR = false;
- m_bGotCRP = false;
- }
- else /* default is to go back to phase C */
- {
- //OutputDebugString( "Going back to phase Cn" );
- RecvShortTraining();
- SetState( STATE_PHASE_C );
- }
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_SENDING_MCF).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- //OutputDebugString( "Error sending MCFn" );
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_MCF;
- }
- }
- break;
- case STATE_WAIT_FOR_CTC:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotDCN )
- {
- m_sLastError.assign( (char*)LoadString(GEN_UNEXPECTED_DCN_RECEIVED).c_str() );
- Terminate();
- DoHangup();
- }
- else if( m_bGotCRP )
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_MCF;
- }
- else if( m_bFinalHDLCFrame && (m_bGotCTC || m_bGotEOR) )
- {
- m_nPPRCtr = 0;
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_CTR;
- m_nLoopCtr = 0;
- m_bGotConnect = false;
- }
- else
- {
- // ask for
- //OutputDebugString( "Waiting for CTCn" );
- //EnableSoftFlowControl( false );
- RecvHDLCFrame();
- }
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- m_bGotConnect = true;
- // get ready for another HDLC frame
- InitHDLC();
- KillTimer( TIMER_HDLC_RECV );
- }
- else if ( m_szLineBuff[0] == DLE && m_szLineBuff[1] == ETX )
- {
- m_bRecvHDLCError = true;
- }
- else
- {
- //OutputDebugString( "Bad HDLC framen" );
- RecvHDLCFrame();
- }
- break;
- case STATE_SEND_CTR:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotCTC )
- {
- //OutputDebugString( "Going back to phase Cn" );
- RecvShortTraining();
- SetState( STATE_PHASE_C );
- }
- else // The only alternative is EOR was received
- {
- m_nPPRCtr = 0;
- m_FaxFile.NextECMBlock();
- if( m_bGotEOP || m_bGotEOM || m_bGotMPS )
- {
- m_FaxFile.WriteIFD();
- }
- // Can't send RTN in ECM mode
- m_bLastPageGood = true;
- if( m_bGotEOP )
- {
- m_bSuccessful = true;
- //OutputDebugString( "Going to phase En" );
- m_bFinalHDLCFrame = false;
- RecvHDLCFrame();
- SetState( STATE_PHASE_E, STATE_RECEIVE_DCN );
- }
- else if( m_bGotEOM )
- {
- SendHDLCFrame();
- SetState( STATE_PHASE_B, STATE_SEND_CSI );
- m_nLoopCtr = 0;
- }
- else /* default is to go back to phase C */
- {
- //OutputDebugString( "Going back to phase Cn" );
- RecvShortTraining();
- SetState( STATE_PHASE_C );
- }
- }
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- m_bGotConnect = true;
- SendCTR();
- }
- else
- {
- if( ++m_nLoopCtr >= 3 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_SENDING_CTR).c_str() );
- // SignalEvent( EVENT_ERROR );
- SendHDLCFrame();
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- //OutputDebugString( "Error sending CTRn" );
- SendHDLCFrame();
- }
- }
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // Phase E - Disconnect
- //////////////////////////////////////////////////////////////////////
- void CClassOne::PhaseE(void)
- {
- switch( m_nPhaseState )
- {
- case STATE_WAIT_FOR_OK:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- SendHDLCFrame();
- m_nPhaseState = STATE_SEND_DCN;
- }
- break;
- case STATE_SEND_DCN:
- if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- SendDCN();
- m_nPhaseState = STATE_DCN_SENT;
- }
- else /* if( stricmp( m_szLineBuff, "OK" ) == 0 ) */
- {
- Terminate();
- DoHangup();
- }
- break;
- case STATE_DCN_SENT:
- Terminate();
- DoHangup();
- break;
- case STATE_RECEIVE_DCN:
- if( stricmp( m_szLineBuff, "OK" ) == 0 )
- {
- if( m_bGotDCN )
- {
- Terminate();
- DoHangup();
- }
- else
- {
- // ask for
- //OutputDebugString( "Waiting for DCNn" );
- //EnableSoftFlowControl( false );
- //RecvHDLCFrame();
- Terminate();
- DoHangup();
- }
- }
- else if( stricmp( m_szLineBuff, "CONNECT" ) == 0 )
- {
- m_bGotConnect = true;
- // get ready for another HDLC frame
- InitHDLC();
- KillTimer( TIMER_HDLC_RECV );
- }
- else if ( m_szLineBuff[0] == DLE && m_szLineBuff[1] == ETX )
- {
- m_bRecvHDLCError = true;
- }
- else
- {
- DoHangup();
- Terminate();
- }
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // on DIS frame
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnDIS(void)
- {
- BOOL bSupported[MAX_CLS1SPEEDS];
- int i;
- m_bGotDIS = true;
- for( i = 0; i < MAX_CLS1SPEEDS; i++ )
- {
- bSupported[i] = 0;
- }
- //OutputDebugString( "OnDISn" );
- BYTE c1 = CFaxFile::s_FlipTable[ m_szHDLCBuff[4] ];
- BYTE c2 = CFaxFile::s_FlipTable[ m_szHDLCBuff[5] ];
- WORD wBitRate = (c1 & 0x3c) >> 2;
- WORD wScanTime = (c2 & 0x0E) >> 1;
- m_DISParams.p.VertRes = (c1 & 0x02) >> 1;
- WORD wWidth = (c2 & 0xc0) >> 6;
- WORD wLength = (c2 & 0x30) >> 4;
- m_b2D = (c1 & 0x01);
- switch( wBitRate )
- {
- case DIS_V27_FALLBACK:
- //OutputDebugString( "V.27 ter fallback supportedn" );
- bSupported[0] = 1;
- m_DISParams.p.BitRate = 0;
- break;
- case DIS_V27:
- //OutputDebugString( "V.27 ter supportedn" );
- bSupported[0] = 1;
- bSupported[1] = 1;
- m_DISParams.p.BitRate = 1;
- break;
- case DIS_V29:
- //OutputDebugString( "V.29 supportedn" );
- bSupported[2] = 1;
- bSupported[3] = 1;
- m_DISParams.p.BitRate = 3;
- break;
- case DIS_V27_V29:
- //OutputDebugString( "V.27 ter and V.29 supportedn" );
- for( i = 0; i < 4; i++ )
- bSupported[i] = 1;
- m_DISParams.p.BitRate = 3;
- break;
- case DIS_V27_V29_V33:
- //OutputDebugString( "V.27 ter, V.29, and V.33 supportedn" );
- for( i = 0; i < 6; i++ )
- bSupported[i] = 1;
- m_DISParams.p.BitRate = 5;
- break;
- case DIS_V27_V29_V33_V17:
- //OutputDebugString( "V.27 ter, V.29, V.33, and V.17 supportedn" );
- for( i = 0; i < 10; i++ )
- bSupported[i] = 1;
- m_DISParams.p.BitRate = 5;
- break;
- }
- //char jbuf[256];
- for( i = 9; i >= 0; i-- )
- if( bSupported[i] && m_bSendSupported[i] && m_nSendBaud >= cls1Speeds[i].ndx )
- break;
- if( i >= 0 ) {
- m_nClass1Speed = i;
- }
- else
- {
- m_nClass1Speed = 0;
- }
- if (!m_DISParams.p.VertRes)
- {
- //OutputDebugString( "Normal resolution supportedn" );
- m_wScanTimeMs = Cls1ScanTimes_normal[wScanTime];
- }
- else
- {
- //OutputDebugString( "Fine resolution supportedn" );
- m_wScanTimeMs = Cls1ScanTimes_fine[wScanTime];
- }
- switch( m_wScanTimeMs )
- {
- case 0:
- m_DISParams.p.ScanTime = 0;
- break;
- case 5:
- m_DISParams.p.ScanTime = 1;
- break;
- case 10:
- m_DISParams.p.ScanTime = 3;
- break;
- case 20:
- m_DISParams.p.ScanTime = 5;
- break;
- case 40:
- m_DISParams.p.ScanTime = 7;
- break;
- }
- //wsprintf( jbuf, "Minimum Scan Time: %dmsn", m_wScanTimeMs );
- //OutputDebugString( jbuf );
- m_DISParams.p.DataFormat = FAXAPI_ENC_CCITT_1D;
- if( m_b2D )
- {
- //OutputDebugString( "Receiver supports 2D encodingn" );
- m_DISParams.p.DataFormat = FAXAPI_ENC_CCITT_2D;
- }
- m_DISParams.p.PageLength = 0;
- switch( wLength )
- {
- case 0:
- //OutputDebugString( "Receiver supports A4 lengthn" );
- break;
- case 1:
- //OutputDebugString( "Receiver supports unlimited lengthn" );
- m_DISParams.p.PageLength = 2;
- break;
- case 2:
- //OutputDebugString( "Receiver supports A4 and B4 lengthn" );
- break;
- case 3:
- //OutputDebugString( "Invalid lengthn" );
- break;
- }
- switch( wWidth )
- {
- case 0:
- //OutputDebugString( "Receiver supports 1728 widthn" );
- m_DISParams.p.PageWidth = 0;
- break;
- case 2:
- //OutputDebugString( "Receiver supports 1728 or 2048 widthn" );
- m_DISParams.p.PageWidth = 1;
- break;
- default:
- //OutputDebugString( "Receiver supports 1728, 2048, or 2432 widthn" );
- m_DISParams.p.PageWidth = 2;
- break;
- }
- m_DISParams.p.ECM = 0;
- m_bECM = false;
- m_bT6Encoding = false;
- if( (c2 & 0x01) && // extension bit
- m_nHDLCBuffPtr > 6 )
- {
- BYTE c3 = CFaxFile::s_FlipTable[ m_szHDLCBuff[6] ];
- //if( c3 & 0x80 )
- //{
- // OutputDebugString( "Receiver supports 2400bps handshakingn" );
- //}
- //if( c3 & 0x40 )
- //{
- // OutputDebugString( "Receiver supports uncompressed moden" );
- //}
- if( c3 & 0x20 )
- {
- m_bECM = true;
- if( c3 & 0x10 )
- {
- m_FaxFile.SetECMFrameSize( 64 );
- //OutputDebugString( "Receiver supports ECM w/ 64 octet framesn" );
- m_DISParams.p.ECM = 1;
- }
- else
- {
- m_FaxFile.SetECMFrameSize( 256 );
- //OutputDebugString( "Receiver supports ECM w/ 256 octet framesn" );
- m_DISParams.p.ECM = 2;
- }
- }
- //if( c3 & 0x08 )
- //{
- // OutputDebugString( "Receiver supports error limiting moden" );
- //}
- //if( c3 & 0x04 )
- //{
- // OutputDebugString( "Receiver supports G4 on PSTNn" );
- //}
- if( c3 & 0x02 )
- {
- //OutputDebugString( "Receiver supports T.6 encodingn" );
- m_bT6Encoding = true;
- m_DISParams.p.DataFormat = FAXAPI_ENC_CCITT_T6;
- }
- if( (c3 & 0x01) && // extension bit
- m_nHDLCBuffPtr > 7 )
- {
- BYTE c4 = CFaxFile::s_FlipTable[ m_szHDLCBuff[7] ];
- // width expansion byte
- if( (c4 & 0x01) && // extension bit
- m_nHDLCBuffPtr > 8 )
- {
- BYTE c5 = CFaxFile::s_FlipTable[ m_szHDLCBuff[8] ];
- // more recent extensions
- }
- }
- }
- switch( m_FaxFile.GetSendEncoding() )
- {
- case (FAXAPI_ENC_CCITT_2D):
- if( m_b2D )
- {
- m_FaxFile.SelectEncoding( m_FaxFile.GetSendEncoding() );
- }
- else
- {
- m_FaxFile.SelectEncoding( FAXAPI_ENC_CCITT_1D );
- }
- break;
- case (FAXAPI_ENC_CCITT_T6):
- if( m_b2D && m_bT6Encoding && m_bECM && m_bECMSupported )
- {
- m_FaxFile.SelectEncoding( m_FaxFile.GetSendEncoding() );
- }
- else if( m_b2D )
- {
- m_FaxFile.SelectEncoding( FAXAPI_ENC_CCITT_2D );
- }
- else
- {
- m_FaxFile.SelectEncoding( FAXAPI_ENC_CCITT_1D );
- }
- break;
- default:
- m_FaxFile.SelectEncoding( m_FaxFile.GetSendEncoding() );
- break;
- }
- SignalEvent( EVENT_RECV_DIS );
- }
- //////////////////////////////////////////////////////////////////////
- // on CSI frame
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnCSI(void)
- {
- char szCSI[21];
- for( int i = 0; i < 20; i++ )
- {
- if( (22 - i) < m_nHDLCBuffPtr )
- {
- szCSI[i] = m_szHDLCBuff[ 22 - i ];
- }
- else
- {
- szCSI[i] = ' ';
- }
- }
- szCSI[20] = ' ';
- m_sRemoteCSID.assign( szCSI );
- SignalEvent( EVENT_GOT_REMOTEID );
- }
- //////////////////////////////////////////////////////////////////////
- // on CFR frame
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnCFR(void)
- {
- m_bGotCFR = true;
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_CFR).c_str() );
- }
- //////////////////////////////////////////////////////////////////////
- // on MCF frame
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnMCF(void)
- {
- m_bGotMCF = true;
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_MCF).c_str() );
- }
- //////////////////////////////////////////////////////////////////////
- // on RTP frame
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnRTP(void)
- {
- m_bGotRTP = true;
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_RTP).c_str() );
- }
- //////////////////////////////////////////////////////////////////////
- // on RTN frame
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnRTN(void)
- {
- m_bGotRTN = true;
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_RTN).c_str() );
- }
- //////////////////////////////////////////////////////////////////////
- // on Failure To Train frame
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnFTT(void)
- {
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_FTT).c_str() );
- m_bGotCFR = false;
- }
- //////////////////////////////////////////////////////////////////////
- // on DCS
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnDCS(void)
- {
- int i;
- m_bGotDCS = true;
- // OutputDebugString( "OnDCSn" );
-
- BYTE c1 = CFaxFile::s_FlipTable[ m_szHDLCBuff[4] ];
- BYTE c2 = CFaxFile::s_FlipTable[ m_szHDLCBuff[5] ];
- WORD wBitRate = (c1 & 0x3c) >> 2;
- WORD wScanTime = (c2 & 0x0E) >> 1;
- WORD wWidth = (c2 & 0xc0) >> 6;
- WORD wLength = (c2 & 0x30) >> 4;
- m_DCSParams.p.VertRes = (c1 & 0x02) >> 1;
- m_b2D = (c1 & 0x01);
- m_nClass1Speed = 0;
- m_bT6Encoding = false;
- m_bECM = false;
- for( i = 0; i < MAX_CLS1SPEEDS; i++ )
- {
- if( cls1Speeds[i].cDCSBits == wBitRate )
- {
- m_nClass1Speed = i;
- break;
- }
- }
- //char jbuf[256];
- //wsprintf( jbuf, "Baud rate bits: %dn", wBitRate );
- //OutputDebugString( jbuf );
- //wsprintf( jbuf, "Selected baud rate: %dn", cls1Speeds[m_nClass1Speed].dwSpeed );
- //OutputDebugString( jbuf );
- m_DCSParams.p.BitRate = cls1Speeds[m_nClass1Speed].ndx;
- if( m_DCSParams.p.VertRes )
- {
- //OutputDebugString( "Receiving fine qualityn" );
- }
- m_DCSParams.p.DataFormat = FAXAPI_ENC_CCITT_1D;
- if( m_b2D )
- {
- m_DCSParams.p.DataFormat = FAXAPI_ENC_CCITT_2D;
- //OutputDebugString( "Receiving 2Dn" );
- }
- switch( wLength )
- {
- case 0:
- m_DCSParams.p.PageLength = 0;
- break;
- case 2:
- m_DCSParams.p.PageLength = 1;
- break;
- default:
- m_DCSParams.p.PageLength = 2;
- break;
- }
- switch( wWidth )
- {
- case 0:
- m_FaxFile.SetImageWidth( 1728 );
- m_DCSParams.p.PageWidth = 0;
- break;
- case 1:
- case 3:
- m_FaxFile.SetImageWidth( 2432 );
- m_DCSParams.p.PageWidth = 2;
- break;
- case 2:
- m_FaxFile.SetImageWidth( 2048 );
- m_DCSParams.p.PageWidth = 1;
- break;
- }
- if (!m_DISParams.p.VertRes)
- {
- //OutputDebugString( "Normal resolution supportedn" );
- m_wScanTimeMs = Cls1ScanTimes_normal[wScanTime];
- }
- else
- {
- //OutputDebugString( "Fine resolution supportedn" );
- m_wScanTimeMs = Cls1ScanTimes_fine[wScanTime];
- }
- switch( m_wScanTimeMs )
- {
- case 0:
- m_DCSParams.p.ScanTime = 0;
- break;
- case 5:
- m_DCSParams.p.ScanTime = 1;
- break;
- case 10:
- m_DCSParams.p.ScanTime = 3;
- break;
- case 20:
- m_DCSParams.p.ScanTime = 5;
- break;
- case 40:
- m_DCSParams.p.ScanTime = 7;
- break;
- }
- m_DCSParams.p.ECM = 0;
- if( (c2 & 0x01) && // extension bit
- m_nHDLCBuffPtr > 6 )
- {
- BYTE c3 = CFaxFile::s_FlipTable[ m_szHDLCBuff[6] ];
- //if( c3 & 0x80 )
- //{
- //OutputDebugString( "Receiving with 2400bps handshakingn" );
- //}
- //if( c3 & 0x40 )
- //{
- // OutputDebugString( "Receiving uncompressed moden" );
- //}
- if( c3 & 0x20 )
- {
- m_bECM = true;
- if( c3 & 0x10 )
- {
- m_FaxFile.SetECMFrameSize( 64 );
- //OutputDebugString( "Receiving ECM w/ 64 octet framesn" );
- m_DCSParams.p.ECM = 1;
- }
- else
- {
- m_FaxFile.SetECMFrameSize( 256 );
- //OutputDebugString( "Receiving ECM w/ 256 octet framesn" );
- m_DCSParams.p.ECM = 2;
- }
- }
- //if( c3 & 0x08 )
- //{
- // OutputDebugString( "Receiving with error limiting moden" );
- //}
- //if( c3 & 0x04 )
- //{
- // OutputDebugString( "Receiving G4 on PSTNn" );
- //}
- if( c3 & 0x02 )
- {
- //OutputDebugString( "Receiving T.6 encodingn" );
- m_bT6Encoding = true;
- m_DCSParams.p.DataFormat = FAXAPI_ENC_CCITT_T6;
- }
- if( (c3 & 0x01) && // extension bit
- m_nHDLCBuffPtr > 7 )
- {
- BYTE c4 = CFaxFile::s_FlipTable[ m_szHDLCBuff[7] ];
- // width expansion byte
- if( (c4 & 0x01) && // extension bit
- m_nHDLCBuffPtr > 8 )
- {
- BYTE c5 = CFaxFile::s_FlipTable[ m_szHDLCBuff[8] ];
- // more recent extensions
- }
- }
- }
- m_FaxFile.SetImageRes( m_DCSParams.p.VertRes != 0 );
- m_FaxFile.SetImageCompression( m_bT6Encoding ? 4 : 3 ); // Set to 4 for Group IV (T.6) encoding
- m_FaxFile.SetT4Options( m_b2D ? 0x01 : 0x00 );
- m_FaxFile.SetImageLength( 0 );
- SignalEvent( EVENT_RECV_DCS );
- }
- //////////////////////////////////////////////////////////////////////
- // on End of Page
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnEOP(void)
- {
- m_bGotEOP = true;
- }
- //////////////////////////////////////////////////////////////////////
- // on End of Message
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnEOM(void)
- {
- m_bGotEOM = true;
- }
- //////////////////////////////////////////////////////////////////////
- // on MPS
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnMPS(void)
- {
- m_bGotMPS = true;
- }
- //////////////////////////////////////////////////////////////////////
- // on DCN
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnDCN(void)
- {
- m_bGotDCN = true;
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_DCN).c_str() );
- }
- //////////////////////////////////////////////////////////////////////
- // on PPS
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnPPS(void)
- {
- // char szMsg[80];
- // wsprintf( szMsg, "OnPPS FCF=%x, Pages=%d, Blocks=%d, Frames=%dn",
- // m_szHDLCBuff[3], m_szHDLCBuff[4], m_szHDLCBuff[5], m_szHDLCBuff[6] );
- // OutputDebugString( szMsg );
- if( m_nPPRCtr == 0 )
- {
- m_ECMFrameCount = m_szHDLCBuff[6];
- }
- switch( m_szHDLCBuff[3] & 0xfe )
- {
- case 0: // partial page boundary
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_PPSNULL).c_str() );
- break;
- case EOP:
- OnEOP();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_PPSEOP).c_str() );
- break;
- case MPS:
- OnMPS();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_PPSMPS).c_str() );
- break;
- case EOM:
- OnEOM();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_PPSEOM).c_str() );
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // on PPR
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnPPR(void)
- {
- m_bGotPPR = true;
- m_nPPRCtr++;
- // save the PPR FIF
- memcpy( m_FaxFile.GetSeqMap(), m_szHDLCBuff + 3, SEQ_MAP_SIZE );
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_PPR).c_str() );
- // make sure we have a bad frame - sometimes the receiving
- // fax sends PPR with no bad bits when it should have sent MCF
-
- int nBadFrames = 0;
- int i,j;
- BYTE b;
- BYTE bit;
- int nSeqNum = 0;
- for(i = 0; i < SEQ_MAP_SIZE; i++ )
- {
- b = m_FaxFile.GetSeqMap()[i];
- for(j = 0; j < 8; j++, b >>= 1 )
- {
- bit = (b & 0x01);
- if( bit )
- {
- nBadFrames++;
- }
- nSeqNum++;
- // Ignore any sequence numbers which we didn't transmit
- if( nSeqNum > m_FaxFile.GetFrameCount() )
- {
- break;
- }
- }
- if( nSeqNum > m_FaxFile.GetFrameCount() )
- {
- break;
- }
- }
- if( nBadFrames == 0 )
- {
- m_bGotPPR = false;
- m_bGotMCF = true;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // on RNR - receive not ready
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnRNR(void)
- {
- m_bGotRNR = true;
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_RNR).c_str() );
- if( m_dwRNRStart == 0 )
- {
- m_dwRNRStart = GetTickCount();
- }
- }
- //////////////////////////////////////////////////////////////////////
- // on RR - receive ready
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnRR(void)
- {
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_RR).c_str() );
- }
- //////////////////////////////////////////////////////////////////////
- // on CRP - command repeat
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnCRP(void)
- {
- m_bGotCRP = true;
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_CRP).c_str() );
- }
- //////////////////////////////////////////////////////////////////////
- // on NSF - nonstandard facilities
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnNSF(void)
- {
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_NSF).c_str() );
- }
- //////////////////////////////////////////////////////////////////////
- // on CTC - Continue to correct
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnCTC(void)
- {
- // OutputDebugString( "OnCTCn" );
- m_bGotCTC = true;
- BYTE c1 = CFaxFile::s_FlipTable[ m_szHDLCBuff[4] ];
- WORD wBitRate = (c1 & 0x3c) >> 2;
- for( int i = 0; i < MAX_CLS1SPEEDS; i++ )
- {
- if( cls1Speeds[i].cDCSBits == wBitRate )
- {
- m_nClass1Speed = i;
- break;
- }
- }
- char jbuf[FAXAPI_MODEMMSG_INFOLEN];
- wsprintf( jbuf, (char*)LoadString(GEN_RECEIVED_CTC).c_str(), cls1Speeds[m_nClass1Speed].dwSpeed );
- SignalEventString( EVENT_INFO, jbuf );
- }
- //////////////////////////////////////////////////////////////////////
- // on CTR - Continue to correct response
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnCTR(void)
- {
- m_bGotCTC = true;
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_CTR).c_str() );
- }
- //////////////////////////////////////////////////////////////////////
- // on EOR - End of retransmission
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnEOR(void)
- {
- // OutputDebugString( "OnEORn" );
- m_bGotEOR = true;
- switch( m_szHDLCBuff[3] & 0xfe )
- {
- case 0: // partial page boundary
- break;
- case EOP:
- OnEOP();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_EOREOP).c_str() );
- break;
- case MPS:
- OnMPS();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_EORMPS).c_str() );
- break;
- case EOM:
- OnEOM();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_EOREOM).c_str() );
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // on ERR - End of retransmission response
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnERR(void)
- {
- m_bGotEOR = true;
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_ERR).c_str() );
- }
- //////////////////////////////////////////////////////////////////////
- // Handle wait timeout - do periodic processing
- //////////////////////////////////////////////////////////////////////
- bool CClassOne::OnWaitTimeout( void )
- {
- CModem::OnWaitTimeout();
- /*
- if( m_nState == STATE_INIT )
- {
- DWORD dwElapsed = GetTickCount() - m_nLastCommandTickCount;
- // In case the modem doesn't respond with an OK
- if( dwElapsed > 1000 )
- {
- if( m_nLastCommand == COMMAND_QUERY_SEND_SPEEDS )
- {
- SendCommand( COMMAND_QUERY_RECEIVE_SPEEDS );
- }
- else if( m_nLastCommand == COMMAND_QUERY_RECEIVE_SPEEDS )
- {
- SetState( STATE_IDLE );
- }
- }
- if( dwElapsed > 4000 )
- {
- char szMsg[256];
- wsprintf( szMsg, "Timeout: %d ms waiting for responsen", dwElapsed );
- //OutputDebugString( szMsg );
- OnDisconnectMsg();
- }
- }
- */
- return false;
- }
- void CClassOne::OnPartialHDLCFrame(void)
- {
- if( m_nState == STATE_PHASE_C && m_bReceiving )
- {
- if( m_bECM )
- {
- m_FaxFile.WriteECMBuffer( m_szHDLCBuff, m_nHDLCBuffPtr );
- }
- else
- {
- m_FaxFile.WriteBuffer( m_szHDLCBuff, m_nHDLCBuffPtr );
- }
- }
- else
- {
- //OutputDebugString( "HDLC buffer overflow!n" );
- }
- }
- void CClassOne::OnHDLCFrame(void)
- {
- // char szDigit[16];
- // OutputDebugString( "CClassOne::OnHDLCFramen" );
- if( m_nHDLCBuffPtr == 0 )
- {
- //OutputDebugString( "Empty HDLC Framen" );
- m_bRecvHDLCError = true;
- return;
- }
- // int i;
- // for( i = 0; i < m_nHDLCBuffPtr; i++ )
- // {
- // wsprintf( szDigit, "%02.2x ", m_szHDLCBuff[i] );
- // OutputDebugString( szDigit );
- // }
- // OutputDebugString( "n" );
- if( m_nState == STATE_PHASE_C && m_bReceiving )
- {
- if( m_bECM )
- {
- m_FaxFile.WriteECMBuffer( m_szHDLCBuff, m_nHDLCBuffPtr );
- }
- else
- {
- m_FaxFile.WriteBuffer( m_szHDLCBuff, m_nHDLCBuffPtr );
- }
- }
- else if( m_nPhaseState == STATE_RECEIVE_TRAINING )
- {
- int i;
- bool bStart = false;
- int nZeros = 0;
- for( i = 0; i < m_nHDLCBuffPtr; i++ )
- {
- if( bStart == false )
- {
- if( m_szHDLCBuff[i] == 0 )
- {
- bStart = true;
- nZeros = 1;
- }
- }
- else
- {
- if( m_szHDLCBuff[i] == 0 )
- {
- nZeros++;
- }
- }
- }
- int nBytesExpected = (int)(((float)(cls1Speeds[m_nClass1Speed].dwSpeed / 8)) * 1.5) + 2;
- m_nCorrect = (int)(100 * nZeros / nBytesExpected);
- //char szMsg[180];
- //wsprintf( szMsg, "Zeros: %d, Correct=%d%%, Total=%d, Expected=%dn", nZeros, m_nCorrect, m_nHDLCBuffPtr, nBytesExpected );
- //OutputDebugString( szMsg );
- }
- else
- {
- if( (unsigned char)m_szHDLCBuff[0] != 0xff )
- {
- bool bFoundFrame = false;
- for( int i = 1; i < m_nHDLCBuffPtr; i++ )
- {
- if( (unsigned char)m_szHDLCBuff[i] == 0xff )
- {
- memmove( m_szHDLCBuff, m_szHDLCBuff + i, m_nHDLCBuffPtr - i );
- bFoundFrame = true;
- }
- }
- if( bFoundFrame == false )
- return;
- }
- if( m_szHDLCBuff[1] & 0x10 )
- {
- /// final frame
- //OutputDebugString( "Final Framen" );
- m_bFinalHDLCFrame = true;
- }
- switch( (m_szHDLCBuff[2] & 0xfe) ) // set LSB to zero
- {
- case DIS:
- OnDIS();
- break;
- case CSI:
- case TSI:
- OnCSI();
- break;
- case CFR:
- OnCFR();
- break;
- case MCF:
- OnMCF();
- break;
- case RTP:
- OnRTP();
- break;
- case RTN:
- OnRTN();
- break;
- case FTT:
- OnFTT();
- break;
- case DCS:
- OnDCS();
- break;
- case EOP:
- OnEOP();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_EOP).c_str() );
- break;
- case EOM:
- OnEOM();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_EOM).c_str() );
- break;
- case MPS:
- OnMPS();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_RECEIVED_MPS).c_str() );
- break;
- case DCN:
- OnDCN();
- break;
- case PPS:
- OnPPS();
- break;
- case PPR:
- OnPPR();
- break;
- case RR:
- OnRR();
- break;
- case RNR:
- OnRNR();
- break;
- case CRP:
- OnCRP();
- break;
- case NSF:
- OnNSF();
- break;
- case CTC:
- OnCTC();
- break;
- case CTR:
- OnCTR();
- break;
- case EOR:
- OnEOR();
- break;
- case ERR:
- OnERR();
- break;
- default:
- {
- //char szMsg[256];
- //wsprintf( szMsg, "Unknown FCF: %dn", m_szHDLCBuff[2] );
- //OutputDebugString( szMsg );
- }
- }
- }
- }
- //////////////////////////////////////////////////////////////////////
- // OnSendFaxMsg
- //////////////////////////////////////////////////////////////////////
- void CClassOne::OnSendFaxMsg(MSG* pMsg)
- {
- CModem::OnSendFaxMsg( pMsg );
- if( !m_bConnected )
- {
- // Error!
- return;
- }
- if( m_nState != STATE_IDLE )
- {
- // Error!
- return;
- }
- m_FaxFile.SetSendEncoding( m_nSendEncoding );
- int nRet = m_FaxFile.ReadFirstHeader();
- if( nRet == 1 )
- {
- //OutputDebugString( "Error reading first headern" );
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_READING_TIFF_FILE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SignalEvent( EVENT_TERMINATE );
- SignalEvent( EVENT_IDLE );
- return;
- }
- else if( nRet == 2 )
- {
- //OutputDebugString( "Error reading first headern" );
- m_sLastError.assign( (char*)LoadString(GEN_ERROR_INCOMPAT_TIFF_FILE).c_str() );
- // SignalEvent( EVENT_ERROR );
- SignalEvent( EVENT_TERMINATE );
- SignalEvent( EVENT_IDLE );
- return;
- }
- m_dwRNRStart = 0;
- m_bReceiving = false;
- m_bHDLCMode = false;
- m_bSuccessful = false;
- SetState( STATE_PHASE_A );
- EnableSoftFlowControl( false );
- SendCommand( COMMAND_DIAL );
- }
- void CClassOne::OnRecvFaxMsg(MSG* pMsg)
- {
- CModem::OnRecvFaxMsg( pMsg );
- if( m_nState == STATE_IDLE || m_nState == STATE_RINGING )
- {
- m_FaxFile.Clear();
- m_FaxFile.SetSendEncoding( m_nSendEncoding );
- m_bReceiving = true;
- m_bSuccessful = false;
- SendCommand( COMMAND_ANSWER );
- SetState( STATE_PHASE_A );
- }
- }
- void CClassOne::SendID(unsigned char octet)
- {
- // OutputDebugString( "SendIDn" );
- HDLCFrameStart();
- HDLCFrameAddByte( 0x03 ); // not final frame
- HDLCFrameAddByte( GetFCF(octet) ); // TSI
- for( int i = 20; i > 0; i-- )
- {
- HDLCFrameAddByte( (i > m_sLocalCSID.size() ) ? SPACE : m_sLocalCSID.at(i-1) );
- }
- HDLCFrameWrite();
- string sMsg;
- if( octet == TSI )
- {
- sMsg.assign( (char*)LoadString(GEN_SENT_TSI).c_str() );
- }
- else
- {
- sMsg.assign( (char*)LoadString(GEN_SENT_CSI).c_str() );
- }
- sMsg.append( m_sLocalCSID );
- SignalEventString( EVENT_INFO, sMsg.c_str() );
- }
- void CClassOne::SendDCS(void)
- {
- // OutputDebugString( "SendDCSn" );
- HDLCFrameStart();
- HDLCFrameAddByte( 0x13 ); // final frame
- HDLCFrameAddByte( GetFCF(DCS) ); // DCS
- HDLCFrameAddByte( 0x00 ); //
- WORD c1 = 0x40;
- WORD c2 = 0;
- WORD c3 = 0;
- // Set Speed
- c1 |= cls1Speeds[m_nClass1Speed].cDCSBits << 2;
- m_DCSParams.p.BitRate = cls1Speeds[m_nClass1Speed].ndx;
- m_DCSParams.p.BFT = 0;
- m_wMinLineChars = ((cls1Speeds[m_nClass1Speed].dwSpeed/8) * m_wScanTimeMs)/1000;
- //char jbuf[256];
- //wsprintf(jbuf, "Min Chars/Line: %dn", m_wMinLineChars );
- //OutputDebugString( jbuf );
- //wsprintf( jbuf, "Selected baud rate: %dn", cls1Speeds[m_nClass1Speed].dwSpeed );
- //OutputDebugString( jbuf );
- // Set Resolution
- if( m_FaxFile.IsHiRes() && m_DISParams.p.VertRes ) // If High Res set 7/7mm bit
- {
- c1 |= 0x02;
- m_DCSParams.p.VertRes = 1;
- }
- else
- {
- m_DCSParams.p.VertRes = 0; // low res
- }
- switch( m_wScanTimeMs )
- {
- case 0:
- c2 |= 0x0E;
- m_DCSParams.p.ScanTime = 0;
- break;
- case 5:
- c2 |= 0x02;
- m_DCSParams.p.ScanTime = 1;
- break;
- case 10:
- c2 |= 0x04;
- m_DCSParams.p.ScanTime = 3;
- break;
- case 20:
- //c2 |= 0x00;
- m_DCSParams.p.ScanTime = 5;
- break;
- case 40:
- c2 |= 0x08;
- m_DCSParams.p.ScanTime = 7;
- break;
- }
- if( m_FaxFile.GetSendEncoding() > FAXAPI_ENC_CCITT_1D )
- {
- //OutputDebugString( "Sending 2Dn" );
- c1 |= 0x01; // Set the 2D bit
- }
- // default is A4 (0x00), but set unlimited if found in DIS
- if( m_DISParams.p.PageLength == 2 )
- {
- c2 |= 0x10;
- m_DCSParams.p.PageLength = 2;
- }
- else
- {
- m_DCSParams.p.PageLength = 0;
- }
- // default is 1728 width (0x00)
- m_DCSParams.p.PageWidth = 0;
- c2 |= 0x01; // set the extend bit
- if( m_FaxFile.GetSendEncoding() == FAXAPI_ENC_CCITT_T6 )
- {
- c3 |= 0x02; // Set T.6 encoding
- }
- // Use ECM?
- if( m_bECM && m_bECMSupported )
- {
- c3 |= 0x20; // set the ECM bit
- if( m_FaxFile.GetECMFrameSize() == 64 )
- {
- c3 |= 0x10; // 64 octet frames
- m_DCSParams.p.ECM = 1;
- }
- else
- {
- m_DCSParams.p.ECM = 2;
- }
- }
- else
- {
- m_bECM = false;
- m_DCSParams.p.ECM = 0;
- }
- m_DCSParams.p.DataFormat = m_FaxFile.GetSendEncoding();
- HDLCFrameAddByte( CFaxFile::s_FlipTable[ c1 ] );
- HDLCFrameAddByte( CFaxFile::s_FlipTable[ c2 ] );
- HDLCFrameAddByte( CFaxFile::s_FlipTable[ c3 ] );
- HDLCFrameWrite();
- SignalEvent( EVENT_SENT_DCS );
- }
- void CClassOne::SendTraining(void)
- {
- unsigned int nBytesToSend;
- // OutputDebugString( "SendTrainingn" );
- nBytesToSend = (int)(((float)(cls1Speeds[m_nClass1Speed].dwSpeed / 8)) * 1.5);
- HDLCFrameStart();
- for( int i = 0; i < nBytesToSend; i++ )
- {
- HDLCFrameAddByte( 0 );
- }
- HDLCFrameWrite();
- //char szMsg[180];
- //wsprintf( szMsg, "Zeros: %dn", nBytesToSend );
- //OutputDebugString( szMsg );
- }
- void CClassOne::SendEOP(void)
- {
- if( m_bECM )
- {
- if( m_FaxFile.GetPartialPage() )
- {
- SendPPSHDLCFrame( 0 );
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_PPSNULL).c_str() );
- }
- else
- {
- if( m_FaxFile.MorePages() == false )
- {
- SendPPSHDLCFrame( EOP );
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_PPSEOP).c_str() );
- }
- else
- {
- SendPPSHDLCFrame( MPS );
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_PPSMPS).c_str() );
- }
- }
- }
- else
- {
- if( m_FaxFile.MorePages() == false )
- {
- SendSimpleHDLCFrame( EOP );
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_EOP).c_str() );
- }
- else
- {
- SendSimpleHDLCFrame( MPS );
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_MPS).c_str() );
- }
- }
- }
- void CClassOne::SendDCN(void)
- {
- SendSimpleHDLCFrame( DCN );
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_DCN).c_str() );
- }
- void CClassOne::SendDIS(void)
- {
- HDLCFrameStart();
- HDLCFrameAddByte( 0x13 ); // final frame
- HDLCFrameAddByte( GetFCF(DIS) ); // DIS
- HDLCFrameAddByte( 0x00 ); // not used in Group 3
- WORD c1 = 0x40; // can receive group 3 TODO: polling
- WORD c2 = 0x01; // extend bit
- WORD c3 = 0x00; //
- if( m_bRecvSupported[9] && m_nRecvBaud >= 5 )
- {
- c1 |= (DIS_V27_V29_V33_V17 << 2);
- m_DISParams.p.BitRate = 5;
- }
- else if( m_bRecvSupported[8] && m_nRecvBaud >= 4 )
- {
- c1 |= (DIS_V27_V29_V33 << 2);
- m_DISParams.p.BitRate = 4;
- }
- else if( m_bRecvSupported[3] && m_bRecvSupported[2] && m_bRecvSupported[1] && m_bRecvSupported[0] && m_nRecvBaud >= 3 )
- {
- c1 |= (DIS_V27_V29 << 2);
- m_DISParams.p.BitRate = 3;
- }
- else if( m_bRecvSupported[3] && m_bRecvSupported[2] && m_nRecvBaud >= 2 )
- {
- c1 |= (DIS_V29 << 2);
- m_DISParams.p.BitRate = 2;
- }
- else if( m_bRecvSupported[1] && m_nRecvBaud >= 1 )
- {
- c1 |= (DIS_V27 << 2);
- m_DISParams.p.BitRate = 1;
- }
- else
- {
- c1 |= (DIS_V27_FALLBACK << 2);
- m_DISParams.p.BitRate = 0;
- }
- // Unlimited length and 1728 width
- m_DISParams.p.PageWidth = 0;
- m_DISParams.p.PageLength = 2;
- // 1728 width is default
- c2 |= 0x10; // unlimited length
- c2 |= 0x0E; // 0 ms scan time
- m_DISParams.p.ScanTime = 0;
- c1 |= 0x02; // support hi res
- m_DISParams.p.VertRes = 1;
- if( m_nSendEncoding > FAXAPI_ENC_CCITT_1D )
- {
- c1 |= 0x01; // support 2D coding
- }
- if( m_bECMSupported )
- {
- c3 |= 0x20; // support 256 octet ECM
- }
- //c3 |= 0x30; // support 64 octet ECM
- if( m_nSendEncoding == FAXAPI_ENC_CCITT_T6 )
- {
- c3 |= 0x02; // support T.6 encoding
- }
- m_DISParams.p.ECM = m_bECMSupported ? 2 : 0;
- m_DISParams.p.DataFormat = m_nSendEncoding;
- HDLCFrameAddByte( CFaxFile::s_FlipTable[ c1 ] );
- HDLCFrameAddByte( CFaxFile::s_FlipTable[ c2 ] );
- HDLCFrameAddByte( CFaxFile::s_FlipTable[ c3 ] );
- HDLCFrameWrite();
- SignalEvent( EVENT_SENT_DIS );
- }
- void CClassOne::SendCFR(void)
- {
- // OutputDebugString( "SendCFRn" );
- SendSimpleHDLCFrame( CFR );
- }
- void CClassOne::SendFTT(void)
- {
- // OutputDebugString( "SendFTTn" );
- SendSimpleHDLCFrame( FTT );
- }
- void CClassOne::SendMCF(void)
- {
- if( m_bSendPPR )
- {
- SendPPR();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_PPR).c_str() );
- m_nPhaseState = STATE_MCF_SENT;
- }
- else
- {
- if( m_bLastPageGood )
- {
- SendSimpleHDLCFrame( MCF );
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_MCF).c_str() );
- m_nPhaseState = STATE_MCF_SENT;
- }
- else
- {
- SendSimpleHDLCFrame( RTN );
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_RTN).c_str() );
- SetState( STATE_PHASE_B, STATE_RECEIVE_TRAINING_DATA );
- m_dwTrainingStart = GetTickCount();
- SetTimer( TIMER_TRAINING, 4000 );
- RecvLongTraining();
- m_nCorrect = 0;
- m_nHDLCBuffPtr = 0;
- }
- }
- }
- void CClassOne::SendPPR(void)
- {
- // OutputDebugString( "SendPPRn" );
- HDLCFrameStart();
- HDLCFrameAddByte( 0x13 ); // final frame
- HDLCFrameAddByte( GetFCF(PPR) );
- for( int i = 0; i < SEQ_MAP_SIZE; i++ )
- {
- HDLCFrameAddByte( m_FaxFile.GetSeqMap()[i] );
- }
- HDLCFrameWrite();
- }
- void CClassOne::SendCTR(void)
- {
- SendSimpleHDLCFrame( m_bGotCTC ? CTR : ERR );
- SignalEventString( EVENT_INFO, m_bGotCTC ? (char*)LoadString(GEN_SENT_CTR).c_str() : (char*)LoadString(GEN_SENT_ERR).c_str() );
- }
- void CClassOne::SendCTC(void)
- {
- WORD c1 = 0x40;
- // Set Speed
- c1 |= cls1Speeds[m_nClass1Speed].cDCSBits << 2;
- HDLCFrameStart();
- HDLCFrameAddByte( 0x13 ); // final frame
- HDLCFrameAddByte( GetFCF(CTC) );
- HDLCFrameAddByte( 0x00 ); // not used in Group 3
- HDLCFrameAddByte( CFaxFile::s_FlipTable[ c1 ] ); //
- HDLCFrameWrite();
- SignalEventString( EVENT_INFO, (char*)LoadString(GEN_SENT_CTC).c_str() );
- }
- void CClassOne::SendSimpleHDLCFrame( unsigned char nFCF )
- {
- HDLCFrameStart();
- HDLCFrameAddByte( 0x13 ); // final frame
- HDLCFrameAddByte( GetFCF(nFCF) );
- HDLCFrameWrite();
- }
- void CClassOne::SendPPSHDLCFrame( unsigned char nFCF )
- {
- HDLCFrameStart();
- HDLCFrameAddByte( 0x13 ); // final frame
- HDLCFrameAddByte( GetFCF(PPS) );
- HDLCFrameAddByte( (nFCF == 0) ? 0 : GetFCF(nFCF) );
- HDLCFrameAddByte( m_FaxFile.GetPageCount() ); // page count
- HDLCFrameAddByte( m_FaxFile.GetBlockCount() ); // block count
- HDLCFrameAddByte( m_FaxFile.GetFrameCount() ); // frame count
- HDLCFrameWrite();
- }
- void CClassOne::SendFormattedCommand( char* szCMD, int nParam )
- {
- char szBuf[32];
- wsprintf( szBuf, "AT+%s=%d", szCMD, nParam );
- DoWrite( szBuf, strlen(szBuf), true );
- //char szWorkBuffer[100];
- //wsprintf( szWorkBuffer, "Sent Command: %sn", szBuf );
- //OutputDebugString( szWorkBuffer );
- }
- void CClassOne::SetState( int nNewState, int nNewPhaseState )
- {
- /*
- bool bSignalIt = false;
- if( m_nState != nNewState )
- {
- bSignalIt = true;
- }
- */
- m_nState = nNewState;
- m_nPhaseState = nNewPhaseState;
- if( nNewState == STATE_IDLE )
- {
- SignalEvent( EVENT_IDLE );
- }
- }
- void CClassOne::DecrementSpeed( void )
- {
- int i;
- long dwCurSpeed;
- i = m_nClass1Speed - 1;
- if( i < 0 )
- return;
-
- dwCurSpeed = cls1Speeds[m_nClass1Speed].dwSpeed;
- while( i > 0 && cls1Speeds[i].dwSpeed >= dwCurSpeed )
- i--;
- m_nClass1Speed = i;
- }
- void CClassOne::HDLCFrameStart(void)
- {
- m_szHDLCBuff[0] = 0xff; // address octet
- m_nHDLCBuffPtr = 1;
- };
- void CClassOne::HDLCFrameAddByte( unsigned char nOctet )
- {
- m_szHDLCBuff[m_nHDLCBuffPtr++] = nOctet;
- if( nOctet == DLE )
- {
- m_szHDLCBuff[m_nHDLCBuffPtr++] = nOctet;
- }
- }
- void CClassOne::HDLCFrameWrite(void)
- {
- m_szHDLCBuff[m_nHDLCBuffPtr++] = DLE;
- m_szHDLCBuff[m_nHDLCBuffPtr++] = ETX;
- DoWrite( (char*)m_szHDLCBuff, m_nHDLCBuffPtr, false );
- }
- void CClassOne::Abort( bool bUserCancelled )
- {
- char endofframe[2] = { DLE, ETX };
- char byteCancel = CAN;
- if( bUserCancelled )
- {
- m_sLastError.assign( (char*)LoadString(GEN_FAX_CANCELLED).c_str() );
- }
- Terminate();
- m_bHDLCMode = false;
- switch( m_nState )
- {
- case STATE_PHASE_A:
- DoWrite( &byteCancel, 1, false );
- m_nState = STATE_DISCONNECT;
- break;
- case STATE_PHASE_B:
- switch( m_nPhaseState )
- {
- case STATE_WAIT_FOR_DIS:
- case STATE_WAIT_FOR_CFR:
- case STATE_WAIT_FOR_DCS:
- case STATE_SENT_FTT_WAIT_FOR_DCS:
- case STATE_RECEIVE_TRAINING_DATA:
- if( m_bGotConnect == false )
- {
- DoWrite( &byteCancel, 1, false );
- }
- SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
- break;
- case STATE_SEND_DCS:
- if( m_bGotConnect == false )
- {
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
- }
- break;
- case STATE_SEND_TIS:
- case STATE_SEND_CSI:
- case STATE_SEND_DIS:
- case STATE_SEND_CFR:
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- break;
- case STATE_SEND_TRAINING_DATA:
- DoWrite( endofframe, 2, false );
- SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
- break;
- case STATE_SENT_FTT:
- case STATE_DIS_SENT:
- case STATE_SEND_SILENCE_BEFORE_DATA:
- case STATE_SEND_TRAINING:
- case STATE_RECEIVE_TRAINING:
- case STATE_SEND_RECV_DATA:
- case STATE_SEND_SILENCE_BEFORE_TRAINING:
- SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
- break;
- }
- break;
- case STATE_PHASE_C:
- if ( m_bReceiving )
- {
- DoWrite( &byteCancel, 1, false );
- SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
- m_bHDLCMode = false;
- }
- else
- {
- DoWrite( endofframe, 2, false );
- SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
- }
- break;
- case STATE_PHASE_D:
- switch( m_nPhaseState )
- {
- case STATE_SEND_PAGE_FINISHED:
- case STATE_SEND_RR:
- case STATE_SEND_CTC:
- case STATE_SEND_MCF:
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- break;
- case STATE_SEND_CTR:
- if( m_bGotConnect == false )
- {
- SetState( STATE_PHASE_E, STATE_SEND_DCN );
- }
- else
- {
- SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
- }
- break;
- case STATE_WAIT_FOR_CTC:
- case STATE_WAIT_FOR_MCF:
- case STATE_WAIT_FOR_CTR:
- case STATE_WAIT_FOR_EOP:
- if( m_bGotConnect == false )
- {
- DoWrite( &byteCancel, 1, false );
- }
- SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
- break;
-
- case STATE_MCF_SENT:
- case STATE_SEND_SILENCE_BEFORE_PAGE_FINISHED:
- case STATE_PAGE_FINISHED_SENT:
- case STATE_SEND_SILENCE_BEFORE_PHASE_C:
- case STATE_CTC_SENT:
- SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
- break;
- }
- break;
- //case STATE_PHASE_E:
- // break;
- }
- }
- void CClassOne::OnTimer( UINT nID )
- {
- //OutputDebugString( "CClassOne::OnTimern" );
-
- if( (nID == TIMER_COMMAND) && (m_nState == STATE_INIT) )
- {
- switch( m_nLastCommand )
- {
- case COMMAND_QUERY_SEND_SPEEDS:
- SendCommand( COMMAND_QUERY_RECEIVE_SPEEDS );
- break;
- case COMMAND_QUERY_RECEIVE_SPEEDS:
- SetState( STATE_IDLE );
- KillTimer( TIMER_COMMAND );
- break;
- case COMMAND_HANGUP:
- m_nState= STATE_IDLE;
- SignalEvent( EVENT_IDLE );
- m_nLoopCtr = 0;
- break;
-
- default:
- if( m_bGotOK )
- {
- m_nLoopCtr = 0;
- KillTimer( TIMER_COMMAND );
- SendCommand( COMMAND_SETUP_STRING );
- }
- 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() );
- }
- }
- break;
- }
- }
- else if( nID == TIMER_HDLC_RECV )
- {
- KillTimer( TIMER_HDLC_RECV );
- char byteCancel = CAN;
- DoWrite( &byteCancel, 1, false );
- m_bRecvHDLCError = true;
- }
- else if( nID == TIMER_TRAINING )
- {
- KillTimer( TIMER_TRAINING );
- char byteCancel = CAN;
- DoWrite( &byteCancel, 1, false );
- m_bRecvHDLCError = true;
- m_bHDLCMode = false;
- }
- else if ( nID == TIMER_MAXPAGERETRIES )
- {
- m_sLastError.assign( "Max page retry timer expired" );
- // SignalEvent( EVENT_ERROR );
- Abort( false );
- m_dwActivityTimer = GetTickCount();
- }
- }
- void CClassOne::RecvHDLCFrame(void)
- {
- SendFormattedCommand( "FRH", 3 );
- m_bRecvHDLCError = false;
- SetTimer( TIMER_HDLC_RECV, 6000 );
- m_bGotConnect = false;
- };
- void CClassOne::OnAbortFaxMsg(void)
- {
- Abort( true );
- }
- void CClassOne::CheckTimeouts( DWORD dwInActive )
- {
- switch( m_nState )
- {
- case STATE_PHASE_A:
- if( dwInActive > 90000 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_TIMEOUT_CONNECTING_TO_REMOTE_FAX).c_str() );
- // SignalEvent( EVENT_ERROR );
- Abort( false );
- m_dwActivityTimer = GetTickCount();
- }
- break;
- case STATE_PHASE_E:
- if( dwInActive > 5000 )
- {
- Terminate();
- DoHangup();
- m_dwActivityTimer = GetTickCount();
- }
- break;
- case STATE_DISCONNECT:
- if( dwInActive > 1000 )
- {
- PhaseDisconnect();
- }
- break;
- default:
- if( m_nState > STATE_IDLE && m_nState != STATE_RINGING )
- {
- int nInActive = GetTickCount() - m_dwActivityTimer;
-
- if( nInActive > 60000 )
- {
- m_sLastError.assign( (char*)LoadString(GEN_TIMEOUT_IN_SENDRECEIVE).c_str() );
- // SignalEvent( EVENT_ERROR );
-
- Abort( false );
- m_dwActivityTimer = GetTickCount();
- }
- }
- break;
- }
- }