Debian Codegraph @

tools/sendiso.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 #include <string.h>
18
19 #include "../src/raw1394.h"
20
21 #define BUFFER 1000
22 #define PACKET_MAX 4096
23
24 unsigned long which_port;
25 char *filename;
26
27 unsigned long loopcount = 1;
28 unsigned int speed;
29
30 void usage_exit(int exitcode)
31 {
32 fprintf(stderr,
33 "Usage: sendiso [opts] FILE\n"
34 "Send IEEE 1394 isochronous packets from dump file FILE.\n"
35 "\n"
36 "-l --loop COUNT Repeat sending data COUNT times.\n"
37 "-i --infinite Repeat sending data infinitely.\n"
38 "-s --speed SPEED Send data at SPEED (valid values are 100, 200, 400 or\n"
39 " alternatively 1, 2, 4). (default: 100)\n"
40 "\n"
41 "-p --port PORT Choose 1394 chip PORT. (default: 0)\n"
42 "-h --help Show this help.\n"
43 );
44
45 exit(exitcode);
46 }
47
48 void parse_args(int argc, char **argv)
49 {
50 char *tail;
51
52 int c;
53 int index;
54 static struct option opts[] = {
55 { "file", required_argument, NULL, 'f' },
56 { "loop", required_argument, NULL, 'l' },
57 { "infinite", no_argument, NULL, 'i' },
58 { "speed", required_argument, NULL, 's' },
59 { "port", required_argument, NULL, 'p' },
60 { "help", no_argument, NULL, 'h' },
61 { 0 }
62 };
63
64 while (1) {
65 c = getopt_long(argc, argv, "f:l:is:p:h", opts, &index);
66 if (c == -1) break;
67
68 switch (c) {
69 case 'f':
70 filename = optarg;
71 break;
72 case 'l':
73 loopcount = strtoul(optarg, &tail, 10);
74 if (*tail) {
75 fprintf(stderr,
76 "invalid argument to loop: %s\n",
77 optarg);
78 usage_exit(1);
79 }
80 break;
81 case 'i':
82 loopcount = 0;
83 break;
84 case 's':
85 speed = strtoul(optarg, &tail, 10);
86 if (*tail) speed = -1;
87
88 switch (speed) {
89 case 1:
90 case 100:
91 speed = RAW1394_ISO_SPEED_100;
92 break;
93 case 2:
94 case 200:
95 speed = RAW1394_ISO_SPEED_200;
96 break;
97 case 4:
98 case 400:
99 speed = RAW1394_ISO_SPEED_400;
100 break;
101 default:
102 fprintf(stderr,
103 "invalid argument to speed: %s\n",
104 optarg);
105 usage_exit(1);
106 }
107 break;
108 case 'p':
109 which_port = strtoul(optarg, &tail, 10);
110 if (*tail) {
111 fprintf(stderr,
112 "invalid argument to port: %s\n",
113 optarg);
114 usage_exit(1);
115 }
116 break;
117 case 'h':
118 usage_exit(0);
119 case '?':
120 usage_exit(1);
121 case 0:
122 break;
123 default:
124 abort();
125 }
126 }
127
128 argv += optind;
129 argc -= optind;
130
131 if (argc > 1) {
132 fprintf(stderr, "Too many arguments.\n");
133 usage_exit(1);
134 }
135
136 if (argc) filename = *argv;
137 }
138
139 #define BUF_SIZE 4096
140 #define BUF_HEAD 8
141 void send_file_once(raw1394handle_t handle, int file)
142 {
143 int count, i, ret;
144 unsigned channel, tag, sy;
145 size_t length;
146 static unsigned char buffer[BUF_SIZE + BUF_HEAD];
147 static unsigned int counter = 0;
148 static int inited = 0;
149
150 while (1) {
151 count = read(file, buffer, BUF_HEAD);
152 if (count < 0) {
153 perror("read");
154 exit(1);
155 }
156 if (count < BUF_HEAD)
157 return;
158
159 i = 0;
160 length = ((unsigned int *)buffer)[i];
161 channel = buffer[i + 4];
162 tag = buffer[i + 5];
163 sy = buffer[i + 6];
164
165 i += BUF_HEAD;
166 while (count < length + BUF_HEAD) {
167 ret = read(file, buffer + count,
168 length - count + BUF_HEAD);
169
170 if (ret < 0) {
171 perror("read");
172 exit(1);
173
174 }
175 if (ret == 0)
176 return;
177
178 count += ret;
179 }
180
181 if (inited == 0) {
182 /*
183 fprintf(stderr, "transmitting first packet with length "
184 "%d on channel %d with tag %d and sy %d\n",
185 length, channel, tag, sy);
186 */
187 ret = raw1394_iso_xmit_init(handle, NULL, BUFFER,
188 PACKET_MAX, channel, speed, -1);
189 if (ret < 0) {
190 perror("raw1394_iso_xmit_init");
191 exit(1);
192 }
193 raw1394_iso_xmit_start(handle, -1, -1);
194 inited = 1;
195 }
196
197 if (++counter % 1000 == 0)
198 fprintf(stderr, "\r%uK packets", counter/1000);
199
200 ret = raw1394_iso_xmit_write(handle, &buffer[i],
201 length, tag, sy);
202 if (ret < 0) {
203 perror("\nraw1394_iso_xmit_write");
204 exit(1);
205 }
206 }
207 }
208
209
210 void send_iso_file(raw1394handle_t handle)
211 {
212 int file;
213 int count, ret;
214 char buffer[32];
215
216 if (filename[0] == '-' && filename[1] == '\0') {
217 file = fileno(stdin);
218 } else {
219 file = open(filename, O_RDONLY, 0);
220
221 if (file < 0) {
222 perror("open");
223 exit(1);
224 }
225 }
226
227 count = 32;
228 while (count) {
229 ret = read(file, buffer, count);
230
231 if (!ret) goto bad_format;
232
233 if (ret < 0) {
234 perror("read");
235 exit(1);
236 }
237
238 count -= ret;
239 }
240
241 if (memcmp("1394 isodump v", buffer, 14)) goto bad_format;
242 if (buffer[14] != '2') goto wrong_version;
243
244 while (1) {
245 send_file_once(handle, file);
246
247 if (!loopcount) {
248 if (lseek(file, 32, SEEK_SET) < 0) {
249 perror("lseek");
250 exit(1);
251 }
252 continue;
253 }
254 if (!(--loopcount)) break;
255
256 if (lseek(file, 32, SEEK_SET) < 0) {
257 perror("lseek");
258 exit(1);
259 }
260 }
261
262 return;
263
264 bad_format:
265 fprintf(stderr, "Input file format not recognized.\n");
266 exit(1);
267
268 wrong_version:
269 fprintf(stderr, "Format version of input file not supported.\n");
270 exit(1);
271 }
272
273
274 int main(int argc, char **argv)
275 {
276 raw1394handle_t handle;
277 int ret;
278
279 parse_args(argc, argv);
280
281 fprintf(stderr, "port: %ld\nloops: %ld\nfile: %s\n", which_port,
282 loopcount, filename);
283
284 handle = raw1394_new_handle();
285 if (!handle) {
286 if (!errno)
287 fprintf(stderr, "No working kernel driver found.\n");
288 else
289 perror("raw1394_get_handle");
290 exit(1);
291 }
292
293 do {
294 if (raw1394_get_port_info(handle, NULL, 0) <= which_port) {
295 fprintf(stderr, "Port %ld does not exist.\n",
296 which_port);
297 exit(1);
298 }
299
300 ret = raw1394_set_port(handle, which_port);
301 } while (ret < 0 && errno == ESTALE);
302
303 if (ret < 0) {
304 perror("raw1394_set_port");
305 exit(1);
306 }
307
308 if (filename)
309 send_iso_file(handle);
310
311 fprintf(stderr, "\n");
312 raw1394_iso_shutdown(handle);
313 raw1394_destroy_handle(handle);
314
315 return 0;
316 }