From 096159054be7e44b5ae345e15a71e857943fd9fe Mon Sep 17 00:00:00 2001 From: Emmanuel Ledoux Date: Thu, 13 Nov 2014 19:54:32 +0100 Subject: [PATCH] winpr-comm: ignore errors on TIOCGICOUNT during the port initialization but the permissive mode must be on afterwards. --- channels/serial/client/serial_main.c | 2 +- winpr/libwinpr/comm/comm.c | 14 +++++++--- winpr/libwinpr/comm/comm_serial_sys.c | 39 ++++++++++++++++++++------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/channels/serial/client/serial_main.c b/channels/serial/client/serial_main.c index 17d5692d7..ff611abf3 100644 --- a/channels/serial/client/serial_main.c +++ b/channels/serial/client/serial_main.c @@ -175,7 +175,7 @@ static void serial_process_irp_create(SERIAL_DEVICE* serial, IRP* irp) if (!serial->hComm || (serial->hComm == INVALID_HANDLE_VALUE)) { - WLog_Print(serial->log, WLOG_WARN, "CreateFile failure: %s last-error: Ox%lX\n", serial->device.name, GetLastError()); + WLog_Print(serial->log, WLOG_WARN, "CreateFile failure: %s last-error: 0x%lX\n", serial->device.name, GetLastError()); irp->IoStatus = STATUS_UNSUCCESSFUL; goto error_handle; diff --git a/winpr/libwinpr/comm/comm.c b/winpr/libwinpr/comm/comm.c index 5aa213894..60ad6d8bd 100644 --- a/winpr/libwinpr/comm/comm.c +++ b/winpr/libwinpr/comm/comm.c @@ -1373,9 +1373,17 @@ HANDLE CommCreateFileA(LPCSTR lpDeviceName, DWORD dwDesiredAccess, DWORD dwShare if (ioctl(pComm->fd, TIOCGICOUNT, &(pComm->counters)) < 0) { - CommLog_Print(WLOG_WARN, "TIOCGICOUNT ioctl failed, errno=[%d] %s", errno, strerror(errno)); - SetLastError(ERROR_IO_DEVICE); - goto error_handle; + CommLog_Print(WLOG_WARN, "TIOCGICOUNT ioctl failed, errno=[%d] %s.", errno, strerror(errno)); + CommLog_Print(WLOG_WARN, "could not read counters."); + + /* could not initialize counters but keep on. + * + * Not all drivers, especially for USB to serial + * adapters (e.g. those based on pl2303), does support + * this call. + */ + + ZeroMemory(&(pComm->counters), sizeof(struct serial_icounter_struct)); } diff --git a/winpr/libwinpr/comm/comm_serial_sys.c b/winpr/libwinpr/comm/comm_serial_sys.c index 223bf5c03..830d158d1 100644 --- a/winpr/libwinpr/comm/comm_serial_sys.c +++ b/winpr/libwinpr/comm/comm_serial_sys.c @@ -1042,11 +1042,19 @@ static BOOL _set_wait_mask(WINPR_COMM *pComm, const ULONG *pWaitMask) if (ioctl(pComm->fd, TIOCGICOUNT, &(pComm->counters)) < 0) { - CommLog_Print(WLOG_WARN, "TIOCGICOUNT ioctl failed, errno=[%d] %s", errno, strerror(errno)); - SetLastError(ERROR_IO_DEVICE); - - LeaveCriticalSection(&pComm->EventsLock); - return FALSE; + CommLog_Print(WLOG_WARN, "TIOCGICOUNT ioctl failed, errno=[%d] %s.", errno, strerror(errno)); + + if (pComm->permissive) + { + /* counters could not be reset but keep on */ + ZeroMemory(&(pComm->counters), sizeof(struct serial_icounter_struct)); + } + else + { + SetLastError(ERROR_IO_DEVICE); + LeaveCriticalSection(&pComm->EventsLock); + return FALSE; + } } pComm->PendingEvents = 0; @@ -1188,11 +1196,22 @@ static BOOL _get_commstatus(WINPR_COMM *pComm, SERIAL_STATUS *pCommstatus) ZeroMemory(¤tCounters, sizeof(struct serial_icounter_struct)); if (ioctl(pComm->fd, TIOCGICOUNT, ¤tCounters) < 0) { - CommLog_Print(WLOG_WARN, "TIOCGICOUNT ioctl failed, errno=[%d] %s", errno, strerror(errno)); - SetLastError(ERROR_IO_DEVICE); - - LeaveCriticalSection(&pComm->EventsLock); - return FALSE; + CommLog_Print(WLOG_WARN, "TIOCGICOUNT ioctl failed, errno=[%d] %s.", errno, strerror(errno)); + CommLog_Print(WLOG_WARN, " coult not read counters."); + + if (pComm->permissive) + { + /* Errors and events based on counters could not be + * detected but keep on. + */ + ZeroMemory(¤tCounters, sizeof(struct serial_icounter_struct)); + } + else + { + SetLastError(ERROR_IO_DEVICE); + LeaveCriticalSection(&pComm->EventsLock); + return FALSE; + } } /* NB: preferred below (currentCounters.* != pComm->counters.*) over (currentCounters.* > pComm->counters.*) thinking the counters can loop */