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

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

Convert char_dev_iface users to chardev.

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