source: rtems/testsuites/libtests/termios01/termios_testdriver.c

Last change on this file was 150dcf5, checked in by Kinsey Moore <kinsey.moore@…>, on 01/02/24 at 20:36:52

libio: Clean up usage of rtems_termios_device_mode

This cleans up outputUsesInterrupts usage with rtems_termios_device_mode
enum values. The outputUsesInterrupts member was typed as an int, named
as if it were a boolean value, and used as if it were a
rtems_termios_device_mode enum. In this patch, values assigned to
outputUsesInterrupts have been converted to the corresponding
rtems_termios_device_mode enum value, conversions from
deviceOutputUsesInterrupts have been made explicit, and uses of
rtems_termios_device_mode enum values with deviceOutputUsesInterrupts
have been converted to booleans.

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[acceb47]1/* SPDX-License-Identifier: BSD-2-Clause */
2
[4c86e3d]3/**
4 *  @file
[4d5b156]5 *
[4c86e3d]6 *  This file contains a test fixture termios device driver.
7 */
8
9/*
10 *  COPYRIGHT (c) 1989-2012.
[4d5b156]11 *  On-Line Applications Research Corporation (OAR).
12 *
[acceb47]13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
[4d5b156]33 */
34
[7d3f9c6]35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
[4d5b156]39#include "tmacros.h"
40#include <rtems/libio.h>
41#include <stdlib.h>
42#include <termios.h>
[4184a9c]43#include <rtems/termiostypes.h>
[2e54cdc4]44#include <rtems/libcsupport.h>
[c8615dcf]45#include <rtems/malloc.h>
[4d5b156]46#include "termios_testdriver.h"
47
[4c86e3d]48/* forward declarations to avoid warnings */
49int termios_test_driver_inbyte_nonblocking(int port);
50void termios_test_driver_outbyte_polled(int port, char ch);
51ssize_t termios_test_driver_write_support(
52  int         minor,
53  const char *buf,
54  size_t      len
55);
56int termios_test_driver_set_attributes(
57  int                   minor,
58  const struct termios *t
59);
60
[4d5b156]61int termios_test_driver_inbyte_nonblocking( int port )
62{
63  return -1;
64}
65
66void termios_test_driver_outbyte_polled(
67  int  port,
68  char ch
69)
70{
71}
72
[4c86e3d]73ssize_t termios_test_driver_write_support(
74  int         minor,
75  const char *buf,
76  size_t      len
77)
[4d5b156]78{
[ea9a626]79  size_t nwrite = 0;
[4d5b156]80
81  while (nwrite < len) {
[dda7c828]82#if (TERMIOS_TEST_DRIVER_USE_INTERRUPTS)
[4d5b156]83    termios_test_driver_outbyte_interrupt( minor, *buf++ );
84#else
85    termios_test_driver_outbyte_polled( minor, *buf++ );
86#endif
87    nwrite++;
88  }
89  return nwrite;
90}
91
92
93/*
94 *  Set Attributes Handler
95 */
96int termios_test_driver_set_attributes(
97  int                   minor,
98  const struct termios *t
99)
100{
101  int                    baud_requested;
102  int                    number;
103  const char            *parity = "NONE";
104  const char            *char_size = "5";
105  const char            *stop = "NONE";
106
[1c6926c1]107  baud_requested = t->c_ispeed;
[4d5b156]108
[009dfd0]109  number = rtems_termios_baud_to_number( baud_requested );
[4d5b156]110
111  /*
112   *  Parity
113   */
114  if (t->c_cflag & PARENB) {
115    parity = "EVEN";
116    if (!(t->c_cflag & PARODD))
117      parity = "ODD";
118  }
119
120  /*
121   *  Character Size
122   */
123  if (t->c_cflag & CSIZE) {
124    switch (t->c_cflag & CSIZE) {
125      case CS5:  char_size = "5"; break;
126      case CS6:  char_size = "6"; break;
127      case CS7:  char_size = "7"; break;
128      case CS8:  char_size = "8"; break;
129    }
130  }
131
132  /*
133   *  Stop Bits
134   */
135  if (t->c_cflag & CSTOPB)
136    stop = "2";
137  else
138    stop = "1";
139
140  printf(
141    "set_attributes - B%d %s-%s-%s\n",
142    number,
143    char_size,
144    parity,
145    stop
146  );
147  return 0;
148}
149
150/*
151 *  Test Device Driver Entry Points
152 */
153rtems_device_driver termios_test_driver_initialize(
154  rtems_device_major_number  major,
155  rtems_device_minor_number  minor,
156  void                      *arg
157)
158{
159  rtems_status_code sc;
160
161  rtems_termios_initialize();
162
163  /*
164   *  Register Device Names
165   */
166  puts(
167    "Termios_test_driver - rtems_io_register "
168      TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
169  );
170  sc = rtems_io_register_name( TERMIOS_TEST_DRIVER_DEVICE_NAME, major, 0 );
171  directive_failed( sc, "rtems_io_register_name" );
172
173  return RTEMS_SUCCESSFUL;
174}
175
176rtems_device_driver termios_test_driver_open(
177  rtems_device_major_number major,
178  rtems_device_minor_number minor,
179  void                    * arg
180)
181{
182  rtems_status_code sc;
183  int               rc;
184  rtems_libio_open_close_args_t *args = arg;
[c8615dcf]185  static bool firstCall = true;
[1c6926c1]186
[4d5b156]187  static const rtems_termios_callbacks Callbacks = {
188    NULL,                                    /* firstOpen */
189    NULL,                                    /* lastClose */
190    termios_test_driver_inbyte_nonblocking,  /* pollRead */
191    termios_test_driver_write_support,       /* write */
192    termios_test_driver_set_attributes,      /* setAttributes */
193    NULL,                                    /* stopRemoteTx */
194    NULL,                                    /* startRemoteTx */
[150dcf5]195    TERMIOS_POLLED                           /* outputUsesInterrupts */
[4d5b156]196  };
197
198  if ( minor > 2 ) {
199    puts( "ERROR - Termios_testdriver - only 1 minor supported" );
200    rtems_test_exit(0);
201  }
202
[c8615dcf]203  if( firstCall ) {
204    static const uintptr_t allocSizes [] = {
205      sizeof( struct rtems_termios_tty ),
206      128,
207      64,
208      256
209    };
210
211    size_t i;
212
213    firstCall = false;
[1c6926c1]214
[c8615dcf]215    for (i = 0; i < sizeof( allocSizes ) / sizeof( allocSizes [0] ); ++i) {
216      void *opaque = rtems_heap_greedy_allocate( allocSizes, i );
217
218      sc = rtems_termios_open( major, minor, arg, &Callbacks );
219      rtems_test_assert( sc == RTEMS_NO_MEMORY );
220
221      rtems_heap_greedy_free( opaque );
222    }
[2e54cdc4]223  }
224 
[4d5b156]225  sc = rtems_termios_open (major, minor, arg, &Callbacks);
226  directive_failed( sc, "rtems_termios_open" );
227
228  puts( "Termios_test_driver - rtems_set_initial_baud - bad baud - OK" );
229  rc = rtems_termios_set_initial_baud( args->iop->data1, 5000 );
230  if ( rc != -1 ) {
231    printf( "ERROR - return %d\n", rc );
232    rtems_test_exit(0);
233  }
234
235  puts( "Termios_test_driver - rtems_set_initial_baud - 38400 - OK" );
236  rc = rtems_termios_set_initial_baud( args->iop->data1, 38400 );
237  if ( rc ) {
238    printf( "ERROR - return %d\n", rc );
239    rtems_test_exit(0);
240  }
241
242  return RTEMS_SUCCESSFUL;
243}
244
245rtems_device_driver termios_test_driver_close(
246  rtems_device_major_number major,
247  rtems_device_minor_number minor,
248  void                    * arg
249)
250{
251  return rtems_termios_close (arg);
252}
253
254rtems_device_driver termios_test_driver_read(
255  rtems_device_major_number major,
256  rtems_device_minor_number minor,
257  void                    * arg
258)
259{
260  return rtems_termios_read (arg);
261}
262
263rtems_device_driver termios_test_driver_write(
264  rtems_device_major_number major,
265  rtems_device_minor_number minor,
266  void                    * arg
267)
268{
269  return rtems_termios_write (arg);
270}
271
272rtems_device_driver termios_test_driver_control(
273  rtems_device_major_number major,
274  rtems_device_minor_number minor,
275  void                    * arg
276)
277{
278  return rtems_termios_ioctl (arg);
279}
Note: See TracBrowser for help on using the repository browser.