[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 |
|
---|
[74017ce] | 37 | #include <async.h>
|
---|
[ba95e8f] | 38 | #include <errno.h>
|
---|
[74017ce] | 39 | #include <io/chardev.h>
|
---|
| 40 | #include <io/serial.h>
|
---|
| 41 | #include <ipc/services.h>
|
---|
| 42 | #include <loc.h>
|
---|
[ba95e8f] | 43 | #include <stdlib.h>
|
---|
[86554e7] | 44 | #include <stdio.h>
|
---|
[582a0b8] | 45 | #include <stddef.h>
|
---|
[c47e1a8] | 46 | #include <str.h>
|
---|
[ffdd2b9] | 47 | #include "../../tester.h"
|
---|
[f658458] | 48 |
|
---|
[ffdd2b9] | 49 | #define DEFAULT_COUNT 1024
|
---|
| 50 | #define DEFAULT_SLEEP 100000
|
---|
| 51 | #define EOT "####> End of transfer <####\n"
|
---|
[ba95e8f] | 52 |
|
---|
[ffdd2b9] | 53 | const char *test_serial1(void)
|
---|
[f658458] | 54 | {
|
---|
[ffdd2b9] | 55 | size_t cnt;
|
---|
[c4c6025] | 56 | serial_t *serial;
|
---|
[74017ce] | 57 | chardev_t *chardev;
|
---|
[b7fd2a0] | 58 | errno_t rc;
|
---|
[74017ce] | 59 | size_t nread;
|
---|
| 60 | size_t nwritten;
|
---|
[a35b458] | 61 |
|
---|
[ffdd2b9] | 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 | }
|
---|
[a35b458] | 75 |
|
---|
[e80188f] | 76 | service_id_t svc_id;
|
---|
[b7fd2a0] | 77 | errno_t res = loc_service_get_id("devices/\\hw\\pci0\\00:01.0\\com1\\a",
|
---|
[e80188f] | 78 | &svc_id, IPC_FLAG_BLOCKING);
|
---|
[ffdd2b9] | 79 | if (res != EOK)
|
---|
[e80188f] | 80 | return "Failed getting serial port service ID";
|
---|
[a35b458] | 81 |
|
---|
[f9b2cb4c] | 82 | async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
|
---|
[79ae36dd] | 83 | IPC_FLAG_BLOCKING);
|
---|
[c4c6025] | 84 | if (sess == NULL)
|
---|
[e80188f] | 85 | return "Failed connecting to serial device";
|
---|
[a35b458] | 86 |
|
---|
[74017ce] | 87 | res = chardev_open(sess, &chardev);
|
---|
| 88 | if (res != EOK) {
|
---|
| 89 | async_hangup(sess);
|
---|
| 90 | return "Failed opening serial port";
|
---|
| 91 | }
|
---|
[a35b458] | 92 |
|
---|
[c4c6025] | 93 | res = serial_open(sess, &serial);
|
---|
[74017ce] | 94 | if (res != EOK) {
|
---|
| 95 | chardev_close(chardev);
|
---|
| 96 | async_hangup(sess);
|
---|
[c4c6025] | 97 | return "Failed opening serial port";
|
---|
[74017ce] | 98 | }
|
---|
[a35b458] | 99 |
|
---|
[848e3d15] | 100 | char *buf = (char *) malloc(cnt + 1);
|
---|
[ffdd2b9] | 101 | if (buf == NULL) {
|
---|
[74017ce] | 102 | chardev_close(chardev);
|
---|
[c4c6025] | 103 | serial_close(serial);
|
---|
[79ae36dd] | 104 | async_hangup(sess);
|
---|
[e80188f] | 105 | return "Failed allocating input buffer";
|
---|
[f658458] | 106 | }
|
---|
[a35b458] | 107 |
|
---|
[c4c6025] | 108 | unsigned old_baud;
|
---|
| 109 | serial_parity_t old_par;
|
---|
| 110 | unsigned old_stop;
|
---|
| 111 | unsigned old_word_size;
|
---|
[a35b458] | 112 |
|
---|
[c4c6025] | 113 | res = serial_get_comm_props(serial, &old_baud, &old_par,
|
---|
| 114 | &old_word_size, &old_stop);
|
---|
[ffdd2b9] | 115 | if (res != EOK) {
|
---|
[cf8cc36] | 116 | free(buf);
|
---|
[74017ce] | 117 | chardev_close(chardev);
|
---|
[c4c6025] | 118 | serial_close(serial);
|
---|
[79ae36dd] | 119 | async_hangup(sess);
|
---|
[ffdd2b9] | 120 | return "Failed to get old serial communication parameters";
|
---|
[cf8cc36] | 121 | }
|
---|
[a35b458] | 122 |
|
---|
[c4c6025] | 123 | res = serial_set_comm_props(serial, 1200, SERIAL_NO_PARITY, 8, 1);
|
---|
[f619943a] | 124 | if (EOK != res) {
|
---|
| 125 | free(buf);
|
---|
[74017ce] | 126 | chardev_close(chardev);
|
---|
[c4c6025] | 127 | serial_close(serial);
|
---|
[79ae36dd] | 128 | async_hangup(sess);
|
---|
[e80188f] | 129 | return "Failed setting serial communication parameters";
|
---|
[f619943a] | 130 | }
|
---|
[a35b458] | 131 |
|
---|
[e80188f] | 132 | TPRINTF("Trying reading %zu characters from serial device "
|
---|
| 133 | "(svc_id=%" PRIun ")\n", cnt, svc_id);
|
---|
[a35b458] | 134 |
|
---|
[ffdd2b9] | 135 | size_t total = 0;
|
---|
[848e3d15] | 136 | while (total < cnt) {
|
---|
[a35b458] | 137 |
|
---|
[74017ce] | 138 | rc = chardev_read(chardev, buf, cnt - total, &nread);
|
---|
| 139 | if (rc != EOK) {
|
---|
[c4c6025] | 140 | (void) serial_set_comm_props(serial, old_baud,
|
---|
[848e3d15] | 141 | old_par, old_word_size, old_stop);
|
---|
[a35b458] | 142 |
|
---|
[ffdd2b9] | 143 | free(buf);
|
---|
[74017ce] | 144 | chardev_close(chardev);
|
---|
[c4c6025] | 145 | serial_close(serial);
|
---|
[79ae36dd] | 146 | async_hangup(sess);
|
---|
[e80188f] | 147 | return "Failed reading from serial device";
|
---|
[ffdd2b9] | 148 | }
|
---|
[a35b458] | 149 |
|
---|
[74017ce] | 150 | if (nread > cnt - total) {
|
---|
[c4c6025] | 151 | (void) serial_set_comm_props(serial, old_baud,
|
---|
[ffdd2b9] | 152 | old_par, old_word_size, old_stop);
|
---|
[a35b458] | 153 |
|
---|
[ba95e8f] | 154 | free(buf);
|
---|
[74017ce] | 155 | chardev_close(chardev);
|
---|
[c4c6025] | 156 | serial_close(serial);
|
---|
[79ae36dd] | 157 | async_hangup(sess);
|
---|
[ffdd2b9] | 158 | return "Read more data than expected";
|
---|
[848e3d15] | 159 | }
|
---|
[a35b458] | 160 |
|
---|
[74017ce] | 161 | TPRINTF("Read %zd bytes\n", nread);
|
---|
[a35b458] | 162 |
|
---|
[f300523] | 163 | buf[nread] = 0;
|
---|
[a35b458] | 164 |
|
---|
[f300523] | 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);
|
---|
[a35b458] | 173 |
|
---|
[f300523] | 174 | free(buf);
|
---|
| 175 | chardev_close(chardev);
|
---|
| 176 | serial_close(serial);
|
---|
| 177 | async_hangup(sess);
|
---|
| 178 | return "Failed writing to serial device";
|
---|
| 179 | }
|
---|
[a35b458] | 180 |
|
---|
[f300523] | 181 | if (nwritten != nread) {
|
---|
| 182 | (void) serial_set_comm_props(serial, old_baud,
|
---|
| 183 | old_par, old_word_size, old_stop);
|
---|
[a35b458] | 184 |
|
---|
[f300523] | 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";
|
---|
[ffdd2b9] | 190 | }
|
---|
[a35b458] | 191 |
|
---|
[f300523] | 192 | TPRINTF("Written %zd bytes\n", nwritten);
|
---|
[a35b458] | 193 |
|
---|
[74017ce] | 194 | total += nread;
|
---|
[f658458] | 195 | }
|
---|
[a35b458] | 196 |
|
---|
[ffdd2b9] | 197 | TPRINTF("Trying to write EOT banner to the serial device\n");
|
---|
[a35b458] | 198 |
|
---|
[ffdd2b9] | 199 | size_t eot_size = str_size(EOT);
|
---|
[74017ce] | 200 | rc = chardev_write(chardev, (void *) EOT, eot_size, &nwritten);
|
---|
[a35b458] | 201 |
|
---|
[c4c6025] | 202 | (void) serial_set_comm_props(serial, old_baud, old_par, old_word_size,
|
---|
| 203 | old_stop);
|
---|
[a35b458] | 204 |
|
---|
[ba95e8f] | 205 | free(buf);
|
---|
[74017ce] | 206 | chardev_close(chardev);
|
---|
[c4c6025] | 207 | serial_close(serial);
|
---|
[79ae36dd] | 208 | async_hangup(sess);
|
---|
[a35b458] | 209 |
|
---|
[74017ce] | 210 | if (rc != EOK)
|
---|
[ffdd2b9] | 211 | return "Failed to write EOT banner to serial device";
|
---|
[a35b458] | 212 |
|
---|
[74017ce] | 213 | if (nwritten != eot_size)
|
---|
[ffdd2b9] | 214 | return "Written less data than the size of the EOT banner "
|
---|
| 215 | "to serial device";
|
---|
[a35b458] | 216 |
|
---|
[ffdd2b9] | 217 | return NULL;
|
---|
[f658458] | 218 | }
|
---|
| 219 |
|
---|
[86554e7] | 220 | /** @}
|
---|
| 221 | */
|
---|