root/branches/libfreebob-1.0/tests/test-freebob.c

Revision 325, 12.5 kB (checked in by wagi, 17 years ago)

2006-11-20 Daniel Wagner <wagi@newton.monom.org>

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