summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/termios09/init.c
blob: 6e651bfafad273d9fe12c229c10ddadea50e5f0b (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (c) 2017 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * 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.
 */

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

#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include <rtems/termiostypes.h>

#include "tmacros.h"

const char rtems_test_name[] = "TERMIOS 9";

#define INTERRUPT 0

#define POLLED 1

#define DEVICE_COUNT 2

#define OUTPUT_BUFFER_SIZE 64

static const char * const paths[DEVICE_COUNT] = {
  "/interrupt",
  "/polled"
};

typedef struct {
  rtems_termios_device_context base;
  rtems_termios_tty *tty;
  size_t output_pending;
  size_t output_count;
  char output_buf[OUTPUT_BUFFER_SIZE];
  int input_char;
} device_context;

typedef struct {
  device_context devices[DEVICE_COUNT];
  int fds[DEVICE_COUNT];
  struct termios term[DEVICE_COUNT];
} test_context;

static test_context test_instance = {
  .devices = {
    {
      .base = RTEMS_TERMIOS_DEVICE_CONTEXT_INITIALIZER("Interrupt")
    }, {
      .base = RTEMS_TERMIOS_DEVICE_CONTEXT_INITIALIZER("Polled"),
      .input_char = -1
    }
  }
};

static bool first_open(
  rtems_termios_tty *tty,
  rtems_termios_device_context *base,
  struct termios *term,
  rtems_libio_open_close_args_t *args
)
{
  device_context *dev = (device_context *) base;

  dev->tty = tty;

  return true;
}

static void write_polled(
  rtems_termios_device_context *base,
  const char *buf,
  size_t len
)
{
  device_context *dev = (device_context *) base;

  rtems_test_assert(dev->output_count + len <= OUTPUT_BUFFER_SIZE);
  memcpy(&dev->output_buf[dev->output_count], buf, len);
  dev->output_count += len;
}

static void write_interrupt(
  rtems_termios_device_context *base,
  const char *buf,
  size_t len
)
{
  device_context *dev = (device_context *) base;

  write_polled(base, buf, len);
  dev->output_pending = len;
}

static int read_polled(rtems_termios_device_context *base)
{
  device_context *dev = (device_context *) base;
  int c = dev->input_char;

  dev->input_char = -1;

  return c;
}

static const rtems_termios_device_handler handlers[DEVICE_COUNT] = {
  {
    .first_open = first_open,
    .write = write_interrupt,
    .mode = TERMIOS_IRQ_DRIVEN
  }, {
    .first_open = first_open,
    .write = write_polled,
    .poll_read = read_polled,
    .mode = TERMIOS_POLLED
  }
};

static void set_term(test_context *ctx, size_t i)
{
  int rv;

  rv = tcsetattr(ctx->fds[i], TCSANOW, &ctx->term[i]);
  rtems_test_assert(rv == 0);
}

static void init_term(test_context *ctx, size_t i)
{
  int rv;

  rv = tcgetattr(ctx->fds[i], &ctx->term[i]);
  rtems_test_assert(rv == 0);

  ctx->term[i].c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
    | INLCR | IGNCR | ICRNL | IXON);
  ctx->term[i].c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  ctx->term[i].c_cflag &= ~(CSIZE | PARENB);
  ctx->term[i].c_cflag |= CS8;
  ctx->term[i].c_oflag &= ~(OPOST | ONLRET | ONLCR | OCRNL | ONLRET
    | TABDLY | OLCUC);

  ctx->term[i].c_cc[VMIN] = 0;
  ctx->term[i].c_cc[VTIME] = 0;

  set_term(ctx, i);
}

static void setup(test_context *ctx)
{
  rtems_status_code sc;
  size_t i;

  rtems_termios_initialize();

  for (i = 0; i < DEVICE_COUNT; ++i) {
    sc = rtems_termios_device_install(
      paths[i],
      &handlers[i],
      NULL,
      &ctx->devices[i].base
    );
    rtems_test_assert(sc == RTEMS_SUCCESSFUL);

    ctx->fds[i] = open(paths[i], O_RDWR);
    rtems_test_assert(ctx->fds[i] >= 0);

    init_term(ctx, i);
  }
}

static void input(test_context *ctx, size_t i, char c)
{
  switch (i) {
    case INTERRUPT:
      rtems_termios_enqueue_raw_characters(ctx->devices[i].tty, &c, sizeof(c));
      break;
    case POLLED:
      ctx->devices[i].input_char = (unsigned char) c;
      break;
    default:
      rtems_test_assert(0);
  }
}

static void clear_set_iflag(
  test_context *ctx,
  size_t i,
  tcflag_t clear,
  tcflag_t set
)
{
  ctx->term[i].c_iflag &= ~clear;
  ctx->term[i].c_iflag |= set;
  set_term(ctx, i);
}

static void test_igncr(test_context *ctx)
{
  size_t i;

  for (i = 0; i < DEVICE_COUNT; ++i) {
    ssize_t n;
    char c;

    c = 'x';

    clear_set_iflag(ctx, i, 0, IGNCR);

    n = read(ctx->fds[i], &c, sizeof(c));
    rtems_test_assert(n == 0);
    rtems_test_assert(c == 'x');

    input(ctx, i, '\r');

    n = read(ctx->fds[i], &c, sizeof(c));
    rtems_test_assert(n == 0);
    rtems_test_assert(c == 'x');

    clear_set_iflag(ctx, i, IGNCR, 0);
    input(ctx, i, '\r');

    n = read(ctx->fds[i], &c, sizeof(c));
    rtems_test_assert(n == 1);
    rtems_test_assert(c == '\r');
  }
}

static void Init(rtems_task_argument arg)
{
  test_context *ctx = &test_instance;

  TEST_BEGIN();

  setup(ctx);
  test_igncr(ctx);

  TEST_END();
  rtems_test_exit(0);
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 5

#define CONFIGURE_MAXIMUM_TASKS 1

#define CONFIGURE_MAXIMUM_SEMAPHORES 7

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>