source: mainline/uspace/app/sportdmp/sportdmp.c@ 948222e4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 948222e4 was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Remove unnecessary includes from <stdio.h>.

  • 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 }
82 else {
83 category_id_t serial_cat_id;
84
85 rc = loc_category_get_id("serial", &serial_cat_id, 0);
86 if (rc != EOK) {
87 fprintf(stderr, "Failed getting id of category "
88 "'serial'\n");
89 return 1;
90 }
91
92 service_id_t *svc_ids;
93 size_t svc_count;
94
95 rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count);
96 if (rc != EOK) {
97 fprintf(stderr, "Failed getting list of services\n");
98 return 1;
99 }
100
101 if (svc_count == 0) {
102 fprintf(stderr, "No service in category 'serial'\n");
103 free(svc_ids);
104 return 1;
105 }
106
107 svc_id = svc_ids[0];
108 free(svc_ids);
109 }
110
111 if (argc > arg) {
112 fprintf(stderr, "Too many arguments\n");
113 syntax_print();
114 return 1;
115 }
116
117
118 async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
119 IPC_FLAG_BLOCKING);
120 if (sess == NULL) {
121 fprintf(stderr, "Failed connecting to service\n");
122 return 2;
123 }
124
125 rc = chardev_open(sess, &chardev);
126 if (rc != EOK) {
127 fprintf(stderr, "Failed opening character device\n");
128 return 2;
129 }
130
131 rc = serial_open(sess, &serial);
132 if (rc != EOK) {
133 fprintf(stderr, "Failed opening serial port\n");
134 return 2;
135 }
136
137 rc = serial_set_comm_props(serial, baud, SERIAL_NO_PARITY, 8, 1);
138 if (rc != EOK) {
139 fprintf(stderr, "Failed setting serial properties\n");
140 return 2;
141 }
142
143 uint8_t *buf = (uint8_t *) malloc(BUF_SIZE);
144 if (buf == NULL) {
145 fprintf(stderr, "Failed allocating buffer\n");
146 return 3;
147 }
148
149 while (true) {
150 rc = chardev_read(chardev, buf, BUF_SIZE, &nread);
151 for (size_t i = 0; i < nread; i++) {
152 printf("%02hhx ", buf[i]);
153 }
154 if (rc != EOK) {
155 fprintf(stderr, "\nFailed reading from serial device\n");
156 break;
157 }
158 fflush(stdout);
159 }
160
161 free(buf);
162 serial_close(serial);
163 chardev_close(chardev);
164 async_hangup(sess);
165 return 0;
166}
167
Note: See TracBrowser for help on using the repository browser.