| 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 |
|
|---|
| 29 | /** @addtogroup tester
|
|---|
| 30 | * @brief Test the serial port driver - loopback test
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <inttypes.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <stdio.h>
|
|---|
| 41 | #include <sys/types.h>
|
|---|
| 42 | #include <async.h>
|
|---|
| 43 | #include <ipc/services.h>
|
|---|
| 44 | #include <loc.h>
|
|---|
| 45 | #include <char_dev_iface.h>
|
|---|
| 46 | #include <str.h>
|
|---|
| 47 | #include <ipc/serial_ctl.h>
|
|---|
| 48 | #include "../../tester.h"
|
|---|
| 49 |
|
|---|
| 50 | #define DEFAULT_COUNT 1024
|
|---|
| 51 | #define DEFAULT_SLEEP 100000
|
|---|
| 52 | #define EOT "####> End of transfer <####\n"
|
|---|
| 53 |
|
|---|
| 54 | const char *test_serial1(void)
|
|---|
| 55 | {
|
|---|
| 56 | size_t cnt;
|
|---|
| 57 |
|
|---|
| 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 | }
|
|---|
| 71 |
|
|---|
| 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);
|
|---|
| 75 | if (res != EOK)
|
|---|
| 76 | return "Failed getting serial port service ID";
|
|---|
| 77 |
|
|---|
| 78 | async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
|
|---|
| 79 | IPC_FLAG_BLOCKING);
|
|---|
| 80 | if (!sess)
|
|---|
| 81 | return "Failed connecting to serial device";
|
|---|
| 82 |
|
|---|
| 83 | char *buf = (char *) malloc(cnt + 1);
|
|---|
| 84 | if (buf == NULL) {
|
|---|
| 85 | async_hangup(sess);
|
|---|
| 86 | return "Failed allocating input buffer";
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | sysarg_t old_baud;
|
|---|
| 90 | sysarg_t old_par;
|
|---|
| 91 | sysarg_t old_stop;
|
|---|
| 92 | sysarg_t old_word_size;
|
|---|
| 93 |
|
|---|
| 94 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 95 | res = async_req_0_4(exch, SERIAL_GET_COM_PROPS, &old_baud,
|
|---|
| 96 | &old_par, &old_word_size, &old_stop);
|
|---|
| 97 | async_exchange_end(exch);
|
|---|
| 98 |
|
|---|
| 99 | if (res != EOK) {
|
|---|
| 100 | free(buf);
|
|---|
| 101 | async_hangup(sess);
|
|---|
| 102 | return "Failed to get old serial communication parameters";
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | exch = async_exchange_begin(sess);
|
|---|
| 106 | res = async_req_4_0(exch, SERIAL_SET_COM_PROPS, 1200,
|
|---|
| 107 | SERIAL_NO_PARITY, 8, 1);
|
|---|
| 108 | async_exchange_end(exch);
|
|---|
| 109 |
|
|---|
| 110 | if (EOK != res) {
|
|---|
| 111 | free(buf);
|
|---|
| 112 | async_hangup(sess);
|
|---|
| 113 | return "Failed setting serial communication parameters";
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | TPRINTF("Trying reading %zu characters from serial device "
|
|---|
| 117 | "(svc_id=%" PRIun ")\n", cnt, svc_id);
|
|---|
| 118 |
|
|---|
| 119 | size_t total = 0;
|
|---|
| 120 | while (total < cnt) {
|
|---|
| 121 | ssize_t read = char_dev_read(sess, buf, cnt - total);
|
|---|
| 122 |
|
|---|
| 123 | if (read < 0) {
|
|---|
| 124 | exch = async_exchange_begin(sess);
|
|---|
| 125 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| 126 | old_par, old_word_size, old_stop);
|
|---|
| 127 | async_exchange_end(exch);
|
|---|
| 128 |
|
|---|
| 129 | free(buf);
|
|---|
| 130 | async_hangup(sess);
|
|---|
| 131 | return "Failed reading from serial device";
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if ((size_t) read > cnt - total) {
|
|---|
| 135 | exch = async_exchange_begin(sess);
|
|---|
| 136 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| 137 | old_par, old_word_size, old_stop);
|
|---|
| 138 | async_exchange_end(exch);
|
|---|
| 139 |
|
|---|
| 140 | free(buf);
|
|---|
| 141 | async_hangup(sess);
|
|---|
| 142 | return "Read more data than expected";
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | TPRINTF("Read %zd bytes\n", read);
|
|---|
| 146 |
|
|---|
| 147 | if (read == 0)
|
|---|
| 148 | usleep(DEFAULT_SLEEP);
|
|---|
| 149 | else {
|
|---|
| 150 | buf[read] = 0;
|
|---|
| 151 |
|
|---|
| 152 | /*
|
|---|
| 153 | * Write data back to the device to test the opposite
|
|---|
| 154 | * direction of data transfer.
|
|---|
| 155 | */
|
|---|
| 156 | ssize_t written = char_dev_write(sess, buf, read);
|
|---|
| 157 |
|
|---|
| 158 | if (written < 0) {
|
|---|
| 159 | exch = async_exchange_begin(sess);
|
|---|
| 160 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| 161 | old_par, old_word_size, old_stop);
|
|---|
| 162 | async_exchange_end(exch);
|
|---|
| 163 |
|
|---|
| 164 | free(buf);
|
|---|
| 165 | async_hangup(sess);
|
|---|
| 166 | return "Failed writing to serial device";
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | if (written != read) {
|
|---|
| 170 | exch = async_exchange_begin(sess);
|
|---|
| 171 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| 172 | old_par, old_word_size, old_stop);
|
|---|
| 173 | async_exchange_end(exch);
|
|---|
| 174 |
|
|---|
| 175 | free(buf);
|
|---|
| 176 | async_hangup(sess);
|
|---|
| 177 | return "Written less data than read from serial device";
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | TPRINTF("Written %zd bytes\n", written);
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | total += read;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | TPRINTF("Trying to write EOT banner to the serial device\n");
|
|---|
| 187 |
|
|---|
| 188 | size_t eot_size = str_size(EOT);
|
|---|
| 189 | ssize_t written = char_dev_write(sess, (void *) EOT, eot_size);
|
|---|
| 190 |
|
|---|
| 191 | exch = async_exchange_begin(sess);
|
|---|
| 192 | async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| 193 | old_par, old_word_size, old_stop);
|
|---|
| 194 | async_exchange_end(exch);
|
|---|
| 195 |
|
|---|
| 196 | free(buf);
|
|---|
| 197 | async_hangup(sess);
|
|---|
| 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";
|
|---|
| 205 |
|
|---|
| 206 | return NULL;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /** @}
|
|---|
| 210 | */
|
|---|