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