1 | /* $Id: SerialTest.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SerialTest - Serial port testing utility.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. 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/errcore.h>
|
---|
32 | #include <iprt/getopt.h>
|
---|
33 | #include <iprt/mem.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 | #include <iprt/param.h>
|
---|
36 | #include <iprt/process.h>
|
---|
37 | #include <iprt/rand.h>
|
---|
38 | #include <iprt/serialport.h>
|
---|
39 | #include <iprt/stream.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/test.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /*********************************************************************************************************************************
|
---|
45 | * Defined Constants And Macros *
|
---|
46 | *********************************************************************************************************************************/
|
---|
47 |
|
---|
48 | /** Number of times to toggle the status lines during the test. */
|
---|
49 | #define SERIALTEST_STS_LINE_TOGGLE_COUNT 100
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Structures and Typedefs *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Serial test mode.
|
---|
59 | */
|
---|
60 | typedef enum SERIALTESTMODE
|
---|
61 | {
|
---|
62 | /** Invalid mode. */
|
---|
63 | SERIALTESTMODE_INVALID = 0,
|
---|
64 | /** Serial port is looped back to itself */
|
---|
65 | SERIALTESTMODE_LOOPBACK,
|
---|
66 | /** A secondary serial port is used with a null modem cable in between. */
|
---|
67 | SERIALTESTMODE_SECONDARY,
|
---|
68 | /** The serial port is connected externally over which we have no control. */
|
---|
69 | SERIALTESTMODE_EXTERNAL,
|
---|
70 | /** Usual 32bit hack. */
|
---|
71 | SERIALTESTMODE_32BIT_HACK = 0x7fffffff
|
---|
72 | } SERIALTESTMODE;
|
---|
73 | /** Pointer to a serial test mode. */
|
---|
74 | typedef SERIALTESTMODE *PSERIALTESTMDOE;
|
---|
75 |
|
---|
76 | /** Pointer to the serial test data instance. */
|
---|
77 | typedef struct SERIALTEST *PSERIALTEST;
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Test callback function.
|
---|
81 | *
|
---|
82 | * @returns IPRT status code.
|
---|
83 | * @param pSerialTest The serial test instance data.
|
---|
84 | */
|
---|
85 | typedef DECLCALLBACK(int) FNSERIALTESTRUN(PSERIALTEST pSerialTest);
|
---|
86 | /** Pointer to the serial test callback. */
|
---|
87 | typedef FNSERIALTESTRUN *PFNSERIALTESTRUN;
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * The serial test instance data.
|
---|
92 | */
|
---|
93 | typedef struct SERIALTEST
|
---|
94 | {
|
---|
95 | /** The assigned test handle. */
|
---|
96 | RTTEST hTest;
|
---|
97 | /** The assigned serial port. */
|
---|
98 | RTSERIALPORT hSerialPort;
|
---|
99 | /** The currently active config. */
|
---|
100 | PCRTSERIALPORTCFG pSerialCfg;
|
---|
101 | } SERIALTEST;
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Test descriptor.
|
---|
106 | */
|
---|
107 | typedef struct SERIALTESTDESC
|
---|
108 | {
|
---|
109 | /** Test ID. */
|
---|
110 | const char *pszId;
|
---|
111 | /** Test description. */
|
---|
112 | const char *pszDesc;
|
---|
113 | /** Test run callback. */
|
---|
114 | PFNSERIALTESTRUN pfnRun;
|
---|
115 | } SERIALTESTDESC;
|
---|
116 | /** Pointer to a test descriptor. */
|
---|
117 | typedef SERIALTESTDESC *PSERIALTESTDESC;
|
---|
118 | /** Pointer to a constant test descriptor. */
|
---|
119 | typedef const SERIALTESTDESC *PCSERIALTESTDESC;
|
---|
120 |
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * TX/RX buffer containing a simple counter.
|
---|
124 | */
|
---|
125 | typedef struct SERIALTESTTXRXBUFCNT
|
---|
126 | {
|
---|
127 | /** The current counter value. */
|
---|
128 | uint32_t iCnt;
|
---|
129 | /** Number of bytes left to receive/transmit. */
|
---|
130 | size_t cbTxRxLeft;
|
---|
131 | /** The offset into the buffer to receive to/send from. */
|
---|
132 | size_t offBuf;
|
---|
133 | /** Maximum size to send/receive before processing is needed again. */
|
---|
134 | size_t cbTxRxMax;
|
---|
135 | /** The data buffer. */
|
---|
136 | uint8_t abBuf[_1K];
|
---|
137 | } SERIALTESTTXRXBUFCNT;
|
---|
138 | /** Pointer to a TX/RX buffer. */
|
---|
139 | typedef SERIALTESTTXRXBUFCNT *PSERIALTESTTXRXBUFCNT;
|
---|
140 |
|
---|
141 |
|
---|
142 | /*********************************************************************************************************************************
|
---|
143 | * Global Variables *
|
---|
144 | *********************************************************************************************************************************/
|
---|
145 |
|
---|
146 |
|
---|
147 | /** Command line parameters */
|
---|
148 | static const RTGETOPTDEF g_aCmdOptions[] =
|
---|
149 | {
|
---|
150 | {"--device", 'd', RTGETOPT_REQ_STRING },
|
---|
151 | {"--baudrate", 'b', RTGETOPT_REQ_UINT32 },
|
---|
152 | {"--parity", 'p', RTGETOPT_REQ_STRING },
|
---|
153 | {"--databits", 'c', RTGETOPT_REQ_UINT32 },
|
---|
154 | {"--stopbits", 's', RTGETOPT_REQ_STRING },
|
---|
155 | {"--mode", 'm', RTGETOPT_REQ_STRING },
|
---|
156 | {"--secondarydevice", 'l', RTGETOPT_REQ_STRING },
|
---|
157 | {"--tests", 't', RTGETOPT_REQ_STRING },
|
---|
158 | {"--txbytes", 'x', RTGETOPT_REQ_UINT32 },
|
---|
159 | {"--verbose", 'v', RTGETOPT_REQ_NOTHING},
|
---|
160 | {"--help", 'h', RTGETOPT_REQ_NOTHING}
|
---|
161 | };
|
---|
162 |
|
---|
163 |
|
---|
164 | static DECLCALLBACK(int) serialTestRunReadWrite(PSERIALTEST pSerialTest);
|
---|
165 | static DECLCALLBACK(int) serialTestRunWrite(PSERIALTEST pSerialTest);
|
---|
166 | static DECLCALLBACK(int) serialTestRunStsLines(PSERIALTEST pSerialTest);
|
---|
167 |
|
---|
168 | /** Implemented tests. */
|
---|
169 | static const SERIALTESTDESC g_aSerialTests[] =
|
---|
170 | {
|
---|
171 | {"readwrite", "Simple Read/Write test on the same serial port", serialTestRunReadWrite },
|
---|
172 | {"write", "Simple write test (verification done somewhere else)", serialTestRunWrite },
|
---|
173 | {"stslines", "Testing the status line setting and receiving", serialTestRunStsLines }
|
---|
174 | };
|
---|
175 |
|
---|
176 | /** Verbosity value. */
|
---|
177 | static unsigned g_cVerbosity = 0;
|
---|
178 | /** The test handle. */
|
---|
179 | static RTTEST g_hTest = NIL_RTTEST;
|
---|
180 | /** The serial test mode. */
|
---|
181 | static SERIALTESTMODE g_enmMode = SERIALTESTMODE_LOOPBACK;
|
---|
182 | /** Random number generator. */
|
---|
183 | static RTRAND g_hRand = NIL_RTRAND;
|
---|
184 | /** The serial port handle. */
|
---|
185 | static RTSERIALPORT g_hSerialPort = NIL_RTSERIALPORT;
|
---|
186 | /** The loopback serial port handle if configured. */
|
---|
187 | static RTSERIALPORT g_hSerialPortSecondary = NIL_RTSERIALPORT;
|
---|
188 | /** Number of bytes to transmit for read/write tests. */
|
---|
189 | static size_t g_cbTx = _1M;
|
---|
190 | /** The config used. */
|
---|
191 | static RTSERIALPORTCFG g_SerialPortCfg =
|
---|
192 | {
|
---|
193 | /* uBaudRate */
|
---|
194 | 115200,
|
---|
195 | /* enmParity */
|
---|
196 | RTSERIALPORTPARITY_NONE,
|
---|
197 | /* enmDataBitCount */
|
---|
198 | RTSERIALPORTDATABITS_8BITS,
|
---|
199 | /* enmStopBitCount */
|
---|
200 | RTSERIALPORTSTOPBITS_ONE
|
---|
201 | };
|
---|
202 |
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Initializes a TX buffer.
|
---|
206 | *
|
---|
207 | * @returns nothing.
|
---|
208 | * @param pSerBuf The serial buffer to initialize.
|
---|
209 | * @param cbTx Maximum number of bytes to transmit.
|
---|
210 | */
|
---|
211 | static void serialTestTxBufInit(PSERIALTESTTXRXBUFCNT pSerBuf, size_t cbTx)
|
---|
212 | {
|
---|
213 | pSerBuf->iCnt = 0;
|
---|
214 | pSerBuf->offBuf = 0;
|
---|
215 | pSerBuf->cbTxRxMax = 0;
|
---|
216 | pSerBuf->cbTxRxLeft = cbTx;
|
---|
217 | RT_ZERO(pSerBuf->abBuf);
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Initializes a RX buffer.
|
---|
223 | *
|
---|
224 | * @returns nothing.
|
---|
225 | * @param pSerBuf The serial buffer to initialize.
|
---|
226 | * @param cbRx Maximum number of bytes to receive.
|
---|
227 | */
|
---|
228 | static void serialTestRxBufInit(PSERIALTESTTXRXBUFCNT pSerBuf, size_t cbRx)
|
---|
229 | {
|
---|
230 | pSerBuf->iCnt = 0;
|
---|
231 | pSerBuf->offBuf = 0;
|
---|
232 | pSerBuf->cbTxRxMax = sizeof(pSerBuf->abBuf);
|
---|
233 | pSerBuf->cbTxRxLeft = cbRx;
|
---|
234 | RT_ZERO(pSerBuf->abBuf);
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Prepares the given TX buffer with data for sending it out.
|
---|
240 | *
|
---|
241 | * @returns nothing.
|
---|
242 | * @param pSerBuf The TX buffer pointer.
|
---|
243 | */
|
---|
244 | static void serialTestTxBufPrepare(PSERIALTESTTXRXBUFCNT pSerBuf)
|
---|
245 | {
|
---|
246 | /* Move the data to the front to make room at the end to fill. */
|
---|
247 | if (pSerBuf->offBuf)
|
---|
248 | {
|
---|
249 | memmove(&pSerBuf->abBuf[0], &pSerBuf->abBuf[pSerBuf->offBuf], sizeof(pSerBuf->abBuf) - pSerBuf->offBuf);
|
---|
250 | pSerBuf->offBuf = 0;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /* Fill up with data. */
|
---|
254 | uint32_t offData = 0;
|
---|
255 | while (pSerBuf->cbTxRxMax + sizeof(uint32_t) <= sizeof(pSerBuf->abBuf))
|
---|
256 | {
|
---|
257 | pSerBuf->iCnt++;
|
---|
258 | *(uint32_t *)&pSerBuf->abBuf[pSerBuf->offBuf + offData] = pSerBuf->iCnt;
|
---|
259 | pSerBuf->cbTxRxMax += sizeof(uint32_t);
|
---|
260 | offData += sizeof(uint32_t);
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Sends a new batch of data from the TX buffer preapring new data if required.
|
---|
267 | *
|
---|
268 | * @returns IPRT status code.
|
---|
269 | * @param hSerialPort The serial port handle to send the data to.
|
---|
270 | * @param pSerBuf The TX buffer pointer.
|
---|
271 | */
|
---|
272 | static int serialTestTxBufSend(RTSERIALPORT hSerialPort, PSERIALTESTTXRXBUFCNT pSerBuf)
|
---|
273 | {
|
---|
274 | int rc = VINF_SUCCESS;
|
---|
275 |
|
---|
276 | if (pSerBuf->cbTxRxLeft)
|
---|
277 | {
|
---|
278 | if (!pSerBuf->cbTxRxMax)
|
---|
279 | serialTestTxBufPrepare(pSerBuf);
|
---|
280 |
|
---|
281 | size_t cbToWrite = RT_MIN(pSerBuf->cbTxRxMax, pSerBuf->cbTxRxLeft);
|
---|
282 | size_t cbWritten = 0;
|
---|
283 | rc = RTSerialPortWriteNB(hSerialPort, &pSerBuf->abBuf[pSerBuf->offBuf], cbToWrite, &cbWritten);
|
---|
284 | if (RT_SUCCESS(rc))
|
---|
285 | {
|
---|
286 | pSerBuf->cbTxRxMax -= cbWritten;
|
---|
287 | pSerBuf->offBuf += cbWritten;
|
---|
288 | pSerBuf->cbTxRxLeft -= cbWritten;
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | return rc;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Receives dat from the given serial port into the supplied RX buffer and does some validity checking.
|
---|
298 | *
|
---|
299 | * @returns IPRT status code.
|
---|
300 | * @param hSerialPort The serial port handle to receive data from.
|
---|
301 | * @param pSerBuf The RX buffer pointer.
|
---|
302 | */
|
---|
303 | static int serialTestRxBufRecv(RTSERIALPORT hSerialPort, PSERIALTESTTXRXBUFCNT pSerBuf)
|
---|
304 | {
|
---|
305 | int rc = VINF_SUCCESS;
|
---|
306 |
|
---|
307 | if (pSerBuf->cbTxRxLeft)
|
---|
308 | {
|
---|
309 | size_t cbToRead = RT_MIN(pSerBuf->cbTxRxMax, pSerBuf->cbTxRxLeft);
|
---|
310 | size_t cbRead = 0;
|
---|
311 | rc = RTSerialPortReadNB(hSerialPort, &pSerBuf->abBuf[pSerBuf->offBuf], cbToRead, &cbRead);
|
---|
312 | if (RT_SUCCESS(rc))
|
---|
313 | {
|
---|
314 | pSerBuf->offBuf += cbRead;
|
---|
315 | pSerBuf->cbTxRxMax -= cbRead;
|
---|
316 | pSerBuf->cbTxRxLeft -= cbRead;
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | return rc;
|
---|
321 | }
|
---|
322 |
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Verifies the data in the given RX buffer for correct transmission.
|
---|
326 | *
|
---|
327 | * @returns Flag whether verification failed.
|
---|
328 | * @param hTest The test handle to report errors to.
|
---|
329 | * @param pSerBuf The RX buffer pointer.
|
---|
330 | * @param iCntTx The current TX counter value the RX buffer should never get ahead of.
|
---|
331 | */
|
---|
332 | static bool serialTestRxBufVerify(RTTEST hTest, PSERIALTESTTXRXBUFCNT pSerBuf, uint32_t iCntTx)
|
---|
333 | {
|
---|
334 | uint32_t offRx = 0;
|
---|
335 | bool fFailed = false;
|
---|
336 |
|
---|
337 | while (offRx + sizeof(uint32_t) < pSerBuf->offBuf)
|
---|
338 | {
|
---|
339 | uint32_t u32Val = *(uint32_t *)&pSerBuf->abBuf[offRx];
|
---|
340 | offRx += sizeof(uint32_t);
|
---|
341 |
|
---|
342 | if (RT_UNLIKELY(u32Val != ++pSerBuf->iCnt))
|
---|
343 | {
|
---|
344 | fFailed = true;
|
---|
345 | if (g_cVerbosity > 0)
|
---|
346 | RTTestFailed(hTest, "Data corruption/loss detected, expected counter value %u got %u\n",
|
---|
347 | pSerBuf->iCnt, u32Val);
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | if (RT_UNLIKELY(pSerBuf->iCnt > iCntTx))
|
---|
352 | {
|
---|
353 | fFailed = true;
|
---|
354 | RTTestFailed(hTest, "Overtook the send buffer, expected maximum counter value %u got %u\n",
|
---|
355 | iCntTx, pSerBuf->iCnt);
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* Remove processed data from the buffer and move the rest to the front. */
|
---|
359 | if (offRx)
|
---|
360 | {
|
---|
361 | memmove(&pSerBuf->abBuf[0], &pSerBuf->abBuf[offRx], sizeof(pSerBuf->abBuf) - offRx);
|
---|
362 | pSerBuf->offBuf -= offRx;
|
---|
363 | pSerBuf->cbTxRxMax += offRx;
|
---|
364 | }
|
---|
365 |
|
---|
366 | return fFailed;
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 | DECLINLINE(bool) serialTestRndTrue(void)
|
---|
371 | {
|
---|
372 | return RTRandAdvU32Ex(g_hRand, 0, 1) == 1;
|
---|
373 | }
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * Runs a simple read/write test.
|
---|
377 | *
|
---|
378 | * @returns IPRT status code.
|
---|
379 | * @param pSerialTest The serial test configuration.
|
---|
380 | */
|
---|
381 | static DECLCALLBACK(int) serialTestRunReadWrite(PSERIALTEST pSerialTest)
|
---|
382 | {
|
---|
383 | uint64_t tsStart = RTTimeNanoTS();
|
---|
384 | bool fFailed = false;
|
---|
385 | SERIALTESTTXRXBUFCNT SerBufTx;
|
---|
386 | SERIALTESTTXRXBUFCNT SerBufRx;
|
---|
387 |
|
---|
388 | serialTestTxBufInit(&SerBufTx, g_cbTx);
|
---|
389 | serialTestRxBufInit(&SerBufRx, g_cbTx);
|
---|
390 |
|
---|
391 | int rc = serialTestTxBufSend(pSerialTest->hSerialPort, &SerBufTx);
|
---|
392 | while ( RT_SUCCESS(rc)
|
---|
393 | && ( SerBufTx.cbTxRxLeft
|
---|
394 | || SerBufRx.cbTxRxLeft))
|
---|
395 | {
|
---|
396 | uint32_t fEvts = 0;
|
---|
397 | uint32_t fEvtsQuery = 0;
|
---|
398 | if (SerBufTx.cbTxRxLeft)
|
---|
399 | fEvtsQuery |= RTSERIALPORT_EVT_F_DATA_TX;
|
---|
400 | if (SerBufRx.cbTxRxLeft)
|
---|
401 | fEvtsQuery |= RTSERIALPORT_EVT_F_DATA_RX;
|
---|
402 |
|
---|
403 | rc = RTSerialPortEvtPoll(pSerialTest->hSerialPort, fEvtsQuery, &fEvts, RT_INDEFINITE_WAIT);
|
---|
404 | if (RT_FAILURE(rc))
|
---|
405 | break;
|
---|
406 |
|
---|
407 | if (fEvts & RTSERIALPORT_EVT_F_DATA_RX)
|
---|
408 | {
|
---|
409 | rc = serialTestRxBufRecv(pSerialTest->hSerialPort, &SerBufRx);
|
---|
410 | if (RT_FAILURE(rc))
|
---|
411 | break;
|
---|
412 |
|
---|
413 | bool fRes = serialTestRxBufVerify(pSerialTest->hTest, &SerBufRx, SerBufTx.iCnt);
|
---|
414 | if (fRes && !fFailed)
|
---|
415 | {
|
---|
416 | fFailed = true;
|
---|
417 | RTTestFailed(pSerialTest->hTest, "Data corruption/loss detected\n");
|
---|
418 | }
|
---|
419 | }
|
---|
420 | if ( RT_SUCCESS(rc)
|
---|
421 | && (fEvts & RTSERIALPORT_EVT_F_DATA_TX))
|
---|
422 | rc = serialTestTxBufSend(pSerialTest->hSerialPort, &SerBufTx);
|
---|
423 | }
|
---|
424 |
|
---|
425 | uint64_t tsRuntime = RTTimeNanoTS() - tsStart;
|
---|
426 | size_t cNsPerByte = tsRuntime / g_cbTx;
|
---|
427 | uint64_t cbBytesPerSec = RT_NS_1SEC / cNsPerByte;
|
---|
428 | RTTestValue(pSerialTest->hTest, "Throughput", cbBytesPerSec, RTTESTUNIT_BYTES_PER_SEC);
|
---|
429 |
|
---|
430 | return rc;
|
---|
431 | }
|
---|
432 |
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * Runs a simple write test without doing any verification.
|
---|
436 | *
|
---|
437 | * @returns IPRT status code.
|
---|
438 | * @param pSerialTest The serial test configuration.
|
---|
439 | */
|
---|
440 | static DECLCALLBACK(int) serialTestRunWrite(PSERIALTEST pSerialTest)
|
---|
441 | {
|
---|
442 | uint64_t tsStart = RTTimeNanoTS();
|
---|
443 | SERIALTESTTXRXBUFCNT SerBufTx;
|
---|
444 |
|
---|
445 | serialTestTxBufInit(&SerBufTx, g_cbTx);
|
---|
446 |
|
---|
447 | int rc = serialTestTxBufSend(pSerialTest->hSerialPort, &SerBufTx);
|
---|
448 | while ( RT_SUCCESS(rc)
|
---|
449 | && SerBufTx.cbTxRxLeft)
|
---|
450 | {
|
---|
451 | uint32_t fEvts = 0;
|
---|
452 |
|
---|
453 | rc = RTSerialPortEvtPoll(pSerialTest->hSerialPort, RTSERIALPORT_EVT_F_DATA_TX, &fEvts, RT_INDEFINITE_WAIT);
|
---|
454 | if (RT_FAILURE(rc))
|
---|
455 | break;
|
---|
456 |
|
---|
457 | if (fEvts & RTSERIALPORT_EVT_F_DATA_TX)
|
---|
458 | rc = serialTestTxBufSend(pSerialTest->hSerialPort, &SerBufTx);
|
---|
459 | }
|
---|
460 |
|
---|
461 | uint64_t tsRuntime = RTTimeNanoTS() - tsStart;
|
---|
462 | size_t cNsPerByte = tsRuntime / g_cbTx;
|
---|
463 | uint64_t cbBytesPerSec = RT_NS_1SEC / cNsPerByte;
|
---|
464 | RTTestValue(pSerialTest->hTest, "Throughput", cbBytesPerSec, RTTESTUNIT_BYTES_PER_SEC);
|
---|
465 |
|
---|
466 | return rc;
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | /**
|
---|
471 | * Tests setting status lines and getting notified about status line changes.
|
---|
472 | *
|
---|
473 | * @returns IPRT status code.
|
---|
474 | * @param pSerialTest The serial test configuration.
|
---|
475 | */
|
---|
476 | static DECLCALLBACK(int) serialTestRunStsLines(PSERIALTEST pSerialTest)
|
---|
477 | {
|
---|
478 | int rc = VINF_SUCCESS;
|
---|
479 |
|
---|
480 | if (g_enmMode == SERIALTESTMODE_LOOPBACK)
|
---|
481 | {
|
---|
482 | uint32_t fStsLinesQueriedOld = 0;
|
---|
483 |
|
---|
484 | rc = RTSerialPortChgStatusLines(pSerialTest->hSerialPort,
|
---|
485 | RTSERIALPORT_CHG_STS_LINES_F_RTS | RTSERIALPORT_CHG_STS_LINES_F_DTR,
|
---|
486 | 0);
|
---|
487 | if (RT_SUCCESS(rc))
|
---|
488 | {
|
---|
489 | rc = RTSerialPortQueryStatusLines(pSerialTest->hSerialPort, &fStsLinesQueriedOld);
|
---|
490 | if (RT_SUCCESS(rc))
|
---|
491 | {
|
---|
492 | /* Everything should be clear at this stage. */
|
---|
493 | if (!fStsLinesQueriedOld)
|
---|
494 | {
|
---|
495 | uint32_t fStsLinesSetOld = 0;
|
---|
496 |
|
---|
497 | for (uint32_t i = 0; i < SERIALTEST_STS_LINE_TOGGLE_COUNT; i++)
|
---|
498 | {
|
---|
499 | uint32_t fStsLinesSet = 0;
|
---|
500 | uint32_t fStsLinesClear = 0;
|
---|
501 |
|
---|
502 | /* Change RTS? */
|
---|
503 | if (serialTestRndTrue())
|
---|
504 | {
|
---|
505 | /* Clear, if set previously otherwise set it. */
|
---|
506 | if (fStsLinesSetOld & RTSERIALPORT_CHG_STS_LINES_F_RTS)
|
---|
507 | fStsLinesClear |= RTSERIALPORT_CHG_STS_LINES_F_RTS;
|
---|
508 | else
|
---|
509 | fStsLinesSet |= RTSERIALPORT_CHG_STS_LINES_F_RTS;
|
---|
510 | }
|
---|
511 |
|
---|
512 | /* Change DTR? */
|
---|
513 | if (serialTestRndTrue())
|
---|
514 | {
|
---|
515 | /* Clear, if set previously otherwise set it. */
|
---|
516 | if (fStsLinesSetOld & RTSERIALPORT_CHG_STS_LINES_F_DTR)
|
---|
517 | fStsLinesClear |= RTSERIALPORT_CHG_STS_LINES_F_DTR;
|
---|
518 | else
|
---|
519 | fStsLinesSet |= RTSERIALPORT_CHG_STS_LINES_F_DTR;
|
---|
520 | }
|
---|
521 |
|
---|
522 | rc = RTSerialPortChgStatusLines(pSerialTest->hSerialPort, fStsLinesClear, fStsLinesSet);
|
---|
523 | if (RT_FAILURE(rc))
|
---|
524 | {
|
---|
525 | RTTestFailed(g_hTest, "Changing status lines failed with %Rrc on iteration %u (fSet=%#x fClear=%#x)\n",
|
---|
526 | rc, i, fStsLinesSet, fStsLinesClear);
|
---|
527 | break;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /* Wait for status line monitor event. */
|
---|
531 | uint32_t fEvtsRecv = 0;
|
---|
532 | rc = RTSerialPortEvtPoll(pSerialTest->hSerialPort, RTSERIALPORT_EVT_F_STATUS_LINE_CHANGED,
|
---|
533 | &fEvtsRecv, RT_MS_1SEC);
|
---|
534 | if ( RT_FAILURE(rc)
|
---|
535 | && (rc != VERR_TIMEOUT && !fStsLinesSet && !fStsLinesClear))
|
---|
536 | {
|
---|
537 | RTTestFailed(g_hTest, "Waiting for status line change failed with %Rrc on iteration %u\n",
|
---|
538 | rc, i);
|
---|
539 | break;
|
---|
540 | }
|
---|
541 |
|
---|
542 | uint32_t fStsLinesQueried = 0;
|
---|
543 | rc = RTSerialPortQueryStatusLines(pSerialTest->hSerialPort, &fStsLinesQueried);
|
---|
544 | if (RT_FAILURE(rc))
|
---|
545 | {
|
---|
546 | RTTestFailed(g_hTest, "Querying status lines failed with %Rrc on iteration %u\n",
|
---|
547 | rc, i);
|
---|
548 | break;
|
---|
549 | }
|
---|
550 |
|
---|
551 | /* Compare expected and real result. */
|
---|
552 | if ( (fStsLinesQueried & RTSERIALPORT_STS_LINE_DSR)
|
---|
553 | != (fStsLinesQueriedOld & RTSERIALPORT_STS_LINE_DSR))
|
---|
554 | {
|
---|
555 | if ( (fStsLinesQueried & RTSERIALPORT_STS_LINE_DSR)
|
---|
556 | && !(fStsLinesSet & RTSERIALPORT_CHG_STS_LINES_F_DTR))
|
---|
557 | RTTestFailed(g_hTest, "DSR line got set when it shouldn't be on iteration %u\n", i);
|
---|
558 | else if ( !(fStsLinesQueried & RTSERIALPORT_STS_LINE_DSR)
|
---|
559 | && !(fStsLinesClear & RTSERIALPORT_CHG_STS_LINES_F_DTR))
|
---|
560 | RTTestFailed(g_hTest, "DSR line got cleared when it shouldn't be on iteration %u\n", i);
|
---|
561 | }
|
---|
562 | else if ( (fStsLinesSet & RTSERIALPORT_CHG_STS_LINES_F_DTR)
|
---|
563 | || (fStsLinesClear & RTSERIALPORT_CHG_STS_LINES_F_DTR))
|
---|
564 | RTTestFailed(g_hTest, "DSR line didn't change when it should have on iteration %u\n", i);
|
---|
565 |
|
---|
566 | if ( (fStsLinesQueried & RTSERIALPORT_STS_LINE_DCD)
|
---|
567 | != (fStsLinesQueriedOld & RTSERIALPORT_STS_LINE_DCD))
|
---|
568 | {
|
---|
569 | if ( (fStsLinesQueried & RTSERIALPORT_STS_LINE_DCD)
|
---|
570 | && !(fStsLinesSet & RTSERIALPORT_CHG_STS_LINES_F_DTR))
|
---|
571 | RTTestFailed(g_hTest, "DCD line got set when it shouldn't be on iteration %u\n", i);
|
---|
572 | else if ( !(fStsLinesQueried & RTSERIALPORT_STS_LINE_DCD)
|
---|
573 | && !(fStsLinesClear & RTSERIALPORT_CHG_STS_LINES_F_DTR))
|
---|
574 | RTTestFailed(g_hTest, "DCD line got cleared when it shouldn't be on iteration %u\n", i);
|
---|
575 | }
|
---|
576 | else if ( (fStsLinesSet & RTSERIALPORT_CHG_STS_LINES_F_DTR)
|
---|
577 | || (fStsLinesClear & RTSERIALPORT_CHG_STS_LINES_F_DTR))
|
---|
578 | RTTestFailed(g_hTest, "DCD line didn't change when it should have on iteration %u\n", i);
|
---|
579 |
|
---|
580 | if ( (fStsLinesQueried & RTSERIALPORT_STS_LINE_CTS)
|
---|
581 | != (fStsLinesQueriedOld & RTSERIALPORT_STS_LINE_CTS))
|
---|
582 | {
|
---|
583 | if ( (fStsLinesQueried & RTSERIALPORT_STS_LINE_CTS)
|
---|
584 | && !(fStsLinesSet & RTSERIALPORT_CHG_STS_LINES_F_RTS))
|
---|
585 | RTTestFailed(g_hTest, "CTS line got set when it shouldn't be on iteration %u\n", i);
|
---|
586 | else if ( !(fStsLinesQueried & RTSERIALPORT_STS_LINE_CTS)
|
---|
587 | && !(fStsLinesClear & RTSERIALPORT_CHG_STS_LINES_F_RTS))
|
---|
588 | RTTestFailed(g_hTest, "CTS line got cleared when it shouldn't be on iteration %u\n", i);
|
---|
589 | }
|
---|
590 | else if ( (fStsLinesSet & RTSERIALPORT_CHG_STS_LINES_F_RTS)
|
---|
591 | || (fStsLinesClear & RTSERIALPORT_CHG_STS_LINES_F_RTS))
|
---|
592 | RTTestFailed(g_hTest, "CTS line didn't change when it should have on iteration %u\n", i);
|
---|
593 |
|
---|
594 | if (RTTestErrorCount(g_hTest) > 0)
|
---|
595 | break;
|
---|
596 |
|
---|
597 | fStsLinesSetOld |= fStsLinesSet;
|
---|
598 | fStsLinesSetOld &= ~fStsLinesClear;
|
---|
599 | fStsLinesQueriedOld = fStsLinesQueried;
|
---|
600 | }
|
---|
601 | }
|
---|
602 | else
|
---|
603 | RTTestFailed(g_hTest, "Status lines active which should be clear (%#x, but expected %#x)\n",
|
---|
604 | fStsLinesQueriedOld, 0);
|
---|
605 | }
|
---|
606 | else
|
---|
607 | RTTestFailed(g_hTest, "Querying status lines failed with %Rrc\n", rc);
|
---|
608 | }
|
---|
609 | else
|
---|
610 | RTTestFailed(g_hTest, "Clearing status lines failed with %Rrc\n", rc);
|
---|
611 | }
|
---|
612 | else
|
---|
613 | rc = VERR_NOT_IMPLEMENTED;
|
---|
614 |
|
---|
615 | return rc;
|
---|
616 | }
|
---|
617 |
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Returns an array of test descriptors get from the given string.
|
---|
621 | *
|
---|
622 | * @returns Pointer to the array of test descriptors.
|
---|
623 | * @param pszTests The string containing the tests separated with ':'.
|
---|
624 | */
|
---|
625 | static PSERIALTESTDESC serialTestSelectFromCmdLine(const char *pszTests)
|
---|
626 | {
|
---|
627 | size_t cTests = 1;
|
---|
628 |
|
---|
629 | const char *pszNext = strchr(pszTests, ':');
|
---|
630 | while (pszNext)
|
---|
631 | {
|
---|
632 | pszNext++;
|
---|
633 | cTests++;
|
---|
634 | pszNext = strchr(pszNext, ':');
|
---|
635 | }
|
---|
636 |
|
---|
637 | PSERIALTESTDESC paTests = (PSERIALTESTDESC)RTMemAllocZ((cTests + 1) * sizeof(SERIALTESTDESC));
|
---|
638 | if (RT_LIKELY(paTests))
|
---|
639 | {
|
---|
640 | uint32_t iTest = 0;
|
---|
641 |
|
---|
642 | pszNext = strchr(pszTests, ':');
|
---|
643 | while (pszNext)
|
---|
644 | {
|
---|
645 | bool fFound = false;
|
---|
646 |
|
---|
647 | pszNext++; /* Skip : character. */
|
---|
648 |
|
---|
649 | for (unsigned i = 0; i < RT_ELEMENTS(g_aSerialTests); i++)
|
---|
650 | {
|
---|
651 | if (!RTStrNICmp(pszTests, g_aSerialTests[i].pszId, pszNext - pszTests - 1))
|
---|
652 | {
|
---|
653 | memcpy(&paTests[iTest], &g_aSerialTests[i], sizeof(SERIALTESTDESC));
|
---|
654 | fFound = true;
|
---|
655 | break;
|
---|
656 | }
|
---|
657 | }
|
---|
658 |
|
---|
659 | if (RT_UNLIKELY(!fFound))
|
---|
660 | {
|
---|
661 | RTPrintf("Testcase \"%.*s\" not known\n", pszNext - pszTests - 1, pszTests);
|
---|
662 | RTMemFree(paTests);
|
---|
663 | return NULL;
|
---|
664 | }
|
---|
665 |
|
---|
666 | pszTests = pszNext;
|
---|
667 | pszNext = strchr(pszTests, ':');
|
---|
668 | }
|
---|
669 |
|
---|
670 | /* Fill last descriptor. */
|
---|
671 | bool fFound = false;
|
---|
672 | for (unsigned i = 0; i < RT_ELEMENTS(g_aSerialTests); i++)
|
---|
673 | {
|
---|
674 | if (!RTStrICmp(pszTests, g_aSerialTests[i].pszId))
|
---|
675 | {
|
---|
676 | memcpy(&paTests[iTest], &g_aSerialTests[i], sizeof(SERIALTESTDESC));
|
---|
677 | fFound = true;
|
---|
678 | break;
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | if (RT_UNLIKELY(!fFound))
|
---|
683 | {
|
---|
684 | RTPrintf("Testcase \"%s\" not known\n", pszTests);
|
---|
685 | RTMemFree(paTests);
|
---|
686 | paTests = NULL;
|
---|
687 | }
|
---|
688 | }
|
---|
689 | else
|
---|
690 | RTPrintf("Failed to allocate test descriptors for %u selected tests\n", cTests);
|
---|
691 |
|
---|
692 | return paTests;
|
---|
693 | }
|
---|
694 |
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Shows tool usage text.
|
---|
698 | */
|
---|
699 | static void serialTestUsage(PRTSTREAM pStrm)
|
---|
700 | {
|
---|
701 | char szExec[RTPATH_MAX];
|
---|
702 | RTStrmPrintf(pStrm, "usage: %s [options]\n",
|
---|
703 | RTPathFilename(RTProcGetExecutablePath(szExec, sizeof(szExec))));
|
---|
704 | RTStrmPrintf(pStrm, "\n");
|
---|
705 | RTStrmPrintf(pStrm, "options: \n");
|
---|
706 |
|
---|
707 |
|
---|
708 | for (unsigned i = 0; i < RT_ELEMENTS(g_aCmdOptions); i++)
|
---|
709 | {
|
---|
710 | const char *pszHelp;
|
---|
711 | switch (g_aCmdOptions[i].iShort)
|
---|
712 | {
|
---|
713 | case 'h':
|
---|
714 | pszHelp = "Displays this help and exit";
|
---|
715 | break;
|
---|
716 | case 'd':
|
---|
717 | pszHelp = "Use the specified serial port device";
|
---|
718 | break;
|
---|
719 | case 'b':
|
---|
720 | pszHelp = "Use the given baudrate";
|
---|
721 | break;
|
---|
722 | case 'p':
|
---|
723 | pszHelp = "Use the given parity, valid modes are: none, even, odd, mark, space";
|
---|
724 | break;
|
---|
725 | case 'c':
|
---|
726 | pszHelp = "Use the given data bitcount, valid are: 5, 6, 7, 8";
|
---|
727 | break;
|
---|
728 | case 's':
|
---|
729 | pszHelp = "Use the given stop bitcount, valid are: 1, 1.5, 2";
|
---|
730 | break;
|
---|
731 | case 'm':
|
---|
732 | pszHelp = "Mode of the serial port, valid are: loopback, secondary, external";
|
---|
733 | break;
|
---|
734 | case 'l':
|
---|
735 | pszHelp = "Use the given serial port device as the secondary device";
|
---|
736 | break;
|
---|
737 | case 't':
|
---|
738 | pszHelp = "The tests to run separated by ':'";
|
---|
739 | break;
|
---|
740 | case 'x':
|
---|
741 | pszHelp = "Number of bytes to transmit during read/write tests";
|
---|
742 | break;
|
---|
743 | default:
|
---|
744 | pszHelp = "Option undocumented";
|
---|
745 | break;
|
---|
746 | }
|
---|
747 | char szOpt[256];
|
---|
748 | RTStrPrintf(szOpt, sizeof(szOpt), "%s, -%c", g_aCmdOptions[i].pszLong, g_aCmdOptions[i].iShort);
|
---|
749 | RTStrmPrintf(pStrm, " %-30s%s\n", szOpt, pszHelp);
|
---|
750 | }
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 | int main(int argc, char *argv[])
|
---|
755 | {
|
---|
756 | /*
|
---|
757 | * Init IPRT and globals.
|
---|
758 | */
|
---|
759 | int rc = RTTestInitAndCreate("SerialTest", &g_hTest);
|
---|
760 | if (rc)
|
---|
761 | return rc;
|
---|
762 |
|
---|
763 | /*
|
---|
764 | * Default values.
|
---|
765 | */
|
---|
766 | const char *pszDevice = NULL;
|
---|
767 | const char *pszDeviceSecondary = NULL;
|
---|
768 | PSERIALTESTDESC paTests = NULL;
|
---|
769 |
|
---|
770 | RTGETOPTUNION ValueUnion;
|
---|
771 | RTGETOPTSTATE GetState;
|
---|
772 | RTGetOptInit(&GetState, argc, argv, g_aCmdOptions, RT_ELEMENTS(g_aCmdOptions), 1, 0 /* fFlags */);
|
---|
773 | while ((rc = RTGetOpt(&GetState, &ValueUnion)))
|
---|
774 | {
|
---|
775 | switch (rc)
|
---|
776 | {
|
---|
777 | case 'h':
|
---|
778 | serialTestUsage(g_pStdOut);
|
---|
779 | return RTEXITCODE_SUCCESS;
|
---|
780 | case 'v':
|
---|
781 | g_cVerbosity++;
|
---|
782 | break;
|
---|
783 | case 'd':
|
---|
784 | pszDevice = ValueUnion.psz;
|
---|
785 | break;
|
---|
786 | case 'l':
|
---|
787 | pszDeviceSecondary = ValueUnion.psz;
|
---|
788 | break;
|
---|
789 | case 'b':
|
---|
790 | g_SerialPortCfg.uBaudRate = ValueUnion.u32;
|
---|
791 | break;
|
---|
792 | case 'p':
|
---|
793 | if (!RTStrICmp(ValueUnion.psz, "none"))
|
---|
794 | g_SerialPortCfg.enmParity = RTSERIALPORTPARITY_NONE;
|
---|
795 | else if (!RTStrICmp(ValueUnion.psz, "even"))
|
---|
796 | g_SerialPortCfg.enmParity = RTSERIALPORTPARITY_EVEN;
|
---|
797 | else if (!RTStrICmp(ValueUnion.psz, "odd"))
|
---|
798 | g_SerialPortCfg.enmParity = RTSERIALPORTPARITY_ODD;
|
---|
799 | else if (!RTStrICmp(ValueUnion.psz, "mark"))
|
---|
800 | g_SerialPortCfg.enmParity = RTSERIALPORTPARITY_MARK;
|
---|
801 | else if (!RTStrICmp(ValueUnion.psz, "space"))
|
---|
802 | g_SerialPortCfg.enmParity = RTSERIALPORTPARITY_SPACE;
|
---|
803 | else
|
---|
804 | {
|
---|
805 | RTPrintf("Unknown parity \"%s\" given\n", ValueUnion.psz);
|
---|
806 | return RTEXITCODE_FAILURE;
|
---|
807 | }
|
---|
808 | break;
|
---|
809 | case 'c':
|
---|
810 | if (ValueUnion.u32 == 5)
|
---|
811 | g_SerialPortCfg.enmDataBitCount = RTSERIALPORTDATABITS_5BITS;
|
---|
812 | else if (ValueUnion.u32 == 6)
|
---|
813 | g_SerialPortCfg.enmDataBitCount = RTSERIALPORTDATABITS_6BITS;
|
---|
814 | else if (ValueUnion.u32 == 7)
|
---|
815 | g_SerialPortCfg.enmDataBitCount = RTSERIALPORTDATABITS_7BITS;
|
---|
816 | else if (ValueUnion.u32 == 8)
|
---|
817 | g_SerialPortCfg.enmDataBitCount = RTSERIALPORTDATABITS_8BITS;
|
---|
818 | else
|
---|
819 | {
|
---|
820 | RTPrintf("Unknown data bitcount \"%u\" given\n", ValueUnion.u32);
|
---|
821 | return RTEXITCODE_FAILURE;
|
---|
822 | }
|
---|
823 | break;
|
---|
824 | case 's':
|
---|
825 | if (!RTStrICmp(ValueUnion.psz, "1"))
|
---|
826 | g_SerialPortCfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONE;
|
---|
827 | else if (!RTStrICmp(ValueUnion.psz, "1.5"))
|
---|
828 | g_SerialPortCfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONEPOINTFIVE;
|
---|
829 | else if (!RTStrICmp(ValueUnion.psz, "2"))
|
---|
830 | g_SerialPortCfg.enmStopBitCount = RTSERIALPORTSTOPBITS_TWO;
|
---|
831 | else
|
---|
832 | {
|
---|
833 | RTPrintf("Unknown stop bitcount \"%s\" given\n", ValueUnion.psz);
|
---|
834 | return RTEXITCODE_FAILURE;
|
---|
835 | }
|
---|
836 | break;
|
---|
837 | case 'm':
|
---|
838 | if (!RTStrICmp(ValueUnion.psz, "loopback"))
|
---|
839 | g_enmMode = SERIALTESTMODE_LOOPBACK;
|
---|
840 | else if (!RTStrICmp(ValueUnion.psz, "secondary"))
|
---|
841 | g_enmMode = SERIALTESTMODE_SECONDARY;
|
---|
842 | else if (!RTStrICmp(ValueUnion.psz, "external"))
|
---|
843 | g_enmMode = SERIALTESTMODE_EXTERNAL;
|
---|
844 | else
|
---|
845 | {
|
---|
846 | RTPrintf("Unknown serial test mode \"%s\" given\n", ValueUnion.psz);
|
---|
847 | return RTEXITCODE_FAILURE;
|
---|
848 | }
|
---|
849 | break;
|
---|
850 | case 't':
|
---|
851 | paTests = serialTestSelectFromCmdLine(ValueUnion.psz);
|
---|
852 | if (!paTests)
|
---|
853 | return RTEXITCODE_FAILURE;
|
---|
854 | break;
|
---|
855 | case 'x':
|
---|
856 | g_cbTx = ValueUnion.u32;
|
---|
857 | break;
|
---|
858 | default:
|
---|
859 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
860 | }
|
---|
861 | }
|
---|
862 |
|
---|
863 | if (g_enmMode == SERIALTESTMODE_SECONDARY && !pszDeviceSecondary)
|
---|
864 | {
|
---|
865 | RTPrintf("Mode set to secondary device but no secondary device given\n");
|
---|
866 | return RTEXITCODE_FAILURE;
|
---|
867 | }
|
---|
868 |
|
---|
869 | if (!paTests)
|
---|
870 | {
|
---|
871 | /* Select all. */
|
---|
872 | paTests = (PSERIALTESTDESC)RTMemAllocZ((RT_ELEMENTS(g_aSerialTests) + 1) * sizeof(SERIALTESTDESC));
|
---|
873 | if (RT_UNLIKELY(!paTests))
|
---|
874 | {
|
---|
875 | RTPrintf("Failed to allocate memory for test descriptors\n");
|
---|
876 | return RTEXITCODE_FAILURE;
|
---|
877 | }
|
---|
878 | memcpy(paTests, &g_aSerialTests[0], RT_ELEMENTS(g_aSerialTests) * sizeof(SERIALTESTDESC));
|
---|
879 | }
|
---|
880 |
|
---|
881 | rc = RTRandAdvCreateParkMiller(&g_hRand);
|
---|
882 | if (RT_FAILURE(rc))
|
---|
883 | {
|
---|
884 | RTPrintf("Failed to create random number generator: %Rrc\n", rc);
|
---|
885 | return RTEXITCODE_FAILURE;
|
---|
886 | }
|
---|
887 |
|
---|
888 | rc = RTRandAdvSeed(g_hRand, UINT64_C(0x123456789abcdef));
|
---|
889 | AssertRC(rc);
|
---|
890 |
|
---|
891 | /*
|
---|
892 | * Start testing.
|
---|
893 | */
|
---|
894 | RTTestBanner(g_hTest);
|
---|
895 |
|
---|
896 | if (pszDevice)
|
---|
897 | {
|
---|
898 | uint32_t fFlags = RTSERIALPORT_OPEN_F_READ
|
---|
899 | | RTSERIALPORT_OPEN_F_WRITE
|
---|
900 | | RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING;
|
---|
901 |
|
---|
902 | RTTestSub(g_hTest, "Opening device");
|
---|
903 | rc = RTSerialPortOpen(&g_hSerialPort, pszDevice, fFlags);
|
---|
904 | if (RT_SUCCESS(rc))
|
---|
905 | {
|
---|
906 | if (g_enmMode == SERIALTESTMODE_SECONDARY)
|
---|
907 | {
|
---|
908 | RTTestSub(g_hTest, "Opening secondary device");
|
---|
909 | rc = RTSerialPortOpen(&g_hSerialPortSecondary, pszDeviceSecondary, fFlags);
|
---|
910 | if (RT_FAILURE(rc))
|
---|
911 | RTTestFailed(g_hTest, "Opening secondary device \"%s\" failed with %Rrc\n", pszDevice, rc);
|
---|
912 | }
|
---|
913 |
|
---|
914 | if (RT_SUCCESS(rc))
|
---|
915 | {
|
---|
916 | RTTestSub(g_hTest, "Setting serial port configuration");
|
---|
917 |
|
---|
918 | rc = RTSerialPortCfgSet(g_hSerialPort, &g_SerialPortCfg ,NULL);
|
---|
919 | if (RT_SUCCESS(rc))
|
---|
920 | {
|
---|
921 | if (g_enmMode == SERIALTESTMODE_SECONDARY)
|
---|
922 | {
|
---|
923 | RTTestSub(g_hTest, "Setting serial port configuration for secondary device");
|
---|
924 | rc = RTSerialPortCfgSet(g_hSerialPortSecondary, &g_SerialPortCfg, NULL);
|
---|
925 | if (RT_FAILURE(rc))
|
---|
926 | RTTestFailed(g_hTest, "Setting configuration of secondary device \"%s\" failed with %Rrc\n", pszDevice, rc);
|
---|
927 | }
|
---|
928 |
|
---|
929 | if (RT_SUCCESS(rc))
|
---|
930 | {
|
---|
931 | SERIALTEST Test;
|
---|
932 | PSERIALTESTDESC pTest = &paTests[0];
|
---|
933 |
|
---|
934 | Test.hTest = g_hTest;
|
---|
935 | Test.hSerialPort = g_hSerialPort;
|
---|
936 | Test.pSerialCfg = &g_SerialPortCfg;
|
---|
937 |
|
---|
938 | while (pTest->pszId)
|
---|
939 | {
|
---|
940 | RTTestSub(g_hTest, pTest->pszDesc);
|
---|
941 | rc = pTest->pfnRun(&Test);
|
---|
942 | if ( RT_FAILURE(rc)
|
---|
943 | || RTTestErrorCount(g_hTest) > 0)
|
---|
944 | RTTestFailed(g_hTest, "Running test \"%s\" failed (%Rrc, cErrors=%u)\n",
|
---|
945 | pTest->pszId, rc, RTTestErrorCount(g_hTest));
|
---|
946 |
|
---|
947 | RTTestSubDone(g_hTest);
|
---|
948 | pTest++;
|
---|
949 | }
|
---|
950 | }
|
---|
951 | }
|
---|
952 | else
|
---|
953 | RTTestFailed(g_hTest, "Setting configuration of device \"%s\" failed with %Rrc\n", pszDevice, rc);
|
---|
954 |
|
---|
955 | RTSerialPortClose(g_hSerialPort);
|
---|
956 | }
|
---|
957 | }
|
---|
958 | else
|
---|
959 | RTTestFailed(g_hTest, "Opening device \"%s\" failed with %Rrc\n", pszDevice, rc);
|
---|
960 | }
|
---|
961 | else
|
---|
962 | RTTestFailed(g_hTest, "No device given on command line\n");
|
---|
963 |
|
---|
964 | RTRandAdvDestroy(g_hRand);
|
---|
965 | RTMemFree(paTests);
|
---|
966 | RTEXITCODE rcExit = RTTestSummaryAndDestroy(g_hTest);
|
---|
967 | return rcExit;
|
---|
968 | }
|
---|
969 |
|
---|