Coverage Report

Created: 2026-01-09 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/comm/comm.c
Line
Count
Source
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
#include <winpr/assert.h>
26
#include <errno.h>
27
#include <fcntl.h>
28
#include <pthread.h>
29
#include <stdarg.h>
30
#if defined(WINPR_HAVE_SYS_EVENTFD_H)
31
#include <sys/eventfd.h>
32
#endif
33
#include <sys/ioctl.h>
34
#include <sys/stat.h>
35
#include <sys/types.h>
36
#include <termios.h>
37
#include <unistd.h>
38
39
#include <winpr/crt.h>
40
#include <winpr/comm.h>
41
#include <winpr/tchar.h>
42
#include <winpr/wlog.h>
43
#include <winpr/handle.h>
44
45
#include "comm_ioctl.h"
46
47
#include "../log.h"
48
0
#define TAG WINPR_TAG("comm")
49
50
/**
51
 * Communication Resources:
52
 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363196/
53
 */
54
55
#include "comm.h"
56
57
static wLog* sLog = NULL;
58
59
struct comm_device
60
{
61
  LPTSTR name;
62
  LPTSTR path;
63
};
64
65
typedef struct comm_device COMM_DEVICE;
66
67
/* FIXME: get a clever data structure, see also io.h functions */
68
/* _CommDevices is a NULL-terminated array with a maximum of COMM_DEVICE_MAX COMM_DEVICE */
69
0
#define COMM_DEVICE_MAX 128
70
static COMM_DEVICE** sCommDevices = NULL;
71
static CRITICAL_SECTION sCommDevicesLock = { 0 };
72
73
static pthread_once_t sCommInitialized = PTHREAD_ONCE_INIT;
74
75
static const _SERIAL_IOCTL_NAME S_SERIAL_IOCTL_NAMES[] = {
76
  { IOCTL_SERIAL_SET_BAUD_RATE, "IOCTL_SERIAL_SET_BAUD_RATE" },
77
  { IOCTL_SERIAL_GET_BAUD_RATE, "IOCTL_SERIAL_GET_BAUD_RATE" },
78
  { IOCTL_SERIAL_SET_LINE_CONTROL, "IOCTL_SERIAL_SET_LINE_CONTROL" },
79
  { IOCTL_SERIAL_GET_LINE_CONTROL, "IOCTL_SERIAL_GET_LINE_CONTROL" },
80
  { IOCTL_SERIAL_SET_TIMEOUTS, "IOCTL_SERIAL_SET_TIMEOUTS" },
81
  { IOCTL_SERIAL_GET_TIMEOUTS, "IOCTL_SERIAL_GET_TIMEOUTS" },
82
  { IOCTL_SERIAL_GET_CHARS, "IOCTL_SERIAL_GET_CHARS" },
83
  { IOCTL_SERIAL_SET_CHARS, "IOCTL_SERIAL_SET_CHARS" },
84
  { IOCTL_SERIAL_SET_DTR, "IOCTL_SERIAL_SET_DTR" },
85
  { IOCTL_SERIAL_CLR_DTR, "IOCTL_SERIAL_CLR_DTR" },
86
  { IOCTL_SERIAL_RESET_DEVICE, "IOCTL_SERIAL_RESET_DEVICE" },
87
  { IOCTL_SERIAL_SET_RTS, "IOCTL_SERIAL_SET_RTS" },
88
  { IOCTL_SERIAL_CLR_RTS, "IOCTL_SERIAL_CLR_RTS" },
89
  { IOCTL_SERIAL_SET_XOFF, "IOCTL_SERIAL_SET_XOFF" },
90
  { IOCTL_SERIAL_SET_XON, "IOCTL_SERIAL_SET_XON" },
91
  { IOCTL_SERIAL_SET_BREAK_ON, "IOCTL_SERIAL_SET_BREAK_ON" },
92
  { IOCTL_SERIAL_SET_BREAK_OFF, "IOCTL_SERIAL_SET_BREAK_OFF" },
93
  { IOCTL_SERIAL_SET_QUEUE_SIZE, "IOCTL_SERIAL_SET_QUEUE_SIZE" },
94
  { IOCTL_SERIAL_GET_WAIT_MASK, "IOCTL_SERIAL_GET_WAIT_MASK" },
95
  { IOCTL_SERIAL_SET_WAIT_MASK, "IOCTL_SERIAL_SET_WAIT_MASK" },
96
  { IOCTL_SERIAL_WAIT_ON_MASK, "IOCTL_SERIAL_WAIT_ON_MASK" },
97
  { IOCTL_SERIAL_IMMEDIATE_CHAR, "IOCTL_SERIAL_IMMEDIATE_CHAR" },
98
  { IOCTL_SERIAL_PURGE, "IOCTL_SERIAL_PURGE" },
99
  { IOCTL_SERIAL_GET_HANDFLOW, "IOCTL_SERIAL_GET_HANDFLOW" },
100
  { IOCTL_SERIAL_SET_HANDFLOW, "IOCTL_SERIAL_SET_HANDFLOW" },
101
  { IOCTL_SERIAL_GET_MODEMSTATUS, "IOCTL_SERIAL_GET_MODEMSTATUS" },
102
  { IOCTL_SERIAL_GET_DTRRTS, "IOCTL_SERIAL_GET_DTRRTS" },
103
  { IOCTL_SERIAL_GET_COMMSTATUS, "IOCTL_SERIAL_GET_COMMSTATUS" },
104
  { IOCTL_SERIAL_GET_PROPERTIES, "IOCTL_SERIAL_GET_PROPERTIES" },
105
  // {IOCTL_SERIAL_XOFF_COUNTER,  "IOCTL_SERIAL_XOFF_COUNTER"},
106
  // {IOCTL_SERIAL_LSRMST_INSERT, "IOCTL_SERIAL_LSRMST_INSERT"},
107
  { IOCTL_SERIAL_CONFIG_SIZE, "IOCTL_SERIAL_CONFIG_SIZE" },
108
  // {IOCTL_SERIAL_GET_STATS, "IOCTL_SERIAL_GET_STATS"},
109
  // {IOCTL_SERIAL_CLEAR_STATS, "IOCTL_SERIAL_CLEAR_STATS"},
110
  // {IOCTL_SERIAL_GET_MODEM_CONTROL,"IOCTL_SERIAL_GET_MODEM_CONTROL"},
111
  // {IOCTL_SERIAL_SET_MODEM_CONTROL,"IOCTL_SERIAL_SET_MODEM_CONTROL"},
112
  // {IOCTL_SERIAL_SET_FIFO_CONTROL,  "IOCTL_SERIAL_SET_FIFO_CONTROL"},
113
114
  // {IOCTL_PAR_QUERY_INFORMATION,  "IOCTL_PAR_QUERY_INFORMATION"},
115
  // {IOCTL_PAR_SET_INFORMATION,  "IOCTL_PAR_SET_INFORMATION"},
116
  // {IOCTL_PAR_QUERY_DEVICE_ID,  "IOCTL_PAR_QUERY_DEVICE_ID"},
117
  // {IOCTL_PAR_QUERY_DEVICE_ID_SIZE,"IOCTL_PAR_QUERY_DEVICE_ID_SIZE"},
118
  // {IOCTL_IEEE1284_GET_MODE,  "IOCTL_IEEE1284_GET_MODE"},
119
  // {IOCTL_IEEE1284_NEGOTIATE, "IOCTL_IEEE1284_NEGOTIATE"},
120
  // {IOCTL_PAR_SET_WRITE_ADDRESS,  "IOCTL_PAR_SET_WRITE_ADDRESS"},
121
  // {IOCTL_PAR_SET_READ_ADDRESS, "IOCTL_PAR_SET_READ_ADDRESS"},
122
  // {IOCTL_PAR_GET_DEVICE_CAPS,  "IOCTL_PAR_GET_DEVICE_CAPS"},
123
  // {IOCTL_PAR_GET_DEFAULT_MODES,  "IOCTL_PAR_GET_DEFAULT_MODES"},
124
  // {IOCTL_PAR_QUERY_RAW_DEVICE_ID, "IOCTL_PAR_QUERY_RAW_DEVICE_ID"},
125
  // {IOCTL_PAR_IS_PORT_FREE, "IOCTL_PAR_IS_PORT_FREE"},
126
127
  { IOCTL_USBPRINT_GET_1284_ID, "IOCTL_USBPRINT_GET_1284_ID" }
128
};
129
130
const char* _comm_serial_ioctl_name(ULONG number)
131
0
{
132
0
  for (size_t x = 0; x < ARRAYSIZE(S_SERIAL_IOCTL_NAMES); x++)
133
0
  {
134
0
    const _SERIAL_IOCTL_NAME* cur = &S_SERIAL_IOCTL_NAMES[x];
135
0
    if (cur->number == number)
136
0
      return cur->name;
137
0
  }
138
139
0
  return "(unknown ioctl name)";
140
0
}
141
142
static int CommGetFd(HANDLE handle)
143
0
{
144
0
  WINPR_COMM* comm = (WINPR_COMM*)handle;
145
146
0
  if (!CommIsHandled(handle))
147
0
    return -1;
148
149
0
  return comm->fd;
150
0
}
151
152
const HANDLE_CREATOR* GetCommHandleCreator(void)
153
0
{
154
0
#if defined(WINPR_HAVE_SERIAL_SUPPORT)
155
0
  static const HANDLE_CREATOR sCommHandleCreator = { .IsHandled = IsCommDevice,
156
0
                                                   .CreateFileA = CommCreateFileA };
157
0
  return &sCommHandleCreator;
158
#else
159
  return NULL;
160
#endif
161
0
}
162
163
static void CommInit(void)
164
0
{
165
  /* NB: error management to be done outside of this function */
166
0
  WINPR_ASSERT(sLog == NULL);
167
0
  WINPR_ASSERT(sCommDevices == NULL);
168
0
  sCommDevices = (COMM_DEVICE**)calloc(COMM_DEVICE_MAX + 1, sizeof(COMM_DEVICE*));
169
170
0
  if (!sCommDevices)
171
0
    return;
172
173
0
  if (!InitializeCriticalSectionEx(&sCommDevicesLock, 0, 0))
174
0
  {
175
0
    free((void*)sCommDevices);
176
0
    sCommDevices = NULL;
177
0
    return;
178
0
  }
179
180
0
  sLog = WLog_Get(TAG);
181
0
  WINPR_ASSERT(sLog != NULL);
182
0
}
183
184
/**
185
 * Returns TRUE when the comm module is correctly initialized, FALSE otherwise
186
 * with ERROR_DLL_INIT_FAILED set as the last error.
187
 */
188
static BOOL CommInitialized(void)
189
0
{
190
0
  if (pthread_once(&sCommInitialized, CommInit) != 0)
191
0
  {
192
0
    SetLastError(ERROR_DLL_INIT_FAILED);
193
0
    return FALSE;
194
0
  }
195
196
0
  return TRUE;
197
0
}
198
199
WINPR_ATTR_FORMAT_ARG(5, 6)
200
void CommLog_PrintEx(DWORD level, const char* file, size_t line, const char* fkt,
201
                     WINPR_FORMAT_ARG const char* fmt, ...)
202
0
{
203
0
  if (!CommInitialized())
204
0
    return;
205
206
0
  if (!WLog_IsLevelActive(sLog, level))
207
0
    return;
208
0
  va_list ap = { 0 };
209
0
  va_start(ap, fmt);
210
0
  WLog_PrintTextMessageVA(sLog, level, line, file, fkt, fmt, ap);
211
0
  va_end(ap);
212
0
}
213
214
BOOL BuildCommDCBA(WINPR_ATTR_UNUSED LPCSTR lpDef, WINPR_ATTR_UNUSED LPDCB lpDCB)
215
0
{
216
0
  if (!CommInitialized())
217
0
    return FALSE;
218
219
  /* TODO: not implemented */
220
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
221
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
222
0
  return FALSE;
223
0
}
224
225
BOOL BuildCommDCBW(WINPR_ATTR_UNUSED LPCWSTR lpDef, WINPR_ATTR_UNUSED LPDCB lpDCB)
226
0
{
227
0
  if (!CommInitialized())
228
0
    return FALSE;
229
230
  /* TODO: not implemented */
231
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
232
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
233
0
  return FALSE;
234
0
}
235
236
BOOL BuildCommDCBAndTimeoutsA(WINPR_ATTR_UNUSED LPCSTR lpDef, WINPR_ATTR_UNUSED LPDCB lpDCB,
237
                              WINPR_ATTR_UNUSED LPCOMMTIMEOUTS lpCommTimeouts)
238
0
{
239
0
  if (!CommInitialized())
240
0
    return FALSE;
241
242
  /* TODO: not implemented */
243
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
244
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
245
0
  return FALSE;
246
0
}
247
248
BOOL BuildCommDCBAndTimeoutsW(WINPR_ATTR_UNUSED LPCWSTR lpDef, WINPR_ATTR_UNUSED LPDCB lpDCB,
249
                              WINPR_ATTR_UNUSED LPCOMMTIMEOUTS lpCommTimeouts)
250
0
{
251
0
  if (!CommInitialized())
252
0
    return FALSE;
253
254
  /* TODO: not implemented */
255
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
256
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
257
0
  return FALSE;
258
0
}
259
260
BOOL CommConfigDialogA(WINPR_ATTR_UNUSED LPCSTR lpszName, WINPR_ATTR_UNUSED HWND hWnd,
261
                       WINPR_ATTR_UNUSED LPCOMMCONFIG lpCC)
262
0
{
263
0
  if (!CommInitialized())
264
0
    return FALSE;
265
266
  /* TODO: not implemented */
267
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
268
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
269
0
  return FALSE;
270
0
}
271
272
BOOL CommConfigDialogW(WINPR_ATTR_UNUSED LPCWSTR lpszName, WINPR_ATTR_UNUSED HWND hWnd,
273
                       WINPR_ATTR_UNUSED LPCOMMCONFIG lpCC)
274
0
{
275
0
  if (!CommInitialized())
276
0
    return FALSE;
277
278
  /* TODO: not implemented */
279
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
280
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
281
0
  return FALSE;
282
0
}
283
284
BOOL GetCommConfig(HANDLE hCommDev, WINPR_ATTR_UNUSED LPCOMMCONFIG lpCC,
285
                   WINPR_ATTR_UNUSED LPDWORD lpdwSize)
286
0
{
287
0
  WINPR_COMM* pComm = (WINPR_COMM*)hCommDev;
288
289
0
  if (!CommInitialized())
290
0
    return FALSE;
291
292
  /* TODO: not implemented */
293
294
0
  if (!pComm)
295
0
    return FALSE;
296
297
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
298
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
299
0
  return FALSE;
300
0
}
301
302
BOOL SetCommConfig(HANDLE hCommDev, WINPR_ATTR_UNUSED LPCOMMCONFIG lpCC,
303
                   WINPR_ATTR_UNUSED DWORD dwSize)
304
0
{
305
0
  WINPR_COMM* pComm = (WINPR_COMM*)hCommDev;
306
307
0
  if (!CommInitialized())
308
0
    return FALSE;
309
310
  /* TODO: not implemented */
311
312
0
  if (!pComm)
313
0
    return FALSE;
314
315
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
316
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
317
0
  return FALSE;
318
0
}
319
320
BOOL GetCommMask(HANDLE hFile, WINPR_ATTR_UNUSED PDWORD lpEvtMask)
321
0
{
322
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
323
324
0
  if (!CommInitialized())
325
0
    return FALSE;
326
327
  /* TODO: not implemented */
328
329
0
  if (!pComm)
330
0
    return FALSE;
331
332
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
333
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
334
0
  return FALSE;
335
0
}
336
337
BOOL SetCommMask(HANDLE hFile, WINPR_ATTR_UNUSED DWORD dwEvtMask)
338
0
{
339
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
340
341
0
  if (!CommInitialized())
342
0
    return FALSE;
343
344
  /* TODO: not implemented */
345
346
0
  if (!pComm)
347
0
    return FALSE;
348
349
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
350
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
351
0
  return FALSE;
352
0
}
353
354
BOOL GetCommModemStatus(HANDLE hFile, WINPR_ATTR_UNUSED PDWORD lpModemStat)
355
0
{
356
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
357
358
0
  if (!CommInitialized())
359
0
    return FALSE;
360
361
  /* TODO: not implemented */
362
363
0
  if (!pComm)
364
0
    return FALSE;
365
366
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
367
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
368
0
  return FALSE;
369
0
}
370
371
/**
372
 * ERRORS:
373
 *   ERROR_DLL_INIT_FAILED
374
 *   ERROR_INVALID_HANDLE
375
 */
376
BOOL GetCommProperties(HANDLE hFile, LPCOMMPROP lpCommProp)
377
0
{
378
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
379
0
  DWORD bytesReturned = 0;
380
381
0
  if (!CommIsHandleValid(hFile))
382
0
    return FALSE;
383
384
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_PROPERTIES, NULL, 0, lpCommProp,
385
0
                           sizeof(COMMPROP), &bytesReturned, NULL))
386
0
  {
387
0
    CommLog_Print(WLOG_WARN, "GetCommProperties failure.");
388
0
    return FALSE;
389
0
  }
390
391
0
  return TRUE;
392
0
}
393
394
/**
395
 *
396
 *
397
 * ERRORS:
398
 *   ERROR_INVALID_HANDLE
399
 *   ERROR_INVALID_DATA
400
 *   ERROR_IO_DEVICE
401
 *   ERROR_OUTOFMEMORY
402
 */
403
BOOL GetCommState(HANDLE hFile, LPDCB lpDCB)
404
0
{
405
0
  DCB* lpLocalDcb = NULL;
406
0
  struct termios currentState;
407
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
408
0
  DWORD bytesReturned = 0;
409
410
0
  if (!CommIsHandleValid(hFile))
411
0
    return FALSE;
412
413
0
  if (!lpDCB)
414
0
  {
415
0
    SetLastError(ERROR_INVALID_DATA);
416
0
    return FALSE;
417
0
  }
418
419
0
  if (lpDCB->DCBlength < sizeof(DCB))
420
0
  {
421
0
    SetLastError(ERROR_INVALID_DATA);
422
0
    return FALSE;
423
0
  }
424
425
0
  if (tcgetattr(pComm->fd, &currentState) < 0)
426
0
  {
427
0
    SetLastError(ERROR_IO_DEVICE);
428
0
    return FALSE;
429
0
  }
430
431
0
  lpLocalDcb = (DCB*)calloc(1, lpDCB->DCBlength);
432
433
0
  if (lpLocalDcb == NULL)
434
0
  {
435
0
    SetLastError(ERROR_OUTOFMEMORY);
436
0
    return FALSE;
437
0
  }
438
439
  /* error_handle */
440
0
  lpLocalDcb->DCBlength = lpDCB->DCBlength;
441
0
  SERIAL_BAUD_RATE baudRate;
442
443
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_BAUD_RATE, NULL, 0, &baudRate,
444
0
                           sizeof(SERIAL_BAUD_RATE), &bytesReturned, NULL))
445
0
  {
446
0
    CommLog_Print(WLOG_WARN, "GetCommState failure: could not get the baud rate.");
447
0
    goto error_handle;
448
0
  }
449
450
0
  lpLocalDcb->BaudRate = baudRate.BaudRate;
451
0
  lpLocalDcb->fBinary = (currentState.c_cflag & ICANON) == 0;
452
453
0
  if (!lpLocalDcb->fBinary)
454
0
  {
455
0
    CommLog_Print(WLOG_WARN, "Unexpected nonbinary mode, consider to unset the ICANON flag.");
456
0
  }
457
458
0
  lpLocalDcb->fParity = (currentState.c_iflag & INPCK) != 0;
459
0
  SERIAL_HANDFLOW handflow;
460
461
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_HANDFLOW, NULL, 0, &handflow,
462
0
                           sizeof(SERIAL_HANDFLOW), &bytesReturned, NULL))
463
0
  {
464
0
    CommLog_Print(WLOG_WARN, "GetCommState failure: could not get the handflow settings.");
465
0
    goto error_handle;
466
0
  }
467
468
0
  lpLocalDcb->fOutxCtsFlow = (handflow.ControlHandShake & SERIAL_CTS_HANDSHAKE) != 0;
469
0
  lpLocalDcb->fOutxDsrFlow = (handflow.ControlHandShake & SERIAL_DSR_HANDSHAKE) != 0;
470
471
0
  if (handflow.ControlHandShake & SERIAL_DTR_HANDSHAKE)
472
0
  {
473
0
    lpLocalDcb->fDtrControl = DTR_CONTROL_HANDSHAKE;
474
0
  }
475
0
  else if (handflow.ControlHandShake & SERIAL_DTR_CONTROL)
476
0
  {
477
0
    lpLocalDcb->fDtrControl = DTR_CONTROL_ENABLE;
478
0
  }
479
0
  else
480
0
  {
481
0
    lpLocalDcb->fDtrControl = DTR_CONTROL_DISABLE;
482
0
  }
483
484
0
  lpLocalDcb->fDsrSensitivity = (handflow.ControlHandShake & SERIAL_DSR_SENSITIVITY) != 0;
485
0
  lpLocalDcb->fTXContinueOnXoff = (handflow.FlowReplace & SERIAL_XOFF_CONTINUE) != 0;
486
0
  lpLocalDcb->fOutX = (handflow.FlowReplace & SERIAL_AUTO_TRANSMIT) != 0;
487
0
  lpLocalDcb->fInX = (handflow.FlowReplace & SERIAL_AUTO_RECEIVE) != 0;
488
0
  lpLocalDcb->fErrorChar = (handflow.FlowReplace & SERIAL_ERROR_CHAR) != 0;
489
0
  lpLocalDcb->fNull = (handflow.FlowReplace & SERIAL_NULL_STRIPPING) != 0;
490
491
0
  if (handflow.FlowReplace & SERIAL_RTS_HANDSHAKE)
492
0
  {
493
0
    lpLocalDcb->fRtsControl = RTS_CONTROL_HANDSHAKE;
494
0
  }
495
0
  else if (handflow.FlowReplace & SERIAL_RTS_CONTROL)
496
0
  {
497
0
    lpLocalDcb->fRtsControl = RTS_CONTROL_ENABLE;
498
0
  }
499
0
  else
500
0
  {
501
0
    lpLocalDcb->fRtsControl = RTS_CONTROL_DISABLE;
502
0
  }
503
504
  // FIXME: how to get the RTS_CONTROL_TOGGLE state? Does it match the UART 16750's Autoflow
505
  // Control Enabled bit in its Modem Control Register (MCR)
506
0
  lpLocalDcb->fAbortOnError = (handflow.ControlHandShake & SERIAL_ERROR_ABORT) != 0;
507
  /* lpLocalDcb->fDummy2 not used */
508
0
  lpLocalDcb->wReserved = 0; /* must be zero */
509
0
  lpLocalDcb->XonLim = WINPR_ASSERTING_INT_CAST(WORD, handflow.XonLimit);
510
0
  lpLocalDcb->XoffLim = WINPR_ASSERTING_INT_CAST(WORD, handflow.XoffLimit);
511
512
0
  {
513
0
    SERIAL_LINE_CONTROL lineControl = { 0 };
514
515
0
    if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_LINE_CONTROL, NULL, 0, &lineControl,
516
0
                             sizeof(SERIAL_LINE_CONTROL), &bytesReturned, NULL))
517
0
    {
518
0
      CommLog_Print(WLOG_WARN, "GetCommState failure: could not get the control settings.");
519
0
      goto error_handle;
520
0
    }
521
522
0
    lpLocalDcb->ByteSize = lineControl.WordLength;
523
0
    lpLocalDcb->Parity = lineControl.Parity;
524
0
    lpLocalDcb->StopBits = lineControl.StopBits;
525
0
  }
526
527
0
  {
528
0
    SERIAL_CHARS serialChars = { 0 };
529
530
0
    if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_CHARS, NULL, 0, &serialChars,
531
0
                             sizeof(SERIAL_CHARS), &bytesReturned, NULL))
532
0
    {
533
0
      CommLog_Print(WLOG_WARN, "GetCommState failure: could not get the serial chars.");
534
0
      goto error_handle;
535
0
    }
536
537
0
    lpLocalDcb->XonChar = serialChars.XonChar;
538
0
    lpLocalDcb->XoffChar = serialChars.XoffChar;
539
0
    lpLocalDcb->ErrorChar = serialChars.ErrorChar;
540
0
    lpLocalDcb->EofChar = serialChars.EofChar;
541
0
    lpLocalDcb->EvtChar = serialChars.EventChar;
542
0
  }
543
0
  memcpy(lpDCB, lpLocalDcb, lpDCB->DCBlength);
544
0
  free(lpLocalDcb);
545
0
  return TRUE;
546
0
error_handle:
547
0
  free(lpLocalDcb);
548
0
  return FALSE;
549
0
}
550
551
/**
552
 * @return TRUE on success, FALSE otherwise.
553
 *
554
 * As of today, SetCommState() can fail half-way with some settings
555
 * applied and some others not. SetCommState() returns on the first
556
 * failure met. FIXME: or is it correct?
557
 *
558
 * ERRORS:
559
 *   ERROR_INVALID_HANDLE
560
 *   ERROR_IO_DEVICE
561
 */
562
BOOL SetCommState(HANDLE hFile, LPDCB lpDCB)
563
0
{
564
0
  struct termios upcomingTermios = { 0 };
565
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
566
0
  DWORD bytesReturned = 0;
567
568
  /* FIXME: validate changes according GetCommProperties? */
569
570
0
  if (!CommIsHandleValid(hFile))
571
0
    return FALSE;
572
573
0
  if (!lpDCB)
574
0
  {
575
0
    SetLastError(ERROR_INVALID_DATA);
576
0
    return FALSE;
577
0
  }
578
579
  /* NB: did the choice to call ioctls first when available and
580
     then to setup upcomingTermios. Don't mix both stages. */
581
  /** ioctl calls stage **/
582
0
  SERIAL_BAUD_RATE baudRate;
583
0
  baudRate.BaudRate = lpDCB->BaudRate;
584
585
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_BAUD_RATE, &baudRate, sizeof(SERIAL_BAUD_RATE),
586
0
                           NULL, 0, &bytesReturned, NULL))
587
0
  {
588
0
    CommLog_Print(WLOG_WARN, "SetCommState failure: could not set the baud rate.");
589
0
    return FALSE;
590
0
  }
591
592
0
  SERIAL_CHARS serialChars;
593
594
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_CHARS, NULL, 0, &serialChars,
595
0
                           sizeof(SERIAL_CHARS), &bytesReturned,
596
0
                           NULL)) /* as of today, required for BreakChar */
597
0
  {
598
0
    CommLog_Print(WLOG_WARN, "SetCommState failure: could not get the initial serial chars.");
599
0
    return FALSE;
600
0
  }
601
602
0
  serialChars.XonChar = lpDCB->XonChar;
603
0
  serialChars.XoffChar = lpDCB->XoffChar;
604
0
  serialChars.ErrorChar = lpDCB->ErrorChar;
605
0
  serialChars.EofChar = lpDCB->EofChar;
606
0
  serialChars.EventChar = lpDCB->EvtChar;
607
608
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_CHARS, &serialChars, sizeof(SERIAL_CHARS),
609
0
                           NULL, 0, &bytesReturned, NULL))
610
0
  {
611
0
    CommLog_Print(WLOG_WARN, "SetCommState failure: could not set the serial chars.");
612
0
    return FALSE;
613
0
  }
614
615
0
  SERIAL_LINE_CONTROL lineControl;
616
0
  lineControl.StopBits = lpDCB->StopBits;
617
0
  lineControl.Parity = lpDCB->Parity;
618
0
  lineControl.WordLength = lpDCB->ByteSize;
619
620
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_LINE_CONTROL, &lineControl,
621
0
                           sizeof(SERIAL_LINE_CONTROL), NULL, 0, &bytesReturned, NULL))
622
0
  {
623
0
    CommLog_Print(WLOG_WARN, "SetCommState failure: could not set the control settings.");
624
0
    return FALSE;
625
0
  }
626
627
0
  SERIAL_HANDFLOW handflow = { 0 };
628
629
0
  if (lpDCB->fOutxCtsFlow)
630
0
  {
631
0
    handflow.ControlHandShake |= SERIAL_CTS_HANDSHAKE;
632
0
  }
633
634
0
  if (lpDCB->fOutxDsrFlow)
635
0
  {
636
0
    handflow.ControlHandShake |= SERIAL_DSR_HANDSHAKE;
637
0
  }
638
639
0
  switch (lpDCB->fDtrControl)
640
0
  {
641
0
    case SERIAL_DTR_HANDSHAKE:
642
0
      handflow.ControlHandShake |= DTR_CONTROL_HANDSHAKE;
643
0
      break;
644
645
0
    case SERIAL_DTR_CONTROL:
646
0
      handflow.ControlHandShake |= DTR_CONTROL_ENABLE;
647
0
      break;
648
649
0
    case DTR_CONTROL_DISABLE:
650
      /* do nothing since handflow is init-zeroed */
651
0
      break;
652
653
0
    default:
654
0
      CommLog_Print(WLOG_WARN, "Unexpected fDtrControl value: %" PRId32 "\n",
655
0
                    lpDCB->fDtrControl);
656
0
      return FALSE;
657
0
  }
658
659
0
  if (lpDCB->fDsrSensitivity)
660
0
  {
661
0
    handflow.ControlHandShake |= SERIAL_DSR_SENSITIVITY;
662
0
  }
663
664
0
  if (lpDCB->fTXContinueOnXoff)
665
0
  {
666
0
    handflow.FlowReplace |= SERIAL_XOFF_CONTINUE;
667
0
  }
668
669
0
  if (lpDCB->fOutX)
670
0
  {
671
0
    handflow.FlowReplace |= SERIAL_AUTO_TRANSMIT;
672
0
  }
673
674
0
  if (lpDCB->fInX)
675
0
  {
676
0
    handflow.FlowReplace |= SERIAL_AUTO_RECEIVE;
677
0
  }
678
679
0
  if (lpDCB->fErrorChar)
680
0
  {
681
0
    handflow.FlowReplace |= SERIAL_ERROR_CHAR;
682
0
  }
683
684
0
  if (lpDCB->fNull)
685
0
  {
686
0
    handflow.FlowReplace |= SERIAL_NULL_STRIPPING;
687
0
  }
688
689
0
  switch (lpDCB->fRtsControl)
690
0
  {
691
0
    case RTS_CONTROL_TOGGLE:
692
0
      CommLog_Print(WLOG_WARN, "Unsupported RTS_CONTROL_TOGGLE feature");
693
      // FIXME: see also GetCommState()
694
0
      return FALSE;
695
696
0
    case RTS_CONTROL_HANDSHAKE:
697
0
      handflow.FlowReplace |= SERIAL_RTS_HANDSHAKE;
698
0
      break;
699
700
0
    case RTS_CONTROL_ENABLE:
701
0
      handflow.FlowReplace |= SERIAL_RTS_CONTROL;
702
0
      break;
703
704
0
    case RTS_CONTROL_DISABLE:
705
      /* do nothing since handflow is init-zeroed */
706
0
      break;
707
708
0
    default:
709
0
      CommLog_Print(WLOG_WARN, "Unexpected fRtsControl value: %" PRId32 "\n",
710
0
                    lpDCB->fRtsControl);
711
0
      return FALSE;
712
0
  }
713
714
0
  if (lpDCB->fAbortOnError)
715
0
  {
716
0
    handflow.ControlHandShake |= SERIAL_ERROR_ABORT;
717
0
  }
718
719
  /* lpDCB->fDummy2 not used */
720
  /* lpLocalDcb->wReserved  ignored */
721
0
  handflow.XonLimit = lpDCB->XonLim;
722
0
  handflow.XoffLimit = lpDCB->XoffLim;
723
724
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_HANDFLOW, &handflow, sizeof(SERIAL_HANDFLOW),
725
0
                           NULL, 0, &bytesReturned, NULL))
726
0
  {
727
0
    CommLog_Print(WLOG_WARN, "SetCommState failure: could not set the handflow settings.");
728
0
    return FALSE;
729
0
  }
730
731
  /** upcomingTermios stage **/
732
733
0
  if (tcgetattr(pComm->fd, &upcomingTermios) <
734
0
      0) /* NB: preserves current settings not directly handled by the Communication Functions */
735
0
  {
736
0
    SetLastError(ERROR_IO_DEVICE);
737
0
    return FALSE;
738
0
  }
739
740
0
  if (lpDCB->fBinary)
741
0
  {
742
0
    upcomingTermios.c_lflag &= (tcflag_t)~ICANON;
743
0
  }
744
0
  else
745
0
  {
746
0
    upcomingTermios.c_lflag |= ICANON;
747
0
    CommLog_Print(WLOG_WARN, "Unexpected nonbinary mode, consider to unset the ICANON flag.");
748
0
  }
749
750
0
  if (lpDCB->fParity)
751
0
  {
752
0
    upcomingTermios.c_iflag |= INPCK;
753
0
  }
754
0
  else
755
0
  {
756
0
    upcomingTermios.c_iflag &= (tcflag_t)~INPCK;
757
0
  }
758
759
  /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363423%28v=vs.85%29.aspx
760
   *
761
   * The SetCommState function reconfigures the communications
762
   * resource, but it does not affect the internal output and
763
   * input buffers of the specified driver. The buffers are not
764
   * flushed, and pending read and write operations are not
765
   * terminated prematurely.
766
   *
767
   * TCSANOW matches the best this definition
768
   */
769
770
0
  if (comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &upcomingTermios) < 0)
771
0
  {
772
0
    SetLastError(ERROR_IO_DEVICE);
773
0
    return FALSE;
774
0
  }
775
776
0
  return TRUE;
777
0
}
778
779
/**
780
 * ERRORS:
781
 *   ERROR_INVALID_HANDLE
782
 */
783
BOOL GetCommTimeouts(HANDLE hFile, LPCOMMTIMEOUTS lpCommTimeouts)
784
0
{
785
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
786
0
  DWORD bytesReturned = 0;
787
788
0
  if (!CommIsHandleValid(hFile))
789
0
    return FALSE;
790
791
  /* as of today, SERIAL_TIMEOUTS and COMMTIMEOUTS structures are identical */
792
793
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, lpCommTimeouts,
794
0
                           sizeof(COMMTIMEOUTS), &bytesReturned, NULL))
795
0
  {
796
0
    CommLog_Print(WLOG_WARN, "GetCommTimeouts failure.");
797
0
    return FALSE;
798
0
  }
799
800
0
  return TRUE;
801
0
}
802
803
/**
804
 * ERRORS:
805
 *   ERROR_INVALID_HANDLE
806
 */
807
BOOL SetCommTimeouts(HANDLE hFile, LPCOMMTIMEOUTS lpCommTimeouts)
808
0
{
809
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
810
0
  DWORD bytesReturned = 0;
811
812
0
  if (!CommIsHandleValid(hFile))
813
0
    return FALSE;
814
815
  /* as of today, SERIAL_TIMEOUTS and COMMTIMEOUTS structures are identical */
816
817
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_TIMEOUTS, lpCommTimeouts, sizeof(COMMTIMEOUTS),
818
0
                           NULL, 0, &bytesReturned, NULL))
819
0
  {
820
0
    CommLog_Print(WLOG_WARN, "SetCommTimeouts failure.");
821
0
    return FALSE;
822
0
  }
823
824
0
  return TRUE;
825
0
}
826
827
BOOL GetDefaultCommConfigA(WINPR_ATTR_UNUSED LPCSTR lpszName, WINPR_ATTR_UNUSED LPCOMMCONFIG lpCC,
828
                           WINPR_ATTR_UNUSED LPDWORD lpdwSize)
829
0
{
830
0
  if (!CommInitialized())
831
0
    return FALSE;
832
833
  /* TODO: not implemented */
834
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
835
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
836
0
  return FALSE;
837
0
}
838
839
BOOL GetDefaultCommConfigW(WINPR_ATTR_UNUSED LPCWSTR lpszName, WINPR_ATTR_UNUSED LPCOMMCONFIG lpCC,
840
                           WINPR_ATTR_UNUSED LPDWORD lpdwSize)
841
0
{
842
0
  if (!CommInitialized())
843
0
    return FALSE;
844
845
  /* TODO: not implemented */
846
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
847
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
848
0
  return FALSE;
849
0
}
850
851
BOOL SetDefaultCommConfigA(WINPR_ATTR_UNUSED LPCSTR lpszName, WINPR_ATTR_UNUSED LPCOMMCONFIG lpCC,
852
                           WINPR_ATTR_UNUSED DWORD dwSize)
853
0
{
854
0
  if (!CommInitialized())
855
0
    return FALSE;
856
857
  /* TODO: not implemented */
858
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
859
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
860
0
  return FALSE;
861
0
}
862
863
BOOL SetDefaultCommConfigW(WINPR_ATTR_UNUSED LPCWSTR lpszName, WINPR_ATTR_UNUSED LPCOMMCONFIG lpCC,
864
                           WINPR_ATTR_UNUSED DWORD dwSize)
865
0
{
866
0
  if (!CommInitialized())
867
0
    return FALSE;
868
869
  /* TODO: not implemented */
870
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
871
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
872
0
  return FALSE;
873
0
}
874
875
BOOL SetCommBreak(HANDLE hFile)
876
0
{
877
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
878
879
0
  if (!CommInitialized())
880
0
    return FALSE;
881
882
  /* TODO: not implemented */
883
884
0
  if (!pComm)
885
0
    return FALSE;
886
887
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
888
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
889
0
  return FALSE;
890
0
}
891
892
BOOL ClearCommBreak(HANDLE hFile)
893
0
{
894
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
895
896
0
  if (!CommInitialized())
897
0
    return FALSE;
898
899
  /* TODO: not implemented */
900
901
0
  if (!pComm)
902
0
    return FALSE;
903
904
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
905
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
906
0
  return FALSE;
907
0
}
908
909
BOOL ClearCommError(HANDLE hFile, WINPR_ATTR_UNUSED PDWORD lpErrors,
910
                    WINPR_ATTR_UNUSED LPCOMSTAT lpStat)
911
0
{
912
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
913
914
0
  if (!CommInitialized())
915
0
    return FALSE;
916
917
  /* TODO: not implemented */
918
919
0
  if (!pComm)
920
0
    return FALSE;
921
922
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
923
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
924
0
  return FALSE;
925
0
}
926
927
BOOL PurgeComm(HANDLE hFile, DWORD dwFlags)
928
0
{
929
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
930
0
  DWORD bytesReturned = 0;
931
932
0
  if (!CommIsHandleValid(hFile))
933
0
    return FALSE;
934
935
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_PURGE, &dwFlags, sizeof(DWORD), NULL, 0,
936
0
                           &bytesReturned, NULL))
937
0
  {
938
0
    CommLog_Print(WLOG_WARN, "PurgeComm failure.");
939
0
    return FALSE;
940
0
  }
941
942
0
  return TRUE;
943
0
}
944
945
BOOL SetupComm(HANDLE hFile, DWORD dwInQueue, DWORD dwOutQueue)
946
0
{
947
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
948
0
  SERIAL_QUEUE_SIZE queueSize;
949
0
  DWORD bytesReturned = 0;
950
951
0
  if (!CommIsHandleValid(hFile))
952
0
    return FALSE;
953
954
0
  queueSize.InSize = dwInQueue;
955
0
  queueSize.OutSize = dwOutQueue;
956
957
0
  if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_QUEUE_SIZE, &queueSize,
958
0
                           sizeof(SERIAL_QUEUE_SIZE), NULL, 0, &bytesReturned, NULL))
959
0
  {
960
0
    CommLog_Print(WLOG_WARN, "SetCommTimeouts failure.");
961
0
    return FALSE;
962
0
  }
963
964
0
  return TRUE;
965
0
}
966
967
BOOL EscapeCommFunction(HANDLE hFile, WINPR_ATTR_UNUSED DWORD dwFunc)
968
0
{
969
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
970
971
0
  if (!CommInitialized())
972
0
    return FALSE;
973
974
  /* TODO: not implemented */
975
976
0
  if (!pComm)
977
0
    return FALSE;
978
979
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
980
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
981
0
  return FALSE;
982
0
}
983
984
BOOL TransmitCommChar(HANDLE hFile, WINPR_ATTR_UNUSED char cChar)
985
0
{
986
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
987
988
0
  if (!CommInitialized())
989
0
    return FALSE;
990
991
  /* TODO: not implemented */
992
993
0
  if (!pComm)
994
0
    return FALSE;
995
996
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
997
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
998
0
  return FALSE;
999
0
}
1000
1001
BOOL WaitCommEvent(HANDLE hFile, WINPR_ATTR_UNUSED PDWORD lpEvtMask,
1002
                   WINPR_ATTR_UNUSED LPOVERLAPPED lpOverlapped)
1003
0
{
1004
0
  WINPR_COMM* pComm = (WINPR_COMM*)hFile;
1005
1006
0
  if (!CommInitialized())
1007
0
    return FALSE;
1008
1009
  /* TODO: not implemented */
1010
1011
0
  if (!pComm)
1012
0
    return FALSE;
1013
1014
0
  CommLog_Print(WLOG_ERROR, "Not implemented");
1015
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1016
0
  return FALSE;
1017
0
}
1018
1019
/**
1020
 * Returns TRUE on success, FALSE otherwise. To get extended error
1021
 * information, call GetLastError.
1022
 *
1023
 * ERRORS:
1024
 *   ERROR_DLL_INIT_FAILED
1025
 *   ERROR_OUTOFMEMORY was not possible to get mappings.
1026
 *   ERROR_INVALID_DATA was not possible to add the device.
1027
 */
1028
BOOL DefineCommDevice(/* DWORD dwFlags,*/ LPCTSTR lpDeviceName, LPCTSTR lpTargetPath)
1029
0
{
1030
0
  LPTSTR storedDeviceName = NULL;
1031
0
  LPTSTR storedTargetPath = NULL;
1032
1033
0
  if (!CommInitialized())
1034
0
    return FALSE;
1035
1036
0
  EnterCriticalSection(&sCommDevicesLock);
1037
1038
0
  if (sCommDevices == NULL)
1039
0
  {
1040
0
    SetLastError(ERROR_DLL_INIT_FAILED);
1041
0
    goto error_handle;
1042
0
  }
1043
1044
0
  storedDeviceName = _tcsdup(lpDeviceName);
1045
1046
0
  if (storedDeviceName == NULL)
1047
0
  {
1048
0
    SetLastError(ERROR_OUTOFMEMORY);
1049
0
    goto error_handle;
1050
0
  }
1051
1052
0
  storedTargetPath = _tcsdup(lpTargetPath);
1053
1054
0
  if (storedTargetPath == NULL)
1055
0
  {
1056
0
    SetLastError(ERROR_OUTOFMEMORY);
1057
0
    goto error_handle;
1058
0
  }
1059
1060
0
  {
1061
0
    int i = 0;
1062
0
    for (; i < COMM_DEVICE_MAX; i++)
1063
0
    {
1064
0
      if (sCommDevices[i] != NULL)
1065
0
      {
1066
0
        if (_tcscmp(sCommDevices[i]->name, storedDeviceName) == 0)
1067
0
        {
1068
          /* take over the emplacement */
1069
0
          free(sCommDevices[i]->name);
1070
0
          free(sCommDevices[i]->path);
1071
0
          sCommDevices[i]->name = storedDeviceName;
1072
0
          sCommDevices[i]->path = storedTargetPath;
1073
0
          break;
1074
0
        }
1075
0
      }
1076
0
      else
1077
0
      {
1078
        /* new emplacement */
1079
0
        sCommDevices[i] = (COMM_DEVICE*)calloc(1, sizeof(COMM_DEVICE));
1080
1081
0
        if (sCommDevices[i] == NULL)
1082
0
        {
1083
0
          SetLastError(ERROR_OUTOFMEMORY);
1084
0
          goto error_handle;
1085
0
        }
1086
1087
0
        sCommDevices[i]->name = storedDeviceName;
1088
0
        sCommDevices[i]->path = storedTargetPath;
1089
0
        break;
1090
0
      }
1091
0
    }
1092
1093
0
    if (i == COMM_DEVICE_MAX)
1094
0
    {
1095
0
      SetLastError(ERROR_OUTOFMEMORY);
1096
0
      goto error_handle;
1097
0
    }
1098
0
  }
1099
1100
0
  LeaveCriticalSection(&sCommDevicesLock);
1101
0
  return TRUE;
1102
0
error_handle:
1103
0
  free(storedDeviceName);
1104
0
  free(storedTargetPath);
1105
0
  LeaveCriticalSection(&sCommDevicesLock);
1106
0
  return FALSE;
1107
0
}
1108
1109
/**
1110
 * Returns the number of target paths in the buffer pointed to by
1111
 * lpTargetPath.
1112
 *
1113
 * The current implementation returns in any case 0 and 1 target
1114
 * path. A NULL lpDeviceName is not supported yet to get all the
1115
 * paths.
1116
 *
1117
 * ERRORS:
1118
 *   ERROR_SUCCESS
1119
 *   ERROR_DLL_INIT_FAILED
1120
 *   ERROR_OUTOFMEMORY was not possible to get mappings.
1121
 *   ERROR_NOT_SUPPORTED equivalent QueryDosDevice feature not supported.
1122
 *   ERROR_INVALID_DATA was not possible to retrieve any device information.
1123
 *   ERROR_INSUFFICIENT_BUFFER too small lpTargetPath
1124
 */
1125
DWORD QueryCommDevice(LPCTSTR lpDeviceName, LPTSTR lpTargetPath, DWORD ucchMax)
1126
0
{
1127
0
  LPTSTR storedTargetPath = NULL;
1128
0
  SetLastError(ERROR_SUCCESS);
1129
1130
0
  if (!CommInitialized())
1131
0
    return 0;
1132
1133
0
  if (sCommDevices == NULL)
1134
0
  {
1135
0
    SetLastError(ERROR_DLL_INIT_FAILED);
1136
0
    return 0;
1137
0
  }
1138
1139
0
  if (lpDeviceName == NULL || lpTargetPath == NULL)
1140
0
  {
1141
0
    SetLastError(ERROR_NOT_SUPPORTED);
1142
0
    return 0;
1143
0
  }
1144
1145
0
  EnterCriticalSection(&sCommDevicesLock);
1146
0
  storedTargetPath = NULL;
1147
1148
0
  for (int i = 0; i < COMM_DEVICE_MAX; i++)
1149
0
  {
1150
0
    if (sCommDevices[i] != NULL)
1151
0
    {
1152
0
      if (_tcscmp(sCommDevices[i]->name, lpDeviceName) == 0)
1153
0
      {
1154
0
        storedTargetPath = sCommDevices[i]->path;
1155
0
        break;
1156
0
      }
1157
1158
0
      continue;
1159
0
    }
1160
1161
0
    break;
1162
0
  }
1163
1164
0
  LeaveCriticalSection(&sCommDevicesLock);
1165
1166
0
  if (storedTargetPath == NULL)
1167
0
  {
1168
0
    SetLastError(ERROR_INVALID_DATA);
1169
0
    return 0;
1170
0
  }
1171
1172
0
  const size_t size = _tcsnlen(storedTargetPath, ucchMax);
1173
0
  if (size + 2 > ucchMax)
1174
0
  {
1175
0
    SetLastError(ERROR_INSUFFICIENT_BUFFER);
1176
0
    return 0;
1177
0
  }
1178
1179
0
  _tcsncpy(lpTargetPath, storedTargetPath, size + 1);
1180
0
  lpTargetPath[size + 2] = '\0'; /* 2nd final '\0' */
1181
0
  return (DWORD)size + 2UL;
1182
0
}
1183
1184
/**
1185
 * Checks whether lpDeviceName is a valid and registered Communication device.
1186
 */
1187
BOOL IsCommDevice(LPCTSTR lpDeviceName)
1188
0
{
1189
0
  TCHAR lpTargetPath[MAX_PATH];
1190
1191
0
  if (!CommInitialized())
1192
0
    return FALSE;
1193
1194
0
  if (QueryCommDevice(lpDeviceName, lpTargetPath, MAX_PATH) > 0)
1195
0
  {
1196
0
    return TRUE;
1197
0
  }
1198
1199
0
  return FALSE;
1200
0
}
1201
1202
/**
1203
 * Sets
1204
 */
1205
void _comm_setServerSerialDriver(HANDLE hComm, SERIAL_DRIVER_ID driverId)
1206
0
{
1207
0
  ULONG Type = 0;
1208
0
  WINPR_HANDLE* Object = NULL;
1209
0
  WINPR_COMM* pComm = NULL;
1210
1211
0
  if (!CommInitialized())
1212
0
    return;
1213
1214
0
  if (!winpr_Handle_GetInfo(hComm, &Type, &Object))
1215
0
  {
1216
0
    CommLog_Print(WLOG_WARN, "_comm_setServerSerialDriver failure");
1217
0
    return;
1218
0
  }
1219
1220
0
  pComm = (WINPR_COMM*)Object;
1221
0
  pComm->serverSerialDriverId = driverId;
1222
0
}
1223
1224
static HANDLE_OPS ops = { CommIsHandled, CommCloseHandle,
1225
                        CommGetFd,     NULL, /* CleanupHandle */
1226
                        NULL,          NULL,
1227
                        NULL,          NULL,
1228
                        NULL,          NULL,
1229
                        NULL,          NULL,
1230
                        NULL,          NULL,
1231
                        NULL,          NULL,
1232
                        NULL,          NULL,
1233
                        NULL,          NULL,
1234
                        NULL };
1235
1236
/**
1237
 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363198%28v=vs.85%29.aspx
1238
 *
1239
 * @param lpDeviceName e.g. COM1, ...
1240
 *
1241
 * @param dwDesiredAccess expects GENERIC_READ | GENERIC_WRITE, a
1242
 * warning message is printed otherwise. TODO: better support.
1243
 *
1244
 * @param dwShareMode must be zero, INVALID_HANDLE_VALUE is returned
1245
 * otherwise and GetLastError() should return ERROR_SHARING_VIOLATION.
1246
 *
1247
 * @param lpSecurityAttributes NULL expected, a warning message is printed
1248
 * otherwise. TODO: better support.
1249
 *
1250
 * @param dwCreationDisposition must be OPEN_EXISTING. If the
1251
 * communication device doesn't exist INVALID_HANDLE_VALUE is returned
1252
 * and GetLastError() returns ERROR_FILE_NOT_FOUND.
1253
 *
1254
 * @param dwFlagsAndAttributes zero expected, a warning message is
1255
 * printed otherwise.
1256
 *
1257
 * @param hTemplateFile must be NULL.
1258
 *
1259
 * @return INVALID_HANDLE_VALUE on error.
1260
 */
1261
HANDLE CommCreateFileA(LPCSTR lpDeviceName, DWORD dwDesiredAccess, DWORD dwShareMode,
1262
                       LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
1263
                       DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
1264
0
{
1265
0
  CHAR devicePath[MAX_PATH] = { 0 };
1266
0
  struct stat deviceStat = { 0 };
1267
0
  WINPR_COMM* pComm = NULL;
1268
0
  struct termios upcomingTermios = { 0 };
1269
1270
0
  if (!CommInitialized())
1271
0
    return INVALID_HANDLE_VALUE;
1272
1273
0
  if (dwDesiredAccess != (GENERIC_READ | GENERIC_WRITE))
1274
0
  {
1275
0
    CommLog_Print(WLOG_WARN, "unexpected access to the device: 0x%08" PRIX32 "",
1276
0
                  dwDesiredAccess);
1277
0
  }
1278
1279
0
  if (dwShareMode != 0)
1280
0
  {
1281
0
    SetLastError(ERROR_SHARING_VIOLATION);
1282
0
    return INVALID_HANDLE_VALUE;
1283
0
  }
1284
1285
  /* TODO: Prevents other processes from opening a file or
1286
   * device if they request delete, read, or write access. */
1287
1288
0
  if (lpSecurityAttributes != NULL)
1289
0
  {
1290
0
    CommLog_Print(WLOG_WARN, "unexpected security attributes, nLength=%" PRIu32 "",
1291
0
                  lpSecurityAttributes->nLength);
1292
0
  }
1293
1294
0
  if (dwCreationDisposition != OPEN_EXISTING)
1295
0
  {
1296
0
    SetLastError(ERROR_FILE_NOT_FOUND); /* FIXME: ERROR_NOT_SUPPORTED better? */
1297
0
    return INVALID_HANDLE_VALUE;
1298
0
  }
1299
1300
0
  if (QueryCommDevice(lpDeviceName, devicePath, MAX_PATH) <= 0)
1301
0
  {
1302
    /* SetLastError(GetLastError()); */
1303
0
    return INVALID_HANDLE_VALUE;
1304
0
  }
1305
1306
0
  if (stat(devicePath, &deviceStat) < 0)
1307
0
  {
1308
0
    CommLog_Print(WLOG_WARN, "device not found %s", devicePath);
1309
0
    SetLastError(ERROR_FILE_NOT_FOUND);
1310
0
    return INVALID_HANDLE_VALUE;
1311
0
  }
1312
1313
0
  if (!S_ISCHR(deviceStat.st_mode))
1314
0
  {
1315
0
    CommLog_Print(WLOG_WARN, "bad device %s", devicePath);
1316
0
    SetLastError(ERROR_BAD_DEVICE);
1317
0
    return INVALID_HANDLE_VALUE;
1318
0
  }
1319
1320
0
  if (dwFlagsAndAttributes != 0)
1321
0
  {
1322
0
    CommLog_Print(WLOG_WARN, "unexpected flags and attributes: 0x%08" PRIX32 "",
1323
0
                  dwFlagsAndAttributes);
1324
0
  }
1325
1326
0
  if (hTemplateFile != NULL)
1327
0
  {
1328
0
    SetLastError(ERROR_NOT_SUPPORTED); /* FIXME: other proper error? */
1329
0
    return INVALID_HANDLE_VALUE;
1330
0
  }
1331
1332
0
  pComm = (WINPR_COMM*)calloc(1, sizeof(WINPR_COMM));
1333
1334
0
  if (pComm == NULL)
1335
0
  {
1336
0
    SetLastError(ERROR_OUTOFMEMORY);
1337
0
    return INVALID_HANDLE_VALUE;
1338
0
  }
1339
1340
0
  WINPR_HANDLE_SET_TYPE_AND_MODE(pComm, HANDLE_TYPE_COMM, WINPR_FD_READ);
1341
0
  pComm->common.ops = &ops;
1342
  /* error_handle */
1343
0
  pComm->fd = open(devicePath, O_RDWR | O_NOCTTY | O_NONBLOCK);
1344
1345
0
  if (pComm->fd < 0)
1346
0
  {
1347
0
    CommLog_Print(WLOG_WARN, "failed to open device %s", devicePath);
1348
0
    SetLastError(ERROR_BAD_DEVICE);
1349
0
    goto error_handle;
1350
0
  }
1351
1352
0
  pComm->fd_read = open(devicePath, O_RDONLY | O_NOCTTY | O_NONBLOCK);
1353
1354
0
  if (pComm->fd_read < 0)
1355
0
  {
1356
0
    CommLog_Print(WLOG_WARN, "failed to open fd_read, device: %s", devicePath);
1357
0
    SetLastError(ERROR_BAD_DEVICE);
1358
0
    goto error_handle;
1359
0
  }
1360
1361
0
#if defined(WINPR_HAVE_SYS_EVENTFD_H)
1362
0
  pComm->fd_read_event = eventfd(
1363
0
      0, EFD_NONBLOCK); /* EFD_NONBLOCK required because a read() is not always expected */
1364
0
#endif
1365
1366
0
  if (pComm->fd_read_event < 0)
1367
0
  {
1368
0
    CommLog_Print(WLOG_WARN, "failed to open fd_read_event, device: %s", devicePath);
1369
0
    SetLastError(ERROR_BAD_DEVICE);
1370
0
    goto error_handle;
1371
0
  }
1372
1373
0
  InitializeCriticalSection(&pComm->ReadLock);
1374
0
  pComm->fd_write = open(devicePath, O_WRONLY | O_NOCTTY | O_NONBLOCK);
1375
1376
0
  if (pComm->fd_write < 0)
1377
0
  {
1378
0
    CommLog_Print(WLOG_WARN, "failed to open fd_write, device: %s", devicePath);
1379
0
    SetLastError(ERROR_BAD_DEVICE);
1380
0
    goto error_handle;
1381
0
  }
1382
1383
0
#if defined(WINPR_HAVE_SYS_EVENTFD_H)
1384
0
  pComm->fd_write_event = eventfd(
1385
0
      0, EFD_NONBLOCK); /* EFD_NONBLOCK required because a read() is not always expected */
1386
0
#endif
1387
1388
0
  if (pComm->fd_write_event < 0)
1389
0
  {
1390
0
    CommLog_Print(WLOG_WARN, "failed to open fd_write_event, device: %s", devicePath);
1391
0
    SetLastError(ERROR_BAD_DEVICE);
1392
0
    goto error_handle;
1393
0
  }
1394
1395
0
  InitializeCriticalSection(&pComm->WriteLock);
1396
  /* can also be setup later on with _comm_setServerSerialDriver() */
1397
0
  pComm->serverSerialDriverId = SerialDriverUnknown;
1398
0
  InitializeCriticalSection(&pComm->EventsLock);
1399
1400
0
  (void)CommUpdateIOCount(pComm, TRUE);
1401
1402
  /* The binary/raw mode is required for the redirection but
1403
   * only flags that are not handle somewhere-else, except
1404
   * ICANON, are forced here. */
1405
0
  ZeroMemory(&upcomingTermios, sizeof(struct termios));
1406
1407
0
  if (tcgetattr(pComm->fd, &upcomingTermios) < 0)
1408
0
  {
1409
0
    SetLastError(ERROR_IO_DEVICE);
1410
0
    goto error_handle;
1411
0
  }
1412
1413
0
  upcomingTermios.c_iflag &=
1414
0
      (tcflag_t) ~(/*IGNBRK |*/ BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL /*| IXON*/);
1415
0
  upcomingTermios.c_oflag = 0; /* <=> &= ~OPOST */
1416
0
  upcomingTermios.c_lflag = 0; /* <=> &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); */
1417
  /* upcomingTermios.c_cflag &= ~(CSIZE | PARENB); */
1418
  /* upcomingTermios.c_cflag |= CS8; */
1419
  /* About missing flags recommended by termios(3):
1420
   *
1421
   *   IGNBRK and IXON, see: IOCTL_SERIAL_SET_HANDFLOW
1422
   *   CSIZE, PARENB and CS8, see: IOCTL_SERIAL_SET_LINE_CONTROL
1423
   */
1424
  /* a few more settings required for the redirection */
1425
0
  upcomingTermios.c_cflag |= CLOCAL | CREAD;
1426
1427
0
  if (comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &upcomingTermios) < 0)
1428
0
  {
1429
0
    SetLastError(ERROR_IO_DEVICE);
1430
0
    goto error_handle;
1431
0
  }
1432
1433
0
  return (HANDLE)pComm;
1434
0
error_handle:
1435
0
  WINPR_PRAGMA_DIAG_PUSH
1436
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC(void) CloseHandle(pComm);
1437
0
  WINPR_PRAGMA_DIAG_POP
1438
0
  return INVALID_HANDLE_VALUE;
1439
0
}
1440
1441
BOOL CommIsHandled(HANDLE handle)
1442
0
{
1443
0
  if (!CommInitialized())
1444
0
    return FALSE;
1445
1446
0
  return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_COMM, TRUE);
1447
0
}
1448
1449
BOOL CommIsHandleValid(HANDLE handle)
1450
0
{
1451
0
  WINPR_COMM* pComm = (WINPR_COMM*)handle;
1452
0
  if (!CommIsHandled(handle))
1453
0
    return FALSE;
1454
0
  if (pComm->fd <= 0)
1455
0
  {
1456
0
    SetLastError(ERROR_INVALID_HANDLE);
1457
0
    return FALSE;
1458
0
  }
1459
0
  return TRUE;
1460
0
}
1461
1462
BOOL CommCloseHandle(HANDLE handle)
1463
0
{
1464
0
  WINPR_COMM* pComm = (WINPR_COMM*)handle;
1465
1466
0
  if (!CommIsHandled(handle))
1467
0
    return FALSE;
1468
1469
0
  DeleteCriticalSection(&pComm->ReadLock);
1470
0
  DeleteCriticalSection(&pComm->WriteLock);
1471
0
  DeleteCriticalSection(&pComm->EventsLock);
1472
1473
0
  if (pComm->fd > 0)
1474
0
    close(pComm->fd);
1475
1476
0
  if (pComm->fd_write > 0)
1477
0
    close(pComm->fd_write);
1478
1479
0
  if (pComm->fd_write_event > 0)
1480
0
    close(pComm->fd_write_event);
1481
1482
0
  if (pComm->fd_read > 0)
1483
0
    close(pComm->fd_read);
1484
1485
0
  if (pComm->fd_read_event > 0)
1486
0
    close(pComm->fd_read_event);
1487
1488
0
  free(pComm);
1489
0
  return TRUE;
1490
0
}
1491
1492
#if defined(WINPR_HAVE_SYS_EVENTFD_H)
1493
#ifndef WITH_EVENTFD_READ_WRITE
1494
int eventfd_read(int fd, eventfd_t* value)
1495
{
1496
  return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;
1497
}
1498
1499
int eventfd_write(int fd, eventfd_t value)
1500
{
1501
  return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
1502
}
1503
#endif
1504
#endif
1505
1506
static const char* CommIoCtlToStr(unsigned long int io)
1507
0
{
1508
0
  switch (io)
1509
0
  {
1510
0
#if defined(WINPR_HAVE_SERIAL_SUPPORT)
1511
0
#if defined(TCGETS)
1512
0
    case TCGETS:
1513
0
      return "TCGETS";
1514
0
#endif
1515
0
#if defined(TCSETS)
1516
0
    case TCSETS:
1517
0
      return "TCSETS";
1518
0
#endif
1519
0
#if defined(TCSETSW)
1520
0
    case TCSETSW:
1521
0
      return "TCSETSW";
1522
0
#endif
1523
0
#if defined(TCSETSF)
1524
0
    case TCSETSF:
1525
0
      return "TCSETSF";
1526
0
#endif
1527
0
#if defined(TCGETA)
1528
0
    case TCGETA:
1529
0
      return "TCGETA";
1530
0
#endif
1531
0
#if defined(TCSETA)
1532
0
    case TCSETA:
1533
0
      return "TCSETA";
1534
0
#endif
1535
0
#if defined(TCSETAW)
1536
0
    case TCSETAW:
1537
0
      return "TCSETAW";
1538
0
#endif
1539
0
#if defined(TCSETAF)
1540
0
    case TCSETAF:
1541
0
      return "TCSETAF";
1542
0
#endif
1543
0
#if defined(TCSBRK)
1544
0
    case TCSBRK:
1545
0
      return "TCSBRK";
1546
0
#endif
1547
0
#if defined(TCXONC)
1548
0
    case TCXONC:
1549
0
      return "TCXONC";
1550
0
#endif
1551
0
#if defined(TCFLSH)
1552
0
    case TCFLSH:
1553
0
      return "TCFLSH";
1554
0
#endif
1555
0
#if defined(TIOCEXCL)
1556
0
    case TIOCEXCL:
1557
0
      return "TIOCEXCL";
1558
0
#endif
1559
0
#if defined(TIOCNXCL)
1560
0
    case TIOCNXCL:
1561
0
      return "TIOCNXCL";
1562
0
#endif
1563
0
#if defined(TIOCSCTTY)
1564
0
    case TIOCSCTTY:
1565
0
      return "TIOCSCTTY";
1566
0
#endif
1567
0
#if defined(TIOCGPGRP)
1568
0
    case TIOCGPGRP:
1569
0
      return "TIOCGPGRP";
1570
0
#endif
1571
0
#if defined(TIOCSPGRP)
1572
0
    case TIOCSPGRP:
1573
0
      return "TIOCSPGRP";
1574
0
#endif
1575
0
#if defined(TIOCOUTQ)
1576
0
    case TIOCOUTQ:
1577
0
      return "TIOCOUTQ";
1578
0
#endif
1579
0
#if defined(TIOCSTI)
1580
0
    case TIOCSTI:
1581
0
      return "TIOCSTI";
1582
0
#endif
1583
0
#if defined(TIOCGWINSZ)
1584
0
    case TIOCGWINSZ:
1585
0
      return "TIOCGWINSZ";
1586
0
#endif
1587
0
#if defined(TIOCSWINSZ)
1588
0
    case TIOCSWINSZ:
1589
0
      return "TIOCSWINSZ";
1590
0
#endif
1591
0
#if defined(TIOCMGET)
1592
0
    case TIOCMGET:
1593
0
      return "TIOCMGET";
1594
0
#endif
1595
0
#if defined(TIOCMBIS)
1596
0
    case TIOCMBIS:
1597
0
      return "TIOCMBIS";
1598
0
#endif
1599
0
#if defined(TIOCMBIC)
1600
0
    case TIOCMBIC:
1601
0
      return "TIOCMBIC";
1602
0
#endif
1603
0
#if defined(TIOCMSET)
1604
0
    case TIOCMSET:
1605
0
      return "TIOCMSET";
1606
0
#endif
1607
0
#if defined(TIOCGSOFTCAR)
1608
0
    case TIOCGSOFTCAR:
1609
0
      return "TIOCGSOFTCAR";
1610
0
#endif
1611
0
#if defined(TIOCSSOFTCAR)
1612
0
    case TIOCSSOFTCAR:
1613
0
      return "TIOCSSOFTCAR";
1614
0
#endif
1615
0
#if defined(FIONREAD)
1616
0
    case FIONREAD:
1617
0
      return "FIONREAD/TIOCINQ";
1618
0
#endif
1619
0
#if defined(TIOCLINUX)
1620
0
    case TIOCLINUX:
1621
0
      return "TIOCLINUX";
1622
0
#endif
1623
0
#if defined(TIOCCONS)
1624
0
    case TIOCCONS:
1625
0
      return "TIOCCONS";
1626
0
#endif
1627
0
#if defined(TIOCGSERIAL)
1628
0
    case TIOCGSERIAL:
1629
0
      return "TIOCGSERIAL";
1630
0
#endif
1631
0
#if defined(TIOCSSERIAL)
1632
0
    case TIOCSSERIAL:
1633
0
      return "TIOCSSERIAL";
1634
0
#endif
1635
0
#if defined(TIOCPKT)
1636
0
    case TIOCPKT:
1637
0
      return "TIOCPKT";
1638
0
#endif
1639
0
#if defined(FIONBIO)
1640
0
    case FIONBIO:
1641
0
      return "FIONBIO";
1642
0
#endif
1643
0
#if defined(TIOCNOTTY)
1644
0
    case TIOCNOTTY:
1645
0
      return "TIOCNOTTY";
1646
0
#endif
1647
0
#if defined(TIOCSETD)
1648
0
    case TIOCSETD:
1649
0
      return "TIOCSETD";
1650
0
#endif
1651
0
#if defined(TIOCGETD)
1652
0
    case TIOCGETD:
1653
0
      return "TIOCGETD";
1654
0
#endif
1655
0
#if defined(TCSBRKP)
1656
0
    case TCSBRKP:
1657
0
      return "TCSBRKP";
1658
0
#endif
1659
0
#if defined(TIOCSBRK)
1660
0
    case TIOCSBRK:
1661
0
      return "TIOCSBRK";
1662
0
#endif
1663
0
#if defined(TIOCCBRK)
1664
0
    case TIOCCBRK:
1665
0
      return "TIOCCBRK";
1666
0
#endif
1667
0
#if defined(TIOCGSID)
1668
0
    case TIOCGSID:
1669
0
      return "TIOCGSID";
1670
0
#endif
1671
0
#if defined(TIOCGRS485)
1672
0
    case TIOCGRS485:
1673
0
      return "TIOCGRS485";
1674
0
#endif
1675
0
#if defined(TIOCSRS485)
1676
0
    case TIOCSRS485:
1677
0
      return "TIOCSRS485";
1678
0
#endif
1679
0
#if defined(TIOCSPTLCK)
1680
0
    case TIOCSPTLCK:
1681
0
      return "TIOCSPTLCK";
1682
0
#endif
1683
0
#if defined(TCGETX)
1684
0
    case TCGETX:
1685
0
      return "TCGETX";
1686
0
#endif
1687
0
#if defined(TCSETX)
1688
0
    case TCSETX:
1689
0
      return "TCSETX";
1690
0
#endif
1691
0
#if defined(TCSETXF)
1692
0
    case TCSETXF:
1693
0
      return "TCSETXF";
1694
0
#endif
1695
0
#if defined(TCSETXW)
1696
0
    case TCSETXW:
1697
0
      return "TCSETXW";
1698
0
#endif
1699
0
#if defined(TIOCSIG)
1700
0
    case TIOCSIG:
1701
0
      return "TIOCSIG";
1702
0
#endif
1703
0
#if defined(TIOCVHANGUP)
1704
0
    case TIOCVHANGUP:
1705
0
      return "TIOCVHANGUP";
1706
0
#endif
1707
0
#if defined(TIOCGPTPEER)
1708
0
    case TIOCGPTPEER:
1709
0
      return "TIOCGPTPEER";
1710
0
#endif
1711
0
#if defined(FIONCLEX)
1712
0
    case FIONCLEX:
1713
0
      return "FIONCLEX";
1714
0
#endif
1715
0
#if defined(FIOCLEX)
1716
0
    case FIOCLEX:
1717
0
      return "FIOCLEX";
1718
0
#endif
1719
0
#if defined(FIOASYNC)
1720
0
    case FIOASYNC:
1721
0
      return "FIOASYNC";
1722
0
#endif
1723
0
#if defined(TIOCSERCONFIG)
1724
0
    case TIOCSERCONFIG:
1725
0
      return "TIOCSERCONFIG";
1726
0
#endif
1727
0
#if defined(TIOCSERGWILD)
1728
0
    case TIOCSERGWILD:
1729
0
      return "TIOCSERGWILD";
1730
0
#endif
1731
0
#if defined(TIOCSERSWILD)
1732
0
    case TIOCSERSWILD:
1733
0
      return "TIOCSERSWILD";
1734
0
#endif
1735
0
#if defined(TIOCGLCKTRMIOS)
1736
0
    case TIOCGLCKTRMIOS:
1737
0
      return "TIOCGLCKTRMIOS";
1738
0
#endif
1739
0
#if defined(TIOCSLCKTRMIOS)
1740
0
    case TIOCSLCKTRMIOS:
1741
0
      return "TIOCSLCKTRMIOS";
1742
0
#endif
1743
0
#if defined(TIOCSERGSTRUCT)
1744
0
    case TIOCSERGSTRUCT:
1745
0
      return "TIOCSERGSTRUCT";
1746
0
#endif
1747
0
#if defined(TIOCSERGETLSR)
1748
0
    case TIOCSERGETLSR:
1749
0
      return "TIOCSERGETLSR";
1750
0
#endif
1751
0
#if defined(TIOCSERGETMULTI)
1752
0
    case TIOCSERGETMULTI:
1753
0
      return "TIOCSERGETMULTI";
1754
0
#endif
1755
0
#if defined(TIOCSERSETMULTI)
1756
0
    case TIOCSERSETMULTI:
1757
0
      return "TIOCSERSETMULTI";
1758
0
#endif
1759
0
#if defined(TIOCMIWAIT)
1760
0
    case TIOCMIWAIT:
1761
0
      return "TIOCMIWAIT";
1762
0
#endif
1763
0
#if defined(TIOCGICOUNT)
1764
0
    case TIOCGICOUNT:
1765
0
      return "TIOCGICOUNT";
1766
0
#endif
1767
0
#if defined(FIOQSIZE)
1768
0
    case FIOQSIZE:
1769
0
      return "FIOQSIZE";
1770
0
#endif
1771
0
#if defined(TIOCPKT_DATA)
1772
0
    case TIOCPKT_DATA:
1773
0
      return "TIOCPKT_DATA";
1774
0
#endif
1775
0
#if defined(TIOCPKT_FLUSHWRITE)
1776
0
    case TIOCPKT_FLUSHWRITE:
1777
0
      return "TIOCPKT_FLUSHWRITE";
1778
0
#endif
1779
0
#if defined(TIOCPKT_STOP)
1780
0
    case TIOCPKT_STOP:
1781
0
      return "TIOCPKT_STOP";
1782
0
#endif
1783
0
#if defined(TIOCPKT_START)
1784
0
    case TIOCPKT_START:
1785
0
      return "TIOCPKT_START";
1786
0
#endif
1787
0
#if defined(TIOCPKT_NOSTOP)
1788
0
    case TIOCPKT_NOSTOP:
1789
0
      return "TIOCPKT_NOSTOP";
1790
0
#endif
1791
0
#if defined(TIOCPKT_DOSTOP)
1792
0
    case TIOCPKT_DOSTOP:
1793
0
      return "TIOCPKT_DOSTOP";
1794
0
#endif
1795
0
#if defined(TIOCPKT_IOCTL)
1796
0
    case TIOCPKT_IOCTL:
1797
0
      return "TIOCPKT_IOCTL";
1798
0
#endif
1799
0
#endif
1800
0
    default:
1801
0
      return "UNKNOWN";
1802
0
  }
1803
0
}
1804
1805
static BOOL CommStatusErrorEx(WINPR_COMM* pComm, unsigned long int ctl, const char* file,
1806
                              const char* fkt, size_t line)
1807
0
{
1808
0
  WINPR_ASSERT(pComm);
1809
0
  BOOL rc = pComm->permissive ? TRUE : FALSE;
1810
0
  const DWORD level = rc ? WLOG_DEBUG : WLOG_WARN;
1811
0
  char ebuffer[256] = { 0 };
1812
0
  const char* str = CommIoCtlToStr(ctl);
1813
1814
0
  if (CommInitialized())
1815
0
  {
1816
0
    if (WLog_IsLevelActive(sLog, level))
1817
0
    {
1818
0
      WLog_PrintTextMessage(sLog, level, line, file, fkt,
1819
0
                            "%s [0x%08lx] ioctl failed, errno=[%d] %s.", str, ctl, errno,
1820
0
                            winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
1821
0
    }
1822
0
  }
1823
1824
0
  if (!rc)
1825
0
    SetLastError(ERROR_IO_DEVICE);
1826
1827
0
  return rc;
1828
0
}
1829
1830
BOOL CommIoCtl_int(WINPR_COMM* pComm, unsigned long int ctl, void* data, const char* file,
1831
                   const char* fkt, size_t line)
1832
0
{
1833
0
  if (ioctl(pComm->fd, ctl, data) < 0)
1834
0
  {
1835
0
    if (!CommStatusErrorEx(pComm, ctl, file, fkt, line))
1836
0
      return FALSE;
1837
0
  }
1838
0
  return TRUE;
1839
0
}
1840
1841
BOOL CommUpdateIOCount(WINPR_ATTR_UNUSED HANDLE handle, WINPR_ATTR_UNUSED BOOL checkSupportStatus)
1842
0
{
1843
0
  WINPR_COMM* pComm = (WINPR_COMM*)handle;
1844
0
  WINPR_ASSERT(pComm);
1845
1846
0
#if defined(WINPR_HAVE_COMM_COUNTERS)
1847
0
  ZeroMemory(&(pComm->counters), sizeof(struct serial_icounter_struct));
1848
0
  if (pComm->TIOCGICOUNTSupported || checkSupportStatus)
1849
0
  {
1850
0
    const int rc = ioctl(pComm->fd, TIOCGICOUNT, &(pComm->counters));
1851
0
    if (checkSupportStatus)
1852
0
      pComm->TIOCGICOUNTSupported = rc >= 0;
1853
0
    else if (rc < 0)
1854
0
    {
1855
0
      if (!CommStatusErrorEx(pComm, TIOCGICOUNT, __FILE__, __func__, __LINE__))
1856
0
        return FALSE;
1857
0
    }
1858
0
  }
1859
0
#endif
1860
0
  return TRUE;
1861
0
}
1862
1863
static const char* CommSerialEvFlagString(ULONG flag)
1864
0
{
1865
0
  switch (flag)
1866
0
  {
1867
0
    case SERIAL_EV_RXCHAR:
1868
0
      return "SERIAL_EV_RXCHAR";
1869
0
    case SERIAL_EV_RXFLAG:
1870
0
      return "SERIAL_EV_RXFLAG";
1871
0
    case SERIAL_EV_TXEMPTY:
1872
0
      return "SERIAL_EV_TXEMPTY";
1873
0
    case SERIAL_EV_CTS:
1874
0
      return "SERIAL_EV_CTS ";
1875
0
    case SERIAL_EV_DSR:
1876
0
      return "SERIAL_EV_DSR ";
1877
0
    case SERIAL_EV_RLSD:
1878
0
      return "SERIAL_EV_RLSD";
1879
0
    case SERIAL_EV_BREAK:
1880
0
      return "SERIAL_EV_BREAK";
1881
0
    case SERIAL_EV_ERR:
1882
0
      return "SERIAL_EV_ERR ";
1883
0
    case SERIAL_EV_RING:
1884
0
      return "SERIAL_EV_RING";
1885
0
    case SERIAL_EV_PERR:
1886
0
      return "SERIAL_EV_PERR";
1887
0
    case SERIAL_EV_RX80FULL:
1888
0
      return "SERIAL_EV_RX80FULL";
1889
0
    case SERIAL_EV_EVENT1:
1890
0
      return "SERIAL_EV_EVENT1";
1891
0
    case SERIAL_EV_EVENT2:
1892
0
      return "SERIAL_EV_EVENT2";
1893
0
    case SERIAL_EV_WINPR_WAITING:
1894
0
      return "SERIAL_EV_WINPR_WAITING";
1895
0
    case SERIAL_EV_WINPR_STOP:
1896
0
      return "SERIAL_EV_WINPR_STOP";
1897
0
    default:
1898
0
      return "SERIAL_EV_UNKNOWN";
1899
0
  }
1900
0
}
1901
1902
const char* CommSerialEvString(ULONG status, char* buffer, size_t size)
1903
0
{
1904
0
  const ULONG flags[] = { SERIAL_EV_RXCHAR, SERIAL_EV_RXFLAG,        SERIAL_EV_TXEMPTY,
1905
0
                        SERIAL_EV_CTS,    SERIAL_EV_DSR,           SERIAL_EV_RLSD,
1906
0
                        SERIAL_EV_BREAK,  SERIAL_EV_ERR,           SERIAL_EV_RING,
1907
0
                        SERIAL_EV_PERR,   SERIAL_EV_RX80FULL,      SERIAL_EV_EVENT1,
1908
0
                        SERIAL_EV_EVENT2, SERIAL_EV_WINPR_WAITING, SERIAL_EV_WINPR_STOP };
1909
1910
0
  winpr_str_append("{", buffer, size, "");
1911
1912
0
  const char* sep = "";
1913
0
  for (size_t x = 0; x < ARRAYSIZE(flags); x++)
1914
0
  {
1915
0
    const ULONG flag = flags[x];
1916
0
    if (status & flag)
1917
0
    {
1918
0
      winpr_str_append(CommSerialEvFlagString(flag), buffer, size, sep);
1919
0
      sep = "|";
1920
0
    }
1921
0
  }
1922
1923
0
  char number[32] = { 0 };
1924
0
  (void)_snprintf(number, sizeof(number), "}[0x%08" PRIx32 "]", status);
1925
0
  winpr_str_append(number, buffer, size, "");
1926
0
  return buffer;
1927
0
}