source: rtems/testsuites/libtests/termios01/init.c @ 24a5a14

4.115
Last change on this file since 24a5a14 was ab59de45, checked in by Joel Sherrill <joel.sherrill@…>, on 06/23/10 at 23:24:11

2010-06-23 Joel Sherrill <joel.sherrilL@…>

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