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

4.104.115
Last change on this file since b1274bd9 was b1274bd9, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/30/09 at 03:33:25

Whitespace removal.

  • 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 <assert.h>
14#include <termios.h>
15#include <rtems/termiostypes.h>
16#include <fcntl.h>
17#include <sys/errno.h>
18
19/* rtems_termios_baud_t is a typedefs to int32_t */
20#define PRIdrtems_termios_baud_t PRId32
21
22/*
23 *  Termios Test Driver
24 */
25#include "termios_testdriver.h"
26
27rtems_driver_address_table test_driver = TERMIOS_TEST_DRIVER_TABLE_ENTRY;
28
29/*
30 *  Baud Rate Constant Mapping Entry
31 */
32typedef struct {
33  int constant;
34  rtems_termios_baud_t baud;
35} termios_baud_test_r;
36
37/*
38 *  Baud Rate Constant Mapping Table
39 */
40termios_baud_test_r baud_table[] = {
41  { B0,           0 },
42  { B50,         50 },
43  { B75,         75 },
44  { B110,       110 },
45  { B134,       134 },
46  { B150,       150 },
47  { B200,       200 },
48  { B300,       300 },
49  { B600,       600 },
50  { B1200,     1200 },
51  { B1800,     1800 },
52  { B2400,     2400 },
53  { B4800,     4800 },
54  { B9600,     9600 },
55  { B19200,   19200 },
56  { B38400,   38400 },
57  { B57600,   57600 },
58  { B115200, 115200 },
59  { B230400, 230400 },
60  { B460800, 460800 },
61  { -1,      -1     }
62};
63
64/*
65 *  Character Size Constant Mapping Entry
66 */
67typedef struct {
68  int constant;
69  int bits;
70} termios_character_size_test_r;
71
72/*
73 *  Character Size Constant Mapping Table
74 */
75termios_character_size_test_r char_size_table[] = {
76  { CS5,      5 },
77  { CS6,      6 },
78  { CS7,      7 },
79  { CS8,      8 },
80  { -1,      -1 }
81};
82
83/*
84 *  Parity Constant Mapping Entry
85 */
86typedef struct {
87  int        constant;
88  const char *parity;
89} termios_parity_test_r;
90
91/*
92 *  Parity Constant Mapping Table
93 */
94termios_parity_test_r parity_table[] = {
95  { 0,                "none" },
96  { PARENB,           "even" },
97  { PARENB | PARODD,  "odd" },
98  { -1,               NULL }
99};
100
101/*
102 *  Stop Bit Constant Mapping Entry
103 */
104typedef struct {
105  int   constant;
106  int   stop;
107} termios_stop_bits_test_r;
108
109/*
110 *  Stop Bit Constant Mapping Table
111 */
112termios_stop_bits_test_r stop_bits_table[] = {
113  { 0,       1 },
114  { CSTOPB,  2 },
115  { -1,     -1 }
116};
117
118/*
119 *  Test converting baud rate into an index
120 */
121void test_termios_baud2index(void)
122{
123  int i;
124  int index;
125
126  puts(
127    "\n"
128    "Test termios_baud2index..."
129  );
130  puts( "termios_baud_to_index(-2) - NOT OK" );
131  i = rtems_termios_baud_to_index( -2 );
132  assert ( i == -1 );
133
134  puts( "termios_baud_to_index(572) - NOT OK" );
135  i = rtems_termios_baud_to_index( -2 );
136  assert ( i == -1 );
137
138  if ( i != -1 )
139  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
140    printf( "termios_baud_to_index(B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
141    index = rtems_termios_baud_to_index( baud_table[i].constant );
142    if ( index != i ) {
143      printf( "ERROR - returned %d should be %d\n", index, i );
144      rtems_test_exit(0);
145    }
146  }
147}
148
149/*
150 *  Test converting termios baud constant to baud number
151 */
152void test_termios_baud2number(void)
153{
154  int i;
155  int number;
156
157  puts(
158    "\n"
159    "Test termios_baud2number..."
160  );
161  puts( "termios_baud_to_number(-2) - NOT OK" );
162  i = rtems_termios_baud_to_number( -2 );
163  assert ( i == -1 );
164
165  puts( "termios_baud_to_number(572) - NOT OK" );
166  i = rtems_termios_baud_to_number( -2 );
167  assert ( i == -1 );
168
169  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
170    printf( "termios_baud_to_number(B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
171    number = rtems_termios_baud_to_number( baud_table[i].constant );
172    if ( number != baud_table[i].baud ) {
173      printf(
174        "ERROR - returned %d should be %" PRIdrtems_termios_baud_t "\n",
175        number,
176        baud_table[i].baud
177      );
178      rtems_test_exit(0);
179    }
180  }
181}
182
183/*
184 *  Test converting baud number to termios baud constant
185 */
186void test_termios_number_to_baud(void)
187{
188  int i;
189  int termios_baud;
190
191  puts(
192    "\n"
193    "Test termios_number_to_baud..."
194  );
195  puts( "termios_number_to_baud(-2) - NOT OK" );
196  i = rtems_termios_number_to_baud( -2 );
197  assert ( i == -1 );
198
199  puts( "termios_number_to_baud(572) - NOT OK" );
200  i = rtems_termios_number_to_baud( -2 );
201  assert ( i == -1 );
202
203  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
204    printf( "termios_number_to_baud(B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
205    termios_baud = rtems_termios_number_to_baud( baud_table[i].baud );
206    if ( termios_baud != baud_table[i].constant ) {
207      printf(
208        "ERROR - returned %d should be %d\n",
209        termios_baud,
210        baud_table[i].constant
211      );
212      rtems_test_exit(0);
213    }
214  }
215}
216
217/*
218 *  Test all the baud rate options
219 */
220void test_termios_set_baud(
221  int test
222)
223{
224  int             sc;
225  int             i;
226  struct termios  attr;
227
228  puts( "Test termios setting device baud rate..." );
229  for (i=0 ; baud_table[i].constant != -1 ; i++ ) {
230    sc = tcgetattr( test, &attr );
231    if ( sc != 0 ) {
232      printf( "ERROR - return %d\n", sc );
233      rtems_test_exit(0);
234    }
235
236    attr.c_cflag &= ~CBAUD;
237    attr.c_cflag |= baud_table[i].constant;
238
239    printf( "tcsetattr(TCSANOW, B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
240    sc = tcsetattr( test, TCSANOW, &attr );
241    if ( sc != 0 ) {
242      printf( "ERROR - return %d\n", sc );
243      rtems_test_exit(0);
244    }
245
246    printf( "tcsetattr(TCSADRAIN, B%" PRIdrtems_termios_baud_t ") - OK\n", baud_table[i].baud );
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}
254
255/*
256 *  Test all the character size options
257 */
258void test_termios_set_charsize(
259  int test
260)
261{
262  int             sc;
263  int             i;
264  struct termios  attr;
265
266  puts(
267    "\n"
268    "Test termios setting device character size ..."
269  );
270  for (i=0 ; char_size_table[i].constant != -1 ; i++ ) {
271    sc = tcgetattr( test, &attr );
272    if ( sc != 0 ) {
273      printf( "ERROR - return %d\n", sc );
274      rtems_test_exit(0);
275    }
276
277    attr.c_cflag &= ~CSIZE;
278    attr.c_cflag |= char_size_table[i].constant;
279
280    printf( "tcsetattr(TCSANOW, CS%d) - OK\n", char_size_table[i].bits );
281    sc = tcsetattr( test, TCSANOW, &attr );
282    if ( sc != 0 ) {
283      printf( "ERROR - return %d\n", sc );
284      rtems_test_exit(0);
285    }
286
287    printf( "tcsetattr(TCSADRAIN, CS%d) - OK\n", char_size_table[i].bits );
288    sc = tcsetattr( test, TCSANOW, &attr );
289    if ( sc != 0 ) {
290      printf( "ERROR - return %d\n", sc );
291      rtems_test_exit(0);
292    }
293  }
294}
295
296/*
297 *  Test all the parity options
298 */
299void test_termios_set_parity(
300  int test
301)
302{
303  int             sc;
304  int             i;
305  struct termios  attr;
306
307  puts(
308    "\n"
309    "Test termios setting device parity ..."
310  );
311  for (i=0 ; parity_table[i].constant != -1 ; i++ ) {
312    sc = tcgetattr( test, &attr );
313    if ( sc != 0 ) {
314      printf( "ERROR - return %d\n", sc );
315      rtems_test_exit(0);
316    }
317
318    attr.c_cflag &= ~(PARENB|PARODD);
319    attr.c_cflag |= parity_table[i].constant;
320
321    printf( "tcsetattr(TCSANOW, %s) - OK\n", parity_table[i].parity );
322    sc = tcsetattr( test, TCSANOW, &attr );
323    if ( sc != 0 ) {
324      printf( "ERROR - return %d\n", sc );
325      rtems_test_exit(0);
326    }
327
328    printf( "tcsetattr(TCSADRAIN, %s) - OK\n", parity_table[i].parity );
329    sc = tcsetattr( test, TCSANOW, &attr );
330    if ( sc != 0 ) {
331      printf( "ERROR - return %d\n", sc );
332      rtems_test_exit(0);
333    }
334  }
335}
336
337/*
338 *  Test all the stop bit options
339 */
340void test_termios_set_stop_bits(
341  int test
342)
343{
344  int             sc;
345  int             i;
346  struct termios  attr;
347
348  puts(
349    "\n"
350    "Test termios setting device character size ..."
351  );
352  for (i=0 ; stop_bits_table[i].constant != -1 ; i++ ) {
353    sc = tcgetattr( test, &attr );
354    if ( sc != 0 ) {
355      printf( "ERROR - return %d\n", sc );
356      rtems_test_exit(0);
357    }
358
359    attr.c_cflag &= ~CSTOPB;
360    attr.c_cflag |= stop_bits_table[i].constant;
361
362    printf( "tcsetattr(TCSANOW, %d bit%s) - OK\n",
363      stop_bits_table[i].stop,
364      ((stop_bits_table[i].stop == 1) ? "" : "s")
365    );
366    sc = tcsetattr( test, TCSANOW, &attr );
367    if ( sc != 0 ) {
368      printf( "ERROR - return %d\n", sc );
369      rtems_test_exit(0);
370    }
371
372    printf( "tcsetattr(TCSADRAIN, %d bits) - OK\n", stop_bits_table[i].stop );
373    sc = tcsetattr( test, TCSANOW, &attr );
374    if ( sc != 0 ) {
375      printf( "ERROR - return %d\n", sc );
376      rtems_test_exit(0);
377    }
378  }
379}
380
381rtems_task Init(
382  rtems_task_argument ignored
383)
384{
385  int                       rc;
386  rtems_status_code         sc;
387  rtems_device_major_number registered;
388  int                       test;
389
390  puts( "\n\n*** TEST TERMIOS 01 ***" );
391
392  test_termios_baud2index();
393  test_termios_baud2number();
394  test_termios_number_to_baud();
395
396  puts(
397    "\n"
398    "Init - rtems_io_register_driver - Termios Test Driver - OK"
399  );
400  sc = rtems_io_register_driver( 0, &test_driver, &registered );
401  printf( "Init - Major slot returned = %d\n", (int) registered );
402  directive_failed( sc, "rtems_io_register_driver" );
403
404  /*
405   * Test baud rate
406   */
407  puts( "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
408  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
409  if ( test == -1 ) {
410    printf( "ERROR - baud opening test device (%d)\n", test );
411    rtems_test_exit(0);
412  }
413
414  test_termios_set_baud(test);
415
416  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
417  rc = close( test );
418  if ( rc != 0 ) {
419    printf( "ERROR - baud close test device (%d) %s\n", test, strerror(errno) );
420    rtems_test_exit(0);
421  }
422
423  /*
424   * Test character size
425   */
426  puts(
427    "\n"
428    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
429  );
430  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
431  if ( test == -1 ) {
432    printf( "ERROR - size open test device (%d) %s\n", test, strerror(errno) );
433    rtems_test_exit(0);
434  }
435
436  test_termios_set_charsize(test);
437
438  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
439  rc = close( test );
440  if ( rc != 0 ) {
441    printf( "ERROR - size close test device (%d) %s\n", test, strerror(errno) );
442    rtems_test_exit(0);
443  }
444
445  /*
446   * Test parity
447   */
448  puts(
449    "\n"
450    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
451  );
452  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
453  if ( test == -1 ) {
454    printf( "ERROR - parity open test device (%d) %s\n",
455      test, strerror(errno) );
456    rtems_test_exit(0);
457  }
458
459  test_termios_set_parity(test);
460
461  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
462  rc = close( test );
463  if ( rc != 0 ) {
464    printf( "ERROR - parity close test device (%d) %s\n",
465      test, strerror(errno) );
466    rtems_test_exit(0);
467  }
468
469  /*
470   * Test stop bits
471   */
472  puts(
473    "\n"
474    "Init - open - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
475  );
476  test = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
477  if ( test == -1 ) {
478    printf( "ERROR - stop bits open test device (%d) %s\n",
479      test, strerror(errno) );
480    rtems_test_exit(0);
481  }
482
483  test_termios_set_stop_bits(test);
484
485  puts( "Init - close - " TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" );
486  rc = close( test );
487  if ( rc != 0 ) {
488    printf( "ERROR - stop bits close test device (%d) %s\n",
489      test, strerror(errno) );
490    rtems_test_exit(0);
491  }
492
493  puts( "*** END OF TEST TERMIOS 01 ***" );
494  rtems_test_exit(0);
495}
496
497/* configuration information */
498
499#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
500#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
501
502/* include an extra slot for registering the termios one dynamically */
503#define CONFIGURE_MAXIMUM_DRIVERS 3
504
505/* one for the console and one for the test port */
506#define CONFIGURE_NUMBER_OF_TERMIOS_PORTS 2
507
508/* we need to be able to open the test device */
509#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
510
511#define CONFIGURE_MAXIMUM_TASKS         1
512#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
513
514#define CONFIGURE_INIT
515#include <rtems/confdefs.h>
516
517/* global variables */
Note: See TracBrowser for help on using the repository browser.