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