| 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 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief Test the serial port driver - loopback test
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <async.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <io/chardev.h>
|
|---|
| 40 | #include <io/serial.h>
|
|---|
| 41 | #include <ipc/services.h>
|
|---|
| 42 | #include <loc.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <stdio.h>
|
|---|
| 45 | #include <stddef.h>
|
|---|
| 46 | #include <str.h>
|
|---|
| 47 | #include "../../tester.h"
|
|---|
| 48 |
|
|---|
| 49 | #define DEFAULT_COUNT 1024
|
|---|
| 50 | #define DEFAULT_SLEEP 100000
|
|---|
| 51 | #define EOT "####> End of transfer <####\n"
|
|---|
| 52 |
|
|---|
| 53 | const char *test_serial1(void)
|
|---|
| 54 | {
|
|---|
| 55 | size_t cnt;
|
|---|
| 56 | serial_t *serial;
|
|---|
| 57 | chardev_t *chardev;
|
|---|
| 58 | errno_t rc;
|
|---|
| 59 | size_t nread;
|
|---|
| 60 | size_t nwritten;
|
|---|
| 61 |
|
|---|
| 62 | if (test_argc < 1)
|
|---|
| 63 | cnt = DEFAULT_COUNT;
|
|---|
| 64 | else
|
|---|
| 65 | switch (str_size_t(test_argv[0], NULL, 0, true, &cnt)) {
|
|---|
| 66 | case EOK:
|
|---|
| 67 | break;
|
|---|
| 68 | case EINVAL:
|
|---|
| 69 | return "Invalid argument, unsigned integer expected";
|
|---|
| 70 | case EOVERFLOW:
|
|---|
| 71 | return "Argument size overflow";
|
|---|
| 72 | default:
|
|---|
| 73 | return "Unexpected argument error";
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | service_id_t svc_id;
|
|---|
| 77 | errno_t res = loc_service_get_id("devices/\\hw\\pci0\\00:01.0\\com1\\a",
|
|---|
| 78 | &svc_id, IPC_FLAG_BLOCKING);
|
|---|
| 79 | if (res != EOK)
|
|---|
| 80 | return "Failed getting serial port service ID";
|
|---|
| 81 |
|
|---|
| 82 | async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
|
|---|
| 83 | IPC_FLAG_BLOCKING);
|
|---|
| 84 | if (sess == NULL)
|
|---|
| 85 | return "Failed connecting to serial device";
|
|---|
| 86 |
|
|---|
| 87 | res = chardev_open(sess, &chardev);
|
|---|
| 88 | if (res != EOK) {
|
|---|
| 89 | async_hangup(sess);
|
|---|
| 90 | return "Failed opening serial port";
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | res = serial_open(sess, &serial);
|
|---|
| 94 | if (res != EOK) {
|
|---|
| 95 | chardev_close(chardev);
|
|---|
| 96 | async_hangup(sess);
|
|---|
| 97 | return "Failed opening serial port";
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | char *buf = (char *) malloc(cnt + 1);
|
|---|
| 101 | if (buf == NULL) {
|
|---|
| 102 | chardev_close(chardev);
|
|---|
| 103 | serial_close(serial);
|
|---|
| 104 | async_hangup(sess);
|
|---|
| 105 | return "Failed allocating input buffer";
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | unsigned old_baud;
|
|---|
| 109 | serial_parity_t old_par;
|
|---|
| 110 | unsigned old_stop;
|
|---|
| 111 | unsigned old_word_size;
|
|---|
| 112 |
|
|---|
| 113 | res = serial_get_comm_props(serial, &old_baud, &old_par,
|
|---|
| 114 | &old_word_size, &old_stop);
|
|---|
| 115 | if (res != EOK) {
|
|---|
| 116 | free(buf);
|
|---|
| 117 | chardev_close(chardev);
|
|---|
| 118 | serial_close(serial);
|
|---|
| 119 | async_hangup(sess);
|
|---|
| 120 | return "Failed to get old serial communication parameters";
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | res = serial_set_comm_props(serial, 1200, SERIAL_NO_PARITY, 8, 1);
|
|---|
| 124 | if (EOK != res) {
|
|---|
| 125 | free(buf);
|
|---|
| 126 | chardev_close(chardev);
|
|---|
| 127 | serial_close(serial);
|
|---|
| 128 | async_hangup(sess);
|
|---|
| 129 | return "Failed setting serial communication parameters";
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | TPRINTF("Trying reading %zu characters from serial device "
|
|---|
| 133 | "(svc_id=%" PRIun ")\n", cnt, svc_id);
|
|---|
| 134 |
|
|---|
| 135 | size_t total = 0;
|
|---|
| 136 | while (total < cnt) {
|
|---|
| 137 |
|
|---|
| 138 | rc = chardev_read(chardev, buf, cnt - total, &nread);
|
|---|
| 139 | if (rc != EOK) {
|
|---|
| 140 | (void) serial_set_comm_props(serial, old_baud,
|
|---|
| 141 | old_par, old_word_size, old_stop);
|
|---|
| 142 |
|
|---|
| 143 | free(buf);
|
|---|
| 144 | chardev_close(chardev);
|
|---|
| 145 | serial_close(serial);
|
|---|
| 146 | async_hangup(sess);
|
|---|
| 147 | return "Failed reading from serial device";
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | if (nread > cnt - total) {
|
|---|
| 151 | (void) serial_set_comm_props(serial, old_baud,
|
|---|
| 152 | old_par, old_word_size, old_stop);
|
|---|
| 153 |
|
|---|
| 154 | free(buf);
|
|---|
| 155 | chardev_close(chardev);
|
|---|
| 156 | serial_close(serial);
|
|---|
| 157 | async_hangup(sess);
|
|---|
| 158 | return "Read more data than expected";
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | TPRINTF("Read %zd bytes\n", nread);
|
|---|
| 162 |
|
|---|
| 163 | buf[nread] = 0;
|
|---|
| 164 |
|
|---|
| 165 | /*
|
|---|
| 166 | * Write data back to the device to test the opposite
|
|---|
| 167 | * direction of data transfer.
|
|---|
| 168 | */
|
|---|
| 169 | rc = chardev_write(chardev, buf, nread, &nwritten);
|
|---|
| 170 | if (rc != EOK) {
|
|---|
| 171 | (void) serial_set_comm_props(serial, old_baud,
|
|---|
| 172 | old_par, old_word_size, old_stop);
|
|---|
| 173 |
|
|---|
| 174 | free(buf);
|
|---|
| 175 | chardev_close(chardev);
|
|---|
| 176 | serial_close(serial);
|
|---|
| 177 | async_hangup(sess);
|
|---|
| 178 | return "Failed writing to serial device";
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | if (nwritten != nread) {
|
|---|
| 182 | (void) serial_set_comm_props(serial, old_baud,
|
|---|
| 183 | old_par, old_word_size, old_stop);
|
|---|
| 184 |
|
|---|
| 185 | free(buf);
|
|---|
| 186 | chardev_close(chardev);
|
|---|
| 187 | serial_close(serial);
|
|---|
| 188 | async_hangup(sess);
|
|---|
| 189 | return "Written less data than read from serial device";
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | TPRINTF("Written %zd bytes\n", nwritten);
|
|---|
| 193 |
|
|---|
| 194 | total += nread;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | TPRINTF("Trying to write EOT banner to the serial device\n");
|
|---|
| 198 |
|
|---|
| 199 | size_t eot_size = str_size(EOT);
|
|---|
| 200 | rc = chardev_write(chardev, (void *) EOT, eot_size, &nwritten);
|
|---|
| 201 |
|
|---|
| 202 | (void) serial_set_comm_props(serial, old_baud, old_par, old_word_size,
|
|---|
| 203 | old_stop);
|
|---|
| 204 |
|
|---|
| 205 | free(buf);
|
|---|
| 206 | chardev_close(chardev);
|
|---|
| 207 | serial_close(serial);
|
|---|
| 208 | async_hangup(sess);
|
|---|
| 209 |
|
|---|
| 210 | if (rc != EOK)
|
|---|
| 211 | return "Failed to write EOT banner to serial device";
|
|---|
| 212 |
|
|---|
| 213 | if (nwritten != eot_size)
|
|---|
| 214 | return "Written less data than the size of the EOT banner "
|
|---|
| 215 | "to serial device";
|
|---|
| 216 |
|
|---|
| 217 | return NULL;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /** @}
|
|---|
| 221 | */
|
|---|