source: mainline/uspace/app/test_serial/test_serial.c@ 0b5a4131

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0b5a4131 was 0b5a4131, checked in by Jakub Jermar <jakub@…>, 15 years ago

Rename device_handle_t to devman_handle_t and make it explicitly clear that
device_handle_t is a handle understood by devman.

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[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
29/** @addtogroup test_serial
[ba95e8f]30 * @brief test the serial port driver - read from the serial port
[86554e7]31 * @{
[848e3d15]32 */
[86554e7]33/**
34 * @file
35 */
36
[ba95e8f]37#include <errno.h>
38#include <stdlib.h>
[86554e7]39#include <stdio.h>
40#include <ipc/ipc.h>
41#include <sys/types.h>
42#include <async.h>
43#include <ipc/services.h>
[f658458]44#include <ipc/devman.h>
45#include <devman.h>
46#include <device/char.h>
[c47e1a8]47#include <str.h>
[f619943a]48#include <ipc/serial_ctl.h>
[f658458]49
[ba95e8f]50#define NAME "test serial"
51
[f658458]52
[848e3d15]53static void print_usage(void)
[f658458]54{
[848e3d15]55 printf("Usage: \n test_serial count \n where count is the number of "
56 "characters to be read\n");
[86554e7]57}
58
[f658458]59int main(int argc, char *argv[])
60{
61 if (argc != 2) {
62 printf(NAME ": incorrect number of arguments.\n");
63 print_usage();
[848e3d15]64 return 0;
[f658458]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);
[0b5a4131]71 devman_handle_t handle;
[f658458]72
[848e3d15]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);
[f658458]78 return 1;
79 }
80
[848e3d15]81 printf(NAME ": trying to read %d characters from device with handle "
82 "%d.\n", cnt, handle);
[f658458]83
[848e3d15]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);
[f658458]89 return 2;
90 }
91
[848e3d15]92 char *buf = (char *) malloc(cnt + 1);
[f658458]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
[fb0baeb]100 ipcarg_t old_baud, old_par, old_stop, old_word_size;
[cf8cc36]101
[848e3d15]102 res = ipc_call_sync_0_4(phone, SERIAL_GET_COM_PROPS, &old_baud,
103 &old_par, &old_word_size, &old_stop);
[cf8cc36]104 if (EOK != res) {
[848e3d15]105 printf(NAME ": failed to get old communication parameters, "
106 "errno = %d.\n", -res);
[cf8cc36]107 devman_hangup_phone(DEVMAN_CLIENT);
108 ipc_hangup(phone);
109 free(buf);
110 return 4;
111 }
112
[848e3d15]113 res = ipc_call_sync_4_0(phone, SERIAL_SET_COM_PROPS, 1200,
114 SERIAL_NO_PARITY, 8, 1);
[f619943a]115 if (EOK != res) {
[848e3d15]116 printf(NAME ": failed to set communication parameters, errno = "
117 "%d.\n", -res);
[f619943a]118 devman_hangup_phone(DEVMAN_CLIENT);
119 ipc_hangup(phone);
120 free(buf);
[cf8cc36]121 return 4;
[f619943a]122 }
123
[ba95e8f]124 int total = 0;
125 int read = 0;
[848e3d15]126 while (total < cnt) {
[ba95e8f]127 read = read_dev(phone, buf, cnt - total);
128 if (0 > read) {
[848e3d15]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);
[ba95e8f]133 ipc_hangup(phone);
134 devman_hangup_phone(DEVMAN_CLIENT);
135 free(buf);
[f619943a]136 return 5;
[848e3d15]137 }
[ba95e8f]138 total += read;
[848e3d15]139 if (read > 0) {
[ba95e8f]140 buf[read] = 0;
[848e3d15]141 printf(buf);
142 /*
143 * Write data back to the device to test the opposite
144 * direction of data transfer.
145 */
[ca97cad]146 write_dev(phone, buf, read);
[848e3d15]147 } else {
148 usleep(100000);
[ba95e8f]149 }
[f658458]150 }
151
[c47e1a8]152 const char *the_end = "\n---------\nTHE END\n---------\n";
153 write_dev(phone, (void *)the_end, str_size(the_end));
[ca97cad]154
[848e3d15]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);
[f658458]158 devman_hangup_phone(DEVMAN_CLIENT);
159 ipc_hangup(phone);
[ba95e8f]160 free(buf);
[f658458]161
162 return 0;
163}
164
[86554e7]165/** @}
166 */
Note: See TracBrowser for help on using the repository browser.