Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

441 lines
14 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1998-2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: DataPort.h
  6. * Content: Serial communications port management class
  7. *
  8. *
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 01/20/98 jtk Created
  13. * 09/14/99 jtk Derived from ComPort.h
  14. ***************************************************************************/
  15. #ifndef __DATA_PORT_H__
  16. #define __DATA_PORT_H__
  17. #undef DPF_SUBCOMP
  18. #define DPF_SUBCOMP DN_SUBCOMP_MODEM
  19. //**********************************************************************
  20. // Constant definitions
  21. //**********************************************************************
  22. //
  23. // enumeration of phone state
  24. //
  25. typedef enum
  26. {
  27. MODEM_STATE_UNKNOWN = 0,
  28. MODEM_STATE_INITIALIZED,
  29. MODEM_STATE_INCOMING_CONNECTED,
  30. MODEM_STATE_OUTGOING_CONNECTED,
  31. MODEM_STATE_WAITING_FOR_OUTGOING_CONNECT,
  32. MODEM_STATE_WAITING_FOR_INCOMING_CONNECT,
  33. MODEM_STATE_CLOSING_OUTGOING_CONNECTION,
  34. MODEM_STATE_CLOSING_INCOMING_CONNECTION
  35. } MODEM_STATE;
  36. //
  37. // invalid TAPI command ID
  38. //
  39. #define INVALID_TAPI_COMMAND -1
  40. //
  41. // enumerated values for state of data port
  42. //
  43. typedef enum _DATA_PORT_STATE
  44. {
  45. DATA_PORT_STATE_UNKNOWN, // unknown state
  46. DATA_PORT_STATE_INITIALIZED, // initialized
  47. DATA_PORT_STATE_RECEIVING, // data port is receiving data
  48. DATA_PORT_STATE_UNBOUND // data port is unboind (closing)
  49. } DATA_PORT_STATE;
  50. //typedef enum _SEND_COMPLETION_CODE
  51. //{
  52. // SEND_UNKNOWN, // send is unknown
  53. // SEND_FAILED, // send failed
  54. // SEND_IN_PROGRESS // send is in progress
  55. //} SEND_COMPLETION_CODE;
  56. //**********************************************************************
  57. // Macro definitions
  58. //**********************************************************************
  59. //**********************************************************************
  60. // Structure definitions
  61. //**********************************************************************
  62. //
  63. // forward structure references
  64. //
  65. class CModemEndpoint;
  66. class CDataPort;
  67. class CModemReadIOData;
  68. class CModemWriteIOData;
  69. typedef enum _ENDPOINT_TYPE ENDPOINT_TYPE;
  70. typedef struct _DATA_PORT_DIALOG_THREAD_PARAM DATA_PORT_DIALOG_THREAD_PARAM;
  71. //
  72. // structure used to get date from the data port pool
  73. //
  74. typedef struct _DATA_PORT_POOL_CONTEXT
  75. {
  76. CModemSPData *pSPData;
  77. } DATA_PORT_POOL_CONTEXT;
  78. ////
  79. //// dialog function to call
  80. ////
  81. //typedef HRESULT (*PDIALOG_SERVICE_FUNCTION)( const DATA_PORT_DIALOG_THREAD_PARAM *const pDialogData, HWND *const phDialog );
  82. //
  83. ////
  84. //// structure used to pass data to/from the data port dialog thread
  85. ////
  86. //typedef struct _DATA_PORT_DIALOG_THREAD_PARAM
  87. //{
  88. // CDataPort *pDataPort;
  89. // BOOL *pfDialogRunning;
  90. // PDIALOG_SERVICE_FUNCTION pDialogFunction;
  91. //} DATA_PORT_DIALOG_THREAD_PARAM;
  92. //**********************************************************************
  93. // Variable definitions
  94. //**********************************************************************
  95. //**********************************************************************
  96. // Function prototypes
  97. //**********************************************************************
  98. //**********************************************************************
  99. // Class definition
  100. //**********************************************************************
  101. class CDataPort
  102. {
  103. public:
  104. void EndpointAddRef( void );
  105. DWORD EndpointDecRef( void );
  106. #undef DPF_MODNAME
  107. #define DPF_MODNAME "CDataPort::AddRef"
  108. void AddRef( void )
  109. {
  110. DNASSERT( m_iRefCount != 0 );
  111. DNInterlockedIncrement( &m_iRefCount );
  112. }
  113. #undef DPF_MODNAME
  114. #define DPF_MODNAME "CDataPort::DecRef"
  115. void DecRef( void )
  116. {
  117. DNASSERT( m_iRefCount != 0 );
  118. if ( DNInterlockedDecrement( &m_iRefCount ) == 0 )
  119. {
  120. ReturnSelfToPool();
  121. }
  122. }
  123. //
  124. // pool functions
  125. //
  126. static BOOL PoolAllocFunction( void* pvItem, void* pvContext );
  127. static void PoolInitFunction( void* pvItem, void* pvContext );
  128. static void PoolReleaseFunction( void* pvItem );
  129. static void PoolDeallocFunction( void* pvItem );
  130. void ReturnSelfToPool( void );
  131. #undef DPF_MODNAME
  132. #define DPF_MODNAME "CDataPort::Lock"
  133. void Lock( void )
  134. {
  135. DEBUG_ONLY( DNASSERT( m_fInitialized != FALSE ) );
  136. DNEnterCriticalSection( &m_Lock );
  137. }
  138. #undef DPF_MODNAME
  139. #define DPF_MODNAME "CDataPort::Unlock"
  140. void Unlock( void )
  141. {
  142. DEBUG_ONLY( DNASSERT( m_fInitialized != FALSE ) );
  143. DNLeaveCriticalSection( &m_Lock );
  144. }
  145. DPNHANDLE GetHandle( void ) const { return m_Handle; }
  146. #undef DPF_MODNAME
  147. #define DPF_MODNAME "CModemEndpoint::SetHandle"
  148. void SetHandle( const DPNHANDLE Handle )
  149. {
  150. DNASSERT( ( m_Handle == 0 ) || ( Handle == 0 ) );
  151. m_Handle = Handle;
  152. }
  153. DATA_PORT_STATE GetState( void ) const { return m_State; }
  154. #undef DPF_MODNAME
  155. #define DPF_MODNAME "CDataPort::SetState"
  156. void SetState( const DATA_PORT_STATE State )
  157. {
  158. //
  159. // Validate state transitions
  160. //
  161. DNASSERT( ( m_State == DATA_PORT_STATE_UNKNOWN ) ||
  162. ( State == DATA_PORT_STATE_UNKNOWN ) ||
  163. ( ( m_State == DATA_PORT_STATE_INITIALIZED ) && ( State == DATA_PORT_STATE_UNBOUND ) ) ||
  164. ( ( m_State == DATA_PORT_STATE_INITIALIZED ) && ( State == DATA_PORT_STATE_RECEIVING ) ) ||
  165. ( ( m_State == DATA_PORT_STATE_RECEIVING ) && ( State == DATA_PORT_STATE_UNBOUND ) ) ||
  166. ( ( m_State == DATA_PORT_STATE_RECEIVING ) && ( State == DATA_PORT_STATE_INITIALIZED ) ) ||
  167. ( ( m_State == DATA_PORT_STATE_INITIALIZED ) && ( State == DATA_PORT_STATE_INITIALIZED ) ) ); // modem failed to answer a call
  168. m_State = State;
  169. }
  170. //
  171. // port settings
  172. //
  173. const CComPortData *ComPortData( void ) const { return &m_ComPortData; }
  174. const SP_BAUD_RATE GetBaudRate( void ) const { return m_ComPortData.GetBaudRate(); }
  175. HRESULT SetBaudRate( const SP_BAUD_RATE BaudRate ) { return m_ComPortData.SetBaudRate( BaudRate ); }
  176. const SP_STOP_BITS GetStopBits( void ) const { return m_ComPortData.GetStopBits(); }
  177. HRESULT SetStopBits( const SP_STOP_BITS StopBits ) { return m_ComPortData.SetStopBits( StopBits ); }
  178. const SP_PARITY_TYPE GetParity( void ) const { return m_ComPortData.GetParity(); }
  179. HRESULT SetParity( const SP_PARITY_TYPE Parity ) { return m_ComPortData.SetParity( Parity ); }
  180. const SP_FLOW_CONTROL GetFlowControl( void ) const { return m_ComPortData.GetFlowControl(); }
  181. HRESULT SetFlowControl( const SP_FLOW_CONTROL FlowControl ) { return m_ComPortData.SetFlowControl( FlowControl ); }
  182. MODEM_STATE GetModemState( void ) const { return m_ModemState; }
  183. #undef DPF_MODNAME
  184. #define DPF_MODNAME "CDataPort::SetModemState"
  185. void SetModemState( const MODEM_STATE NewState )
  186. {
  187. DNASSERT( ( GetModemState() == MODEM_STATE_UNKNOWN ) ||
  188. ( NewState == MODEM_STATE_UNKNOWN ) ||
  189. ( ( GetModemState() == MODEM_STATE_INITIALIZED ) && ( NewState == MODEM_STATE_WAITING_FOR_INCOMING_CONNECT ) ) ||
  190. ( ( GetModemState() == MODEM_STATE_INITIALIZED ) && ( NewState == MODEM_STATE_WAITING_FOR_OUTGOING_CONNECT ) ) ||
  191. ( ( GetModemState() == MODEM_STATE_WAITING_FOR_INCOMING_CONNECT ) && ( NewState == MODEM_STATE_INCOMING_CONNECTED ) ) ||
  192. ( ( GetModemState() == MODEM_STATE_WAITING_FOR_INCOMING_CONNECT ) && ( NewState == MODEM_STATE_INITIALIZED ) ) ||
  193. ( ( GetModemState() == MODEM_STATE_WAITING_FOR_OUTGOING_CONNECT ) && ( NewState == MODEM_STATE_OUTGOING_CONNECTED ) ) ||
  194. ( ( GetModemState() == MODEM_STATE_INCOMING_CONNECTED ) && ( NewState == MODEM_STATE_INITIALIZED ) ) ||
  195. ( ( GetModemState() == MODEM_STATE_OUTGOING_CONNECTED ) && ( NewState == MODEM_STATE_INITIALIZED ) ) );
  196. m_ModemState = NewState;
  197. }
  198. IDirectPlay8Address *GetLocalAdapterDP8Address( const ADDRESS_TYPE AddressType ) const;
  199. HRESULT BindComPort( void );
  200. void ProcessTAPIMessage( const LINEMESSAGE *const pLineMessage );
  201. CModemSPData *GetSPData( void ) const { return m_pSPData; }
  202. LINK_DIRECTION GetLinkDirection( void ) const { return m_LinkDirection; }
  203. #undef DPF_MODNAME
  204. #define DPF_MODNAME "CDataPort::SetLinkDirection"
  205. void SetLinkDirection( const LINK_DIRECTION LinkDirection )
  206. {
  207. DNASSERT( ( m_LinkDirection == LINK_DIRECTION_UNKNOWN ) || ( LinkDirection == LINK_DIRECTION_UNKNOWN ) );
  208. m_LinkDirection = LinkDirection;
  209. }
  210. HRESULT EnumAdapters( SPENUMADAPTERSDATA *const pEnumAdaptersData ) const;
  211. HRESULT BindToNetwork( const DWORD dwDeviceID, const void *const pDeviceContext );
  212. void UnbindFromNetwork( void );
  213. HRESULT BindEndpoint( CModemEndpoint *const pEndpoint, const ENDPOINT_TYPE EndpointType );
  214. void UnbindEndpoint( CModemEndpoint *const pEndpoint, const ENDPOINT_TYPE EndpointType );
  215. HRESULT SetPortCommunicationParameters( void );
  216. DWORD GetDeviceID( void ) const
  217. {
  218. if (m_fModem)
  219. {
  220. return m_dwDeviceID;
  221. }
  222. else
  223. {
  224. return m_ComPortData.GetDeviceID();
  225. }
  226. }
  227. #undef DPF_MODNAME
  228. #define DPF_MODNAME "CDataPort::SetDeviceID"
  229. HRESULT SetDeviceID( const DWORD dwDeviceID )
  230. {
  231. DNASSERT( ( GetDeviceID() == INVALID_DEVICE_ID ) ||
  232. ( dwDeviceID == INVALID_DEVICE_ID ) );
  233. if (m_fModem)
  234. {
  235. m_dwDeviceID = dwDeviceID;
  236. return DPN_OK;
  237. }
  238. else
  239. {
  240. return m_ComPortData.SetDeviceID( dwDeviceID );
  241. }
  242. }
  243. DNHANDLE GetFileHandle( void ) const { return m_hFile; }
  244. //
  245. // send functions
  246. //
  247. #undef DPF_MODNAME
  248. #define DPF_MODNAME "CDataPort::SendUserData"
  249. void SendUserData( CModemWriteIOData *const pWriteIOData )
  250. {
  251. DNASSERT( pWriteIOData != NULL );
  252. DNASSERT( pWriteIOData->m_DataBuffer.MessageHeader.SerialSignature == SERIAL_HEADER_START );
  253. pWriteIOData->m_DataBuffer.MessageHeader.MessageTypeToken = SERIAL_DATA_USER_DATA;
  254. SendData( pWriteIOData );
  255. }
  256. #undef DPF_MODNAME
  257. #define DPF_MODNAME "CDataPort::SendEnumQueryData"
  258. void SendEnumQueryData( CModemWriteIOData *const pWriteIOData, const UINT_PTR uRTTIndex )
  259. {
  260. DNASSERT( pWriteIOData != NULL );
  261. DNASSERT( pWriteIOData->m_DataBuffer.MessageHeader.SerialSignature == SERIAL_HEADER_START );
  262. pWriteIOData->m_DataBuffer.MessageHeader.MessageTypeToken = SERIAL_DATA_ENUM_QUERY | static_cast<BYTE>( uRTTIndex );
  263. SendData( pWriteIOData );
  264. }
  265. #undef DPF_MODNAME
  266. #define DPF_MODNAME "CDataPort::SendEnumResponseData"
  267. void SendEnumResponseData( CModemWriteIOData *const pWriteIOData, const UINT_PTR uRTTIndex )
  268. {
  269. DNASSERT( pWriteIOData != NULL );
  270. DNASSERT( pWriteIOData->m_DataBuffer.MessageHeader.SerialSignature == SERIAL_HEADER_START );
  271. pWriteIOData->m_DataBuffer.MessageHeader.MessageTypeToken = SERIAL_DATA_ENUM_RESPONSE | static_cast<BYTE>( uRTTIndex );
  272. SendData( pWriteIOData );
  273. }
  274. void ProcessReceivedData( const DWORD dwBytesReceived, const DWORD dwError );
  275. void SendComplete( CModemWriteIOData *const pWriteIOData, const HRESULT hSendResult );
  276. private:
  277. CModemReadIOData *GetActiveRead( void ) const { return m_pActiveRead; }
  278. BOOL m_fModem;
  279. CBilink m_ActiveListLinkage; // link to active data port list
  280. //
  281. // file I/O management parameters
  282. //
  283. LINK_DIRECTION m_LinkDirection; // direction of link
  284. DNHANDLE m_hFile; // file handle for reading/writing data
  285. //
  286. // bound endpoints
  287. //
  288. DPNHANDLE m_hListenEndpoint; // endpoint for active listen
  289. DPNHANDLE m_hConnectEndpoint; // endpoint for active connect
  290. DPNHANDLE m_hEnumEndpoint; // endpoint for active enum
  291. HRESULT StartReceiving( void );
  292. HRESULT Receive( void );
  293. //
  294. // private I/O functions
  295. //
  296. void SendData( CModemWriteIOData *const pWriteIOData );
  297. //
  298. // debug only items
  299. //
  300. DEBUG_ONLY( BOOL m_fInitialized );
  301. //
  302. // reference count and state
  303. //
  304. volatile LONG m_EndpointRefCount; // endpoint reference count
  305. volatile LONG m_iRefCount;
  306. volatile DATA_PORT_STATE m_State; // state of data port
  307. volatile DPNHANDLE m_Handle; // handle
  308. #ifndef DPNBUILD_ONLYONETHREAD
  309. DNCRITICAL_SECTION m_Lock; // critical section lock
  310. #endif // !DPNBUILD_ONLYONETHREAD
  311. CModemSPData *m_pSPData; // pointer to SP data
  312. CModemReadIOData *m_pActiveRead; // pointer to current read
  313. CComPortData m_ComPortData;
  314. HRESULT SetPortState( void );
  315. volatile MODEM_STATE m_ModemState;
  316. DWORD m_dwDeviceID;
  317. DWORD m_dwNegotiatedAPIVersion;
  318. HLINE m_hLine;
  319. HCALL m_hCall;
  320. LONG m_lActiveLineCommand;
  321. DWORD GetNegotiatedAPIVersion( void ) const { return m_dwNegotiatedAPIVersion; }
  322. void SetNegotiatedAPIVersion( const DWORD dwVersion )
  323. {
  324. DNASSERT( ( GetNegotiatedAPIVersion() == 0 ) || ( dwVersion == 0 ) );
  325. m_dwNegotiatedAPIVersion = dwVersion;
  326. }
  327. HLINE GetLineHandle( void ) const { return m_hLine; }
  328. void SetLineHandle( const HLINE hLine )
  329. {
  330. DNASSERT( ( GetLineHandle() == NULL ) || ( hLine == NULL ) );
  331. m_hLine = hLine;
  332. }
  333. HCALL GetCallHandle( void ) const { return m_hCall; }
  334. void SetCallHandle( const HCALL hCall )
  335. {
  336. DNASSERT( ( GetCallHandle() == NULL ) ||
  337. ( hCall == NULL ) );
  338. m_hCall = hCall;
  339. }
  340. LONG GetActiveLineCommand( void ) const { return m_lActiveLineCommand; }
  341. void SetActiveLineCommand( const LONG lLineCommand )
  342. {
  343. DNASSERT( ( GetActiveLineCommand() == INVALID_TAPI_COMMAND ) ||
  344. ( lLineCommand == INVALID_TAPI_COMMAND ) );
  345. m_lActiveLineCommand = lLineCommand;
  346. }
  347. void CancelOutgoingConnections( void );
  348. //
  349. // prevent unwarranted copies
  350. //
  351. CDataPort( const CDataPort & );
  352. CDataPort& operator=( const CDataPort & );
  353. };
  354. #undef DPF_MODNAME
  355. #endif // __DATA_PORT_H__