1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2010. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include "tmacros.h" |
---|
15 | #define TTYDEFCHARS |
---|
16 | #include <termios.h> |
---|
17 | #include <rtems/libcsupport.h> |
---|
18 | #include <rtems/malloc.h> |
---|
19 | #include <rtems/termiostypes.h> |
---|
20 | #include <fcntl.h> |
---|
21 | #include <limits.h> |
---|
22 | #include <unistd.h> |
---|
23 | #include <sys/errno.h> |
---|
24 | #include <sys/stat.h> |
---|
25 | |
---|
26 | const char rtems_test_name[] = "TERMIOS 1"; |
---|
27 | |
---|
28 | /* rtems_termios_baud_t is a typedefs to int32_t */ |
---|
29 | #define PRIdrtems_termios_baud_t PRId32 |
---|
30 | |
---|
31 | /* |
---|
32 | * Termios Test Driver |
---|
33 | */ |
---|
34 | #include "termios_testdriver.h" |
---|
35 | |
---|
36 | static const rtems_driver_address_table test_driver = |
---|
37 | TERMIOS_TEST_DRIVER_TABLE_ENTRY; |
---|
38 | |
---|
39 | /* |
---|
40 | * Baud Rate Constant Mapping Entry |
---|
41 | */ |
---|
42 | typedef struct { |
---|
43 | tcflag_t constant; |
---|
44 | rtems_termios_baud_t baud; |
---|
45 | } termios_baud_test_r; |
---|
46 | |
---|
47 | #define INVALID_CONSTANT ((tcflag_t) -2) |
---|
48 | |
---|
49 | #define INVALID_BAUD ((rtems_termios_baud_t) -2) |
---|
50 | /* |
---|
51 | * Baud Rate Constant Mapping Table |
---|
52 | */ |
---|
53 | static const termios_baud_test_r baud_table[] = { |
---|
54 | { B0, 0 }, |
---|
55 | { B50, 50 }, |
---|
56 | { B75, 75 }, |
---|
57 | { B110, 110 }, |
---|
58 | { B134, 134 }, |
---|
59 | { B150, 150 }, |
---|
60 | { B200, 200 }, |
---|
61 | { B300, 300 }, |
---|
62 | { B600, 600 }, |
---|
63 | { B1200, 1200 }, |
---|
64 | { B1800, 1800 }, |
---|
65 | { B2400, 2400 }, |
---|
66 | { B4800, 4800 }, |
---|
67 | { B9600, 9600 }, |
---|
68 | { B19200, 19200 }, |
---|
69 | { B38400, 38400 }, |
---|
70 | { B7200, 7200 }, |
---|
71 | { B14400, 14400 }, |
---|
72 | { B28800, 28800 }, |
---|
73 | { B57600, 57600 }, |
---|
74 | { B76800, 76800 }, |
---|
75 | { B115200, 115200 }, |
---|
76 | { B230400, 230400 }, |
---|
77 | { B460800, 460800 }, |
---|
78 | { B921600, 921600 }, |
---|
79 | { INVALID_CONSTANT, INVALID_BAUD } |
---|
80 | }; |
---|
81 | |
---|
82 | /* |
---|
83 | * Character Size Constant Mapping Entry |
---|
84 | */ |
---|
85 | typedef struct { |
---|
86 | tcflag_t constant; |
---|
87 | int bits; |
---|
88 | } termios_character_size_test_r; |
---|
89 | |
---|
90 | /* |
---|
91 | * Character Size Constant Mapping Table |
---|
92 | */ |
---|
93 | static const termios_character_size_test_r char_size_table[] = { |
---|
94 | { CS5, 5 }, |
---|
95 | { CS6, 6 }, |
---|
96 | { CS7, 7 }, |
---|
97 | { CS8, 8 }, |
---|
98 | { INVALID_CONSTANT, -1 } |
---|
99 | }; |
---|
100 | |
---|
101 | /* |
---|
102 | * Parity Constant Mapping Entry |
---|
103 | */ |
---|
104 | typedef struct { |
---|
105 | tcflag_t constant; |
---|
106 | const char *parity; |
---|
107 | } termios_parity_test_r; |
---|
108 | |
---|
109 | /* |
---|
110 | * Parity Constant Mapping Table |
---|
111 | */ |
---|
112 | static const termios_parity_test_r parity_table[] = { |
---|
113 | { 0, "none" }, |
---|
114 | { PARENB, "even" }, |
---|
115 | { PARENB | PARODD, "odd" }, |
---|
116 | { INVALID_CONSTANT, NULL } |
---|
117 | }; |
---|
118 | |
---|
119 | /* |
---|
120 | * Stop Bit Constant Mapping Entry |
---|
121 | */ |
---|
122 | typedef struct { |
---|
123 | tcflag_t constant; |
---|
124 | int stop; |
---|
125 | } termios_stop_bits_test_r; |
---|
126 | |
---|
127 | /* |
---|
128 | * Stop Bit Constant Mapping Table |
---|
129 | */ |
---|
130 | static const termios_stop_bits_test_r stop_bits_table[] = { |
---|
131 | { 0, 1 }, |
---|
132 | { CSTOPB, 2 }, |
---|
133 | { INVALID_CONSTANT, -1 } |
---|
134 | }; |
---|
135 | |
---|
136 | /* |
---|
137 | * Test converting baud rate into an index |
---|
138 | */ |
---|
139 | static void test_termios_baud2index(void) |
---|
140 | { |
---|
141 | int i; |
---|
142 | int index; |
---|
143 | |
---|
144 | puts( "Test termios_baud2index..." ); |
---|
145 | puts( "termios_baud_to_index(-2) - NOT OK" ); |
---|
146 | i = rtems_termios_baud_to_index( INVALID_CONSTANT ); |
---|
147 | rtems_test_assert( i == -1 ); |
---|
148 | |
---|
149 | for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) { |
---|
150 | printf( |
---|
151 | "termios_baud_to_index(B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
152 | baud_table[i].baud |
---|
153 | ); |
---|
154 | index = rtems_termios_baud_to_index( baud_table[i].constant ); |
---|
155 | if ( index != i ) { |
---|
156 | printf( "ERROR - returned %d should be %d\n", index, i ); |
---|
157 | rtems_test_exit(0); |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | /* |
---|
163 | * Test converting termios baud constant to baud number |
---|
164 | */ |
---|
165 | static void test_termios_baud2number(void) |
---|
166 | { |
---|
167 | int i; |
---|
168 | rtems_termios_baud_t number; |
---|
169 | |
---|
170 | puts( |
---|
171 | "\n" |
---|
172 | "Test termios_baud2number..." |
---|
173 | ); |
---|
174 | puts( "termios_baud_to_number(-2) - NOT OK" ); |
---|
175 | number = rtems_termios_baud_to_number( INVALID_CONSTANT ); |
---|
176 | rtems_test_assert( number == 0 ); |
---|
177 | |
---|
178 | for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) { |
---|
179 | printf( |
---|
180 | "termios_baud_to_number(B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
181 | baud_table[i].baud |
---|
182 | ); |
---|
183 | number = rtems_termios_baud_to_number( baud_table[i].constant ); |
---|
184 | if ( number != baud_table[i].baud ) { |
---|
185 | printf( |
---|
186 | "ERROR - returned %" PRIdrtems_termios_baud_t |
---|
187 | " should be %" PRIdrtems_termios_baud_t "\n", |
---|
188 | number, |
---|
189 | baud_table[i].baud |
---|
190 | ); |
---|
191 | rtems_test_exit(0); |
---|
192 | } |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | /* |
---|
197 | * Test converting baud number to termios baud constant |
---|
198 | */ |
---|
199 | static void test_termios_number_to_baud(void) |
---|
200 | { |
---|
201 | int i; |
---|
202 | tcflag_t termios_baud; |
---|
203 | |
---|
204 | puts( |
---|
205 | "\n" |
---|
206 | "Test termios_number_to_baud..." |
---|
207 | ); |
---|
208 | puts( "termios_number_to_baud(-2) - NOT OK" ); |
---|
209 | termios_baud = rtems_termios_number_to_baud( INVALID_BAUD ); |
---|
210 | rtems_test_assert( termios_baud == 0 ); |
---|
211 | |
---|
212 | for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) { |
---|
213 | printf( |
---|
214 | "termios_number_to_baud(B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
215 | baud_table[i].baud |
---|
216 | ); |
---|
217 | termios_baud = rtems_termios_number_to_baud( baud_table[i].baud ); |
---|
218 | if ( termios_baud != baud_table[i].constant ) { |
---|
219 | printf( |
---|
220 | "ERROR - returned %d should be %d\n", |
---|
221 | termios_baud, |
---|
222 | baud_table[i].constant |
---|
223 | ); |
---|
224 | rtems_test_exit(0); |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | /* |
---|
230 | * Test all the baud rate options |
---|
231 | */ |
---|
232 | static void test_termios_set_baud( |
---|
233 | int test |
---|
234 | ) |
---|
235 | { |
---|
236 | int sc; |
---|
237 | int i; |
---|
238 | struct termios attr; |
---|
239 | |
---|
240 | puts( "Test termios setting device baud rate..." ); |
---|
241 | for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) { |
---|
242 | sc = tcgetattr( test, &attr ); |
---|
243 | if ( sc != 0 ) { |
---|
244 | printf( "ERROR - return %d\n", sc ); |
---|
245 | rtems_test_exit(0); |
---|
246 | } |
---|
247 | |
---|
248 | attr.c_ispeed = baud_table[i].constant; |
---|
249 | attr.c_ospeed = baud_table[i].constant; |
---|
250 | |
---|
251 | printf( |
---|
252 | "tcsetattr(TCSANOW, B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
253 | baud_table[i].baud |
---|
254 | ); |
---|
255 | sc = tcsetattr( test, TCSANOW, &attr ); |
---|
256 | if ( sc != 0 ) { |
---|
257 | printf( "ERROR - return %d\n", sc ); |
---|
258 | rtems_test_exit(0); |
---|
259 | } |
---|
260 | |
---|
261 | printf( |
---|
262 | "tcsetattr(TCSADRAIN, B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
263 | baud_table[i].baud |
---|
264 | ); |
---|
265 | sc = tcsetattr( test, TCSADRAIN, &attr ); |
---|
266 | if ( sc != 0 ) { |
---|
267 | printf( "ERROR - return %d\n", sc ); |
---|
268 | rtems_test_exit(0); |
---|
269 | } |
---|
270 | |
---|
271 | printf( |
---|
272 | "tcsetattr(TCSAFLUSH, B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
273 | baud_table[i].baud |
---|
274 | ); |
---|
275 | sc = tcsetattr( test, TCSAFLUSH, &attr ); |
---|
276 | if ( sc != 0 ) { |
---|
277 | printf( "ERROR - return %d\n", sc ); |
---|
278 | rtems_test_exit(0); |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | /* |
---|
284 | * Test all the character size options |
---|
285 | */ |
---|
286 | static void test_termios_set_charsize( |
---|
287 | int test |
---|
288 | ) |
---|
289 | { |
---|
290 | int sc; |
---|
291 | int i; |
---|
292 | struct termios attr; |
---|
293 | |
---|
294 | puts( |
---|
295 | "\n" |
---|
296 | "Test termios setting device character size ..." |
---|
297 | ); |
---|
298 | for (i=0 ; char_size_table[i].constant != INVALID_CONSTANT ; i++ ) { |
---|
299 | tcflag_t csize = CSIZE; |
---|
300 | |
---|
301 | sc = tcgetattr( test, &attr ); |
---|
302 | if ( sc != 0 ) { |
---|
303 | printf( "ERROR - return %d\n", sc ); |
---|
304 | rtems_test_exit(0); |
---|
305 | } |
---|
306 | |
---|
307 | attr.c_cflag &= ~csize; |
---|
308 | attr.c_cflag |= char_size_table[i].constant; |
---|
309 | |
---|
310 | printf( "tcsetattr(TCSANOW, CS%d) - OK\n", char_size_table[i].bits ); |
---|
311 | sc = tcsetattr( test, TCSANOW, &attr ); |
---|
312 | if ( sc != 0 ) { |
---|
313 | printf( "ERROR - return %d\n", sc ); |
---|
314 | rtems_test_exit(0); |
---|
315 | } |
---|
316 | |
---|
317 | printf( "tcsetattr(TCSADRAIN, CS%d) - OK\n", char_size_table[i].bits ); |
---|
318 | sc = tcsetattr( test, TCSADRAIN, &attr ); |
---|
319 | if ( sc != 0 ) { |
---|
320 | printf( "ERROR - return %d\n", sc ); |
---|
321 | rtems_test_exit(0); |
---|
322 | } |
---|
323 | |
---|
324 | printf( "tcsetattr(TCSAFLUSH, CS%d) - OK\n", char_size_table[i].bits ); |
---|
325 | sc = tcsetattr( test, TCSAFLUSH, &attr ); |
---|
326 | if ( sc != 0 ) { |
---|
327 | printf( "ERROR - return %d\n", sc ); |
---|
328 | rtems_test_exit(0); |
---|
329 | } |
---|
330 | |
---|
331 | printf( "tcsetattr(TCSASOFT, CS%d) - OK\n", char_size_table[i].bits ); |
---|
332 | sc = tcsetattr( test, TCSASOFT, &attr ); |
---|
333 | if ( sc != 0 ) { |
---|
334 | printf( "ERROR - return %d\n", sc ); |
---|
335 | rtems_test_exit(0); |
---|
336 | } |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | /* |
---|
341 | * Test all the parity options |
---|
342 | */ |
---|
343 | static void test_termios_set_parity( |
---|
344 | int test |
---|
345 | ) |
---|
346 | { |
---|
347 | int sc; |
---|
348 | int i; |
---|
349 | struct termios attr; |
---|
350 | |
---|
351 | puts( |
---|
352 | "\n" |
---|
353 | "Test termios setting device parity ..." |
---|
354 | ); |
---|
355 | for (i=0 ; parity_table[i].constant != INVALID_CONSTANT ; i++ ) { |
---|
356 | tcflag_t par = PARENB | PARODD; |
---|
357 | |
---|
358 | sc = tcgetattr( test, &attr ); |
---|
359 | if ( sc != 0 ) { |
---|
360 | printf( "ERROR - return %d\n", sc ); |
---|
361 | rtems_test_exit(0); |
---|
362 | } |
---|
363 | |
---|
364 | attr.c_cflag &= ~par; |
---|
365 | attr.c_cflag |= parity_table[i].constant; |
---|
366 | |
---|
367 | printf( "tcsetattr(TCSANOW, %s) - OK\n", parity_table[i].parity ); |
---|
368 | sc = tcsetattr( test, TCSANOW, &attr ); |
---|
369 | if ( sc != 0 ) { |
---|
370 | printf( "ERROR - return %d\n", sc ); |
---|
371 | rtems_test_exit(0); |
---|
372 | } |
---|
373 | |
---|
374 | printf( "tcsetattr(TCSADRAIN, %s) - OK\n", parity_table[i].parity ); |
---|
375 | sc = tcsetattr( test, TCSADRAIN, &attr ); |
---|
376 | if ( sc != 0 ) { |
---|
377 | printf( "ERROR - return %d\n", sc ); |
---|
378 | rtems_test_exit(0); |
---|
379 | } |
---|
380 | |
---|
381 | printf( "tcsetattr(TCSAFLUSH, %s) - OK\n", parity_table[i].parity ); |
---|
382 | sc = tcsetattr( test, TCSAFLUSH, &attr ); |
---|
383 | if ( sc != 0 ) { |
---|
384 | printf( "ERROR - return %d\n", sc ); |
---|
385 | rtems_test_exit(0); |
---|
386 | } |
---|
387 | |
---|
388 | printf( "tcsetattr(TCSASOFT, %s) - OK\n", parity_table[i].parity ); |
---|
389 | sc = tcsetattr( test, TCSASOFT, &attr ); |
---|
390 | if ( sc != 0 ) { |
---|
391 | printf( "ERROR - return %d\n", sc ); |
---|
392 | rtems_test_exit(0); |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | /* |
---|
398 | * Test all the stop bit options |
---|
399 | */ |
---|
400 | static void test_termios_set_stop_bits( |
---|
401 | int test |
---|
402 | ) |
---|
403 | { |
---|
404 | int sc; |
---|
405 | int i; |
---|
406 | struct termios attr; |
---|
407 | |
---|
408 | puts( |
---|
409 | "\n" |
---|
410 | "Test termios setting device character size ..." |
---|
411 | ); |
---|
412 | for (i=0 ; stop_bits_table[i].constant != INVALID_CONSTANT ; i++ ) { |
---|
413 | tcflag_t cstopb = CSTOPB; |
---|
414 | |
---|
415 | sc = tcgetattr( test, &attr ); |
---|
416 | if ( sc != 0 ) { |
---|
417 | printf( "ERROR - return %d\n", sc ); |
---|
418 | rtems_test_exit(0); |
---|
419 | } |
---|
420 | |
---|
421 | attr.c_cflag &= ~cstopb; |
---|
422 | attr.c_cflag |= stop_bits_table[i].constant; |
---|
423 | |
---|
424 | printf( "tcsetattr(TCSANOW, %d bit%s) - OK\n", |
---|
425 | stop_bits_table[i].stop, |
---|
426 | ((stop_bits_table[i].stop == 1) ? "" : "s") |
---|
427 | ); |
---|
428 | sc = tcsetattr( test, TCSANOW, &attr ); |
---|
429 | if ( sc != 0 ) { |
---|
430 | printf( "ERROR - return %d\n", sc ); |
---|
431 | rtems_test_exit(0); |
---|
432 | } |
---|
433 | |
---|
434 | printf( "tcsetattr(TCSADRAIN, %d bits) - OK\n", stop_bits_table[i].stop ); |
---|
435 | sc = tcsetattr( test, TCSADRAIN, &attr ); |
---|
436 | if ( sc != 0 ) { |
---|
437 | printf( "ERROR - return %d\n", sc ); |
---|
438 | rtems_test_exit(0); |
---|
439 | } |
---|
440 | |
---|
441 | printf( "tcsetattr(TCSAFLUSH, %d bits) - OK\n", stop_bits_table[i].stop ); |
---|
442 | sc = tcsetattr( test, TCSAFLUSH, &attr ); |
---|
443 | if ( sc != 0 ) { |
---|
444 | printf( "ERROR - return %d\n", sc ); |
---|
445 | rtems_test_exit(0); |
---|
446 | } |
---|
447 | |
---|
448 | printf( "tcsetattr(TCSASOFT, %d bits) - OK\n", stop_bits_table[i].stop ); |
---|
449 | sc = tcsetattr( test, TCSASOFT, &attr ); |
---|
450 | if ( sc != 0 ) { |
---|
451 | printf( "ERROR - return %d\n", sc ); |
---|
452 | rtems_test_exit(0); |
---|
453 | } |
---|
454 | } |
---|
455 | } |
---|
456 | |
---|
457 | static void test_termios_cfoutspeed(void) |
---|
458 | { |
---|
459 | int i; |
---|
460 | int sc; |
---|
461 | speed_t speed; |
---|
462 | struct termios term; |
---|
463 | speed_t bad; |
---|
464 | |
---|
465 | bad = B921600 << 1; |
---|
466 | memset( &term, '\0', sizeof(term) ); |
---|
467 | puts( "cfsetospeed(BAD BAUD) - EINVAL" ); |
---|
468 | sc = cfsetospeed( &term, bad ); |
---|
469 | rtems_test_assert( sc == -1 ); |
---|
470 | rtems_test_assert( errno == EINVAL ); |
---|
471 | |
---|
472 | for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) { |
---|
473 | memset( &term, '\0', sizeof(term) ); |
---|
474 | printf( |
---|
475 | "cfsetospeed(B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
476 | baud_table[i].baud |
---|
477 | ); |
---|
478 | sc = cfsetospeed( &term, baud_table[i].constant ); |
---|
479 | rtems_test_assert( !sc ); |
---|
480 | printf( |
---|
481 | "cfgetospeed(B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
482 | baud_table[i].baud |
---|
483 | ); |
---|
484 | speed = cfgetospeed( &term ); |
---|
485 | rtems_test_assert( speed == baud_table[i].constant ); |
---|
486 | } |
---|
487 | } |
---|
488 | |
---|
489 | static void test_termios_cfinspeed(void) |
---|
490 | { |
---|
491 | int i; |
---|
492 | int sc; |
---|
493 | speed_t speed; |
---|
494 | struct termios term; |
---|
495 | speed_t bad; |
---|
496 | |
---|
497 | bad = B921600 << 1; |
---|
498 | memset( &term, '\0', sizeof(term) ); |
---|
499 | puts( "cfsetispeed(BAD BAUD) - EINVAL" ); |
---|
500 | sc = cfsetispeed( &term, bad ); |
---|
501 | rtems_test_assert( sc == -1 ); |
---|
502 | rtems_test_assert( errno == EINVAL ); |
---|
503 | |
---|
504 | for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) { |
---|
505 | memset( &term, '\0', sizeof(term) ); |
---|
506 | printf( |
---|
507 | "cfsetispeed(B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
508 | baud_table[i].baud |
---|
509 | ); |
---|
510 | sc = cfsetispeed( &term, baud_table[i].constant ); |
---|
511 | rtems_test_assert( !sc ); |
---|
512 | |
---|
513 | printf( |
---|
514 | "cfgetispeed(B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
515 | baud_table[i].baud |
---|
516 | ); |
---|
517 | speed = cfgetispeed( &term ); |
---|
518 | rtems_test_assert( speed == baud_table[i].constant ); |
---|
519 | } |
---|
520 | } |
---|
521 | |
---|
522 | static void test_termios_cfsetspeed(void) |
---|
523 | { |
---|
524 | int i; |
---|
525 | int status; |
---|
526 | speed_t speed; |
---|
527 | struct termios term; |
---|
528 | speed_t bad; |
---|
529 | |
---|
530 | bad = B921600 << 1; |
---|
531 | memset( &term, '\0', sizeof(term) ); |
---|
532 | puts( "cfsetspeed(BAD BAUD) - EINVAL" ); |
---|
533 | status = cfsetspeed( &term, bad ); |
---|
534 | rtems_test_assert( status == -1 ); |
---|
535 | rtems_test_assert( errno == EINVAL ); |
---|
536 | |
---|
537 | for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) { |
---|
538 | memset( &term, '\0', sizeof(term) ); |
---|
539 | printf( |
---|
540 | "cfsetspeed(B%" PRIdrtems_termios_baud_t ") - OK\n", |
---|
541 | baud_table[i].baud |
---|
542 | ); |
---|
543 | status = cfsetspeed( &term, baud_table[i].constant ); |
---|
544 | rtems_test_assert( !status ); |
---|
545 | |
---|
546 | printf( |
---|
547 | "cfgetspeed(B%" PRIdrtems_termios_baud_t ") - checking both inspeed and outspeed - OK\n", |
---|
548 | baud_table[i].baud |
---|
549 | ); |
---|
550 | speed = cfgetispeed( &term ); |
---|
551 | rtems_test_assert( speed == baud_table[i].constant ); |
---|
552 | |
---|
553 | speed = cfgetospeed( &term ); |
---|
554 | rtems_test_assert( speed == baud_table[i].constant ); |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | static void test_termios_cfmakeraw(void) |
---|
559 | { |
---|
560 | struct termios term; |
---|
561 | |
---|
562 | memset( &term, '\0', sizeof(term) ); |
---|
563 | cfmakeraw( &term ); |
---|
564 | puts( "cfmakeraw - OK" ); |
---|
565 | |
---|
566 | /* Check that all of the flags were set correctly */ |
---|
567 | rtems_test_assert( ~(term.c_iflag & (IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR)) ); |
---|
568 | |
---|
569 | rtems_test_assert( term.c_iflag & (IGNBRK) ); |
---|
570 | |
---|
571 | rtems_test_assert( ~(term.c_oflag & OPOST) ); |
---|
572 | |
---|
573 | rtems_test_assert( ~(term.c_lflag & (ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN)) ); |
---|
574 | |
---|
575 | rtems_test_assert( ~(term.c_cflag & (CSIZE|PARENB)) ); |
---|
576 | |
---|
577 | rtems_test_assert( term.c_cflag & (CS8|CREAD) ); |
---|
578 | |
---|
579 | rtems_test_assert( term.c_cc[VMIN] == 1 ); |
---|
580 | |
---|
581 | rtems_test_assert( term.c_cc[VTIME] == 0 ); |
---|
582 | } |
---|
583 | |
---|
584 | static void test_termios_cfmakesane(void) |
---|
585 | { |
---|
586 | struct termios term; |
---|
587 | |
---|
588 | memset( &term, '\0', sizeof(term) ); |
---|
589 | cfmakesane( &term ); |
---|
590 | puts( "cfmakesane - OK" ); |
---|
591 | |
---|
592 | /* Check that all of the flags were set correctly */ |
---|
593 | rtems_test_assert( term.c_iflag == TTYDEF_IFLAG ); |
---|
594 | |
---|
595 | rtems_test_assert( term.c_oflag == TTYDEF_OFLAG ); |
---|
596 | |
---|
597 | rtems_test_assert( term.c_lflag == TTYDEF_LFLAG ); |
---|
598 | |
---|
599 | rtems_test_assert( term.c_cflag == TTYDEF_CFLAG ); |
---|
600 | |
---|
601 | rtems_test_assert( term.c_ispeed == TTYDEF_SPEED ); |
---|
602 | |
---|
603 | rtems_test_assert( term.c_ospeed == TTYDEF_SPEED ); |
---|
604 | |
---|
605 | rtems_test_assert( memcmp(&term.c_cc, ttydefchars, sizeof(term.c_cc)) == 0 ); |
---|
606 | } |
---|
607 | |
---|
608 | typedef struct { |
---|
609 | rtems_termios_device_context base; |
---|
610 | bool done; |
---|
611 | } device_context; |
---|
612 | |
---|
613 | static rtems_status_code test_early_device_install( |
---|
614 | rtems_device_major_number major, |
---|
615 | rtems_device_minor_number minor, |
---|
616 | void *arg |
---|
617 | ) |
---|
618 | { |
---|
619 | static const rtems_termios_device_handler handler; |
---|
620 | static const char dev[] = "/foobar"; |
---|
621 | |
---|
622 | rtems_resource_snapshot snapshot; |
---|
623 | rtems_status_code sc; |
---|
624 | int fd; |
---|
625 | int rv; |
---|
626 | int i; |
---|
627 | |
---|
628 | rtems_resource_snapshot_take( &snapshot ); |
---|
629 | |
---|
630 | sc = rtems_termios_device_install( &dev[0], &handler, NULL, NULL ); |
---|
631 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
632 | |
---|
633 | /* |
---|
634 | * The loop ensures that file descriptor 0 is the first free file descriptor |
---|
635 | * after this test case. |
---|
636 | */ |
---|
637 | for (i = 0; i < 4; ++i) { |
---|
638 | errno = 0; |
---|
639 | fd = open( &dev[0], O_RDWR ); |
---|
640 | rtems_test_assert( fd == -1 ); |
---|
641 | rtems_test_assert( errno == ENXIO ); |
---|
642 | } |
---|
643 | |
---|
644 | rv = unlink( &dev[0] ); |
---|
645 | rtems_test_assert( rv == 0 ); |
---|
646 | |
---|
647 | rtems_test_assert( rtems_resource_snapshot_check( &snapshot ) ); |
---|
648 | |
---|
649 | return RTEMS_SUCCESSFUL; |
---|
650 | } |
---|
651 | |
---|
652 | static void test_device_install_remove(void) |
---|
653 | { |
---|
654 | static const rtems_termios_device_handler handler; |
---|
655 | static const char dev[] = "/foobar"; |
---|
656 | |
---|
657 | rtems_resource_snapshot snapshot; |
---|
658 | rtems_status_code sc; |
---|
659 | void *greedy; |
---|
660 | int rv; |
---|
661 | |
---|
662 | rtems_resource_snapshot_take( &snapshot ); |
---|
663 | |
---|
664 | greedy = rtems_heap_greedy_allocate( NULL, 0 ); |
---|
665 | |
---|
666 | sc = rtems_termios_device_install( "/", &handler, NULL, NULL ); |
---|
667 | rtems_test_assert( sc == RTEMS_NO_MEMORY ); |
---|
668 | |
---|
669 | rtems_heap_greedy_free( greedy ); |
---|
670 | |
---|
671 | rtems_test_assert( rtems_resource_snapshot_check( &snapshot ) ); |
---|
672 | |
---|
673 | sc = rtems_termios_device_install( "/", &handler, NULL, NULL ); |
---|
674 | rtems_test_assert( sc == RTEMS_UNSATISFIED ); |
---|
675 | |
---|
676 | rtems_test_assert( rtems_resource_snapshot_check( &snapshot ) ); |
---|
677 | |
---|
678 | sc = rtems_termios_device_install( &dev[0], &handler, NULL, NULL ); |
---|
679 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
680 | |
---|
681 | rv = unlink( &dev[0] ); |
---|
682 | rtems_test_assert( rv == 0 ); |
---|
683 | |
---|
684 | rtems_test_assert( rtems_resource_snapshot_check( &snapshot ) ); |
---|
685 | } |
---|
686 | |
---|
687 | static bool first_open_error( |
---|
688 | rtems_termios_tty *tty, |
---|
689 | rtems_termios_device_context *base, |
---|
690 | struct termios *term, |
---|
691 | rtems_libio_open_close_args_t *args |
---|
692 | ) |
---|
693 | { |
---|
694 | device_context *ctx = (device_context *) base; |
---|
695 | |
---|
696 | (void) tty; |
---|
697 | (void) term; |
---|
698 | (void) args; |
---|
699 | |
---|
700 | ctx->done = true; |
---|
701 | |
---|
702 | return false; |
---|
703 | } |
---|
704 | |
---|
705 | static void test_first_open_error(void) |
---|
706 | { |
---|
707 | static const rtems_termios_device_handler handler = { |
---|
708 | .first_open = first_open_error |
---|
709 | }; |
---|
710 | static const char dev[] = "/foobar"; |
---|
711 | |
---|
712 | rtems_resource_snapshot snapshot; |
---|
713 | rtems_status_code sc; |
---|
714 | int fd; |
---|
715 | int rv; |
---|
716 | device_context ctx = { |
---|
717 | .base = RTEMS_TERMIOS_DEVICE_CONTEXT_INITIALIZER( "abc" ), |
---|
718 | .done = false |
---|
719 | }; |
---|
720 | |
---|
721 | rtems_resource_snapshot_take( &snapshot ); |
---|
722 | |
---|
723 | sc = rtems_termios_device_install( &dev[0], &handler, NULL, &ctx.base ); |
---|
724 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
725 | |
---|
726 | rtems_test_assert( !ctx.done ); |
---|
727 | errno = 0; |
---|
728 | fd = open( &dev[0], O_RDWR ); |
---|
729 | rtems_test_assert( fd == -1 ); |
---|
730 | rtems_test_assert( errno == ENOMEM ); |
---|
731 | rtems_test_assert( ctx.done ); |
---|
732 | |
---|
733 | rv = unlink( &dev[0] ); |
---|
734 | rtems_test_assert( rv == 0 ); |
---|
735 | |
---|
736 | rtems_test_assert( rtems_resource_snapshot_check( &snapshot ) ); |
---|
737 | } |
---|
738 | |
---|
739 | static bool set_attributes_error( |
---|
740 | rtems_termios_device_context *base, |
---|
741 | const struct termios *term |
---|
742 | ) |
---|
743 | { |
---|
744 | device_context *ctx = (device_context *) base; |
---|
745 | |
---|
746 | (void) term; |
---|
747 | |
---|
748 | ctx->done = true; |
---|
749 | |
---|
750 | return false; |
---|
751 | } |
---|
752 | |
---|
753 | static void test_set_attributes_error(void) |
---|
754 | { |
---|
755 | static const rtems_termios_device_handler handler = { |
---|
756 | .set_attributes = set_attributes_error |
---|
757 | }; |
---|
758 | static const char dev[] = "/foobar"; |
---|
759 | |
---|
760 | rtems_resource_snapshot snapshot; |
---|
761 | rtems_status_code sc; |
---|
762 | struct termios term; |
---|
763 | device_context ctx = { |
---|
764 | .base = RTEMS_TERMIOS_DEVICE_CONTEXT_INITIALIZER( "abc" ), |
---|
765 | .done = false |
---|
766 | }; |
---|
767 | int fd; |
---|
768 | int rv; |
---|
769 | |
---|
770 | rtems_resource_snapshot_take( &snapshot ); |
---|
771 | |
---|
772 | sc = rtems_termios_device_install( &dev[0], &handler, NULL, &ctx.base ); |
---|
773 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
774 | |
---|
775 | fd = open( &dev[0], O_RDWR ); |
---|
776 | rtems_test_assert( fd >= 0 ); |
---|
777 | |
---|
778 | rtems_test_assert( !ctx.done ); |
---|
779 | errno = 0; |
---|
780 | rv = ioctl( fd, TIOCSETA, &term ); |
---|
781 | rtems_test_assert( rv == -1 ); |
---|
782 | rtems_test_assert( errno == EIO ); |
---|
783 | rtems_test_assert( ctx.done ); |
---|
784 | |
---|
785 | rv = close( fd ); |
---|
786 | rtems_test_assert( rv == 0 ); |
---|
787 | |
---|
788 | rv = unlink( &dev[0] ); |
---|
789 | rtems_test_assert( rv == 0 ); |
---|
790 | |
---|
791 | rtems_test_assert( rtems_resource_snapshot_check( &snapshot ) ); |
---|
792 | } |
---|
793 | |
---|
794 | static void test_set_best_baud(void) |
---|
795 | { |
---|
796 | static const struct { |
---|
797 | uint32_t baud; |
---|
798 | speed_t speed; |
---|
799 | } baud_to_speed_table[] = { |
---|
800 | { 0, B0 }, |
---|
801 | { 25, B0 }, |
---|
802 | { 26, B50 }, |
---|
803 | { 50, B50 }, |
---|
804 | { 62, B50 }, |
---|
805 | { 63, B75 }, |
---|
806 | { 75, B75 }, |
---|
807 | { 110, B110 }, |
---|
808 | { 134, B134 }, |
---|
809 | { 150, B150 }, |
---|
810 | { 200, B200 }, |
---|
811 | { 300, B300 }, |
---|
812 | { 600, B600 }, |
---|
813 | { 1200, B1200 }, |
---|
814 | { 1800, B1800 }, |
---|
815 | { 2400, B2400 }, |
---|
816 | { 4800, B4800 }, |
---|
817 | { 9600, B9600 }, |
---|
818 | { 19200, B19200 }, |
---|
819 | { 38400, B38400 }, |
---|
820 | { 57600, B57600 }, |
---|
821 | { 115200, B115200 }, |
---|
822 | { 230400, B230400 }, |
---|
823 | { 460800, B460800 }, |
---|
824 | { 0xffffffff, B460800 } |
---|
825 | }; |
---|
826 | |
---|
827 | size_t n = RTEMS_ARRAY_SIZE(baud_to_speed_table); |
---|
828 | size_t i; |
---|
829 | |
---|
830 | for ( i = 0; i < n; ++i ) { |
---|
831 | struct termios term; |
---|
832 | |
---|
833 | memset( &term, 0xff, sizeof( term ) ); |
---|
834 | rtems_termios_set_best_baud( &term, baud_to_speed_table[ i ].baud ); |
---|
835 | |
---|
836 | rtems_test_assert( term.c_ispeed == baud_to_speed_table[ i ].speed ); |
---|
837 | rtems_test_assert( term.c_ospeed == baud_to_speed_table[ i ].speed ); |
---|
838 | } |
---|
839 | } |
---|
840 | |
---|
841 | static rtems_task Init( |
---|
842 | rtems_task_argument ignored |
---|
843 | ) |
---|
844 | { |
---|
845 | int rc; |
---|
846 | rtems_status_code sc; |
---|
847 | rtems_device_major_number registered; |
---|
848 | int test; |
---|
849 | struct termios t; |
---|
850 | int index = 0; |
---|
851 | |
---|
852 | TEST_BEGIN(); |
---|
853 | |
---|
854 | test_termios_baud2index(); |
---|
855 | test_termios_baud2number(); |
---|
856 | test_termios_number_to_baud(); |
---|
857 | |
---|
858 | sc = rtems_termios_bufsize( 256, 128, 64 ); |
---|
859 | directive_failed( sc, "rtems_termios_bufsize" ); |
---|
860 | |
---|
861 | /* |
---|
862 | * Register a driver |
---|
863 | */ |
---|
864 | puts( |
---|
865 | "\n" |
---|
866 | "Init - rtems_io_register_driver - Termios Test Driver - OK" |
---|
867 | ); |
---|
868 | sc = rtems_io_register_driver( 0, &test_driver, ®istered ); |
---|
869 | printf( "Init - Major slot returned = %d\n", (int) registered ); |
---|
870 | directive_failed( sc, "rtems_io_register_driver" ); |
---|
871 | |
---|
872 | /* |
---|
873 | * Test baud rate |
---|
874 | */ |
---|
875 | puts( "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" ); |
---|
876 | test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR ); |
---|
877 | if ( test == -1 ) { |
---|
878 | printf( "ERROR - baud opening test device (%d)\n", test ); |
---|
879 | rtems_test_exit(0); |
---|
880 | } |
---|
881 | |
---|
882 | /* |
---|
883 | * tcsetattr - ERROR invalid operation |
---|
884 | */ |
---|
885 | puts( "tcsetattr - invalid operation - EINVAL" ); |
---|
886 | rc = tcsetattr( test, INT_MAX, &t ); |
---|
887 | rtems_test_assert( rc == -1 ); |
---|
888 | rtems_test_assert( errno == EINVAL ); |
---|
889 | |
---|
890 | test_termios_cfmakeraw(); |
---|
891 | test_termios_cfmakesane(); |
---|
892 | |
---|
893 | /* |
---|
894 | * tcsetattr - TCSADRAIN |
---|
895 | */ |
---|
896 | puts( "\ntcsetattr - drain - OK" ); |
---|
897 | rc = tcsetattr( test, TCSADRAIN, &t ); |
---|
898 | rtems_test_assert( rc == 0 ); |
---|
899 | |
---|
900 | test_termios_set_baud(test); |
---|
901 | |
---|
902 | puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" ); |
---|
903 | rc = close( test ); |
---|
904 | if ( rc != 0 ) { |
---|
905 | printf( "ERROR - baud close test device (%d) %s\n", test, strerror(errno) ); |
---|
906 | rtems_test_exit(0); |
---|
907 | } |
---|
908 | |
---|
909 | /* |
---|
910 | * Test character size |
---|
911 | */ |
---|
912 | puts( |
---|
913 | "\n" |
---|
914 | "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" |
---|
915 | ); |
---|
916 | test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR ); |
---|
917 | if ( test == -1 ) { |
---|
918 | printf( "ERROR - size open test device (%d) %s\n", test, strerror(errno) ); |
---|
919 | rtems_test_exit(0); |
---|
920 | } |
---|
921 | |
---|
922 | test_termios_set_charsize(test); |
---|
923 | |
---|
924 | puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" ); |
---|
925 | rc = close( test ); |
---|
926 | if ( rc != 0 ) { |
---|
927 | printf( "ERROR - size close test device (%d) %s\n", test, strerror(errno) ); |
---|
928 | rtems_test_exit(0); |
---|
929 | } |
---|
930 | |
---|
931 | /* |
---|
932 | * Test parity |
---|
933 | */ |
---|
934 | puts( |
---|
935 | "\n" |
---|
936 | "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" |
---|
937 | ); |
---|
938 | test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR ); |
---|
939 | if ( test == -1 ) { |
---|
940 | printf( "ERROR - parity open test device (%d) %s\n", |
---|
941 | test, strerror(errno) ); |
---|
942 | rtems_test_exit(0); |
---|
943 | } |
---|
944 | |
---|
945 | test_termios_set_parity(test); |
---|
946 | |
---|
947 | puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" ); |
---|
948 | rc = close( test ); |
---|
949 | if ( rc != 0 ) { |
---|
950 | printf( "ERROR - parity close test device (%d) %s\n", |
---|
951 | test, strerror(errno) ); |
---|
952 | rtems_test_exit(0); |
---|
953 | } |
---|
954 | |
---|
955 | /* |
---|
956 | * Test stop bits |
---|
957 | */ |
---|
958 | puts( |
---|
959 | "\n" |
---|
960 | "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" |
---|
961 | ); |
---|
962 | test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR ); |
---|
963 | if ( test == -1 ) { |
---|
964 | printf( "ERROR - stop bits open test device (%d) %s\n", |
---|
965 | test, strerror(errno) ); |
---|
966 | rtems_test_exit(0); |
---|
967 | } |
---|
968 | |
---|
969 | test_termios_set_stop_bits(test); |
---|
970 | |
---|
971 | test_termios_cfoutspeed(); |
---|
972 | |
---|
973 | test_termios_cfinspeed(); |
---|
974 | |
---|
975 | test_termios_cfsetspeed(); |
---|
976 | |
---|
977 | puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" ); |
---|
978 | rc = close( test ); |
---|
979 | if ( rc != 0 ) { |
---|
980 | printf( "ERROR - stop bits close test device (%d) %s\n", |
---|
981 | test, strerror(errno) ); |
---|
982 | rtems_test_exit(0); |
---|
983 | } |
---|
984 | |
---|
985 | |
---|
986 | puts( "Multiple open of the device" ); |
---|
987 | for( ; index < 26; ++index ) { |
---|
988 | test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR ); |
---|
989 | rtems_test_assert( test != -1 ); |
---|
990 | rc = close( test ); |
---|
991 | rtems_test_assert( rc == 0 ); |
---|
992 | } |
---|
993 | puts( "" ); |
---|
994 | |
---|
995 | test_device_install_remove(); |
---|
996 | test_first_open_error(); |
---|
997 | test_set_attributes_error(); |
---|
998 | test_set_best_baud(); |
---|
999 | |
---|
1000 | TEST_END(); |
---|
1001 | rtems_test_exit(0); |
---|
1002 | } |
---|
1003 | |
---|
1004 | /* configuration information */ |
---|
1005 | |
---|
1006 | #define CONFIGURE_APPLICATION_PREREQUISITE_DRIVERS \ |
---|
1007 | { .initialization_entry = test_early_device_install } |
---|
1008 | |
---|
1009 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
1010 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
1011 | |
---|
1012 | /* include an extra slot for registering the termios one dynamically */ |
---|
1013 | #define CONFIGURE_MAXIMUM_DRIVERS 4 |
---|
1014 | |
---|
1015 | /* one for the console and one for the test port */ |
---|
1016 | #define CONFIGURE_NUMBER_OF_TERMIOS_PORTS 3 |
---|
1017 | |
---|
1018 | /* we need to be able to open the test device */ |
---|
1019 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4 |
---|
1020 | |
---|
1021 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
1022 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
1023 | |
---|
1024 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
1025 | |
---|
1026 | #define CONFIGURE_INIT |
---|
1027 | #include <rtems/confdefs.h> |
---|
1028 | |
---|
1029 | /* global variables */ |
---|