| [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 <ipc/ipc.h>
|
|---|
| 42 | #include <sys/types.h>
|
|---|
| 43 | #include <async.h>
|
|---|
| 44 | #include <ipc/services.h>
|
|---|
| [f658458] | 45 | #include <ipc/devman.h>
|
|---|
| 46 | #include <devman.h>
|
|---|
| 47 | #include <device/char.h>
|
|---|
| [c47e1a8] | 48 | #include <str.h>
|
|---|
| [f619943a] | 49 | #include <ipc/serial_ctl.h>
|
|---|
| [ffdd2b9] | 50 | #include "../../tester.h"
|
|---|
| [f658458] | 51 |
|
|---|
| [ffdd2b9] | 52 | #define DEFAULT_COUNT 1024
|
|---|
| 53 | #define DEFAULT_SLEEP 100000
|
|---|
| 54 | #define EOT "####> End of transfer <####\n"
|
|---|
| [ba95e8f] | 55 |
|
|---|
| [ffdd2b9] | 56 | const char *test_serial1(void)
|
|---|
| [f658458] | 57 | {
|
|---|
| [ffdd2b9] | 58 | size_t cnt;
|
|---|
| [f658458] | 59 |
|
|---|
| [ffdd2b9] | 60 | if (test_argc < 1)
|
|---|
| 61 | cnt = DEFAULT_COUNT;
|
|---|
| 62 | else
|
|---|
| 63 | switch (str_size_t(test_argv[0], NULL, 0, true, &cnt)) {
|
|---|
| 64 | case EOK:
|
|---|
| 65 | break;
|
|---|
| 66 | case EINVAL:
|
|---|
| 67 | return "Invalid argument, unsigned integer expected";
|
|---|
| 68 | case EOVERFLOW:
|
|---|
| 69 | return "Argument size overflow";
|
|---|
| 70 | default:
|
|---|
| 71 | return "Unexpected argument error";
|
|---|
| 72 | }
|
|---|
| [f658458] | 73 |
|
|---|
| [ffdd2b9] | 74 | int res = devman_get_phone(DEVMAN_CLIENT, IPC_FLAG_BLOCKING);
|
|---|
| [f658458] | 75 |
|
|---|
| [ffdd2b9] | 76 | devman_handle_t handle;
|
|---|
| [848e3d15] | 77 | res = devman_device_get_handle("/hw/pci0/00:01.0/com1", &handle,
|
|---|
| 78 | IPC_FLAG_BLOCKING);
|
|---|
| [ffdd2b9] | 79 | if (res != EOK)
|
|---|
| 80 | return "Could not get serial device handle";
|
|---|
| [f658458] | 81 |
|
|---|
| [848e3d15] | 82 | int phone = devman_device_connect(handle, IPC_FLAG_BLOCKING);
|
|---|
| [ffdd2b9] | 83 | if (phone < 0) {
|
|---|
| [848e3d15] | 84 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| [ffdd2b9] | 85 | return "Unable to connect to serial device";
|
|---|
| [f658458] | 86 | }
|
|---|
| 87 |
|
|---|
| [848e3d15] | 88 | char *buf = (char *) malloc(cnt + 1);
|
|---|
| [ffdd2b9] | 89 | if (buf == NULL) {
|
|---|
| [f658458] | 90 | ipc_hangup(phone);
|
|---|
| 91 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| [ffdd2b9] | 92 | return "Failed to allocate input buffer";
|
|---|
| [f658458] | 93 | }
|
|---|
| 94 |
|
|---|
| [96b02eb9] | 95 | sysarg_t old_baud;
|
|---|
| 96 | sysarg_t old_par;
|
|---|
| 97 | sysarg_t old_stop;
|
|---|
| 98 | sysarg_t old_word_size;
|
|---|
| [cf8cc36] | 99 |
|
|---|
| [848e3d15] | 100 | res = ipc_call_sync_0_4(phone, SERIAL_GET_COM_PROPS, &old_baud,
|
|---|
| 101 | &old_par, &old_word_size, &old_stop);
|
|---|
| [ffdd2b9] | 102 | if (res != EOK) {
|
|---|
| [cf8cc36] | 103 | free(buf);
|
|---|
| [ffdd2b9] | 104 | ipc_hangup(phone);
|
|---|
| 105 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 106 | return "Failed to get old serial communication parameters";
|
|---|
| [cf8cc36] | 107 | }
|
|---|
| 108 |
|
|---|
| [848e3d15] | 109 | res = ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, 1200,
|
|---|
| 110 | SERIAL_NO_PARITY, 8, 1);
|
|---|
| [f619943a] | 111 | if (EOK != res) {
|
|---|
| 112 | free(buf);
|
|---|
| [ffdd2b9] | 113 | ipc_hangup(phone);
|
|---|
| 114 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 115 | return "Failed to set serial communication parameters";
|
|---|
| [f619943a] | 116 | }
|
|---|
| 117 |
|
|---|
| [ffdd2b9] | 118 | TPRINTF("Trying to read %zu characters from serial device "
|
|---|
| 119 | "(handle=%" PRIun ")\n", cnt, handle);
|
|---|
| 120 |
|
|---|
| 121 | size_t total = 0;
|
|---|
| [848e3d15] | 122 | while (total < cnt) {
|
|---|
| [ffdd2b9] | 123 | ssize_t read = read_dev(phone, buf, cnt - total);
|
|---|
| 124 |
|
|---|
| 125 | if (read < 0) {
|
|---|
| [848e3d15] | 126 | ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| 127 | old_par, old_word_size, old_stop);
|
|---|
| [ffdd2b9] | 128 | free(buf);
|
|---|
| [ba95e8f] | 129 | ipc_hangup(phone);
|
|---|
| 130 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| [ffdd2b9] | 131 | return "Failed read from serial device";
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if ((size_t) read > cnt - total) {
|
|---|
| 135 | ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| 136 | old_par, old_word_size, old_stop);
|
|---|
| [ba95e8f] | 137 | free(buf);
|
|---|
| [ffdd2b9] | 138 | ipc_hangup(phone);
|
|---|
| 139 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 140 | return "Read more data than expected";
|
|---|
| [848e3d15] | 141 | }
|
|---|
| [ffdd2b9] | 142 |
|
|---|
| 143 | TPRINTF("Read %zd bytes\n", read);
|
|---|
| 144 |
|
|---|
| 145 | if (read == 0)
|
|---|
| 146 | usleep(DEFAULT_SLEEP);
|
|---|
| 147 | else {
|
|---|
| [ba95e8f] | 148 | buf[read] = 0;
|
|---|
| [ffdd2b9] | 149 |
|
|---|
| [848e3d15] | 150 | /*
|
|---|
| 151 | * Write data back to the device to test the opposite
|
|---|
| 152 | * direction of data transfer.
|
|---|
| 153 | */
|
|---|
| [ffdd2b9] | 154 | ssize_t written = write_dev(phone, buf, read);
|
|---|
| 155 |
|
|---|
| 156 | if (written < 0) {
|
|---|
| 157 | ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| 158 | old_par, old_word_size, old_stop);
|
|---|
| 159 | free(buf);
|
|---|
| 160 | ipc_hangup(phone);
|
|---|
| 161 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 162 | return "Failed write to serial device";
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | if (written != read) {
|
|---|
| 166 | ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| 167 | old_par, old_word_size, old_stop);
|
|---|
| 168 | free(buf);
|
|---|
| 169 | ipc_hangup(phone);
|
|---|
| 170 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 171 | return "Written less data than read from serial device";
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | TPRINTF("Written %zd bytes\n", written);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | total += read;
|
|---|
| [f658458] | 178 | }
|
|---|
| 179 |
|
|---|
| [ffdd2b9] | 180 | TPRINTF("Trying to write EOT banner to the serial device\n");
|
|---|
| [ca97cad] | 181 |
|
|---|
| [ffdd2b9] | 182 | size_t eot_size = str_size(EOT);
|
|---|
| 183 | ssize_t written = write_dev(phone, (void *) EOT, eot_size);
|
|---|
| 184 |
|
|---|
| 185 | ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
|---|
| 186 | old_par, old_word_size, old_stop);
|
|---|
| [ba95e8f] | 187 | free(buf);
|
|---|
| [ffdd2b9] | 188 | ipc_hangup(phone);
|
|---|
| 189 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 190 |
|
|---|
| 191 | if (written < 0)
|
|---|
| 192 | return "Failed to write EOT banner to serial device";
|
|---|
| 193 |
|
|---|
| 194 | if ((size_t) written != eot_size)
|
|---|
| 195 | return "Written less data than the size of the EOT banner "
|
|---|
| 196 | "to serial device";
|
|---|
| [f658458] | 197 |
|
|---|
| [ffdd2b9] | 198 | return NULL;
|
|---|
| [f658458] | 199 | }
|
|---|
| 200 |
|
|---|
| [86554e7] | 201 | /** @}
|
|---|
| 202 | */
|
|---|