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.

317 lines
9.1 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. DrPRT
  5. Abstract:
  6. This module declares the DrPRT class.
  7. The job of the DrPRT class is to translate IO requests received
  8. from the TS server into communications (serial/parallel) port IO
  9. functions and to handle generic IO port behavior in a
  10. platform-independent way to promote reuse between the various TS
  11. client platforms, with respect to implementing comm port
  12. redirection.
  13. Subclasses of DrPRT will implement the specific comm functions
  14. for their respective platform.
  15. Author:
  16. Tad Brockway 5/26/99
  17. Revision History:
  18. --*/
  19. #ifndef __DRPRT_H__
  20. #define __DRPRT_H__
  21. #include <rdpdr.h>
  22. #include <stdlib.h>
  23. #include "drobject.h"
  24. #include "proc.h"
  25. #include "w32utl.h"
  26. #if DBG
  27. #include "tracecom.h"
  28. #endif
  29. ///////////////////////////////////////////////////////////////
  30. //
  31. // Defines
  32. //
  33. //
  34. #define DRPORTHANDLE HANDLE
  35. #define INVALID_TSPORTHANDLE INVALID_HANDLE_VALUE
  36. #if !defined(MAXULONG)
  37. #define MAXULONG ((ULONG)((ULONG_PTR)-1))
  38. #endif
  39. //
  40. // This function is required by the COM IO tracing module, tracecom.c
  41. //
  42. #if DBG
  43. void TraceCOMProtocol(TCHAR *format, ...);
  44. #endif
  45. //
  46. // Declare Tracing Macros for COM IO if we are in a DBG build.
  47. //
  48. #if DBG
  49. #define TRACEREQ(req) \
  50. TraceSerialIrpRequest(GetID(), req->IoRequest.MajorFunction, \
  51. req->IoRequest.MinorFunction, (PBYTE)(req + 1), \
  52. req->IoRequest.Parameters.DeviceIoControl.OutputBufferLength, \
  53. req->IoRequest.Parameters.DeviceIoControl.InputBufferLength, \
  54. req->IoRequest.Parameters.DeviceIoControl.IoControlCode)
  55. #define TRACERESP(req, resp) \
  56. TraceSerialIrpResponse(GetID(), req->IoRequest.MajorFunction, \
  57. req->IoRequest.MinorFunction, \
  58. resp->IoCompletion.Parameters.DeviceIoControl.OutputBuffer, \
  59. resp->IoCompletion.Parameters.DeviceIoControl.OutputBufferLength, \
  60. req->IoRequest.Parameters.DeviceIoControl.InputBufferLength, \
  61. req->IoRequest.Parameters.DeviceIoControl.IoControlCode, \
  62. resp->IoCompletion.IoStatus)
  63. #define TRACERESP_WITHPARAMS(req, outputBuf, outputBufLen, status) \
  64. TraceSerialIrpResponse(GetID(), req->IoRequest.MajorFunction, \
  65. req->IoRequest.MinorFunction, outputBuf, outputBufLen, \
  66. req->IoRequest.Parameters.DeviceIoControl.InputBufferLength, \
  67. req->IoRequest.Parameters.DeviceIoControl.IoControlCode, \
  68. status)
  69. #else
  70. #define TRACEREQ(req)
  71. #define TRACERESP(req, resp)
  72. #define TRACERESP_WITHPARAMS(req, outputBuf, outputBufLen, status)
  73. #endif
  74. ///////////////////////////////////////////////////////////////
  75. //
  76. // DrPRT
  77. //
  78. //
  79. class DrPRT
  80. {
  81. public:
  82. //
  83. // Platform-Independent Serial Device Control Block
  84. //
  85. typedef struct tagRDPDR_DCB
  86. {
  87. DWORD baudRate; // Actual numeric non-negative
  88. // baud-rate.
  89. } RDPDR_DCB, *PRDPDR_DCB;
  90. private:
  91. DRSTRING _portName;
  92. FILE *_traceFile;
  93. ProcObj *_procObj;
  94. BOOL _isValid;
  95. protected:
  96. //
  97. // Return back the port handle.
  98. //
  99. virtual DRPORTHANDLE GetPortHandle(ULONG FileId) = 0;
  100. //
  101. // Return the "parent" TS Device Redirection IO processing object.
  102. //
  103. virtual ProcObj *ProcessObject() = 0;
  104. //
  105. // Return the ID for this port.
  106. //
  107. virtual ULONG GetID() = 0;
  108. //
  109. // Remember whether this instance is valid.
  110. //
  111. VOID SetValid(BOOL set) { _isValid = set; }
  112. //
  113. // Default IO Request Handling.
  114. //
  115. virtual VOID DefaultIORequestMsgHandle(
  116. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  117. IN NTSTATUS serverReturnStatus
  118. ) = 0;
  119. //
  120. // Handle IOCTL IRP's from the server and translate to
  121. // the appropriate subclass-implemented COMM function.
  122. //
  123. // Returns TRUE if there was a valid translation. Otherwise,
  124. // FALSE is returned.
  125. //
  126. virtual BOOL MsgIrpDeviceControlTranslate(
  127. PRDPDR_IOREQUEST_PACKET pIoReq
  128. );
  129. //
  130. // Serial IOCTL Dispatch Functions
  131. //
  132. // These functions handle the platform-specific details of satisfying
  133. // serial IO requests, including sending an appropriate response to the
  134. // server.
  135. //
  136. virtual void SerialSetRTS(PRDPDR_IOREQUEST_PACKET pIoReq);
  137. virtual void SerialClearRTS(PRDPDR_IOREQUEST_PACKET pIoReq);
  138. virtual void SerialSetXOff(PRDPDR_IOREQUEST_PACKET pIoReq);
  139. virtual void SerialSetXon(PRDPDR_IOREQUEST_PACKET pIoReq);
  140. virtual void SerialSetBreakOn(PRDPDR_IOREQUEST_PACKET pIoReq);
  141. virtual void SerialSetBreakOff(PRDPDR_IOREQUEST_PACKET pIoReq);
  142. virtual void SerialSetBaudRate(PRDPDR_IOREQUEST_PACKET pIoReq);
  143. virtual void SerialGetBaudRate(PRDPDR_IOREQUEST_PACKET pIoReq);
  144. virtual void SerialSetDTR(PRDPDR_IOREQUEST_PACKET pIoReq);
  145. virtual void SerialClearDTR(PRDPDR_IOREQUEST_PACKET pIoReq);
  146. virtual void SerialSetLineControl(PRDPDR_IOREQUEST_PACKET pIoReq);
  147. virtual void SerialGetLineControl(PRDPDR_IOREQUEST_PACKET pIoReq);
  148. virtual void SerialImmediateChar(PRDPDR_IOREQUEST_PACKET pIoReq);
  149. virtual void SerialSetTimeouts(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  150. virtual void SerialGetTimeouts(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  151. virtual void SerialSetChars(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  152. virtual void SerialGetChars(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  153. virtual void SerialResetDevice(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  154. virtual void SerialSetQueueSize(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  155. virtual void SerialGetWaitMask(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  156. virtual void SerialSetWaitMask(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  157. virtual void SerialWaitOnMask(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  158. virtual void SerialPurge(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  159. virtual void SerialGetHandflow(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  160. virtual void SerialSetHandflow(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  161. virtual void SerialGetModemStatus(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  162. virtual void SerialGetDTRRTS(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  163. virtual void SerialGetCommStatus(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  164. virtual void SerialGetProperties(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  165. virtual void SerialXoffCounter(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  166. virtual void SerialLSRMSTInsert(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  167. virtual void SerialConfigSize(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  168. virtual void SerialGetConfig(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  169. virtual void SerialGetStats(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  170. virtual void SerialClearStats(PRDPDR_IOREQUEST_PACKET pIoReq) = 0;
  171. //
  172. // Handle Communication Escape Code IO Requests
  173. //
  174. void SerialHandleEscapeCode(PRDPDR_IOREQUEST_PACKET pIoReq,
  175. DWORD controlCode);
  176. public:
  177. //
  178. // Constructor/Destructor
  179. //
  180. DrPRT(const DRSTRING portName, ProcObj *processObject);
  181. virtual ~DrPRT();
  182. //
  183. // Return the size (in bytes) of a device announce packet for
  184. // this device.
  185. //
  186. virtual ULONG GetDevAnnounceDataSize();
  187. //
  188. // Add a device announce packet for this device to the input
  189. // buffer.
  190. //
  191. virtual VOID GetDevAnnounceData(
  192. IN PRDPDR_DEVICE_ANNOUNCE pDeviceAnnounce,
  193. IN ULONG deviceID,
  194. IN ULONG deviceType
  195. );
  196. //
  197. // Return whether this class instance is valid.
  198. //
  199. virtual BOOL IsValid()
  200. {
  201. return _isValid;
  202. }
  203. //
  204. // Get basic information about the device.
  205. //
  206. virtual DRSTRING GetName() {
  207. return _portName;
  208. }
  209. };
  210. ///////////////////////////////////////////////////////////////
  211. //
  212. // DrPRT Inline Methods
  213. //
  214. //
  215. inline void DrPRT::SerialHandleEscapeCode(
  216. IN PRDPDR_IOREQUEST_PACKET pIoReq,
  217. IN DWORD controlCode
  218. )
  219. {
  220. NTSTATUS status;
  221. PRDPDR_DEVICE_IOREQUEST pIoRequest;
  222. DRPORTHANDLE FileHandle;
  223. DC_BEGIN_FN("DrPRT::SerialHandleEscapeCode");
  224. //
  225. // Get the IO request.
  226. //
  227. pIoRequest = &pIoReq->IoRequest;
  228. //
  229. // Get Port Handle
  230. //
  231. FileHandle = GetPortHandle(pIoRequest->FileId);
  232. ASSERT(FileHandle != INVALID_TSPORTHANDLE);
  233. //
  234. // Send the escape code to the serial port.
  235. //
  236. if (EscapeCommFunction(FileHandle, (int)controlCode)) {
  237. status = STATUS_SUCCESS;
  238. }
  239. else {
  240. DWORD err = GetLastError();
  241. TRC_ERR((TB, _T("EscapeCommFunction failed with %08x"), GetLastError()));
  242. status = TranslateWinError(err);
  243. }
  244. //
  245. // Send the results to the server.
  246. //
  247. TRACERESP_WITHPARAMS(pIoReq, NULL, 0, status);
  248. DefaultIORequestMsgHandle(pIoReq, status);
  249. DC_END_FN();
  250. }
  251. #endif