Modem.h
上传用户:glass0516
上传日期:2010-01-11
资源大小:104k
文件大小:10k
- /*****************************************************************************
- * 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 CModem build the message loop of an independent thread
- // into CCommPort and handle communication with other threads.
- //
- ////////////////////////////////////////////////////////////////////////////////
- #ifndef MODEM_H
- #define MODEM_H
- #include "FaxApi.h"
- #include "CommPort.h"
- #include "Thread.h"
- #include "HiddenWindow.h"
- #include "FaxFile.h"
- #define WM_MODEM_CONNECT WM_USER+1
- #define WM_MODEM_DISCONNECT WM_USER+2
- #define WM_MODEM_SENDFAX WM_USER+3
- #define WM_MODEM_RECVFAX WM_USER+4
- #define WM_MODEM_ABORTFAX WM_USER+5
- #define WM_MODEM_CLEARRINGCNT WM_USER+6
- #define WM_MODEM_SETID WM_USER+7
- #define WM_MODEM_MESSAGE WM_USER+423
- #define XMT_FCF(c) (c|0x01)
- #define RCV_FCF(c) (c)
- // Initial identification
- #define DIS 0x80
- #define CSI 0x40
- #define NSF 0x20
- // Command to send
- #define DCS 0x82
- #define TSI 0x42
- #define NSS 0x22
- // Phase B responses
- #define CFR 0x84
- #define FTT 0x44
- // Post-message commands
- #define EOM 0x8e
- #define MPS 0x4e
- #define EOP 0x2e
- // Post-mesage responses
- #define MCF 0x8c
- #define RTP 0xcc
- #define PIP 0xac
- #define RTN 0x4c
- #define PIN 0x2c
- // Other signals
- #define DCN 0xfa
- #define CRP 0x1a
- // ECM frames
- #define FCD 0x06
- #define RCP 0x86
- #define PPS 0xbe
- #define PPR 0xbc
- #define RNR 0xec
- #define RR 0x6e
- #define CTC 0x12
- #define CTR 0xc4
- #define EOR 0xce
- #define ERR 0x1c
- #define HDLC_FLAG 0x7e
- struct InternalModemMsg
- {
- char szFile[MAX_PATH];
- char szNumber[MAX_PATH];
- };
- typedef union
- {
- FaxApiParameters p;
- int n[8];
- } CommandParameters;
- class CModem :
- public CThread,
- public CHiddenWindow,
- public CCommPort
- {
- public:
- CModem();
- virtual ~CModem();
- enum CommandIDs { COMMAND_NONE,
- COMMAND_INIT,
- COMMAND_SETUP_STRING,
- COMMAND_DISABLE_ECHO,
- COMMAND_SET_SPKR_VOL,
- COMMAND_SET_SPKR_MODE,
- COMMAND_SET_FCLASS_1,
- COMMAND_SET_FCLASS_1_0,
- COMMAND_SET_FCLASS_2,
- COMMAND_SET_FCLASS_2_0,
- COMMAND_SET_FCLASS_2_1,
- COMMAND_QUERY_SEND_SPEEDS,
- COMMAND_QUERY_RECEIVE_SPEEDS,
- COMMAND_SET_LOCAL_ID,
- COMMAND_SET_RECV,
- COMMAND_SET_BIT_ORDER,
- COMMAND_SET_FLOW_CONTROL,
- COMMAND_SET_HDLC_REPORTING,
- COMMAND_SET_FAX_PARAMS,
- COMMAND_DIAL,
- COMMAND_HANGUP,
- COMMAND_ANSWER,
- COMMAND_QUERY_FCLASS,
- COMMAND_QUERY_PRODUCT_CODE,
- COMMAND_QUERY_MANUFACTURER,
- COMMAND_QUERY_CLASS2_CAP,
- COMMAND_QUERY_CLASS2_MANUFACT,
- COMMAND_QUERY_CLASS2_MODEL,
- COMMAND_QUERY_CLASS2_0_CAP,
- COMMAND_QUERY_CLASS2_0_MANUFACT,
- COMMAND_QUERY_CLASS2_0_MODEL,
- COMMAND_COUNT };
- enum States { STATE_NONE,
- STATE_INIT,
- STATE_IDLE,
- STATE_PHASE_A,
- STATE_PHASE_B,
- STATE_PHASE_C,
- STATE_PHASE_D,
- STATE_PHASE_E,
- STATE_RINGING,
- STATE_HANGUP, // not used
- STATE_DISCONNECT };
- enum Events { EVENT_ERROR,
- EVENT_IDLE,
- EVENT_RING,
- EVENT_START_RECV,
- EVENT_START_SEND,
- EVENT_RECV_DIS,
- EVENT_SENT_DIS,
- EVENT_RECV_DCS,
- EVENT_SENT_DCS,
- EVENT_START_TRAINING,
- EVENT_START_PAGE,
- EVENT_PAGE_DATA,
- EVENT_GOT_REMOTEID,
- EVENT_TERMINATE,
- EVENT_DISCONNECT,
- EVENT_CALLERID,
- EVENT_INFO,
- };
- enum TimerIDs { TIMER_COMMAND, TIMER_HDLC_RECV, TIMER_TRAINING, TIMER_MAXPAGERETRIES };
- static char* s_CommonCmds[COMMAND_COUNT];
- // These functions call be called by other threads
- void Initialize( HANDLE hStop, DWORD faxThreadID );
- void SetPort( char* szPort );
- void Disconnect(void);
- void RecvFax( char* szFaxFile );
- void AbortFax(void);
- bool IsConnected(void) { return m_bConnected; }
- void SetSpkrParams( int nSpkrVol, int nSpkrMode );
- void SetDistinctiveRing( LPCSTR szRingCodes ) { m_sRingCodes.assign( szRingCodes ); };
- void SetInitString( LPCSTR szString );
- void SetSendEncoding( int nEncoding ) { m_nSendEncoding = nEncoding; m_FaxFile.SetSendEncoding( nEncoding ); };
- void SetSendECM( bool bECMSupported ) { m_bECMSupported = bECMSupported; };
- void SetSendFine( bool bFineSupported ) { m_bFineSupported = bFineSupported; };
- void SetSendUnlimited( bool bUnlimitedSupported ) { m_bUnlimitedSupported = bUnlimitedSupported; };
- void SetMaxPageRetries( int nRetries ) { m_nMaxPageRetries = nRetries; };
- void SetPulseDialing( bool bPulseDialing ) { m_bPulseDialing = bPulseDialing; };
- void SetCSID( LPCSTR szString );
- void ChangeCSID( LPCSTR szString );
- void SetSendBaud( int nBaud ) { m_nSendBaud = nBaud; };
- void SetRecvBaud( int nBaud ) { m_nRecvBaud = nBaud; };
- int GetSendBaud(void) { return m_nSendBaud; };
- int GetRecvBaud(void) { return m_nRecvBaud; };
- bool SendFax( char* szNumberToDial, char* szFaxFile );
- void SetFaxFile( LPCSTR szFaxFile ) { m_FaxFile.SetFileName( szFaxFile ); };
- LPCSTR GetLastError( void ) { return m_sLastError.c_str(); }
- bool IsReceiving(void) { return m_bReceiving; };
- unsigned int GetPageCount(void) { return m_FaxFile.GetPageCount(); };
- bool WasSuccessful(void) { return m_bSuccessful; };
- bool GetCapParam( int nIndex, int nValue );
- LPCSTR GetEIAClass(void) { return m_sEIAClass.c_str(); };
- // LPCSTR GetRemoteID(void) { return m_sRemoteCSID.c_str(); };
- void ClearRingCount(void);
- void SetMaxPageRetriesTimer(void);
- bool ThreadStarted(void) { return m_FaxThreadID != 0; };
- // Overridden methods of CThread
- virtual bool OnStartup(void);
- virtual void OnShutdown(void);
- virtual bool OnEvent( int nIndex );
- virtual bool OnWaitTimeout( void );
- virtual bool OnMsg( MSG* pMsg );
- virtual void OnRead(void);
- // Overridden methods of CHiddenWindow
- virtual LRESULT HandleMsg( UINT msg, WPARAM wParam, LPARAM lParam );
- // Methods overridden by derived classes
- virtual void OnHDLCFrame(void);
- virtual void OnPartialHDLCFrame(void);
- virtual void OnTimer( UINT nID );
- virtual void Abort( bool bUserCancelled );
- virtual void CheckTimeouts( DWORD dwInActive );
- protected:
- bool IsHexDigit( char digit );
- bool IsHex( char digit );
- void PurgeOutput( void );
- void PurgeWriteQueue( void );
- void DoHangup( void );
- void PhaseHangup(void);
- void PhaseDisconnect(void);
- void Terminate( void ); // terminates xfer
- bool OkToAnswer( void );
- bool IsRing(void);
- // Message handlers
- virtual void OnConnectMsg(void);
- virtual void OnDisconnectMsg(void);
- virtual void OnSendFaxMsg( MSG* pMsg );
- virtual void OnRecvFaxMsg( MSG* pMsg );
- virtual void OnAbortFaxMsg(void);
- void SendCommand( int nCommand, char* szCmdString = NULL );
- void SignalEvent( int nEvent, char info = 0 );
- void SignalEventString( int nEvent, LPCSTR szMsg );
- void SendModemMessage( int nEvent, FaxApiModemMsg* pMsg );
- void ErrorUnexpectedResponse( void );
- void ErrorConnectResponse( void );
- void ProcSupportedSpeeds( LPSTR lpSpeeds, bool bSend );
- int ParseFaxParams( int nMaxParams, int* pParams );
- void ProcCapabilities( LPSTR lpCaps );
- void ProcCapValue( LPSTR lpValue, int nIndex );
- int ProcCapValueIndex( LPSTR lpValue, int nIndex, bool bComma, bool bDash, int nLast );
- void ExitAndDelete(void);
- // Used by SendCommand
- int m_nLastCommand;
- string m_LastCommandString;
- // int m_nLastCommandTickCount;
- string m_sLastError;
- int m_nState;
- DWORD m_FaxThreadID;
- bool m_bConnected;
- int m_nSpkrVol;
- int m_nSpkrMode;
- string m_sInitString;
- string m_sEIAClass;
- string m_sLocalCSID;
- string m_sRemoteCSID;
- string m_sRingCodes;
- // fax params
- string m_sNumberToDial;
- CFaxFile m_FaxFile;
- bool m_bRecvSupported[MAX_CLS1SPEEDS];
- bool m_bSendSupported[MAX_CLS1SPEEDS];
- bool m_bECMSupported;
- bool m_bFineSupported;
- bool m_bUnlimitedSupported;
- int m_nSendEncoding;
- bool m_ParamMatrix[FAXAPI_MAXPARAMETERS][FAXAPI_MAXPARAMVALUE];
- bool m_bEnableReceive;
- int m_nRingCount;
- int m_nAnswerOnRing;
- bool m_bReceiving;
- bool m_bSuccessful;
- void InitHDLC(void) { m_nHDLCBuffPtr = 0; m_bGotDLE = false; m_bHDLCMode = true;
- m_bEolFlag = true; m_bFinalHDLCFrame = false; };
- unsigned char m_szHDLCBuff[READBUF_SIZE];
- int m_nHDLCBuffPtr;
- bool m_bGotDLE;
- bool m_bHDLCMode;
- bool m_bFinalHDLCFrame;
- bool m_bGotOK;
- bool m_bGotConnect;
- CommandParameters m_DISParams;
- CommandParameters m_DCSParams;
- unsigned int m_nPageBytes;
- int m_nSendBaud;
- int m_nRecvBaud;
- int m_nMaxSendBaud;
- int m_nMaxRecvBaud;
- DWORD m_dwActivityTimer;
- bool m_bPulseDialing;
- int m_nMaxPageRetries;
- int m_nMaxPageRetriesPageNo;
- bool m_bTerminated;
- bool m_bLastPageGood;
- };
- #endif // MODEM_H