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

55-freebsd-126-freebsd-12
Last change on this file since 0237319 was 0237319, checked in by Sebastian Huber <sebastian.huber@…>, on May 23, 2017 at 11:18:31 AM

Update due to Newlib 2017-06-07 changes

The following files are now provided by Newlib:

  • arpa/inet.h
  • net/if.h
  • netinet/in.h
  • netinet/tcp.h
  • sys/socket.h
  • sys/uio.h
  • sys/un.h

The <sys/param.h> and <sys/cpuset.h> are now compatible enough to be
used directly.

Update #2833.

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