Debian Codegraph @

tools/dumpiso.c

raw
1 /*
2 * libraw1394 - library for raw access to the 1394 bus with the Linux subsystem.
3 *
4 * Copyright (C) 1999,2000 Andreas Bombe
5 *
6 * This library is licensed under the GNU Lesser General Public License (LGPL),
7 * version 2.1 or later. See the file COPYING.LIB in the distribution for
8 * details.
9 */
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <getopt.h>
15 #include <errno.h>
16 #include <fcntl.h>
17
18 #include "../src/raw1394.h"
19
20 #define BUFFER 1000
21 #define PACKET_MAX 4096
22
23 u_int64_t listen_channels;
24 unsigned long which_port;
25 char *filename;
26 int file;
27 enum raw1394_iso_dma_recv_mode mode = RAW1394_DMA_DEFAULT;
28
29 void usage_exit(int exitcode)
30 {
31 fprintf(stderr,
32 "Usage: dumpiso [opts] [FILE]\n"
33 "Dump IEEE 1394 isochronous channels to FILE or standard output.\n"
34 "\n"
35 "-c --channels CHANNELS Listen on these channels; CHANNELS is either a\n"
36 " number X or a range X-Y.\n"
37 "-p --port PORT Choose 1394 chip PORT. (default: 0)\n"
38 "-h --help Show this help.\n"
39 );
40
41 exit(exitcode);
42 }
43
44 void parse_args(int argc, char **argv)
45 {
46 int i;
47 char *tail;
48 unsigned long chan1, chan2;
49
50 int c;
51 int index;
52 static struct option opts[] = {
53 { "channels", required_argument, NULL, 'c' },
54 { "port", required_argument, NULL, 'p' },
55 { "help", no_argument, NULL, 'h' },
56 { 0 }
57 };
58
59 while (1) {
60 c = getopt_long(argc, argv, "hc:p:", opts, &index);
61 if (c == -1) break;
62
63 switch (c) {
64 case 'c':
65 chan1 = strtoul(optarg, &tail, 10);
66 chan2 = chan1;
67
68 if (*tail) {
69 if (tail[0] != '-' || !tail[1]) {
70 fprintf(stderr,
71 "invalid argument to channels: %s\n",
72 optarg);
73 usage_exit(1);
74 }
75
76 tail++;
77 chan2 = strtoul(tail, &tail, 10);
78 if (*tail) {
79 fprintf(stderr,
80 "invalid argument to channels: %s\n",
81 optarg);
82 usage_exit(1);
83 }
84 } else {
85 mode = RAW1394_DMA_PACKET_PER_BUFFER;
86 }
87
88 if (chan2 < chan1) {
89 unsigned long x = chan1;
90 chan1 = chan2;
91 chan2 = x;
92 }
93
94 if (chan2 > 63) {
95 fprintf(stderr,
96 "invalid channel numbers: %s\n",
97 optarg);
98 exit(1);
99 }
100
101 for (i = chan1; i <= chan2; i++)
102 listen_channels |= 1ULL << i;
103
104 break;
105 case 'p':
106 which_port = strtoul(optarg, &tail, 10);
107 if (*tail) {
108 fprintf(stderr,
109 "invalid argument to port: %s\n",
110 optarg);
111 usage_exit(1);
112 }
113 break;
114 case 'h':
115 usage_exit(0);
116 case '?':
117 usage_exit(1);
118 default:
119 abort();
120 }
121 }
122
123 argv += optind;
124 argc -= optind;
125
126 if (argc > 1) {
127 fprintf(stderr, "Too many arguments.\n");
128 usage_exit(1);
129 }
130
131 if (argc) filename = *argv;
132
133 if (!listen_channels) listen_channels = ~0ULL;
134 }
135
136 void write_header()
137 {
138 static char header[32] = "1394 isodump v2";
139 int i;
140
141 for (i = 0; i < 8; i++)
142 header[i+16] = (listen_channels >> (56 - 8*i)) & 0xff;
143
144 i = 0;
145 while (i < 32) {
146 int ret;
147 ret = write(file, header + i, 32 - i);
148
149 if (ret < 0) {
150 perror("header write");
151 exit(1);
152 }
153
154 i += ret;
155 }
156 }
157
158 void open_dumpfile()
159 {
160 if (!filename || !filename[0] || (filename[0] == '-' && !filename[1])) {
161 file = fileno(stdout);
162 write_header();
163 return;
164 }
165
166 file = creat(filename, 0666);
167 if (file < 0) {
168 perror("dumpfile open");
169 exit(1);
170 }
171
172 write_header();
173 }
174
175 static enum raw1394_iso_disposition
176 iso_handler(raw1394handle_t handle, unsigned char *data,
177 unsigned int length, unsigned char channel,
178 unsigned char tag, unsigned char sy, unsigned int cycle,
179 unsigned int dropped)
180 {
181 int ret;
182 unsigned char pad = 0;
183 static unsigned int counter = 0;
184
185 if (++counter % 1000 == 0)
186 fprintf(stderr, "\r%uK packets", counter/1000);
187
188 /* write header */
189 if (write(file, &length, sizeof(length)) != sizeof(length) ||
190 write(file, &channel, sizeof(channel)) != sizeof(channel) ||
191 write(file, &tag, sizeof(tag)) != sizeof(tag) ||
192 write(file, &sy, sizeof(sy)) != sizeof(sy) ||
193 write(file, &pad, sizeof(pad)) != sizeof(pad)) {
194 perror("data write");
195 return RAW1394_ISO_ERROR;
196 }
197
198 while (length) {
199 ret = write(file, data, length);
200 if (ret < 0) {
201 perror("data write");
202 return RAW1394_ISO_ERROR;
203 }
204
205 length -= ret;
206 data += ret;
207 }
208
209 return RAW1394_ISO_OK;
210 }
211
212 int main(int argc, char **argv)
213 {
214 raw1394handle_t handle;
215 int i, ret;
216
217 parse_args(argc, argv);
218
219 fprintf(stderr, "port: %ld\nchannels: %#016llx\nfile: %s\n", which_port,
220 (long long unsigned)listen_channels, filename);
221
222 handle = raw1394_new_handle();
223 if (!handle) {
224 if (!errno)
225 fprintf(stderr,
226 "No working kernel driver found.\n");
227 else
228 perror("raw1394_get_handle");
229 exit(1);
230 }
231
232 do {
233 if (raw1394_get_port_info(handle, NULL, 0) <= which_port) {
234 fprintf(stderr, "Port %ld does not exist.\n",
235 which_port);
236 exit(1);
237 }
238
239 ret = raw1394_set_port(handle, which_port);
240 } while (ret < 0 && errno == ESTALE);
241
242 if (ret < 0) {
243 perror("raw1394_set_port");
244 exit(1);
245 }
246
247 open_dumpfile();
248
249 if (mode == RAW1394_DMA_DEFAULT) {
250 raw1394_iso_multichannel_recv_init(handle, iso_handler,
251 BUFFER, 2048, -1); /* >2048 makes rawiso stall! */
252 raw1394_iso_recv_set_channel_mask(handle, listen_channels);
253
254 } else for (i = 0; i < 64; i++) {
255 if (!(listen_channels & 1ULL << i))
256 continue;
257 raw1394_iso_recv_init(handle, iso_handler, BUFFER, PACKET_MAX,
258 i, mode, -1);
259 }
260 raw1394_iso_recv_start(handle, -1, -1, 0);
261
262 while (raw1394_loop_iterate(handle) == 0);
263
264 fprintf(stderr, "\n");
265 raw1394_iso_shutdown(handle);
266 raw1394_destroy_handle(handle);
267
268 return 0;
269 }