2014-05-12 19:33:56 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Serial Communication API
|
|
|
|
*
|
|
|
|
* Copyright 2014 Hewlett-Packard Development Company, L.P.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-04 19:40:06 +04:00
|
|
|
#if defined __linux__ && !defined ANDROID
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-28 18:41:24 +04:00
|
|
|
#include <assert.h>
|
2014-05-12 19:33:56 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <winpr/io.h>
|
2014-07-02 17:59:16 +04:00
|
|
|
#include <winpr/wlog.h>
|
2014-05-12 19:33:56 +04:00
|
|
|
#include <winpr/wtypes.h>
|
|
|
|
|
|
|
|
#include "comm.h"
|
|
|
|
|
2014-05-19 18:53:57 +04:00
|
|
|
BOOL _comm_set_permissive(HANDLE hDevice, BOOL permissive)
|
|
|
|
{
|
|
|
|
WINPR_COMM* pComm = (WINPR_COMM*) hDevice;
|
|
|
|
|
|
|
|
if (hDevice == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2016-10-07 15:04:40 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-05-19 18:53:57 +04:00
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
if (!pComm || pComm->Type != HANDLE_TYPE_COMM)
|
2014-05-19 18:53:57 +04:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
pComm->permissive = permissive;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-03 23:16:29 +03:00
|
|
|
/* Computes VTIME in deciseconds from Ti in milliseconds */
|
2014-05-29 00:11:19 +04:00
|
|
|
static UCHAR _vtime(ULONG Ti)
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
|
|
|
/* FIXME: look for an equivalent math function otherwise let
|
|
|
|
* do the compiler do the optimization */
|
2014-05-29 00:11:19 +04:00
|
|
|
if (Ti == 0)
|
2014-05-12 19:33:56 +04:00
|
|
|
return 0;
|
2014-05-29 00:11:19 +04:00
|
|
|
else if (Ti < 100)
|
2014-05-12 19:33:56 +04:00
|
|
|
return 1;
|
2014-05-29 00:11:19 +04:00
|
|
|
else if (Ti > 25500)
|
2014-05-12 19:33:56 +04:00
|
|
|
return 255; /* 0xFF */
|
|
|
|
else
|
2016-10-07 15:04:40 +03:00
|
|
|
return Ti / 100;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ERRORS:
|
|
|
|
* ERROR_INVALID_HANDLE
|
|
|
|
* ERROR_NOT_SUPPORTED
|
|
|
|
* ERROR_INVALID_PARAMETER
|
|
|
|
* ERROR_TIMEOUT
|
|
|
|
* ERROR_IO_DEVICE
|
|
|
|
* ERROR_BAD_DEVICE
|
|
|
|
*/
|
|
|
|
BOOL CommReadFile(HANDLE hDevice, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
|
2016-10-07 15:04:40 +03:00
|
|
|
LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
|
|
|
WINPR_COMM* pComm = (WINPR_COMM*) hDevice;
|
2014-05-28 20:42:23 +04:00
|
|
|
int biggestFd = -1;
|
|
|
|
fd_set read_set;
|
|
|
|
int nbFds;
|
2016-10-07 15:04:40 +03:00
|
|
|
COMMTIMEOUTS* pTimeouts;
|
2014-05-12 19:33:56 +04:00
|
|
|
UCHAR vmin = 0;
|
|
|
|
UCHAR vtime = 0;
|
2014-05-29 00:11:19 +04:00
|
|
|
ULONGLONG Tmax = 0;
|
2015-03-09 22:01:51 +03:00
|
|
|
struct timeval tmaxTimeout, *pTmaxTimeout;
|
2014-05-12 19:33:56 +04:00
|
|
|
struct termios currentTermios;
|
2014-06-17 18:34:20 +04:00
|
|
|
EnterCriticalSection(&pComm->ReadLock); /* KISSer by the function's beginning */
|
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
if (hDevice == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2016-10-07 15:04:40 +03:00
|
|
|
goto return_false;
|
|
|
|
}
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
if (!pComm || pComm->Type != HANDLE_TYPE_COMM)
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lpOverlapped != NULL)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_SUPPORTED);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lpNumberOfBytesRead == NULL)
|
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
SetLastError(
|
|
|
|
ERROR_INVALID_PARAMETER); /* since we doesn't suppport lpOverlapped != NULL */
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*lpNumberOfBytesRead = 0; /* will be ajusted if required ... */
|
|
|
|
|
|
|
|
if (nNumberOfBytesToRead <= 0) /* N */
|
|
|
|
{
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_true; /* FIXME: or FALSE? */
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tcgetattr(pComm->fd, ¤tTermios) < 0)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_IO_DEVICE);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (currentTermios.c_lflag & ICANON)
|
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
CommLog_Print(WLOG_WARN,
|
|
|
|
"Canonical mode not supported"); /* the timeout could not be set */
|
2014-05-12 19:33:56 +04:00
|
|
|
SetLastError(ERROR_NOT_SUPPORTED);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* http://msdn.microsoft.com/en-us/library/hh439614%28v=vs.85%29.aspx
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/hardware/hh439614%28v=vs.85%29.aspx
|
|
|
|
*
|
2014-05-29 00:11:19 +04:00
|
|
|
* ReadIntervalTimeout | ReadTotalTimeoutMultiplier | ReadTotalTimeoutConstant | VMIN | VTIME | TMAX |
|
2015-03-09 22:01:51 +03:00
|
|
|
* 0 | 0 | 0 | N | 0 | INDEF | Blocks for N bytes available.
|
2016-10-07 15:04:40 +03:00
|
|
|
* 0< Ti <MAXULONG | 0 | 0 | N | Ti | INDEF | Blocks on first byte, then use Ti between bytes.
|
2014-05-29 00:11:19 +04:00
|
|
|
* MAXULONG | 0 | 0 | 0 | 0 | 0 | Returns immediately with bytes available (don't block)
|
2014-06-16 21:18:45 +04:00
|
|
|
* MAXULONG | MAXULONG | 0< Tc <MAXULONG | N | 0 | Tc | Blocks on first byte during Tc or returns immediately whith bytes available
|
2014-05-29 00:11:19 +04:00
|
|
|
* MAXULONG | m | MAXULONG | | Invalid
|
2015-03-09 22:01:51 +03:00
|
|
|
* 0 | m | 0< Tc <MAXULONG | N | 0 | Tmax | Blocks on first byte during Tmax or returns immediately whith bytes available
|
|
|
|
* 0< Ti <MAXULONG | m | 0< Tc <MAXULONG | N | Ti | Tmax | Blocks on first byte, then use Ti between bytes. Tmax is used for the whole system call.
|
2014-05-12 19:33:56 +04:00
|
|
|
*/
|
2014-05-29 00:11:19 +04:00
|
|
|
/* NB: timeouts are in milliseconds, VTIME are in deciseconds and is an unsigned char */
|
|
|
|
/* FIXME: double check whether open(pComm->fd_read_event, O_NONBLOCK) doesn't conflict with above use cases */
|
2014-05-12 19:33:56 +04:00
|
|
|
pTimeouts = &(pComm->timeouts);
|
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
if ((pTimeouts->ReadIntervalTimeout == MAXULONG)
|
|
|
|
&& (pTimeouts->ReadTotalTimeoutConstant == MAXULONG))
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
CommLog_Print(WLOG_WARN,
|
|
|
|
"ReadIntervalTimeout and ReadTotalTimeoutConstant cannot be both set to MAXULONG");
|
2014-05-12 19:33:56 +04:00
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* VMIN */
|
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
if ((pTimeouts->ReadIntervalTimeout == MAXULONG)
|
|
|
|
&& (pTimeouts->ReadTotalTimeoutMultiplier == 0)
|
|
|
|
&& (pTimeouts->ReadTotalTimeoutConstant == 0))
|
2014-05-29 00:11:19 +04:00
|
|
|
{
|
|
|
|
vmin = 0;
|
|
|
|
}
|
|
|
|
else
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
2014-05-29 00:11:19 +04:00
|
|
|
/* N */
|
|
|
|
/* vmin = nNumberOfBytesToRead < 256 ? nNumberOfBytesToRead : 255;*/ /* 0xFF */
|
|
|
|
/* NB: we might wait endlessly with vmin=N, prefer to
|
|
|
|
* force vmin=1 and return with bytes
|
2014-06-16 21:18:45 +04:00
|
|
|
* available. FIXME: is a feature disarded here? */
|
2014-05-29 00:11:19 +04:00
|
|
|
vmin = 1;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
/* VTIME */
|
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
if ((pTimeouts->ReadIntervalTimeout > 0)
|
|
|
|
&& (pTimeouts->ReadIntervalTimeout < MAXULONG))
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
|
|
|
/* Ti */
|
2014-05-29 00:11:19 +04:00
|
|
|
vtime = _vtime(pTimeouts->ReadIntervalTimeout);
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
2014-05-29 00:11:19 +04:00
|
|
|
|
|
|
|
/* TMAX */
|
2016-10-07 15:04:40 +03:00
|
|
|
pTmaxTimeout = &tmaxTimeout;
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
if ((pTimeouts->ReadIntervalTimeout == MAXULONG)
|
|
|
|
&& (pTimeouts->ReadTotalTimeoutMultiplier == MAXULONG))
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
|
|
|
/* Tc */
|
2014-05-29 00:11:19 +04:00
|
|
|
Tmax = pTimeouts->ReadTotalTimeoutConstant;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
2014-05-29 00:11:19 +04:00
|
|
|
else
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
2014-05-29 00:11:19 +04:00
|
|
|
/* Tmax */
|
2016-10-07 15:04:40 +03:00
|
|
|
Tmax = nNumberOfBytesToRead * pTimeouts->ReadTotalTimeoutMultiplier +
|
|
|
|
pTimeouts->ReadTotalTimeoutConstant;
|
2015-03-09 22:01:51 +03:00
|
|
|
|
|
|
|
/* INDEFinitely */
|
2016-10-07 15:04:40 +03:00
|
|
|
if ((Tmax == 0) && (pTimeouts->ReadIntervalTimeout < MAXULONG)
|
|
|
|
&& (pTimeouts->ReadTotalTimeoutMultiplier == 0))
|
2015-03-09 22:01:51 +03:00
|
|
|
pTmaxTimeout = NULL;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
if ((currentTermios.c_cc[VMIN] != vmin)
|
|
|
|
|| (currentTermios.c_cc[VTIME] != vtime))
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
|
|
|
currentTermios.c_cc[VMIN] = vmin;
|
|
|
|
currentTermios.c_cc[VTIME] = vtime;
|
|
|
|
|
|
|
|
if (tcsetattr(pComm->fd, TCSANOW, ¤tTermios) < 0)
|
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
CommLog_Print(WLOG_WARN,
|
2016-12-14 00:47:08 +03:00
|
|
|
"CommReadFile failure, could not apply new timeout values: VMIN=%"PRIu8", VTIME=%"PRIu8"",
|
2016-10-07 15:04:40 +03:00
|
|
|
vmin, vtime);
|
2014-05-12 19:33:56 +04:00
|
|
|
SetLastError(ERROR_IO_DEVICE);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
/* wait indefinitely if pTmaxTimeout is NULL */
|
2015-03-09 22:01:51 +03:00
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
if (pTmaxTimeout != NULL)
|
2014-05-29 00:11:19 +04:00
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
ZeroMemory(pTmaxTimeout, sizeof(struct timeval));
|
2014-05-29 00:11:19 +04:00
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
if (Tmax > 0) /* return immdiately if Tmax == 0 */
|
|
|
|
{
|
|
|
|
pTmaxTimeout->tv_sec = Tmax / 1000; /* s */
|
|
|
|
pTmaxTimeout->tv_usec = (Tmax % 1000) * 1000; /* us */
|
|
|
|
}
|
|
|
|
}
|
2014-05-29 00:11:19 +04:00
|
|
|
|
2014-05-28 22:15:08 +04:00
|
|
|
/* FIXME: had expected eventfd_write() to return EAGAIN when
|
|
|
|
* there is no eventfd_read() but this not the case. */
|
|
|
|
/* discard a possible and no more relevant event */
|
|
|
|
eventfd_read(pComm->fd_read_event, NULL);
|
2014-05-28 20:42:23 +04:00
|
|
|
biggestFd = pComm->fd_read;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
if (pComm->fd_read_event > biggestFd)
|
|
|
|
biggestFd = pComm->fd_read_event;
|
|
|
|
|
|
|
|
FD_ZERO(&read_set);
|
|
|
|
assert(pComm->fd_read_event < FD_SETSIZE);
|
|
|
|
assert(pComm->fd_read < FD_SETSIZE);
|
|
|
|
FD_SET(pComm->fd_read_event, &read_set);
|
|
|
|
FD_SET(pComm->fd_read, &read_set);
|
2016-10-07 15:04:40 +03:00
|
|
|
nbFds = select(biggestFd + 1, &read_set, NULL, NULL, pTmaxTimeout);
|
2014-05-28 20:42:23 +04:00
|
|
|
|
|
|
|
if (nbFds < 0)
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
CommLog_Print(WLOG_WARN, "select() failure, errno=[%d] %s\n", errno,
|
|
|
|
strerror(errno));
|
2014-05-28 20:42:23 +04:00
|
|
|
SetLastError(ERROR_IO_DEVICE);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 20:42:23 +04:00
|
|
|
}
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
if (nbFds == 0)
|
|
|
|
{
|
|
|
|
/* timeout */
|
|
|
|
SetLastError(ERROR_TIMEOUT);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 20:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read_set */
|
|
|
|
|
|
|
|
if (FD_ISSET(pComm->fd_read_event, &read_set))
|
|
|
|
{
|
|
|
|
eventfd_t event = 0;
|
|
|
|
|
2014-05-28 21:10:01 +04:00
|
|
|
if (eventfd_read(pComm->fd_read_event, &event) < 0)
|
2014-05-28 20:42:23 +04:00
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
{
|
|
|
|
assert(FALSE); /* not quite sure this should ever happen */
|
|
|
|
/* keep on */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
CommLog_Print(WLOG_WARN,
|
|
|
|
"unexpected error on reading fd_read_event, errno=[%d] %s\n", errno,
|
|
|
|
strerror(errno));
|
2014-06-17 18:34:20 +04:00
|
|
|
/* FIXME: goto return_false ? */
|
2014-05-28 20:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(errno == EAGAIN);
|
|
|
|
}
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
if (event == FREERDP_PURGE_RXABORT)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_CANCELLED);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 20:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(event == FREERDP_PURGE_RXABORT); /* no other expected event so far */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(pComm->fd_read, &read_set))
|
|
|
|
{
|
2014-05-28 22:15:08 +04:00
|
|
|
ssize_t nbRead = 0;
|
2014-05-28 20:42:23 +04:00
|
|
|
nbRead = read(pComm->fd_read, lpBuffer, nNumberOfBytesToRead);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
if (nbRead < 0)
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
CommLog_Print(WLOG_WARN,
|
2016-12-14 00:47:08 +03:00
|
|
|
"CommReadFile failed, ReadIntervalTimeout=%"PRIu32", ReadTotalTimeoutMultiplier=%"PRIu32", ReadTotalTimeoutConstant=%"PRIu32" VMIN=%u, VTIME=%u",
|
2016-10-07 15:04:40 +03:00
|
|
|
pTimeouts->ReadIntervalTimeout, pTimeouts->ReadTotalTimeoutMultiplier,
|
|
|
|
pTimeouts->ReadTotalTimeoutConstant,
|
|
|
|
currentTermios.c_cc[VMIN], currentTermios.c_cc[VTIME]);
|
|
|
|
CommLog_Print(WLOG_WARN,
|
2016-12-14 00:47:08 +03:00
|
|
|
"CommReadFile failed, nNumberOfBytesToRead=%"PRIu32", errno=[%d] %s",
|
2016-10-07 15:04:40 +03:00
|
|
|
nNumberOfBytesToRead, errno, strerror(errno));
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
if (errno == EAGAIN)
|
|
|
|
{
|
|
|
|
/* keep on */
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_true; /* expect a read-loop to be implemented on the server side */
|
2014-05-28 20:42:23 +04:00
|
|
|
}
|
|
|
|
else if (errno == EBADF)
|
|
|
|
{
|
2014-05-12 19:33:56 +04:00
|
|
|
SetLastError(ERROR_BAD_DEVICE); /* STATUS_INVALID_DEVICE_REQUEST */
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 20:42:23 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(FALSE);
|
|
|
|
SetLastError(ERROR_IO_DEVICE);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 20:42:23 +04:00
|
|
|
}
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
if (nbRead == 0)
|
|
|
|
{
|
|
|
|
/* termios timeout */
|
|
|
|
SetLastError(ERROR_TIMEOUT);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 20:42:23 +04:00
|
|
|
}
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
*lpNumberOfBytesRead = nbRead;
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_true;
|
2014-05-28 20:42:23 +04:00
|
|
|
}
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
assert(FALSE);
|
|
|
|
*lpNumberOfBytesRead = 0;
|
2016-10-07 15:04:40 +03:00
|
|
|
return_false:
|
2014-06-17 18:34:20 +04:00
|
|
|
LeaveCriticalSection(&pComm->ReadLock);
|
2014-05-28 20:42:23 +04:00
|
|
|
return FALSE;
|
2016-10-07 15:04:40 +03:00
|
|
|
return_true:
|
2014-06-17 18:34:20 +04:00
|
|
|
LeaveCriticalSection(&pComm->ReadLock);
|
|
|
|
return TRUE;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ERRORS:
|
|
|
|
* ERROR_INVALID_HANDLE
|
|
|
|
* ERROR_NOT_SUPPORTED
|
|
|
|
* ERROR_INVALID_PARAMETER
|
|
|
|
* ERROR_BAD_DEVICE
|
|
|
|
*/
|
2016-10-07 15:04:40 +03:00
|
|
|
BOOL CommWriteFile(HANDLE hDevice, LPCVOID lpBuffer,
|
|
|
|
DWORD nNumberOfBytesToWrite,
|
|
|
|
LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
|
|
|
WINPR_COMM* pComm = (WINPR_COMM*) hDevice;
|
2015-03-09 22:01:51 +03:00
|
|
|
struct timeval tmaxTimeout, *pTmaxTimeout;
|
2016-10-07 15:04:40 +03:00
|
|
|
EnterCriticalSection(
|
|
|
|
&pComm->WriteLock); /* KISSer by the function's beginning */
|
2014-06-17 18:34:20 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
if (hDevice == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2016-10-07 15:04:40 +03:00
|
|
|
goto return_false;
|
|
|
|
}
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
if (!pComm || pComm->Type != HANDLE_TYPE_COMM)
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lpOverlapped != NULL)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_SUPPORTED);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lpNumberOfBytesWritten == NULL)
|
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
SetLastError(
|
|
|
|
ERROR_INVALID_PARAMETER); /* since we doesn't suppport lpOverlapped != NULL */
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*lpNumberOfBytesWritten = 0; /* will be ajusted if required ... */
|
|
|
|
|
|
|
|
if (nNumberOfBytesToWrite <= 0)
|
|
|
|
{
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_true; /* FIXME: or FALSE? */
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
2014-05-28 22:15:08 +04:00
|
|
|
/* FIXME: had expected eventfd_write() to return EAGAIN when
|
|
|
|
* there is no eventfd_read() but this not the case. */
|
|
|
|
/* discard a possible and no more relevant event */
|
|
|
|
eventfd_read(pComm->fd_write_event, NULL);
|
2014-05-28 18:41:24 +04:00
|
|
|
/* ms */
|
2016-10-07 15:04:40 +03:00
|
|
|
ULONGLONG Tmax = nNumberOfBytesToWrite *
|
|
|
|
pComm->timeouts.WriteTotalTimeoutMultiplier +
|
|
|
|
pComm->timeouts.WriteTotalTimeoutConstant;
|
2014-05-28 18:41:24 +04:00
|
|
|
/* NB: select() may update the timeout argument to indicate
|
|
|
|
* how much time was left. Keep the timeout variable out of
|
|
|
|
* the while() */
|
2016-10-07 15:04:40 +03:00
|
|
|
pTmaxTimeout = &tmaxTimeout;
|
2015-03-09 22:01:51 +03:00
|
|
|
ZeroMemory(pTmaxTimeout, sizeof(struct timeval));
|
2016-10-07 15:04:40 +03:00
|
|
|
|
|
|
|
if (Tmax > 0)
|
2015-03-09 22:01:51 +03:00
|
|
|
{
|
|
|
|
pTmaxTimeout->tv_sec = Tmax / 1000; /* s */
|
|
|
|
pTmaxTimeout->tv_usec = (Tmax % 1000) * 1000; /* us */
|
|
|
|
}
|
2016-10-07 15:04:40 +03:00
|
|
|
else if ((pComm->timeouts.WriteTotalTimeoutMultiplier == 0)
|
|
|
|
&& (pComm->timeouts.WriteTotalTimeoutConstant == 0))
|
2014-05-28 18:41:24 +04:00
|
|
|
{
|
2015-03-09 22:01:51 +03:00
|
|
|
pTmaxTimeout = NULL;
|
2014-05-28 18:41:24 +04:00
|
|
|
}
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-03-09 22:01:51 +03:00
|
|
|
/* else return immdiately */
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
while (*lpNumberOfBytesWritten < nNumberOfBytesToWrite)
|
|
|
|
{
|
2014-05-28 18:41:24 +04:00
|
|
|
int biggestFd = -1;
|
|
|
|
fd_set event_set, write_set;
|
2014-05-28 19:18:33 +04:00
|
|
|
int nbFds;
|
2014-05-28 18:41:24 +04:00
|
|
|
biggestFd = pComm->fd_write;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-28 18:41:24 +04:00
|
|
|
if (pComm->fd_write_event > biggestFd)
|
|
|
|
biggestFd = pComm->fd_write_event;
|
|
|
|
|
|
|
|
FD_ZERO(&event_set);
|
|
|
|
FD_ZERO(&write_set);
|
|
|
|
assert(pComm->fd_write_event < FD_SETSIZE);
|
|
|
|
assert(pComm->fd_write < FD_SETSIZE);
|
|
|
|
FD_SET(pComm->fd_write_event, &event_set);
|
|
|
|
FD_SET(pComm->fd_write, &write_set);
|
2016-10-07 15:04:40 +03:00
|
|
|
nbFds = select(biggestFd + 1, &event_set, &write_set, NULL, pTmaxTimeout);
|
2014-05-28 18:41:24 +04:00
|
|
|
|
2014-05-28 19:18:33 +04:00
|
|
|
if (nbFds < 0)
|
2014-05-28 18:41:24 +04:00
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
CommLog_Print(WLOG_WARN, "select() failure, errno=[%d] %s\n", errno,
|
|
|
|
strerror(errno));
|
2014-05-28 18:41:24 +04:00
|
|
|
SetLastError(ERROR_IO_DEVICE);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 18:41:24 +04:00
|
|
|
}
|
|
|
|
|
2014-05-28 19:18:33 +04:00
|
|
|
if (nbFds == 0)
|
|
|
|
{
|
|
|
|
/* timeout */
|
|
|
|
SetLastError(ERROR_TIMEOUT);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 19:18:33 +04:00
|
|
|
}
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-28 18:41:24 +04:00
|
|
|
/* event_set */
|
|
|
|
|
|
|
|
if (FD_ISSET(pComm->fd_write_event, &event_set))
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
2014-05-28 18:41:24 +04:00
|
|
|
eventfd_t event = 0;
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-28 21:10:01 +04:00
|
|
|
if (eventfd_read(pComm->fd_write_event, &event) < 0)
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
2014-05-28 18:41:24 +04:00
|
|
|
if (errno == EAGAIN)
|
|
|
|
{
|
|
|
|
assert(FALSE); /* not quite sure this should ever happen */
|
2014-05-28 20:42:23 +04:00
|
|
|
/* keep on */
|
2014-05-28 18:41:24 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
CommLog_Print(WLOG_WARN,
|
|
|
|
"unexpected error on reading fd_write_event, errno=[%d] %s\n", errno,
|
|
|
|
strerror(errno));
|
2014-06-17 18:34:20 +04:00
|
|
|
/* FIXME: goto return_false ? */
|
2014-05-28 18:41:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(errno == EAGAIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event == FREERDP_PURGE_TXABORT)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_CANCELLED);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 18:41:24 +04:00
|
|
|
}
|
|
|
|
|
2014-05-28 20:42:23 +04:00
|
|
|
assert(event == FREERDP_PURGE_TXABORT); /* no other expected event so far */
|
2014-05-28 18:41:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* write_set */
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-28 18:41:24 +04:00
|
|
|
if (FD_ISSET(pComm->fd_write, &write_set))
|
|
|
|
{
|
|
|
|
ssize_t nbWritten;
|
2014-06-16 21:18:45 +04:00
|
|
|
nbWritten = write(pComm->fd_write,
|
2016-10-07 15:04:40 +03:00
|
|
|
((BYTE*)lpBuffer) + (*lpNumberOfBytesWritten),
|
|
|
|
nNumberOfBytesToWrite - (*lpNumberOfBytesWritten));
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-28 18:41:24 +04:00
|
|
|
if (nbWritten < 0)
|
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
CommLog_Print(WLOG_WARN,
|
2016-12-14 00:47:08 +03:00
|
|
|
"CommWriteFile failed after %"PRIu32" bytes written, errno=[%d] %s\n",
|
2016-10-07 15:04:40 +03:00
|
|
|
*lpNumberOfBytesWritten, errno, strerror(errno));
|
2014-05-28 18:41:24 +04:00
|
|
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
{
|
2014-05-28 19:18:33 +04:00
|
|
|
/* keep on */
|
|
|
|
continue;
|
2014-05-28 18:41:24 +04:00
|
|
|
}
|
|
|
|
else if (errno == EBADF)
|
|
|
|
{
|
2014-05-12 19:33:56 +04:00
|
|
|
SetLastError(ERROR_BAD_DEVICE); /* STATUS_INVALID_DEVICE_REQUEST */
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 18:41:24 +04:00
|
|
|
}
|
2014-05-28 20:42:23 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(FALSE);
|
|
|
|
SetLastError(ERROR_IO_DEVICE);
|
2014-06-17 18:34:20 +04:00
|
|
|
goto return_false;
|
2014-05-28 20:42:23 +04:00
|
|
|
}
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-28 18:41:24 +04:00
|
|
|
*lpNumberOfBytesWritten += nbWritten;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
2014-05-28 18:41:24 +04:00
|
|
|
} /* while */
|
2014-05-23 14:27:09 +04:00
|
|
|
|
|
|
|
/* FIXME: this call to tcdrain() doesn't look correct and
|
|
|
|
* might hide a bug but was required while testing a serial
|
|
|
|
* printer. Its driver was expecting the modem line status
|
|
|
|
* SERIAL_MSR_DSR true after the sending which was never
|
|
|
|
* happenning otherwise. A purge was also done before each
|
|
|
|
* Write operation. The serial port was oppened with:
|
|
|
|
* DesiredAccess=0x0012019F. The printer worked fine with
|
|
|
|
* mstsc. */
|
2014-05-28 20:42:23 +04:00
|
|
|
tcdrain(pComm->fd_write);
|
2016-10-07 15:04:40 +03:00
|
|
|
return_true:
|
2014-06-17 18:34:20 +04:00
|
|
|
LeaveCriticalSection(&pComm->WriteLock);
|
2014-05-12 19:33:56 +04:00
|
|
|
return TRUE;
|
2016-10-07 15:04:40 +03:00
|
|
|
return_false:
|
2014-06-17 18:34:20 +04:00
|
|
|
LeaveCriticalSection(&pComm->WriteLock);
|
|
|
|
return FALSE;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
|
|
|
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-07-04 18:16:26 +04:00
|
|
|
#endif /* __linux__ */
|