1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * This file contains a test fixture termios device driver |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * COPYRIGHT (c) 1989-2012. |
---|
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 |
---|
13 | * http://www.rtems.org/license/LICENSE. |
---|
14 | */ |
---|
15 | |
---|
16 | #ifdef HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include "tmacros.h" |
---|
21 | #include <rtems/libio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <termios.h> |
---|
24 | #include <rtems/termiostypes.h> |
---|
25 | #include <rtems/dumpbuf.h> |
---|
26 | #include "termios_testdriver_polled.h" |
---|
27 | |
---|
28 | /* forward declarations to avoid warnings */ |
---|
29 | int termios_test_driver_inbyte_nonblocking(int port); |
---|
30 | void termios_test_driver_outbyte_polled(int port, char ch); |
---|
31 | ssize_t termios_test_driver_write_support( |
---|
32 | int minor, |
---|
33 | const char *buf, |
---|
34 | size_t len |
---|
35 | ); |
---|
36 | int termios_test_driver_set_attributes( |
---|
37 | int minor, |
---|
38 | const struct termios *t |
---|
39 | ); |
---|
40 | |
---|
41 | #define TX_MAX 1024 |
---|
42 | uint8_t Tx_Buffer[TX_MAX]; |
---|
43 | int Tx_Index = 0; |
---|
44 | |
---|
45 | void termios_test_driver_dump_tx(const char *c) |
---|
46 | { |
---|
47 | printf( "%s %d characters\n", c, Tx_Index ); |
---|
48 | rtems_print_buffer( Tx_Buffer, Tx_Index ); |
---|
49 | Tx_Index = 0; |
---|
50 | } |
---|
51 | |
---|
52 | const uint8_t *Rx_Buffer; |
---|
53 | int Rx_Index; |
---|
54 | int Rx_Length; |
---|
55 | bool Rx_FirstTime = true; |
---|
56 | |
---|
57 | void termios_test_driver_set_rx( |
---|
58 | const void *p, |
---|
59 | size_t len |
---|
60 | ) |
---|
61 | { |
---|
62 | Rx_Buffer = p; |
---|
63 | Rx_Length = len; |
---|
64 | Rx_Index = 0; |
---|
65 | } |
---|
66 | |
---|
67 | int termios_test_driver_inbyte_nonblocking( int port ) |
---|
68 | { |
---|
69 | if ( Rx_FirstTime == true ) { |
---|
70 | Rx_FirstTime = false; |
---|
71 | return -1; |
---|
72 | } |
---|
73 | if ( Rx_Index >= Rx_Length ) |
---|
74 | return -1; |
---|
75 | return Rx_Buffer[ Rx_Index++ ]; |
---|
76 | } |
---|
77 | |
---|
78 | void termios_test_driver_outbyte_polled( |
---|
79 | int port, |
---|
80 | char ch |
---|
81 | ) |
---|
82 | { |
---|
83 | Tx_Buffer[Tx_Index++] = (uint8_t) ch; |
---|
84 | } |
---|
85 | |
---|
86 | ssize_t termios_test_driver_write_support (int minor, const char *buf, size_t len) |
---|
87 | { |
---|
88 | size_t nwrite = 0; |
---|
89 | |
---|
90 | while (nwrite < len) { |
---|
91 | termios_test_driver_outbyte_polled( minor, *buf++ ); |
---|
92 | nwrite++; |
---|
93 | } |
---|
94 | return nwrite; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | /* |
---|
99 | * Set Attributes Handler |
---|
100 | */ |
---|
101 | int termios_test_driver_set_attributes( |
---|
102 | int minor, |
---|
103 | const struct termios *t |
---|
104 | ) |
---|
105 | { |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | /* |
---|
110 | * Test Device Driver Entry Points |
---|
111 | */ |
---|
112 | rtems_device_driver termios_test_driver_initialize( |
---|
113 | rtems_device_major_number major, |
---|
114 | rtems_device_minor_number minor, |
---|
115 | void *arg |
---|
116 | ) |
---|
117 | { |
---|
118 | rtems_termios_initialize(); |
---|
119 | |
---|
120 | /* |
---|
121 | * Register Device Names |
---|
122 | */ |
---|
123 | (void) rtems_io_register_name( TERMIOS_TEST_DRIVER_DEVICE_NAME, major, 0 ); |
---|
124 | |
---|
125 | return RTEMS_SUCCESSFUL; |
---|
126 | } |
---|
127 | |
---|
128 | rtems_device_driver termios_test_driver_open( |
---|
129 | rtems_device_major_number major, |
---|
130 | rtems_device_minor_number minor, |
---|
131 | void * arg |
---|
132 | ) |
---|
133 | { |
---|
134 | rtems_status_code sc; |
---|
135 | static const rtems_termios_callbacks Callbacks = { |
---|
136 | NULL, /* firstOpen */ |
---|
137 | NULL, /* lastClose */ |
---|
138 | termios_test_driver_inbyte_nonblocking, /* pollRead */ |
---|
139 | termios_test_driver_write_support, /* write */ |
---|
140 | termios_test_driver_set_attributes, /* setAttributes */ |
---|
141 | NULL, /* stopRemoteTx */ |
---|
142 | NULL, /* startRemoteTx */ |
---|
143 | 0 /* outputUsesInterrupts */ |
---|
144 | }; |
---|
145 | |
---|
146 | if ( minor > 2 ) { |
---|
147 | puts( "ERROR - Termios_testdriver - only 1 minor supported" ); |
---|
148 | rtems_test_exit(0); |
---|
149 | } |
---|
150 | |
---|
151 | sc = rtems_termios_open (major, minor, arg, &Callbacks); |
---|
152 | directive_failed( sc, "termios open" ); |
---|
153 | |
---|
154 | return RTEMS_SUCCESSFUL; |
---|
155 | } |
---|
156 | |
---|
157 | rtems_device_driver termios_test_driver_close( |
---|
158 | rtems_device_major_number major, |
---|
159 | rtems_device_minor_number minor, |
---|
160 | void * arg |
---|
161 | ) |
---|
162 | { |
---|
163 | return rtems_termios_close (arg); |
---|
164 | } |
---|
165 | |
---|
166 | rtems_device_driver termios_test_driver_read( |
---|
167 | rtems_device_major_number major, |
---|
168 | rtems_device_minor_number minor, |
---|
169 | void * arg |
---|
170 | ) |
---|
171 | { |
---|
172 | return rtems_termios_read (arg); |
---|
173 | } |
---|
174 | |
---|
175 | rtems_device_driver termios_test_driver_write( |
---|
176 | rtems_device_major_number major, |
---|
177 | rtems_device_minor_number minor, |
---|
178 | void * arg |
---|
179 | ) |
---|
180 | { |
---|
181 | return rtems_termios_write (arg); |
---|
182 | } |
---|
183 | |
---|
184 | rtems_device_driver termios_test_driver_control( |
---|
185 | rtems_device_major_number major, |
---|
186 | rtems_device_minor_number minor, |
---|
187 | void * arg |
---|
188 | ) |
---|
189 | { |
---|
190 | return rtems_termios_ioctl (arg); |
---|
191 | } |
---|