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 <inttypes.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <sys/types.h>
|
---|
42 | #include <async.h>
|
---|
43 | #include <ipc/services.h>
|
---|
44 | #include <ipc/devman.h>
|
---|
45 | #include <devman.h>
|
---|
46 | #include <device/char_dev.h>
|
---|
47 | #include <str.h>
|
---|
48 | #include <ipc/serial_ctl.h>
|
---|
49 | #include "../../tester.h"
|
---|
50 |
|
---|
51 | #define DEFAULT_COUNT 1024
|
---|
52 | #define DEFAULT_SLEEP 100000
|
---|
53 | #define EOT "####> End of transfer <####\n"
|
---|
54 |
|
---|
55 | const char *test_serial1(void)
|
---|
56 | {
|
---|
57 | size_t cnt;
|
---|
58 |
|
---|
59 | if (test_argc < 1)
|
---|
60 | cnt = DEFAULT_COUNT;
|
---|
61 | else
|
---|
62 | switch (str_size_t(test_argv[0], NULL, 0, true, &cnt)) {
|
---|
63 | case EOK:
|
---|
64 | break;
|
---|
65 | case EINVAL:
|
---|
66 | return "Invalid argument, unsigned integer expected";
|
---|
67 | case EOVERFLOW:
|
---|
68 | return "Argument size overflow";
|
---|
69 | default:
|
---|
70 | return "Unexpected argument error";
|
---|
71 | }
|
---|
72 |
|
---|
73 | devman_handle_t handle;
|
---|
74 | int res = devman_fun_get_handle("/hw/pci0/00:01.0/com1/a", &handle,
|
---|
75 | IPC_FLAG_BLOCKING);
|
---|
76 | if (res != EOK)
|
---|
77 | return "Could not get serial device handle";
|
---|
78 |
|
---|
79 | async_sess_t *sess = devman_device_connect(EXCHANGE_SERIALIZE, handle,
|
---|
80 | IPC_FLAG_BLOCKING);
|
---|
81 | if (!sess)
|
---|
82 | return "Unable to connect to serial device";
|
---|
83 |
|
---|
84 | char *buf = (char *) malloc(cnt + 1);
|
---|
85 | if (buf == NULL) {
|
---|
86 | async_hangup(sess);
|
---|
87 | return "Failed to allocate input buffer";
|
---|
88 | }
|
---|
89 |
|
---|
90 | sysarg_t old_baud;
|
---|
91 | sysarg_t old_par;
|
---|
92 | sysarg_t old_stop;
|
---|
93 | sysarg_t old_word_size;
|
---|
94 |
|
---|
95 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
96 | res = async_req_0_4(exch, SERIAL_GET_COM_PROPS, &old_baud,
|
---|
97 | &old_par, &old_word_size, &old_stop);
|
---|
98 | async_exchange_end(exch);
|
---|
99 |
|
---|
100 | if (res != EOK) {
|
---|
101 | free(buf);
|
---|
102 | async_hangup(sess);
|
---|
103 | return "Failed to get old serial communication parameters";
|
---|
104 | }
|
---|
105 |
|
---|
106 | exch = async_exchange_begin(sess);
|
---|
107 | res = async_req_4_0(exch, SERIAL_SET_COM_PROPS, 1200,
|
---|
108 | SERIAL_NO_PARITY, 8, 1);
|
---|
109 | async_exchange_end(exch);
|
---|
110 |
|
---|
111 | if (EOK != res) {
|
---|
112 | free(buf);
|
---|
113 | async_hangup(sess);
|
---|
114 | return "Failed to set serial communication parameters";
|
---|
115 | }
|
---|
116 |
|
---|
117 | TPRINTF("Trying to read %zu characters from serial device "
|
---|
118 | "(handle=%" PRIun ")\n", cnt, handle);
|
---|
119 |
|
---|
120 | size_t total = 0;
|
---|
121 | while (total < cnt) {
|
---|
122 | ssize_t read = char_dev_read(sess, buf, cnt - total);
|
---|
123 |
|
---|
124 | if (read < 0) {
|
---|
125 | exch = async_exchange_begin(sess);
|
---|
126 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
---|
127 | old_par, old_word_size, old_stop);
|
---|
128 | async_exchange_end(exch);
|
---|
129 |
|
---|
130 | free(buf);
|
---|
131 | async_hangup(sess);
|
---|
132 | return "Failed read from serial device";
|
---|
133 | }
|
---|
134 |
|
---|
135 | if ((size_t) read > cnt - total) {
|
---|
136 | exch = async_exchange_begin(sess);
|
---|
137 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
---|
138 | old_par, old_word_size, old_stop);
|
---|
139 | async_exchange_end(exch);
|
---|
140 |
|
---|
141 | free(buf);
|
---|
142 | async_hangup(sess);
|
---|
143 | return "Read more data than expected";
|
---|
144 | }
|
---|
145 |
|
---|
146 | TPRINTF("Read %zd bytes\n", read);
|
---|
147 |
|
---|
148 | if (read == 0)
|
---|
149 | usleep(DEFAULT_SLEEP);
|
---|
150 | else {
|
---|
151 | buf[read] = 0;
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * Write data back to the device to test the opposite
|
---|
155 | * direction of data transfer.
|
---|
156 | */
|
---|
157 | ssize_t written = char_dev_write(sess, buf, read);
|
---|
158 |
|
---|
159 | if (written < 0) {
|
---|
160 | exch = async_exchange_begin(sess);
|
---|
161 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
---|
162 | old_par, old_word_size, old_stop);
|
---|
163 | async_exchange_end(exch);
|
---|
164 |
|
---|
165 | free(buf);
|
---|
166 | async_hangup(sess);
|
---|
167 | return "Failed write to serial device";
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (written != read) {
|
---|
171 | exch = async_exchange_begin(sess);
|
---|
172 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
---|
173 | old_par, old_word_size, old_stop);
|
---|
174 | async_exchange_end(exch);
|
---|
175 |
|
---|
176 | free(buf);
|
---|
177 | async_hangup(sess);
|
---|
178 | return "Written less data than read from serial device";
|
---|
179 | }
|
---|
180 |
|
---|
181 | TPRINTF("Written %zd bytes\n", written);
|
---|
182 | }
|
---|
183 |
|
---|
184 | total += read;
|
---|
185 | }
|
---|
186 |
|
---|
187 | TPRINTF("Trying to write EOT banner to the serial device\n");
|
---|
188 |
|
---|
189 | size_t eot_size = str_size(EOT);
|
---|
190 | ssize_t written = char_dev_write(sess, (void *) EOT, eot_size);
|
---|
191 |
|
---|
192 | exch = async_exchange_begin(sess);
|
---|
193 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
---|
194 | old_par, old_word_size, old_stop);
|
---|
195 | async_exchange_end(exch);
|
---|
196 |
|
---|
197 | free(buf);
|
---|
198 | async_hangup(sess);
|
---|
199 |
|
---|
200 | if (written < 0)
|
---|
201 | return "Failed to write EOT banner to serial device";
|
---|
202 |
|
---|
203 | if ((size_t) written != eot_size)
|
---|
204 | return "Written less data than the size of the EOT banner "
|
---|
205 | "to serial device";
|
---|
206 |
|
---|
207 | return NULL;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** @}
|
---|
211 | */
|
---|