source: rtems/testsuites/libtests/termios01/init.c @ 500fcd5

4.104.115
Last change on this file since 500fcd5 was 500fcd5, checked in by Joel Sherrill <joel.sherrill@…>, on 12/08/09 at 17:52:49

2009-12-08 Joel Sherrill <joel.sherrill@…>

  • block01/init.c, block02/init.c, block03/init.c, block04/init.c, block05/init.c, block07/init.c, bspcmdline01/init.c, stringto01/init.c, stringto01/stringto_test_template.h, termios01/init.c, termios01/termios_testdriver.c, termios02/init.c: Use rtems_test_assert() consistently instead of system assert(). rtems_test_assert() is designed to integrate into the RTEMS test suite infrastructure.
  • Property mode set to 100644
File size: 11.8 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( "termios_baud_to_index(B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
140    index = rtems_termios_baud_to_index( baud_table[i].constant );
141    if ( index != i ) {
142      printf( "ERROR - returned %d should be %d\n", index, i );
143      rtems_test_exit(0);
144    }
145  }
146}
147
148/*
149 *  Test converting termios baud constant to baud number
150 */
151void test_termios_baud2number(void)
152{
153  int i;
154  int number;
155
156  puts(
157    "\n"
158    "Test termios_baud2number..."
159  );
160  puts( "termios_baud_to_number(-2) - NOT OK" );
161  i = rtems_termios_baud_to_number( -2 );
162  rtems_test_assert( i == -1 );
163
164  puts( "termios_baud_to_number(572) - NOT OK" );
165  i = rtems_termios_baud_to_number( -2 );
166  rtems_test_assert( i == -1 );
167
168  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
169    printf( "termios_baud_to_number(B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
170    number = rtems_termios_baud_to_number( baud_table[i].constant );
171    if ( number != baud_table[i].baud ) {
172      printf(
173        "ERROR - returned %d should be %" PRIdrtems_termios_baud_t "\n",
174        number,
175        baud_table[i].baud
176      );
177      rtems_test_exit(0);
178    }
179  }
180}
181
182/*
183 *  Test converting baud number to termios baud constant
184 */
185void test_termios_number_to_baud(void)
186{
187  int i;
188  int termios_baud;
189
190  puts(
191    "\n"
192    "Test termios_number_to_baud..."
193  );
194  puts( "termios_number_to_baud(-2) - NOT OK" );
195  i = rtems_termios_number_to_baud( -2 );
196  rtems_test_assert( i == -1 );
197
198  puts( "termios_number_to_baud(572) - NOT OK" );
199  i = rtems_termios_number_to_baud( -2 );
200  rtems_test_assert( i == -1 );
201
202  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
203    printf( "termios_number_to_baud(B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
204    termios_baud = rtems_termios_number_to_baud( baud_table[i].baud );
205    if ( termios_baud != baud_table[i].constant ) {
206      printf(
207        "ERROR - returned %d should be %d\n",
208        termios_baud,
209        baud_table[i].constant
210      );
211      rtems_test_exit(0);
212    }
213  }
214}
215
216/*
217 *  Test all the baud rate options
218 */
219void test_termios_set_baud(
220  int test
221)
222{
223  int             sc;
224  int             i;
225  struct termios  attr;
226
227  puts( "Test termios setting device baud rate..." );
228  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
229    sc = tcgetattr( test, &attr );
230    if ( sc != 0 ) {
231      printf( "ERROR - return %d\n", sc );
232      rtems_test_exit(0);
233    }
234
235    attr.c_cflag &= ~CBAUD;
236    attr.c_cflag |= baud_table[i].constant;
237
238    printf( "tcsetattr(TCSANOW, B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
239    sc = tcsetattr( test, TCSANOW, &attr );
240    if ( sc != 0 ) {
241      printf( "ERROR - return %d\n", sc );
242      rtems_test_exit(0);
243    }
244
245    printf( "tcsetattr(TCSADRAIN, B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
246    sc = tcsetattr( test, TCSANOW, &attr );
247    if ( sc != 0 ) {
248      printf( "ERROR - return %d\n", sc );
249      rtems_test_exit(0);
250    }
251  }
252}
253
254/*
255 *  Test all the character size options
256 */
257void test_termios_set_charsize(
258  int test
259)
260{
261  int             sc;
262  int             i;
263  struct termios  attr;
264
265  puts(
266    "\n"
267    "Test termios setting device character size ..."
268  );
269  for (i=0 ; char_size_table[i].constant != -1 ; i++ ) {
270    sc = tcgetattr( test, &attr );
271    if ( sc != 0 ) {
272      printf( "ERROR - return %d\n", sc );
273      rtems_test_exit(0);
274    }
275
276    attr.c_cflag &= ~CSIZE;
277    attr.c_cflag |= char_size_table[i].constant;
278
279    printf( "tcsetattr(TCSANOW, CS%d) - OK\n", char_size_table[i].bits );
280    sc = tcsetattr( test, TCSANOW, &attr );
281    if ( sc != 0 ) {
282      printf( "ERROR - return %d\n", sc );
283      rtems_test_exit(0);
284    }
285
286    printf( "tcsetattr(TCSADRAIN, CS%d) - OK\n", char_size_table[i].bits );
287    sc = tcsetattr( test, TCSANOW, &attr );
288    if ( sc != 0 ) {
289      printf( "ERROR - return %d\n", sc );
290      rtems_test_exit(0);
291    }
292  }
293}
294
295/*
296 *  Test all the parity options
297 */
298void test_termios_set_parity(
299  int test
300)
301{
302  int             sc;
303  int             i;
304  struct termios  attr;
305
306  puts(
307    "\n"
308    "Test termios setting device parity ..."
309  );
310  for (i=0 ; parity_table[i].constant != -1 ; i++ ) {
311    sc = tcgetattr( test, &attr );
312    if ( sc != 0 ) {
313      printf( "ERROR - return %d\n", sc );
314      rtems_test_exit(0);
315    }
316
317    attr.c_cflag &= ~(PARENB|PARODD);
318    attr.c_cflag |= parity_table[i].constant;
319
320    printf( "tcsetattr(TCSANOW, %s) - OK\n", parity_table[i].parity );
321    sc = tcsetattr( test, TCSANOW, &attr );
322    if ( sc != 0 ) {
323      printf( "ERROR - return %d\n", sc );
324      rtems_test_exit(0);
325    }
326
327    printf( "tcsetattr(TCSADRAIN, %s) - OK\n", parity_table[i].parity );
328    sc = tcsetattr( test, TCSANOW, &attr );
329    if ( sc != 0 ) {
330      printf( "ERROR - return %d\n", sc );
331      rtems_test_exit(0);
332    }
333  }
334}
335
336/*
337 *  Test all the stop bit options
338 */
339void test_termios_set_stop_bits(
340  int test
341)
342{
343  int             sc;
344  int             i;
345  struct termios  attr;
346
347  puts(
348    "\n"
349    "Test termios setting device character size ..."
350  );
351  for (i=0 ; stop_bits_table[i].constant != -1 ; i++ ) {
352    sc = tcgetattr( test, &attr );
353    if ( sc != 0 ) {
354      printf( "ERROR - return %d\n", sc );
355      rtems_test_exit(0);
356    }
357
358    attr.c_cflag &= ~CSTOPB;
359    attr.c_cflag |= stop_bits_table[i].constant;
360
361    printf( "tcsetattr(TCSANOW, %d bit%s) - OK\n",
362      stop_bits_table[i].stop,
363      ((stop_bits_table[i].stop == 1) ? "" : "s")
364    );
365    sc = tcsetattr( test, TCSANOW, &attr );
366    if ( sc != 0 ) {
367      printf( "ERROR - return %d\n", sc );
368      rtems_test_exit(0);
369    }
370
371    printf( "tcsetattr(TCSADRAIN, %d bits) - OK\n", stop_bits_table[i].stop );
372    sc = tcsetattr( test, TCSANOW, &attr );
373    if ( sc != 0 ) {
374      printf( "ERROR - return %d\n", sc );
375      rtems_test_exit(0);
376    }
377  }
378}
379
380rtems_task Init(
381  rtems_task_argument ignored
382)
383{
384  int                       rc;
385  rtems_status_code         sc;
386  rtems_device_major_number registered;
387  int                       test;
388
389  puts( "\n\n*** TEST TERMIOS 01 ***" );
390
391  test_termios_baud2index();
392  test_termios_baud2number();
393  test_termios_number_to_baud();
394
395  puts(
396    "\n"
397    "Init - rtems_io_register_driver - Termios Test Driver - OK"
398  );
399  sc = rtems_io_register_driver( 0, &test_driver, &registered );
400  printf( "Init - Major slot returned = %d\n", (int) registered );
401  directive_failed( sc, "rtems_io_register_driver" );
402
403  /*
404   * Test baud rate
405   */
406  puts( "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
407  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
408  if ( test == -1 ) {
409    printf( "ERROR - baud opening test device (%d)\n", test );
410    rtems_test_exit(0);
411  }
412
413  test_termios_set_baud(test);
414
415  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
416  rc = close( test );
417  if ( rc != 0 ) {
418    printf( "ERROR - baud close test device (%d) %s\n", test, strerror(errno) );
419    rtems_test_exit(0);
420  }
421
422  /*
423   * Test character size
424   */
425  puts(
426    "\n"
427    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
428  );
429  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
430  if ( test == -1 ) {
431    printf( "ERROR - size open test device (%d) %s\n", test, strerror(errno) );
432    rtems_test_exit(0);
433  }
434
435  test_termios_set_charsize(test);
436
437  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
438  rc = close( test );
439  if ( rc != 0 ) {
440    printf( "ERROR - size close test device (%d) %s\n", test, strerror(errno) );
441    rtems_test_exit(0);
442  }
443
444  /*
445   * Test parity
446   */
447  puts(
448    "\n"
449    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
450  );
451  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
452  if ( test == -1 ) {
453    printf( "ERROR - parity open test device (%d) %s\n",
454      test, strerror(errno) );
455    rtems_test_exit(0);
456  }
457
458  test_termios_set_parity(test);
459
460  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
461  rc = close( test );
462  if ( rc != 0 ) {
463    printf( "ERROR - parity close test device (%d) %s\n",
464      test, strerror(errno) );
465    rtems_test_exit(0);
466  }
467
468  /*
469   * Test stop bits
470   */
471  puts(
472    "\n"
473    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
474  );
475  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
476  if ( test == -1 ) {
477    printf( "ERROR - stop bits open test device (%d) %s\n",
478      test, strerror(errno) );
479    rtems_test_exit(0);
480  }
481
482  test_termios_set_stop_bits(test);
483
484  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
485  rc = close( test );
486  if ( rc != 0 ) {
487    printf( "ERROR - stop bits close test device (%d) %s\n",
488      test, strerror(errno) );
489    rtems_test_exit(0);
490  }
491
492  puts( "*** END OF TEST TERMIOS 01 ***" );
493  rtems_test_exit(0);
494}
495
496/* configuration information */
497
498#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
499#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
500
501/* include an extra slot for registering the termios one dynamically */
502#define CONFIGURE_MAXIMUM_DRIVERS 3
503
504/* one for the console and one for the test port */
505#define CONFIGURE_NUMBER_OF_TERMIOS_PORTS 2
506
507/* we need to be able to open the test device */
508#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
509
510#define CONFIGURE_MAXIMUM_TASKS         1
511#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
512
513#define CONFIGURE_INIT
514#include <rtems/confdefs.h>
515
516/* global variables */
Note: See TracBrowser for help on using the repository browser.