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

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

unify interface API

  • introduce new interfaces
  • unify location service clients to always expect service ID as the second argument
  • remove obsolete methods that take explicit exchange management arguments (first phase)
  • use interfaces in device drivers, devman, location service, logger, inet
  • Property mode set to 100644
File size: 5.6 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>
[e80188f]44#include <loc.h>
[5c65e61]45#include <char_dev_iface.h>
[c47e1a8]46#include <str.h>
[f619943a]47#include <ipc/serial_ctl.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;
[f658458]57
[ffdd2b9]58 if (test_argc < 1)
59 cnt = DEFAULT_COUNT;
60 else
61 switch (str_size_t(test_argv[0], NULL, 0, true, &cnt)) {
62 case EOK:
63 break;
64 case EINVAL:
65 return "Invalid argument, unsigned integer expected";
66 case EOVERFLOW:
67 return "Argument size overflow";
68 default:
69 return "Unexpected argument error";
70 }
[f658458]71
[e80188f]72 service_id_t svc_id;
73 int res = loc_service_get_id("devices/\\hw\\pci0\\00:01.0\\com1\\a",
74 &svc_id, IPC_FLAG_BLOCKING);
[ffdd2b9]75 if (res != EOK)
[e80188f]76 return "Failed getting serial port service ID";
[f658458]77
[f9b2cb4c]78 async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
[79ae36dd]79 IPC_FLAG_BLOCKING);
80 if (!sess)
[e80188f]81 return "Failed connecting to serial device";
[f658458]82
[848e3d15]83 char *buf = (char *) malloc(cnt + 1);
[ffdd2b9]84 if (buf == NULL) {
[79ae36dd]85 async_hangup(sess);
[e80188f]86 return "Failed allocating input buffer";
[f658458]87 }
88
[96b02eb9]89 sysarg_t old_baud;
90 sysarg_t old_par;
91 sysarg_t old_stop;
92 sysarg_t old_word_size;
[cf8cc36]93
[79ae36dd]94 async_exch_t *exch = async_exchange_begin(sess);
95 res = async_req_0_4(exch, SERIAL_GET_COM_PROPS, &old_baud,
[848e3d15]96 &old_par, &old_word_size, &old_stop);
[79ae36dd]97 async_exchange_end(exch);
98
[ffdd2b9]99 if (res != EOK) {
[cf8cc36]100 free(buf);
[79ae36dd]101 async_hangup(sess);
[ffdd2b9]102 return "Failed to get old serial communication parameters";
[cf8cc36]103 }
104
[79ae36dd]105 exch = async_exchange_begin(sess);
106 res = async_req_4_0(exch, SERIAL_SET_COM_PROPS, 1200,
[848e3d15]107 SERIAL_NO_PARITY, 8, 1);
[79ae36dd]108 async_exchange_end(exch);
109
[f619943a]110 if (EOK != res) {
111 free(buf);
[79ae36dd]112 async_hangup(sess);
[e80188f]113 return "Failed setting serial communication parameters";
[f619943a]114 }
115
[e80188f]116 TPRINTF("Trying reading %zu characters from serial device "
117 "(svc_id=%" PRIun ")\n", cnt, svc_id);
[ffdd2b9]118
119 size_t total = 0;
[848e3d15]120 while (total < cnt) {
[79ae36dd]121 ssize_t read = char_dev_read(sess, buf, cnt - total);
[ffdd2b9]122
123 if (read < 0) {
[79ae36dd]124 exch = async_exchange_begin(sess);
125 async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
[848e3d15]126 old_par, old_word_size, old_stop);
[79ae36dd]127 async_exchange_end(exch);
128
[ffdd2b9]129 free(buf);
[79ae36dd]130 async_hangup(sess);
[e80188f]131 return "Failed reading from serial device";
[ffdd2b9]132 }
133
134 if ((size_t) read > cnt - total) {
[79ae36dd]135 exch = async_exchange_begin(sess);
136 async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
[ffdd2b9]137 old_par, old_word_size, old_stop);
[79ae36dd]138 async_exchange_end(exch);
139
[ba95e8f]140 free(buf);
[79ae36dd]141 async_hangup(sess);
[ffdd2b9]142 return "Read more data than expected";
[848e3d15]143 }
[ffdd2b9]144
145 TPRINTF("Read %zd bytes\n", read);
146
147 if (read == 0)
148 usleep(DEFAULT_SLEEP);
149 else {
[ba95e8f]150 buf[read] = 0;
[ffdd2b9]151
[848e3d15]152 /*
153 * Write data back to the device to test the opposite
154 * direction of data transfer.
155 */
[79ae36dd]156 ssize_t written = char_dev_write(sess, buf, read);
[ffdd2b9]157
158 if (written < 0) {
[79ae36dd]159 exch = async_exchange_begin(sess);
160 async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
[ffdd2b9]161 old_par, old_word_size, old_stop);
[79ae36dd]162 async_exchange_end(exch);
163
[ffdd2b9]164 free(buf);
[79ae36dd]165 async_hangup(sess);
[e80188f]166 return "Failed writing to serial device";
[ffdd2b9]167 }
168
169 if (written != read) {
[79ae36dd]170 exch = async_exchange_begin(sess);
171 async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
[ffdd2b9]172 old_par, old_word_size, old_stop);
[79ae36dd]173 async_exchange_end(exch);
174
[ffdd2b9]175 free(buf);
[79ae36dd]176 async_hangup(sess);
[ffdd2b9]177 return "Written less data than read from serial device";
178 }
179
180 TPRINTF("Written %zd bytes\n", written);
181 }
182
183 total += read;
[f658458]184 }
185
[ffdd2b9]186 TPRINTF("Trying to write EOT banner to the serial device\n");
[ca97cad]187
[ffdd2b9]188 size_t eot_size = str_size(EOT);
[79ae36dd]189 ssize_t written = char_dev_write(sess, (void *) EOT, eot_size);
[ffdd2b9]190
[79ae36dd]191 exch = async_exchange_begin(sess);
192 async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
[ffdd2b9]193 old_par, old_word_size, old_stop);
[79ae36dd]194 async_exchange_end(exch);
195
[ba95e8f]196 free(buf);
[79ae36dd]197 async_hangup(sess);
[ffdd2b9]198
199 if (written < 0)
200 return "Failed to write EOT banner to serial device";
201
202 if ((size_t) written != eot_size)
203 return "Written less data than the size of the EOT banner "
204 "to serial device";
[f658458]205
[ffdd2b9]206 return NULL;
[f658458]207}
208
[86554e7]209/** @}
210 */
Note: See TracBrowser for help on using the repository browser.