1 | /*
|
---|
2 | * Copyright (c) 2009 Lenka Trochtova
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup tester
|
---|
30 | * @brief Test the serial port driver - loopback test
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <async.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <io/chardev.h>
|
---|
40 | #include <io/serial.h>
|
---|
41 | #include <ipc/services.h>
|
---|
42 | #include <loc.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <stddef.h>
|
---|
46 | #include <str.h>
|
---|
47 | #include <thread.h>
|
---|
48 | #include "../../tester.h"
|
---|
49 |
|
---|
50 | #define DEFAULT_COUNT 1024
|
---|
51 | #define DEFAULT_SLEEP 100000
|
---|
52 | #define EOT "####> End of transfer <####\n"
|
---|
53 |
|
---|
54 | const char *test_serial1(void)
|
---|
55 | {
|
---|
56 | size_t cnt;
|
---|
57 | serial_t *serial;
|
---|
58 | chardev_t *chardev;
|
---|
59 | int rc;
|
---|
60 | size_t nread;
|
---|
61 | size_t nwritten;
|
---|
62 |
|
---|
63 | if (test_argc < 1)
|
---|
64 | cnt = DEFAULT_COUNT;
|
---|
65 | else
|
---|
66 | switch (str_size_t(test_argv[0], NULL, 0, true, &cnt)) {
|
---|
67 | case EOK:
|
---|
68 | break;
|
---|
69 | case EINVAL:
|
---|
70 | return "Invalid argument, unsigned integer expected";
|
---|
71 | case EOVERFLOW:
|
---|
72 | return "Argument size overflow";
|
---|
73 | default:
|
---|
74 | return "Unexpected argument error";
|
---|
75 | }
|
---|
76 |
|
---|
77 | service_id_t svc_id;
|
---|
78 | int res = loc_service_get_id("devices/\\hw\\pci0\\00:01.0\\com1\\a",
|
---|
79 | &svc_id, IPC_FLAG_BLOCKING);
|
---|
80 | if (res != EOK)
|
---|
81 | return "Failed getting serial port service ID";
|
---|
82 |
|
---|
83 | async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
|
---|
84 | IPC_FLAG_BLOCKING);
|
---|
85 | if (sess == NULL)
|
---|
86 | return "Failed connecting to serial device";
|
---|
87 |
|
---|
88 | res = chardev_open(sess, &chardev);
|
---|
89 | if (res != EOK) {
|
---|
90 | async_hangup(sess);
|
---|
91 | return "Failed opening serial port";
|
---|
92 | }
|
---|
93 |
|
---|
94 | res = serial_open(sess, &serial);
|
---|
95 | if (res != EOK) {
|
---|
96 | chardev_close(chardev);
|
---|
97 | async_hangup(sess);
|
---|
98 | return "Failed opening serial port";
|
---|
99 | }
|
---|
100 |
|
---|
101 | char *buf = (char *) malloc(cnt + 1);
|
---|
102 | if (buf == NULL) {
|
---|
103 | chardev_close(chardev);
|
---|
104 | serial_close(serial);
|
---|
105 | async_hangup(sess);
|
---|
106 | return "Failed allocating input buffer";
|
---|
107 | }
|
---|
108 |
|
---|
109 | unsigned old_baud;
|
---|
110 | serial_parity_t old_par;
|
---|
111 | unsigned old_stop;
|
---|
112 | unsigned old_word_size;
|
---|
113 |
|
---|
114 | res = serial_get_comm_props(serial, &old_baud, &old_par,
|
---|
115 | &old_word_size, &old_stop);
|
---|
116 | if (res != EOK) {
|
---|
117 | free(buf);
|
---|
118 | chardev_close(chardev);
|
---|
119 | serial_close(serial);
|
---|
120 | async_hangup(sess);
|
---|
121 | return "Failed to get old serial communication parameters";
|
---|
122 | }
|
---|
123 |
|
---|
124 | res = serial_set_comm_props(serial, 1200, SERIAL_NO_PARITY, 8, 1);
|
---|
125 | if (EOK != res) {
|
---|
126 | free(buf);
|
---|
127 | chardev_close(chardev);
|
---|
128 | serial_close(serial);
|
---|
129 | async_hangup(sess);
|
---|
130 | return "Failed setting serial communication parameters";
|
---|
131 | }
|
---|
132 |
|
---|
133 | TPRINTF("Trying reading %zu characters from serial device "
|
---|
134 | "(svc_id=%" PRIun ")\n", cnt, svc_id);
|
---|
135 |
|
---|
136 | size_t total = 0;
|
---|
137 | while (total < cnt) {
|
---|
138 |
|
---|
139 | rc = chardev_read(chardev, buf, cnt - total, &nread);
|
---|
140 | if (rc != EOK) {
|
---|
141 | (void) serial_set_comm_props(serial, old_baud,
|
---|
142 | old_par, old_word_size, old_stop);
|
---|
143 |
|
---|
144 | free(buf);
|
---|
145 | chardev_close(chardev);
|
---|
146 | serial_close(serial);
|
---|
147 | async_hangup(sess);
|
---|
148 | return "Failed reading from serial device";
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (nread > cnt - total) {
|
---|
152 | (void) serial_set_comm_props(serial, old_baud,
|
---|
153 | old_par, old_word_size, old_stop);
|
---|
154 |
|
---|
155 | free(buf);
|
---|
156 | chardev_close(chardev);
|
---|
157 | serial_close(serial);
|
---|
158 | async_hangup(sess);
|
---|
159 | return "Read more data than expected";
|
---|
160 | }
|
---|
161 |
|
---|
162 | TPRINTF("Read %zd bytes\n", nread);
|
---|
163 |
|
---|
164 | if (nread == 0)
|
---|
165 | thread_usleep(DEFAULT_SLEEP);
|
---|
166 | else {
|
---|
167 | buf[nread] = 0;
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * Write data back to the device to test the opposite
|
---|
171 | * direction of data transfer.
|
---|
172 | */
|
---|
173 | rc = chardev_write(chardev, buf, nread, &nwritten);
|
---|
174 | if (rc != EOK) {
|
---|
175 | (void) serial_set_comm_props(serial, old_baud,
|
---|
176 | old_par, old_word_size, old_stop);
|
---|
177 |
|
---|
178 | free(buf);
|
---|
179 | chardev_close(chardev);
|
---|
180 | serial_close(serial);
|
---|
181 | async_hangup(sess);
|
---|
182 | return "Failed writing to serial device";
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (nwritten != nread) {
|
---|
186 | (void) serial_set_comm_props(serial, old_baud,
|
---|
187 | old_par, old_word_size, old_stop);
|
---|
188 |
|
---|
189 | free(buf);
|
---|
190 | chardev_close(chardev);
|
---|
191 | serial_close(serial);
|
---|
192 | async_hangup(sess);
|
---|
193 | return "Written less data than read from serial device";
|
---|
194 | }
|
---|
195 |
|
---|
196 | TPRINTF("Written %zd bytes\n", nwritten);
|
---|
197 | }
|
---|
198 |
|
---|
199 | total += nread;
|
---|
200 | }
|
---|
201 |
|
---|
202 | TPRINTF("Trying to write EOT banner to the serial device\n");
|
---|
203 |
|
---|
204 | size_t eot_size = str_size(EOT);
|
---|
205 | rc = chardev_write(chardev, (void *) EOT, eot_size, &nwritten);
|
---|
206 |
|
---|
207 | (void) serial_set_comm_props(serial, old_baud, old_par, old_word_size,
|
---|
208 | old_stop);
|
---|
209 |
|
---|
210 | free(buf);
|
---|
211 | chardev_close(chardev);
|
---|
212 | serial_close(serial);
|
---|
213 | async_hangup(sess);
|
---|
214 |
|
---|
215 | if (rc != EOK)
|
---|
216 | return "Failed to write EOT banner to serial device";
|
---|
217 |
|
---|
218 | if (nwritten != eot_size)
|
---|
219 | return "Written less data than the size of the EOT banner "
|
---|
220 | "to serial device";
|
---|
221 |
|
---|
222 | return NULL;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /** @}
|
---|
226 | */
|
---|