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 | int res = devman_get_phone(DEVMAN_CLIENT, IPC_FLAG_BLOCKING);
|
---|
74 |
|
---|
75 | devman_handle_t handle;
|
---|
76 | res = devman_device_get_handle("/hw/pci0/00:01.0/com1", &handle,
|
---|
77 | IPC_FLAG_BLOCKING);
|
---|
78 | if (res != EOK)
|
---|
79 | return "Could not get serial device handle";
|
---|
80 |
|
---|
81 | int phone = devman_device_connect(handle, IPC_FLAG_BLOCKING);
|
---|
82 | if (phone < 0) {
|
---|
83 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
84 | return "Unable to connect to serial device";
|
---|
85 | }
|
---|
86 |
|
---|
87 | char *buf = (char *) malloc(cnt + 1);
|
---|
88 | if (buf == NULL) {
|
---|
89 | async_hangup(phone);
|
---|
90 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
91 | return "Failed to allocate input buffer";
|
---|
92 | }
|
---|
93 |
|
---|
94 | sysarg_t old_baud;
|
---|
95 | sysarg_t old_par;
|
---|
96 | sysarg_t old_stop;
|
---|
97 | sysarg_t old_word_size;
|
---|
98 |
|
---|
99 | res = async_req_0_4(phone, SERIAL_GET_COM_PROPS, &old_baud,
|
---|
100 | &old_par, &old_word_size, &old_stop);
|
---|
101 | if (res != EOK) {
|
---|
102 | free(buf);
|
---|
103 | async_hangup(phone);
|
---|
104 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
105 | return "Failed to get old serial communication parameters";
|
---|
106 | }
|
---|
107 |
|
---|
108 | res = async_req_4_0(phone, SERIAL_SET_COM_PROPS, 1200,
|
---|
109 | SERIAL_NO_PARITY, 8, 1);
|
---|
110 | if (EOK != res) {
|
---|
111 | free(buf);
|
---|
112 | async_hangup(phone);
|
---|
113 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
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(phone, buf, cnt - total);
|
---|
123 |
|
---|
124 | if (read < 0) {
|
---|
125 | async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
---|
126 | old_par, old_word_size, old_stop);
|
---|
127 | free(buf);
|
---|
128 | async_hangup(phone);
|
---|
129 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
130 | return "Failed read from serial device";
|
---|
131 | }
|
---|
132 |
|
---|
133 | if ((size_t) read > cnt - total) {
|
---|
134 | async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
---|
135 | old_par, old_word_size, old_stop);
|
---|
136 | free(buf);
|
---|
137 | async_hangup(phone);
|
---|
138 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
139 | return "Read more data than expected";
|
---|
140 | }
|
---|
141 |
|
---|
142 | TPRINTF("Read %zd bytes\n", read);
|
---|
143 |
|
---|
144 | if (read == 0)
|
---|
145 | usleep(DEFAULT_SLEEP);
|
---|
146 | else {
|
---|
147 | buf[read] = 0;
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * Write data back to the device to test the opposite
|
---|
151 | * direction of data transfer.
|
---|
152 | */
|
---|
153 | ssize_t written = char_dev_write(phone, buf, read);
|
---|
154 |
|
---|
155 | if (written < 0) {
|
---|
156 | async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
---|
157 | old_par, old_word_size, old_stop);
|
---|
158 | free(buf);
|
---|
159 | async_hangup(phone);
|
---|
160 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
161 | return "Failed write to serial device";
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (written != read) {
|
---|
165 | async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
---|
166 | old_par, old_word_size, old_stop);
|
---|
167 | free(buf);
|
---|
168 | async_hangup(phone);
|
---|
169 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
170 | return "Written less data than read from serial device";
|
---|
171 | }
|
---|
172 |
|
---|
173 | TPRINTF("Written %zd bytes\n", written);
|
---|
174 | }
|
---|
175 |
|
---|
176 | total += read;
|
---|
177 | }
|
---|
178 |
|
---|
179 | TPRINTF("Trying to write EOT banner to the serial device\n");
|
---|
180 |
|
---|
181 | size_t eot_size = str_size(EOT);
|
---|
182 | ssize_t written = char_dev_write(phone, (void *) EOT, eot_size);
|
---|
183 |
|
---|
184 | async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
---|
185 | old_par, old_word_size, old_stop);
|
---|
186 | free(buf);
|
---|
187 | async_hangup(phone);
|
---|
188 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
189 |
|
---|
190 | if (written < 0)
|
---|
191 | return "Failed to write EOT banner to serial device";
|
---|
192 |
|
---|
193 | if ((size_t) written != eot_size)
|
---|
194 | return "Written less data than the size of the EOT banner "
|
---|
195 | "to serial device";
|
---|
196 |
|
---|
197 | return NULL;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** @}
|
---|
201 | */
|
---|