source: mainline/uspace/app/tester/hw/serial/serial1.c

Last change on this file was f2d88f3, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Add forgotten changes to enable non-blocking chardev read

  • Property mode set to 100644
File size: 5.7 KB
Line 
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 * @{
31 */
32/**
33 * @file
34 * @brief Test the serial port driver - loopback test
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 "../../tester.h"
48
49#define DEFAULT_COUNT 1024
50#define DEFAULT_SLEEP 100000
51#define EOT "####> End of transfer <####\n"
52
53const char *test_serial1(void)
54{
55 size_t cnt;
56 serial_t *serial;
57 chardev_t *chardev;
58 errno_t rc;
59 size_t nread;
60 size_t nwritten;
61
62 if (test_argc < 1)
63 cnt = DEFAULT_COUNT;
64 else
65 switch (str_size_t(test_argv[0], NULL, 0, true, &cnt)) {
66 case EOK:
67 break;
68 case EINVAL:
69 return "Invalid argument, unsigned integer expected";
70 case EOVERFLOW:
71 return "Argument size overflow";
72 default:
73 return "Unexpected argument error";
74 }
75
76 service_id_t svc_id;
77 errno_t res = loc_service_get_id("devices/\\hw\\pci0\\00:01.0\\com1\\a",
78 &svc_id, IPC_FLAG_BLOCKING);
79 if (res != EOK)
80 return "Failed getting serial port service ID";
81
82 async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
83 IPC_FLAG_BLOCKING);
84 if (sess == NULL)
85 return "Failed connecting to serial device";
86
87 res = chardev_open(sess, &chardev);
88 if (res != EOK) {
89 async_hangup(sess);
90 return "Failed opening serial port";
91 }
92
93 res = serial_open(sess, &serial);
94 if (res != EOK) {
95 chardev_close(chardev);
96 async_hangup(sess);
97 return "Failed opening serial port";
98 }
99
100 char *buf = (char *) malloc(cnt + 1);
101 if (buf == NULL) {
102 chardev_close(chardev);
103 serial_close(serial);
104 async_hangup(sess);
105 return "Failed allocating input buffer";
106 }
107
108 unsigned old_baud;
109 serial_parity_t old_par;
110 unsigned old_stop;
111 unsigned old_word_size;
112
113 res = serial_get_comm_props(serial, &old_baud, &old_par,
114 &old_word_size, &old_stop);
115 if (res != EOK) {
116 free(buf);
117 chardev_close(chardev);
118 serial_close(serial);
119 async_hangup(sess);
120 return "Failed to get old serial communication parameters";
121 }
122
123 res = serial_set_comm_props(serial, 1200, SERIAL_NO_PARITY, 8, 1);
124 if (EOK != res) {
125 free(buf);
126 chardev_close(chardev);
127 serial_close(serial);
128 async_hangup(sess);
129 return "Failed setting serial communication parameters";
130 }
131
132 TPRINTF("Trying reading %zu characters from serial device "
133 "(svc_id=%" PRIun ")\n", cnt, svc_id);
134
135 size_t total = 0;
136 while (total < cnt) {
137
138 rc = chardev_read(chardev, buf, cnt - total, &nread,
139 chardev_f_none);
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 buf[nread] = 0;
165
166 /*
167 * Write data back to the device to test the opposite
168 * direction of data transfer.
169 */
170 rc = chardev_write(chardev, buf, nread, &nwritten);
171 if (rc != EOK) {
172 (void) serial_set_comm_props(serial, old_baud,
173 old_par, old_word_size, old_stop);
174
175 free(buf);
176 chardev_close(chardev);
177 serial_close(serial);
178 async_hangup(sess);
179 return "Failed writing to serial device";
180 }
181
182 if (nwritten != nread) {
183 (void) serial_set_comm_props(serial, old_baud,
184 old_par, old_word_size, old_stop);
185
186 free(buf);
187 chardev_close(chardev);
188 serial_close(serial);
189 async_hangup(sess);
190 return "Written less data than read from serial device";
191 }
192
193 TPRINTF("Written %zd bytes\n", nwritten);
194
195 total += nread;
196 }
197
198 TPRINTF("Trying to write EOT banner to the serial device\n");
199
200 size_t eot_size = str_size(EOT);
201 rc = chardev_write(chardev, (void *) EOT, eot_size, &nwritten);
202
203 (void) serial_set_comm_props(serial, old_baud, old_par, old_word_size,
204 old_stop);
205
206 free(buf);
207 chardev_close(chardev);
208 serial_close(serial);
209 async_hangup(sess);
210
211 if (rc != EOK)
212 return "Failed to write EOT banner to serial device";
213
214 if (nwritten != eot_size)
215 return "Written less data than the size of the EOT banner "
216 "to serial device";
217
218 return NULL;
219}
220
221/** @}
222 */
Note: See TracBrowser for help on using the repository browser.