FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
TestSerialChars.c
1
20#include <stdio.h>
21#include <sys/stat.h>
22
23#ifndef _WIN32
24#include <termios.h>
25#endif
26
27#include <winpr/comm.h>
28#include <winpr/crt.h>
29
30#include "../comm.h"
31
32static BOOL test_SerCxSys(HANDLE hComm)
33{
34 DCB dcb = { 0 };
35 UCHAR XonChar = 0;
36 UCHAR XoffChar = 0;
37
38 struct termios currentTermios = { 0 };
39
40 if (tcgetattr(((WINPR_COMM*)hComm)->fd, &currentTermios) < 0)
41 {
42 (void)fprintf(stderr, "tcgetattr failure.\n");
43 return FALSE;
44 }
45
46 dcb.DCBlength = sizeof(DCB);
47 if (!GetCommState(hComm, &dcb))
48 {
49 (void)fprintf(stderr, "GetCommState failure, GetLastError(): 0x%08x\n", GetLastError());
50 return FALSE;
51 }
52
53 if ((dcb.XonChar == '\0') || (dcb.XoffChar == '\0'))
54 {
55 (void)fprintf(stderr, "test_SerCxSys failure, expected XonChar and XoffChar to be set\n");
56 return FALSE;
57 }
58
59 /* retrieve Xon/Xoff chars */
60 if ((dcb.XonChar != currentTermios.c_cc[VSTART]) ||
61 (dcb.XoffChar != currentTermios.c_cc[VSTOP]))
62 {
63 (void)fprintf(stderr, "test_SerCxSys failure, could not retrieve XonChar and XoffChar\n");
64 return FALSE;
65 }
66
67 /* swap XonChar/XoffChar */
68
69 XonChar = dcb.XonChar;
70 XoffChar = dcb.XoffChar;
71 dcb.XonChar = XoffChar;
72 dcb.XoffChar = XonChar;
73 if (!SetCommState(hComm, &dcb))
74 {
75 (void)fprintf(stderr, "SetCommState failure, GetLastError(): 0x%08x\n", GetLastError());
76 return FALSE;
77 }
78
79 ZeroMemory(&dcb, sizeof(DCB));
80 dcb.DCBlength = sizeof(DCB);
81 if (!GetCommState(hComm, &dcb))
82 {
83 (void)fprintf(stderr, "GetCommState failure, GetLastError(): 0x%08x\n", GetLastError());
84 return FALSE;
85 }
86
87 if ((dcb.XonChar != XoffChar) || (dcb.XoffChar != XonChar))
88 {
89 (void)fprintf(stderr, "test_SerCxSys, expected XonChar and XoffChar to be swapped\n");
90 return FALSE;
91 }
92
93 /* same XonChar / XoffChar */
94 dcb.XonChar = dcb.XoffChar;
95 if (SetCommState(hComm, &dcb))
96 {
97 (void)fprintf(stderr,
98 "test_SerCxSys failure, SetCommState() was supposed to failed because "
99 "XonChar and XoffChar are the same\n");
100 return FALSE;
101 }
102 if (GetLastError() != ERROR_INVALID_PARAMETER)
103 {
104 (void)fprintf(stderr, "test_SerCxSys failure, SetCommState() was supposed to failed with "
105 "GetLastError()=ERROR_INVALID_PARAMETER\n");
106 return FALSE;
107 }
108
109 return TRUE;
110}
111
112static BOOL test_SerCx2Sys(HANDLE hComm)
113{
114 DCB dcb = { 0 };
115
116 dcb.DCBlength = sizeof(DCB);
117 if (!GetCommState(hComm, &dcb))
118 {
119 (void)fprintf(stderr, "GetCommState failure; GetLastError(): %08x\n", GetLastError());
120 return FALSE;
121 }
122
123 if ((dcb.ErrorChar != '\0') || (dcb.EofChar != '\0') || (dcb.EvtChar != '\0') ||
124 (dcb.XonChar != '\0') || (dcb.XoffChar != '\0'))
125 {
126 (void)fprintf(stderr, "test_SerCx2Sys failure, expected all characters to be: '\\0'\n");
127 return FALSE;
128 }
129
130 return TRUE;
131}
132
133int TestSerialChars(int argc, char* argv[])
134{
135 struct stat statbuf = { 0 };
136 BOOL result = 0;
137 HANDLE hComm = NULL;
138
139 if (stat("/dev/ttyS0", &statbuf) < 0)
140 {
141 (void)fprintf(stderr, "/dev/ttyS0 not available, making the test to succeed though\n");
142 return EXIT_SUCCESS;
143 }
144
145 result = DefineCommDevice("COM1", "/dev/ttyS0");
146 if (!result)
147 {
148 (void)fprintf(stderr, "DefineCommDevice failure: 0x%x\n", GetLastError());
149 return EXIT_FAILURE;
150 }
151
152 hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
153 if (hComm == INVALID_HANDLE_VALUE)
154 {
155 (void)fprintf(stderr, "CreateFileA failure: 0x%x\n", GetLastError());
156 return EXIT_FAILURE;
157 }
158
159 _comm_setServerSerialDriver(hComm, SerialDriverSerCxSys);
160 if (!test_SerCxSys(hComm))
161 {
162 (void)fprintf(stderr, "test_SerCxSys failure\n");
163 return EXIT_FAILURE;
164 }
165
166 _comm_setServerSerialDriver(hComm, SerialDriverSerCx2Sys);
167 if (!test_SerCx2Sys(hComm))
168 {
169 (void)fprintf(stderr, "test_SerCxSys failure\n");
170 return EXIT_FAILURE;
171 }
172
173 if (!CloseHandle(hComm))
174 {
175 (void)fprintf(stderr, "CloseHandle failure, GetLastError()=%08x\n", GetLastError());
176 return EXIT_FAILURE;
177 }
178
179 return EXIT_SUCCESS;
180}