summaryrefslogtreecommitdiffstats
path: root/testsuite/termios/test_termios_utilities.c
blob: ee3e0fb6af1ca6d0c1ae779b66acc8e24cc8d312 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 *  COPYRIGHT (c) 1989-2017.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#include <machine/rtems-bsd-kernel-space.h>

#include <sys/param.h>
#include <sys/tty.h>
#include <sys/ttydisc.h>

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <rtems/console.h>
#include <rtems/dumpbuf.h>
#include <rtems/shell.h>
#include <rtems/bsd/bsd.h>

#include <termios.h>
#include <rtems/libcsupport.h>
#include <rtems/termiostypes.h>

#include "../termios/test_termios_driver.h"
#include "../termios/test_termios_utilities.h"

int Test_fd;
uint8_t read_helper_buffer[256];
uint8_t Tx_Buffer[TX_MAX];

void open_it(void)
{
  puts( "open(" TERMIOS_TEST_DRIVER_DEVICE_NAME ") - OK " );
  Test_fd = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
  assert( Test_fd != -1 );

  change_iflag("BRKINT|ICRNL|IXON|IMAXBEL", 0xFFFFFFFF, BRKINT|ICRNL|IXON|IMAXBEL);
  change_lflag("ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOCTL", 0xFFFFFFFF, ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOCTL);
  change_oflag("OPOST|ONLCR|OXTABS", 0xFFFFFFFF, OPOST|ONLCR|OXTABS);
}

void close_it(void)
{
  int rc;

  puts( "close(" TERMIOS_TEST_DRIVER_DEVICE_NAME ") - OK " );
  rc = close( Test_fd );
  assert( rc == 0 );
}

void read_it( ssize_t expected )
{
  ssize_t rc;
  char    buf[32];

  assert( expected <= sizeof(buf) );

  printf( "read - %zd expected\n", expected );
  rc = read( Test_fd, buf, expected );
  if ( expected != rc )
    printf( "ERROR - expected=%zd rc=%zd\n", expected, rc );
  assert( expected == rc );
}

void write_it(void)
{
  ssize_t sc;
  char    ch[10] = "PPPD TEST";

  puts( "write(PPPD TEST) - OK " );
  sc = write(Test_fd, ch, sizeof(ch));
  assert( sc == sizeof(ch) );
}

void change_iflag( const char *desc, int mask, int new )
{
  int            rc;
  struct termios attr;

  printf( "Changing c_iflag to: %s\n", desc );
  rc = tcgetattr( Test_fd, &attr );
  assert( rc == 0 );

  attr.c_iflag &= ~mask;
  attr.c_iflag |= new;

  rc = tcsetattr( Test_fd, TCSANOW, &attr );
  assert( rc == 0 );
}

void change_lflag( const char *desc, int mask, int new )
{
  int            rc;
  struct termios attr;

  printf( "Changing c_lflag to: %s\n", desc );
  rc = tcgetattr( Test_fd, &attr );
  assert( rc == 0 );

  attr.c_lflag &= ~mask;
  attr.c_lflag |= new;

  rc = tcsetattr( Test_fd, TCSANOW, &attr );
  assert( rc == 0 );
}

void change_oflag( const char *desc, int mask, int new )
{
  int            rc;
  struct termios attr;

  printf( "Changing c_oflag to: %s\n", desc );
  rc = tcgetattr( Test_fd, &attr );
  assert( rc == 0 );

  attr.c_oflag &= ~mask;
  attr.c_oflag |= new;

  rc = tcsetattr( Test_fd, TCSANOW, &attr );
  assert( rc == 0 );
}

void change_vmin_vtime( const char *desc, int min, int time )
{
  int            rc;
  struct termios attr;

  printf( "Changing %s - VMIN=%d VTIME=%d\n", desc, min, time );
  rc = tcgetattr( Test_fd, &attr );
  assert( rc == 0 );

  attr.c_cc[VMIN] = min;
  attr.c_cc[VTIME] = time;

  rc = tcsetattr( Test_fd, TCSANOW, &attr );
  assert( rc == 0 );
}

void enable_non_blocking(bool enable)
{
  int flags;
  int rv;

  flags = fcntl(Test_fd, F_GETFL, 0);
  assert(flags >= 0);

  if (enable) {
    flags |= O_NONBLOCK;
  } else {
    flags &= ~O_NONBLOCK;
  }

  rv = fcntl(Test_fd, F_SETFL, flags);
  assert(rv == 0);
}

void write_helper( int fd, const char *c )
{
  size_t   len;
  int      rc;

  len = strlen( c );
  printf( "Writing: %s", c );

  rc = write( fd, c, len );
  assert( rc == len );

  termios_test_driver_dump_tx("Transmitted");
}

void read_helper( int fd, const char *expected )
{
  int    rc;
  size_t len;

  len = strlen( expected );

  termios_test_driver_set_rx( expected, len );
  printf( "\nReading (expected):\n" );
  rtems_print_buffer( (unsigned char *)expected, len-1 );

  rc = read( fd, read_helper_buffer, sizeof(read_helper_buffer) );
  assert( rc != -1 );

  printf( "Read %d bytes from read(2)\n", rc );
  rtems_print_buffer( read_helper_buffer, rc );

  termios_test_driver_dump_tx("Echoed");
}

int termios_test_driver_read_tx(void)
{
  int Tx_Index = 0;
  int Tx_Size = TX_MAX;
  int Tx_Length;

  for (;;) {
    Tx_Length = ttydisc_getc(tt_softc.tp, &Tx_Buffer[Tx_Index], Tx_Size);
    if (Tx_Length == 0)
        break;
    Tx_Index += Tx_Length;
    Tx_Size -= Tx_Length;
  }

  return Tx_Index;
}

void termios_test_driver_dump_tx(const char *c)
{
  int Tx_Index;

  Tx_Index = termios_test_driver_read_tx();
  printf( "%s %d characters\n", c, Tx_Index );
  if( Tx_Index > 0 ) {
    rtems_print_buffer( &Tx_Buffer[0], Tx_Index );
  }
}

void termios_test_driver_set_rx( const void *p, size_t len )
{
  tcflush(Test_fd, TCIFLUSH );
  ttydisc_rint_simple(tt_softc.tp, p, len);
  ttydisc_rint_done(tt_softc.tp);
}

void termios_test_driver_set_rx_char( char ch )
{
  char chs[1];

  chs[0] = ch;
  termios_test_driver_set_rx( &chs[0], 1 );
}