| 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 test_serial
|
|---|
| 30 | * @brief test the serial port driver - read from the serial port
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <stdio.h>
|
|---|
| 40 | #include <ipc/ipc.h>
|
|---|
| 41 | #include <sys/types.h>
|
|---|
| 42 | #include <async.h>
|
|---|
| 43 | #include <ipc/services.h>
|
|---|
| 44 | #include <ipc/devman.h>
|
|---|
| 45 | #include <devman.h>
|
|---|
| 46 | #include <device/char.h>
|
|---|
| 47 | #include <str.h>
|
|---|
| 48 | #include <ipc/serial_ctl.h>
|
|---|
| 49 |
|
|---|
| 50 | #define NAME "test serial"
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | static void print_usage()
|
|---|
| 54 | {
|
|---|
| 55 | printf("Usage: \n test_serial count \n where count is the number of characters to be read\n");
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | int main(int argc, char *argv[])
|
|---|
| 59 | {
|
|---|
| 60 | if (argc != 2) {
|
|---|
| 61 | printf(NAME ": incorrect number of arguments.\n");
|
|---|
| 62 | print_usage();
|
|---|
| 63 | return 0;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | long int cnt = strtol(argv[1], NULL, 10);
|
|---|
| 67 |
|
|---|
| 68 | int res;
|
|---|
| 69 | res = devman_get_phone(DEVMAN_CLIENT, IPC_FLAG_BLOCKING);
|
|---|
| 70 | device_handle_t handle;
|
|---|
| 71 |
|
|---|
| 72 | if (EOK != (res = devman_device_get_handle("/hw/pci0/00:01.0/com1", &handle, IPC_FLAG_BLOCKING))) {
|
|---|
| 73 | printf(NAME ": could not get the device handle, errno = %d.\n", -res);
|
|---|
| 74 | return 1;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | printf(NAME ": trying to read %d characters from device with handle %d.\n", cnt, handle);
|
|---|
| 78 |
|
|---|
| 79 | int phone;
|
|---|
| 80 | if (0 >= (phone = devman_device_connect(handle, IPC_FLAG_BLOCKING))) {
|
|---|
| 81 | printf(NAME ": could not connect to the device, errno = %d.\n", -res);
|
|---|
| 82 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 83 | return 2;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | char *buf = (char *)malloc(cnt+1);
|
|---|
| 87 | if (NULL == buf) {
|
|---|
| 88 | printf(NAME ": failed to allocate the input buffer\n");
|
|---|
| 89 | ipc_hangup(phone);
|
|---|
| 90 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 91 | return 3;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | unsigned int old_baud, old_par, old_stop, old_word_size;
|
|---|
| 95 |
|
|---|
| 96 | res = ipc_call_sync_0_4(phone, SERIAL_GET_COM_PROPS, &old_baud, &old_par, &old_word_size, &old_stop);
|
|---|
| 97 | if (EOK != res) {
|
|---|
| 98 | printf(NAME ": failed to get old communication parameters, errno = %d.\n", -res);
|
|---|
| 99 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 100 | ipc_hangup(phone);
|
|---|
| 101 | free(buf);
|
|---|
| 102 | return 4;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | res = ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, 1200, SERIAL_NO_PARITY, 8, 1);
|
|---|
| 106 | if (EOK != res) {
|
|---|
| 107 | printf(NAME ": failed to set communication parameters, errno = %d.\n", -res);
|
|---|
| 108 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 109 | ipc_hangup(phone);
|
|---|
| 110 | free(buf);
|
|---|
| 111 | return 4;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | int total = 0;
|
|---|
| 116 | int read = 0;
|
|---|
| 117 | while (total < cnt) {
|
|---|
| 118 | read = read_dev(phone, buf, cnt - total);
|
|---|
| 119 | if (0 > read) {
|
|---|
| 120 | printf(NAME ": failed read from device, errno = %d.\n", -read);
|
|---|
| 121 | ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, old_baud, old_par, old_word_size, old_stop);
|
|---|
| 122 | ipc_hangup(phone);
|
|---|
| 123 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 124 | free(buf);
|
|---|
| 125 | return 5;
|
|---|
| 126 | }
|
|---|
| 127 | total += read;
|
|---|
| 128 | if (read > 0) {
|
|---|
| 129 | buf[read] = 0;
|
|---|
| 130 | printf(buf);
|
|---|
| 131 | // write data back to the device to test the opposite direction of data transfer
|
|---|
| 132 | write_dev(phone, buf, read);
|
|---|
| 133 | } else {
|
|---|
| 134 | usleep(100000);
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | const char *the_end = "\n---------\nTHE END\n---------\n";
|
|---|
| 139 | write_dev(phone, (void *)the_end, str_size(the_end));
|
|---|
| 140 |
|
|---|
| 141 | // restore original communication settings
|
|---|
| 142 | ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, old_baud, old_par, old_word_size, old_stop);
|
|---|
| 143 | devman_hangup_phone(DEVMAN_CLIENT);
|
|---|
| 144 | ipc_hangup(phone);
|
|---|
| 145 | free(buf);
|
|---|
| 146 |
|
|---|
| 147 | return 0;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | /** @}
|
|---|
| 152 | */
|
|---|