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);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-29 00:11:19 +04:00
|
|
|
/* Computes VMIN in deciseconds from Ti in milliseconds */
|
|
|
|
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
|
2014-05-29 00:11:19 +04: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,
|
2014-06-16 21:18:45 +04: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;
|
2014-05-12 19:33:56 +04:00
|
|
|
COMMTIMEOUTS *pTimeouts;
|
|
|
|
UCHAR vmin = 0;
|
|
|
|
UCHAR vtime = 0;
|
2014-05-29 00:11:19 +04:00
|
|
|
ULONGLONG Tmax = 0;
|
|
|
|
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);
|
2014-06-17 18:34:20 +04: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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2014-07-02 17:59:16 +04: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 |
|
|
|
|
* 0 | 0 | 0 | N | 0 | 0 | Blocks for N bytes available.
|
|
|
|
* 0< Ti <MAXULONG | 0 | 0 | N | Ti | 0 | Block on first byte, then use Ti between bytes.
|
|
|
|
* 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
|
2014-06-16 21:18:45 +04:00
|
|
|
* 0 | m | 0< Tc <MAXULONG | N | 0 | Tmax | Block on first byte during Tmax or returns immediately whith bytes available
|
2014-05-29 00:11:19 +04:00
|
|
|
* 0< Ti <MAXULONG | m | 0< Tc <MAXULONG | N | Ti | Tmax | Block on first byte, then use Ti between bytes. Tmax is use 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 */
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-29 00:11:19 +04:00
|
|
|
/* 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);
|
|
|
|
|
|
|
|
if ((pTimeouts->ReadIntervalTimeout == MAXULONG) && (pTimeouts->ReadTotalTimeoutConstant == MAXULONG))
|
|
|
|
{
|
2014-07-02 17:59:16 +04: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 */
|
|
|
|
|
2014-05-29 00:11:19 +04:00
|
|
|
if ((pTimeouts->ReadIntervalTimeout == MAXULONG) && (pTimeouts->ReadTotalTimeoutMultiplier == 0) && (pTimeouts->ReadTotalTimeoutConstant == 0))
|
|
|
|
{
|
|
|
|
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 */
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-29 00:11:19 +04:00
|
|
|
/* 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 */
|
|
|
|
|
2014-05-29 00:11:19 +04: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 */
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-29 00:11:19 +04: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 */
|
|
|
|
Tmax = nNumberOfBytesToRead * pTimeouts->ReadTotalTimeoutMultiplier + pTimeouts->ReadTotalTimeoutConstant;
|
2014-05-12 19:33:56 +04:00
|
|
|
}
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
if ((currentTermios.c_cc[VMIN] != vmin) || (currentTermios.c_cc[VTIME] != vtime))
|
|
|
|
{
|
|
|
|
currentTermios.c_cc[VMIN] = vmin;
|
|
|
|
currentTermios.c_cc[VTIME] = vtime;
|
|
|
|
|
|
|
|
if (tcsetattr(pComm->fd, TCSANOW, ¤tTermios) < 0)
|
|
|
|
{
|
2014-07-02 17:59:16 +04:00
|
|
|
CommLog_Print(WLOG_WARN, "CommReadFile failure, could not apply new timeout values: VMIN=%u, VTIME=%u", 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 00:11:19 +04:00
|
|
|
pTmaxTimeout = NULL; /* no timeout if Tmax == 0 */
|
|
|
|
if (Tmax > 0)
|
|
|
|
{
|
|
|
|
ZeroMemory(&tmaxTimeout, sizeof(struct timeval));
|
|
|
|
|
|
|
|
tmaxTimeout.tv_sec = Tmax / 1000; /* s */
|
|
|
|
tmaxTimeout.tv_usec = (Tmax % 1000) * 1000; /* us */
|
|
|
|
|
|
|
|
pTmaxTimeout = &tmaxTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
|
2014-05-29 00:11:19 +04: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
|
|
|
{
|
2014-07-02 17:59:16 +04: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
|
|
|
|
{
|
2014-07-02 17:59:16 +04: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);
|
|
|
|
if (nbRead < 0)
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
2014-07-02 17:59:16 +04:00
|
|
|
CommLog_Print(WLOG_WARN, "CommReadFile failed, ReadIntervalTimeout=%lu, ReadTotalTimeoutMultiplier=%lu, ReadTotalTimeoutConstant=%lu VMIN=%u, VTIME=%u",
|
2014-06-16 21:18:45 +04:00
|
|
|
pTimeouts->ReadIntervalTimeout, pTimeouts->ReadTotalTimeoutMultiplier, pTimeouts->ReadTotalTimeoutConstant,
|
|
|
|
currentTermios.c_cc[VMIN], currentTermios.c_cc[VTIME]);
|
2014-07-02 17:59:16 +04:00
|
|
|
CommLog_Print(WLOG_WARN, "CommReadFile failed, nNumberOfBytesToRead=%lu, errno=[%d] %s", 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;
|
2014-06-17 18:34:20 +04:00
|
|
|
|
|
|
|
return_false:
|
|
|
|
LeaveCriticalSection(&pComm->ReadLock);
|
2014-05-28 20:42:23 +04:00
|
|
|
return FALSE;
|
2014-06-17 18:34:20 +04:00
|
|
|
|
|
|
|
return_true:
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
BOOL CommWriteFile(HANDLE hDevice, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
|
2014-06-16 21:18:45 +04:00
|
|
|
LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
|
2014-05-12 19:33:56 +04:00
|
|
|
{
|
|
|
|
WINPR_COMM* pComm = (WINPR_COMM*) hDevice;
|
2014-05-29 00:11:19 +04:00
|
|
|
struct timeval tmaxTimeout, *pTmaxTimeout;
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-06-17 18:34:20 +04:00
|
|
|
EnterCriticalSection(&pComm->WriteLock); /* KISSer by the function's beginning */
|
|
|
|
|
2014-05-12 19:33:56 +04:00
|
|
|
if (hDevice == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2014-06-17 18:34:20 +04: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)
|
|
|
|
{
|
|
|
|
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 */
|
|
|
|
ULONGLONG Tmax = nNumberOfBytesToWrite * pComm->timeouts.WriteTotalTimeoutMultiplier + pComm->timeouts.WriteTotalTimeoutConstant;
|
|
|
|
|
|
|
|
/* NB: select() may update the timeout argument to indicate
|
|
|
|
* how much time was left. Keep the timeout variable out of
|
|
|
|
* the while() */
|
|
|
|
|
2014-05-29 00:11:19 +04:00
|
|
|
pTmaxTimeout = NULL; /* no timeout if Tmax == 0 */
|
2014-05-28 18:41:24 +04:00
|
|
|
if (Tmax > 0)
|
|
|
|
{
|
2014-05-29 00:11:19 +04:00
|
|
|
ZeroMemory(&tmaxTimeout, sizeof(struct timeval));
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-29 00:11:19 +04:00
|
|
|
tmaxTimeout.tv_sec = Tmax / 1000; /* s */
|
|
|
|
tmaxTimeout.tv_usec = (Tmax % 1000) * 1000; /* us */
|
2014-05-28 18:41:24 +04:00
|
|
|
|
2014-05-29 00:11:19 +04:00
|
|
|
pTmaxTimeout = &tmaxTimeout;
|
2014-05-28 18:41:24 +04:00
|
|
|
}
|
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;
|
|
|
|
if (pComm->fd_write_event > biggestFd)
|
|
|
|
biggestFd = pComm->fd_write_event;
|
|
|
|
|
|
|
|
FD_ZERO(&event_set);
|
|
|
|
FD_ZERO(&write_set);
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-28 18:41:24 +04:00
|
|
|
assert(pComm->fd_write_event < FD_SETSIZE);
|
|
|
|
assert(pComm->fd_write < FD_SETSIZE);
|
2014-05-12 19:33:56 +04:00
|
|
|
|
2014-05-28 18:41:24 +04:00
|
|
|
FD_SET(pComm->fd_write_event, &event_set);
|
|
|
|
FD_SET(pComm->fd_write, &write_set);
|
|
|
|
|
2014-05-29 00:11:19 +04:00
|
|
|
nbFds = select(biggestFd+1, &event_set, &write_set, NULL, pTmaxTimeout);
|
2014-05-28 19:18:33 +04:00
|
|
|
if (nbFds < 0)
|
2014-05-28 18:41:24 +04:00
|
|
|
{
|
2014-07-02 17:59:16 +04: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
|
|
|
|
{
|
2014-07-02 17:59:16 +04: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,
|
|
|
|
lpBuffer + (*lpNumberOfBytesWritten),
|
|
|
|
nNumberOfBytesToWrite - (*lpNumberOfBytesWritten));
|
|
|
|
|
2014-05-28 18:41:24 +04:00
|
|
|
if (nbWritten < 0)
|
|
|
|
{
|
2014-07-02 17:59:16 +04:00
|
|
|
CommLog_Print(WLOG_WARN, "CommWriteFile failed after %lu bytes written, errno=[%d] %s\n", *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);
|
2014-05-23 14:27:09 +04:00
|
|
|
|
|
|
|
|
2014-06-17 18:34:20 +04:00
|
|
|
return_true:
|
|
|
|
LeaveCriticalSection(&pComm->WriteLock);
|
2014-05-12 19:33:56 +04:00
|
|
|
return TRUE;
|
2014-06-17 18:34:20 +04:00
|
|
|
|
|
|
|
return_false:
|
|
|
|
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__ */
|