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
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
[86554e7]30 * @{
[848e3d15]31 */
[86554e7]32/**
33 * @file
[a63966d]34 * @brief Test the serial port driver - loopback test
[86554e7]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;
[b7fd2a0]58 errno_t rc;
[74017ce]59 size_t nread;
60 size_t nwritten;
[a35b458]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 }
[a35b458]75
[e80188f]76 service_id_t svc_id;
[b7fd2a0]77 errno_t res = loc_service_get_id("devices/\\hw\\pci0\\00:01.0\\com1\\a",
[e80188f]78 &svc_id, IPC_FLAG_BLOCKING);
[ffdd2b9]79 if (res != EOK)
[e80188f]80 return "Failed getting serial port service ID";
[a35b458]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";
[a35b458]86
[74017ce]87 res = chardev_open(sess, &chardev);
88 if (res != EOK) {
89 async_hangup(sess);
90 return "Failed opening serial port";
91 }
[a35b458]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 }
[a35b458]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 }
[a35b458]107
[c4c6025]108 unsigned old_baud;
109 serial_parity_t old_par;
110 unsigned old_stop;
111 unsigned old_word_size;
[a35b458]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 }
[a35b458]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 }
[a35b458]131
[e80188f]132 TPRINTF("Trying reading %zu characters from serial device "
133 "(svc_id=%" PRIun ")\n", cnt, svc_id);
[a35b458]134
[ffdd2b9]135 size_t total = 0;
[848e3d15]136 while (total < cnt) {
[a35b458]137
[f2d88f3]138 rc = chardev_read(chardev, buf, cnt - total, &nread,
139 chardev_f_none);
[74017ce]140 if (rc != EOK) {
[c4c6025]141 (void) serial_set_comm_props(serial, old_baud,
[848e3d15]142 old_par, old_word_size, old_stop);
[a35b458]143
[ffdd2b9]144 free(buf);
[74017ce]145 chardev_close(chardev);
[c4c6025]146 serial_close(serial);
[79ae36dd]147 async_hangup(sess);
[e80188f]148 return "Failed reading from serial device";
[ffdd2b9]149 }
[a35b458]150
[74017ce]151 if (nread > cnt - total) {
[c4c6025]152 (void) serial_set_comm_props(serial, old_baud,
[ffdd2b9]153 old_par, old_word_size, old_stop);
[a35b458]154
[ba95e8f]155 free(buf);
[74017ce]156 chardev_close(chardev);
[c4c6025]157 serial_close(serial);
[79ae36dd]158 async_hangup(sess);
[ffdd2b9]159 return "Read more data than expected";
[848e3d15]160 }
[a35b458]161
[74017ce]162 TPRINTF("Read %zd bytes\n", nread);
[a35b458]163
[f300523]164 buf[nread] = 0;
[a35b458]165
[f300523]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);
[a35b458]174
[f300523]175 free(buf);
176 chardev_close(chardev);
177 serial_close(serial);
178 async_hangup(sess);
179 return "Failed writing to serial device";
180 }
[a35b458]181
[f300523]182 if (nwritten != nread) {
183 (void) serial_set_comm_props(serial, old_baud,
184 old_par, old_word_size, old_stop);
[a35b458]185
[f300523]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";
[ffdd2b9]191 }
[a35b458]192
[f300523]193 TPRINTF("Written %zd bytes\n", nwritten);
[a35b458]194
[74017ce]195 total += nread;
[f658458]196 }
[a35b458]197
[ffdd2b9]198 TPRINTF("Trying to write EOT banner to the serial device\n");
[a35b458]199
[ffdd2b9]200 size_t eot_size = str_size(EOT);
[74017ce]201 rc = chardev_write(chardev, (void *) EOT, eot_size, &nwritten);
[a35b458]202
[c4c6025]203 (void) serial_set_comm_props(serial, old_baud, old_par, old_word_size,
204 old_stop);
[a35b458]205
[ba95e8f]206 free(buf);
[74017ce]207 chardev_close(chardev);
[c4c6025]208 serial_close(serial);
[79ae36dd]209 async_hangup(sess);
[a35b458]210
[74017ce]211 if (rc != EOK)
[ffdd2b9]212 return "Failed to write EOT banner to serial device";
[a35b458]213
[74017ce]214 if (nwritten != eot_size)
[ffdd2b9]215 return "Written less data than the size of the EOT banner "
216 "to serial device";
[a35b458]217
[ffdd2b9]218 return NULL;
[f658458]219}
220
[86554e7]221/** @}
222 */
Note: See TracBrowser for help on using the repository browser.