winpr-comm: implemented IOCTL_SERIAL_SET_CHARS and IOCTL_SERIAL_GET_CHARS
This commit is contained in:
parent
fff1f22f8c
commit
ee2339addc
@ -305,6 +305,10 @@ static void serial_process_irp_device_control(SERIAL_DEVICE* serial, IRP* irp)
|
||||
irp->IoStatus = STATUS_BUFFER_TOO_SMALL; /* TMP: better have STATUS_BUFFER_SIZE_TOO_SMALL? http://msdn.microsoft.com/en-us/library/windows/hardware/ff547466%28v=vs.85%29.aspx#generic_status_values_for_serial_device_control_requests */
|
||||
break;
|
||||
|
||||
case ERROR_INVALID_PARAMETER:
|
||||
irp->IoStatus = STATUS_INVALID_PARAMETER;
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG_SVC("unexpected last-error: 0x%x", GetLastError());
|
||||
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
||||
|
@ -218,8 +218,29 @@ BOOL GetCommState(HANDLE hFile, LPDCB lpDCB)
|
||||
|
||||
lpLocalDcb->fBinary = TRUE; /* TMP: TODO: seems equivalent to the raw mode */
|
||||
|
||||
lpLocalDcb->fParity = (currentState.c_iflag & INPCK) != 0;
|
||||
lpLocalDcb->fParity = (currentState.c_cflag & PARENB) != 0;
|
||||
|
||||
/* TMP: TODO: */
|
||||
/* (...) */
|
||||
|
||||
|
||||
|
||||
SERIAL_CHARS serialChars;
|
||||
if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_CHARS, NULL, 0, &serialChars, sizeof(SERIAL_CHARS), &bytesReturned, NULL))
|
||||
{
|
||||
DEBUG_WARN("GetCommState failure: could not get the serial chars.");
|
||||
goto error_handle;
|
||||
}
|
||||
|
||||
lpLocalDcb->XonChar = serialChars.XonChar;
|
||||
|
||||
lpLocalDcb->XoffChar = serialChars.XoffChar;
|
||||
|
||||
lpLocalDcb->ErrorChar = serialChars.ErrorChar;
|
||||
|
||||
lpLocalDcb->EofChar = serialChars.EofChar;
|
||||
|
||||
lpLocalDcb->EvtChar = serialChars.EventChar;
|
||||
|
||||
|
||||
memcpy(lpDCB, lpLocalDcb, lpDCB->DCBlength);
|
||||
@ -278,6 +299,22 @@ BOOL SetCommState(HANDLE hFile, LPDCB lpDCB)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
SERIAL_CHARS serialChars;
|
||||
if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_GET_CHARS, NULL, 0, &serialChars, sizeof(SERIAL_CHARS), &bytesReturned, NULL))
|
||||
{
|
||||
DEBUG_WARN("SetCommState failure: could not get the initial serial chars.");
|
||||
return FALSE;
|
||||
}
|
||||
serialChars.XonChar = lpDCB->XonChar;
|
||||
serialChars.XoffChar = lpDCB->XoffChar;
|
||||
serialChars.ErrorChar = lpDCB->ErrorChar;
|
||||
serialChars.EofChar = lpDCB->EofChar;
|
||||
serialChars.EventChar = lpDCB->EvtChar;
|
||||
if (!CommDeviceIoControl(pComm, IOCTL_SERIAL_SET_CHARS, &serialChars, sizeof(SERIAL_CHARS), NULL, 0, &bytesReturned, NULL))
|
||||
{
|
||||
DEBUG_WARN("SetCommState failure: could not set the serial chars.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/** upcomingTermios stage **/
|
||||
@ -300,13 +337,16 @@ BOOL SetCommState(HANDLE hFile, LPDCB lpDCB)
|
||||
|
||||
if (lpDCB->fParity)
|
||||
{
|
||||
upcomingTermios.c_iflag |= INPCK;
|
||||
upcomingTermios.c_cflag |= PARENB;
|
||||
}
|
||||
else
|
||||
{
|
||||
upcomingTermios.c_iflag &= ~INPCK;
|
||||
upcomingTermios.c_cflag &= ~PARENB;
|
||||
}
|
||||
|
||||
// TMP: TODO:
|
||||
// (...)
|
||||
|
||||
/* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363423%28v=vs.85%29.aspx
|
||||
*
|
||||
* The SetCommState function reconfigures the communications
|
||||
|
@ -176,6 +176,42 @@ BOOL CommDeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffe
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
case IOCTL_SERIAL_SET_CHARS:
|
||||
{
|
||||
if (pRemoteSerialDriver->set_serial_chars)
|
||||
{
|
||||
SERIAL_CHARS *pSerialChars = (SERIAL_CHARS*)lpInBuffer;
|
||||
|
||||
assert(nInBufferSize >= sizeof(SERIAL_CHARS));
|
||||
if (nInBufferSize < sizeof(SERIAL_CHARS))
|
||||
{
|
||||
SetLastError(ERROR_INVALID_DATA);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return pRemoteSerialDriver->set_serial_chars(pComm, pSerialChars);
|
||||
}
|
||||
}
|
||||
case IOCTL_SERIAL_GET_CHARS:
|
||||
{
|
||||
if (pRemoteSerialDriver->get_serial_chars)
|
||||
{
|
||||
SERIAL_CHARS *pSerialChars = (SERIAL_CHARS*)lpOutBuffer;
|
||||
|
||||
assert(nOutBufferSize >= sizeof(SERIAL_CHARS));
|
||||
if (nOutBufferSize < sizeof(SERIAL_CHARS))
|
||||
{
|
||||
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!pRemoteSerialDriver->get_serial_chars(pComm, pSerialChars))
|
||||
return FALSE;
|
||||
|
||||
*lpBytesReturned = sizeof(SERIAL_CHARS);
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DEBUG_WARN(_T("unsupported IoControlCode: Ox%x (remote serial driver: %s)"), dwIoControlCode, pRemoteSerialDriver->name);
|
||||
|
@ -48,8 +48,11 @@ extern "C" {
|
||||
/* IOCTL_SERIAL_GET_LINE_CONTROL 0x001B0054 */
|
||||
/* IOCTL_SERIAL_SET_TIMEOUTS 0x001B001C */
|
||||
/* IOCTL_SERIAL_GET_TIMEOUTS 0x001B0020 */
|
||||
/* IOCTL_SERIAL_SET_CHARS 0x001B0058 */
|
||||
/* IOCTL_SERIAL_GET_CHARS 0x001B005C */
|
||||
|
||||
/* GET_CHARS and SET_CHARS are swapped in the RDP docs [MS-RDPESP] */
|
||||
#define IOCTL_SERIAL_GET_CHARS 0x001B0058
|
||||
#define IOCTL_SERIAL_SET_CHARS 0x001B005C
|
||||
|
||||
/* IOCTL_SERIAL_SET_DTR 0x001B0024 */
|
||||
/* IOCTL_SERIAL_CLR_DTR 0x001B0028 */
|
||||
/* IOCTL_SERIAL_RESET_DEVICE 0x001B002C */
|
||||
@ -101,6 +104,17 @@ typedef struct _SERIAL_BAUD_RATE
|
||||
} SERIAL_BAUD_RATE, *PSERIAL_BAUD_RATE;
|
||||
|
||||
|
||||
typedef struct _SERIAL_CHARS
|
||||
{
|
||||
UCHAR EofChar;
|
||||
UCHAR ErrorChar;
|
||||
UCHAR BreakChar;
|
||||
UCHAR EventChar;
|
||||
UCHAR XonChar;
|
||||
UCHAR XoffChar;
|
||||
} SERIAL_CHARS, *PSERIAL_CHARS;
|
||||
|
||||
|
||||
/**
|
||||
* A function might be NULL if not supported by the underlying remote driver.
|
||||
*
|
||||
@ -110,9 +124,11 @@ typedef struct _REMOTE_SERIAL_DRIVER
|
||||
{
|
||||
REMOTE_SERIAL_DRIVER_ID id;
|
||||
TCHAR *name;
|
||||
BOOL (*set_baud_rate)(WINPR_COMM *pComm, SERIAL_BAUD_RATE *pBaudRate /* in */);
|
||||
BOOL (*get_baud_rate)(WINPR_COMM *pComm, SERIAL_BAUD_RATE *pBaudRate /* out */);
|
||||
BOOL (*get_properties)(WINPR_COMM *pComm, COMMPROP *pProperties /* out */);
|
||||
BOOL (*set_baud_rate)(WINPR_COMM *pComm, const SERIAL_BAUD_RATE *pBaudRate);
|
||||
BOOL (*get_baud_rate)(WINPR_COMM *pComm, SERIAL_BAUD_RATE *pBaudRate);
|
||||
BOOL (*get_properties)(WINPR_COMM *pComm, COMMPROP *pProperties);
|
||||
BOOL (*set_serial_chars)(WINPR_COMM *pComm, const SERIAL_CHARS *pSerialChars);
|
||||
BOOL (*get_serial_chars)(WINPR_COMM *pComm, SERIAL_CHARS *pSerialChars);
|
||||
|
||||
} REMOTE_SERIAL_DRIVER;
|
||||
|
||||
|
@ -27,14 +27,40 @@
|
||||
#include "comm_sercx2_sys.h"
|
||||
|
||||
|
||||
/* http://msdn.microsoft.com/en-us/library/dn265347%28v=vs.85%29.aspx
|
||||
*
|
||||
* SerCx2 does not support special characters. SerCx2 always completes
|
||||
* an IOCTL_SERIAL_SET_CHARS request with a STATUS_SUCCESS status
|
||||
* code, but does not set any special characters or perform any other
|
||||
* operation in response to this request. For an
|
||||
* IOCTL_SERIAL_GET_CHARS request, SerCx2 sets all the character
|
||||
* values in the SERIAL_CHARS structure to null, and completes the
|
||||
* request with a STATUS_SUCCESS status code.
|
||||
*/
|
||||
|
||||
static BOOL _set_serial_chars(WINPR_COMM* pComm, const SERIAL_CHARS* pSerialChars)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
static BOOL _get_serial_chars(WINPR_COMM* pComm, SERIAL_CHARS* pSerialChars)
|
||||
{
|
||||
ZeroMemory(pSerialChars, sizeof(SERIAL_CHARS));
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/* specific functions only */
|
||||
static REMOTE_SERIAL_DRIVER _SerCx2Sys =
|
||||
{
|
||||
.id = RemoteSerialDriverSerCx2Sys,
|
||||
.name = _T("SerCx2.sys"),
|
||||
.set_baud_rate = NULL,
|
||||
.get_baud_rate = NULL,
|
||||
.get_properties = NULL,
|
||||
.id = RemoteSerialDriverSerCx2Sys,
|
||||
.name = _T("SerCx2.sys"),
|
||||
.set_baud_rate = NULL,
|
||||
.get_baud_rate = NULL,
|
||||
.get_properties = NULL,
|
||||
.set_serial_chars = _set_serial_chars,
|
||||
.get_serial_chars = _get_serial_chars,
|
||||
};
|
||||
|
||||
|
||||
@ -44,9 +70,9 @@ REMOTE_SERIAL_DRIVER* SerCx2Sys_s()
|
||||
//REMOTE_SERIAL_DRIVER* pSerialSys = SerialSys_s();
|
||||
REMOTE_SERIAL_DRIVER* pSerCxSys = SerCxSys_s();
|
||||
|
||||
_SerCx2Sys.set_baud_rate = pSerCxSys->set_baud_rate;
|
||||
_SerCx2Sys.get_baud_rate = pSerCxSys->get_baud_rate;
|
||||
_SerCx2Sys.get_properties = pSerCxSys->get_properties;
|
||||
_SerCx2Sys.set_baud_rate = pSerCxSys->set_baud_rate;
|
||||
_SerCx2Sys.get_baud_rate = pSerCxSys->get_baud_rate;
|
||||
_SerCx2Sys.get_properties = pSerCxSys->get_properties;
|
||||
|
||||
return &_SerCx2Sys;
|
||||
}
|
||||
|
@ -174,7 +174,7 @@ static BOOL _get_properties(WINPR_COMM *pComm, COMMPROP *pProperties)
|
||||
}
|
||||
|
||||
|
||||
static BOOL _set_baud_rate(WINPR_COMM *pComm, SERIAL_BAUD_RATE *pBaudRate)
|
||||
static BOOL _set_baud_rate(WINPR_COMM *pComm, const SERIAL_BAUD_RATE *pBaudRate)
|
||||
{
|
||||
int i;
|
||||
speed_t newSpeed;
|
||||
@ -249,19 +249,24 @@ static BOOL _get_baud_rate(WINPR_COMM *pComm, SERIAL_BAUD_RATE *pBaudRate)
|
||||
/* specific functions only */
|
||||
static REMOTE_SERIAL_DRIVER _SerCxSys =
|
||||
{
|
||||
.id = RemoteSerialDriverSerCxSys,
|
||||
.name = _T("SerCx.sys"),
|
||||
.set_baud_rate = _set_baud_rate,
|
||||
.get_baud_rate = _get_baud_rate,
|
||||
.get_properties = _get_properties,
|
||||
.id = RemoteSerialDriverSerCxSys,
|
||||
.name = _T("SerCx.sys"),
|
||||
.set_baud_rate = _set_baud_rate,
|
||||
.get_baud_rate = _get_baud_rate,
|
||||
.get_properties = _get_properties,
|
||||
.set_serial_chars = NULL,
|
||||
.get_serial_chars = NULL,
|
||||
};
|
||||
|
||||
|
||||
|
||||
REMOTE_SERIAL_DRIVER* SerCxSys_s()
|
||||
{
|
||||
/* _SerCxSys completed with default SerialSys functions */
|
||||
//REMOTE_SERIAL_DRIVER* pSerialSys = SerialSys();
|
||||
/* _SerCxSys completed with default SerialSys_s functions */
|
||||
REMOTE_SERIAL_DRIVER* pSerialSys = SerialSys_s();
|
||||
|
||||
_SerCxSys.set_serial_chars = pSerialSys->set_serial_chars;
|
||||
_SerCxSys.get_serial_chars = pSerialSys->get_serial_chars;
|
||||
|
||||
return &_SerCxSys;
|
||||
}
|
||||
|
@ -175,14 +175,14 @@ static BOOL _get_properties(WINPR_COMM *pComm, COMMPROP *pProperties)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static BOOL _set_baud_rate(WINPR_COMM *pComm, SERIAL_BAUD_RATE *pBaudRate)
|
||||
static BOOL _set_baud_rate(WINPR_COMM *pComm, const SERIAL_BAUD_RATE *pBaudRate)
|
||||
{
|
||||
int i;
|
||||
speed_t newSpeed;
|
||||
struct termios futureState;
|
||||
struct termios upcomingTermios;
|
||||
|
||||
ZeroMemory(&futureState, sizeof(struct termios));
|
||||
if (tcgetattr(pComm->fd, &futureState) < 0) /* NB: preserves current settings not directly handled by the Communication Functions */
|
||||
ZeroMemory(&upcomingTermios, sizeof(struct termios));
|
||||
if (tcgetattr(pComm->fd, &upcomingTermios) < 0)
|
||||
{
|
||||
SetLastError(ERROR_IO_DEVICE);
|
||||
return FALSE;
|
||||
@ -193,13 +193,13 @@ static BOOL _set_baud_rate(WINPR_COMM *pComm, SERIAL_BAUD_RATE *pBaudRate)
|
||||
if (_SERIAL_SYS_BAUD_TABLE[i][1] == pBaudRate->BaudRate)
|
||||
{
|
||||
newSpeed = _SERIAL_SYS_BAUD_TABLE[i][0];
|
||||
if (cfsetspeed(&futureState, newSpeed) < 0)
|
||||
if (cfsetspeed(&upcomingTermios, newSpeed) < 0)
|
||||
{
|
||||
DEBUG_WARN("failed to set speed %d (%d)", newSpeed, pBaudRate->BaudRate);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (_comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &futureState) < 0)
|
||||
if (_comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &upcomingTermios) < 0)
|
||||
{
|
||||
DEBUG_WARN("_comm_ioctl_tcsetattr failure: last-error: 0x0.8x", GetLastError());
|
||||
return FALSE;
|
||||
@ -244,14 +244,152 @@ static BOOL _get_baud_rate(WINPR_COMM *pComm, SERIAL_BAUD_RATE *pBaudRate)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: Only XonChar and XoffChar are plenty supported with Linux
|
||||
* N_TTY line discipline.
|
||||
*
|
||||
* ERRORS:
|
||||
* ERROR_IO_DEVICE
|
||||
* ERROR_INVALID_PARAMETER when Xon and Xoff chars are the same;
|
||||
*/
|
||||
static BOOL _set_serial_chars(WINPR_COMM *pComm, const SERIAL_CHARS *pSerialChars)
|
||||
{
|
||||
struct termios upcomingTermios;
|
||||
|
||||
ZeroMemory(&upcomingTermios, sizeof(struct termios));
|
||||
if (tcgetattr(pComm->fd, &upcomingTermios) < 0)
|
||||
{
|
||||
SetLastError(ERROR_IO_DEVICE);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (pSerialChars->XonChar == pSerialChars->XoffChar)
|
||||
{
|
||||
/* http://msdn.microsoft.com/en-us/library/windows/hardware/ff546688%28v=vs.85%29.aspx */
|
||||
SetLastError(ERROR_INVALID_PARAMETER);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* termios(3): (..) above symbolic subscript values are all
|
||||
* different, except that VTIME, VMIN may have the same value
|
||||
* as VEOL, VEOF, respectively. In noncanonical mode the
|
||||
* special character meaning is replaced by the timeout
|
||||
* meaning.
|
||||
*
|
||||
* It doesn't seem the case of the Linux's implementation but
|
||||
* in our context, the cannonical mode (fBinary=FALSE) should
|
||||
* never be enabled.
|
||||
*/
|
||||
|
||||
if (upcomingTermios.c_lflag & ICANON)
|
||||
{
|
||||
upcomingTermios.c_cc[VEOF] = pSerialChars->EofChar;
|
||||
DEBUG_WARN("c_cc[VEOF] is not supposed to be modified!");
|
||||
}
|
||||
/* else let c_cc[VEOF] unchanged */
|
||||
|
||||
|
||||
/* According the Linux's n_tty discipline, charaters with a
|
||||
* parity error can only be let unchanged, replaced by \0 or
|
||||
* get the prefix the prefix \377 \0
|
||||
*/
|
||||
|
||||
if (pSerialChars->ErrorChar == '\0')
|
||||
{
|
||||
/* Also suppose PARENB !IGNPAR !PARMRK to be effective */
|
||||
upcomingTermios.c_iflag |= INPCK;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* FIXME: develop a line discipline dedicated to the
|
||||
* RDP redirection. Erroneous characters might also be
|
||||
* caught during read/write operations?
|
||||
*/
|
||||
DEBUG_WARN("ErrorChar='%c' cannot be set, characters with a parity error will be let unchanged.\n", pSerialChars->ErrorChar);
|
||||
}
|
||||
|
||||
if (pSerialChars->BreakChar == '\0')
|
||||
{
|
||||
upcomingTermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* FIXME: develop a line discipline dedicated to the
|
||||
* RDP redirection. Break characters might also be
|
||||
* caught during read/write operations?
|
||||
*/
|
||||
DEBUG_WARN("BreakChar='%c' cannot be set.\n", pSerialChars->ErrorChar);
|
||||
}
|
||||
|
||||
/* FIXME: Didn't find anything similar inside N_TTY. Develop a
|
||||
* line discipline dedicated to the RDP redirection.
|
||||
*/
|
||||
DEBUG_WARN("EventChar='%c' cannot be set\n", pSerialChars->EventChar);
|
||||
|
||||
upcomingTermios.c_cc[VSTART] = pSerialChars->XonChar;
|
||||
|
||||
upcomingTermios.c_cc[VSTOP] = pSerialChars->XoffChar;
|
||||
|
||||
|
||||
if (_comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &upcomingTermios) < 0)
|
||||
{
|
||||
DEBUG_WARN("_comm_ioctl_tcsetattr failure: last-error: 0x0.8x", GetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
static BOOL _get_serial_chars(WINPR_COMM *pComm, SERIAL_CHARS *pSerialChars)
|
||||
{
|
||||
struct termios currentTermios;
|
||||
|
||||
ZeroMemory(¤tTermios, sizeof(struct termios));
|
||||
if (tcgetattr(pComm->fd, ¤tTermios) < 0)
|
||||
{
|
||||
SetLastError(ERROR_IO_DEVICE);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ZeroMemory(pSerialChars, sizeof(SERIAL_CHARS));
|
||||
|
||||
if (currentTermios.c_lflag & ICANON)
|
||||
{
|
||||
pSerialChars->EofChar = currentTermios.c_cc[VEOF];
|
||||
}
|
||||
|
||||
/* FIXME: see also: _set_serial_chars() */
|
||||
if (currentTermios.c_iflag & INPCK)
|
||||
pSerialChars->ErrorChar = '\0';
|
||||
/* else '\0' is currently used anyway */
|
||||
|
||||
/* FIXME: see also: _set_serial_chars() */
|
||||
if (currentTermios.c_iflag & ~IGNBRK & BRKINT)
|
||||
pSerialChars->BreakChar = '\0';
|
||||
/* else '\0' is currently used anyway */
|
||||
|
||||
/* FIXME: see also: _set_serial_chars() */
|
||||
pSerialChars->EventChar = '\0';
|
||||
|
||||
pSerialChars->XonChar = currentTermios.c_cc[VSTART];
|
||||
|
||||
pSerialChars->XoffChar = currentTermios.c_cc[VSTOP];
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static REMOTE_SERIAL_DRIVER _SerialSys =
|
||||
{
|
||||
.id = RemoteSerialDriverSerialSys,
|
||||
.name = _T("Serial.sys"),
|
||||
.set_baud_rate = _set_baud_rate,
|
||||
.get_baud_rate = _get_baud_rate,
|
||||
.get_properties = _get_properties,
|
||||
.id = RemoteSerialDriverSerialSys,
|
||||
.name = _T("Serial.sys"),
|
||||
.set_baud_rate = _set_baud_rate,
|
||||
.get_baud_rate = _get_baud_rate,
|
||||
.get_properties = _get_properties,
|
||||
.set_serial_chars = _set_serial_chars,
|
||||
.get_serial_chars = _get_serial_chars,
|
||||
};
|
||||
|
||||
|
||||
|
@ -9,6 +9,7 @@ set(${MODULE_PREFIX}_TESTS
|
||||
TestCommConfig.c
|
||||
TestGetCommState.c
|
||||
TestSetCommState.c
|
||||
TestSerialChars.c
|
||||
TestCommMonitor.c)
|
||||
|
||||
create_test_sourcelist(${MODULE_PREFIX}_SRCS
|
||||
|
@ -29,7 +29,7 @@ static BOOL test_generic(HANDLE hComm)
|
||||
DCB dcb, *pDcb;
|
||||
BOOL result;
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
ZeroMemory(&dcb, sizeof(DCB));
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (result)
|
||||
{
|
||||
@ -38,7 +38,7 @@ static BOOL test_generic(HANDLE hComm)
|
||||
}
|
||||
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
ZeroMemory(&dcb, sizeof(DCB));
|
||||
dcb.DCBlength = sizeof(DCB) / 2; /* improper value */
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (result)
|
||||
@ -47,7 +47,7 @@ static BOOL test_generic(HANDLE hComm)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
ZeroMemory(&dcb, sizeof(DCB));
|
||||
dcb.DCBlength = sizeof(DCB);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
@ -119,5 +119,11 @@ int TestGetCommState(int argc, char* argv[])
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!CloseHandle(hComm))
|
||||
{
|
||||
fprintf(stderr, "CloseHandle failure, GetLastError()=%0.8x\n", GetLastError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
154
winpr/libwinpr/comm/test/TestSerialChars.c
Normal file
154
winpr/libwinpr/comm/test/TestSerialChars.c
Normal file
@ -0,0 +1,154 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <winpr/comm.h>
|
||||
#include <winpr/crt.h>
|
||||
|
||||
#include "../comm.h"
|
||||
|
||||
static BOOL test_SerCxSys(HANDLE hComm)
|
||||
{
|
||||
DCB dcb;
|
||||
UCHAR XonChar, XoffChar;
|
||||
|
||||
ZeroMemory(&dcb, sizeof(DCB));
|
||||
dcb.DCBlength = sizeof(DCB);
|
||||
if (!GetCommState(hComm, &dcb))
|
||||
{
|
||||
fprintf(stderr, "GetCommState failure, GetLastError(): 0x%0.8x\n", GetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if ((dcb.XonChar == '\0') || (dcb.XoffChar == '\0'))
|
||||
{
|
||||
fprintf(stderr, "test_SerCxSys failure, expected XonChar and XoffChar to be set\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* swap XonChar/XoffChar */
|
||||
|
||||
XonChar = dcb.XonChar;
|
||||
XoffChar = dcb.XoffChar;
|
||||
dcb.XonChar = XoffChar;
|
||||
dcb.XoffChar = XonChar;
|
||||
if (!SetCommState(hComm, &dcb))
|
||||
{
|
||||
fprintf(stderr, "SetCommState failure, GetLastError(): 0x%0.8x\n", GetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ZeroMemory(&dcb, sizeof(DCB));
|
||||
dcb.DCBlength = sizeof(DCB);
|
||||
if (!GetCommState(hComm, &dcb))
|
||||
{
|
||||
fprintf(stderr, "GetCommState failure, GetLastError(): 0x%0.8x\n", GetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if ((dcb.XonChar != XoffChar) || (dcb.XoffChar != XonChar))
|
||||
{
|
||||
fprintf(stderr, "test_SerCxSys, expected XonChar and XoffChar to be swapped\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* same XonChar / XoffChar */
|
||||
dcb.XonChar = dcb.XoffChar;
|
||||
if (SetCommState(hComm, &dcb))
|
||||
{
|
||||
fprintf(stderr, "test_SerCxSys failure, SetCommState() was supposed to failed because XonChar and XoffChar are the same\n");
|
||||
return FALSE;
|
||||
}
|
||||
if (GetLastError() != ERROR_INVALID_PARAMETER)
|
||||
{
|
||||
fprintf(stderr, "test_SerCxSys failure, SetCommState() was supposed to failed with GetLastError()=ERROR_INVALID_PARAMETER\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
static BOOL test_SerCx2Sys(HANDLE hComm)
|
||||
{
|
||||
DCB dcb;
|
||||
|
||||
ZeroMemory(&dcb, sizeof(DCB));
|
||||
dcb.DCBlength = sizeof(DCB);
|
||||
if (!GetCommState(hComm, &dcb))
|
||||
{
|
||||
fprintf(stderr, "GetCommState failure; GetLastError(): %0.8x\n", GetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if ((dcb.XonChar != '\0') || (dcb.XoffChar != '\0') || (dcb.ErrorChar != '\0') || (dcb.EofChar != '\0') || (dcb.EvtChar != '\0'))
|
||||
{
|
||||
fprintf(stderr, "test_SerCx2Sys failure, expected all characters to '\0'\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int TestSerialChars(int argc, char* argv[])
|
||||
{
|
||||
BOOL result;
|
||||
HANDLE hComm;
|
||||
|
||||
// TMP: FIXME: check if we can proceed with tests on the actual device, skip and warn otherwise but don't fail
|
||||
result = DefineCommDevice("COM1", "/dev/ttyS0");
|
||||
if (!result)
|
||||
{
|
||||
fprintf(stderr, "DefineCommDevice failure: 0x%x\n", GetLastError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
hComm = CreateFile("COM1",
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
0, NULL, OPEN_EXISTING, 0, NULL);
|
||||
if (hComm == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
fprintf(stderr, "CreateFileA failure: 0x%x\n", GetLastError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
_comm_setRemoteSerialDriver(hComm, RemoteSerialDriverSerCxSys);
|
||||
if (!test_SerCxSys(hComm))
|
||||
{
|
||||
fprintf(stderr, "test_SerCxSys failure\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
_comm_setRemoteSerialDriver(hComm, RemoteSerialDriverSerCx2Sys);
|
||||
if (!test_SerCx2Sys(hComm))
|
||||
{
|
||||
fprintf(stderr, "test_SerCxSys failure\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
if (!CloseHandle(hComm))
|
||||
{
|
||||
fprintf(stderr, "CloseHandle failure, GetLastError()=%0.8x\n", GetLastError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
@ -24,13 +24,20 @@
|
||||
|
||||
#include "../comm.h"
|
||||
|
||||
static void init_empty_dcb(DCB *pDcb)
|
||||
{
|
||||
ZeroMemory(pDcb, sizeof(DCB));
|
||||
pDcb->DCBlength = sizeof(DCB);
|
||||
pDcb->XonChar = 1;
|
||||
pDcb->XoffChar = 2;
|
||||
}
|
||||
|
||||
static BOOL test_fParity(HANDLE hComm)
|
||||
{
|
||||
DCB dcb;
|
||||
BOOL result;
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -47,8 +54,7 @@ static BOOL test_fParity(HANDLE hComm)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -71,8 +77,7 @@ static BOOL test_fParity(HANDLE hComm)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -95,8 +100,7 @@ static BOOL test_fParity(HANDLE hComm)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -119,8 +123,7 @@ static BOOL test_SerialSys(HANDLE hComm)
|
||||
DCB dcb;
|
||||
BOOL result;
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -137,8 +140,7 @@ static BOOL test_SerialSys(HANDLE hComm)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -161,8 +163,7 @@ static BOOL test_SerialSys(HANDLE hComm)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -192,8 +193,7 @@ static BOOL test_SerCxSys(HANDLE hComm)
|
||||
DCB dcb;
|
||||
BOOL result;
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -210,8 +210,7 @@ static BOOL test_SerCxSys(HANDLE hComm)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -234,8 +233,7 @@ static BOOL test_SerCxSys(HANDLE hComm)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -272,8 +270,7 @@ static BOOL test_generic(HANDLE hComm)
|
||||
DCB dcb, dcb2;
|
||||
BOOL result;
|
||||
|
||||
ZeroMemory(&dcb, sizeof(dcb));
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
init_empty_dcb(&dcb);
|
||||
result = GetCommState(hComm, &dcb);
|
||||
if (!result)
|
||||
{
|
||||
@ -382,5 +379,11 @@ int TestSetCommState(int argc, char* argv[])
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!CloseHandle(hComm))
|
||||
{
|
||||
fprintf(stderr, "CloseHandle failure, GetLastError()=%0.8x\n", GetLastError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user