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 <device/char_dev.h>
|
---|
30 | #include <errno.h>
|
---|
31 | #include <ipc/serial_ctl.h>
|
---|
32 | #include <loc.h>
|
---|
33 | #include <stdio.h>
|
---|
34 |
|
---|
35 | #define BUF_SIZE 1
|
---|
36 |
|
---|
37 | static void syntax_print(void)
|
---|
38 | {
|
---|
39 | fprintf(stderr, "Usage: sportdmp [--baud=<baud>] [device_service]\n");
|
---|
40 | }
|
---|
41 |
|
---|
42 | int main(int argc, char **argv)
|
---|
43 | {
|
---|
44 | sysarg_t baud = 9600;
|
---|
45 | service_id_t svc_id;
|
---|
46 |
|
---|
47 | int arg = 1;
|
---|
48 | int rc;
|
---|
49 |
|
---|
50 | if (argc > arg && str_test_prefix(argv[arg], "--baud=")) {
|
---|
51 | size_t arg_offset = str_lsize(argv[arg], 7);
|
---|
52 | char* arg_str = argv[arg] + arg_offset;
|
---|
53 | if (str_length(arg_str) == 0) {
|
---|
54 | fprintf(stderr, "--baud requires an argument\n");
|
---|
55 | syntax_print();
|
---|
56 | return 1;
|
---|
57 | }
|
---|
58 | char *endptr;
|
---|
59 | baud = strtol(arg_str, &endptr, 10);
|
---|
60 | if (*endptr != '\0') {
|
---|
61 | fprintf(stderr, "Invalid value for baud\n");
|
---|
62 | syntax_print();
|
---|
63 | return 1;
|
---|
64 | }
|
---|
65 | arg++;
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (argc > arg) {
|
---|
69 | rc = loc_service_get_id(argv[arg], &svc_id, 0);
|
---|
70 | if (rc != EOK) {
|
---|
71 | fprintf(stderr, "Cannot find device service %s\n",
|
---|
72 | argv[arg]);
|
---|
73 | return 1;
|
---|
74 | }
|
---|
75 | arg++;
|
---|
76 | }
|
---|
77 | else {
|
---|
78 | category_id_t serial_cat_id;
|
---|
79 |
|
---|
80 | rc = loc_category_get_id("serial", &serial_cat_id, 0);
|
---|
81 | if (rc != EOK) {
|
---|
82 | fprintf(stderr, "Failed getting id of category "
|
---|
83 | "'serial'\n");
|
---|
84 | return 1;
|
---|
85 | }
|
---|
86 |
|
---|
87 | service_id_t *svc_ids;
|
---|
88 | size_t svc_count;
|
---|
89 |
|
---|
90 | rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count); if (rc != EOK) {
|
---|
91 | fprintf(stderr, "Failed getting list of services\n");
|
---|
92 | return 1;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (svc_count == 0) {
|
---|
96 | fprintf(stderr, "No service in category 'serial'\n");
|
---|
97 | free(svc_ids);
|
---|
98 | return 1;
|
---|
99 | }
|
---|
100 |
|
---|
101 | svc_id = svc_ids[0];
|
---|
102 | free(svc_ids);
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (argc > arg) {
|
---|
106 | fprintf(stderr, "Too many arguments\n");
|
---|
107 | syntax_print();
|
---|
108 | return 1;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | async_sess_t *sess = loc_service_connect(EXCHANGE_SERIALIZE, svc_id,
|
---|
113 | IPC_FLAG_BLOCKING);
|
---|
114 | if (!sess) {
|
---|
115 | fprintf(stderr, "Failed connecting to service\n");
|
---|
116 | }
|
---|
117 |
|
---|
118 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
119 | rc = async_req_4_0(exch, SERIAL_SET_COM_PROPS, baud,
|
---|
120 | SERIAL_NO_PARITY, 8, 1);
|
---|
121 | async_exchange_end(exch);
|
---|
122 |
|
---|
123 | if (rc != EOK) {
|
---|
124 | fprintf(stderr, "Failed setting serial properties\n");
|
---|
125 | return 2;
|
---|
126 | }
|
---|
127 |
|
---|
128 | uint8_t *buf = (uint8_t *) malloc(BUF_SIZE);
|
---|
129 | if (buf == NULL) {
|
---|
130 | fprintf(stderr, "Failed allocating buffer\n");
|
---|
131 | return 3;
|
---|
132 | }
|
---|
133 |
|
---|
134 | while (true) {
|
---|
135 | ssize_t read = char_dev_read(sess, buf, BUF_SIZE);
|
---|
136 | if (read < 0) {
|
---|
137 | fprintf(stderr, "Failed reading from serial device\n");
|
---|
138 | break;
|
---|
139 | }
|
---|
140 | ssize_t i;
|
---|
141 | for (i = 0; i < read; i++) {
|
---|
142 | printf("%02hhx ", buf[i]);
|
---|
143 | }
|
---|
144 | fflush(stdout);
|
---|
145 | }
|
---|
146 |
|
---|
147 | free(buf);
|
---|
148 | return 0;
|
---|
149 | }
|
---|