root/branches/streaming-rework/tests/test-freebob.c

Revision 336, 12.6 kB (checked in by pieterpalmers, 17 years ago)

- Merged the developments on trunk since branch-off:

branch occurred at rev 194
svn merge -r 194:HEAD https://svn.sourceforge.net/svnroot/freebob/trunk/libfreebob

- Modified libfreebobavc to use the messagebuffer for debug info.
- This should compile and run

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