| [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] | 55 | const 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 | devman_handle_t handle;
|
|---|
| [79ae36dd] | 74 | int res = devman_device_get_handle("/hw/pci0/00:01.0/com1/a", &handle,
|
|---|
| [848e3d15] | 75 | IPC_FLAG_BLOCKING);
|
|---|
| [ffdd2b9] | 76 | if (res != EOK)
|
|---|
| 77 | return "Could not get serial device handle";
|
|---|
| [f658458] | 78 |
|
|---|
| [79ae36dd] | 79 | async_sess_t *sess = devman_device_connect(EXCHANGE_SERIALIZE, handle,
|
|---|
| 80 | IPC_FLAG_BLOCKING);
|
|---|
| 81 | if (!sess)
|
|---|
| [ffdd2b9] | 82 | return "Unable to connect to serial device";
|
|---|
| [f658458] | 83 |
|
|---|
| [848e3d15] | 84 | char *buf = (char *) malloc(cnt + 1);
|
|---|
| [ffdd2b9] | 85 | if (buf == NULL) {
|
|---|
| [79ae36dd] | 86 | async_hangup(sess);
|
|---|
| [ffdd2b9] | 87 | return "Failed to allocate input buffer";
|
|---|
| [f658458] | 88 | }
|
|---|
| 89 |
|
|---|
| [96b02eb9] | 90 | sysarg_t old_baud;
|
|---|
| 91 | sysarg_t old_par;
|
|---|
| 92 | sysarg_t old_stop;
|
|---|
| 93 | sysarg_t old_word_size;
|
|---|
| [cf8cc36] | 94 |
|
|---|
| [79ae36dd] | 95 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 96 | res = async_req_0_4(exch, SERIAL_GET_COM_PROPS, &old_baud,
|
|---|
| [848e3d15] | 97 | &old_par, &old_word_size, &old_stop);
|
|---|
| [79ae36dd] | 98 | async_exchange_end(exch);
|
|---|
| 99 |
|
|---|
| [ffdd2b9] | 100 | if (res != EOK) {
|
|---|
| [cf8cc36] | 101 | free(buf);
|
|---|
| [79ae36dd] | 102 | async_hangup(sess);
|
|---|
| [ffdd2b9] | 103 | return "Failed to get old serial communication parameters";
|
|---|
| [cf8cc36] | 104 | }
|
|---|
| 105 |
|
|---|
| [79ae36dd] | 106 | exch = async_exchange_begin(sess);
|
|---|
| 107 | res = async_req_4_0(exch, SERIAL_SET_COM_PROPS, 1200,
|
|---|
| [848e3d15] | 108 | SERIAL_NO_PARITY, 8, 1);
|
|---|
| [79ae36dd] | 109 | async_exchange_end(exch);
|
|---|
| 110 |
|
|---|
| [f619943a] | 111 | if (EOK != res) {
|
|---|
| 112 | free(buf);
|
|---|
| [79ae36dd] | 113 | async_hangup(sess);
|
|---|
| [ffdd2b9] | 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) {
|
|---|
| [79ae36dd] | 122 | ssize_t read = char_dev_read(sess, buf, cnt - total);
|
|---|
| [ffdd2b9] | 123 |
|
|---|
| 124 | if (read < 0) {
|
|---|
| [79ae36dd] | 125 | exch = async_exchange_begin(sess);
|
|---|
| 126 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| [848e3d15] | 127 | old_par, old_word_size, old_stop);
|
|---|
| [79ae36dd] | 128 | async_exchange_end(exch);
|
|---|
| 129 |
|
|---|
| [ffdd2b9] | 130 | free(buf);
|
|---|
| [79ae36dd] | 131 | async_hangup(sess);
|
|---|
| [ffdd2b9] | 132 | return "Failed read from serial device";
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | if ((size_t) read > cnt - total) {
|
|---|
| [79ae36dd] | 136 | exch = async_exchange_begin(sess);
|
|---|
| 137 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| [ffdd2b9] | 138 | old_par, old_word_size, old_stop);
|
|---|
| [79ae36dd] | 139 | async_exchange_end(exch);
|
|---|
| 140 |
|
|---|
| [ba95e8f] | 141 | free(buf);
|
|---|
| [79ae36dd] | 142 | async_hangup(sess);
|
|---|
| [ffdd2b9] | 143 | return "Read more data than expected";
|
|---|
| [848e3d15] | 144 | }
|
|---|
| [ffdd2b9] | 145 |
|
|---|
| 146 | TPRINTF("Read %zd bytes\n", read);
|
|---|
| 147 |
|
|---|
| 148 | if (read == 0)
|
|---|
| 149 | usleep(DEFAULT_SLEEP);
|
|---|
| 150 | else {
|
|---|
| [ba95e8f] | 151 | buf[read] = 0;
|
|---|
| [ffdd2b9] | 152 |
|
|---|
| [848e3d15] | 153 | /*
|
|---|
| 154 | * Write data back to the device to test the opposite
|
|---|
| 155 | * direction of data transfer.
|
|---|
| 156 | */
|
|---|
| [79ae36dd] | 157 | ssize_t written = char_dev_write(sess, buf, read);
|
|---|
| [ffdd2b9] | 158 |
|
|---|
| 159 | if (written < 0) {
|
|---|
| [79ae36dd] | 160 | exch = async_exchange_begin(sess);
|
|---|
| 161 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| [ffdd2b9] | 162 | old_par, old_word_size, old_stop);
|
|---|
| [79ae36dd] | 163 | async_exchange_end(exch);
|
|---|
| 164 |
|
|---|
| [ffdd2b9] | 165 | free(buf);
|
|---|
| [79ae36dd] | 166 | async_hangup(sess);
|
|---|
| [ffdd2b9] | 167 | return "Failed write to serial device";
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | if (written != read) {
|
|---|
| [79ae36dd] | 171 | exch = async_exchange_begin(sess);
|
|---|
| 172 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| [ffdd2b9] | 173 | old_par, old_word_size, old_stop);
|
|---|
| [79ae36dd] | 174 | async_exchange_end(exch);
|
|---|
| 175 |
|
|---|
| [ffdd2b9] | 176 | free(buf);
|
|---|
| [79ae36dd] | 177 | async_hangup(sess);
|
|---|
| [ffdd2b9] | 178 | return "Written less data than read from serial device";
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | TPRINTF("Written %zd bytes\n", written);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | total += read;
|
|---|
| [f658458] | 185 | }
|
|---|
| 186 |
|
|---|
| [ffdd2b9] | 187 | TPRINTF("Trying to write EOT banner to the serial device\n");
|
|---|
| [ca97cad] | 188 |
|
|---|
| [ffdd2b9] | 189 | size_t eot_size = str_size(EOT);
|
|---|
| [79ae36dd] | 190 | ssize_t written = char_dev_write(sess, (void *) EOT, eot_size);
|
|---|
| [ffdd2b9] | 191 |
|
|---|
| [79ae36dd] | 192 | exch = async_exchange_begin(sess);
|
|---|
| 193 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| [ffdd2b9] | 194 | old_par, old_word_size, old_stop);
|
|---|
| [79ae36dd] | 195 | async_exchange_end(exch);
|
|---|
| 196 |
|
|---|
| [ba95e8f] | 197 | free(buf);
|
|---|
| [79ae36dd] | 198 | async_hangup(sess);
|
|---|
| [ffdd2b9] | 199 |
|
|---|
| 200 | if (written < 0)
|
|---|
| 201 | return "Failed to write EOT banner to serial device";
|
|---|
| 202 |
|
|---|
| 203 | if ((size_t) written != eot_size)
|
|---|
| 204 | return "Written less data than the size of the EOT banner "
|
|---|
| 205 | "to serial device";
|
|---|
| [f658458] | 206 |
|
|---|
| [ffdd2b9] | 207 | return NULL;
|
|---|
| [f658458] | 208 | }
|
|---|
| 209 |
|
|---|
| [86554e7] | 210 | /** @}
|
|---|
| 211 | */
|
|---|