20#include <winpr/config.h>
22#include <winpr/assert.h>
28#include <winpr/wlog.h>
29#include <winpr/wtypes.h>
33BOOL _comm_set_permissive(HANDLE hDevice, BOOL permissive)
35 WINPR_COMM* pComm = (WINPR_COMM*)hDevice;
37 if (!CommIsHandled(hDevice))
40 pComm->permissive = permissive;
45static UCHAR svtime(ULONG Ti)
56 return (UCHAR)(Ti / 100);
68BOOL CommReadFile(HANDLE hDevice, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
69 LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
71 WINPR_COMM* pComm = (WINPR_COMM*)hDevice;
79 struct timeval tmaxTimeout;
80 struct timeval* pTmaxTimeout =
nullptr;
81 struct termios currentTermios;
83 if (!CommIsHandled(hDevice))
86 EnterCriticalSection(&pComm->ReadLock);
88 if (lpOverlapped !=
nullptr)
90 SetLastError(ERROR_NOT_SUPPORTED);
94 if (lpNumberOfBytesRead ==
nullptr)
97 ERROR_INVALID_PARAMETER);
101 *lpNumberOfBytesRead = 0;
103 if (nNumberOfBytesToRead <= 0)
108 if (tcgetattr(pComm->fd, ¤tTermios) < 0)
110 SetLastError(ERROR_IO_DEVICE);
114 if (currentTermios.c_lflag & ICANON)
116 CommLog_Print(WLOG_WARN,
"Canonical mode not supported");
117 SetLastError(ERROR_NOT_SUPPORTED);
140 pTimeouts = &(pComm->timeouts);
142 if ((pTimeouts->ReadIntervalTimeout == MAXULONG) &&
143 (pTimeouts->ReadTotalTimeoutConstant == MAXULONG))
147 "ReadIntervalTimeout and ReadTotalTimeoutConstant cannot be both set to MAXULONG");
148 SetLastError(ERROR_INVALID_PARAMETER);
154 if ((pTimeouts->ReadIntervalTimeout == MAXULONG) &&
155 (pTimeouts->ReadTotalTimeoutMultiplier == 0) && (pTimeouts->ReadTotalTimeoutConstant == 0))
171 if ((pTimeouts->ReadIntervalTimeout > 0) && (pTimeouts->ReadIntervalTimeout < MAXULONG))
174 vtime = svtime(pTimeouts->ReadIntervalTimeout);
178 pTmaxTimeout = &tmaxTimeout;
180 if ((pTimeouts->ReadIntervalTimeout == MAXULONG) &&
181 (pTimeouts->ReadTotalTimeoutMultiplier == MAXULONG))
184 Tmax = pTimeouts->ReadTotalTimeoutConstant;
189 Tmax = 1ll * nNumberOfBytesToRead * pTimeouts->ReadTotalTimeoutMultiplier +
190 1ll * pTimeouts->ReadTotalTimeoutConstant;
193 if ((Tmax == 0) && (pTimeouts->ReadIntervalTimeout < MAXULONG) &&
194 (pTimeouts->ReadTotalTimeoutMultiplier == 0))
195 pTmaxTimeout =
nullptr;
198 if ((currentTermios.c_cc[VMIN] != vmin) || (currentTermios.c_cc[VTIME] != vtime))
200 currentTermios.c_cc[VMIN] = vmin;
201 currentTermios.c_cc[VTIME] = vtime;
203 if (tcsetattr(pComm->fd, TCSANOW, ¤tTermios) < 0)
205 CommLog_Print(WLOG_WARN,
206 "CommReadFile failure, could not apply new timeout values: VMIN=%" PRIu8
207 ", VTIME=%" PRIu8
"",
209 SetLastError(ERROR_IO_DEVICE);
216 if (pTmaxTimeout !=
nullptr)
218 ZeroMemory(pTmaxTimeout,
sizeof(
struct timeval));
222 pTmaxTimeout->tv_sec = Tmax / 1000;
223 pTmaxTimeout->tv_usec = (Tmax % 1000) * 1000;
230#if defined(WINPR_HAVE_SYS_EVENTFD_H)
233 (void)eventfd_read(pComm->fd_read_event, &val);
236 biggestFd = pComm->fd_read;
238 if (pComm->fd_read_event > biggestFd)
239 biggestFd = pComm->fd_read_event;
243 if ((pComm->fd_read_event >= FD_SETSIZE) || (pComm->fd_read >= FD_SETSIZE))
245 CommLog_Print(WLOG_WARN,
"CommReadFile failed, descriptor above FD_SETSIZE=%d", FD_SETSIZE);
246 SetLastError(ERROR_IO_DEVICE);
251 FD_SET(pComm->fd_read_event, &read_set);
252 FD_SET(pComm->fd_read, &read_set);
253 nbFds = select(biggestFd + 1, &read_set,
nullptr,
nullptr, pTmaxTimeout);
257 char ebuffer[256] = WINPR_C_ARRAY_INIT;
258 CommLog_Print(WLOG_WARN,
"select() failure, errno=[%d] %s\n", errno,
259 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
260 SetLastError(ERROR_IO_DEVICE);
267 SetLastError(ERROR_TIMEOUT);
273 if (FD_ISSET(pComm->fd_read_event, &read_set))
275#if defined(WINPR_HAVE_SYS_EVENTFD_H)
278 if (eventfd_read(pComm->fd_read_event, &event) < 0)
287 char ebuffer[256] = WINPR_C_ARRAY_INIT;
288 CommLog_Print(WLOG_WARN,
289 "unexpected error on reading fd_read_event, errno=[%d] %s\n", errno,
290 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
294 WINPR_ASSERT(errno == EAGAIN);
302 SetLastError(ERROR_CANCELLED);
308 if (FD_ISSET(pComm->fd_read, &read_set))
310 ssize_t nbRead = read(pComm->fd_read, lpBuffer, nNumberOfBytesToRead);
312 if ((nbRead < 0) || (nbRead > nNumberOfBytesToRead))
314 char ebuffer[256] = WINPR_C_ARRAY_INIT;
315 CommLog_Print(WLOG_WARN,
316 "CommReadFile failed, ReadIntervalTimeout=%" PRIu32
317 ", ReadTotalTimeoutMultiplier=%" PRIu32
318 ", ReadTotalTimeoutConstant=%" PRIu32
" VMIN=%u, VTIME=%u",
319 pTimeouts->ReadIntervalTimeout, pTimeouts->ReadTotalTimeoutMultiplier,
320 pTimeouts->ReadTotalTimeoutConstant, currentTermios.c_cc[VMIN],
321 currentTermios.c_cc[VTIME]);
323 WLOG_WARN,
"CommReadFile failed, nNumberOfBytesToRead=%" PRIu32
", errno=[%d] %s",
324 nNumberOfBytesToRead, errno, winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
331 else if (errno == EBADF)
333 SetLastError(ERROR_BAD_DEVICE);
339 SetLastError(ERROR_IO_DEVICE);
347 SetLastError(ERROR_TIMEOUT);
351 *lpNumberOfBytesRead = WINPR_ASSERTING_INT_CAST(UINT32, nbRead);
353 EnterCriticalSection(&pComm->EventsLock);
354 if (pComm->PendingEvents & SERIAL_EV_WINPR_WAITING)
356 if (pComm->eventChar !=
'\0' &&
357 memchr(lpBuffer, pComm->eventChar, WINPR_ASSERTING_INT_CAST(
size_t, nbRead)))
358 pComm->PendingEvents |= SERIAL_EV_RXCHAR;
360 LeaveCriticalSection(&pComm->EventsLock);
365 *lpNumberOfBytesRead = 0;
367 LeaveCriticalSection(&pComm->ReadLock);
370 LeaveCriticalSection(&pComm->ReadLock);
381BOOL CommWriteFile(HANDLE hDevice, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
382 LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
384 WINPR_COMM* pComm = (WINPR_COMM*)hDevice;
385 struct timeval tmaxTimeout;
386 struct timeval* pTmaxTimeout =
nullptr;
388 if (!CommIsHandled(hDevice))
391 EnterCriticalSection(&pComm->WriteLock);
393 if (lpOverlapped !=
nullptr)
395 SetLastError(ERROR_NOT_SUPPORTED);
399 if (lpNumberOfBytesWritten ==
nullptr)
402 ERROR_INVALID_PARAMETER);
406 *lpNumberOfBytesWritten = 0;
408 if (nNumberOfBytesToWrite <= 0)
417#if defined(WINPR_HAVE_SYS_EVENTFD_H)
420 (void)eventfd_read(pComm->fd_write_event, &val);
426 const LONGLONG Tmax =
427 1ll * nNumberOfBytesToWrite * pComm->timeouts.WriteTotalTimeoutMultiplier +
428 1ll * pComm->timeouts.WriteTotalTimeoutConstant;
432 pTmaxTimeout = &tmaxTimeout;
433 ZeroMemory(pTmaxTimeout,
sizeof(
struct timeval));
437 pTmaxTimeout->tv_sec = Tmax / 1000;
438 pTmaxTimeout->tv_usec = (Tmax % 1000) * 1000;
440 else if ((pComm->timeouts.WriteTotalTimeoutMultiplier == 0) &&
441 (pComm->timeouts.WriteTotalTimeoutConstant == 0))
443 pTmaxTimeout =
nullptr;
449 while (*lpNumberOfBytesWritten < nNumberOfBytesToWrite)
455 biggestFd = pComm->fd_write;
457 if (pComm->fd_write_event > biggestFd)
458 biggestFd = pComm->fd_write_event;
461 if ((pComm->fd_write_event >= FD_SETSIZE) || (pComm->fd_write >= FD_SETSIZE))
463 CommLog_Print(WLOG_WARN,
"CommWriteFile failed, descriptor above FD_SETSIZE=%d",
465 SetLastError(ERROR_IO_DEVICE);
471 FD_SET(pComm->fd_write_event, &event_set);
472 FD_SET(pComm->fd_write, &write_set);
473 nbFds = select(biggestFd + 1, &event_set, &write_set,
nullptr, pTmaxTimeout);
477 char ebuffer[256] = WINPR_C_ARRAY_INIT;
478 CommLog_Print(WLOG_WARN,
"select() failure, errno=[%d] %s\n", errno,
479 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
480 SetLastError(ERROR_IO_DEVICE);
487 SetLastError(ERROR_TIMEOUT);
493 if (FD_ISSET(pComm->fd_write_event, &event_set))
495#if defined(WINPR_HAVE_SYS_EVENTFD_H)
498 if (eventfd_read(pComm->fd_write_event, &event) < 0)
507 char ebuffer[256] = WINPR_C_ARRAY_INIT;
508 CommLog_Print(WLOG_WARN,
509 "unexpected error on reading fd_write_event, errno=[%d] %s\n",
510 errno, winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
514 WINPR_ASSERT(errno == EAGAIN);
522 SetLastError(ERROR_CANCELLED);
530 if (FD_ISSET(pComm->fd_write, &write_set))
532 ssize_t nbWritten = 0;
533 const BYTE* ptr = lpBuffer;
534 nbWritten = write(pComm->fd_write, &ptr[*lpNumberOfBytesWritten],
535 nNumberOfBytesToWrite - (*lpNumberOfBytesWritten));
539 char ebuffer[256] = WINPR_C_ARRAY_INIT;
540 CommLog_Print(WLOG_WARN,
541 "CommWriteFile failed after %" PRIu32
542 " bytes written, errno=[%d] %s\n",
543 *lpNumberOfBytesWritten, errno,
544 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
551 else if (errno == EBADF)
553 SetLastError(ERROR_BAD_DEVICE);
559 SetLastError(ERROR_IO_DEVICE);
564 *lpNumberOfBytesWritten += nbWritten;
576 tcdrain(pComm->fd_write);
579 LeaveCriticalSection(&pComm->WriteLock);
583 LeaveCriticalSection(&pComm->WriteLock);