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

4.115
Last change on this file since ec76b114 was 9f5f6f53, checked in by Joel Sherrill <joel.sherrill@…>, on 06/07/10 at 18:33:09

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

  • termios01/init.c, termios01/termios01.scn, termios02/init.c, termios02/termios02.scn: Add tests for cfigetspeed(), cfogetspeed(), cfisetspeed(), cfosetspeed(), ctermid(), tcflow(), tcflush(), tcsendbreak(), tcsetpgrp(), and tcgetpgrp(). Some of these methods are minimal implementations so the tests will have to grow as the methods grow.
  • Property mode set to 100644
File size: 13.5 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2009.
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(
126    "\n"
127    "Test termios_baud2index..."
128  );
129  puts( "termios_baud_to_index(-2) - NOT OK" );
130  i = rtems_termios_baud_to_index( -2 );
131  rtems_test_assert( i == -1 );
132
133  puts( "termios_baud_to_index(572) - NOT OK" );
134  i = rtems_termios_baud_to_index( -2 );
135  rtems_test_assert( i == -1 );
136
137  if ( i != -1 )
138    for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
139      printf(
140        "termios_baud_to_index(B%" PRIdrtems_termios_baud_t ") - OK\n",
141        baud_table[i].baud
142      );
143      index = rtems_termios_baud_to_index( baud_table[i].constant );
144      if ( index != i ) {
145        printf( "ERROR - returned %d should be %d\n", index, i );
146        rtems_test_exit(0);
147      }
148    }
149}
150
151/*
152 *  Test converting termios baud constant to baud number
153 */
154void test_termios_baud2number(void)
155{
156  int i;
157  int number;
158
159  puts(
160    "\n"
161    "Test termios_baud2number..."
162  );
163  puts( "termios_baud_to_number(-2) - NOT OK" );
164  i = rtems_termios_baud_to_number( -2 );
165  rtems_test_assert( i == -1 );
166
167  puts( "termios_baud_to_number(572) - NOT OK" );
168  i = rtems_termios_baud_to_number( -2 );
169  rtems_test_assert( i == -1 );
170
171  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
172    printf( "termios_baud_to_number(B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
173    number = rtems_termios_baud_to_number( baud_table[i].constant );
174    if ( number != baud_table[i].baud ) {
175      printf(
176        "ERROR - returned %d should be %" PRIdrtems_termios_baud_t "\n",
177        number,
178        baud_table[i].baud
179      );
180      rtems_test_exit(0);
181    }
182  }
183}
184
185/*
186 *  Test converting baud number to termios baud constant
187 */
188void test_termios_number_to_baud(void)
189{
190  int i;
191  int termios_baud;
192
193  puts(
194    "\n"
195    "Test termios_number_to_baud..."
196  );
197  puts( "termios_number_to_baud(-2) - NOT OK" );
198  i = rtems_termios_number_to_baud( -2 );
199  rtems_test_assert( i == -1 );
200
201  puts( "termios_number_to_baud(572) - NOT OK" );
202  i = rtems_termios_number_to_baud( -2 );
203  rtems_test_assert( i == -1 );
204
205  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
206    printf( "termios_number_to_baud(B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
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 */
222void 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 != -1 ; i++ ) {
232    sc = tcgetattr( test, &attr );
233    if ( sc != 0 ) {
234      printf( "ERROR - return %d\n", sc );
235      rtems_test_exit(0);
236    }
237
238    attr.c_cflag &= ~CBAUD;
239    attr.c_cflag |= baud_table[i].constant;
240
241    printf( "tcsetattr(TCSANOW, B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
242    sc = tcsetattr( test, TCSANOW, &attr );
243    if ( sc != 0 ) {
244      printf( "ERROR - return %d\n", sc );
245      rtems_test_exit(0);
246    }
247
248    printf( "tcsetattr(TCSADRAIN, B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
249    sc = tcsetattr( test, TCSANOW, &attr );
250    if ( sc != 0 ) {
251      printf( "ERROR - return %d\n", sc );
252      rtems_test_exit(0);
253    }
254  }
255}
256
257/*
258 *  Test all the character size options
259 */
260void test_termios_set_charsize(
261  int test
262)
263{
264  int             sc;
265  int             i;
266  struct termios  attr;
267
268  puts(
269    "\n"
270    "Test termios setting device character size ..."
271  );
272  for (i=0 ; char_size_table[i].constant != -1 ; i++ ) {
273    sc = tcgetattr( test, &attr );
274    if ( sc != 0 ) {
275      printf( "ERROR - return %d\n", sc );
276      rtems_test_exit(0);
277    }
278
279    attr.c_cflag &= ~CSIZE;
280    attr.c_cflag |= char_size_table[i].constant;
281
282    printf( "tcsetattr(TCSANOW, CS%d) - OK\n", char_size_table[i].bits );
283    sc = tcsetattr( test, TCSANOW, &attr );
284    if ( sc != 0 ) {
285      printf( "ERROR - return %d\n", sc );
286      rtems_test_exit(0);
287    }
288
289    printf( "tcsetattr(TCSADRAIN, CS%d) - OK\n", char_size_table[i].bits );
290    sc = tcsetattr( test, TCSANOW, &attr );
291    if ( sc != 0 ) {
292      printf( "ERROR - return %d\n", sc );
293      rtems_test_exit(0);
294    }
295  }
296}
297
298/*
299 *  Test all the parity options
300 */
301void test_termios_set_parity(
302  int test
303)
304{
305  int             sc;
306  int             i;
307  struct termios  attr;
308
309  puts(
310    "\n"
311    "Test termios setting device parity ..."
312  );
313  for (i=0 ; parity_table[i].constant != -1 ; i++ ) {
314    sc = tcgetattr( test, &attr );
315    if ( sc != 0 ) {
316      printf( "ERROR - return %d\n", sc );
317      rtems_test_exit(0);
318    }
319
320    attr.c_cflag &= ~(PARENB|PARODD);
321    attr.c_cflag |= parity_table[i].constant;
322
323    printf( "tcsetattr(TCSANOW, %s) - OK\n", parity_table[i].parity );
324    sc = tcsetattr( test, TCSANOW, &attr );
325    if ( sc != 0 ) {
326      printf( "ERROR - return %d\n", sc );
327      rtems_test_exit(0);
328    }
329
330    printf( "tcsetattr(TCSADRAIN, %s) - OK\n", parity_table[i].parity );
331    sc = tcsetattr( test, TCSANOW, &attr );
332    if ( sc != 0 ) {
333      printf( "ERROR - return %d\n", sc );
334      rtems_test_exit(0);
335    }
336  }
337}
338
339/*
340 *  Test all the stop bit options
341 */
342void test_termios_set_stop_bits(
343  int test
344)
345{
346  int             sc;
347  int             i;
348  struct termios  attr;
349
350  puts(
351    "\n"
352    "Test termios setting device character size ..."
353  );
354  for (i=0 ; stop_bits_table[i].constant != -1 ; i++ ) {
355    sc = tcgetattr( test, &attr );
356    if ( sc != 0 ) {
357      printf( "ERROR - return %d\n", sc );
358      rtems_test_exit(0);
359    }
360
361    attr.c_cflag &= ~CSTOPB;
362    attr.c_cflag |= stop_bits_table[i].constant;
363
364    printf( "tcsetattr(TCSANOW, %d bit%s) - OK\n",
365      stop_bits_table[i].stop,
366      ((stop_bits_table[i].stop == 1) ? "" : "s")
367    );
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, %d bits) - OK\n", stop_bits_table[i].stop );
375    sc = tcsetattr( test, TCSANOW, &attr );
376    if ( sc != 0 ) {
377      printf( "ERROR - return %d\n", sc );
378      rtems_test_exit(0);
379    }
380  }
381}
382
383void test_termios_cfoutspeed(void)
384{
385  int i;
386  int sc;
387  speed_t speed;
388  struct termios term;
389  tcflag_t        bad;
390
391  bad = CBAUD << 1;
392  memset( &term, '\0', sizeof(term) );
393  puts( "cfsetospeed(BAD BAUD) - EINVAL" );
394  sc = cfsetospeed( &term, bad );
395  rtems_test_assert( sc == -1 );
396  rtems_test_assert( errno == EINVAL );
397
398  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
399    memset( &term, '\0', sizeof(term) );
400    printf(
401      "cfsetospeed(B%" PRIdrtems_termios_baud_t ") - OK\n",
402      baud_table[i].baud
403    );
404    sc = cfsetospeed( &term, baud_table[i].constant );
405    rtems_test_assert( !sc );
406    printf(
407      "cfgetospeed(B%" PRIdrtems_termios_baud_t ") - OK\n",
408      baud_table[i].baud
409    );
410    speed = cfgetospeed( &term );
411    rtems_test_assert( speed == baud_table[i].constant );
412  }
413}
414
415void test_termios_cfinspeed(void)
416{
417  int             i;
418  int             sc;
419  speed_t         speed;
420  struct termios  term;
421  tcflag_t        bad;
422
423  bad = CBAUD << 1;
424  memset( &term, '\0', sizeof(term) );
425  puts( "cfsetispeed(BAD BAUD) - EINVAL" );
426  sc = cfsetispeed( &term, bad );
427  rtems_test_assert( sc == -1 );
428  rtems_test_assert( errno == EINVAL );
429
430  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
431    memset( &term, '\0', sizeof(term) );
432    printf(
433      "cfsetispeed(B%" PRIdrtems_termios_baud_t ") - OK\n",
434      baud_table[i].baud
435    );
436    sc = cfsetispeed( &term, baud_table[i].constant );
437    rtems_test_assert( !sc );
438
439    printf(
440      "cfgetispeed(B%" PRIdrtems_termios_baud_t ") - OK\n",
441      baud_table[i].baud
442    );
443    speed = cfgetispeed( &term );
444    rtems_test_assert( speed == baud_table[i].constant );
445  }
446}
447
448rtems_task Init(
449  rtems_task_argument ignored
450)
451{
452  int                       rc;
453  rtems_status_code         sc;
454  rtems_device_major_number registered;
455  int                       test;
456
457  puts( "\n\n*** TEST TERMIOS 01 ***" );
458
459  test_termios_baud2index();
460  test_termios_baud2number();
461  test_termios_number_to_baud();
462
463  puts(
464    "\n"
465    "Init - rtems_io_register_driver - Termios Test Driver - OK"
466  );
467  sc = rtems_io_register_driver( 0, &test_driver, &registered );
468  printf( "Init - Major slot returned = %d\n", (int) registered );
469  directive_failed( sc, "rtems_io_register_driver" );
470
471  /*
472   * Test baud rate
473   */
474  puts( "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
475  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
476  if ( test == -1 ) {
477    printf( "ERROR - baud opening test device (%d)\n", test );
478    rtems_test_exit(0);
479  }
480
481  test_termios_set_baud(test);
482
483  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
484  rc = close( test );
485  if ( rc != 0 ) {
486    printf( "ERROR - baud close test device (%d) %s\n", test, strerror(errno) );
487    rtems_test_exit(0);
488  }
489
490  /*
491   * Test character size
492   */
493  puts(
494    "\n"
495    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
496  );
497  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
498  if ( test == -1 ) {
499    printf( "ERROR - size open test device (%d) %s\n", test, strerror(errno) );
500    rtems_test_exit(0);
501  }
502
503  test_termios_set_charsize(test);
504
505  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
506  rc = close( test );
507  if ( rc != 0 ) {
508    printf( "ERROR - size close test device (%d) %s\n", test, strerror(errno) );
509    rtems_test_exit(0);
510  }
511
512  /*
513   * Test parity
514   */
515  puts(
516    "\n"
517    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
518  );
519  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
520  if ( test == -1 ) {
521    printf( "ERROR - parity open test device (%d) %s\n",
522      test, strerror(errno) );
523    rtems_test_exit(0);
524  }
525
526  test_termios_set_parity(test);
527
528  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
529  rc = close( test );
530  if ( rc != 0 ) {
531    printf( "ERROR - parity close test device (%d) %s\n",
532      test, strerror(errno) );
533    rtems_test_exit(0);
534  }
535
536  /*
537   * Test stop bits
538   */
539  puts(
540    "\n"
541    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
542  );
543  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
544  if ( test == -1 ) {
545    printf( "ERROR - stop bits open test device (%d) %s\n",
546      test, strerror(errno) );
547    rtems_test_exit(0);
548  }
549
550  test_termios_set_stop_bits(test);
551
552  test_termios_cfoutspeed();
553
554  test_termios_cfinspeed();
555
556  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
557  rc = close( test );
558  if ( rc != 0 ) {
559    printf( "ERROR - stop bits close test device (%d) %s\n",
560      test, strerror(errno) );
561    rtems_test_exit(0);
562  }
563
564  puts( "*** END OF TEST TERMIOS 01 ***" );
565  rtems_test_exit(0);
566}
567
568/* configuration information */
569
570#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
571#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
572
573/* include an extra slot for registering the termios one dynamically */
574#define CONFIGURE_MAXIMUM_DRIVERS 3
575
576/* one for the console and one for the test port */
577#define CONFIGURE_NUMBER_OF_TERMIOS_PORTS 3
578
579/* we need to be able to open the test device */
580#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
581
582#define CONFIGURE_MAXIMUM_TASKS         1
583#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
584
585#define CONFIGURE_INIT
586#include <rtems/confdefs.h>
587
588/* global variables */
Note: See TracBrowser for help on using the repository browser.