source: mainline/uspace/app/tester/chardev/chardev1.c@ 36f0738

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 36f0738 was c1694b6b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Add str_error() in numerous places.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2017 Jiri Svoboda
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#include <errno.h>
30#include <str_error.h>
31#include <ipc/services.h>
32#include <io/chardev.h>
33#include <loc.h>
34#include "../tester.h"
35
36#define SMALL_BUFFER_SIZE 64
37#define LARGE_BUFFER_SIZE (DATA_XFER_LIMIT * 4)
38
39static char small_buffer[SMALL_BUFFER_SIZE];
40static char large_buffer[LARGE_BUFFER_SIZE];
41
42/** Test device that always performs small transfers. */
43static const char *test_chardev1_smallx(void)
44{
45 chardev_t *chardev;
46 service_id_t sid;
47 async_sess_t *sess;
48 size_t nbytes;
49 int rc;
50
51 TPRINTF("Test small transfer character device operations\n");
52
53 rc = loc_service_get_id(SERVICE_NAME_CHARDEV_TEST_SMALLX, &sid, 0);
54 if (rc != EOK) {
55 return "Failed resolving test device "
56 SERVICE_NAME_CHARDEV_TEST_SMALLX;
57 }
58
59 sess = loc_service_connect(sid, INTERFACE_DDF, 0);
60 if (sess == NULL)
61 return "Failed connecting test device";
62
63 rc = chardev_open(sess, &chardev);
64 if (rc != EOK) {
65 async_hangup(sess);
66 return "Failed opening test device";
67 }
68
69 rc = chardev_write(chardev, small_buffer, SMALL_BUFFER_SIZE, &nbytes);
70 if (rc != EOK) {
71 chardev_close(chardev);
72 async_hangup(sess);
73 return "Failed sending data";
74 }
75
76 TPRINTF("Sent %zu bytes\n", nbytes);
77
78 rc = chardev_read(chardev, small_buffer, SMALL_BUFFER_SIZE, &nbytes);
79 if (rc != EOK) {
80 chardev_close(chardev);
81 async_hangup(sess);
82 return "Failed receiving data";
83 }
84
85 TPRINTF("Received %zu bytes\n", nbytes);
86
87 chardev_close(chardev);
88 async_hangup(sess);
89
90 TPRINTF("Done\n");
91 return NULL;
92}
93
94/** Test device that always performs large transfers. */
95static const char *test_chardev1_largex(void)
96{
97 chardev_t *chardev;
98 service_id_t sid;
99 async_sess_t *sess;
100 size_t nbytes;
101 int rc;
102
103 TPRINTF("Test large transfer character device operations\n");
104
105 rc = loc_service_get_id(SERVICE_NAME_CHARDEV_TEST_LARGEX, &sid, 0);
106 if (rc != EOK) {
107 return "Failed resolving test device "
108 SERVICE_NAME_CHARDEV_TEST_LARGEX;
109 }
110
111 sess = loc_service_connect(sid, INTERFACE_DDF, 0);
112 if (sess == NULL)
113 return "Failed connecting test device";
114
115 rc = chardev_open(sess, &chardev);
116 if (rc != EOK) {
117 async_hangup(sess);
118 return "Failed opening test device";
119 }
120
121 rc = chardev_write(chardev, large_buffer, LARGE_BUFFER_SIZE, &nbytes);
122 if (rc != EOK) {
123 chardev_close(chardev);
124 async_hangup(sess);
125 return "Failed sending data";
126 }
127
128 TPRINTF("Sent %zu bytes\n", nbytes);
129
130 rc = chardev_read(chardev, large_buffer, LARGE_BUFFER_SIZE, &nbytes);
131 if (rc != EOK) {
132 chardev_close(chardev);
133 async_hangup(sess);
134 return "Failed receiving data";
135 }
136
137 TPRINTF("Received %zu bytes\n", nbytes);
138
139 chardev_close(chardev);
140 async_hangup(sess);
141
142 TPRINTF("Done\n");
143 return NULL;
144}
145
146/** Test device where all transfers return partial success. */
147static const char *test_chardev1_partialx(void)
148{
149 chardev_t *chardev;
150 service_id_t sid;
151 async_sess_t *sess;
152 size_t nbytes;
153 int rc;
154
155 TPRINTF("Test partially-successful character device operations\n");
156
157 rc = loc_service_get_id(SERVICE_NAME_CHARDEV_TEST_PARTIALX, &sid, 0);
158 if (rc != EOK) {
159 return "Failed resolving test device "
160 SERVICE_NAME_CHARDEV_TEST_SMALLX;
161 }
162
163 sess = loc_service_connect(sid, INTERFACE_DDF, 0);
164 if (sess == NULL)
165 return "Failed connecting test device";
166
167 rc = chardev_open(sess, &chardev);
168 if (rc != EOK) {
169 async_hangup(sess);
170 return "Failed opening test device";
171 }
172
173 rc = chardev_write(chardev, small_buffer, SMALL_BUFFER_SIZE, &nbytes);
174 if (rc != EIO || nbytes != 1) {
175 chardev_close(chardev);
176 async_hangup(sess);
177 return "Failed sending data";
178 }
179
180 TPRINTF("Sent %zu bytes and got rc = %s (expected)\n", nbytes,
181 str_error_name(rc));
182
183 rc = chardev_read(chardev, small_buffer, SMALL_BUFFER_SIZE, &nbytes);
184 if (rc != EIO || nbytes != 1) {
185 chardev_close(chardev);
186 async_hangup(sess);
187 return "Failed receiving data";
188 }
189
190 TPRINTF("Received %zu bytes and got rc = %s (expected)\n", nbytes,
191 str_error_name(rc));
192
193 chardev_close(chardev);
194 async_hangup(sess);
195
196 TPRINTF("Done\n");
197 return NULL;
198}
199
200const char *test_chardev1(void)
201{
202 const char *s;
203
204 s = test_chardev1_smallx();
205 if (s != NULL)
206 return s;
207
208 s = test_chardev1_largex();
209 if (s != NULL)
210 return s;
211
212 return test_chardev1_partialx();;
213}
Note: See TracBrowser for help on using the repository browser.