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

📄 classtwopointzero.cpp

📁 fax engine 传真引擎 relay fax 的开源项目 商业软件使用 高质量 高可靠
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			DoWrite( CLASSTWO_DT, CLASSTWO_DT_SIZE, true );
			SetState( STATE_PHASE_C );
			m_bGotDCS = false;
			KillTimer( TIMER_COMMAND );
			SignalEvent( EVENT_START_TRAINING );
			m_bLastPageGood = false;
		}
		else
		{
			ErrorUnexpectedResponse();
			KillTimer( TIMER_COMMAND );
			Terminate();
			SetState( STATE_IDLE );	
		}
		break;

	}
}


//////////////////////////////////////////////////////////////////////
// Phase C - transmit/receive data
//////////////////////////////////////////////////////////////////////
void CClassTwoPointZero::PhaseC(void)
{
	if( m_bReceiving )
	{
		if( strnicmp( m_szLineBuff, "CONNECT", 7 ) == 0 )
		{
			char dc2 = DC2;
			DoWrite( &dc2, 1, false );
			InitHDLC();
			//EnableSoftFlowControl( false );
			m_bFirstFrame = true;

			SignalEvent( EVENT_START_PAGE );
		}
		else if( stricmp( m_szLineBuff, "OK" ) == 0 )
		{
			m_nLoopCtr = 0;
			m_bLastPageGood = m_FaxFile.WriteIFD();

			//EnableSoftFlowControl( true );

			if( m_bGotEOP || (!m_bGotEOP && !m_bGotEOM && !m_bGotMPS ))
			{
				SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
			}

			DoWrite( CLASSTWO_DR, CLASSTWO_DR_SIZE, true );

			m_bGotEOP = false;
			m_bGotMPS = false;
			m_bGotEOM = false;
		}
		else if( strnicmp( m_szLineBuff, CLASSTWO_PTS, CLASSTWO_PTS_SIZE ) == 0 )
		{
			OnFPTS();
		}
		else if( strnicmp( m_szLineBuff, CLASSTWO_ET, CLASSTWO_ET_SIZE ) == 0 )
		{
			OnFET();
		}
		else if( strnicmp( m_szLineBuff, CLASSTWO_HNGUP, CLASSTWO_HNGUP_SIZE ) == 0 )
		{
			OnFHNG();
		}	
	}
	else
	{
		if( strnicmp( m_szLineBuff, "CONNECT", 7 ) == 0 )
		{
			if( !m_bGotDCS )
			{
				char can = CAN;
				DoWrite( &can, 1, false );
				m_sLastError.assign( (char*)LoadString(GEN_ERROR_DCS_NOT_RECEIVED).c_str() );
//				SignalEvent( EVENT_ERROR );
				Terminate();
				SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
			}
			else
			{
				char xon = XON;
				DoWrite( &xon, 1, false );
				m_nPageBytes = 0;
				DoWrite( m_FaxFile.GetPageData(), m_FaxFile.GetPageBytes(), false );

				SignalEvent( EVENT_START_PAGE );

				SetMaxPageRetriesTimer();
			}
		}
		else if( stricmp( m_szLineBuff, "OK" ) == 0 )
		{
			if( true /*m_bLastPageGood*/ )
			{
				if( m_FaxFile.MorePages() )
				{
					DoWrite( CLASSTWO_DT, CLASSTWO_DT_SIZE, true );
					SetState( STATE_PHASE_C );

					if( m_FaxFile.ReadNextHeader() == false )
					{
						m_sLastError.assign( (char*)LoadString(GEN_ERROR_READING_TIFF_FILE).c_str() );
	//					SignalEvent( EVENT_ERROR );
						Terminate();
						SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
					}
					else
					{
						if( m_FaxFile.ReadPage( false, 0, m_wMinLineChars, true ) == false )
						{
							m_sLastError.assign( (char*)LoadString(GEN_ERROR_READING_TIFF_FILE).c_str() );
	//						SignalEvent( EVENT_ERROR );
							Terminate();
							SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
						}
					}
				}
				else
				{
					m_FaxFile.IncrementPageCount();
					m_bSuccessful = true;
					//DoWrite( CLASSTWO_FK, CLASSTWO_FK_SIZE, true );
					SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
					KillTimer( TIMER_MAXPAGERETRIES );
				}
			}
			else
			{
				DoWrite( CLASSTWO_DT, CLASSTWO_DT_SIZE, true );
				SetState( STATE_PHASE_C );
			}
			m_bLastPageGood = false;
		}
		else if( strnicmp( m_szLineBuff, CLASSTWO_DCS, CLASSTWO_DCS_SIZE ) == 0 )
		{
			OnDCS();
		}
		else if( strnicmp( m_szLineBuff, CLASSTWO_HNGUP, CLASSTWO_HNGUP_SIZE ) == 0 )
		{
			OnFHNG();
		}
	}
}


//////////////////////////////////////////////////////////////////////
// Phase D - post-page messages
//////////////////////////////////////////////////////////////////////
/*
void CClassTwoPointZero::PhaseD(void)
{
	if( stricmp( m_szLineBuff, "OK" ) == 0 )
	{
		/*
		m_nLoopCtr = 0;
		m_FaxFile.WriteIFD();

		EnableSoftFlowControl( true );

		if( m_bGotEOP || (!m_bGotEOP && !m_bGotEOM && !m_bGotMPS ))
		{
			SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
		}

		DoWrite( CLASSTWO_DR, CLASSTWO_DR_SIZE, true );

		m_bGotEOP = false;
		m_bGotMPS = false;
		m_bGotEOM = false;
		*/ /*

		if( m_FaxFile.MorePages() )
		{
			DoWrite( CLASSTWO_DT, CLASSTWO_DT_SIZE, true );
			SetState( STATE_PHASE_C );

			if( m_FaxFile.ReadNextHeader() )
			{
				m_FaxFile.ReadPage( false, 0, 0, true );
			}
		}
		else
		{
			m_FaxFile.IncrementPageCount();
			m_bSuccessful = true;
			DoWrite( CLASSTWO_FK, CLASSTWO_FK_SIZE, true );
			SetState( STATE_PHASE_E, STATE_WAIT_FOR_FK_RESP );
		}
	}
	else if( strnicmp( m_szLineBuff, CLASSTWO_PTS, CLASSTWO_PTS_SIZE ) == 0 )
	{
		OnFPTS();
	}
	else if( strnicmp( m_szLineBuff, CLASSTWO_HNGUP, CLASSTWO_HNGUP_SIZE ) == 0 )
	{
		OnFHNG();
	}
}
*/

//////////////////////////////////////////////////////////////////////
// Phase E - Disconnect
//////////////////////////////////////////////////////////////////////
void CClassTwoPointZero::PhaseE(void)
{
	switch( m_nPhaseState )
	{
	case STATE_WAIT_FOR_OK:
		if( strnicmp( m_szLineBuff, "OK", 2 ) == 0 )
		{
			DoWrite( CLASSTWO_FK, CLASSTWO_FK_SIZE, true );
			m_nPhaseState = STATE_WAIT_FOR_FK_RESP;
		}
		else if( strnicmp( m_szLineBuff, CLASSTWO_HNGUP, CLASSTWO_HNGUP_SIZE ) == 0 )
		{
			OnFHNG();
		}	
		break;

	case STATE_WAIT_FOR_FK_RESP:
		Terminate();
		if( strnicmp( m_szLineBuff, "NO CARRIER", 2 ) == 0 )
		{
			PhaseDisconnect();
		}
		else
		{
			DoHangup();
		}
		break;
	}
}


//////////////////////////////////////////////////////////////////////
// OnSendFaxMsg
//////////////////////////////////////////////////////////////////////
void CClassTwoPointZero::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 header\n" );
		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 header\n" );
		m_sLastError.assign( (char*)LoadString(GEN_ERROR_INCOMPAT_TIFF_FILE).c_str() );
//		SignalEvent( EVENT_ERROR );
		SignalEvent( EVENT_TERMINATE );
		SignalEvent( EVENT_IDLE );
		return;
	}

	m_bReceiving = false;
	m_bHDLCMode = false;
	m_bSuccessful = false;

	SetState( STATE_PHASE_A, STATE_SET_PARAMS );

	EnableSoftFlowControl( true );

	m_nScanTime = 0;

	m_DISParams.p.BitRate = m_nSendBaud;
	m_DISParams.p.PageLength = 2;

	SetFaxParams( true );
}


void CClassTwoPointZero::OnRecvFaxMsg(MSG* pMsg)
{
	CModem::OnRecvFaxMsg( pMsg );

	if( m_nState == STATE_IDLE || m_nState == STATE_RINGING )
	{
		EnableSoftFlowControl( false );

		m_FaxFile.Clear();

		m_FaxFile.SetSendEncoding( m_nSendEncoding );

		m_bReceiving = true;
		m_bSuccessful = false;

		SetState( STATE_PHASE_A, STATE_SET_CSID );
		SetLocalID();
	}
}


//////////////////////////////////////////////////////////////////////
// Handle wait timeout - do periodic processing
//////////////////////////////////////////////////////////////////////
bool CClassTwoPointZero::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 response\n", dwElapsed );
			//OutputDebugString( szMsg );

			OnDisconnectMsg();
		}
	}
*/
	return false;
}


void CClassTwoPointZero::SetState( int nNewState, int nNewPhaseState )
{
	m_nState = nNewState;
	m_nPhaseState = nNewPhaseState;

	if( nNewState == STATE_IDLE )
	{
		SignalEvent( EVENT_IDLE );
	}
}


void CClassTwoPointZero::Abort( bool bUserCancelled )
{
	char endofframe[2] = { DLE, ETX };
	char byteCancel = CAN;


	if( bUserCancelled ) 
	{
		m_sLastError.assign( (char*)LoadString(GEN_FAX_CANCELLED).c_str() );
	}
	
	Terminate();

	switch( m_nState )
	{
	case STATE_PHASE_A:
		switch( m_nPhaseState )
		{
		case STATE_SET_PARAMS:				
		case STATE_SET_CSID:
			SetState( STATE_PHASE_E, STATE_WAIT_FOR_FK_RESP );
			break;

		case STATE_DIALING:
			DoWrite( &byteCancel, 1, false );
			SetState( STATE_PHASE_E, STATE_WAIT_FOR_FK_RESP );
			break;
		}
		break;

	case STATE_PHASE_B:
		SetState( STATE_PHASE_E, STATE_WAIT_FOR_OK );
		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_E:
	//	break;
	}
}



void CClassTwoPointZero::SetLocalID( void )
{
	char szCmdBuff[256];

	wsprintf( szCmdBuff, CLASSTWO_SETID, m_sLocalCSID.c_str() );

	SendCommand( COMMAND_SET_LOCAL_ID, szCmdBuff );
}


void CClassTwoPointZero::SetRecv(void)
{
	char szCmdBuff[256];

	wsprintf( szCmdBuff, CLASSTWO_SETRECV, m_bEnableReceive ? 1 : 0 );


	SendCommand( COMMAND_SET_RECV, szCmdBuff );
}

void CClassTwoPointZero::SetBitOrder(void)
{
	SendCommand( COMMAND_SET_BIT_ORDER, CLASSTWO_SETBOR );
}

void CClassTwoPointZero::SetFlowControl(void)
{
	SendCommand( COMMAND_SET_FLOW_CONTROL );
}



void CClassTwoPointZero::SetHDLCReporting(void)
{
	SendCommand( COMMAND_SET_HDLC_REPORTING, CLASSTWO_SETRPTING );
}

void CClassTwoPointZero::SetFaxParams( bool bSending )
{
	char szCmdBuff[256];

	m_DCSParams.p.VertRes = m_bFineSupported ? 1 : 0;
	m_DCSParams.p.BitRate = m_nRecvBaud;
	m_DCSParams.p.PageWidth = 0;
	m_DCSParams.p.PageLength = m_bUnlimitedSupported ? 2 : 0;
	m_DCSParams.p.DataFormat = m_FaxFile.GetSendEncoding();
	m_DCSParams.p.ECM = m_bECMSupported ? 1 : 0;

	// TODO: Check if ECM enabled for modem and maximum bit rate

	if( bSending )
	{
		if( m_DISParams.p.BitRate > m_nSendBaud )
		{
			m_DCSParams.p.BitRate = m_nSendBaud;
		}
		else
		{
			m_DCSParams.p.BitRate = m_DISParams.p.BitRate;
		}

		if( m_FaxFile.IsHiRes() == false )
		{
			m_DCSParams.p.VertRes = 0;
		}
		if( m_DISParams.p.PageLength == 0 )
		{
			m_DCSParams.p.PageLength = 0;
		}
	}

	wsprintf( szCmdBuff, CLASSTWO_SETPARAMS,
		      m_DCSParams.p.VertRes,			// resolution
			  m_DCSParams.p.BitRate,			// bit rate
			  m_DCSParams.p.PageWidth,		// page width
			  m_DCSParams.p.PageLength,		// page length
			  m_DCSParams.p.DataFormat,		// format
			  m_DCSParams.p.ECM,			// error checking
			  m_nScanTime );

	SendCommand( COMMAND_SET_FAX_PARAMS, szCmdBuff );
}


void CClassTwoPointZero::OnDIS(void)
{
	//OutputDebugString( "OnDIS\n" );

	ParseFaxParams( 8, m_DISParams.n );

	m_bGotDIS = true;
	m_bResendFaxParams = false;

	m_nScanTime = m_DISParams.p.ScanTime;

	if( m_DISParams.p.ScanTime > 0 )
	{
		m_bResendFaxParams = true;
	}

	/*
	if(    (m_DISParams.p.DataFormat != FAXAPI_ENC_CCITT_1D)
		&& (m_DISParams.p.DataFormat != m_nSendEncoding ) )
	{
		//
		//OutputDebugString( "Need to re-set fax parameters!\n" );
		m_bResendFaxParams = true;

		m_FaxFile.SetSendEncoding( FAXAPI_ENC_CCITT_1D );
	}
	*/

	SignalEvent( EVENT_RECV_DIS );
}

void CClassTwoPointZero::OnDCS(void)
{
	//OutputDebugString( "OnDCS\n" );

	m_bGotDCS = true;

	ParseFaxParams( 8, m_DCSParams.n );

	if( m_bReceiving )
	{
		m_FaxFile.SetImageRes( m_DCSParams.p.VertRes != 0 );

		switch( m_DCSParams.p.PageWidth )
		{
		case 1:
			m_FaxFile.SetImageWidth( 2048 );
			break;

		case 2:
			m_FaxFile.SetImageWidth( 2432 );
			break;

		case 3:
			m_FaxFile.SetImageWidth( 1216 );
			break;

		case 4:
			m_FaxFile.SetImageWidth( 864 );
			break;

		default:
			m_FaxFile.SetImageWidth( 1728 );
			break;
		}


		switch( m_DCSParams.p.DataFormat )
		{
		case 1: // 2D modified Read
			m_FaxFile.SetImageCompression( 3 );
			m_FaxFile.SetT4Options( 0x01 );
			break;

		case 2: // 2D uncompressed
			m_FaxFile.SetImageCompression( 3 );
			m_FaxFile.SetT4Options( 0 );
			break;

		case 3: // T.6
			m_FaxFile.SetImageCompression( 4 );
			m_FaxFile.SetT4Options( 0 );
			break;

		default:	// 0 = 1D modified huffman
			m_FaxFile.SetImageCompression( 3 );
			m_FaxFile.SetT4Options( 0 );
			break;
		}

		m_FaxFile.SetImageLength( 0 );
		SignalEvent( EVENT_RECV_DCS );
	}
	else
	{
		// TODO: What if we get multiple DCS (e.g. training failed)
		int nBaud = Cls2FaxParamBitRates[m_DCSParams.p.BitRate];
		int nScanTimeMs = (m_DCSParams.p.VertRes == 0) ? 
						  Cls2ScanTimes_normal[ m_DCSParams.p.ScanTime ] :
			              Cls2ScanTimes_fine[ m_DCSParams.p.ScanTime ];
		m_wMinLineChars = ((nBaud/8) * nScanTimeMs)/1000;

		m_FaxFile.SelectEncoding( m_DCSParams.p.DataFormat );
		SignalEvent( EVENT_SENT_DCS );

		if( m_FaxFile.ReadPage( false, 0, m_wMinLineChars, true ) == false )

⌨️ 快捷键说明

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