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(void)
|
---|
54 | {
|
---|
55 | printf("Usage: \n test_serial count \n where count is the number of "
|
---|
56 | "characters to be read\n");
|
---|
57 | }
|
---|
58 |
|
---|
59 | int main(int argc, char *argv[])
|
---|
60 | {
|
---|
61 | if (argc != 2) {
|
---|
62 | printf(NAME ": incorrect number of arguments.\n");
|
---|
63 | print_usage();
|
---|
64 | return 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | long int cnt = strtol(argv[1], NULL, 10);
|
---|
68 |
|
---|
69 | int res;
|
---|
70 | res = devman_get_phone(DEVMAN_CLIENT, IPC_FLAG_BLOCKING);
|
---|
71 | devman_handle_t handle;
|
---|
72 |
|
---|
73 | res = devman_device_get_handle("/hw/pci0/00:01.0/com1", &handle,
|
---|
74 | IPC_FLAG_BLOCKING);
|
---|
75 | if (EOK != res) {
|
---|
76 | printf(NAME ": could not get the device handle, errno = %d.\n",
|
---|
77 | -res);
|
---|
78 | return 1;
|
---|
79 | }
|
---|
80 |
|
---|
81 | printf(NAME ": trying to read %d characters from device with handle "
|
---|
82 | "%d.\n", cnt, handle);
|
---|
83 |
|
---|
84 | int phone = devman_device_connect(handle, IPC_FLAG_BLOCKING);
|
---|
85 | if (0 > phone) {
|
---|
86 | printf(NAME ": could not connect to the device, errno = %d.\n",
|
---|
87 | -res);
|
---|
88 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
89 | return 2;
|
---|
90 | }
|
---|
91 |
|
---|
92 | char *buf = (char *) malloc(cnt + 1);
|
---|
93 | if (NULL == buf) {
|
---|
94 | printf(NAME ": failed to allocate the input buffer\n");
|
---|
95 | ipc_hangup(phone);
|
---|
96 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
97 | return 3;
|
---|
98 | }
|
---|
99 |
|
---|
100 | ipcarg_t old_baud, old_par, old_stop, old_word_size;
|
---|
101 |
|
---|
102 | res = ipc_call_sync_0_4(phone, SERIAL_GET_COM_PROPS, &old_baud,
|
---|
103 | &old_par, &old_word_size, &old_stop);
|
---|
104 | if (EOK != res) {
|
---|
105 | printf(NAME ": failed to get old communication parameters, "
|
---|
106 | "errno = %d.\n", -res);
|
---|
107 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
108 | ipc_hangup(phone);
|
---|
109 | free(buf);
|
---|
110 | return 4;
|
---|
111 | }
|
---|
112 |
|
---|
113 | res = ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, 1200,
|
---|
114 | SERIAL_NO_PARITY, 8, 1);
|
---|
115 | if (EOK != res) {
|
---|
116 | printf(NAME ": failed to set communication parameters, errno = "
|
---|
117 | "%d.\n", -res);
|
---|
118 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
119 | ipc_hangup(phone);
|
---|
120 | free(buf);
|
---|
121 | return 4;
|
---|
122 | }
|
---|
123 |
|
---|
124 | int total = 0;
|
---|
125 | int read = 0;
|
---|
126 | while (total < cnt) {
|
---|
127 | read = read_dev(phone, buf, cnt - total);
|
---|
128 | if (0 > read) {
|
---|
129 | printf(NAME ": failed read from device, errno = %d.\n",
|
---|
130 | -read);
|
---|
131 | ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
|
---|
132 | old_par, old_word_size, old_stop);
|
---|
133 | ipc_hangup(phone);
|
---|
134 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
135 | free(buf);
|
---|
136 | return 5;
|
---|
137 | }
|
---|
138 | total += read;
|
---|
139 | if (read > 0) {
|
---|
140 | buf[read] = 0;
|
---|
141 | printf(buf);
|
---|
142 | /*
|
---|
143 | * Write data back to the device to test the opposite
|
---|
144 | * direction of data transfer.
|
---|
145 | */
|
---|
146 | write_dev(phone, buf, read);
|
---|
147 | } else {
|
---|
148 | usleep(100000);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | const char *the_end = "\n---------\nTHE END\n---------\n";
|
---|
153 | write_dev(phone, (void *)the_end, str_size(the_end));
|
---|
154 |
|
---|
155 | /* restore original communication settings */
|
---|
156 | ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, old_baud, old_par,
|
---|
157 | old_word_size, old_stop);
|
---|
158 | devman_hangup_phone(DEVMAN_CLIENT);
|
---|
159 | ipc_hangup(phone);
|
---|
160 | free(buf);
|
---|
161 |
|
---|
162 | return 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /** @}
|
---|
166 | */
|
---|