source: mainline/uspace/app/sportdmp/sportdmp.c@ 9bec33a

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9bec33a was f2d88f3, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Add forgotten changes to enable non-blocking chardev read

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Sucha
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#include <errno.h>
30#include <io/chardev.h>
31#include <io/serial.h>
32#include <loc.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <str.h>
36
37#define BUF_SIZE 1
38
39static void syntax_print(void)
40{
41 fprintf(stderr, "Usage: sportdmp [--baud=<baud>] [device_service]\n");
42}
43
44int main(int argc, char **argv)
45{
46 sysarg_t baud = 9600;
47 service_id_t svc_id;
48 chardev_t *chardev;
49 serial_t *serial;
50 size_t nread;
51
52 int arg = 1;
53 errno_t rc;
54
55 if (argc > arg && str_test_prefix(argv[arg], "--baud=")) {
56 size_t arg_offset = str_lsize(argv[arg], 7);
57 char *arg_str = argv[arg] + arg_offset;
58 if (str_length(arg_str) == 0) {
59 fprintf(stderr, "--baud requires an argument\n");
60 syntax_print();
61 return 1;
62 }
63 char *endptr;
64 baud = strtol(arg_str, &endptr, 10);
65 if (*endptr != '\0') {
66 fprintf(stderr, "Invalid value for baud\n");
67 syntax_print();
68 return 1;
69 }
70 arg++;
71 }
72
73 if (argc > arg) {
74 rc = loc_service_get_id(argv[arg], &svc_id, 0);
75 if (rc != EOK) {
76 fprintf(stderr, "Cannot find device service %s\n",
77 argv[arg]);
78 return 1;
79 }
80 arg++;
81 } else {
82 category_id_t serial_cat_id;
83
84 rc = loc_category_get_id("serial", &serial_cat_id, 0);
85 if (rc != EOK) {
86 fprintf(stderr, "Failed getting id of category "
87 "'serial'\n");
88 return 1;
89 }
90
91 service_id_t *svc_ids;
92 size_t svc_count;
93
94 rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count);
95 if (rc != EOK) {
96 fprintf(stderr, "Failed getting list of services\n");
97 return 1;
98 }
99
100 if (svc_count == 0) {
101 fprintf(stderr, "No service in category 'serial'\n");
102 free(svc_ids);
103 return 1;
104 }
105
106 svc_id = svc_ids[0];
107 free(svc_ids);
108 }
109
110 if (argc > arg) {
111 fprintf(stderr, "Too many arguments\n");
112 syntax_print();
113 return 1;
114 }
115
116 async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
117 IPC_FLAG_BLOCKING);
118 if (sess == NULL) {
119 fprintf(stderr, "Failed connecting to service\n");
120 return 2;
121 }
122
123 rc = chardev_open(sess, &chardev);
124 if (rc != EOK) {
125 fprintf(stderr, "Failed opening character device\n");
126 return 2;
127 }
128
129 rc = serial_open(sess, &serial);
130 if (rc != EOK) {
131 fprintf(stderr, "Failed opening serial port\n");
132 return 2;
133 }
134
135 rc = serial_set_comm_props(serial, baud, SERIAL_NO_PARITY, 8, 1);
136 if (rc != EOK) {
137 fprintf(stderr, "Failed setting serial properties\n");
138 return 2;
139 }
140
141 uint8_t *buf = (uint8_t *) malloc(BUF_SIZE);
142 if (buf == NULL) {
143 fprintf(stderr, "Failed allocating buffer\n");
144 return 3;
145 }
146
147 while (true) {
148 rc = chardev_read(chardev, buf, BUF_SIZE, &nread,
149 chardev_f_none);
150 for (size_t i = 0; i < nread; i++) {
151 printf("%02hhx ", buf[i]);
152 }
153 if (rc != EOK) {
154 fprintf(stderr, "\nFailed reading from serial device\n");
155 break;
156 }
157 fflush(stdout);
158 }
159
160 free(buf);
161 serial_close(serial);
162 chardev_close(chardev);
163 async_hangup(sess);
164 return 0;
165}
Note: See TracBrowser for help on using the repository browser.