Coverage Report

Created: 2026-03-04 06:13

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 = nullptr;
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 nullptr-terminated array with a maximum of COMM_DEVICE_MAX COMM_DEVICE */
69
0
#define COMM_DEVICE_MAX 128
70
static COMM_DEVICE** sCommDevices = nullptr;
71
static CRITICAL_SECTION sCommDevicesLock = WINPR_C_ARRAY_INIT;
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 nullptr;
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 == nullptr);
167
0
  WINPR_ASSERT(sCommDevices == nullptr);
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 = nullptr;
177
0
    return;
178
0
  }
179
180
0
  sLog = WLog_Get(TAG);
181
0
  WINPR_ASSERT(sLog != nullptr);
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 = WINPR_C_ARRAY_INIT;
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, nullptr, 0, lpCommProp,
385
0
                           sizeof(COMMPROP), &bytesReturned, nullptr))
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 = nullptr;
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 == nullptr)
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, nullptr, 0, &baudRate,
444
0
                           sizeof(SERIAL_BAUD_RATE), &bytesReturned, nullptr))
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, nullptr, 0, &handflow,
462
0
                           sizeof(SERIAL_HANDFLOW), &bytesReturned, nullptr))
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 = WINPR_C_ARRAY_INIT;
514
515
0
    if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_LINE_CONTROL, nullptr, 0, &lineControl,
516
0
                             sizeof(SERIAL_LINE_CONTROL), &bytesReturned, nullptr))
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 = WINPR_C_ARRAY_INIT;
529
530
0
    if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_CHARS, nullptr, 0, &serialChars,
531
0
                             sizeof(SERIAL_CHARS), &bytesReturned, nullptr))
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 = WINPR_C_ARRAY_INIT;
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
                           nullptr, 0, &bytesReturned, nullptr))
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, nullptr, 0, &serialChars,
595
0
                           sizeof(SERIAL_CHARS), &bytesReturned,
596
0
                           nullptr)) /* 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
                           nullptr, 0, &bytesReturned, nullptr))
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), nullptr, 0, &bytesReturned, nullptr))
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 = WINPR_C_ARRAY_INIT;
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
                           nullptr, 0, &bytesReturned, nullptr))
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, nullptr, 0, lpCommTimeouts,
794
0
                           sizeof(COMMTIMEOUTS), &bytesReturned, nullptr))
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
                           nullptr, 0, &bytesReturned, nullptr))
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), nullptr, 0,
936
0
                           &bytesReturned, nullptr))
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), nullptr, 0, &bytesReturned, nullptr))
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 = nullptr;
1031
0
  LPTSTR storedTargetPath = nullptr;
1032
1033
0
  if (!CommInitialized())
1034
0
    return FALSE;
1035
1036
0
  EnterCriticalSection(&sCommDevicesLock);
1037
1038
0
  if (sCommDevices == nullptr)
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 == nullptr)
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 == nullptr)
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] != nullptr)
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] == nullptr)
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 nullptr 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 = nullptr;
1128
0
  SetLastError(ERROR_SUCCESS);
1129
1130
0
  if (!CommInitialized())
1131
0
    return 0;
1132
1133
0
  if (sCommDevices == nullptr)
1134
0
  {
1135
0
    SetLastError(ERROR_DLL_INIT_FAILED);
1136
0
    return 0;
1137
0
  }
1138
1139
0
  if (lpDeviceName == nullptr || lpTargetPath == nullptr)
1140
0
  {
1141
0
    SetLastError(ERROR_NOT_SUPPORTED);
1142
0
    return 0;
1143
0
  }
1144
1145
0
  EnterCriticalSection(&sCommDevicesLock);
1146
0
  storedTargetPath = nullptr;
1147
1148
0
  for (int i = 0; i < COMM_DEVICE_MAX; i++)
1149
0
  {
1150
0
    if (sCommDevices[i] != nullptr)
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 == nullptr)
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 = nullptr;
1209
0
  WINPR_COMM* pComm = nullptr;
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, CommGetFd, nullptr, /* CleanupHandle */
1225
                        nullptr,       nullptr,         nullptr,   nullptr, nullptr, nullptr,
1226
                        nullptr,       nullptr,         nullptr,   nullptr, nullptr, nullptr,
1227
                        nullptr,       nullptr,         nullptr,   nullptr, nullptr };
1228
1229
/**
1230
 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363198%28v=vs.85%29.aspx
1231
 *
1232
 * @param lpDeviceName e.g. COM1, ...
1233
 *
1234
 * @param dwDesiredAccess expects GENERIC_READ | GENERIC_WRITE, a
1235
 * warning message is printed otherwise. TODO: better support.
1236
 *
1237
 * @param dwShareMode must be zero, INVALID_HANDLE_VALUE is returned
1238
 * otherwise and GetLastError() should return ERROR_SHARING_VIOLATION.
1239
 *
1240
 * @param lpSecurityAttributes nullptr expected, a warning message is printed
1241
 * otherwise. TODO: better support.
1242
 *
1243
 * @param dwCreationDisposition must be OPEN_EXISTING. If the
1244
 * communication device doesn't exist INVALID_HANDLE_VALUE is returned
1245
 * and GetLastError() returns ERROR_FILE_NOT_FOUND.
1246
 *
1247
 * @param dwFlagsAndAttributes zero expected, a warning message is
1248
 * printed otherwise.
1249
 *
1250
 * @param hTemplateFile must be nullptr.
1251
 *
1252
 * @return INVALID_HANDLE_VALUE on error.
1253
 */
1254
HANDLE CommCreateFileA(LPCSTR lpDeviceName, DWORD dwDesiredAccess, DWORD dwShareMode,
1255
                       LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
1256
                       DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
1257
0
{
1258
0
  CHAR devicePath[MAX_PATH] = WINPR_C_ARRAY_INIT;
1259
0
  struct stat deviceStat = WINPR_C_ARRAY_INIT;
1260
0
  WINPR_COMM* pComm = nullptr;
1261
0
  struct termios upcomingTermios = WINPR_C_ARRAY_INIT;
1262
1263
0
  if (!CommInitialized())
1264
0
    return INVALID_HANDLE_VALUE;
1265
1266
0
  if (dwDesiredAccess != (GENERIC_READ | GENERIC_WRITE))
1267
0
  {
1268
0
    CommLog_Print(WLOG_WARN, "unexpected access to the device: 0x%08" PRIX32 "",
1269
0
                  dwDesiredAccess);
1270
0
  }
1271
1272
0
  if (dwShareMode != 0)
1273
0
  {
1274
0
    SetLastError(ERROR_SHARING_VIOLATION);
1275
0
    return INVALID_HANDLE_VALUE;
1276
0
  }
1277
1278
  /* TODO: Prevents other processes from opening a file or
1279
   * device if they request delete, read, or write access. */
1280
1281
0
  if (lpSecurityAttributes != nullptr)
1282
0
  {
1283
0
    CommLog_Print(WLOG_WARN, "unexpected security attributes, nLength=%" PRIu32 "",
1284
0
                  lpSecurityAttributes->nLength);
1285
0
  }
1286
1287
0
  if (dwCreationDisposition != OPEN_EXISTING)
1288
0
  {
1289
0
    SetLastError(ERROR_FILE_NOT_FOUND); /* FIXME: ERROR_NOT_SUPPORTED better? */
1290
0
    return INVALID_HANDLE_VALUE;
1291
0
  }
1292
1293
0
  if (QueryCommDevice(lpDeviceName, devicePath, MAX_PATH) <= 0)
1294
0
  {
1295
    /* SetLastError(GetLastError()); */
1296
0
    return INVALID_HANDLE_VALUE;
1297
0
  }
1298
1299
0
  if (stat(devicePath, &deviceStat) < 0)
1300
0
  {
1301
0
    CommLog_Print(WLOG_WARN, "device not found %s", devicePath);
1302
0
    SetLastError(ERROR_FILE_NOT_FOUND);
1303
0
    return INVALID_HANDLE_VALUE;
1304
0
  }
1305
1306
0
  if (!S_ISCHR(deviceStat.st_mode))
1307
0
  {
1308
0
    CommLog_Print(WLOG_WARN, "bad device %s", devicePath);
1309
0
    SetLastError(ERROR_BAD_DEVICE);
1310
0
    return INVALID_HANDLE_VALUE;
1311
0
  }
1312
1313
0
  if (dwFlagsAndAttributes != 0)
1314
0
  {
1315
0
    CommLog_Print(WLOG_WARN, "unexpected flags and attributes: 0x%08" PRIX32 "",
1316
0
                  dwFlagsAndAttributes);
1317
0
  }
1318
1319
0
  if (hTemplateFile != nullptr)
1320
0
  {
1321
0
    SetLastError(ERROR_NOT_SUPPORTED); /* FIXME: other proper error? */
1322
0
    return INVALID_HANDLE_VALUE;
1323
0
  }
1324
1325
0
  pComm = (WINPR_COMM*)calloc(1, sizeof(WINPR_COMM));
1326
1327
0
  if (pComm == nullptr)
1328
0
  {
1329
0
    SetLastError(ERROR_OUTOFMEMORY);
1330
0
    return INVALID_HANDLE_VALUE;
1331
0
  }
1332
1333
0
  WINPR_HANDLE_SET_TYPE_AND_MODE(pComm, HANDLE_TYPE_COMM, WINPR_FD_READ);
1334
0
  pComm->common.ops = &ops;
1335
  /* error_handle */
1336
0
  pComm->fd = open(devicePath, O_RDWR | O_NOCTTY | O_NONBLOCK);
1337
1338
0
  if (pComm->fd < 0)
1339
0
  {
1340
0
    CommLog_Print(WLOG_WARN, "failed to open device %s", devicePath);
1341
0
    SetLastError(ERROR_BAD_DEVICE);
1342
0
    goto error_handle;
1343
0
  }
1344
1345
0
  pComm->fd_read = open(devicePath, O_RDONLY | O_NOCTTY | O_NONBLOCK);
1346
1347
0
  if (pComm->fd_read < 0)
1348
0
  {
1349
0
    CommLog_Print(WLOG_WARN, "failed to open fd_read, device: %s", devicePath);
1350
0
    SetLastError(ERROR_BAD_DEVICE);
1351
0
    goto error_handle;
1352
0
  }
1353
1354
0
#if defined(WINPR_HAVE_SYS_EVENTFD_H)
1355
0
  pComm->fd_read_event = eventfd(
1356
0
      0, EFD_NONBLOCK); /* EFD_NONBLOCK required because a read() is not always expected */
1357
0
#endif
1358
1359
0
  if (pComm->fd_read_event < 0)
1360
0
  {
1361
0
    CommLog_Print(WLOG_WARN, "failed to open fd_read_event, device: %s", devicePath);
1362
0
    SetLastError(ERROR_BAD_DEVICE);
1363
0
    goto error_handle;
1364
0
  }
1365
1366
0
  InitializeCriticalSection(&pComm->ReadLock);
1367
0
  pComm->fd_write = open(devicePath, O_WRONLY | O_NOCTTY | O_NONBLOCK);
1368
1369
0
  if (pComm->fd_write < 0)
1370
0
  {
1371
0
    CommLog_Print(WLOG_WARN, "failed to open fd_write, device: %s", devicePath);
1372
0
    SetLastError(ERROR_BAD_DEVICE);
1373
0
    goto error_handle;
1374
0
  }
1375
1376
0
#if defined(WINPR_HAVE_SYS_EVENTFD_H)
1377
0
  pComm->fd_write_event = eventfd(
1378
0
      0, EFD_NONBLOCK); /* EFD_NONBLOCK required because a read() is not always expected */
1379
0
#endif
1380
1381
0
  if (pComm->fd_write_event < 0)
1382
0
  {
1383
0
    CommLog_Print(WLOG_WARN, "failed to open fd_write_event, device: %s", devicePath);
1384
0
    SetLastError(ERROR_BAD_DEVICE);
1385
0
    goto error_handle;
1386
0
  }
1387
1388
0
  InitializeCriticalSection(&pComm->WriteLock);
1389
  /* can also be setup later on with _comm_setServerSerialDriver() */
1390
0
  pComm->serverSerialDriverId = SerialDriverUnknown;
1391
0
  InitializeCriticalSection(&pComm->EventsLock);
1392
1393
0
  (void)CommUpdateIOCount(pComm, TRUE);
1394
1395
  /* The binary/raw mode is required for the redirection but
1396
   * only flags that are not handle somewhere-else, except
1397
   * ICANON, are forced here. */
1398
0
  ZeroMemory(&upcomingTermios, sizeof(struct termios));
1399
1400
0
  if (tcgetattr(pComm->fd, &upcomingTermios) < 0)
1401
0
  {
1402
0
    SetLastError(ERROR_IO_DEVICE);
1403
0
    goto error_handle;
1404
0
  }
1405
1406
0
  upcomingTermios.c_iflag &=
1407
0
      (tcflag_t) ~(/*IGNBRK |*/ BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL /*| IXON*/);
1408
0
  upcomingTermios.c_oflag = 0; /* <=> &= ~OPOST */
1409
0
  upcomingTermios.c_lflag = 0; /* <=> &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); */
1410
  /* upcomingTermios.c_cflag &= ~(CSIZE | PARENB); */
1411
  /* upcomingTermios.c_cflag |= CS8; */
1412
  /* About missing flags recommended by termios(3):
1413
   *
1414
   *   IGNBRK and IXON, see: IOCTL_SERIAL_SET_HANDFLOW
1415
   *   CSIZE, PARENB and CS8, see: IOCTL_SERIAL_SET_LINE_CONTROL
1416
   */
1417
  /* a few more settings required for the redirection */
1418
0
  upcomingTermios.c_cflag |= CLOCAL | CREAD;
1419
1420
0
  if (comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &upcomingTermios) < 0)
1421
0
  {
1422
0
    SetLastError(ERROR_IO_DEVICE);
1423
0
    goto error_handle;
1424
0
  }
1425
1426
0
  return (HANDLE)pComm;
1427
0
error_handle:
1428
0
  WINPR_PRAGMA_DIAG_PUSH
1429
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC(void) CloseHandle(pComm);
1430
0
  WINPR_PRAGMA_DIAG_POP
1431
0
  return INVALID_HANDLE_VALUE;
1432
0
}
1433
1434
BOOL CommIsHandled(HANDLE handle)
1435
0
{
1436
0
  if (!CommInitialized())
1437
0
    return FALSE;
1438
1439
0
  return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_COMM, TRUE);
1440
0
}
1441
1442
BOOL CommIsHandleValid(HANDLE handle)
1443
0
{
1444
0
  WINPR_COMM* pComm = (WINPR_COMM*)handle;
1445
0
  if (!CommIsHandled(handle))
1446
0
    return FALSE;
1447
0
  if (pComm->fd <= 0)
1448
0
  {
1449
0
    SetLastError(ERROR_INVALID_HANDLE);
1450
0
    return FALSE;
1451
0
  }
1452
0
  return TRUE;
1453
0
}
1454
1455
BOOL CommCloseHandle(HANDLE handle)
1456
0
{
1457
0
  WINPR_COMM* pComm = (WINPR_COMM*)handle;
1458
1459
0
  if (!CommIsHandled(handle))
1460
0
    return FALSE;
1461
1462
0
  DeleteCriticalSection(&pComm->ReadLock);
1463
0
  DeleteCriticalSection(&pComm->WriteLock);
1464
0
  DeleteCriticalSection(&pComm->EventsLock);
1465
1466
0
  if (pComm->fd > 0)
1467
0
    close(pComm->fd);
1468
1469
0
  if (pComm->fd_write > 0)
1470
0
    close(pComm->fd_write);
1471
1472
0
  if (pComm->fd_write_event > 0)
1473
0
    close(pComm->fd_write_event);
1474
1475
0
  if (pComm->fd_read > 0)
1476
0
    close(pComm->fd_read);
1477
1478
0
  if (pComm->fd_read_event > 0)
1479
0
    close(pComm->fd_read_event);
1480
1481
0
  free(pComm);
1482
0
  return TRUE;
1483
0
}
1484
1485
#if defined(WINPR_HAVE_SYS_EVENTFD_H)
1486
#ifndef WITH_EVENTFD_READ_WRITE
1487
int eventfd_read(int fd, eventfd_t* value)
1488
{
1489
  return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;
1490
}
1491
1492
int eventfd_write(int fd, eventfd_t value)
1493
{
1494
  return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
1495
}
1496
#endif
1497
#endif
1498
1499
static const char* CommIoCtlToStr(unsigned long int io)
1500
0
{
1501
0
  switch (io)
1502
0
  {
1503
0
#if defined(WINPR_HAVE_SERIAL_SUPPORT)
1504
0
#if defined(TCGETS)
1505
0
    case TCGETS:
1506
0
      return "TCGETS";
1507
0
#endif
1508
0
#if defined(TCSETS)
1509
0
    case TCSETS:
1510
0
      return "TCSETS";
1511
0
#endif
1512
0
#if defined(TCSETSW)
1513
0
    case TCSETSW:
1514
0
      return "TCSETSW";
1515
0
#endif
1516
0
#if defined(TCSETSF)
1517
0
    case TCSETSF:
1518
0
      return "TCSETSF";
1519
0
#endif
1520
0
#if defined(TCGETA)
1521
0
    case TCGETA:
1522
0
      return "TCGETA";
1523
0
#endif
1524
0
#if defined(TCSETA)
1525
0
    case TCSETA:
1526
0
      return "TCSETA";
1527
0
#endif
1528
0
#if defined(TCSETAW)
1529
0
    case TCSETAW:
1530
0
      return "TCSETAW";
1531
0
#endif
1532
0
#if defined(TCSETAF)
1533
0
    case TCSETAF:
1534
0
      return "TCSETAF";
1535
0
#endif
1536
0
#if defined(TCSBRK)
1537
0
    case TCSBRK:
1538
0
      return "TCSBRK";
1539
0
#endif
1540
0
#if defined(TCXONC)
1541
0
    case TCXONC:
1542
0
      return "TCXONC";
1543
0
#endif
1544
0
#if defined(TCFLSH)
1545
0
    case TCFLSH:
1546
0
      return "TCFLSH";
1547
0
#endif
1548
0
#if defined(TIOCEXCL)
1549
0
    case TIOCEXCL:
1550
0
      return "TIOCEXCL";
1551
0
#endif
1552
0
#if defined(TIOCNXCL)
1553
0
    case TIOCNXCL:
1554
0
      return "TIOCNXCL";
1555
0
#endif
1556
0
#if defined(TIOCSCTTY)
1557
0
    case TIOCSCTTY:
1558
0
      return "TIOCSCTTY";
1559
0
#endif
1560
0
#if defined(TIOCGPGRP)
1561
0
    case TIOCGPGRP:
1562
0
      return "TIOCGPGRP";
1563
0
#endif
1564
0
#if defined(TIOCSPGRP)
1565
0
    case TIOCSPGRP:
1566
0
      return "TIOCSPGRP";
1567
0
#endif
1568
0
#if defined(TIOCOUTQ)
1569
0
    case TIOCOUTQ:
1570
0
      return "TIOCOUTQ";
1571
0
#endif
1572
0
#if defined(TIOCSTI)
1573
0
    case TIOCSTI:
1574
0
      return "TIOCSTI";
1575
0
#endif
1576
0
#if defined(TIOCGWINSZ)
1577
0
    case TIOCGWINSZ:
1578
0
      return "TIOCGWINSZ";
1579
0
#endif
1580
0
#if defined(TIOCSWINSZ)
1581
0
    case TIOCSWINSZ:
1582
0
      return "TIOCSWINSZ";
1583
0
#endif
1584
0
#if defined(TIOCMGET)
1585
0
    case TIOCMGET:
1586
0
      return "TIOCMGET";
1587
0
#endif
1588
0
#if defined(TIOCMBIS)
1589
0
    case TIOCMBIS:
1590
0
      return "TIOCMBIS";
1591
0
#endif
1592
0
#if defined(TIOCMBIC)
1593
0
    case TIOCMBIC:
1594
0
      return "TIOCMBIC";
1595
0
#endif
1596
0
#if defined(TIOCMSET)
1597
0
    case TIOCMSET:
1598
0
      return "TIOCMSET";
1599
0
#endif
1600
0
#if defined(TIOCGSOFTCAR)
1601
0
    case TIOCGSOFTCAR:
1602
0
      return "TIOCGSOFTCAR";
1603
0
#endif
1604
0
#if defined(TIOCSSOFTCAR)
1605
0
    case TIOCSSOFTCAR:
1606
0
      return "TIOCSSOFTCAR";
1607
0
#endif
1608
0
#if defined(FIONREAD)
1609
0
    case FIONREAD:
1610
0
      return "FIONREAD/TIOCINQ";
1611
0
#endif
1612
0
#if defined(TIOCLINUX)
1613
0
    case TIOCLINUX:
1614
0
      return "TIOCLINUX";
1615
0
#endif
1616
0
#if defined(TIOCCONS)
1617
0
    case TIOCCONS:
1618
0
      return "TIOCCONS";
1619
0
#endif
1620
0
#if defined(TIOCGSERIAL)
1621
0
    case TIOCGSERIAL:
1622
0
      return "TIOCGSERIAL";
1623
0
#endif
1624
0
#if defined(TIOCSSERIAL)
1625
0
    case TIOCSSERIAL:
1626
0
      return "TIOCSSERIAL";
1627
0
#endif
1628
0
#if defined(TIOCPKT)
1629
0
    case TIOCPKT:
1630
0
      return "TIOCPKT";
1631
0
#endif
1632
0
#if defined(FIONBIO)
1633
0
    case FIONBIO:
1634
0
      return "FIONBIO";
1635
0
#endif
1636
0
#if defined(TIOCNOTTY)
1637
0
    case TIOCNOTTY:
1638
0
      return "TIOCNOTTY";
1639
0
#endif
1640
0
#if defined(TIOCSETD)
1641
0
    case TIOCSETD:
1642
0
      return "TIOCSETD";
1643
0
#endif
1644
0
#if defined(TIOCGETD)
1645
0
    case TIOCGETD:
1646
0
      return "TIOCGETD";
1647
0
#endif
1648
0
#if defined(TCSBRKP)
1649
0
    case TCSBRKP:
1650
0
      return "TCSBRKP";
1651
0
#endif
1652
0
#if defined(TIOCSBRK)
1653
0
    case TIOCSBRK:
1654
0
      return "TIOCSBRK";
1655
0
#endif
1656
0
#if defined(TIOCCBRK)
1657
0
    case TIOCCBRK:
1658
0
      return "TIOCCBRK";
1659
0
#endif
1660
0
#if defined(TIOCGSID)
1661
0
    case TIOCGSID:
1662
0
      return "TIOCGSID";
1663
0
#endif
1664
0
#if defined(TIOCGRS485)
1665
0
    case TIOCGRS485:
1666
0
      return "TIOCGRS485";
1667
0
#endif
1668
0
#if defined(TIOCSRS485)
1669
0
    case TIOCSRS485:
1670
0
      return "TIOCSRS485";
1671
0
#endif
1672
0
#if defined(TIOCSPTLCK)
1673
0
    case TIOCSPTLCK:
1674
0
      return "TIOCSPTLCK";
1675
0
#endif
1676
0
#if defined(TCGETX)
1677
0
    case TCGETX:
1678
0
      return "TCGETX";
1679
0
#endif
1680
0
#if defined(TCSETX)
1681
0
    case TCSETX:
1682
0
      return "TCSETX";
1683
0
#endif
1684
0
#if defined(TCSETXF)
1685
0
    case TCSETXF:
1686
0
      return "TCSETXF";
1687
0
#endif
1688
0
#if defined(TCSETXW)
1689
0
    case TCSETXW:
1690
0
      return "TCSETXW";
1691
0
#endif
1692
0
#if defined(TIOCSIG)
1693
0
    case TIOCSIG:
1694
0
      return "TIOCSIG";
1695
0
#endif
1696
0
#if defined(TIOCVHANGUP)
1697
0
    case TIOCVHANGUP:
1698
0
      return "TIOCVHANGUP";
1699
0
#endif
1700
0
#if defined(TIOCGPTPEER)
1701
0
    case TIOCGPTPEER:
1702
0
      return "TIOCGPTPEER";
1703
0
#endif
1704
0
#if defined(FIONCLEX)
1705
0
    case FIONCLEX:
1706
0
      return "FIONCLEX";
1707
0
#endif
1708
0
#if defined(FIOCLEX)
1709
0
    case FIOCLEX:
1710
0
      return "FIOCLEX";
1711
0
#endif
1712
0
#if defined(FIOASYNC)
1713
0
    case FIOASYNC:
1714
0
      return "FIOASYNC";
1715
0
#endif
1716
0
#if defined(TIOCSERCONFIG)
1717
0
    case TIOCSERCONFIG:
1718
0
      return "TIOCSERCONFIG";
1719
0
#endif
1720
0
#if defined(TIOCSERGWILD)
1721
0
    case TIOCSERGWILD:
1722
0
      return "TIOCSERGWILD";
1723
0
#endif
1724
0
#if defined(TIOCSERSWILD)
1725
0
    case TIOCSERSWILD:
1726
0
      return "TIOCSERSWILD";
1727
0
#endif
1728
0
#if defined(TIOCGLCKTRMIOS)
1729
0
    case TIOCGLCKTRMIOS:
1730
0
      return "TIOCGLCKTRMIOS";
1731
0
#endif
1732
0
#if defined(TIOCSLCKTRMIOS)
1733
0
    case TIOCSLCKTRMIOS:
1734
0
      return "TIOCSLCKTRMIOS";
1735
0
#endif
1736
0
#if defined(TIOCSERGSTRUCT)
1737
0
    case TIOCSERGSTRUCT:
1738
0
      return "TIOCSERGSTRUCT";
1739
0
#endif
1740
0
#if defined(TIOCSERGETLSR)
1741
0
    case TIOCSERGETLSR:
1742
0
      return "TIOCSERGETLSR";
1743
0
#endif
1744
0
#if defined(TIOCSERGETMULTI)
1745
0
    case TIOCSERGETMULTI:
1746
0
      return "TIOCSERGETMULTI";
1747
0
#endif
1748
0
#if defined(TIOCSERSETMULTI)
1749
0
    case TIOCSERSETMULTI:
1750
0
      return "TIOCSERSETMULTI";
1751
0
#endif
1752
0
#if defined(TIOCMIWAIT)
1753
0
    case TIOCMIWAIT:
1754
0
      return "TIOCMIWAIT";
1755
0
#endif
1756
0
#if defined(TIOCGICOUNT)
1757
0
    case TIOCGICOUNT:
1758
0
      return "TIOCGICOUNT";
1759
0
#endif
1760
0
#if defined(FIOQSIZE)
1761
0
    case FIOQSIZE:
1762
0
      return "FIOQSIZE";
1763
0
#endif
1764
0
#if defined(TIOCPKT_DATA)
1765
0
    case TIOCPKT_DATA:
1766
0
      return "TIOCPKT_DATA";
1767
0
#endif
1768
0
#if defined(TIOCPKT_FLUSHWRITE)
1769
0
    case TIOCPKT_FLUSHWRITE:
1770
0
      return "TIOCPKT_FLUSHWRITE";
1771
0
#endif
1772
0
#if defined(TIOCPKT_STOP)
1773
0
    case TIOCPKT_STOP:
1774
0
      return "TIOCPKT_STOP";
1775
0
#endif
1776
0
#if defined(TIOCPKT_START)
1777
0
    case TIOCPKT_START:
1778
0
      return "TIOCPKT_START";
1779
0
#endif
1780
0
#if defined(TIOCPKT_NOSTOP)
1781
0
    case TIOCPKT_NOSTOP:
1782
0
      return "TIOCPKT_NOSTOP";
1783
0
#endif
1784
0
#if defined(TIOCPKT_DOSTOP)
1785
0
    case TIOCPKT_DOSTOP:
1786
0
      return "TIOCPKT_DOSTOP";
1787
0
#endif
1788
0
#if defined(TIOCPKT_IOCTL)
1789
0
    case TIOCPKT_IOCTL:
1790
0
      return "TIOCPKT_IOCTL";
1791
0
#endif
1792
0
#endif
1793
0
    default:
1794
0
      return "UNKNOWN";
1795
0
  }
1796
0
}
1797
1798
static BOOL CommStatusErrorEx(WINPR_COMM* pComm, unsigned long int ctl, const char* file,
1799
                              const char* fkt, size_t line)
1800
0
{
1801
0
  WINPR_ASSERT(pComm);
1802
0
  BOOL rc = (pComm->permissive);
1803
0
  const DWORD level = rc ? WLOG_DEBUG : WLOG_WARN;
1804
0
  char ebuffer[256] = WINPR_C_ARRAY_INIT;
1805
0
  const char* str = CommIoCtlToStr(ctl);
1806
1807
0
  if (CommInitialized())
1808
0
  {
1809
0
    if (WLog_IsLevelActive(sLog, level))
1810
0
    {
1811
0
      WLog_PrintTextMessage(sLog, level, line, file, fkt,
1812
0
                            "%s [0x%08lx] ioctl failed, errno=[%d] %s.", str, ctl, errno,
1813
0
                            winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
1814
0
    }
1815
0
  }
1816
1817
0
  if (!rc)
1818
0
    SetLastError(ERROR_IO_DEVICE);
1819
1820
0
  return rc;
1821
0
}
1822
1823
BOOL CommIoCtl_int(WINPR_COMM* pComm, unsigned long int ctl, void* data, const char* file,
1824
                   const char* fkt, size_t line)
1825
0
{
1826
0
  if (ioctl(pComm->fd, ctl, data) < 0)
1827
0
  {
1828
0
    if (!CommStatusErrorEx(pComm, ctl, file, fkt, line))
1829
0
      return FALSE;
1830
0
  }
1831
0
  return TRUE;
1832
0
}
1833
1834
BOOL CommUpdateIOCount(WINPR_ATTR_UNUSED HANDLE handle, WINPR_ATTR_UNUSED BOOL checkSupportStatus)
1835
0
{
1836
0
  WINPR_COMM* pComm = (WINPR_COMM*)handle;
1837
0
  WINPR_ASSERT(pComm);
1838
1839
0
#if defined(WINPR_HAVE_COMM_COUNTERS)
1840
0
  ZeroMemory(&(pComm->counters), sizeof(struct serial_icounter_struct));
1841
0
  if (pComm->TIOCGICOUNTSupported || checkSupportStatus)
1842
0
  {
1843
0
    const int rc = ioctl(pComm->fd, TIOCGICOUNT, &(pComm->counters));
1844
0
    if (checkSupportStatus)
1845
0
      pComm->TIOCGICOUNTSupported = rc >= 0;
1846
0
    else if (rc < 0)
1847
0
    {
1848
0
      if (!CommStatusErrorEx(pComm, TIOCGICOUNT, __FILE__, __func__, __LINE__))
1849
0
        return FALSE;
1850
0
    }
1851
0
  }
1852
0
#endif
1853
0
  return TRUE;
1854
0
}
1855
1856
static const char* CommSerialEvFlagString(ULONG flag)
1857
0
{
1858
0
  switch (flag)
1859
0
  {
1860
0
    case SERIAL_EV_RXCHAR:
1861
0
      return "SERIAL_EV_RXCHAR";
1862
0
    case SERIAL_EV_RXFLAG:
1863
0
      return "SERIAL_EV_RXFLAG";
1864
0
    case SERIAL_EV_TXEMPTY:
1865
0
      return "SERIAL_EV_TXEMPTY";
1866
0
    case SERIAL_EV_CTS:
1867
0
      return "SERIAL_EV_CTS ";
1868
0
    case SERIAL_EV_DSR:
1869
0
      return "SERIAL_EV_DSR ";
1870
0
    case SERIAL_EV_RLSD:
1871
0
      return "SERIAL_EV_RLSD";
1872
0
    case SERIAL_EV_BREAK:
1873
0
      return "SERIAL_EV_BREAK";
1874
0
    case SERIAL_EV_ERR:
1875
0
      return "SERIAL_EV_ERR ";
1876
0
    case SERIAL_EV_RING:
1877
0
      return "SERIAL_EV_RING";
1878
0
    case SERIAL_EV_PERR:
1879
0
      return "SERIAL_EV_PERR";
1880
0
    case SERIAL_EV_RX80FULL:
1881
0
      return "SERIAL_EV_RX80FULL";
1882
0
    case SERIAL_EV_EVENT1:
1883
0
      return "SERIAL_EV_EVENT1";
1884
0
    case SERIAL_EV_EVENT2:
1885
0
      return "SERIAL_EV_EVENT2";
1886
0
    case SERIAL_EV_WINPR_WAITING:
1887
0
      return "SERIAL_EV_WINPR_WAITING";
1888
0
    case SERIAL_EV_WINPR_STOP:
1889
0
      return "SERIAL_EV_WINPR_STOP";
1890
0
    default:
1891
0
      return "SERIAL_EV_UNKNOWN";
1892
0
  }
1893
0
}
1894
1895
const char* CommSerialEvString(ULONG status, char* buffer, size_t size)
1896
0
{
1897
0
  const ULONG flags[] = { SERIAL_EV_RXCHAR, SERIAL_EV_RXFLAG,        SERIAL_EV_TXEMPTY,
1898
0
                        SERIAL_EV_CTS,    SERIAL_EV_DSR,           SERIAL_EV_RLSD,
1899
0
                        SERIAL_EV_BREAK,  SERIAL_EV_ERR,           SERIAL_EV_RING,
1900
0
                        SERIAL_EV_PERR,   SERIAL_EV_RX80FULL,      SERIAL_EV_EVENT1,
1901
0
                        SERIAL_EV_EVENT2, SERIAL_EV_WINPR_WAITING, SERIAL_EV_WINPR_STOP };
1902
1903
0
  winpr_str_append("{", buffer, size, "");
1904
1905
0
  const char* sep = "";
1906
0
  for (size_t x = 0; x < ARRAYSIZE(flags); x++)
1907
0
  {
1908
0
    const ULONG flag = flags[x];
1909
0
    if (status & flag)
1910
0
    {
1911
0
      winpr_str_append(CommSerialEvFlagString(flag), buffer, size, sep);
1912
0
      sep = "|";
1913
0
    }
1914
0
  }
1915
1916
0
  char number[32] = WINPR_C_ARRAY_INIT;
1917
0
  (void)_snprintf(number, sizeof(number), "}[0x%08" PRIx32 "]", status);
1918
0
  winpr_str_append(number, buffer, size, "");
1919
0
  return buffer;
1920
0
}