source: rtems-libbsd/testsuite/termios/test_termios_utilities.c @ 8e35aaa

55-freebsd-126-freebsd-12
Last change on this file since 8e35aaa was 8e35aaa, checked in by Sebastian Huber <sebastian.huber@…>, on Aug 20, 2018 at 1:19:37 PM

tests/termios: Fix warnings

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2017.
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.org/license/LICENSE.
8 */
9
10#include <machine/rtems-bsd-kernel-space.h>
11
12#include <sys/param.h>
13#include <sys/tty.h>
14#include <sys/ttydisc.h>
15
16#include <assert.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <inttypes.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include <rtems/console.h>
25#include <rtems/dumpbuf.h>
26#include <rtems/shell.h>
27#include <rtems/bsd/bsd.h>
28
29#include <termios.h>
30#include <rtems/libcsupport.h>
31#include <rtems/termiostypes.h>
32
33#include "../termios/test_termios_driver.h"
34#include "../termios/test_termios_utilities.h"
35
36int Test_fd;
37uint8_t read_helper_buffer[256];
38uint8_t Tx_Buffer[TX_MAX];
39
40void open_it(void)
41{
42  puts( "open(" TERMIOS_TEST_DRIVER_DEVICE_NAME ") - OK " );
43  Test_fd = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
44  assert( Test_fd != -1 );
45
46  change_iflag("BRKINT|ICRNL|IXON|IMAXBEL", 0xFFFFFFFF, BRKINT|ICRNL|IXON|IMAXBEL);
47  change_lflag("ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOCTL", 0xFFFFFFFF, ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOCTL);
48  change_oflag("OPOST|ONLCR|OXTABS", 0xFFFFFFFF, OPOST|ONLCR|OXTABS);
49}
50
51void close_it(void)
52{
53  int rc;
54
55  puts( "close(" TERMIOS_TEST_DRIVER_DEVICE_NAME ") - OK " );
56  rc = close( Test_fd );
57  assert( rc == 0 );
58}
59
60void read_it( ssize_t expected )
61{
62  ssize_t rc;
63  char    buf[32];
64
65  assert( expected <= sizeof(buf) );
66
67  printf( "read - %zd expected\n", expected );
68  rc = read( Test_fd, buf, expected );
69  if ( expected != rc )
70    printf( "ERROR - expected=%zd rc=%zd\n", expected, rc );
71  assert( expected == rc );
72}
73
74void write_it(void)
75{
76  ssize_t sc;
77  char    ch[10] = "PPPD TEST";
78
79  puts( "write(PPPD TEST) - OK " );
80  sc = write(Test_fd, ch, sizeof(ch));
81  assert( sc == sizeof(ch) );
82}
83
84void change_iflag( const char *desc, int mask, int new )
85{
86  int            rc;
87  struct termios attr;
88
89  printf( "Changing c_iflag to: %s\n", desc );
90  rc = tcgetattr( Test_fd, &attr );
91  assert( rc == 0 );
92
93  attr.c_iflag &= ~mask;
94  attr.c_iflag |= new;
95
96  rc = tcsetattr( Test_fd, TCSANOW, &attr );
97  assert( rc == 0 );
98}
99
100void change_lflag( const char *desc, int mask, int new )
101{
102  int            rc;
103  struct termios attr;
104
105  printf( "Changing c_lflag to: %s\n", desc );
106  rc = tcgetattr( Test_fd, &attr );
107  assert( rc == 0 );
108
109  attr.c_lflag &= ~mask;
110  attr.c_lflag |= new;
111
112  rc = tcsetattr( Test_fd, TCSANOW, &attr );
113  assert( rc == 0 );
114}
115
116void change_oflag( const char *desc, int mask, int new )
117{
118  int            rc;
119  struct termios attr;
120
121  printf( "Changing c_oflag to: %s\n", desc );
122  rc = tcgetattr( Test_fd, &attr );
123  assert( rc == 0 );
124
125  attr.c_oflag &= ~mask;
126  attr.c_oflag |= new;
127
128  rc = tcsetattr( Test_fd, TCSANOW, &attr );
129  assert( rc == 0 );
130}
131
132void change_vmin_vtime( const char *desc, int min, int time )
133{
134  int            rc;
135  struct termios attr;
136
137  printf( "Changing %s - VMIN=%d VTIME=%d\n", desc, min, time );
138  rc = tcgetattr( Test_fd, &attr );
139  assert( rc == 0 );
140
141  attr.c_cc[VMIN] = min;
142  attr.c_cc[VTIME] = time;
143
144  rc = tcsetattr( Test_fd, TCSANOW, &attr );
145  assert( rc == 0 );
146}
147
148void enable_non_blocking(bool enable)
149{
150  int flags;
151  int rv;
152
153  flags = fcntl(Test_fd, F_GETFL, 0);
154  assert(flags >= 0);
155
156  if (enable) {
157    flags |= O_NONBLOCK;
158  } else {
159    flags &= ~O_NONBLOCK;
160  }
161
162  rv = fcntl(Test_fd, F_SETFL, flags);
163  assert(rv == 0);
164}
165
166void write_helper( int fd, const char *c )
167{
168  size_t   len;
169  int      rc;
170
171  len = strlen( c );
172  printf( "Writing: %s", c );
173
174  rc = write( fd, c, len );
175  assert( rc == len );
176
177  termios_test_driver_dump_tx("Transmitted");
178}
179
180void read_helper( int fd, const char *expected )
181{
182  int    rc;
183  size_t len;
184
185  len = strlen( expected );
186
187  termios_test_driver_set_rx( expected, len );
188  printf( "\nReading (expected):\n" );
189  rtems_print_buffer( (unsigned char *)expected, len-1 );
190
191  rc = read( fd, read_helper_buffer, sizeof(read_helper_buffer) );
192  assert( rc != -1 );
193
194  printf( "Read %d bytes from read(2)\n", rc );
195  rtems_print_buffer( read_helper_buffer, rc );
196
197  termios_test_driver_dump_tx("Echoed");
198}
199
200int termios_test_driver_read_tx(void)
201{
202  int Tx_Index = 0;
203  int Tx_Size = TX_MAX;
204  int Tx_Length;
205
206  for (;;) {
207    Tx_Length = ttydisc_getc(tt_softc.tp, &Tx_Buffer[Tx_Index], Tx_Size);
208    if (Tx_Length == 0)
209        break;
210    Tx_Index += Tx_Length;
211    Tx_Size -= Tx_Length;
212  }
213
214  return Tx_Index;
215}
216
217void termios_test_driver_dump_tx(const char *c)
218{
219  int Tx_Index;
220
221  Tx_Index = termios_test_driver_read_tx();
222  printf( "%s %d characters\n", c, Tx_Index );
223  if( Tx_Index > 0 ) {
224    rtems_print_buffer( &Tx_Buffer[0], Tx_Index );
225  }
226}
227
228void termios_test_driver_set_rx( const void *p, size_t len )
229{
230  tcflush(Test_fd, TCIFLUSH );
231  ttydisc_rint_simple(tt_softc.tp, p, len);
232  ttydisc_rint_done(tt_softc.tp);
233}
234
235void termios_test_driver_set_rx_char( char ch )
236{
237  char chs[1];
238
239  chs[0] = ch;
240  termios_test_driver_set_rx( &chs[0], 1 );
241}
Note: See TracBrowser for help on using the repository browser.