source: rtems/testsuites/libtests/termios01/init.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 16.3 KB
Line 
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#include <termios.h>
16#include <rtems/termiostypes.h>
17#include <fcntl.h>
18#include <limits.h>
19#include <sys/errno.h>
20
21const char rtems_test_name[] = "TERMIOS 1";
22
23/* rtems_termios_baud_t is a typedefs to int32_t */
24#define PRIdrtems_termios_baud_t PRId32
25
26/*
27 *  Termios Test Driver
28 */
29#include "termios_testdriver.h"
30
31static const rtems_driver_address_table test_driver =
32  TERMIOS_TEST_DRIVER_TABLE_ENTRY;
33
34/*
35 *  Baud Rate Constant Mapping Entry
36 */
37typedef struct {
38  tcflag_t constant;
39  rtems_termios_baud_t baud;
40} termios_baud_test_r;
41
42#define INVALID_CONSTANT ((tcflag_t) -2)
43
44#define INVALID_BAUD ((rtems_termios_baud_t) -2)
45/*
46 *  Baud Rate Constant Mapping Table
47 */
48static const termios_baud_test_r baud_table[] = {
49  { B0,           0 },
50  { B50,         50 },
51  { B75,         75 },
52  { B110,       110 },
53  { B134,       134 },
54  { B150,       150 },
55  { B200,       200 },
56  { B300,       300 },
57  { B600,       600 },
58  { B1200,     1200 },
59  { B1800,     1800 },
60  { B2400,     2400 },
61  { B4800,     4800 },
62  { B9600,     9600 },
63  { B19200,   19200 },
64  { B38400,   38400 },
65  { B57600,   57600 },
66  { B115200, 115200 },
67  { B230400, 230400 },
68  { B460800, 460800 },
69  { INVALID_CONSTANT, INVALID_BAUD }
70};
71
72/*
73 *  Character Size Constant Mapping Entry
74 */
75typedef struct {
76  tcflag_t constant;
77  int bits;
78} termios_character_size_test_r;
79
80/*
81 *  Character Size Constant Mapping Table
82 */
83static const termios_character_size_test_r char_size_table[] = {
84  { CS5,      5 },
85  { CS6,      6 },
86  { CS7,      7 },
87  { CS8,      8 },
88  { INVALID_CONSTANT, -1 }
89};
90
91/*
92 *  Parity Constant Mapping Entry
93 */
94typedef struct {
95  tcflag_t constant;
96  const char *parity;
97} termios_parity_test_r;
98
99/*
100 *  Parity Constant Mapping Table
101 */
102static const termios_parity_test_r parity_table[] = {
103  { 0,                "none" },
104  { PARENB,           "even" },
105  { PARENB | PARODD,  "odd" },
106  { INVALID_CONSTANT, NULL }
107};
108
109/*
110 *  Stop Bit Constant Mapping Entry
111 */
112typedef struct {
113  tcflag_t constant;
114  int stop;
115} termios_stop_bits_test_r;
116
117/*
118 *  Stop Bit Constant Mapping Table
119 */
120static const termios_stop_bits_test_r stop_bits_table[] = {
121  { 0,       1 },
122  { CSTOPB,  2 },
123  { INVALID_CONSTANT, -1 }
124};
125
126/*
127 *  Test converting baud rate into an index
128 */
129static void test_termios_baud2index(void)
130{
131  int i;
132  int index;
133
134  puts( "Test termios_baud2index..." );
135  puts( "termios_baud_to_index(-2) - NOT OK" );
136  i = rtems_termios_baud_to_index( INVALID_CONSTANT );
137  rtems_test_assert( i == -1 );
138
139  for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) {
140    printf(
141      "termios_baud_to_index(B%" PRIdrtems_termios_baud_t ") - OK\n",
142      baud_table[i].baud
143    );
144    index = rtems_termios_baud_to_index( baud_table[i].constant );
145    if ( index != i ) {
146      printf( "ERROR - returned %d should be %d\n", index, i );
147      rtems_test_exit(0);
148    }
149  }
150}
151
152/*
153 *  Test converting termios baud constant to baud number
154 */
155static void test_termios_baud2number(void)
156{
157  int i;
158  rtems_termios_baud_t number;
159
160  puts(
161    "\n"
162    "Test termios_baud2number..."
163  );
164  puts( "termios_baud_to_number(-2) - NOT OK" );
165  number = rtems_termios_baud_to_number( INVALID_CONSTANT );
166  rtems_test_assert( number == 0 );
167
168  for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) {
169    printf(
170      "termios_baud_to_number(B%" PRIdrtems_termios_baud_t ") - OK\n",
171      baud_table[i].baud
172    );
173    number = rtems_termios_baud_to_number( baud_table[i].constant );
174    if ( number != baud_table[i].baud ) {
175      printf(
176        "ERROR - returned %" PRIdrtems_termios_baud_t
177        " should be %" PRIdrtems_termios_baud_t "\n",
178        number,
179        baud_table[i].baud
180      );
181      rtems_test_exit(0);
182    }
183  }
184}
185
186/*
187 *  Test converting baud number to termios baud constant
188 */
189static void test_termios_number_to_baud(void)
190{
191  int i;
192  tcflag_t termios_baud;
193
194  puts(
195    "\n"
196    "Test termios_number_to_baud..."
197  );
198  puts( "termios_number_to_baud(-2) - NOT OK" );
199  termios_baud = rtems_termios_number_to_baud( INVALID_BAUD );
200  rtems_test_assert( termios_baud == 0 );
201
202  for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) {
203    printf(
204      "termios_number_to_baud(B%" PRIdrtems_termios_baud_t ") - OK\n",
205      baud_table[i].baud
206    );
207    termios_baud = rtems_termios_number_to_baud( baud_table[i].baud );
208    if ( termios_baud != baud_table[i].constant ) {
209      printf(
210        "ERROR - returned %d should be %d\n",
211        termios_baud,
212        baud_table[i].constant
213      );
214      rtems_test_exit(0);
215    }
216  }
217}
218
219/*
220 *  Test all the baud rate options
221 */
222static void test_termios_set_baud(
223  int test
224)
225{
226  int             sc;
227  int             i;
228  struct termios  attr;
229
230  puts( "Test termios setting device baud rate..." );
231  for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) {
232    tcflag_t cbaud = CBAUD;
233
234    sc = tcgetattr( test, &attr );
235    if ( sc != 0 ) {
236      printf( "ERROR - return %d\n", sc );
237      rtems_test_exit(0);
238    }
239
240    attr.c_cflag &= ~cbaud;
241    attr.c_cflag |= baud_table[i].constant;
242
243    printf(
244      "tcsetattr(TCSANOW, B%" PRIdrtems_termios_baud_t ") - OK\n",
245      baud_table[i].baud
246    );
247    sc = tcsetattr( test, TCSANOW, &attr );
248    if ( sc != 0 ) {
249      printf( "ERROR - return %d\n", sc );
250      rtems_test_exit(0);
251    }
252
253    printf(
254      "tcsetattr(TCSADRAIN, B%" PRIdrtems_termios_baud_t ") - OK\n",
255      baud_table[i].baud
256    );
257    sc = tcsetattr( test, TCSANOW, &attr );
258    if ( sc != 0 ) {
259      printf( "ERROR - return %d\n", sc );
260      rtems_test_exit(0);
261    }
262  }
263}
264
265/*
266 *  Test all the character size options
267 */
268static void test_termios_set_charsize(
269  int test
270)
271{
272  int             sc;
273  int             i;
274  struct termios  attr;
275
276  puts(
277    "\n"
278    "Test termios setting device character size ..."
279  );
280  for (i=0 ; char_size_table[i].constant != INVALID_CONSTANT ; i++ ) {
281    tcflag_t csize = CSIZE;
282
283    sc = tcgetattr( test, &attr );
284    if ( sc != 0 ) {
285      printf( "ERROR - return %d\n", sc );
286      rtems_test_exit(0);
287    }
288
289    attr.c_cflag &= ~csize;
290    attr.c_cflag |= char_size_table[i].constant;
291
292    printf( "tcsetattr(TCSANOW, CS%d) - OK\n", char_size_table[i].bits );
293    sc = tcsetattr( test, TCSANOW, &attr );
294    if ( sc != 0 ) {
295      printf( "ERROR - return %d\n", sc );
296      rtems_test_exit(0);
297    }
298
299    printf( "tcsetattr(TCSADRAIN, CS%d) - OK\n", char_size_table[i].bits );
300    sc = tcsetattr( test, TCSANOW, &attr );
301    if ( sc != 0 ) {
302      printf( "ERROR - return %d\n", sc );
303      rtems_test_exit(0);
304    }
305  }
306}
307
308/*
309 *  Test all the parity options
310 */
311static void test_termios_set_parity(
312  int test
313)
314{
315  int             sc;
316  int             i;
317  struct termios  attr;
318
319  puts(
320    "\n"
321    "Test termios setting device parity ..."
322  );
323  for (i=0 ; parity_table[i].constant != INVALID_CONSTANT ; i++ ) {
324    tcflag_t par = PARENB | PARODD;
325
326    sc = tcgetattr( test, &attr );
327    if ( sc != 0 ) {
328      printf( "ERROR - return %d\n", sc );
329      rtems_test_exit(0);
330    }
331
332    attr.c_cflag &= ~par;
333    attr.c_cflag |= parity_table[i].constant;
334
335    printf( "tcsetattr(TCSANOW, %s) - OK\n", parity_table[i].parity );
336    sc = tcsetattr( test, TCSANOW, &attr );
337    if ( sc != 0 ) {
338      printf( "ERROR - return %d\n", sc );
339      rtems_test_exit(0);
340    }
341
342    printf( "tcsetattr(TCSADRAIN, %s) - OK\n", parity_table[i].parity );
343    sc = tcsetattr( test, TCSANOW, &attr );
344    if ( sc != 0 ) {
345      printf( "ERROR - return %d\n", sc );
346      rtems_test_exit(0);
347    }
348  }
349}
350
351/*
352 *  Test all the stop bit options
353 */
354static void test_termios_set_stop_bits(
355  int test
356)
357{
358  int             sc;
359  int             i;
360  struct termios  attr;
361
362  puts(
363    "\n"
364    "Test termios setting device character size ..."
365  );
366  for (i=0 ; stop_bits_table[i].constant != INVALID_CONSTANT ; i++ ) {
367    tcflag_t cstopb = CSTOPB;
368
369    sc = tcgetattr( test, &attr );
370    if ( sc != 0 ) {
371      printf( "ERROR - return %d\n", sc );
372      rtems_test_exit(0);
373    }
374
375    attr.c_cflag &= ~cstopb;
376    attr.c_cflag |= stop_bits_table[i].constant;
377
378    printf( "tcsetattr(TCSANOW, %d bit%s) - OK\n",
379      stop_bits_table[i].stop,
380      ((stop_bits_table[i].stop == 1) ? "" : "s")
381    );
382    sc = tcsetattr( test, TCSANOW, &attr );
383    if ( sc != 0 ) {
384      printf( "ERROR - return %d\n", sc );
385      rtems_test_exit(0);
386    }
387
388    printf( "tcsetattr(TCSADRAIN, %d bits) - OK\n", stop_bits_table[i].stop );
389    sc = tcsetattr( test, TCSANOW, &attr );
390    if ( sc != 0 ) {
391      printf( "ERROR - return %d\n", sc );
392      rtems_test_exit(0);
393    }
394  }
395}
396
397static void test_termios_cfoutspeed(void)
398{
399  int i;
400  int sc;
401  speed_t speed;
402  struct termios term;
403  tcflag_t        bad;
404
405  bad = CBAUD << 1;
406  memset( &term, '\0', sizeof(term) );
407  puts( "cfsetospeed(BAD BAUD) - EINVAL" );
408  sc = cfsetospeed( &term, bad );
409  rtems_test_assert( sc == -1 );
410  rtems_test_assert( errno == EINVAL );
411
412  for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) {
413    memset( &term, '\0', sizeof(term) );
414    printf(
415      "cfsetospeed(B%" PRIdrtems_termios_baud_t ") - OK\n",
416      baud_table[i].baud
417    );
418    sc = cfsetospeed( &term, baud_table[i].constant );
419    rtems_test_assert( !sc );
420    printf(
421      "cfgetospeed(B%" PRIdrtems_termios_baud_t ") - OK\n",
422      baud_table[i].baud
423    );
424    speed = cfgetospeed( &term );
425    rtems_test_assert( speed == baud_table[i].constant );
426  }
427}
428
429static void test_termios_cfinspeed(void)
430{
431  int             i;
432  int             sc;
433  speed_t         speed;
434  struct termios  term;
435  tcflag_t        bad;
436
437  bad = CBAUD << 1;
438  memset( &term, '\0', sizeof(term) );
439  puts( "cfsetispeed(BAD BAUD) - EINVAL" );
440  sc = cfsetispeed( &term, bad );
441  rtems_test_assert( sc == -1 );
442  rtems_test_assert( errno == EINVAL );
443
444  for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) {
445    memset( &term, '\0', sizeof(term) );
446    printf(
447      "cfsetispeed(B%" PRIdrtems_termios_baud_t ") - OK\n",
448      baud_table[i].baud
449    );
450    sc = cfsetispeed( &term, baud_table[i].constant );
451    rtems_test_assert( !sc );
452
453    printf(
454      "cfgetispeed(B%" PRIdrtems_termios_baud_t ") - OK\n",
455      baud_table[i].baud
456    );
457    speed = cfgetispeed( &term );
458    rtems_test_assert( speed == baud_table[i].constant );
459  }
460}
461
462static void test_termios_cfsetspeed(void)
463{
464  int             i;
465  int             status;
466  speed_t         speed;
467  struct termios  term;
468  tcflag_t        bad;
469
470  bad = CBAUD << 1;
471  memset( &term, '\0', sizeof(term) );
472  puts( "cfsetspeed(BAD BAUD) - EINVAL" );
473  status = cfsetspeed( &term, bad );
474  rtems_test_assert( status == -1 );
475  rtems_test_assert( errno == EINVAL );
476
477  for (i=0 ; baud_table[i].constant != INVALID_CONSTANT ; i++ ) {
478    memset( &term, '\0', sizeof(term) );
479    printf(
480      "cfsetspeed(B%" PRIdrtems_termios_baud_t ") - OK\n",
481      baud_table[i].baud
482    );
483    status = cfsetspeed( &term, baud_table[i].constant );
484    rtems_test_assert( !status );
485
486    printf(
487      "cfgetspeed(B%" PRIdrtems_termios_baud_t ") - checking both inspeed and outspeed - OK\n",
488      baud_table[i].baud
489    );
490    speed = cfgetispeed( &term );
491    rtems_test_assert( speed == baud_table[i].constant );
492
493    speed = cfgetospeed( &term );
494    rtems_test_assert( speed == baud_table[i].constant );
495  }
496}
497
498static void test_termios_cfmakeraw(void)
499{
500  struct termios  term;
501
502  memset( &term, '\0', sizeof(term) );
503  cfmakeraw( &term );
504  puts( "cfmakeraw - OK" );
505
506  /* Check that all of the flags were set correctly */
507  rtems_test_assert( ~(term.c_iflag & (IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON)) );
508
509  rtems_test_assert( ~(term.c_oflag & OPOST) );
510
511  rtems_test_assert( ~(term.c_lflag & (ECHO|ECHONL|ICANON|ISIG|IEXTEN)) );
512
513  rtems_test_assert( ~(term.c_cflag & (CSIZE|PARENB)) );
514
515  rtems_test_assert( term.c_cflag & CS8 );
516}
517
518static rtems_task Init(
519  rtems_task_argument ignored
520)
521{
522  int                       rc;
523  rtems_status_code         sc;
524  rtems_device_major_number registered;
525  int                       test;
526  struct termios            t;
527  int index = 0;
528
529  TEST_BEGIN();
530
531  test_termios_baud2index();
532  test_termios_baud2number();
533  test_termios_number_to_baud();
534
535  sc = rtems_termios_bufsize( 256, 128, 64 );
536  directive_failed( sc, "rtems_termios_bufsize" );
537
538  /*
539   * Register a driver
540   */
541  puts(
542    "\n"
543    "Init - rtems_io_register_driver - Termios Test Driver - OK"
544  );
545  sc = rtems_io_register_driver( 0, &test_driver, &registered );
546  printf( "Init - Major slot returned = %d\n", (int) registered );
547  directive_failed( sc, "rtems_io_register_driver" );
548
549  /*
550   * Test baud rate
551   */
552  puts( "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
553  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
554  if ( test == -1 ) {
555    printf( "ERROR - baud opening test device (%d)\n", test );
556    rtems_test_exit(0);
557  }
558
559  /*
560   * tcsetattr - ERROR invalid operation
561   */
562  puts( "tcsetattr - invalid operation - ENOTSUP" );
563  rc = tcsetattr( test, INT_MAX, &t );
564  rtems_test_assert( rc == -1 );
565  rtems_test_assert( errno == ENOTSUP );
566
567  test_termios_cfmakeraw();
568 
569  /*
570   * tcsetattr - TCSADRAIN
571   */
572  puts( "\ntcsetattr - drain - OK" );
573  rc = tcsetattr( test, TCSADRAIN, &t );
574  rtems_test_assert( rc == 0 );
575
576  test_termios_set_baud(test);
577
578  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
579  rc = close( test );
580  if ( rc != 0 ) {
581    printf( "ERROR - baud close test device (%d) %s\n", test, strerror(errno) );
582    rtems_test_exit(0);
583  }
584
585  /*
586   * Test character size
587   */
588  puts(
589    "\n"
590    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
591  );
592  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
593  if ( test == -1 ) {
594    printf( "ERROR - size open test device (%d) %s\n", test, strerror(errno) );
595    rtems_test_exit(0);
596  }
597
598  test_termios_set_charsize(test);
599
600  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
601  rc = close( test );
602  if ( rc != 0 ) {
603    printf( "ERROR - size close test device (%d) %s\n", test, strerror(errno) );
604    rtems_test_exit(0);
605  }
606
607  /*
608   * Test parity
609   */
610  puts(
611    "\n"
612    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
613  );
614  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
615  if ( test == -1 ) {
616    printf( "ERROR - parity open test device (%d) %s\n",
617      test, strerror(errno) );
618    rtems_test_exit(0);
619  }
620
621  test_termios_set_parity(test);
622
623  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
624  rc = close( test );
625  if ( rc != 0 ) {
626    printf( "ERROR - parity close test device (%d) %s\n",
627      test, strerror(errno) );
628    rtems_test_exit(0);
629  }
630
631  /*
632   * Test stop bits
633   */
634  puts(
635    "\n"
636    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
637  );
638  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
639  if ( test == -1 ) {
640    printf( "ERROR - stop bits open test device (%d) %s\n",
641      test, strerror(errno) );
642    rtems_test_exit(0);
643  }
644
645  test_termios_set_stop_bits(test);
646
647  test_termios_cfoutspeed();
648
649  test_termios_cfinspeed();
650
651  test_termios_cfsetspeed();
652
653  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
654  rc = close( test );
655  if ( rc != 0 ) {
656    printf( "ERROR - stop bits close test device (%d) %s\n",
657      test, strerror(errno) );
658    rtems_test_exit(0);
659  }
660
661
662  puts( "Multiple open of the device" );
663  for( ; index < 26; ++index ) {
664    test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
665    rtems_test_assert( test != -1 );
666    rc = close( test );
667    rtems_test_assert( rc == 0 );
668  }
669  puts( "" );
670
671  TEST_END();
672  rtems_test_exit(0);
673}
674
675/* configuration information */
676
677#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
678#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
679
680/* include an extra slot for registering the termios one dynamically */
681#define CONFIGURE_MAXIMUM_DRIVERS 3
682
683/* one for the console and one for the test port */
684#define CONFIGURE_NUMBER_OF_TERMIOS_PORTS 3
685
686/* we need to be able to open the test device */
687#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
688
689#define CONFIGURE_MAXIMUM_TASKS         1
690#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
691
692#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
693
694#define CONFIGURE_INIT
695#include <rtems/confdefs.h>
696
697/* global variables */
Note: See TracBrowser for help on using the repository browser.