1 | /* $Id: SerialTest.cpp 69985 2017-12-07 15:46:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SerialTest - Serial port testing utility.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/err.h>
|
---|
32 | #include <iprt/getopt.h>
|
---|
33 | #include <iprt/path.h>
|
---|
34 | #include <iprt/param.h>
|
---|
35 | #include <iprt/process.h>
|
---|
36 | #include <iprt/serialport.h>
|
---|
37 | #include <iprt/stream.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/test.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Defined Constants And Macros *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | /*********************************************************************************************************************************
|
---|
55 | * Global Variables *
|
---|
56 | *********************************************************************************************************************************/
|
---|
57 |
|
---|
58 |
|
---|
59 | /** Command line parameters */
|
---|
60 | static const RTGETOPTDEF g_aCmdOptions[] =
|
---|
61 | {
|
---|
62 | {"--device", 'd', RTGETOPT_REQ_STRING },
|
---|
63 | {"--baudrate", 'b', RTGETOPT_REQ_UINT32 },
|
---|
64 | {"--parity", 'p', RTGETOPT_REQ_STRING },
|
---|
65 | {"--databits", 'c', RTGETOPT_REQ_UINT32 },
|
---|
66 | {"--stopbits", 's', RTGETOPT_REQ_STRING },
|
---|
67 | {"--loopbackdevice", 'l', RTGETOPT_REQ_STRING },
|
---|
68 | {"--help", 'h', RTGETOPT_REQ_NOTHING}
|
---|
69 | };
|
---|
70 |
|
---|
71 | /** The test handle. */
|
---|
72 | static RTTEST g_hTest;
|
---|
73 | /** The serial port handle. */
|
---|
74 | static RTSERIALPORT g_hSerialPort = NIL_RTSERIALPORT;
|
---|
75 | /** The loopback serial port handle if configured. */
|
---|
76 | static RTSERIALPORT g_hSerialPortLoopback = NIL_RTSERIALPORT;
|
---|
77 | /** The config used. */
|
---|
78 | static RTSERIALPORTCFG g_SerialPortCfg =
|
---|
79 | {
|
---|
80 | /* uBaudRate */
|
---|
81 | 115200,
|
---|
82 | /* enmParity */
|
---|
83 | RTSERIALPORTPARITY_NONE,
|
---|
84 | /* enmDataBitCount */
|
---|
85 | RTSERIALPORTDATABITS_8BITS,
|
---|
86 | /* enmStopBitCount */
|
---|
87 | RTSERIALPORTSTOPBITS_ONE
|
---|
88 | };
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Runs the selected serial tests with the given configuration.
|
---|
94 | *
|
---|
95 | * @returns IPRT status code.
|
---|
96 | */
|
---|
97 | static int serialTestRun(void)
|
---|
98 | {
|
---|
99 | return VERR_NOT_IMPLEMENTED;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Shows tool usage text.
|
---|
105 | */
|
---|
106 | static void serialTestUsage(PRTSTREAM pStrm)
|
---|
107 | {
|
---|
108 | char szExec[RTPATH_MAX];
|
---|
109 | RTStrmPrintf(pStrm, "usage: %s [options]\n",
|
---|
110 | RTPathFilename(RTProcGetExecutablePath(szExec, sizeof(szExec))));
|
---|
111 | RTStrmPrintf(pStrm, "\n");
|
---|
112 | RTStrmPrintf(pStrm, "options: \n");
|
---|
113 |
|
---|
114 |
|
---|
115 | for (unsigned i = 0; i < RT_ELEMENTS(g_aCmdOptions); i++)
|
---|
116 | {
|
---|
117 | const char *pszHelp;
|
---|
118 | switch (g_aCmdOptions[i].iShort)
|
---|
119 | {
|
---|
120 | case 'h':
|
---|
121 | pszHelp = "Displays this help and exit";
|
---|
122 | break;
|
---|
123 | case 'd':
|
---|
124 | pszHelp = "Use the specified serial port device";
|
---|
125 | break;
|
---|
126 | case 'b':
|
---|
127 | pszHelp = "Use the given baudrate";
|
---|
128 | break;
|
---|
129 | case 'p':
|
---|
130 | pszHelp = "Use the given parity, valid modes are: none, even, odd, mark, space";
|
---|
131 | break;
|
---|
132 | case 'c':
|
---|
133 | pszHelp = "Use the given data bitcount, valid are: 5, 6, 7, 8";
|
---|
134 | break;
|
---|
135 | case 's':
|
---|
136 | pszHelp = "Use the given stop bitcount, valid are: 1, 1.5, 2";
|
---|
137 | break;
|
---|
138 | case 'l':
|
---|
139 | pszHelp = "Use the given serial port device as the loopback device";
|
---|
140 | break;
|
---|
141 | default:
|
---|
142 | pszHelp = "Option undocumented";
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | char szOpt[256];
|
---|
146 | RTStrPrintf(szOpt, sizeof(szOpt), "%s, -%c", g_aCmdOptions[i].pszLong, g_aCmdOptions[i].iShort);
|
---|
147 | RTStrmPrintf(pStrm, " %-30s%s\n", szOpt, pszHelp);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | int main(int argc, char *argv[])
|
---|
153 | {
|
---|
154 | /*
|
---|
155 | * Init IPRT and globals.
|
---|
156 | */
|
---|
157 | int rc = RTTestInitAndCreate("SerialTest", &g_hTest);
|
---|
158 | if (rc)
|
---|
159 | return rc;
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * Default values.
|
---|
163 | */
|
---|
164 | const char *pszDevice = NULL;
|
---|
165 | const char *pszDeviceLoopback = NULL;
|
---|
166 |
|
---|
167 | RTGETOPTUNION ValueUnion;
|
---|
168 | RTGETOPTSTATE GetState;
|
---|
169 | RTGetOptInit(&GetState, argc, argv, g_aCmdOptions, RT_ELEMENTS(g_aCmdOptions), 1, 0 /* fFlags */);
|
---|
170 | while ((rc = RTGetOpt(&GetState, &ValueUnion)))
|
---|
171 | {
|
---|
172 | switch (rc)
|
---|
173 | {
|
---|
174 | case 'h':
|
---|
175 | serialTestUsage(g_pStdOut);
|
---|
176 | return RTEXITCODE_SUCCESS;
|
---|
177 | case 'd':
|
---|
178 | pszDevice = ValueUnion.psz;
|
---|
179 | break;
|
---|
180 | case 'l':
|
---|
181 | pszDeviceLoopback = ValueUnion.psz;
|
---|
182 | break;
|
---|
183 | case 'b':
|
---|
184 | g_SerialPortCfg.uBaudRate = ValueUnion.u32;
|
---|
185 | break;
|
---|
186 | case 'p':
|
---|
187 | if (!RTStrICmp(ValueUnion.psz, "none"))
|
---|
188 | g_SerialPortCfg.enmParity = RTSERIALPORTPARITY_NONE;
|
---|
189 | else if (!RTStrICmp(ValueUnion.psz, "even"))
|
---|
190 | g_SerialPortCfg.enmParity = RTSERIALPORTPARITY_EVEN;
|
---|
191 | else if (!RTStrICmp(ValueUnion.psz, "odd"))
|
---|
192 | g_SerialPortCfg.enmParity = RTSERIALPORTPARITY_ODD;
|
---|
193 | else if (!RTStrICmp(ValueUnion.psz, "mark"))
|
---|
194 | g_SerialPortCfg.enmParity = RTSERIALPORTPARITY_MARK;
|
---|
195 | else if (!RTStrICmp(ValueUnion.psz, "space"))
|
---|
196 | g_SerialPortCfg.enmParity = RTSERIALPORTPARITY_SPACE;
|
---|
197 | else
|
---|
198 | {
|
---|
199 | RTPrintf("Unknown parity \"%s\" given\n", ValueUnion.psz);
|
---|
200 | return RTEXITCODE_FAILURE;
|
---|
201 | }
|
---|
202 | break;
|
---|
203 | case 'c':
|
---|
204 | if (!RTStrICmp(ValueUnion.psz, "5"))
|
---|
205 | g_SerialPortCfg.enmDataBitCount = RTSERIALPORTDATABITS_5BITS;
|
---|
206 | else if (!RTStrICmp(ValueUnion.psz, "6"))
|
---|
207 | g_SerialPortCfg.enmDataBitCount = RTSERIALPORTDATABITS_6BITS;
|
---|
208 | else if (!RTStrICmp(ValueUnion.psz, "7"))
|
---|
209 | g_SerialPortCfg.enmDataBitCount = RTSERIALPORTDATABITS_7BITS;
|
---|
210 | else if (!RTStrICmp(ValueUnion.psz, "8"))
|
---|
211 | g_SerialPortCfg.enmDataBitCount = RTSERIALPORTDATABITS_8BITS;
|
---|
212 | else
|
---|
213 | {
|
---|
214 | RTPrintf("Unknown data bitcount \"%s\" given\n", ValueUnion.psz);
|
---|
215 | return RTEXITCODE_FAILURE;
|
---|
216 | }
|
---|
217 | break;
|
---|
218 | case 's':
|
---|
219 | if (!RTStrICmp(ValueUnion.psz, "1"))
|
---|
220 | g_SerialPortCfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONE;
|
---|
221 | else if (!RTStrICmp(ValueUnion.psz, "1.5"))
|
---|
222 | g_SerialPortCfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONEPOINTFIVE;
|
---|
223 | else if (!RTStrICmp(ValueUnion.psz, "2"))
|
---|
224 | g_SerialPortCfg.enmStopBitCount = RTSERIALPORTSTOPBITS_TWO;
|
---|
225 | else
|
---|
226 | {
|
---|
227 | RTPrintf("Unknown stop bitcount \"%s\" given\n", ValueUnion.psz);
|
---|
228 | return RTEXITCODE_FAILURE;
|
---|
229 | }
|
---|
230 | break;
|
---|
231 | default:
|
---|
232 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Start testing.
|
---|
238 | */
|
---|
239 | RTTestBanner(g_hTest);
|
---|
240 |
|
---|
241 | if (pszDevice)
|
---|
242 | {
|
---|
243 | uint32_t fFlags = RTSERIALPORT_OPEN_F_READ
|
---|
244 | | RTSERIALPORT_OPEN_F_WRITE
|
---|
245 | | RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING;
|
---|
246 |
|
---|
247 | RTTestSub(g_hTest, "Opening device");
|
---|
248 | rc = RTSerialPortOpen(&g_hSerialPort, pszDevice, fFlags);
|
---|
249 | if (RT_SUCCESS(rc))
|
---|
250 | {
|
---|
251 | if (pszDeviceLoopback)
|
---|
252 | {
|
---|
253 | RTTestSub(g_hTest, "Opening loopback device");
|
---|
254 | rc = RTSerialPortOpen(&g_hSerialPortLoopback, pszDeviceLoopback, fFlags);
|
---|
255 | if (RT_FAILURE(rc))
|
---|
256 | RTTestFailed(g_hTest, "Opening loopback device \"%s\" failed with %Rrc\n", pszDevice, rc);
|
---|
257 | }
|
---|
258 |
|
---|
259 | if (RT_SUCCESS(rc))
|
---|
260 | {
|
---|
261 | RTTestSub(g_hTest, "Setting serial port configuration");
|
---|
262 |
|
---|
263 | rc = RTSerialPortCfgSet(g_hSerialPort, &g_SerialPortCfg ,NULL);
|
---|
264 | if (RT_SUCCESS(rc))
|
---|
265 | {
|
---|
266 | if (pszDeviceLoopback)
|
---|
267 | {
|
---|
268 | RTTestSub(g_hTest, "Setting serial port configuration for loopback device");
|
---|
269 | rc = RTSerialPortCfgSet(g_hSerialPortLoopback,&g_SerialPortCfg ,NULL);
|
---|
270 | if (RT_FAILURE(rc))
|
---|
271 | RTTestFailed(g_hTest, "Setting configuration of loopback device \"%s\" failed with %Rrc\n", pszDevice, rc);
|
---|
272 | }
|
---|
273 |
|
---|
274 | if (RT_SUCCESS(rc))
|
---|
275 | rc = serialTestRun();
|
---|
276 | }
|
---|
277 | else
|
---|
278 | RTTestFailed(g_hTest, "Setting configuration of device \"%s\" failed with %Rrc\n", pszDevice, rc);
|
---|
279 |
|
---|
280 | RTSerialPortClose(g_hSerialPort);
|
---|
281 | }
|
---|
282 | }
|
---|
283 | else
|
---|
284 | RTTestFailed(g_hTest, "Opening device \"%s\" failed with %Rrc\n", pszDevice, rc);
|
---|
285 | }
|
---|
286 | else
|
---|
287 | RTTestFailed(g_hTest, "No device given on command line\n");
|
---|
288 |
|
---|
289 | RTEXITCODE rcExit = RTTestSummaryAndDestroy(g_hTest);
|
---|
290 | return rcExit;
|
---|
291 | }
|
---|
292 |
|
---|