FreeRDP
include/winpr/comm.h
1 
23 #ifndef WINPR_COMM_H
24 #define WINPR_COMM_H
25 
26 #include <winpr/collections.h>
27 #include <winpr/file.h>
28 #include <winpr/winpr.h>
29 #include <winpr/wtypes.h>
30 
31 #define NOPARITY 0
32 #define ODDPARITY 1
33 #define EVENPARITY 2
34 #define MARKPARITY 3
35 #define SPACEPARITY 4
36 
37 #define ONESTOPBIT 0
38 #define ONE5STOPBITS 1
39 #define TWOSTOPBITS 2
40 
41 #ifndef IGNORE
42 #define IGNORE 0
43 #endif
44 
45 #define CBR_110 110
46 #define CBR_300 300
47 #define CBR_600 600
48 #define CBR_1200 1200
49 #define CBR_2400 2400
50 #define CBR_4800 4800
51 #define CBR_9600 9600
52 #define CBR_14400 14400
53 #define CBR_19200 19200
54 #define CBR_38400 38400
55 #define CBR_56000 56000
56 #define CBR_57600 57600
57 #define CBR_115200 115200
58 #define CBR_128000 128000
59 #define CBR_256000 256000
60 
61 #define CE_RXOVER 0x0001
62 #define CE_OVERRUN 0x0002
63 #define CE_RXPARITY 0x0004
64 #define CE_FRAME 0x0008
65 #define CE_BREAK 0x0010
66 #define CE_TXFULL 0x0100
67 #define CE_PTO 0x0200
68 #define CE_IOE 0x0400
69 #define CE_DNS 0x0800
70 #define CE_OOP 0x1000
71 #define CE_MODE 0x8000
72 
73 #define IE_BADID (-1)
74 #define IE_OPEN (-2)
75 #define IE_NOPEN (-3)
76 #define IE_MEMORY (-4)
77 #define IE_DEFAULT (-5)
78 #define IE_HARDWARE (-10)
79 #define IE_BYTESIZE (-11)
80 #define IE_BAUDRATE (-12)
81 
82 #define EV_RXCHAR 0x0001
83 #define EV_RXFLAG 0x0002
84 #define EV_TXEMPTY 0x0004
85 #define EV_CTS 0x0008
86 #define EV_DSR 0x0010
87 #define EV_RLSD 0x0020
88 #define EV_BREAK 0x0040
89 #define EV_ERR 0x0080
90 #define EV_RING 0x0100
91 #define EV_PERR 0x0200
92 #define EV_RX80FULL 0x0400
93 #define EV_EVENT1 0x0800
94 #define EV_EVENT2 0x1000
95 
96 #define SETXOFF 1
97 #define SETXON 2
98 #define SETRTS 3
99 #define CLRRTS 4
100 #define SETDTR 5
101 #define CLRDTR 6
102 #define RESETDEV 7
103 #define SETBREAK 8
104 #define CLRBREAK 9
105 
106 #define PURGE_TXABORT 0x0001
107 #define PURGE_RXABORT 0x0002
108 #define PURGE_TXCLEAR 0x0004
109 #define PURGE_RXCLEAR 0x0008
110 
111 #define LPTx 0x80
112 
113 #define MS_CTS_ON ((DWORD)0x0010)
114 #define MS_DSR_ON ((DWORD)0x0020)
115 #define MS_RING_ON ((DWORD)0x0040)
116 #define MS_RLSD_ON ((DWORD)0x0080)
117 
118 #define SP_SERIALCOMM ((DWORD)0x00000001)
119 
120 #define PST_UNSPECIFIED ((DWORD)0x00000000)
121 #define PST_RS232 ((DWORD)0x00000001)
122 #define PST_PARALLELPORT ((DWORD)0x00000002)
123 #define PST_RS422 ((DWORD)0x00000003)
124 #define PST_RS423 ((DWORD)0x00000004)
125 #define PST_RS449 ((DWORD)0x00000005)
126 #define PST_MODEM ((DWORD)0x00000006)
127 #define PST_FAX ((DWORD)0x00000021)
128 #define PST_SCANNER ((DWORD)0x00000022)
129 #define PST_NETWORK_BRIDGE ((DWORD)0x00000100)
130 #define PST_LAT ((DWORD)0x00000101)
131 #define PST_TCPIP_TELNET ((DWORD)0x00000102)
132 #define PST_X25 ((DWORD)0x00000103)
133 
134 #define PCF_DTRDSR ((DWORD)0x0001)
135 #define PCF_RTSCTS ((DWORD)0x0002)
136 #define PCF_RLSD ((DWORD)0x0004)
137 #define PCF_PARITY_CHECK ((DWORD)0x0008)
138 #define PCF_XONXOFF ((DWORD)0x0010)
139 #define PCF_SETXCHAR ((DWORD)0x0020)
140 #define PCF_TOTALTIMEOUTS ((DWORD)0x0040)
141 #define PCF_INTTIMEOUTS ((DWORD)0x0080)
142 #define PCF_SPECIALCHARS ((DWORD)0x0100)
143 #define PCF_16BITMODE ((DWORD)0x0200)
144 
145 #define SP_PARITY ((DWORD)0x0001)
146 #define SP_BAUD ((DWORD)0x0002)
147 #define SP_DATABITS ((DWORD)0x0004)
148 #define SP_STOPBITS ((DWORD)0x0008)
149 #define SP_HANDSHAKING ((DWORD)0x0010)
150 #define SP_PARITY_CHECK ((DWORD)0x0020)
151 #define SP_RLSD ((DWORD)0x0040)
152 
153 #define BAUD_075 ((DWORD)0x00000001)
154 #define BAUD_110 ((DWORD)0x00000002)
155 #define BAUD_134_5 ((DWORD)0x00000004)
156 #define BAUD_150 ((DWORD)0x00000008)
157 #define BAUD_300 ((DWORD)0x00000010)
158 #define BAUD_600 ((DWORD)0x00000020)
159 #define BAUD_1200 ((DWORD)0x00000040)
160 #define BAUD_1800 ((DWORD)0x00000080)
161 #define BAUD_2400 ((DWORD)0x00000100)
162 #define BAUD_4800 ((DWORD)0x00000200)
163 #define BAUD_7200 ((DWORD)0x00000400)
164 #define BAUD_9600 ((DWORD)0x00000800)
165 #define BAUD_14400 ((DWORD)0x00001000)
166 #define BAUD_19200 ((DWORD)0x00002000)
167 #define BAUD_38400 ((DWORD)0x00004000)
168 #define BAUD_56K ((DWORD)0x00008000)
169 #define BAUD_128K ((DWORD)0x00010000)
170 #define BAUD_115200 ((DWORD)0x00020000)
171 #define BAUD_57600 ((DWORD)0x00040000)
172 #define BAUD_USER ((DWORD)0x10000000)
173 
174 #define DATABITS_5 ((WORD)0x0001)
175 #define DATABITS_6 ((WORD)0x0002)
176 #define DATABITS_7 ((WORD)0x0004)
177 #define DATABITS_8 ((WORD)0x0008)
178 #define DATABITS_16 ((WORD)0x0010)
179 #define DATABITS_16X ((WORD)0x0020)
180 
181 #define STOPBITS_10 ((WORD)0x0001)
182 #define STOPBITS_15 ((WORD)0x0002)
183 #define STOPBITS_20 ((WORD)0x0004)
184 
185 #define PARITY_NONE ((WORD)0x0100)
186 #define PARITY_ODD ((WORD)0x0200)
187 #define PARITY_EVEN ((WORD)0x0400)
188 #define PARITY_MARK ((WORD)0x0800)
189 #define PARITY_SPACE ((WORD)0x1000)
190 
191 #define COMMPROP_INITIALIZED ((DWORD)0xE73CF52E)
192 
193 #define DTR_CONTROL_DISABLE 0x00
194 #define DTR_CONTROL_ENABLE 0x01
195 #define DTR_CONTROL_HANDSHAKE 0x02
196 
197 #define RTS_CONTROL_DISABLE 0x00
198 #define RTS_CONTROL_ENABLE 0x01
199 #define RTS_CONTROL_HANDSHAKE 0x02
200 #define RTS_CONTROL_TOGGLE 0x03
201 
202 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214%28v=vs.85%29.aspx
203 typedef struct
204 {
205  DWORD DCBlength;
206  DWORD BaudRate;
207  DWORD fBinary : 1;
208  DWORD fParity : 1;
209  DWORD fOutxCtsFlow : 1;
210  DWORD fOutxDsrFlow : 1;
211  DWORD fDtrControl : 2;
212  DWORD fDsrSensitivity : 1;
213  DWORD fTXContinueOnXoff : 1;
214  DWORD fOutX : 1;
215  DWORD fInX : 1;
216  DWORD fErrorChar : 1;
217  DWORD fNull : 1;
218  DWORD fRtsControl : 2;
219  DWORD fAbortOnError : 1;
220  DWORD fDummy2 : 17;
221  WORD wReserved;
222  WORD XonLim;
223  WORD XoffLim;
224  BYTE ByteSize;
225  BYTE Parity;
226  BYTE StopBits;
227  BYTE XonChar;
228  BYTE XoffChar;
229  BYTE ErrorChar;
230  BYTE EofChar;
231  BYTE EvtChar;
232  WORD wReserved1;
233 } DCB, *LPDCB;
234 
235 typedef struct
236 {
237  DWORD dwSize;
238  WORD wVersion;
239  WORD wReserved;
240  DCB dcb;
241  DWORD dwProviderSubType;
242  DWORD dwProviderOffset;
243  DWORD dwProviderSize;
244  WCHAR wcProviderData[1];
246 
247 typedef struct
248 {
249  WORD wPacketLength;
250  WORD wPacketVersion;
251  DWORD dwServiceMask;
252  DWORD dwReserved1;
253  DWORD dwMaxTxQueue;
254  DWORD dwMaxRxQueue;
255  DWORD dwMaxBaud;
256  DWORD dwProvSubType;
257  DWORD dwProvCapabilities;
258  DWORD dwSettableParams;
259  DWORD dwSettableBaud;
260  WORD wSettableData;
261  WORD wSettableStopParity;
262  DWORD dwCurrentTxQueue;
263  DWORD dwCurrentRxQueue;
264  DWORD dwProvSpec1;
265  DWORD dwProvSpec2;
266  WCHAR wcProvChar[1];
267 } COMMPROP, *LPCOMMPROP;
268 
269 typedef struct
270 {
271  DWORD ReadIntervalTimeout;
272  DWORD ReadTotalTimeoutMultiplier;
273  DWORD ReadTotalTimeoutConstant;
274  DWORD WriteTotalTimeoutMultiplier;
275  DWORD WriteTotalTimeoutConstant;
277 
278 typedef struct
279 {
280  DWORD fCtsHold : 1;
281  DWORD fDsrHold : 1;
282  DWORD fRlsdHold : 1;
283  DWORD fXoffHold : 1;
284  DWORD fXoffSent : 1;
285  DWORD fEof : 1;
286  DWORD fTxim : 1;
287  DWORD fReserved : 25;
288  DWORD cbInQue;
289  DWORD cbOutQue;
290 } COMSTAT, *LPCOMSTAT;
291 
292 #ifdef __cplusplus
293 extern "C"
294 {
295 #endif
296 
297  WINPR_API BOOL BuildCommDCBA(LPCSTR lpDef, LPDCB lpDCB);
298  WINPR_API BOOL BuildCommDCBW(LPCWSTR lpDef, LPDCB lpDCB);
299 
300  WINPR_API BOOL BuildCommDCBAndTimeoutsA(LPCSTR lpDef, LPDCB lpDCB,
301  LPCOMMTIMEOUTS lpCommTimeouts);
302  WINPR_API BOOL BuildCommDCBAndTimeoutsW(LPCWSTR lpDef, LPDCB lpDCB,
303  LPCOMMTIMEOUTS lpCommTimeouts);
304 
305  WINPR_API BOOL CommConfigDialogA(LPCSTR lpszName, HWND hWnd, LPCOMMCONFIG lpCC);
306  WINPR_API BOOL CommConfigDialogW(LPCWSTR lpszName, HWND hWnd, LPCOMMCONFIG lpCC);
307 
308  WINPR_API BOOL GetCommConfig(HANDLE hCommDev, LPCOMMCONFIG lpCC, LPDWORD lpdwSize);
309  WINPR_API BOOL SetCommConfig(HANDLE hCommDev, LPCOMMCONFIG lpCC, DWORD dwSize);
310 
311  WINPR_API BOOL GetCommMask(HANDLE hFile, PDWORD lpEvtMask);
312  WINPR_API BOOL SetCommMask(HANDLE hFile, DWORD dwEvtMask);
313 
314  WINPR_API BOOL GetCommModemStatus(HANDLE hFile, PDWORD lpModemStat);
315  WINPR_API BOOL GetCommProperties(HANDLE hFile, LPCOMMPROP lpCommProp);
316 
317  WINPR_API BOOL GetCommState(HANDLE hFile, LPDCB lpDCB);
318  WINPR_API BOOL SetCommState(HANDLE hFile, LPDCB lpDCB);
319 
320  WINPR_API BOOL GetCommTimeouts(HANDLE hFile, LPCOMMTIMEOUTS lpCommTimeouts);
321  WINPR_API BOOL SetCommTimeouts(HANDLE hFile, LPCOMMTIMEOUTS lpCommTimeouts);
322 
323  WINPR_API BOOL GetDefaultCommConfigA(LPCSTR lpszName, LPCOMMCONFIG lpCC, LPDWORD lpdwSize);
324  WINPR_API BOOL GetDefaultCommConfigW(LPCWSTR lpszName, LPCOMMCONFIG lpCC, LPDWORD lpdwSize);
325 
326  WINPR_API BOOL SetDefaultCommConfigA(LPCSTR lpszName, LPCOMMCONFIG lpCC, DWORD dwSize);
327  WINPR_API BOOL SetDefaultCommConfigW(LPCWSTR lpszName, LPCOMMCONFIG lpCC, DWORD dwSize);
328 
329  WINPR_API BOOL SetCommBreak(HANDLE hFile);
330  WINPR_API BOOL ClearCommBreak(HANDLE hFile);
331  WINPR_API BOOL ClearCommError(HANDLE hFile, PDWORD lpErrors, LPCOMSTAT lpStat);
332 
333  WINPR_API BOOL PurgeComm(HANDLE hFile, DWORD dwFlags);
334  WINPR_API BOOL SetupComm(HANDLE hFile, DWORD dwInQueue, DWORD dwOutQueue);
335 
336  WINPR_API BOOL EscapeCommFunction(HANDLE hFile, DWORD dwFunc);
337 
338  WINPR_API BOOL TransmitCommChar(HANDLE hFile, char cChar);
339 
340  WINPR_API BOOL WaitCommEvent(HANDLE hFile, PDWORD lpEvtMask, LPOVERLAPPED lpOverlapped);
341 
342 #ifdef UNICODE
343 #define BuildCommDCB BuildCommDCBW
344 #define BuildCommDCBAndTimeouts BuildCommDCBAndTimeoutsW
345 #define CommConfigDialog CommConfigDialogW
346 #define GetDefaultCommConfig GetDefaultCommConfigW
347 #define SetDefaultCommConfig SetDefaultCommConfigW
348 #else
349 #define BuildCommDCB BuildCommDCBA
350 #define BuildCommDCBAndTimeouts BuildCommDCBAndTimeoutsA
351 #define CommConfigDialog CommConfigDialogA
352 #define GetDefaultCommConfig GetDefaultCommConfigA
353 #define SetDefaultCommConfig SetDefaultCommConfigA
354 #endif
355 
356 /* Extended API */
357 
358 /* FIXME: MAXULONG should be defined around winpr/limits.h */
359 #ifndef MAXULONG
360 #define MAXULONG (4294967295UL)
361 #endif
362 
367  typedef enum
368  {
369  SerialDriverUnknown = 0,
370  SerialDriverSerialSys,
371  SerialDriverSerCxSys,
372  SerialDriverSerCx2Sys /* default fallback, see also CommDeviceIoControl() */
373  } SERIAL_DRIVER_ID;
374 
375  /*
376  * About DefineCommDevice() / QueryDosDevice()
377  *
378  * Did something close to QueryDosDevice() and DefineDosDevice() but with
379  * following constraints:
380  * - mappings are stored in a static array.
381  * - QueryCommDevice returns only the mappings that have been defined through
382  * DefineCommDevice()
383  */
384  WINPR_API BOOL DefineCommDevice(/* DWORD dwFlags,*/ LPCTSTR lpDeviceName, LPCTSTR lpTargetPath);
385  WINPR_API DWORD QueryCommDevice(LPCTSTR lpDeviceName, LPTSTR lpTargetPath, DWORD ucchMax);
386  WINPR_API BOOL IsCommDevice(LPCTSTR lpDeviceName);
387 
393  WINPR_ATTR_MALLOC(CloseHandle, 1)
394  WINPR_API HANDLE CommCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
395  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
396  DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
397  HANDLE hTemplateFile);
398 
399 #define IOCTL_SERIAL_SET_BAUD_RATE 0x001B0004
400 #define IOCTL_SERIAL_GET_BAUD_RATE 0x001B0050
401 #define IOCTL_SERIAL_SET_LINE_CONTROL 0x001B000C
402 #define IOCTL_SERIAL_GET_LINE_CONTROL 0x001B0054
403 #define IOCTL_SERIAL_SET_TIMEOUTS 0x001B001C
404 #define IOCTL_SERIAL_GET_TIMEOUTS 0x001B0020
405 /* GET_CHARS and SET_CHARS are swapped in the RDP docs [MS-RDPESP] */
406 #define IOCTL_SERIAL_GET_CHARS 0x001B0058
407 #define IOCTL_SERIAL_SET_CHARS 0x001B005C
408 
409 #define IOCTL_SERIAL_SET_DTR 0x001B0024
410 #define IOCTL_SERIAL_CLR_DTR 0x001B0028
411 #define IOCTL_SERIAL_RESET_DEVICE 0x001B002C
412 #define IOCTL_SERIAL_SET_RTS 0x001B0030
413 #define IOCTL_SERIAL_CLR_RTS 0x001B0034
414 #define IOCTL_SERIAL_SET_XOFF 0x001B0038
415 #define IOCTL_SERIAL_SET_XON 0x001B003C
416 #define IOCTL_SERIAL_SET_BREAK_ON 0x001B0010
417 #define IOCTL_SERIAL_SET_BREAK_OFF 0x001B0014
418 #define IOCTL_SERIAL_SET_QUEUE_SIZE 0x001B0008
419 #define IOCTL_SERIAL_GET_WAIT_MASK 0x001B0040
420 #define IOCTL_SERIAL_SET_WAIT_MASK 0x001B0044
421 #define IOCTL_SERIAL_WAIT_ON_MASK 0x001B0048
422 #define IOCTL_SERIAL_IMMEDIATE_CHAR 0x001B0018
423 #define IOCTL_SERIAL_PURGE 0x001B004C
424 #define IOCTL_SERIAL_GET_HANDFLOW 0x001B0060
425 #define IOCTL_SERIAL_SET_HANDFLOW 0x001B0064
426 #define IOCTL_SERIAL_GET_MODEMSTATUS 0x001B0068
427 #define IOCTL_SERIAL_GET_DTRRTS 0x001B0078
428 
429 /* according to [MS-RDPESP] it should be 0x001B0084, but servers send 0x001B006C */
430 #define IOCTL_SERIAL_GET_COMMSTATUS 0x001B006C
431 
432 #define IOCTL_SERIAL_GET_PROPERTIES 0x001B0074
433 /* IOCTL_SERIAL_XOFF_COUNTER 0x001B0070 */
434 /* IOCTL_SERIAL_LSRMST_INSERT 0x001B007C */
435 #define IOCTL_SERIAL_CONFIG_SIZE 0x001B0080
436 /* IOCTL_SERIAL_GET_STATS 0x001B008C */
437 /* IOCTL_SERIAL_CLEAR_STATS 0x001B0090 */
438 /* IOCTL_SERIAL_GET_MODEM_CONTROL 0x001B0094 */
439 /* IOCTL_SERIAL_SET_MODEM_CONTROL 0x001B0098 */
440 /* IOCTL_SERIAL_SET_FIFO_CONTROL 0x001B009C */
441 
442 /* IOCTL_PAR_QUERY_INFORMATION 0x00160004 */
443 /* IOCTL_PAR_SET_INFORMATION 0x00160008 */
444 /* IOCTL_PAR_QUERY_DEVICE_ID 0x0016000C */
445 /* IOCTL_PAR_QUERY_DEVICE_ID_SIZE 0x00160010 */
446 /* IOCTL_IEEE1284_GET_MODE 0x00160014 */
447 /* IOCTL_IEEE1284_NEGOTIATE 0x00160018 */
448 /* IOCTL_PAR_SET_WRITE_ADDRESS 0x0016001C */
449 /* IOCTL_PAR_SET_READ_ADDRESS 0x00160020 */
450 /* IOCTL_PAR_GET_DEVICE_CAPS 0x00160024 */
451 /* IOCTL_PAR_GET_DEFAULT_MODES 0x00160028 */
452 /* IOCTL_PAR_QUERY_RAW_DEVICE_ID 0x00160030 */
453 /* IOCTL_PAR_IS_PORT_FREE 0x00160054 */
454 
455 /* http://msdn.microsoft.com/en-us/library/windows/hardware/ff551803(v=vs.85).aspx */
456 #define IOCTL_USBPRINT_GET_1284_ID 0x220034
457 
458  typedef struct
459  {
460  ULONG number;
461  const char* name;
463 
467  WINPR_API const char* _comm_serial_ioctl_name(ULONG number);
468 
472  WINPR_API void _comm_setServerSerialDriver(HANDLE hComm, SERIAL_DRIVER_ID);
473 
479  WINPR_API BOOL _comm_set_permissive(HANDLE hDevice, BOOL permissive);
480 
484  WINPR_API BOOL CommDeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer,
485  DWORD nInBufferSize, LPVOID lpOutBuffer,
486  DWORD nOutBufferSize, LPDWORD lpBytesReturned,
487  LPOVERLAPPED lpOverlapped);
488 
492  WINPR_API BOOL CommReadFile(HANDLE hDevice, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
493  LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped);
494 
498  WINPR_API BOOL CommWriteFile(HANDLE hDevice, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
499  LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped);
500 
501 #ifdef __cplusplus
502 }
503 #endif
504 
505 #endif /* WINPR_COMM_H */