27 #include <winpr/comm.h>
28 #include <winpr/crt.h>
32 static BOOL test_SerCxSys(HANDLE hComm)
38 struct termios currentTermios = { 0 };
40 if (tcgetattr(((WINPR_COMM*)hComm)->fd, ¤tTermios) < 0)
42 (void)fprintf(stderr,
"tcgetattr failure.\n");
46 dcb.DCBlength =
sizeof(
DCB);
47 if (!GetCommState(hComm, &dcb))
49 (void)fprintf(stderr,
"GetCommState failure, GetLastError(): 0x%08x\n", GetLastError());
53 if ((dcb.XonChar ==
'\0') || (dcb.XoffChar ==
'\0'))
55 (void)fprintf(stderr,
"test_SerCxSys failure, expected XonChar and XoffChar to be set\n");
60 if ((dcb.XonChar != currentTermios.c_cc[VSTART]) ||
61 (dcb.XoffChar != currentTermios.c_cc[VSTOP]))
63 (void)fprintf(stderr,
"test_SerCxSys failure, could not retrieve XonChar and XoffChar\n");
69 XonChar = dcb.XonChar;
70 XoffChar = dcb.XoffChar;
71 dcb.XonChar = XoffChar;
72 dcb.XoffChar = XonChar;
73 if (!SetCommState(hComm, &dcb))
75 (void)fprintf(stderr,
"SetCommState failure, GetLastError(): 0x%08x\n", GetLastError());
79 ZeroMemory(&dcb,
sizeof(
DCB));
80 dcb.DCBlength =
sizeof(
DCB);
81 if (!GetCommState(hComm, &dcb))
83 (void)fprintf(stderr,
"GetCommState failure, GetLastError(): 0x%08x\n", GetLastError());
87 if ((dcb.XonChar != XoffChar) || (dcb.XoffChar != XonChar))
89 (void)fprintf(stderr,
"test_SerCxSys, expected XonChar and XoffChar to be swapped\n");
94 dcb.XonChar = dcb.XoffChar;
95 if (SetCommState(hComm, &dcb))
98 "test_SerCxSys failure, SetCommState() was supposed to failed because "
99 "XonChar and XoffChar are the same\n");
102 if (GetLastError() != ERROR_INVALID_PARAMETER)
104 (void)fprintf(stderr,
"test_SerCxSys failure, SetCommState() was supposed to failed with "
105 "GetLastError()=ERROR_INVALID_PARAMETER\n");
112 static BOOL test_SerCx2Sys(HANDLE hComm)
116 dcb.DCBlength =
sizeof(
DCB);
117 if (!GetCommState(hComm, &dcb))
119 (void)fprintf(stderr,
"GetCommState failure; GetLastError(): %08x\n", GetLastError());
123 if ((dcb.ErrorChar !=
'\0') || (dcb.EofChar !=
'\0') || (dcb.EvtChar !=
'\0') ||
124 (dcb.XonChar !=
'\0') || (dcb.XoffChar !=
'\0'))
126 (void)fprintf(stderr,
"test_SerCx2Sys failure, expected all characters to be: '\\0'\n");
133 int TestSerialChars(
int argc,
char* argv[])
135 struct stat statbuf = { 0 };
139 if (stat(
"/dev/ttyS0", &statbuf) < 0)
141 (void)fprintf(stderr,
"/dev/ttyS0 not available, making the test to succeed though\n");
145 result = DefineCommDevice(
"COM1",
"/dev/ttyS0");
148 (void)fprintf(stderr,
"DefineCommDevice failure: 0x%x\n", GetLastError());
152 hComm = CreateFile(
"COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
153 if (hComm == INVALID_HANDLE_VALUE)
155 (void)fprintf(stderr,
"CreateFileA failure: 0x%x\n", GetLastError());
159 _comm_setServerSerialDriver(hComm, SerialDriverSerCxSys);
160 if (!test_SerCxSys(hComm))
162 (void)fprintf(stderr,
"test_SerCxSys failure\n");
166 _comm_setServerSerialDriver(hComm, SerialDriverSerCx2Sys);
167 if (!test_SerCx2Sys(hComm))
169 (void)fprintf(stderr,
"test_SerCxSys failure\n");
173 if (!CloseHandle(hComm))
175 (void)fprintf(stderr,
"CloseHandle failure, GetLastError()=%08x\n", GetLastError());