[4c86e3d] | 1 | /** |
---|
| 2 | * @file |
---|
[4d5b156] | 3 | * |
---|
[4c86e3d] | 4 | * This file contains a test fixture termios device driver. |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /* |
---|
| 8 | * COPYRIGHT (c) 1989-2012. |
---|
[4d5b156] | 9 | * On-Line Applications Research Corporation (OAR). |
---|
| 10 | * |
---|
| 11 | * The license and distribution terms for this file may be |
---|
| 12 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 13 | * http://www.rtems.org/license/LICENSE. |
---|
[4d5b156] | 14 | */ |
---|
| 15 | |
---|
[7d3f9c6] | 16 | #ifdef HAVE_CONFIG_H |
---|
| 17 | #include "config.h" |
---|
| 18 | #endif |
---|
| 19 | |
---|
[4d5b156] | 20 | #include "tmacros.h" |
---|
| 21 | #include <rtems/libio.h> |
---|
| 22 | #include <stdlib.h> |
---|
| 23 | #include <termios.h> |
---|
[4184a9c] | 24 | #include <rtems/termiostypes.h> |
---|
[2e54cdc4] | 25 | #include <rtems/libcsupport.h> |
---|
[c8615dcf] | 26 | #include <rtems/malloc.h> |
---|
[4d5b156] | 27 | #include "termios_testdriver.h" |
---|
| 28 | |
---|
[4c86e3d] | 29 | /* forward declarations to avoid warnings */ |
---|
| 30 | int termios_test_driver_inbyte_nonblocking(int port); |
---|
| 31 | void termios_test_driver_outbyte_polled(int port, char ch); |
---|
| 32 | ssize_t termios_test_driver_write_support( |
---|
| 33 | int minor, |
---|
| 34 | const char *buf, |
---|
| 35 | size_t len |
---|
| 36 | ); |
---|
| 37 | int termios_test_driver_set_attributes( |
---|
| 38 | int minor, |
---|
| 39 | const struct termios *t |
---|
| 40 | ); |
---|
| 41 | |
---|
[4d5b156] | 42 | int termios_test_driver_inbyte_nonblocking( int port ) |
---|
| 43 | { |
---|
| 44 | return -1; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | void termios_test_driver_outbyte_polled( |
---|
| 48 | int port, |
---|
| 49 | char ch |
---|
| 50 | ) |
---|
| 51 | { |
---|
| 52 | } |
---|
| 53 | |
---|
[4c86e3d] | 54 | ssize_t termios_test_driver_write_support( |
---|
| 55 | int minor, |
---|
| 56 | const char *buf, |
---|
| 57 | size_t len |
---|
| 58 | ) |
---|
[4d5b156] | 59 | { |
---|
[ea9a626] | 60 | size_t nwrite = 0; |
---|
[4d5b156] | 61 | |
---|
| 62 | while (nwrite < len) { |
---|
[dda7c828] | 63 | #if (TERMIOS_TEST_DRIVER_USE_INTERRUPTS) |
---|
[4d5b156] | 64 | termios_test_driver_outbyte_interrupt( minor, *buf++ ); |
---|
| 65 | #else |
---|
| 66 | termios_test_driver_outbyte_polled( minor, *buf++ ); |
---|
| 67 | #endif |
---|
| 68 | nwrite++; |
---|
| 69 | } |
---|
| 70 | return nwrite; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | /* |
---|
| 75 | * Set Attributes Handler |
---|
| 76 | */ |
---|
| 77 | int termios_test_driver_set_attributes( |
---|
| 78 | int minor, |
---|
| 79 | const struct termios *t |
---|
| 80 | ) |
---|
| 81 | { |
---|
| 82 | int baud_requested; |
---|
| 83 | int number; |
---|
| 84 | const char *parity = "NONE"; |
---|
| 85 | const char *char_size = "5"; |
---|
| 86 | const char *stop = "NONE"; |
---|
| 87 | |
---|
| 88 | baud_requested = t->c_cflag & CBAUD; |
---|
| 89 | |
---|
[009dfd0] | 90 | number = rtems_termios_baud_to_number( baud_requested ); |
---|
[4d5b156] | 91 | |
---|
| 92 | /* |
---|
| 93 | * Parity |
---|
| 94 | */ |
---|
| 95 | if (t->c_cflag & PARENB) { |
---|
| 96 | parity = "EVEN"; |
---|
| 97 | if (!(t->c_cflag & PARODD)) |
---|
| 98 | parity = "ODD"; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | /* |
---|
| 102 | * Character Size |
---|
| 103 | */ |
---|
| 104 | if (t->c_cflag & CSIZE) { |
---|
| 105 | switch (t->c_cflag & CSIZE) { |
---|
| 106 | case CS5: char_size = "5"; break; |
---|
| 107 | case CS6: char_size = "6"; break; |
---|
| 108 | case CS7: char_size = "7"; break; |
---|
| 109 | case CS8: char_size = "8"; break; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /* |
---|
| 114 | * Stop Bits |
---|
| 115 | */ |
---|
| 116 | if (t->c_cflag & CSTOPB) |
---|
| 117 | stop = "2"; |
---|
| 118 | else |
---|
| 119 | stop = "1"; |
---|
| 120 | |
---|
| 121 | printf( |
---|
| 122 | "set_attributes - B%d %s-%s-%s\n", |
---|
| 123 | number, |
---|
| 124 | char_size, |
---|
| 125 | parity, |
---|
| 126 | stop |
---|
| 127 | ); |
---|
| 128 | return 0; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /* |
---|
| 132 | * Test Device Driver Entry Points |
---|
| 133 | */ |
---|
| 134 | rtems_device_driver termios_test_driver_initialize( |
---|
| 135 | rtems_device_major_number major, |
---|
| 136 | rtems_device_minor_number minor, |
---|
| 137 | void *arg |
---|
| 138 | ) |
---|
| 139 | { |
---|
| 140 | rtems_status_code sc; |
---|
| 141 | |
---|
| 142 | rtems_termios_initialize(); |
---|
| 143 | |
---|
| 144 | /* |
---|
| 145 | * Register Device Names |
---|
| 146 | */ |
---|
| 147 | puts( |
---|
| 148 | "Termios_test_driver - rtems_io_register " |
---|
| 149 | TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK" |
---|
| 150 | ); |
---|
| 151 | sc = rtems_io_register_name( TERMIOS_TEST_DRIVER_DEVICE_NAME, major, 0 ); |
---|
| 152 | directive_failed( sc, "rtems_io_register_name" ); |
---|
| 153 | |
---|
| 154 | return RTEMS_SUCCESSFUL; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | rtems_device_driver termios_test_driver_open( |
---|
| 158 | rtems_device_major_number major, |
---|
| 159 | rtems_device_minor_number minor, |
---|
| 160 | void * arg |
---|
| 161 | ) |
---|
| 162 | { |
---|
| 163 | rtems_status_code sc; |
---|
| 164 | int rc; |
---|
| 165 | rtems_libio_open_close_args_t *args = arg; |
---|
[c8615dcf] | 166 | static bool firstCall = true; |
---|
[2e54cdc4] | 167 | |
---|
[4d5b156] | 168 | static const rtems_termios_callbacks Callbacks = { |
---|
| 169 | NULL, /* firstOpen */ |
---|
| 170 | NULL, /* lastClose */ |
---|
| 171 | termios_test_driver_inbyte_nonblocking, /* pollRead */ |
---|
| 172 | termios_test_driver_write_support, /* write */ |
---|
| 173 | termios_test_driver_set_attributes, /* setAttributes */ |
---|
| 174 | NULL, /* stopRemoteTx */ |
---|
| 175 | NULL, /* startRemoteTx */ |
---|
| 176 | 0 /* outputUsesInterrupts */ |
---|
| 177 | }; |
---|
| 178 | |
---|
| 179 | if ( minor > 2 ) { |
---|
| 180 | puts( "ERROR - Termios_testdriver - only 1 minor supported" ); |
---|
| 181 | rtems_test_exit(0); |
---|
| 182 | } |
---|
| 183 | |
---|
[c8615dcf] | 184 | if( firstCall ) { |
---|
| 185 | static const uintptr_t allocSizes [] = { |
---|
| 186 | sizeof( struct rtems_termios_tty ), |
---|
| 187 | 128, |
---|
| 188 | 64, |
---|
| 189 | 256 |
---|
| 190 | }; |
---|
| 191 | |
---|
| 192 | size_t i; |
---|
| 193 | |
---|
| 194 | firstCall = false; |
---|
[2e54cdc4] | 195 | |
---|
[c8615dcf] | 196 | for (i = 0; i < sizeof( allocSizes ) / sizeof( allocSizes [0] ); ++i) { |
---|
| 197 | void *opaque = rtems_heap_greedy_allocate( allocSizes, i ); |
---|
| 198 | |
---|
| 199 | sc = rtems_termios_open( major, minor, arg, &Callbacks ); |
---|
| 200 | rtems_test_assert( sc == RTEMS_NO_MEMORY ); |
---|
| 201 | |
---|
| 202 | rtems_heap_greedy_free( opaque ); |
---|
| 203 | } |
---|
[2e54cdc4] | 204 | } |
---|
| 205 | |
---|
[4d5b156] | 206 | sc = rtems_termios_open (major, minor, arg, &Callbacks); |
---|
| 207 | directive_failed( sc, "rtems_termios_open" ); |
---|
| 208 | |
---|
| 209 | puts( "Termios_test_driver - rtems_set_initial_baud - bad baud - OK" ); |
---|
| 210 | rc = rtems_termios_set_initial_baud( args->iop->data1, 5000 ); |
---|
| 211 | if ( rc != -1 ) { |
---|
| 212 | printf( "ERROR - return %d\n", rc ); |
---|
| 213 | rtems_test_exit(0); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | puts( "Termios_test_driver - rtems_set_initial_baud - 38400 - OK" ); |
---|
| 217 | rc = rtems_termios_set_initial_baud( args->iop->data1, 38400 ); |
---|
| 218 | if ( rc ) { |
---|
| 219 | printf( "ERROR - return %d\n", rc ); |
---|
| 220 | rtems_test_exit(0); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | return RTEMS_SUCCESSFUL; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | rtems_device_driver termios_test_driver_close( |
---|
| 227 | rtems_device_major_number major, |
---|
| 228 | rtems_device_minor_number minor, |
---|
| 229 | void * arg |
---|
| 230 | ) |
---|
| 231 | { |
---|
| 232 | return rtems_termios_close (arg); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | rtems_device_driver termios_test_driver_read( |
---|
| 236 | rtems_device_major_number major, |
---|
| 237 | rtems_device_minor_number minor, |
---|
| 238 | void * arg |
---|
| 239 | ) |
---|
| 240 | { |
---|
| 241 | return rtems_termios_read (arg); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | rtems_device_driver termios_test_driver_write( |
---|
| 245 | rtems_device_major_number major, |
---|
| 246 | rtems_device_minor_number minor, |
---|
| 247 | void * arg |
---|
| 248 | ) |
---|
| 249 | { |
---|
| 250 | return rtems_termios_write (arg); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | rtems_device_driver termios_test_driver_control( |
---|
| 254 | rtems_device_major_number major, |
---|
| 255 | rtems_device_minor_number minor, |
---|
| 256 | void * arg |
---|
| 257 | ) |
---|
| 258 | { |
---|
| 259 | return rtems_termios_ioctl (arg); |
---|
| 260 | } |
---|