summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/termios10/termios10impl.h
blob: a01f203857c6a7b90cd961640faa03acf953e0cc (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
/*
 *  COPYRIGHT (c) 1989-2012,2019.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  SPDX-License-Identifier: BSD-2-Clause
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tmacros.h>
#include "test_support.h"

#ifdef INTERRUPT_DRIVEN
#include "../termios04/termios_testdriver_intr.h"
const char rtems_test_name[] = "TERMIOS 11 -- Interrupt driven";
#else
#include "../termios03/termios_testdriver_polled.h"
const char rtems_test_name[] = "TERMIOS 10 -- Polled";
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <termios.h>
#include <rtems/dumpbuf.h>
#include <rtems/libio.h>

int Test_fd;

static void open_it(void)
{
  Test_fd = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
  rtems_test_assert( Test_fd != -1 );
}

static void change_lflag( const char *desc, int mask, int new )
{ 
  int            rc;
  struct termios attr;
  
  if (desc) {
    printf( "Changing c_lflag to: %s\n", desc );
  }

  rc = tcgetattr( Test_fd, &attr );
  rtems_test_assert( rc == 0 );
  
  attr.c_lflag &= ~mask;
  attr.c_lflag |= new;
  
  rc = tcsetattr( Test_fd, TCSANOW, &attr );
  rtems_test_assert( rc == 0 );
}


static void read_it(ssize_t expected, int expected_intr)
{
  ssize_t rc;
  char    buf[32];

  rtems_test_assert( expected <= sizeof(buf) );

  rc = read( Test_fd, buf, expected ); 
  if (expected_intr) {
    rtems_test_assert( rc == -1 );
    rtems_test_assert( errno == EINTR );
  } else {
    if ( expected != rc )
      printf( "ERROR - expected=%zd rc=%zd\n", expected, rc );
    rtems_test_assert( expected == rc );
  }
}

static void close_it(void)
{
  int rc;

  rc = close( Test_fd );
  rtems_test_assert( rc == 0 );
}

volatile int sigint_occurred = 0;
volatile int sigquit_occurred = 0;

static void sigint_handler(int signo)
{
  rtems_test_assert(signo == SIGINT);
  sigint_occurred = 1; 
}

static void sigquit_handler(int signo)
{
  rtems_test_assert(signo == SIGQUIT);
  sigquit_occurred = 1; 
}

static void test_read_for_signal(
  const char *description,
  int         isig_value,
  char        c,
  int         sigint_expected,
  int         sigquit_expected
)
{
  char expected[3];

  printf("Test read for %s\n", description);

  expected[0] = c;
  expected[1] = '\n'; /* in canonical mode, so need \n for read to return */
  expected[2] = '\0';

  sigint_occurred  = 0;
  sigquit_occurred = 0;

  open_it();

  change_lflag(NULL, ISIG, isig_value);

  termios_test_driver_set_rx( expected, 2 );

  read_it(1, (sigint_expected || sigquit_expected));

  rtems_test_assert(sigint_occurred == sigint_expected);
  rtems_test_assert(sigquit_occurred == sigquit_expected);
  close_it();
}

/*
 * Use a POSIX init thread so signals are enabled.
 */
static void *POSIX_Init(void *argument)
{
  int rc;

  TEST_BEGIN();

  signal(SIGINT, sigint_handler);
  signal(SIGQUIT, sigquit_handler);

  puts( "Exercise default ISIG handler with ISIG enabled");
  test_read_for_signal("VKILL - no signals", ISIG, '\003', 0, 0);
  test_read_for_signal("VQUIT - no signals", ISIG, '\034', 0, 0);

  puts( "Exercise POSIX ISIG handler with ISIG enabled");
  rc = rtems_termios_register_isig_handler(rtems_termios_posix_isig_handler);
  rtems_test_assert( rc == 0 );
  test_read_for_signal("VKILL - signal caught", ISIG, '\003', 1, 0);
  test_read_for_signal("VQUIT - signal caught", ISIG, '\034', 0, 1);

  puts( "Exercise default ISIG handler with ISIG enabled");
  rc = rtems_termios_register_isig_handler(rtems_termios_default_isig_handler);
  rtems_test_assert( rc == 0 );
  test_read_for_signal("VKILL - signal caught", ISIG, '\003', 0, 0);
  test_read_for_signal("VQUIT - signal caught", ISIG, '\034', 0, 0);

  puts( "Exercise POSIX ISIG handler with ISIG disabled");
  rc = rtems_termios_register_isig_handler(rtems_termios_posix_isig_handler);
  rtems_test_assert( rc == 0 );
  test_read_for_signal("VKILL - signal caught", 0, '\003', 0, 0);
  test_read_for_signal("VQUIT - signal caught", 0, '\034', 0, 0);

  TEST_END();

  rtems_test_exit(0);
}

/* configuration information */

#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_EXTRA_DRIVERS \
  TERMIOS_TEST_DRIVER_TABLE_ENTRY

/* we need to be able to open the test device */
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
#define CONFIGURE_MAXIMUM_POSIX_THREADS     1
#define CONFIGURE_MAXIMUM_TIMERS            2
#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_POSIX_INIT_THREAD_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>
/* end of file */