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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 48fd597 was f300523, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Eliminate uses of thread_usleep() in favor of async_usleep(). Obvious cases are in components that don't explicitly create threads.

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[86554e7]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
[ffdd2b9]29/** @addtogroup tester
30 * @brief Test the serial port driver - loopback test
[86554e7]31 * @{
[848e3d15]32 */
[86554e7]33/**
34 * @file
35 */
36
[74017ce]37#include <async.h>
[ba95e8f]38#include <errno.h>
[74017ce]39#include <io/chardev.h>
40#include <io/serial.h>
41#include <ipc/services.h>
42#include <loc.h>
[ba95e8f]43#include <stdlib.h>
[86554e7]44#include <stdio.h>
[582a0b8]45#include <stddef.h>
[c47e1a8]46#include <str.h>
[ffdd2b9]47#include "../../tester.h"
[f658458]48
[ffdd2b9]49#define DEFAULT_COUNT 1024
50#define DEFAULT_SLEEP 100000
51#define EOT "####> End of transfer <####\n"
[ba95e8f]52
[ffdd2b9]53const char *test_serial1(void)
[f658458]54{
[ffdd2b9]55 size_t cnt;
[c4c6025]56 serial_t *serial;
[74017ce]57 chardev_t *chardev;
58 int rc;
59 size_t nread;
60 size_t nwritten;
[f658458]61
[ffdd2b9]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 }
[f658458]75
[e80188f]76 service_id_t svc_id;
77 int res = loc_service_get_id("devices/\\hw\\pci0\\00:01.0\\com1\\a",
78 &svc_id, IPC_FLAG_BLOCKING);
[ffdd2b9]79 if (res != EOK)
[e80188f]80 return "Failed getting serial port service ID";
[f658458]81
[f9b2cb4c]82 async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
[79ae36dd]83 IPC_FLAG_BLOCKING);
[c4c6025]84 if (sess == NULL)
[e80188f]85 return "Failed connecting to serial device";
[f658458]86
[74017ce]87 res = chardev_open(sess, &chardev);
88 if (res != EOK) {
89 async_hangup(sess);
90 return "Failed opening serial port";
91 }
92
[c4c6025]93 res = serial_open(sess, &serial);
[74017ce]94 if (res != EOK) {
95 chardev_close(chardev);
96 async_hangup(sess);
[c4c6025]97 return "Failed opening serial port";
[74017ce]98 }
[c4c6025]99
[848e3d15]100 char *buf = (char *) malloc(cnt + 1);
[ffdd2b9]101 if (buf == NULL) {
[74017ce]102 chardev_close(chardev);
[c4c6025]103 serial_close(serial);
[79ae36dd]104 async_hangup(sess);
[e80188f]105 return "Failed allocating input buffer";
[f658458]106 }
107
[c4c6025]108 unsigned old_baud;
109 serial_parity_t old_par;
110 unsigned old_stop;
111 unsigned old_word_size;
[79ae36dd]112
[c4c6025]113 res = serial_get_comm_props(serial, &old_baud, &old_par,
114 &old_word_size, &old_stop);
[ffdd2b9]115 if (res != EOK) {
[cf8cc36]116 free(buf);
[74017ce]117 chardev_close(chardev);
[c4c6025]118 serial_close(serial);
[79ae36dd]119 async_hangup(sess);
[ffdd2b9]120 return "Failed to get old serial communication parameters";
[cf8cc36]121 }
122
[c4c6025]123 res = serial_set_comm_props(serial, 1200, SERIAL_NO_PARITY, 8, 1);
[f619943a]124 if (EOK != res) {
125 free(buf);
[74017ce]126 chardev_close(chardev);
[c4c6025]127 serial_close(serial);
[79ae36dd]128 async_hangup(sess);
[e80188f]129 return "Failed setting serial communication parameters";
[f619943a]130 }
131
[e80188f]132 TPRINTF("Trying reading %zu characters from serial device "
133 "(svc_id=%" PRIun ")\n", cnt, svc_id);
[ffdd2b9]134
135 size_t total = 0;
[848e3d15]136 while (total < cnt) {
[ffdd2b9]137
[74017ce]138 rc = chardev_read(chardev, buf, cnt - total, &nread);
139 if (rc != EOK) {
[c4c6025]140 (void) serial_set_comm_props(serial, old_baud,
[848e3d15]141 old_par, old_word_size, old_stop);
[79ae36dd]142
[ffdd2b9]143 free(buf);
[74017ce]144 chardev_close(chardev);
[c4c6025]145 serial_close(serial);
[79ae36dd]146 async_hangup(sess);
[e80188f]147 return "Failed reading from serial device";
[ffdd2b9]148 }
149
[74017ce]150 if (nread > cnt - total) {
[c4c6025]151 (void) serial_set_comm_props(serial, old_baud,
[ffdd2b9]152 old_par, old_word_size, old_stop);
[79ae36dd]153
[ba95e8f]154 free(buf);
[74017ce]155 chardev_close(chardev);
[c4c6025]156 serial_close(serial);
[79ae36dd]157 async_hangup(sess);
[ffdd2b9]158 return "Read more data than expected";
[848e3d15]159 }
[ffdd2b9]160
[74017ce]161 TPRINTF("Read %zd bytes\n", nread);
[ffdd2b9]162
[f300523]163 buf[nread] = 0;
164
165 /*
166 * Write data back to the device to test the opposite
167 * direction of data transfer.
168 */
169 rc = chardev_write(chardev, buf, nread, &nwritten);
170 if (rc != EOK) {
171 (void) serial_set_comm_props(serial, old_baud,
172 old_par, old_word_size, old_stop);
[ffdd2b9]173
[f300523]174 free(buf);
175 chardev_close(chardev);
176 serial_close(serial);
177 async_hangup(sess);
178 return "Failed writing to serial device";
179 }
180
181 if (nwritten != nread) {
182 (void) serial_set_comm_props(serial, old_baud,
183 old_par, old_word_size, old_stop);
[ffdd2b9]184
[f300523]185 free(buf);
186 chardev_close(chardev);
187 serial_close(serial);
188 async_hangup(sess);
189 return "Written less data than read from serial device";
[ffdd2b9]190 }
191
[f300523]192 TPRINTF("Written %zd bytes\n", nwritten);
193
[74017ce]194 total += nread;
[f658458]195 }
196
[ffdd2b9]197 TPRINTF("Trying to write EOT banner to the serial device\n");
[ca97cad]198
[ffdd2b9]199 size_t eot_size = str_size(EOT);
[74017ce]200 rc = chardev_write(chardev, (void *) EOT, eot_size, &nwritten);
[ffdd2b9]201
[c4c6025]202 (void) serial_set_comm_props(serial, old_baud, old_par, old_word_size,
203 old_stop);
[79ae36dd]204
[ba95e8f]205 free(buf);
[74017ce]206 chardev_close(chardev);
[c4c6025]207 serial_close(serial);
[79ae36dd]208 async_hangup(sess);
[ffdd2b9]209
[74017ce]210 if (rc != EOK)
[ffdd2b9]211 return "Failed to write EOT banner to serial device";
212
[74017ce]213 if (nwritten != eot_size)
[ffdd2b9]214 return "Written less data than the size of the EOT banner "
215 "to serial device";
[f658458]216
[ffdd2b9]217 return NULL;
[f658458]218}
219
[86554e7]220/** @}
221 */
Note: See TracBrowser for help on using the repository browser.