source: rtems/testsuites/libtests/termios01/init.c @ 8cd0bb0f

4.115
Last change on this file since 8cd0bb0f was 8cd0bb0f, checked in by Joel Sherrill <joel.sherrill@…>, on 07/14/10 at 15:59:37

2010-07-14 Joel Sherrill <joel.sherrill@…>

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