/src/FreeRDP/winpr/libwinpr/comm/comm.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * WinPR: Windows Portable Runtime  | 
3  |  |  * Serial Communication API  | 
4  |  |  *  | 
5  |  |  * Copyright 2011 O.S. Systems Software Ltda.  | 
6  |  |  * Copyright 2011 Eduardo Fiss Beloni <beloni@ossystems.com.br>  | 
7  |  |  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>  | 
8  |  |  * Copyright 2014 Hewlett-Packard Development Company, L.P.  | 
9  |  |  *  | 
10  |  |  * Licensed under the Apache License, Version 2.0 (the "License");  | 
11  |  |  * you may not use this file except in compliance with the License.  | 
12  |  |  * You may obtain a copy of the License at  | 
13  |  |  *  | 
14  |  |  *     http://www.apache.org/licenses/LICENSE-2.0  | 
15  |  |  *  | 
16  |  |  * Unless required by applicable law or agreed to in writing, software  | 
17  |  |  * distributed under the License is distributed on an "AS IS" BASIS,  | 
18  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
19  |  |  * See the License for the specific language governing permissions and  | 
20  |  |  * limitations under the License.  | 
21  |  |  */  | 
22  |  |  | 
23  |  | #include <winpr/config.h>  | 
24  |  |  | 
25  |  | #if defined __linux__ && !defined ANDROID  | 
26  |  |  | 
27  |  | #include <winpr/assert.h>  | 
28  |  | #include <errno.h>  | 
29  |  | #include <fcntl.h>  | 
30  |  | #include <pthread.h>  | 
31  |  | #include <stdarg.h>  | 
32  |  | #include <sys/ioctl.h>  | 
33  |  | #include <sys/stat.h>  | 
34  |  | #include <sys/types.h>  | 
35  |  | #include <termios.h>  | 
36  |  | #include <unistd.h>  | 
37  |  |  | 
38  |  | #include <winpr/crt.h>  | 
39  |  | #include <winpr/comm.h>  | 
40  |  | #include <winpr/tchar.h>  | 
41  |  | #include <winpr/wlog.h>  | 
42  |  | #include <winpr/handle.h>  | 
43  |  |  | 
44  |  | #include "comm_ioctl.h"  | 
45  |  |  | 
46  |  | /**  | 
47  |  |  * Communication Resources:  | 
48  |  |  * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363196/  | 
49  |  |  */  | 
50  |  |  | 
51  |  | #include "comm.h"  | 
52  |  |  | 
53  |  | static wLog* _Log = NULL;  | 
54  |  |  | 
55  |  | struct comm_device  | 
56  |  | { | 
57  |  |   LPTSTR name;  | 
58  |  |   LPTSTR path;  | 
59  |  | };  | 
60  |  |  | 
61  |  | typedef struct comm_device COMM_DEVICE;  | 
62  |  |  | 
63  |  | /* FIXME: get a clever data structure, see also io.h functions */  | 
64  |  | /* _CommDevices is a NULL-terminated array with a maximun of COMM_DEVICE_MAX COMM_DEVICE */  | 
65  | 0  | #define COMM_DEVICE_MAX 128  | 
66  |  | static COMM_DEVICE** _CommDevices = NULL;  | 
67  |  | static CRITICAL_SECTION _CommDevicesLock;  | 
68  |  |  | 
69  |  | static HANDLE_CREATOR _CommHandleCreator;  | 
70  |  |  | 
71  |  | static pthread_once_t _CommInitialized = PTHREAD_ONCE_INIT;  | 
72  |  |  | 
73  |  | static int CommGetFd(HANDLE handle)  | 
74  | 0  | { | 
75  | 0  |   WINPR_COMM* comm = (WINPR_COMM*)handle;  | 
76  |  | 
  | 
77  | 0  |   if (!CommIsHandled(handle))  | 
78  | 0  |     return -1;  | 
79  |  |  | 
80  | 0  |   return comm->fd;  | 
81  | 0  | }  | 
82  |  |  | 
83  |  | HANDLE_CREATOR* GetCommHandleCreator(void)  | 
84  | 0  | { | 
85  | 0  |   _CommHandleCreator.IsHandled = IsCommDevice;  | 
86  | 0  |   _CommHandleCreator.CreateFileA = CommCreateFileA;  | 
87  | 0  |   return &_CommHandleCreator;  | 
88  | 0  | }  | 
89  |  |  | 
90  |  | static void _CommInit(void)  | 
91  | 0  | { | 
92  |  |   /* NB: error management to be done outside of this function */  | 
93  | 0  |   WINPR_ASSERT(_Log == NULL);  | 
94  | 0  |   WINPR_ASSERT(_CommDevices == NULL);  | 
95  | 0  |   _CommDevices = (COMM_DEVICE**)calloc(COMM_DEVICE_MAX + 1, sizeof(COMM_DEVICE*));  | 
96  |  | 
  | 
97  | 0  |   if (!_CommDevices)  | 
98  | 0  |     return;  | 
99  |  |  | 
100  | 0  |   if (!InitializeCriticalSectionEx(&_CommDevicesLock, 0, 0))  | 
101  | 0  |   { | 
102  | 0  |     free(_CommDevices);  | 
103  | 0  |     _CommDevices = NULL;  | 
104  | 0  |     return;  | 
105  | 0  |   }  | 
106  |  |  | 
107  | 0  |   _Log = WLog_Get("com.winpr.comm"); | 
108  | 0  |   WINPR_ASSERT(_Log != NULL);  | 
109  | 0  | }  | 
110  |  |  | 
111  |  | /**  | 
112  |  |  * Returns TRUE when the comm module is correctly intialized, FALSE otherwise  | 
113  |  |  * with ERROR_DLL_INIT_FAILED set as the last error.  | 
114  |  |  */  | 
115  |  | static BOOL CommInitialized(void)  | 
116  | 0  | { | 
117  | 0  |   if (pthread_once(&_CommInitialized, _CommInit) != 0)  | 
118  | 0  |   { | 
119  | 0  |     SetLastError(ERROR_DLL_INIT_FAILED);  | 
120  | 0  |     return FALSE;  | 
121  | 0  |   }  | 
122  |  |  | 
123  | 0  |   return TRUE;  | 
124  | 0  | }  | 
125  |  |  | 
126  |  | void CommLog_Print(DWORD level, ...)  | 
127  | 0  | { | 
128  | 0  |   if (!CommInitialized())  | 
129  | 0  |     return;  | 
130  |  |  | 
131  | 0  |   va_list ap;  | 
132  | 0  |   va_start(ap, level);  | 
133  | 0  |   WLog_PrintVA(_Log, level, ap);  | 
134  | 0  |   va_end(ap);  | 
135  | 0  | }  | 
136  |  |  | 
137  |  | BOOL BuildCommDCBA(LPCSTR lpDef, LPDCB lpDCB)  | 
138  | 0  | { | 
139  | 0  |   if (!CommInitialized())  | 
140  | 0  |     return FALSE;  | 
141  |  |  | 
142  |  |   /* TODO: not implemented */  | 
143  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
144  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
145  | 0  |   return FALSE;  | 
146  | 0  | }  | 
147  |  |  | 
148  |  | BOOL BuildCommDCBW(LPCWSTR lpDef, LPDCB lpDCB)  | 
149  | 0  | { | 
150  | 0  |   if (!CommInitialized())  | 
151  | 0  |     return FALSE;  | 
152  |  |  | 
153  |  |   /* TODO: not implemented */  | 
154  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
155  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
156  | 0  |   return FALSE;  | 
157  | 0  | }  | 
158  |  |  | 
159  |  | BOOL BuildCommDCBAndTimeoutsA(LPCSTR lpDef, LPDCB lpDCB, LPCOMMTIMEOUTS lpCommTimeouts)  | 
160  | 0  | { | 
161  | 0  |   if (!CommInitialized())  | 
162  | 0  |     return FALSE;  | 
163  |  |  | 
164  |  |   /* TODO: not implemented */  | 
165  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
166  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
167  | 0  |   return FALSE;  | 
168  | 0  | }  | 
169  |  |  | 
170  |  | BOOL BuildCommDCBAndTimeoutsW(LPCWSTR lpDef, LPDCB lpDCB, LPCOMMTIMEOUTS lpCommTimeouts)  | 
171  | 0  | { | 
172  | 0  |   if (!CommInitialized())  | 
173  | 0  |     return FALSE;  | 
174  |  |  | 
175  |  |   /* TODO: not implemented */  | 
176  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
177  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
178  | 0  |   return FALSE;  | 
179  | 0  | }  | 
180  |  |  | 
181  |  | BOOL CommConfigDialogA(LPCSTR lpszName, HWND hWnd, LPCOMMCONFIG lpCC)  | 
182  | 0  | { | 
183  | 0  |   if (!CommInitialized())  | 
184  | 0  |     return FALSE;  | 
185  |  |  | 
186  |  |   /* TODO: not implemented */  | 
187  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
188  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
189  | 0  |   return FALSE;  | 
190  | 0  | }  | 
191  |  |  | 
192  |  | BOOL CommConfigDialogW(LPCWSTR lpszName, HWND hWnd, LPCOMMCONFIG lpCC)  | 
193  | 0  | { | 
194  | 0  |   if (!CommInitialized())  | 
195  | 0  |     return FALSE;  | 
196  |  |  | 
197  |  |   /* TODO: not implemented */  | 
198  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
199  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
200  | 0  |   return FALSE;  | 
201  | 0  | }  | 
202  |  |  | 
203  |  | BOOL GetCommConfig(HANDLE hCommDev, LPCOMMCONFIG lpCC, LPDWORD lpdwSize)  | 
204  | 0  | { | 
205  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hCommDev;  | 
206  |  | 
  | 
207  | 0  |   if (!CommInitialized())  | 
208  | 0  |     return FALSE;  | 
209  |  |  | 
210  |  |   /* TODO: not implemented */  | 
211  |  |  | 
212  | 0  |   if (!pComm)  | 
213  | 0  |     return FALSE;  | 
214  |  |  | 
215  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
216  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
217  | 0  |   return FALSE;  | 
218  | 0  | }  | 
219  |  |  | 
220  |  | BOOL SetCommConfig(HANDLE hCommDev, LPCOMMCONFIG lpCC, DWORD dwSize)  | 
221  | 0  | { | 
222  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hCommDev;  | 
223  |  | 
  | 
224  | 0  |   if (!CommInitialized())  | 
225  | 0  |     return FALSE;  | 
226  |  |  | 
227  |  |   /* TODO: not implemented */  | 
228  |  |  | 
229  | 0  |   if (!pComm)  | 
230  | 0  |     return FALSE;  | 
231  |  |  | 
232  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
233  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
234  | 0  |   return FALSE;  | 
235  | 0  | }  | 
236  |  |  | 
237  |  | BOOL GetCommMask(HANDLE hFile, PDWORD lpEvtMask)  | 
238  | 0  | { | 
239  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
240  |  | 
  | 
241  | 0  |   if (!CommInitialized())  | 
242  | 0  |     return FALSE;  | 
243  |  |  | 
244  |  |   /* TODO: not implemented */  | 
245  |  |  | 
246  | 0  |   if (!pComm)  | 
247  | 0  |     return FALSE;  | 
248  |  |  | 
249  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
250  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
251  | 0  |   return FALSE;  | 
252  | 0  | }  | 
253  |  |  | 
254  |  | BOOL SetCommMask(HANDLE hFile, DWORD dwEvtMask)  | 
255  | 0  | { | 
256  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
257  |  | 
  | 
258  | 0  |   if (!CommInitialized())  | 
259  | 0  |     return FALSE;  | 
260  |  |  | 
261  |  |   /* TODO: not implemented */  | 
262  |  |  | 
263  | 0  |   if (!pComm)  | 
264  | 0  |     return FALSE;  | 
265  |  |  | 
266  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
267  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
268  | 0  |   return FALSE;  | 
269  | 0  | }  | 
270  |  |  | 
271  |  | BOOL GetCommModemStatus(HANDLE hFile, PDWORD lpModemStat)  | 
272  | 0  | { | 
273  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
274  |  | 
  | 
275  | 0  |   if (!CommInitialized())  | 
276  | 0  |     return FALSE;  | 
277  |  |  | 
278  |  |   /* TODO: not implemented */  | 
279  |  |  | 
280  | 0  |   if (!pComm)  | 
281  | 0  |     return FALSE;  | 
282  |  |  | 
283  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
284  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
285  | 0  |   return FALSE;  | 
286  | 0  | }  | 
287  |  |  | 
288  |  | /**  | 
289  |  |  * ERRORS:  | 
290  |  |  *   ERROR_DLL_INIT_FAILED  | 
291  |  |  *   ERROR_INVALID_HANDLE  | 
292  |  |  */  | 
293  |  | BOOL GetCommProperties(HANDLE hFile, LPCOMMPROP lpCommProp)  | 
294  | 0  | { | 
295  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
296  | 0  |   DWORD bytesReturned = 0;  | 
297  |  | 
  | 
298  | 0  |   if (!CommIsHandleValid(hFile))  | 
299  | 0  |     return FALSE;  | 
300  |  |  | 
301  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_PROPERTIES, NULL, 0, lpCommProp,  | 
302  | 0  |                            sizeof(COMMPROP), &bytesReturned, NULL))  | 
303  | 0  |   { | 
304  | 0  |     CommLog_Print(WLOG_WARN, "GetCommProperties failure.");  | 
305  | 0  |     return FALSE;  | 
306  | 0  |   }  | 
307  |  |  | 
308  | 0  |   return TRUE;  | 
309  | 0  | }  | 
310  |  |  | 
311  |  | /**  | 
312  |  |  *  | 
313  |  |  *  | 
314  |  |  * ERRORS:  | 
315  |  |  *   ERROR_INVALID_HANDLE  | 
316  |  |  *   ERROR_INVALID_DATA  | 
317  |  |  *   ERROR_IO_DEVICE  | 
318  |  |  *   ERROR_OUTOFMEMORY  | 
319  |  |  */  | 
320  |  | BOOL GetCommState(HANDLE hFile, LPDCB lpDCB)  | 
321  | 0  | { | 
322  | 0  |   DCB* lpLocalDcb = NULL;  | 
323  | 0  |   struct termios currentState;  | 
324  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
325  | 0  |   DWORD bytesReturned = 0;  | 
326  |  | 
  | 
327  | 0  |   if (!CommIsHandleValid(hFile))  | 
328  | 0  |     return FALSE;  | 
329  |  |  | 
330  | 0  |   if (!lpDCB)  | 
331  | 0  |   { | 
332  | 0  |     SetLastError(ERROR_INVALID_DATA);  | 
333  | 0  |     return FALSE;  | 
334  | 0  |   }  | 
335  |  |  | 
336  | 0  |   if (lpDCB->DCBlength < sizeof(DCB))  | 
337  | 0  |   { | 
338  | 0  |     SetLastError(ERROR_INVALID_DATA);  | 
339  | 0  |     return FALSE;  | 
340  | 0  |   }  | 
341  |  |  | 
342  | 0  |   if (tcgetattr(pComm->fd, ¤tState) < 0)  | 
343  | 0  |   { | 
344  | 0  |     SetLastError(ERROR_IO_DEVICE);  | 
345  | 0  |     return FALSE;  | 
346  | 0  |   }  | 
347  |  |  | 
348  | 0  |   lpLocalDcb = (DCB*)calloc(1, lpDCB->DCBlength);  | 
349  |  | 
  | 
350  | 0  |   if (lpLocalDcb == NULL)  | 
351  | 0  |   { | 
352  | 0  |     SetLastError(ERROR_OUTOFMEMORY);  | 
353  | 0  |     return FALSE;  | 
354  | 0  |   }  | 
355  |  |  | 
356  |  |   /* error_handle */  | 
357  | 0  |   lpLocalDcb->DCBlength = lpDCB->DCBlength;  | 
358  | 0  |   SERIAL_BAUD_RATE baudRate;  | 
359  |  | 
  | 
360  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_BAUD_RATE, NULL, 0, &baudRate,  | 
361  | 0  |                            sizeof(SERIAL_BAUD_RATE), &bytesReturned, NULL))  | 
362  | 0  |   { | 
363  | 0  |     CommLog_Print(WLOG_WARN, "GetCommState failure: could not get the baud rate.");  | 
364  | 0  |     goto error_handle;  | 
365  | 0  |   }  | 
366  |  |  | 
367  | 0  |   lpLocalDcb->BaudRate = baudRate.BaudRate;  | 
368  | 0  |   lpLocalDcb->fBinary = (currentState.c_cflag & ICANON) == 0;  | 
369  |  | 
  | 
370  | 0  |   if (!lpLocalDcb->fBinary)  | 
371  | 0  |   { | 
372  | 0  |     CommLog_Print(WLOG_WARN, "Unexpected nonbinary mode, consider to unset the ICANON flag.");  | 
373  | 0  |   }  | 
374  |  | 
  | 
375  | 0  |   lpLocalDcb->fParity = (currentState.c_iflag & INPCK) != 0;  | 
376  | 0  |   SERIAL_HANDFLOW handflow;  | 
377  |  | 
  | 
378  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_HANDFLOW, NULL, 0, &handflow,  | 
379  | 0  |                            sizeof(SERIAL_HANDFLOW), &bytesReturned, NULL))  | 
380  | 0  |   { | 
381  | 0  |     CommLog_Print(WLOG_WARN, "GetCommState failure: could not get the handflow settings.");  | 
382  | 0  |     goto error_handle;  | 
383  | 0  |   }  | 
384  |  |  | 
385  | 0  |   lpLocalDcb->fOutxCtsFlow = (handflow.ControlHandShake & SERIAL_CTS_HANDSHAKE) != 0;  | 
386  | 0  |   lpLocalDcb->fOutxDsrFlow = (handflow.ControlHandShake & SERIAL_DSR_HANDSHAKE) != 0;  | 
387  |  | 
  | 
388  | 0  |   if (handflow.ControlHandShake & SERIAL_DTR_HANDSHAKE)  | 
389  | 0  |   { | 
390  | 0  |     lpLocalDcb->fDtrControl = DTR_CONTROL_HANDSHAKE;  | 
391  | 0  |   }  | 
392  | 0  |   else if (handflow.ControlHandShake & SERIAL_DTR_CONTROL)  | 
393  | 0  |   { | 
394  | 0  |     lpLocalDcb->fDtrControl = DTR_CONTROL_ENABLE;  | 
395  | 0  |   }  | 
396  | 0  |   else  | 
397  | 0  |   { | 
398  | 0  |     lpLocalDcb->fDtrControl = DTR_CONTROL_DISABLE;  | 
399  | 0  |   }  | 
400  |  | 
  | 
401  | 0  |   lpLocalDcb->fDsrSensitivity = (handflow.ControlHandShake & SERIAL_DSR_SENSITIVITY) != 0;  | 
402  | 0  |   lpLocalDcb->fTXContinueOnXoff = (handflow.FlowReplace & SERIAL_XOFF_CONTINUE) != 0;  | 
403  | 0  |   lpLocalDcb->fOutX = (handflow.FlowReplace & SERIAL_AUTO_TRANSMIT) != 0;  | 
404  | 0  |   lpLocalDcb->fInX = (handflow.FlowReplace & SERIAL_AUTO_RECEIVE) != 0;  | 
405  | 0  |   lpLocalDcb->fErrorChar = (handflow.FlowReplace & SERIAL_ERROR_CHAR) != 0;  | 
406  | 0  |   lpLocalDcb->fNull = (handflow.FlowReplace & SERIAL_NULL_STRIPPING) != 0;  | 
407  |  | 
  | 
408  | 0  |   if (handflow.FlowReplace & SERIAL_RTS_HANDSHAKE)  | 
409  | 0  |   { | 
410  | 0  |     lpLocalDcb->fRtsControl = RTS_CONTROL_HANDSHAKE;  | 
411  | 0  |   }  | 
412  | 0  |   else if (handflow.FlowReplace & SERIAL_RTS_CONTROL)  | 
413  | 0  |   { | 
414  | 0  |     lpLocalDcb->fRtsControl = RTS_CONTROL_ENABLE;  | 
415  | 0  |   }  | 
416  | 0  |   else  | 
417  | 0  |   { | 
418  | 0  |     lpLocalDcb->fRtsControl = RTS_CONTROL_DISABLE;  | 
419  | 0  |   }  | 
420  |  |  | 
421  |  |   // FIXME: how to get the RTS_CONTROL_TOGGLE state? Does it match the UART 16750's Autoflow  | 
422  |  |   // Control Enabled bit in its Modem Control Register (MCR)  | 
423  | 0  |   lpLocalDcb->fAbortOnError = (handflow.ControlHandShake & SERIAL_ERROR_ABORT) != 0;  | 
424  |  |   /* lpLocalDcb->fDummy2 not used */  | 
425  | 0  |   lpLocalDcb->wReserved = 0; /* must be zero */  | 
426  | 0  |   lpLocalDcb->XonLim = handflow.XonLimit;  | 
427  | 0  |   lpLocalDcb->XoffLim = handflow.XoffLimit;  | 
428  | 0  |   SERIAL_LINE_CONTROL lineControl;  | 
429  |  | 
  | 
430  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_LINE_CONTROL, NULL, 0, &lineControl,  | 
431  | 0  |                            sizeof(SERIAL_LINE_CONTROL), &bytesReturned, NULL))  | 
432  | 0  |   { | 
433  | 0  |     CommLog_Print(WLOG_WARN, "GetCommState failure: could not get the control settings.");  | 
434  | 0  |     goto error_handle;  | 
435  | 0  |   }  | 
436  |  |  | 
437  | 0  |   lpLocalDcb->ByteSize = lineControl.WordLength;  | 
438  | 0  |   lpLocalDcb->Parity = lineControl.Parity;  | 
439  | 0  |   lpLocalDcb->StopBits = lineControl.StopBits;  | 
440  | 0  |   SERIAL_CHARS serialChars;  | 
441  |  | 
  | 
442  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_CHARS, NULL, 0, &serialChars,  | 
443  | 0  |                            sizeof(SERIAL_CHARS), &bytesReturned, NULL))  | 
444  | 0  |   { | 
445  | 0  |     CommLog_Print(WLOG_WARN, "GetCommState failure: could not get the serial chars.");  | 
446  | 0  |     goto error_handle;  | 
447  | 0  |   }  | 
448  |  |  | 
449  | 0  |   lpLocalDcb->XonChar = serialChars.XonChar;  | 
450  | 0  |   lpLocalDcb->XoffChar = serialChars.XoffChar;  | 
451  | 0  |   lpLocalDcb->ErrorChar = serialChars.ErrorChar;  | 
452  | 0  |   lpLocalDcb->EofChar = serialChars.EofChar;  | 
453  | 0  |   lpLocalDcb->EvtChar = serialChars.EventChar;  | 
454  | 0  |   memcpy(lpDCB, lpLocalDcb, lpDCB->DCBlength);  | 
455  | 0  |   free(lpLocalDcb);  | 
456  | 0  |   return TRUE;  | 
457  | 0  | error_handle:  | 
458  | 0  |   free(lpLocalDcb);  | 
459  | 0  |   return FALSE;  | 
460  | 0  | }  | 
461  |  |  | 
462  |  | /**  | 
463  |  |  * @return TRUE on success, FALSE otherwise.  | 
464  |  |  *  | 
465  |  |  * As of today, SetCommState() can fail half-way with some settings  | 
466  |  |  * applied and some others not. SetCommState() returns on the first  | 
467  |  |  * failure met. FIXME: or is it correct?  | 
468  |  |  *  | 
469  |  |  * ERRORS:  | 
470  |  |  *   ERROR_INVALID_HANDLE  | 
471  |  |  *   ERROR_IO_DEVICE  | 
472  |  |  */  | 
473  |  | BOOL SetCommState(HANDLE hFile, LPDCB lpDCB)  | 
474  | 0  | { | 
475  | 0  |   struct termios upcomingTermios = { 0 }; | 
476  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
477  | 0  |   DWORD bytesReturned = 0;  | 
478  |  |  | 
479  |  |   /* FIXME: validate changes according GetCommProperties? */  | 
480  |  | 
  | 
481  | 0  |   if (!CommIsHandleValid(hFile))  | 
482  | 0  |     return FALSE;  | 
483  |  |  | 
484  | 0  |   if (!lpDCB)  | 
485  | 0  |   { | 
486  | 0  |     SetLastError(ERROR_INVALID_DATA);  | 
487  | 0  |     return FALSE;  | 
488  | 0  |   }  | 
489  |  |  | 
490  |  |   /* NB: did the choice to call ioctls first when available and  | 
491  |  |      then to setup upcomingTermios. Don't mix both stages. */  | 
492  |  |   /** ioctl calls stage **/  | 
493  | 0  |   SERIAL_BAUD_RATE baudRate;  | 
494  | 0  |   baudRate.BaudRate = lpDCB->BaudRate;  | 
495  |  | 
  | 
496  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_BAUD_RATE, &baudRate, sizeof(SERIAL_BAUD_RATE),  | 
497  | 0  |                            NULL, 0, &bytesReturned, NULL))  | 
498  | 0  |   { | 
499  | 0  |     CommLog_Print(WLOG_WARN, "SetCommState failure: could not set the baud rate.");  | 
500  | 0  |     return FALSE;  | 
501  | 0  |   }  | 
502  |  |  | 
503  | 0  |   SERIAL_CHARS serialChars;  | 
504  |  | 
  | 
505  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_CHARS, NULL, 0, &serialChars,  | 
506  | 0  |                            sizeof(SERIAL_CHARS), &bytesReturned,  | 
507  | 0  |                            NULL)) /* as of today, required for BreakChar */  | 
508  | 0  |   { | 
509  | 0  |     CommLog_Print(WLOG_WARN, "SetCommState failure: could not get the initial serial chars.");  | 
510  | 0  |     return FALSE;  | 
511  | 0  |   }  | 
512  |  |  | 
513  | 0  |   serialChars.XonChar = lpDCB->XonChar;  | 
514  | 0  |   serialChars.XoffChar = lpDCB->XoffChar;  | 
515  | 0  |   serialChars.ErrorChar = lpDCB->ErrorChar;  | 
516  | 0  |   serialChars.EofChar = lpDCB->EofChar;  | 
517  | 0  |   serialChars.EventChar = lpDCB->EvtChar;  | 
518  |  | 
  | 
519  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_CHARS, &serialChars, sizeof(SERIAL_CHARS),  | 
520  | 0  |                            NULL, 0, &bytesReturned, NULL))  | 
521  | 0  |   { | 
522  | 0  |     CommLog_Print(WLOG_WARN, "SetCommState failure: could not set the serial chars.");  | 
523  | 0  |     return FALSE;  | 
524  | 0  |   }  | 
525  |  |  | 
526  | 0  |   SERIAL_LINE_CONTROL lineControl;  | 
527  | 0  |   lineControl.StopBits = lpDCB->StopBits;  | 
528  | 0  |   lineControl.Parity = lpDCB->Parity;  | 
529  | 0  |   lineControl.WordLength = lpDCB->ByteSize;  | 
530  |  | 
  | 
531  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_LINE_CONTROL, &lineControl,  | 
532  | 0  |                            sizeof(SERIAL_LINE_CONTROL), NULL, 0, &bytesReturned, NULL))  | 
533  | 0  |   { | 
534  | 0  |     CommLog_Print(WLOG_WARN, "SetCommState failure: could not set the control settings.");  | 
535  | 0  |     return FALSE;  | 
536  | 0  |   }  | 
537  |  |  | 
538  | 0  |   SERIAL_HANDFLOW handflow = { 0 }; | 
539  |  | 
  | 
540  | 0  |   if (lpDCB->fOutxCtsFlow)  | 
541  | 0  |   { | 
542  | 0  |     handflow.ControlHandShake |= SERIAL_CTS_HANDSHAKE;  | 
543  | 0  |   }  | 
544  |  | 
  | 
545  | 0  |   if (lpDCB->fOutxDsrFlow)  | 
546  | 0  |   { | 
547  | 0  |     handflow.ControlHandShake |= SERIAL_DSR_HANDSHAKE;  | 
548  | 0  |   }  | 
549  |  | 
  | 
550  | 0  |   switch (lpDCB->fDtrControl)  | 
551  | 0  |   { | 
552  | 0  |     case SERIAL_DTR_HANDSHAKE:  | 
553  | 0  |       handflow.ControlHandShake |= DTR_CONTROL_HANDSHAKE;  | 
554  | 0  |       break;  | 
555  |  |  | 
556  | 0  |     case SERIAL_DTR_CONTROL:  | 
557  | 0  |       handflow.ControlHandShake |= DTR_CONTROL_ENABLE;  | 
558  | 0  |       break;  | 
559  |  |  | 
560  | 0  |     case DTR_CONTROL_DISABLE:  | 
561  |  |       /* do nothing since handflow is init-zeroed */  | 
562  | 0  |       break;  | 
563  |  |  | 
564  | 0  |     default:  | 
565  | 0  |       CommLog_Print(WLOG_WARN, "Unexpected fDtrControl value: %" PRIu32 "\n",  | 
566  | 0  |                     lpDCB->fDtrControl);  | 
567  | 0  |       return FALSE;  | 
568  | 0  |   }  | 
569  |  |  | 
570  | 0  |   if (lpDCB->fDsrSensitivity)  | 
571  | 0  |   { | 
572  | 0  |     handflow.ControlHandShake |= SERIAL_DSR_SENSITIVITY;  | 
573  | 0  |   }  | 
574  |  | 
  | 
575  | 0  |   if (lpDCB->fTXContinueOnXoff)  | 
576  | 0  |   { | 
577  | 0  |     handflow.FlowReplace |= SERIAL_XOFF_CONTINUE;  | 
578  | 0  |   }  | 
579  |  | 
  | 
580  | 0  |   if (lpDCB->fOutX)  | 
581  | 0  |   { | 
582  | 0  |     handflow.FlowReplace |= SERIAL_AUTO_TRANSMIT;  | 
583  | 0  |   }  | 
584  |  | 
  | 
585  | 0  |   if (lpDCB->fInX)  | 
586  | 0  |   { | 
587  | 0  |     handflow.FlowReplace |= SERIAL_AUTO_RECEIVE;  | 
588  | 0  |   }  | 
589  |  | 
  | 
590  | 0  |   if (lpDCB->fErrorChar)  | 
591  | 0  |   { | 
592  | 0  |     handflow.FlowReplace |= SERIAL_ERROR_CHAR;  | 
593  | 0  |   }  | 
594  |  | 
  | 
595  | 0  |   if (lpDCB->fNull)  | 
596  | 0  |   { | 
597  | 0  |     handflow.FlowReplace |= SERIAL_NULL_STRIPPING;  | 
598  | 0  |   }  | 
599  |  | 
  | 
600  | 0  |   switch (lpDCB->fRtsControl)  | 
601  | 0  |   { | 
602  | 0  |     case RTS_CONTROL_TOGGLE:  | 
603  | 0  |       CommLog_Print(WLOG_WARN, "Unsupported RTS_CONTROL_TOGGLE feature");  | 
604  |  |       // FIXME: see also GetCommState()  | 
605  | 0  |       return FALSE;  | 
606  |  |  | 
607  | 0  |     case RTS_CONTROL_HANDSHAKE:  | 
608  | 0  |       handflow.FlowReplace |= SERIAL_RTS_HANDSHAKE;  | 
609  | 0  |       break;  | 
610  |  |  | 
611  | 0  |     case RTS_CONTROL_ENABLE:  | 
612  | 0  |       handflow.FlowReplace |= SERIAL_RTS_CONTROL;  | 
613  | 0  |       break;  | 
614  |  |  | 
615  | 0  |     case RTS_CONTROL_DISABLE:  | 
616  |  |       /* do nothing since handflow is init-zeroed */  | 
617  | 0  |       break;  | 
618  |  |  | 
619  | 0  |     default:  | 
620  | 0  |       CommLog_Print(WLOG_WARN, "Unexpected fRtsControl value: %" PRIu32 "\n",  | 
621  | 0  |                     lpDCB->fRtsControl);  | 
622  | 0  |       return FALSE;  | 
623  | 0  |   }  | 
624  |  |  | 
625  | 0  |   if (lpDCB->fAbortOnError)  | 
626  | 0  |   { | 
627  | 0  |     handflow.ControlHandShake |= SERIAL_ERROR_ABORT;  | 
628  | 0  |   }  | 
629  |  |  | 
630  |  |   /* lpDCB->fDummy2 not used */  | 
631  |  |   /* lpLocalDcb->wReserved  ignored */  | 
632  | 0  |   handflow.XonLimit = lpDCB->XonLim;  | 
633  | 0  |   handflow.XoffLimit = lpDCB->XoffLim;  | 
634  |  | 
  | 
635  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_HANDFLOW, &handflow, sizeof(SERIAL_HANDFLOW),  | 
636  | 0  |                            NULL, 0, &bytesReturned, NULL))  | 
637  | 0  |   { | 
638  | 0  |     CommLog_Print(WLOG_WARN, "SetCommState failure: could not set the handflow settings.");  | 
639  | 0  |     return FALSE;  | 
640  | 0  |   }  | 
641  |  |  | 
642  |  |   /** upcomingTermios stage **/  | 
643  |  |  | 
644  | 0  |   if (tcgetattr(pComm->fd, &upcomingTermios) <  | 
645  | 0  |       0) /* NB: preserves current settings not directly handled by the Communication Functions */  | 
646  | 0  |   { | 
647  | 0  |     SetLastError(ERROR_IO_DEVICE);  | 
648  | 0  |     return FALSE;  | 
649  | 0  |   }  | 
650  |  |  | 
651  | 0  |   if (lpDCB->fBinary)  | 
652  | 0  |   { | 
653  | 0  |     upcomingTermios.c_lflag &= ~ICANON;  | 
654  | 0  |   }  | 
655  | 0  |   else  | 
656  | 0  |   { | 
657  | 0  |     upcomingTermios.c_lflag |= ICANON;  | 
658  | 0  |     CommLog_Print(WLOG_WARN, "Unexpected nonbinary mode, consider to unset the ICANON flag.");  | 
659  | 0  |   }  | 
660  |  | 
  | 
661  | 0  |   if (lpDCB->fParity)  | 
662  | 0  |   { | 
663  | 0  |     upcomingTermios.c_iflag |= INPCK;  | 
664  | 0  |   }  | 
665  | 0  |   else  | 
666  | 0  |   { | 
667  | 0  |     upcomingTermios.c_iflag &= ~INPCK;  | 
668  | 0  |   }  | 
669  |  |  | 
670  |  |   /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363423%28v=vs.85%29.aspx  | 
671  |  |    *  | 
672  |  |    * The SetCommState function reconfigures the communications  | 
673  |  |    * resource, but it does not affect the internal output and  | 
674  |  |    * input buffers of the specified driver. The buffers are not  | 
675  |  |    * flushed, and pending read and write operations are not  | 
676  |  |    * terminated prematurely.  | 
677  |  |    *  | 
678  |  |    * TCSANOW matches the best this definition  | 
679  |  |    */  | 
680  |  | 
  | 
681  | 0  |   if (_comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &upcomingTermios) < 0)  | 
682  | 0  |   { | 
683  | 0  |     SetLastError(ERROR_IO_DEVICE);  | 
684  | 0  |     return FALSE;  | 
685  | 0  |   }  | 
686  |  |  | 
687  | 0  |   return TRUE;  | 
688  | 0  | }  | 
689  |  |  | 
690  |  | /**  | 
691  |  |  * ERRORS:  | 
692  |  |  *   ERROR_INVALID_HANDLE  | 
693  |  |  */  | 
694  |  | BOOL GetCommTimeouts(HANDLE hFile, LPCOMMTIMEOUTS lpCommTimeouts)  | 
695  | 0  | { | 
696  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
697  | 0  |   DWORD bytesReturned = 0;  | 
698  |  | 
  | 
699  | 0  |   if (!CommIsHandleValid(hFile))  | 
700  | 0  |     return FALSE;  | 
701  |  |  | 
702  |  |   /* as of today, SERIAL_TIMEOUTS and COMMTIMEOUTS structures are identical */  | 
703  |  |  | 
704  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, lpCommTimeouts,  | 
705  | 0  |                            sizeof(COMMTIMEOUTS), &bytesReturned, NULL))  | 
706  | 0  |   { | 
707  | 0  |     CommLog_Print(WLOG_WARN, "GetCommTimeouts failure.");  | 
708  | 0  |     return FALSE;  | 
709  | 0  |   }  | 
710  |  |  | 
711  | 0  |   return TRUE;  | 
712  | 0  | }  | 
713  |  |  | 
714  |  | /**  | 
715  |  |  * ERRORS:  | 
716  |  |  *   ERROR_INVALID_HANDLE  | 
717  |  |  */  | 
718  |  | BOOL SetCommTimeouts(HANDLE hFile, LPCOMMTIMEOUTS lpCommTimeouts)  | 
719  | 0  | { | 
720  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
721  | 0  |   DWORD bytesReturned = 0;  | 
722  |  | 
  | 
723  | 0  |   if (!CommIsHandleValid(hFile))  | 
724  | 0  |     return FALSE;  | 
725  |  |  | 
726  |  |   /* as of today, SERIAL_TIMEOUTS and COMMTIMEOUTS structures are identical */  | 
727  |  |  | 
728  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_TIMEOUTS, lpCommTimeouts, sizeof(COMMTIMEOUTS),  | 
729  | 0  |                            NULL, 0, &bytesReturned, NULL))  | 
730  | 0  |   { | 
731  | 0  |     CommLog_Print(WLOG_WARN, "SetCommTimeouts failure.");  | 
732  | 0  |     return FALSE;  | 
733  | 0  |   }  | 
734  |  |  | 
735  | 0  |   return TRUE;  | 
736  | 0  | }  | 
737  |  |  | 
738  |  | BOOL GetDefaultCommConfigA(LPCSTR lpszName, LPCOMMCONFIG lpCC, LPDWORD lpdwSize)  | 
739  | 0  | { | 
740  | 0  |   if (!CommInitialized())  | 
741  | 0  |     return FALSE;  | 
742  |  |  | 
743  |  |   /* TODO: not implemented */  | 
744  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
745  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
746  | 0  |   return FALSE;  | 
747  | 0  | }  | 
748  |  |  | 
749  |  | BOOL GetDefaultCommConfigW(LPCWSTR lpszName, LPCOMMCONFIG lpCC, LPDWORD lpdwSize)  | 
750  | 0  | { | 
751  | 0  |   if (!CommInitialized())  | 
752  | 0  |     return FALSE;  | 
753  |  |  | 
754  |  |   /* TODO: not implemented */  | 
755  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
756  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
757  | 0  |   return FALSE;  | 
758  | 0  | }  | 
759  |  |  | 
760  |  | BOOL SetDefaultCommConfigA(LPCSTR lpszName, LPCOMMCONFIG lpCC, DWORD dwSize)  | 
761  | 0  | { | 
762  | 0  |   if (!CommInitialized())  | 
763  | 0  |     return FALSE;  | 
764  |  |  | 
765  |  |   /* TODO: not implemented */  | 
766  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
767  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
768  | 0  |   return FALSE;  | 
769  | 0  | }  | 
770  |  |  | 
771  |  | BOOL SetDefaultCommConfigW(LPCWSTR lpszName, LPCOMMCONFIG lpCC, DWORD dwSize)  | 
772  | 0  | { | 
773  | 0  |   if (!CommInitialized())  | 
774  | 0  |     return FALSE;  | 
775  |  |  | 
776  |  |   /* TODO: not implemented */  | 
777  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
778  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
779  | 0  |   return FALSE;  | 
780  | 0  | }  | 
781  |  |  | 
782  |  | BOOL SetCommBreak(HANDLE hFile)  | 
783  | 0  | { | 
784  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
785  |  | 
  | 
786  | 0  |   if (!CommInitialized())  | 
787  | 0  |     return FALSE;  | 
788  |  |  | 
789  |  |   /* TODO: not implemented */  | 
790  |  |  | 
791  | 0  |   if (!pComm)  | 
792  | 0  |     return FALSE;  | 
793  |  |  | 
794  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
795  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
796  | 0  |   return FALSE;  | 
797  | 0  | }  | 
798  |  |  | 
799  |  | BOOL ClearCommBreak(HANDLE hFile)  | 
800  | 0  | { | 
801  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
802  |  | 
  | 
803  | 0  |   if (!CommInitialized())  | 
804  | 0  |     return FALSE;  | 
805  |  |  | 
806  |  |   /* TODO: not implemented */  | 
807  |  |  | 
808  | 0  |   if (!pComm)  | 
809  | 0  |     return FALSE;  | 
810  |  |  | 
811  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
812  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
813  | 0  |   return FALSE;  | 
814  | 0  | }  | 
815  |  |  | 
816  |  | BOOL ClearCommError(HANDLE hFile, PDWORD lpErrors, LPCOMSTAT lpStat)  | 
817  | 0  | { | 
818  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
819  |  | 
  | 
820  | 0  |   if (!CommInitialized())  | 
821  | 0  |     return FALSE;  | 
822  |  |  | 
823  |  |   /* TODO: not implemented */  | 
824  |  |  | 
825  | 0  |   if (!pComm)  | 
826  | 0  |     return FALSE;  | 
827  |  |  | 
828  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
829  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
830  | 0  |   return FALSE;  | 
831  | 0  | }  | 
832  |  |  | 
833  |  | BOOL PurgeComm(HANDLE hFile, DWORD dwFlags)  | 
834  | 0  | { | 
835  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
836  | 0  |   DWORD bytesReturned = 0;  | 
837  |  | 
  | 
838  | 0  |   if (!CommIsHandleValid(hFile))  | 
839  | 0  |     return FALSE;  | 
840  |  |  | 
841  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_PURGE, &dwFlags, sizeof(DWORD), NULL, 0,  | 
842  | 0  |                            &bytesReturned, NULL))  | 
843  | 0  |   { | 
844  | 0  |     CommLog_Print(WLOG_WARN, "PurgeComm failure.");  | 
845  | 0  |     return FALSE;  | 
846  | 0  |   }  | 
847  |  |  | 
848  | 0  |   return TRUE;  | 
849  | 0  | }  | 
850  |  |  | 
851  |  | BOOL SetupComm(HANDLE hFile, DWORD dwInQueue, DWORD dwOutQueue)  | 
852  | 0  | { | 
853  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
854  | 0  |   SERIAL_QUEUE_SIZE queueSize;  | 
855  | 0  |   DWORD bytesReturned = 0;  | 
856  |  | 
  | 
857  | 0  |   if (!CommIsHandleValid(hFile))  | 
858  | 0  |     return FALSE;  | 
859  |  |  | 
860  | 0  |   queueSize.InSize = dwInQueue;  | 
861  | 0  |   queueSize.OutSize = dwOutQueue;  | 
862  |  | 
  | 
863  | 0  |   if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_QUEUE_SIZE, &queueSize,  | 
864  | 0  |                            sizeof(SERIAL_QUEUE_SIZE), NULL, 0, &bytesReturned, NULL))  | 
865  | 0  |   { | 
866  | 0  |     CommLog_Print(WLOG_WARN, "SetCommTimeouts failure.");  | 
867  | 0  |     return FALSE;  | 
868  | 0  |   }  | 
869  |  |  | 
870  | 0  |   return TRUE;  | 
871  | 0  | }  | 
872  |  |  | 
873  |  | BOOL EscapeCommFunction(HANDLE hFile, DWORD dwFunc)  | 
874  | 0  | { | 
875  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
876  |  | 
  | 
877  | 0  |   if (!CommInitialized())  | 
878  | 0  |     return FALSE;  | 
879  |  |  | 
880  |  |   /* TODO: not implemented */  | 
881  |  |  | 
882  | 0  |   if (!pComm)  | 
883  | 0  |     return FALSE;  | 
884  |  |  | 
885  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
886  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
887  | 0  |   return FALSE;  | 
888  | 0  | }  | 
889  |  |  | 
890  |  | BOOL TransmitCommChar(HANDLE hFile, char cChar)  | 
891  | 0  | { | 
892  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
893  |  | 
  | 
894  | 0  |   if (!CommInitialized())  | 
895  | 0  |     return FALSE;  | 
896  |  |  | 
897  |  |   /* TODO: not implemented */  | 
898  |  |  | 
899  | 0  |   if (!pComm)  | 
900  | 0  |     return FALSE;  | 
901  |  |  | 
902  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
903  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
904  | 0  |   return FALSE;  | 
905  | 0  | }  | 
906  |  |  | 
907  |  | BOOL WaitCommEvent(HANDLE hFile, PDWORD lpEvtMask, LPOVERLAPPED lpOverlapped)  | 
908  | 0  | { | 
909  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)hFile;  | 
910  |  | 
  | 
911  | 0  |   if (!CommInitialized())  | 
912  | 0  |     return FALSE;  | 
913  |  |  | 
914  |  |   /* TODO: not implemented */  | 
915  |  |  | 
916  | 0  |   if (!pComm)  | 
917  | 0  |     return FALSE;  | 
918  |  |  | 
919  | 0  |   CommLog_Print(WLOG_ERROR, "Not implemented");  | 
920  | 0  |   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  | 
921  | 0  |   return FALSE;  | 
922  | 0  | }  | 
923  |  |  | 
924  |  | /**  | 
925  |  |  * Returns TRUE on success, FALSE otherwise. To get extended error  | 
926  |  |  * information, call GetLastError.  | 
927  |  |  *  | 
928  |  |  * ERRORS:  | 
929  |  |  *   ERROR_DLL_INIT_FAILED  | 
930  |  |  *   ERROR_OUTOFMEMORY was not possible to get mappings.  | 
931  |  |  *   ERROR_INVALID_DATA was not possible to add the device.  | 
932  |  |  */  | 
933  |  | BOOL DefineCommDevice(/* DWORD dwFlags,*/ LPCTSTR lpDeviceName, LPCTSTR lpTargetPath)  | 
934  | 0  | { | 
935  | 0  |   LPTSTR storedDeviceName = NULL;  | 
936  | 0  |   LPTSTR storedTargetPath = NULL;  | 
937  |  | 
  | 
938  | 0  |   if (!CommInitialized())  | 
939  | 0  |     return FALSE;  | 
940  |  |  | 
941  | 0  |   EnterCriticalSection(&_CommDevicesLock);  | 
942  |  | 
  | 
943  | 0  |   if (_CommDevices == NULL)  | 
944  | 0  |   { | 
945  | 0  |     SetLastError(ERROR_DLL_INIT_FAILED);  | 
946  | 0  |     goto error_handle;  | 
947  | 0  |   }  | 
948  |  |  | 
949  | 0  |   storedDeviceName = _tcsdup(lpDeviceName);  | 
950  |  | 
  | 
951  | 0  |   if (storedDeviceName == NULL)  | 
952  | 0  |   { | 
953  | 0  |     SetLastError(ERROR_OUTOFMEMORY);  | 
954  | 0  |     goto error_handle;  | 
955  | 0  |   }  | 
956  |  |  | 
957  | 0  |   storedTargetPath = _tcsdup(lpTargetPath);  | 
958  |  | 
  | 
959  | 0  |   if (storedTargetPath == NULL)  | 
960  | 0  |   { | 
961  | 0  |     SetLastError(ERROR_OUTOFMEMORY);  | 
962  | 0  |     goto error_handle;  | 
963  | 0  |   }  | 
964  |  |  | 
965  | 0  |   int i = 0;  | 
966  | 0  |   for (; i < COMM_DEVICE_MAX; i++)  | 
967  | 0  |   { | 
968  | 0  |     if (_CommDevices[i] != NULL)  | 
969  | 0  |     { | 
970  | 0  |       if (_tcscmp(_CommDevices[i]->name, storedDeviceName) == 0)  | 
971  | 0  |       { | 
972  |  |         /* take over the emplacement */  | 
973  | 0  |         free(_CommDevices[i]->name);  | 
974  | 0  |         free(_CommDevices[i]->path);  | 
975  | 0  |         _CommDevices[i]->name = storedDeviceName;  | 
976  | 0  |         _CommDevices[i]->path = storedTargetPath;  | 
977  | 0  |         break;  | 
978  | 0  |       }  | 
979  | 0  |     }  | 
980  | 0  |     else  | 
981  | 0  |     { | 
982  |  |       /* new emplacement */  | 
983  | 0  |       _CommDevices[i] = (COMM_DEVICE*)calloc(1, sizeof(COMM_DEVICE));  | 
984  |  | 
  | 
985  | 0  |       if (_CommDevices[i] == NULL)  | 
986  | 0  |       { | 
987  | 0  |         SetLastError(ERROR_OUTOFMEMORY);  | 
988  | 0  |         goto error_handle;  | 
989  | 0  |       }  | 
990  |  |  | 
991  | 0  |       _CommDevices[i]->name = storedDeviceName;  | 
992  | 0  |       _CommDevices[i]->path = storedTargetPath;  | 
993  | 0  |       break;  | 
994  | 0  |     }  | 
995  | 0  |   }  | 
996  |  |  | 
997  | 0  |   if (i == COMM_DEVICE_MAX)  | 
998  | 0  |   { | 
999  | 0  |     SetLastError(ERROR_OUTOFMEMORY);  | 
1000  | 0  |     goto error_handle;  | 
1001  | 0  |   }  | 
1002  |  |  | 
1003  | 0  |   LeaveCriticalSection(&_CommDevicesLock);  | 
1004  | 0  |   return TRUE;  | 
1005  | 0  | error_handle:  | 
1006  | 0  |   free(storedDeviceName);  | 
1007  | 0  |   free(storedTargetPath);  | 
1008  | 0  |   LeaveCriticalSection(&_CommDevicesLock);  | 
1009  | 0  |   return FALSE;  | 
1010  | 0  | }  | 
1011  |  |  | 
1012  |  | /**  | 
1013  |  |  * Returns the number of target paths in the buffer pointed to by  | 
1014  |  |  * lpTargetPath.  | 
1015  |  |  *  | 
1016  |  |  * The current implementation returns in any case 0 and 1 target  | 
1017  |  |  * path. A NULL lpDeviceName is not supported yet to get all the  | 
1018  |  |  * paths.  | 
1019  |  |  *  | 
1020  |  |  * ERRORS:  | 
1021  |  |  *   ERROR_SUCCESS  | 
1022  |  |  *   ERROR_DLL_INIT_FAILED  | 
1023  |  |  *   ERROR_OUTOFMEMORY was not possible to get mappings.  | 
1024  |  |  *   ERROR_NOT_SUPPORTED equivalent QueryDosDevice feature not supported.  | 
1025  |  |  *   ERROR_INVALID_DATA was not possible to retrieve any device information.  | 
1026  |  |  *   ERROR_INSUFFICIENT_BUFFER too small lpTargetPath  | 
1027  |  |  */  | 
1028  |  | DWORD QueryCommDevice(LPCTSTR lpDeviceName, LPTSTR lpTargetPath, DWORD ucchMax)  | 
1029  | 0  | { | 
1030  | 0  |   LPTSTR storedTargetPath = NULL;  | 
1031  | 0  |   SetLastError(ERROR_SUCCESS);  | 
1032  |  | 
  | 
1033  | 0  |   if (!CommInitialized())  | 
1034  | 0  |     return 0;  | 
1035  |  |  | 
1036  | 0  |   if (_CommDevices == NULL)  | 
1037  | 0  |   { | 
1038  | 0  |     SetLastError(ERROR_DLL_INIT_FAILED);  | 
1039  | 0  |     return 0;  | 
1040  | 0  |   }  | 
1041  |  |  | 
1042  | 0  |   if (lpDeviceName == NULL || lpTargetPath == NULL)  | 
1043  | 0  |   { | 
1044  | 0  |     SetLastError(ERROR_NOT_SUPPORTED);  | 
1045  | 0  |     return 0;  | 
1046  | 0  |   }  | 
1047  |  |  | 
1048  | 0  |   EnterCriticalSection(&_CommDevicesLock);  | 
1049  | 0  |   storedTargetPath = NULL;  | 
1050  |  | 
  | 
1051  | 0  |   for (int i = 0; i < COMM_DEVICE_MAX; i++)  | 
1052  | 0  |   { | 
1053  | 0  |     if (_CommDevices[i] != NULL)  | 
1054  | 0  |     { | 
1055  | 0  |       if (_tcscmp(_CommDevices[i]->name, lpDeviceName) == 0)  | 
1056  | 0  |       { | 
1057  | 0  |         storedTargetPath = _CommDevices[i]->path;  | 
1058  | 0  |         break;  | 
1059  | 0  |       }  | 
1060  |  |  | 
1061  | 0  |       continue;  | 
1062  | 0  |     }  | 
1063  |  |  | 
1064  | 0  |     break;  | 
1065  | 0  |   }  | 
1066  |  | 
  | 
1067  | 0  |   LeaveCriticalSection(&_CommDevicesLock);  | 
1068  |  | 
  | 
1069  | 0  |   if (storedTargetPath == NULL)  | 
1070  | 0  |   { | 
1071  | 0  |     SetLastError(ERROR_INVALID_DATA);  | 
1072  | 0  |     return 0;  | 
1073  | 0  |   }  | 
1074  |  |  | 
1075  | 0  |   if (_tcslen(storedTargetPath) + 2 > ucchMax)  | 
1076  | 0  |   { | 
1077  | 0  |     SetLastError(ERROR_INSUFFICIENT_BUFFER);  | 
1078  | 0  |     return 0;  | 
1079  | 0  |   }  | 
1080  |  |  | 
1081  | 0  |   _tcscpy(lpTargetPath, storedTargetPath);  | 
1082  | 0  |   lpTargetPath[_tcslen(storedTargetPath) + 1] = '\0'; /* 2nd final '\0' */  | 
1083  | 0  |   return _tcslen(lpTargetPath) + 2;  | 
1084  | 0  | }  | 
1085  |  |  | 
1086  |  | /**  | 
1087  |  |  * Checks whether lpDeviceName is a valid and registered Communication device.  | 
1088  |  |  */  | 
1089  |  | BOOL IsCommDevice(LPCTSTR lpDeviceName)  | 
1090  | 0  | { | 
1091  | 0  |   TCHAR lpTargetPath[MAX_PATH];  | 
1092  |  | 
  | 
1093  | 0  |   if (!CommInitialized())  | 
1094  | 0  |     return FALSE;  | 
1095  |  |  | 
1096  | 0  |   if (QueryCommDevice(lpDeviceName, lpTargetPath, MAX_PATH) > 0)  | 
1097  | 0  |   { | 
1098  | 0  |     return TRUE;  | 
1099  | 0  |   }  | 
1100  |  |  | 
1101  | 0  |   return FALSE;  | 
1102  | 0  | }  | 
1103  |  |  | 
1104  |  | /**  | 
1105  |  |  * Sets  | 
1106  |  |  */  | 
1107  |  | void _comm_setServerSerialDriver(HANDLE hComm, SERIAL_DRIVER_ID driverId)  | 
1108  | 0  | { | 
1109  | 0  |   ULONG Type = 0;  | 
1110  | 0  |   WINPR_HANDLE* Object = NULL;  | 
1111  | 0  |   WINPR_COMM* pComm = NULL;  | 
1112  |  | 
  | 
1113  | 0  |   if (!CommInitialized())  | 
1114  | 0  |     return;  | 
1115  |  |  | 
1116  | 0  |   if (!winpr_Handle_GetInfo(hComm, &Type, &Object))  | 
1117  | 0  |   { | 
1118  | 0  |     CommLog_Print(WLOG_WARN, "_comm_setServerSerialDriver failure");  | 
1119  | 0  |     return;  | 
1120  | 0  |   }  | 
1121  |  |  | 
1122  | 0  |   pComm = (WINPR_COMM*)Object;  | 
1123  | 0  |   pComm->serverSerialDriverId = driverId;  | 
1124  | 0  | }  | 
1125  |  |  | 
1126  |  | static HANDLE_OPS ops = { CommIsHandled, CommCloseHandle, | 
1127  |  |                         CommGetFd,     NULL, /* CleanupHandle */  | 
1128  |  |                         NULL,          NULL,  | 
1129  |  |                         NULL,          NULL,  | 
1130  |  |                         NULL,          NULL,  | 
1131  |  |                         NULL,          NULL,  | 
1132  |  |                         NULL,          NULL,  | 
1133  |  |                         NULL,          NULL,  | 
1134  |  |                         NULL,          NULL,  | 
1135  |  |                         NULL,          NULL,  | 
1136  |  |                         NULL };  | 
1137  |  |  | 
1138  |  | /**  | 
1139  |  |  * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363198%28v=vs.85%29.aspx  | 
1140  |  |  *  | 
1141  |  |  * @param lpDeviceName e.g. COM1, ...  | 
1142  |  |  *  | 
1143  |  |  * @param dwDesiredAccess expects GENERIC_READ | GENERIC_WRITE, a  | 
1144  |  |  * warning message is printed otherwise. TODO: better support.  | 
1145  |  |  *  | 
1146  |  |  * @param dwShareMode must be zero, INVALID_HANDLE_VALUE is returned  | 
1147  |  |  * otherwise and GetLastError() should return ERROR_SHARING_VIOLATION.  | 
1148  |  |  *  | 
1149  |  |  * @param lpSecurityAttributes NULL expected, a warning message is printed  | 
1150  |  |  * otherwise. TODO: better support.  | 
1151  |  |  *  | 
1152  |  |  * @param dwCreationDisposition must be OPEN_EXISTING. If the  | 
1153  |  |  * communication device doesn't exist INVALID_HANDLE_VALUE is returned  | 
1154  |  |  * and GetLastError() returns ERROR_FILE_NOT_FOUND.  | 
1155  |  |  *  | 
1156  |  |  * @param dwFlagsAndAttributes zero expected, a warning message is  | 
1157  |  |  * printed otherwise.  | 
1158  |  |  *  | 
1159  |  |  * @param hTemplateFile must be NULL.  | 
1160  |  |  *  | 
1161  |  |  * @return INVALID_HANDLE_VALUE on error.  | 
1162  |  |  */  | 
1163  |  | HANDLE CommCreateFileA(LPCSTR lpDeviceName, DWORD dwDesiredAccess, DWORD dwShareMode,  | 
1164  |  |                        LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,  | 
1165  |  |                        DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)  | 
1166  | 0  | { | 
1167  | 0  |   CHAR devicePath[MAX_PATH] = { 0 }; | 
1168  | 0  |   struct stat deviceStat = { 0 }; | 
1169  | 0  |   WINPR_COMM* pComm = NULL;  | 
1170  | 0  |   struct termios upcomingTermios = { 0 }; | 
1171  |  | 
  | 
1172  | 0  |   if (!CommInitialized())  | 
1173  | 0  |     return INVALID_HANDLE_VALUE;  | 
1174  |  |  | 
1175  | 0  |   if (dwDesiredAccess != (GENERIC_READ | GENERIC_WRITE))  | 
1176  | 0  |   { | 
1177  | 0  |     CommLog_Print(WLOG_WARN, "unexpected access to the device: 0x%08" PRIX32 "",  | 
1178  | 0  |                   dwDesiredAccess);  | 
1179  | 0  |   }  | 
1180  |  | 
  | 
1181  | 0  |   if (dwShareMode != 0)  | 
1182  | 0  |   { | 
1183  | 0  |     SetLastError(ERROR_SHARING_VIOLATION);  | 
1184  | 0  |     return INVALID_HANDLE_VALUE;  | 
1185  | 0  |   }  | 
1186  |  |  | 
1187  |  |   /* TODO: Prevents other processes from opening a file or  | 
1188  |  |    * device if they request delete, read, or write access. */  | 
1189  |  |  | 
1190  | 0  |   if (lpSecurityAttributes != NULL)  | 
1191  | 0  |   { | 
1192  | 0  |     CommLog_Print(WLOG_WARN, "unexpected security attributes, nLength=%" PRIu32 "",  | 
1193  | 0  |                   lpSecurityAttributes->nLength);  | 
1194  | 0  |   }  | 
1195  |  | 
  | 
1196  | 0  |   if (dwCreationDisposition != OPEN_EXISTING)  | 
1197  | 0  |   { | 
1198  | 0  |     SetLastError(ERROR_FILE_NOT_FOUND); /* FIXME: ERROR_NOT_SUPPORTED better? */  | 
1199  | 0  |     return INVALID_HANDLE_VALUE;  | 
1200  | 0  |   }  | 
1201  |  |  | 
1202  | 0  |   if (QueryCommDevice(lpDeviceName, devicePath, MAX_PATH) <= 0)  | 
1203  | 0  |   { | 
1204  |  |     /* SetLastError(GetLastError()); */  | 
1205  | 0  |     return INVALID_HANDLE_VALUE;  | 
1206  | 0  |   }  | 
1207  |  |  | 
1208  | 0  |   if (stat(devicePath, &deviceStat) < 0)  | 
1209  | 0  |   { | 
1210  | 0  |     CommLog_Print(WLOG_WARN, "device not found %s", devicePath);  | 
1211  | 0  |     SetLastError(ERROR_FILE_NOT_FOUND);  | 
1212  | 0  |     return INVALID_HANDLE_VALUE;  | 
1213  | 0  |   }  | 
1214  |  |  | 
1215  | 0  |   if (!S_ISCHR(deviceStat.st_mode))  | 
1216  | 0  |   { | 
1217  | 0  |     CommLog_Print(WLOG_WARN, "bad device %s", devicePath);  | 
1218  | 0  |     SetLastError(ERROR_BAD_DEVICE);  | 
1219  | 0  |     return INVALID_HANDLE_VALUE;  | 
1220  | 0  |   }  | 
1221  |  |  | 
1222  | 0  |   if (dwFlagsAndAttributes != 0)  | 
1223  | 0  |   { | 
1224  | 0  |     CommLog_Print(WLOG_WARN, "unexpected flags and attributes: 0x%08" PRIX32 "",  | 
1225  | 0  |                   dwFlagsAndAttributes);  | 
1226  | 0  |   }  | 
1227  |  | 
  | 
1228  | 0  |   if (hTemplateFile != NULL)  | 
1229  | 0  |   { | 
1230  | 0  |     SetLastError(ERROR_NOT_SUPPORTED); /* FIXME: other proper error? */  | 
1231  | 0  |     return INVALID_HANDLE_VALUE;  | 
1232  | 0  |   }  | 
1233  |  |  | 
1234  | 0  |   pComm = (WINPR_COMM*)calloc(1, sizeof(WINPR_COMM));  | 
1235  |  | 
  | 
1236  | 0  |   if (pComm == NULL)  | 
1237  | 0  |   { | 
1238  | 0  |     SetLastError(ERROR_OUTOFMEMORY);  | 
1239  | 0  |     return INVALID_HANDLE_VALUE;  | 
1240  | 0  |   }  | 
1241  |  |  | 
1242  | 0  |   WINPR_HANDLE_SET_TYPE_AND_MODE(pComm, HANDLE_TYPE_COMM, WINPR_FD_READ);  | 
1243  | 0  |   pComm->common.ops = &ops;  | 
1244  |  |   /* error_handle */  | 
1245  | 0  |   pComm->fd = open(devicePath, O_RDWR | O_NOCTTY | O_NONBLOCK);  | 
1246  |  | 
  | 
1247  | 0  |   if (pComm->fd < 0)  | 
1248  | 0  |   { | 
1249  | 0  |     CommLog_Print(WLOG_WARN, "failed to open device %s", devicePath);  | 
1250  | 0  |     SetLastError(ERROR_BAD_DEVICE);  | 
1251  | 0  |     goto error_handle;  | 
1252  | 0  |   }  | 
1253  |  |  | 
1254  | 0  |   pComm->fd_read = open(devicePath, O_RDONLY | O_NOCTTY | O_NONBLOCK);  | 
1255  |  | 
  | 
1256  | 0  |   if (pComm->fd_read < 0)  | 
1257  | 0  |   { | 
1258  | 0  |     CommLog_Print(WLOG_WARN, "failed to open fd_read, device: %s", devicePath);  | 
1259  | 0  |     SetLastError(ERROR_BAD_DEVICE);  | 
1260  | 0  |     goto error_handle;  | 
1261  | 0  |   }  | 
1262  |  |  | 
1263  | 0  |   pComm->fd_read_event = eventfd(  | 
1264  | 0  |       0, EFD_NONBLOCK); /* EFD_NONBLOCK required because a read() is not always expected */  | 
1265  |  | 
  | 
1266  | 0  |   if (pComm->fd_read_event < 0)  | 
1267  | 0  |   { | 
1268  | 0  |     CommLog_Print(WLOG_WARN, "failed to open fd_read_event, device: %s", devicePath);  | 
1269  | 0  |     SetLastError(ERROR_BAD_DEVICE);  | 
1270  | 0  |     goto error_handle;  | 
1271  | 0  |   }  | 
1272  |  |  | 
1273  | 0  |   InitializeCriticalSection(&pComm->ReadLock);  | 
1274  | 0  |   pComm->fd_write = open(devicePath, O_WRONLY | O_NOCTTY | O_NONBLOCK);  | 
1275  |  | 
  | 
1276  | 0  |   if (pComm->fd_write < 0)  | 
1277  | 0  |   { | 
1278  | 0  |     CommLog_Print(WLOG_WARN, "failed to open fd_write, device: %s", devicePath);  | 
1279  | 0  |     SetLastError(ERROR_BAD_DEVICE);  | 
1280  | 0  |     goto error_handle;  | 
1281  | 0  |   }  | 
1282  |  |  | 
1283  | 0  |   pComm->fd_write_event = eventfd(  | 
1284  | 0  |       0, EFD_NONBLOCK); /* EFD_NONBLOCK required because a read() is not always expected */  | 
1285  |  | 
  | 
1286  | 0  |   if (pComm->fd_write_event < 0)  | 
1287  | 0  |   { | 
1288  | 0  |     CommLog_Print(WLOG_WARN, "failed to open fd_write_event, device: %s", devicePath);  | 
1289  | 0  |     SetLastError(ERROR_BAD_DEVICE);  | 
1290  | 0  |     goto error_handle;  | 
1291  | 0  |   }  | 
1292  |  |  | 
1293  | 0  |   InitializeCriticalSection(&pComm->WriteLock);  | 
1294  |  |   /* can also be setup later on with _comm_setServerSerialDriver() */  | 
1295  | 0  |   pComm->serverSerialDriverId = SerialDriverUnknown;  | 
1296  | 0  |   InitializeCriticalSection(&pComm->EventsLock);  | 
1297  |  | 
  | 
1298  | 0  |   if (ioctl(pComm->fd, TIOCGICOUNT, &(pComm->counters)) < 0)  | 
1299  | 0  |   { | 
1300  | 0  |     char ebuffer[256] = { 0 }; | 
1301  | 0  |     CommLog_Print(WLOG_WARN, "TIOCGICOUNT ioctl failed, errno=[%d] %s.", errno,  | 
1302  | 0  |                   winpr_strerror(errno, ebuffer, sizeof(ebuffer)));  | 
1303  | 0  |     CommLog_Print(WLOG_WARN, "could not read counters.");  | 
1304  |  |     /* could not initialize counters but keep on.  | 
1305  |  |      *  | 
1306  |  |      * Not all drivers, especially for USB to serial  | 
1307  |  |      * adapters (e.g. those based on pl2303), does support  | 
1308  |  |      * this call.  | 
1309  |  |      */  | 
1310  | 0  |     ZeroMemory(&(pComm->counters), sizeof(struct serial_icounter_struct));  | 
1311  | 0  |   }  | 
1312  |  |  | 
1313  |  |   /* The binary/raw mode is required for the redirection but  | 
1314  |  |    * only flags that are not handle somewhere-else, except  | 
1315  |  |    * ICANON, are forced here. */  | 
1316  | 0  |   ZeroMemory(&upcomingTermios, sizeof(struct termios));  | 
1317  |  | 
  | 
1318  | 0  |   if (tcgetattr(pComm->fd, &upcomingTermios) < 0)  | 
1319  | 0  |   { | 
1320  | 0  |     SetLastError(ERROR_IO_DEVICE);  | 
1321  | 0  |     goto error_handle;  | 
1322  | 0  |   }  | 
1323  |  |  | 
1324  | 0  |   upcomingTermios.c_iflag &=  | 
1325  | 0  |       ~(/*IGNBRK |*/ BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL /*| IXON*/);  | 
1326  | 0  |   upcomingTermios.c_oflag = 0; /* <=> &= ~OPOST */  | 
1327  | 0  |   upcomingTermios.c_lflag = 0; /* <=> &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); */  | 
1328  |  |   /* upcomingTermios.c_cflag &= ~(CSIZE | PARENB); */  | 
1329  |  |   /* upcomingTermios.c_cflag |= CS8; */  | 
1330  |  |   /* About missing flags recommended by termios(3):  | 
1331  |  |    *  | 
1332  |  |    *   IGNBRK and IXON, see: IOCTL_SERIAL_SET_HANDFLOW  | 
1333  |  |    *   CSIZE, PARENB and CS8, see: IOCTL_SERIAL_SET_LINE_CONTROL  | 
1334  |  |    */  | 
1335  |  |   /* a few more settings required for the redirection */  | 
1336  | 0  |   upcomingTermios.c_cflag |= CLOCAL | CREAD;  | 
1337  |  | 
  | 
1338  | 0  |   if (_comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &upcomingTermios) < 0)  | 
1339  | 0  |   { | 
1340  | 0  |     SetLastError(ERROR_IO_DEVICE);  | 
1341  | 0  |     goto error_handle;  | 
1342  | 0  |   }  | 
1343  |  |  | 
1344  | 0  |   return (HANDLE)pComm;  | 
1345  | 0  | error_handle:  | 
1346  | 0  |   WINPR_PRAGMA_DIAG_PUSH  | 
1347  | 0  |   WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC  | 
1348  | 0  |   CloseHandle(pComm);  | 
1349  | 0  |   WINPR_PRAGMA_DIAG_POP  | 
1350  | 0  |   return INVALID_HANDLE_VALUE;  | 
1351  | 0  | }  | 
1352  |  |  | 
1353  |  | BOOL CommIsHandled(HANDLE handle)  | 
1354  | 0  | { | 
1355  | 0  |   if (!CommInitialized())  | 
1356  | 0  |     return FALSE;  | 
1357  |  |  | 
1358  | 0  |   return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_COMM, TRUE);  | 
1359  | 0  | }  | 
1360  |  |  | 
1361  |  | BOOL CommIsHandleValid(HANDLE handle)  | 
1362  | 0  | { | 
1363  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)handle;  | 
1364  | 0  |   if (!CommIsHandled(handle))  | 
1365  | 0  |     return FALSE;  | 
1366  | 0  |   if (pComm->fd <= 0)  | 
1367  | 0  |   { | 
1368  | 0  |     SetLastError(ERROR_INVALID_HANDLE);  | 
1369  | 0  |     return FALSE;  | 
1370  | 0  |   }  | 
1371  | 0  |   return TRUE;  | 
1372  | 0  | }  | 
1373  |  |  | 
1374  |  | BOOL CommCloseHandle(HANDLE handle)  | 
1375  | 0  | { | 
1376  | 0  |   WINPR_COMM* pComm = (WINPR_COMM*)handle;  | 
1377  |  | 
  | 
1378  | 0  |   if (!CommIsHandled(handle))  | 
1379  | 0  |     return FALSE;  | 
1380  |  |  | 
1381  | 0  |   if (pComm->PendingEvents & SERIAL_EV_WINPR_WAITING)  | 
1382  | 0  |   { | 
1383  | 0  |     ULONG WaitMask = 0;  | 
1384  | 0  |     DWORD BytesReturned = 0;  | 
1385  |  |  | 
1386  |  |     /* ensures to gracefully stop the WAIT_ON_MASK's loop */  | 
1387  | 0  |     if (!CommDeviceIoControl(handle, IOCTL_SERIAL_SET_WAIT_MASK, &WaitMask, sizeof(ULONG), NULL,  | 
1388  | 0  |                              0, &BytesReturned, NULL))  | 
1389  | 0  |     { | 
1390  | 0  |       CommLog_Print(WLOG_WARN, "failure to WAIT_ON_MASK's loop!");  | 
1391  | 0  |     }  | 
1392  | 0  |   }  | 
1393  |  | 
  | 
1394  | 0  |   DeleteCriticalSection(&pComm->ReadLock);  | 
1395  | 0  |   DeleteCriticalSection(&pComm->WriteLock);  | 
1396  | 0  |   DeleteCriticalSection(&pComm->EventsLock);  | 
1397  |  | 
  | 
1398  | 0  |   if (pComm->fd > 0)  | 
1399  | 0  |     close(pComm->fd);  | 
1400  |  | 
  | 
1401  | 0  |   if (pComm->fd_write > 0)  | 
1402  | 0  |     close(pComm->fd_write);  | 
1403  |  | 
  | 
1404  | 0  |   if (pComm->fd_write_event > 0)  | 
1405  | 0  |     close(pComm->fd_write_event);  | 
1406  |  | 
  | 
1407  | 0  |   if (pComm->fd_read > 0)  | 
1408  | 0  |     close(pComm->fd_read);  | 
1409  |  | 
  | 
1410  | 0  |   if (pComm->fd_read_event > 0)  | 
1411  | 0  |     close(pComm->fd_read_event);  | 
1412  |  | 
  | 
1413  | 0  |   free(pComm);  | 
1414  | 0  |   return TRUE;  | 
1415  | 0  | }  | 
1416  |  |  | 
1417  |  | #ifndef WITH_EVENTFD_READ_WRITE  | 
1418  |  | int eventfd_read(int fd, eventfd_t* value)  | 
1419  |  | { | 
1420  |  |   return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;  | 
1421  |  | }  | 
1422  |  |  | 
1423  |  | int eventfd_write(int fd, eventfd_t value)  | 
1424  |  | { | 
1425  |  |   return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;  | 
1426  |  | }  | 
1427  |  | #endif  | 
1428  |  |  | 
1429  |  | #endif /* __linux__ */  |