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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ffa2c8ef was ffa2c8ef, checked in by Martin Decky <martin@…>, 14 years ago

do not intermix low-level IPC methods with async framework methods

  • Property mode set to 100644
File size: 5.5 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
[7e752b2]37#include <inttypes.h>
[ba95e8f]38#include <errno.h>
39#include <stdlib.h>
[86554e7]40#include <stdio.h>
41#include <sys/types.h>
42#include <async.h>
43#include <ipc/services.h>
[f658458]44#include <ipc/devman.h>
45#include <devman.h>
[ce79069b]46#include <device/char_dev.h>
[c47e1a8]47#include <str.h>
[f619943a]48#include <ipc/serial_ctl.h>
[ffdd2b9]49#include "../../tester.h"
[f658458]50
[ffdd2b9]51#define DEFAULT_COUNT 1024
52#define DEFAULT_SLEEP 100000
53#define EOT "####> End of transfer <####\n"
[ba95e8f]54
[ffdd2b9]55const char *test_serial1(void)
[f658458]56{
[ffdd2b9]57 size_t cnt;
[f658458]58
[ffdd2b9]59 if (test_argc < 1)
60 cnt = DEFAULT_COUNT;
61 else
62 switch (str_size_t(test_argv[0], NULL, 0, true, &cnt)) {
63 case EOK:
64 break;
65 case EINVAL:
66 return "Invalid argument, unsigned integer expected";
67 case EOVERFLOW:
68 return "Argument size overflow";
69 default:
70 return "Unexpected argument error";
71 }
[f658458]72
[ffdd2b9]73 int res = devman_get_phone(DEVMAN_CLIENT, IPC_FLAG_BLOCKING);
[f658458]74
[ffdd2b9]75 devman_handle_t handle;
[848e3d15]76 res = devman_device_get_handle("/hw/pci0/00:01.0/com1", &handle,
77 IPC_FLAG_BLOCKING);
[ffdd2b9]78 if (res != EOK)
79 return "Could not get serial device handle";
[f658458]80
[848e3d15]81 int phone = devman_device_connect(handle, IPC_FLAG_BLOCKING);
[ffdd2b9]82 if (phone < 0) {
[848e3d15]83 devman_hangup_phone(DEVMAN_CLIENT);
[ffdd2b9]84 return "Unable to connect to serial device";
[f658458]85 }
86
[848e3d15]87 char *buf = (char *) malloc(cnt + 1);
[ffdd2b9]88 if (buf == NULL) {
[ffa2c8ef]89 async_hangup(phone);
[f658458]90 devman_hangup_phone(DEVMAN_CLIENT);
[ffdd2b9]91 return "Failed to allocate input buffer";
[f658458]92 }
93
[96b02eb9]94 sysarg_t old_baud;
95 sysarg_t old_par;
96 sysarg_t old_stop;
97 sysarg_t old_word_size;
[cf8cc36]98
[ffa2c8ef]99 res = async_req_0_4(phone, SERIAL_GET_COM_PROPS, &old_baud,
[848e3d15]100 &old_par, &old_word_size, &old_stop);
[ffdd2b9]101 if (res != EOK) {
[cf8cc36]102 free(buf);
[ffa2c8ef]103 async_hangup(phone);
[ffdd2b9]104 devman_hangup_phone(DEVMAN_CLIENT);
105 return "Failed to get old serial communication parameters";
[cf8cc36]106 }
107
[ffa2c8ef]108 res = async_req_4_0(phone, SERIAL_SET_COM_PROPS, 1200,
[848e3d15]109 SERIAL_NO_PARITY, 8, 1);
[f619943a]110 if (EOK != res) {
111 free(buf);
[ffa2c8ef]112 async_hangup(phone);
[ffdd2b9]113 devman_hangup_phone(DEVMAN_CLIENT);
114 return "Failed to set serial communication parameters";
[f619943a]115 }
116
[ffdd2b9]117 TPRINTF("Trying to read %zu characters from serial device "
118 "(handle=%" PRIun ")\n", cnt, handle);
119
120 size_t total = 0;
[848e3d15]121 while (total < cnt) {
[b2263e6a]122 ssize_t read = char_dev_read(phone, buf, cnt - total);
[ffdd2b9]123
124 if (read < 0) {
[ffa2c8ef]125 async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
[848e3d15]126 old_par, old_word_size, old_stop);
[ffdd2b9]127 free(buf);
[ffa2c8ef]128 async_hangup(phone);
[ba95e8f]129 devman_hangup_phone(DEVMAN_CLIENT);
[ffdd2b9]130 return "Failed read from serial device";
131 }
132
133 if ((size_t) read > cnt - total) {
[ffa2c8ef]134 async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
[ffdd2b9]135 old_par, old_word_size, old_stop);
[ba95e8f]136 free(buf);
[ffa2c8ef]137 async_hangup(phone);
[ffdd2b9]138 devman_hangup_phone(DEVMAN_CLIENT);
139 return "Read more data than expected";
[848e3d15]140 }
[ffdd2b9]141
142 TPRINTF("Read %zd bytes\n", read);
143
144 if (read == 0)
145 usleep(DEFAULT_SLEEP);
146 else {
[ba95e8f]147 buf[read] = 0;
[ffdd2b9]148
[848e3d15]149 /*
150 * Write data back to the device to test the opposite
151 * direction of data transfer.
152 */
[b2263e6a]153 ssize_t written = char_dev_write(phone, buf, read);
[ffdd2b9]154
155 if (written < 0) {
[ffa2c8ef]156 async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
[ffdd2b9]157 old_par, old_word_size, old_stop);
158 free(buf);
[ffa2c8ef]159 async_hangup(phone);
[ffdd2b9]160 devman_hangup_phone(DEVMAN_CLIENT);
161 return "Failed write to serial device";
162 }
163
164 if (written != read) {
[ffa2c8ef]165 async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
[ffdd2b9]166 old_par, old_word_size, old_stop);
167 free(buf);
[ffa2c8ef]168 async_hangup(phone);
[ffdd2b9]169 devman_hangup_phone(DEVMAN_CLIENT);
170 return "Written less data than read from serial device";
171 }
172
173 TPRINTF("Written %zd bytes\n", written);
174 }
175
176 total += read;
[f658458]177 }
178
[ffdd2b9]179 TPRINTF("Trying to write EOT banner to the serial device\n");
[ca97cad]180
[ffdd2b9]181 size_t eot_size = str_size(EOT);
[b2263e6a]182 ssize_t written = char_dev_write(phone, (void *) EOT, eot_size);
[ffdd2b9]183
[ffa2c8ef]184 async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
[ffdd2b9]185 old_par, old_word_size, old_stop);
[ba95e8f]186 free(buf);
[ffa2c8ef]187 async_hangup(phone);
[ffdd2b9]188 devman_hangup_phone(DEVMAN_CLIENT);
189
190 if (written < 0)
191 return "Failed to write EOT banner to serial device";
192
193 if ((size_t) written != eot_size)
194 return "Written less data than the size of the EOT banner "
195 "to serial device";
[f658458]196
[ffdd2b9]197 return NULL;
[f658458]198}
199
[86554e7]200/** @}
201 */
Note: See TracBrowser for help on using the repository browser.