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

📄 modem.cpp

📁 fax engine 传真引擎 relay fax 的开源项目 商业软件使用 高质量 高可靠
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		if( m_bSendSupported[9] )
		{
			m_nMaxSendBaud = 5;
		}
		else if( m_bSendSupported[8] )
		{
			m_nMaxSendBaud = 4;
		}
		else if( m_bSendSupported[7] )
		{
			m_nMaxSendBaud = 3;
		}
		else if( m_bSendSupported[6] )
		{
			m_nMaxSendBaud = 2;
		}
		else if( m_bSendSupported[1] )
		{
			m_nMaxSendBaud = 1;
		}
		else
		{
			m_nMaxSendBaud = 0;
		}

		if( m_nSendBaud > m_nMaxSendBaud )
		{
			m_nSendBaud = m_nMaxSendBaud;
		}
	}
	else
	{
		if( m_bRecvSupported[9] )
		{
			m_nMaxRecvBaud = 5;
		}
		else if( m_bRecvSupported[8] )
		{
			m_nMaxRecvBaud = 4;
		}
		else if( m_bRecvSupported[7] )
		{
			m_nMaxRecvBaud = 3;
		}
		else if( m_bRecvSupported[6] )
		{
			m_nMaxRecvBaud = 2;
		}
		else if( m_bRecvSupported[1] )
		{
			m_nMaxRecvBaud = 1;
		}
		else 
		{
			m_nMaxRecvBaud = 0;
		}

		if( m_nRecvBaud > m_nMaxRecvBaud )
		{
			m_nRecvBaud = m_nMaxRecvBaud;
		}
	}
}


bool CModem::GetCapParam( int nIndex, int nValue )
{
	if( nIndex >= 0 && nIndex <= FAXAPI_MAXPARAMETERS && nValue >= 0 && nValue <= FAXAPI_MAXPARAMVALUE )
	{
		return m_ParamMatrix[nIndex][nValue];
	}
	
	return false;
}


int CModem::ProcCapValueIndex( LPSTR lpValue, int nIndex, bool bComma, bool bDash, int nLast )
{
	int nCurrent = 0;
	char* p = lpValue;
	bool bHex = false;
	//OnFHNG

	while( *p )
	{
		if( IsHex(*p) )
		{
			bHex = true;
			break;
		}
		p++;
	}
	
	if( bHex )
	{
		sscanf( lpValue, "%X", &nCurrent );
	}
	else
	{
		nCurrent = atoi(lpValue);
	}
	
	if( bDash )
	{
		while( nLast <= nCurrent )
		{
			if( nIndex >= 0 && nIndex < FAXAPI_MAXPARAMETERS && nLast >= 0 && nLast < FAXAPI_MAXPARAMVALUE )
			{
				m_ParamMatrix[nIndex][nLast] = true;
			}
			nLast++;
		}

	}
	else 
	{
		if( nIndex >= 0 && nIndex < FAXAPI_MAXPARAMETERS && nCurrent >= 0 && nCurrent < FAXAPI_MAXPARAMVALUE )
		{
			m_ParamMatrix[nIndex][nCurrent] = true;
		}
	}
		

	return nCurrent;
}


void CModem::ProcCapValue( LPSTR lpValue, int nIndex )
{
	char szN[32];
	bool bComma = false;
	bool bDash = false;
	int nLast = 0;

	int i = 0;

	char* p = lpValue;

	while( *p )
	{
		bool bProcessNumber = false;

		if( *p == ',' )
		{
			szN[i] = 0;
			nLast = ProcCapValueIndex( szN, nIndex, bComma, bDash, nLast );
			bComma = true;
			bDash = false;
			i = 0;
		}
		else if( *p == '-' )
		{
			szN[i] = 0;
			nLast = ProcCapValueIndex( szN, nIndex, bComma, bDash, nLast );
			bDash = true;
			bComma = false;
			i = 0;
		}
		else if( IsHexDigit( *p ) )
		{
			szN[i++] = *p;
		}

		p++;

		if( *p == '\0'  )
		{
			szN[i] = 0;
			nLast = ProcCapValueIndex( szN, nIndex, bComma, bDash, nLast );
			i = 0;
		}

	}
}


void CModem::ProcCapabilities( LPSTR lpCaps )
{
	char szValue[32];
	bool bInParens = false;
	int i = 0;
	int j = 0;

	ZeroMemory( m_ParamMatrix, sizeof(m_ParamMatrix) );

	char* p = lpCaps;

	while( *p )
	{
		bool bProcessNumber = false;

		if( bInParens == false )
		{
			if( *p == '(' )
			{
				bInParens = true;
			}
			else if( *p == ',' )
			{
				bProcessNumber = true;
			}
			else 
			{
				if( i < (sizeof(szValue) - 1) )
				{
					szValue[i++] = *p;
				}
			}
		}
		else
		{
			if( *p == ')' )
			{
				bInParens = false;
			}
			else 
			{
				if( i < (sizeof(szValue) - 1) )
				{
					szValue[i++] = *p;
				}
			}
		}
		p++;

		if( *p == '\0' || bProcessNumber )
		{
			szValue[i] = 0;
			ProcCapValue( szValue, j );
			// next element
			i = 0;
			j++;
		}
	}
}

//////////////////////////////////////////////////////////////////////
// Handle wait timeout - do periodic processing
//////////////////////////////////////////////////////////////////////
bool CModem::OnWaitTimeout( void )
{
	DWORD nInActive = GetTickCount() - m_dwActivityTimer;

	CheckTimeouts( nInActive );

	return false;
}


void CModem::CheckTimeouts( DWORD dwInActive )
{
}


void CModem::PurgeWriteQueue( void )
{
	// Purge write queue
	deque<CWriteBuffer*>::iterator iter = m_WriteQueue.begin();

	while( iter != m_WriteQueue.end() )
	{
		delete (*iter);
		m_WriteQueue.pop_front();
		iter = m_WriteQueue.begin();
	}
}


void CModem::PurgeOutput( void )
{
	PurgeWriteQueue();

	PurgeComm( m_hPort, PURGE_TXABORT | PURGE_TXCLEAR );
}


void CModem::DoHangup( void )
{
	SendCommand( COMMAND_HANGUP );
	m_nState = STATE_DISCONNECT;
}

void CModem::PhaseHangup(void)
{
//	if( strnicmp( m_szLineBuff, "OK", 2 ) == 0 )
	{
		m_bGotOK = true;
		KillTimer( TIMER_COMMAND );
		m_nState = STATE_IDLE;
		SignalEvent( EVENT_IDLE );
	}
}


void CModem::PhaseDisconnect(void)
{
//	if( strnicmp( m_szLineBuff, "OK", 2 ) == 0 )
	{
		m_bGotOK = true;
		KillTimer( TIMER_COMMAND );
		DisconnectPort();
		m_nState = STATE_NONE;
		SignalEvent( EVENT_DISCONNECT );
		m_bConnected = false;
		ExitAndDelete();
	}
}

void CModem::Terminate()
{
	if( m_bTerminated == false )
	{
		SignalEvent( EVENT_TERMINATE );

		PurgeWriteQueue();

		m_FaxFile.Close();

		KillTimer( TIMER_MAXPAGERETRIES );

		m_bTerminated = true;
	}
}


bool CModem::OkToAnswer( void )
{
	return ( m_bEnableReceive && (m_nRingCount >= m_nAnswerOnRing) );
}


//////////////////////////////////////////////////////////////////////
// OnRead
//////////////////////////////////////////////////////////////////////
void CModem::OnRead(void)
{
	DWORD i;

	m_dwActivityTimer = GetTickCount();

//	char szMsg[80];
//	wsprintf( szMsg, "OnRead %d bytes\n", m_BytesRead );
//	OutputDebugString( szMsg );

	for( i = 0; i < m_BytesRead; i++ )
	{
		//char szDigit[32];
		//wsprintf( szDigit, "%d:[%02x]\n", i, m_szReadBuff[i] );
		//OutputDebugString( szDigit );

		if( m_bEolFlag )
		{
			if( (m_szReadBuff[i] != '\r') && (m_szReadBuff[i] != '\n') )
			{
				m_bEolFlag = false;
			}
		}

		if( !m_bEolFlag )
		{
			if( m_bHDLCMode ) // HDLC mode
			{
				if( m_bGotDLE ) 
				{
					m_bGotDLE = false;

					if( m_szReadBuff[i] == ETX )
					{
						OnHDLCFrame();
						InitLineParser();
						m_bHDLCMode = false;
					}
					else if( m_szReadBuff[i] == DLE )
					{
						if( m_nHDLCBuffPtr >= READBUF_SIZE )
						{
							OnPartialHDLCFrame();
							m_nHDLCBuffPtr = 0;
						}

						m_szHDLCBuff[m_nHDLCBuffPtr++] = m_szReadBuff[i];
					}
					else if( m_szReadBuff[i] == SUB )
					{
						if( m_nHDLCBuffPtr >= READBUF_SIZE )
						{
							OnPartialHDLCFrame();
							m_nHDLCBuffPtr = 0;
						}

						m_szHDLCBuff[m_nHDLCBuffPtr++] = DLE;

						if( m_nHDLCBuffPtr >= READBUF_SIZE )
						{
							OnPartialHDLCFrame();
							m_nHDLCBuffPtr = 0;
						}

						m_szHDLCBuff[m_nHDLCBuffPtr++] = DLE;
					}
					else
					{
						//OutputDebugString( "Illegal HDLC Coding!\n" );
					}
				}
				else
				{
					if( m_szReadBuff[i] == DLE )
					{
						m_bGotDLE = true;
					}
					else
					{
						if( m_nHDLCBuffPtr >= READBUF_SIZE )
						{
							OnPartialHDLCFrame();
							InitHDLC();
						}

						m_szHDLCBuff[m_nHDLCBuffPtr++] = m_szReadBuff[i];
					}
				}

				// Sometimes NO CARRIER is returned w/o DLE-ETX
				if( (m_nHDLCBuffPtr > 9) && (m_nHDLCBuffPtr < 14) && strstr( (char*) m_szHDLCBuff, "NO CARRIER" ) )
				{
					m_bHDLCMode = false;
					memcpy( m_szLineBuff, m_szHDLCBuff, m_nHDLCBuffPtr );
					m_nLineBuffPtr = m_nHDLCBuffPtr;
					m_szLineBuff[m_nLineBuffPtr] = '\0';
					OnReadLine();
					InitLineParser();
				}

			}
			else // line mode
			{
				if( (m_szReadBuff[i] == '\r') || (m_szReadBuff[i] == '\n') )
				{
					m_bEolFlag = true;

					if( m_nLineBuffPtr > 0 )
					{
						m_szLineBuff[m_nLineBuffPtr] = '\0';

						//char szMsg[256];
						//wsprintf( szMsg, "OnRead:(%d:%d)\n", m_BytesRead, i );
						//OutputDebugString( szMsg );

						// Ignore intermediate responses in init state
						if( ( m_nState > STATE_INIT ) || ((m_BytesRead - i) <= 2 ) )
						{
							OnReadLine();
						}
						else if( ( m_nState == STATE_INIT) && 
							     ((m_nLastCommand == COMMAND_QUERY_SEND_SPEEDS)||
								  (m_nLastCommand == COMMAND_QUERY_RECEIVE_SPEEDS)||
								  (m_nLastCommand >= COMMAND_QUERY_FCLASS)) )

						{
							OnReadLine();
						}

						if( !m_bHDLCMode )
						{
							InitLineParser();
						}
					}
				}
				else
				{
					if( m_nLineBuffPtr >= READBUF_SIZE )
					{
						//OutputDebugString( "Line buffer overflow!\n" );
						InitLineParser();
					}
					m_szLineBuff[m_nLineBuffPtr++] = m_szReadBuff[i];
				}
			}
		}
	}
}



void CModem::OnHDLCFrame(void)
{
}

void CModem::OnPartialHDLCFrame(void)
{
}

void CModem::OnTimer( UINT nID )
{
	//OutputDebugString( "CModem::OnTimer\n" );
}

//////////////////////////////////////////////////////////////////////
// HandleMsg
//////////////////////////////////////////////////////////////////////
LRESULT CModem::HandleMsg( UINT message, WPARAM wParam, LPARAM lParam )
{

	if( message == WM_TIMER )
	{
		OnTimer( wParam );
	}

	return DefWindowProc( m_hwnd, message, wParam, lParam );
}



int CModem::ParseFaxParams( int nMaxParams, int* pParams )
{
	char szNumBuff[21];
	int j;
	int i;

	ZeroMemory( pParams, sizeof(int) * nMaxParams );

	char* p = m_szLineBuff + 5; // Skip +XXXX

	if ( *p == ':' )			// skip the colon
	{
		p++;
	}

	// advance to first digit
	while( *p && (IsHexDigit(*p) == false) && (*p != ',') )
	{
		p++;
	}

	for( i = 0; i < nMaxParams; i++ )
	{
		// check for end of string
		if( *p == '\0' )
		{
			return i;
		}

		j = 0;

		// advance to next comma
		while( *p && IsHexDigit(*p) && (j < 20) )
		{
			szNumBuff[j++] = *p++;
		}

		szNumBuff[j] = '\0';

		pParams[i] = atoi( szNumBuff );

		if( *p )	// advance past comma
			p++;
	}

	return i;
}


void CModem::ExitAndDelete(void)
{
	OnShutdown();

	delete this;
	
	ExitThread(0);
}

bool CModem::IsRing(void)
{
	if( stricmp( m_szLineBuff, "RING" ) == 0 )
	{
		return true;
	}

	if( !m_sRingCodes.empty() )
	{
		if( strchr( m_sRingCodes.c_str(), '1' ) )
		{
			if( ( stricmp( m_szLineBuff, "RING1" ) == 0 ) || ( stricmp( m_szLineBuff, "RINGA" ) == 0 ) )
			{
				return true;
			}	
		}
		if( strchr( m_sRingCodes.c_str(), '2' ) )
		{
			if( ( stricmp( m_szLineBuff, "RING2" ) == 0 ) || ( stricmp( m_szLineBuff, "RINGB" ) == 0 ) )
			{
				return true;
			}	
		}
		if( strchr( m_sRingCodes.c_str(), '3' ) )
		{
			if( ( stricmp( m_szLineBuff, "RING3" ) == 0 ) || ( stricmp( m_szLineBuff, "RINGC" ) == 0 ) )
			{
				return true;
			}				
		}
	}

	return false;
}


void CModem::AbortFax(void)
{
	PostMsg( WM_MODEM_ABORTFAX, 0, 0 );	
}


bool CModem::IsHexDigit( char digit )
{
	return (strchr( "1234567890ABCDEFabcdef", digit ) != NULL);
}

bool CModem::IsHex( char digit )
{
	return (strchr( "ABCDEFabcdef", digit ) != NULL);
}


void CModem::Abort( bool bUserCancelled )
{
}

void CModem::ClearRingCount(void) 
{ 
	PostMsg( WM_MODEM_CLEARRINGCNT, 0, 0 );
};


void CModem::SetMaxPageRetriesTimer(void)
{
	DWORD dwTimeout = 0;

	if( m_nMaxPageRetries < 1 )
		return;

	int nPageNo = GetPageCount();

	if( nPageNo > m_nMaxPageRetriesPageNo )
	{
		KillTimer( TIMER_MAXPAGERETRIES );

		m_nMaxPageRetriesPageNo = nPageNo;

		// have to be careful how this timeout is computed so we don't overflow
		unsigned int nSecsPerPage = m_FaxFile.GetTotalPageSize() / (Cls2FaxParamBitRates[m_DCSParams.p.BitRate] / 8) ;

		dwTimeout = m_nMaxPageRetries * 1000 * (15 + nSecsPerPage);

		SetTimer( TIMER_MAXPAGERETRIES, dwTimeout );

		//char szMsg[128];
		//wsprintf( szMsg, "Setting timeout %d for page %d (%d bytes) Bitrate=%d\n", dwTimeout, nPageNo, m_FaxFile.GetTotalPageSize(), Cls2FaxParamBitRates[m_DCSParams.p.BitRate] );
		//OutputDebugString( szMsg );
	}
}

⌨️ 快捷键说明

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