1 |
/* test-freebob.c |
---|
2 |
* Copyright (C) 2005 by Daniel Wagner |
---|
3 |
* |
---|
4 |
* This file is part of FreeBob. |
---|
5 |
* |
---|
6 |
* FreeBob is free software; you can redistribute it and/or modify |
---|
7 |
* it under the terms of the GNU General Public License as published by |
---|
8 |
* the Free Software Foundation; either version 2 of the License, or |
---|
9 |
* (at your option) any later version. |
---|
10 |
* FreeBob is distributed in the hope that it will be useful, |
---|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 |
* GNU General Public License for more details. |
---|
14 |
* |
---|
15 |
* You should have received a copy of the GNU General Public License |
---|
16 |
* along with FreeBob; if not, write to the Free Software |
---|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
18 |
* MA 02111-1307 USA. |
---|
19 |
*/ |
---|
20 |
|
---|
21 |
#include <config.h> |
---|
22 |
|
---|
23 |
#include "libfreebob/freebob.h" |
---|
24 |
#include "libfreebob/freebob_bounce.h" |
---|
25 |
|
---|
26 |
#include <argp.h> |
---|
27 |
#include <stdlib.h> |
---|
28 |
#include <stdio.h> |
---|
29 |
#include <string.h> |
---|
30 |
|
---|
31 |
const char *argp_program_version = PACKAGE_STRING; |
---|
32 |
const char *argp_program_bug_address = PACKAGE_BUGREPORT; |
---|
33 |
|
---|
34 |
// Program documentation. |
---|
35 |
static char doc[] = "FreeBob -- a driver for BeBob devices (test application)\n\n" |
---|
36 |
"OPERATION: discover\n" |
---|
37 |
" odisocver\n" |
---|
38 |
" setsamplerate\n" |
---|
39 |
" xmldump\n" |
---|
40 |
" testmultidevicediscovery\n" |
---|
41 |
" streamformats\n"; |
---|
42 |
|
---|
43 |
// A description of the arguments we accept. |
---|
44 |
static char args_doc[] = "OPERATION"; |
---|
45 |
|
---|
46 |
struct arguments |
---|
47 |
{ |
---|
48 |
short silent; |
---|
49 |
short verbose; |
---|
50 |
int port; |
---|
51 |
int node_id; |
---|
52 |
int node_id_set; |
---|
53 |
int time; |
---|
54 |
char* args[2]; |
---|
55 |
}; |
---|
56 |
|
---|
57 |
// The options we understand. |
---|
58 |
static struct argp_option options[] = { |
---|
59 |
{"quiet", 'q', 0, 0, "Don't produce any output" }, |
---|
60 |
{"silent", 's', 0, OPTION_ALIAS }, |
---|
61 |
|
---|
62 |
{"verbose", 'v', "level", 0, "Produce verbose output" }, |
---|
63 |
|
---|
64 |
|
---|
65 |
{"node", 'n', "id", 0, "Node to use" }, |
---|
66 |
{"port", 'p', "nr", 0, "IEEE1394 Port to use" }, |
---|
67 |
{"time", 't', "time", 0, "Workaround: sleep <time> usec after AVC command\n" }, |
---|
68 |
{ 0 } |
---|
69 |
}; |
---|
70 |
|
---|
71 |
//------------------------------------------------------------- |
---|
72 |
|
---|
73 |
// Parse a single option. |
---|
74 |
static error_t |
---|
75 |
parse_opt( int key, char* arg, struct argp_state* state ) |
---|
76 |
{ |
---|
77 |
// Get the input argument from `argp_parse', which we |
---|
78 |
// know is a pointer to our arguments structure. |
---|
79 |
struct arguments* arguments = ( struct arguments* ) state->input; |
---|
80 |
char* tail; |
---|
81 |
|
---|
82 |
switch (key) { |
---|
83 |
case 'q': case 's': |
---|
84 |
arguments->silent = 1; |
---|
85 |
break; |
---|
86 |
case 'v': |
---|
87 |
if (arg) { |
---|
88 |
arguments->verbose = strtol( arg, &tail, 0 ); |
---|
89 |
if ( errno ) { |
---|
90 |
fprintf( stderr, "Could not parse 'verbose' argument\n" ); |
---|
91 |
return ARGP_ERR_UNKNOWN; |
---|
92 |
} |
---|
93 |
} |
---|
94 |
break; |
---|
95 |
case 't': |
---|
96 |
if (arg) { |
---|
97 |
arguments->time = strtol( arg, &tail, 0 ); |
---|
98 |
if ( errno ) { |
---|
99 |
fprintf( stderr, "Could not parse 'time' argument\n" ); |
---|
100 |
return ARGP_ERR_UNKNOWN; |
---|
101 |
} |
---|
102 |
} |
---|
103 |
break; |
---|
104 |
case 'p': |
---|
105 |
if (arg) { |
---|
106 |
arguments->port = strtol( arg, &tail, 0 ); |
---|
107 |
if ( errno ) { |
---|
108 |
fprintf( stderr, "Could not parse 'port' argument\n" ); |
---|
109 |
return ARGP_ERR_UNKNOWN; |
---|
110 |
} |
---|
111 |
} else { |
---|
112 |
if ( errno ) { |
---|
113 |
fprintf( stderr, "Could not parse 'port' argumen\n" ); |
---|
114 |
return ARGP_ERR_UNKNOWN; |
---|
115 |
} |
---|
116 |
} |
---|
117 |
break; |
---|
118 |
case 'n': |
---|
119 |
if (arg) { |
---|
120 |
arguments->node_id = strtol( arg, &tail, 0 ); |
---|
121 |
if ( errno ) { |
---|
122 |
fprintf( stderr, "Could not parse 'node' argument\n" ); |
---|
123 |
return ARGP_ERR_UNKNOWN; |
---|
124 |
} |
---|
125 |
arguments->node_id_set=1; |
---|
126 |
} else { |
---|
127 |
if ( errno ) { |
---|
128 |
fprintf( stderr, "Could not parse 'node' argumen\n" ); |
---|
129 |
return ARGP_ERR_UNKNOWN; |
---|
130 |
} |
---|
131 |
} |
---|
132 |
break; |
---|
133 |
case ARGP_KEY_ARG: |
---|
134 |
if (state->arg_num >= 2) { |
---|
135 |
// Too many arguments. |
---|
136 |
argp_usage( state ); |
---|
137 |
} |
---|
138 |
arguments->args[state->arg_num] = arg; |
---|
139 |
break; |
---|
140 |
case ARGP_KEY_END: |
---|
141 |
if (state->arg_num < 1) { |
---|
142 |
// Not enough arguments. |
---|
143 |
argp_usage( state ); |
---|
144 |
} |
---|
145 |
break; |
---|
146 |
default: |
---|
147 |
return ARGP_ERR_UNKNOWN; |
---|
148 |
} |
---|
149 |
return 0; |
---|
150 |
} |
---|
151 |
|
---|
152 |
// Our argp parser. |
---|
153 |
static struct argp argp = { options, parse_opt, args_doc, doc }; |
---|
154 |
|
---|
155 |
int |
---|
156 |
main( int argc, char **argv ) |
---|
157 |
{ |
---|
158 |
struct arguments arguments; |
---|
159 |
|
---|
160 |
// Default values. |
---|
161 |
arguments.silent = 0; |
---|
162 |
arguments.verbose = 0; |
---|
163 |
arguments.port = 0; |
---|
164 |
arguments.node_id = 0; |
---|
165 |
arguments.node_id_set = 0; // if we don't specify a node, discover all |
---|
166 |
arguments.time = 0; |
---|
167 |
arguments.args[0] = ""; |
---|
168 |
arguments.args[1] = ""; |
---|
169 |
|
---|
170 |
// Parse our arguments; every option seen by `parse_opt' will |
---|
171 |
// be reflected in `arguments'. |
---|
172 |
if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) { |
---|
173 |
fprintf( stderr, "Could not parse command line\n" ); |
---|
174 |
return -1; |
---|
175 |
} |
---|
176 |
|
---|
177 |
printf("verbose level = %d\n", arguments.verbose); |
---|
178 |
|
---|
179 |
printf( "Using freebob library version: %s\n\n", freebob_get_version() ); |
---|
180 |
|
---|
181 |
freebob_sleep_after_avc_command( arguments.time ); |
---|
182 |
|
---|
183 |
if ( strcmp( arguments.args[0], "discover" ) == 0 ) { |
---|
184 |
freebob_handle_t fb_handle = freebob_new_handle( arguments.port ); |
---|
185 |
if ( !fb_handle ) { |
---|
186 |
fprintf( stderr, "Could not create freebob handle\n" ); |
---|
187 |
return -1; |
---|
188 |
} |
---|
189 |
|
---|
190 |
if ( freebob_discover_devices( fb_handle, arguments.verbose ) != 0 ) { |
---|
191 |
fprintf( stderr, "Could not discover devices\n" ); |
---|
192 |
freebob_destroy_handle( fb_handle ); |
---|
193 |
return -1; |
---|
194 |
} |
---|
195 |
|
---|
196 |
freebob_connection_info_t* test_info; |
---|
197 |
|
---|
198 |
if(arguments.node_id_set) { |
---|
199 |
printf(" port = %d, node_id = %d\n", arguments.port, arguments.node_id); |
---|
200 |
test_info = freebob_get_connection_info( fb_handle, |
---|
201 |
arguments.node_id, |
---|
202 |
0 ); |
---|
203 |
freebob_print_connection_info( test_info ); |
---|
204 |
freebob_free_connection_info( test_info ); |
---|
205 |
|
---|
206 |
printf("\n"); |
---|
207 |
|
---|
208 |
test_info = freebob_get_connection_info( fb_handle, |
---|
209 |
arguments.node_id, |
---|
210 |
1 ); |
---|
211 |
freebob_print_connection_info( test_info ); |
---|
212 |
freebob_free_connection_info( test_info ); |
---|
213 |
} else { |
---|
214 |
int i=0; |
---|
215 |
|
---|
216 |
int devices_on_bus = freebob_get_nb_devices_on_bus(fb_handle); |
---|
217 |
printf(" port = %d, devices_on_bus = %d\n", arguments.port, devices_on_bus); |
---|
218 |
|
---|
219 |
for(i=0;i<devices_on_bus;i++) { |
---|
220 |
int node_id=freebob_get_device_node_id(fb_handle, i); |
---|
221 |
printf(" get info for device = %d, node = %d\n", i, node_id); |
---|
222 |
|
---|
223 |
test_info = freebob_get_connection_info( fb_handle, |
---|
224 |
node_id, |
---|
225 |
0 ); |
---|
226 |
freebob_print_connection_info( test_info ); |
---|
227 |
freebob_free_connection_info( test_info ); |
---|
228 |
|
---|
229 |
printf("\n"); |
---|
230 |
|
---|
231 |
test_info = freebob_get_connection_info( fb_handle, |
---|
232 |
node_id, |
---|
233 |
1 ); |
---|
234 |
freebob_print_connection_info( test_info ); |
---|
235 |
freebob_free_connection_info( test_info ); |
---|
236 |
} |
---|
237 |
} |
---|
238 |
|
---|
239 |
freebob_destroy_handle( fb_handle ); |
---|
240 |
|
---|
241 |
} else if ( strcmp( arguments.args[0], "setsamplerate" ) == 0 ) { |
---|
242 |
char* tail; |
---|
243 |
int samplerate = strtol( arguments.args[1], &tail, 0 ); |
---|
244 |
if ( errno ) { |
---|
245 |
fprintf( stderr, "Could not parse samplerate argument\n" ); |
---|
246 |
return -1; |
---|
247 |
} |
---|
248 |
|
---|
249 |
freebob_handle_t fb_handle = freebob_new_handle( arguments.port ); |
---|
250 |
if ( !fb_handle ) { |
---|
251 |
fprintf( stderr, "Could not create freebob handle\n" ); |
---|
252 |
return -1; |
---|
253 |
} |
---|
254 |
|
---|
255 |
if ( freebob_discover_devices( fb_handle, arguments.verbose ) != 0 ) { |
---|
256 |
fprintf( stderr, "Could not discover devices\n" ); |
---|
257 |
freebob_destroy_handle( fb_handle ); |
---|
258 |
return -1; |
---|
259 |
} |
---|
260 |
|
---|
261 |
if(arguments.node_id_set) { |
---|
262 |
if (! freebob_set_samplerate(fb_handle, arguments.node_id, samplerate)) { |
---|
263 |
fprintf( stderr, "Could not set samplerate\n" ); |
---|
264 |
freebob_destroy_handle( fb_handle ); |
---|
265 |
return -1; |
---|
266 |
} |
---|
267 |
|
---|
268 |
} else { |
---|
269 |
int i=0; |
---|
270 |
|
---|
271 |
int devices_on_bus = freebob_get_nb_devices_on_bus(fb_handle); |
---|
272 |
printf(" port = %d, devices_on_bus = %d\n", arguments.port, devices_on_bus); |
---|
273 |
|
---|
274 |
for(i=0;i<devices_on_bus;i++) { |
---|
275 |
int node_id=freebob_get_device_node_id(fb_handle, i); |
---|
276 |
printf(" set samplerate for device = %d, node = %d\n", i, node_id); |
---|
277 |
|
---|
278 |
if (! freebob_set_samplerate(fb_handle, node_id, samplerate)) { |
---|
279 |
fprintf( stderr, "Could not set samplerate\n" ); |
---|
280 |
freebob_destroy_handle( fb_handle ); |
---|
281 |
return -1; |
---|
282 |
} |
---|
283 |
} |
---|
284 |
|
---|
285 |
} |
---|
286 |
|
---|
287 |
freebob_destroy_handle( fb_handle ); |
---|
288 |
|
---|
289 |
} else if ( strcmp( arguments.args[0], "odiscover" ) == 0 ) { |
---|
290 |
freebob_handle_t fb_handle = freebob_new_handle( arguments.port ); |
---|
291 |
if ( !fb_handle ) { |
---|
292 |
fprintf( stderr, "Could not create freebob handle\n" ); |
---|
293 |
return -1; |
---|
294 |
} |
---|
295 |
|
---|
296 |
if ( freebob_discover_devices( fb_handle, arguments.verbose ) != 0 ) { |
---|
297 |
fprintf( stderr, "Could not discover devices\n" ); |
---|
298 |
freebob_destroy_handle( fb_handle ); |
---|
299 |
return -1; |
---|
300 |
} |
---|
301 |
} else if ( strcmp( arguments.args[0], "xmldump" ) == 0 ) { |
---|
302 |
freebob_handle_t fb_handle = freebob_new_handle( arguments.port ); |
---|
303 |
if ( !fb_handle ) { |
---|
304 |
fprintf( stderr, "Could not create freebob handle\n" ); |
---|
305 |
return -1; |
---|
306 |
} |
---|
307 |
|
---|
308 |
if ( freebob_discover_devices( fb_handle, 0 ) != 0 ) { |
---|
309 |
fprintf( stderr, "Could not discover devices\n" ); |
---|
310 |
freebob_destroy_handle( fb_handle ); |
---|
311 |
return -1; |
---|
312 |
} |
---|
313 |
|
---|
314 |
if(arguments.node_id_set) { |
---|
315 |
freebob_print_xml_description( fb_handle, |
---|
316 |
arguments.node_id, |
---|
317 |
0 ); |
---|
318 |
|
---|
319 |
printf("\n"); |
---|
320 |
|
---|
321 |
freebob_print_xml_description( fb_handle, |
---|
322 |
arguments.node_id, |
---|
323 |
1 ); |
---|
324 |
} else { |
---|
325 |
int i=0; |
---|
326 |
|
---|
327 |
int devices_on_bus = freebob_get_nb_devices_on_bus(fb_handle); |
---|
328 |
|
---|
329 |
for(i=0;i<devices_on_bus;i++) { |
---|
330 |
int node_id=freebob_get_device_node_id(fb_handle, i); |
---|
331 |
|
---|
332 |
freebob_print_xml_description( fb_handle, |
---|
333 |
node_id, |
---|
334 |
0 ); |
---|
335 |
printf("\n"); |
---|
336 |
|
---|
337 |
freebob_print_xml_description( fb_handle, |
---|
338 |
node_id, |
---|
339 |
1 ); |
---|
340 |
} |
---|
341 |
} |
---|
342 |
|
---|
343 |
freebob_destroy_handle( fb_handle ); |
---|
344 |
|
---|
345 |
} else if ( strcmp( arguments.args[0], "testmultidevicediscovery" ) == 0 ) { |
---|
346 |
freebob_connection_info_t* test_info; |
---|
347 |
freebob_handle_t fb_handle = freebob_new_handle( arguments.port ); |
---|
348 |
if ( !fb_handle ) { |
---|
349 |
fprintf( stderr, "Could not create freebob handle\n" ); |
---|
350 |
return -1; |
---|
351 |
} |
---|
352 |
|
---|
353 |
if ( freebob_discover_devices( fb_handle, arguments.verbose ) != 0 ) { |
---|
354 |
fprintf( stderr, "Could not discover devices\n" ); |
---|
355 |
freebob_destroy_handle( fb_handle ); |
---|
356 |
return -1; |
---|
357 |
} |
---|
358 |
test_info = freebob_get_connection_info( fb_handle, |
---|
359 |
-1, |
---|
360 |
0 ); |
---|
361 |
freebob_print_connection_info( test_info ); |
---|
362 |
freebob_free_connection_info( test_info ); |
---|
363 |
|
---|
364 |
printf("\n"); |
---|
365 |
|
---|
366 |
test_info = freebob_get_connection_info( fb_handle, |
---|
367 |
-1, |
---|
368 |
1 ); |
---|
369 |
freebob_print_connection_info( test_info ); |
---|
370 |
freebob_free_connection_info( test_info ); |
---|
371 |
|
---|
372 |
freebob_destroy_handle( fb_handle ); |
---|
373 |
|
---|
374 |
} else if ( strcmp( arguments.args[0], "streamformats" ) == 0 ) { |
---|
375 |
freebob_handle_t fb_handle = freebob_new_handle( arguments.port ); |
---|
376 |
if ( !fb_handle ) { |
---|
377 |
fprintf( stderr, "Could not create freebob handle\n" ); |
---|
378 |
return -1; |
---|
379 |
} |
---|
380 |
|
---|
381 |
if ( freebob_discover_devices( fb_handle, arguments.verbose ) != 0 ) { |
---|
382 |
fprintf( stderr, "Could not discover devices\n" ); |
---|
383 |
freebob_destroy_handle( fb_handle ); |
---|
384 |
return -1; |
---|
385 |
} |
---|
386 |
|
---|
387 |
freebob_supported_stream_format_info_t* stream_info; |
---|
388 |
if(arguments.node_id_set) { |
---|
389 |
printf(" port = %d, node_id = %d\n", arguments.port, arguments.node_id); |
---|
390 |
stream_info = freebob_get_supported_stream_format_info( fb_handle, |
---|
391 |
arguments.node_id, |
---|
392 |
0 ); |
---|
393 |
freebob_print_supported_stream_format_info( stream_info ); |
---|
394 |
freebob_free_supported_stream_format_info( stream_info ); |
---|
395 |
|
---|
396 |
printf("\n"); |
---|
397 |
|
---|
398 |
stream_info = freebob_get_supported_stream_format_info( fb_handle, |
---|
399 |
arguments.node_id, |
---|
400 |
1 ); |
---|
401 |
freebob_print_supported_stream_format_info( stream_info ); |
---|
402 |
freebob_free_supported_stream_format_info( stream_info ); |
---|
403 |
|
---|
404 |
} else { |
---|
405 |
int i=0; |
---|
406 |
|
---|
407 |
int devices_on_bus = freebob_get_nb_devices_on_bus(fb_handle); |
---|
408 |
printf(" port = %d, devices_on_bus = %d\n", arguments.port, devices_on_bus); |
---|
409 |
|
---|
410 |
for(i=0;i<devices_on_bus;i++) { |
---|
411 |
int node_id=freebob_get_device_node_id(fb_handle, i); |
---|
412 |
printf(" get info for device = %d, node = %d\n", i, node_id); |
---|
413 |
|
---|
414 |
stream_info = freebob_get_supported_stream_format_info( fb_handle, |
---|
415 |
node_id, |
---|
416 |
0 ); |
---|
417 |
freebob_print_supported_stream_format_info( stream_info ); |
---|
418 |
freebob_free_supported_stream_format_info( stream_info ); |
---|
419 |
|
---|
420 |
printf("\n"); |
---|
421 |
|
---|
422 |
stream_info = freebob_get_supported_stream_format_info( fb_handle, |
---|
423 |
node_id, |
---|
424 |
1 ); |
---|
425 |
freebob_print_supported_stream_format_info( stream_info ); |
---|
426 |
freebob_free_supported_stream_format_info( stream_info ); |
---|
427 |
} |
---|
428 |
} |
---|
429 |
|
---|
430 |
freebob_destroy_handle( fb_handle ); |
---|
431 |
|
---|
432 |
} else { |
---|
433 |
printf( "unknown operation\n" ); |
---|
434 |
} |
---|
435 |
|
---|
436 |
return 0; |
---|
437 |
} |
---|