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

Revision 412, 7.2 kB (checked in by pieterpalmers, 16 years ago)

- added some documentation
- added a lock()/unlock() to IAvDevice (see header)
- reimplemented test-freebob to the new C++ api
- started with support for AddressRangeMapping?, i.e. response

to reads/writes of the 1394 memory space on the host

Line 
1 /* test-freebob.c
2  * Copyright (C) 2005 by Daniel Wagner
3  * Copyright (C) 2007 by Pieter Palmers
4  *
5  * This file is part of FreeBoB.
6  *
7  * FreeBoB is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  * FreeBoB is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with FreeBoB; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA.
20  */
21
22 /*
23  * This version uses the CPP API
24  */
25
26 #include <config.h>
27
28 #include "libfreebob/freebob.h"
29 #include "libfreebob/freebob_bounce.h"
30
31 #include "debugmodule/debugmodule.h"
32 #include "fbtypes.h"
33 #include "devicemanager.h"
34 #include "iavdevice.h"
35
36 #include <signal.h>
37
38 #include <argp.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 DECLARE_GLOBAL_DEBUG_MODULE;
44
45 int run=1;
46 static void sighandler (int sig)
47 {
48     run = 0;
49 }
50
51 const char *argp_program_version = PACKAGE_STRING;
52 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
53
54 // Program documentation.
55 static char doc[] = "FreeBoB -- a driver for Firewire Audio devices (test application)\n\n"
56                     "OPERATION: discover\n"
57                     "           setsamplerate\n";
58
59 // A description of the arguments we accept.
60 static char args_doc[] = "OPERATION";
61
62 struct arguments
63 {
64     short silent;
65     short verbose;
66     int   port;
67     int   node_id;
68     int   node_id_set;
69     char* args[2]; 
70 };
71
72 // The options we understand.
73 static struct argp_option options[] = {
74     {"quiet",    'q',       0,    0,  "Don't produce any output" },
75     {"silent",   's',       0,    OPTION_ALIAS },
76
77     {"verbose",  'v', "level",    0,  "Produce verbose output" },
78
79
80     {"node",     'n',    "id",    0,  "Node to use" },
81     {"port",     'p',    "nr",    0,  "IEEE1394 Port to use" },
82     { 0 }
83 };
84
85 //-------------------------------------------------------------
86
87 // Parse a single option.
88 static error_t
89 parse_opt( int key, char* arg, struct argp_state* state )
90 {
91     // Get the input argument from `argp_parse', which we
92     // know is a pointer to our arguments structure.
93     struct arguments* arguments = ( struct arguments* ) state->input;
94     char* tail;
95
96     switch (key) {
97     case 'q': case 's':
98         arguments->silent = 1;
99         break;
100     case 'v':
101         if (arg) {
102             arguments->verbose = strtol( arg, &tail, 0 );
103             if ( errno ) {
104                 fprintf( stderr,  "Could not parse 'verbose' argument\n" );
105                 return ARGP_ERR_UNKNOWN;
106             }
107         }
108         break;
109     case 'p':
110         if (arg) {
111             arguments->port = strtol( arg, &tail, 0 );
112             if ( errno ) {
113                 fprintf( stderr,  "Could not parse 'port' argument\n" );
114                 return ARGP_ERR_UNKNOWN;
115             }
116         } else {
117             if ( errno ) {
118                 fprintf( stderr, "Could not parse 'port' argumen\n" );
119                 return ARGP_ERR_UNKNOWN;
120             }
121         }
122         break;
123     case 'n':
124         if (arg) {
125             arguments->node_id = strtol( arg, &tail, 0 );
126             if ( errno ) {
127                 fprintf( stderr,  "Could not parse 'node' argument\n" );
128                 return ARGP_ERR_UNKNOWN;
129             }
130             arguments->node_id_set=1;
131         } else {
132             if ( errno ) {
133                 fprintf( stderr, "Could not parse 'node' argumen\n" );
134                 return ARGP_ERR_UNKNOWN;
135             }
136         }
137         break;
138     case ARGP_KEY_ARG:
139         if (state->arg_num >= 2) {
140             // Too many arguments.
141             argp_usage( state );
142         }
143         arguments->args[state->arg_num] = arg;
144         break;
145     case ARGP_KEY_END:
146         if (state->arg_num < 1) {
147         // Not enough arguments.
148         argp_usage( state );
149         }
150         break;
151     default:
152         return ARGP_ERR_UNKNOWN;
153     }
154     return 0;
155 }
156
157 // Our argp parser.
158 static struct argp argp = { options, parse_opt, args_doc, doc };
159
160 int
161 main( int argc, char **argv )
162 {
163     struct arguments arguments;
164
165     // Default values.
166     arguments.silent      = 0;
167     arguments.verbose     = 0;
168     arguments.port        = 0;
169     arguments.node_id     = 0;
170     arguments.node_id_set = 0; // if we don't specify a node, discover all
171     arguments.args[0]     = "";
172     arguments.args[1]     = "";
173
174     // Parse our arguments; every option seen by `parse_opt' will
175     // be reflected in `arguments'.
176     if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) {
177         fprintf( stderr, "Could not parse command line\n" );
178         return -1;
179     }
180
181     printf("verbose level = %d\n", arguments.verbose);
182
183     printf( "Using freebob library version: %s\n\n", freebob_get_version() );
184
185     DeviceManager *m_deviceManager = new DeviceManager();
186     if ( !m_deviceManager ) {
187         fprintf( stderr, "Could not allocate device manager\n" );
188         return -1;
189     }
190     if ( !m_deviceManager->initialize( arguments.port ) ) {
191         fprintf( stderr, "Could not initialize device manager\n" );
192         delete m_deviceManager;
193         return -1;
194     }
195
196     if ( strcmp( arguments.args[0], "discover" ) == 0 ) {
197         if ( m_deviceManager->discover(arguments.verbose) ) {
198             fprintf( stderr, "Could not discover devices\n" );
199             delete m_deviceManager;
200             return -1;
201         }
202     } else if ( strcmp( arguments.args[0], "setsamplerate" ) == 0 ) {
203         char* tail;
204         int samplerate = strtol( arguments.args[1], &tail, 0 );
205         if ( errno ) {
206             fprintf( stderr,  "Could not parse samplerate argument\n" );
207             return -1;
208         }
209
210         if ( m_deviceManager->discover(arguments.verbose) ) {
211             fprintf( stderr, "Could not discover devices\n" );
212             delete m_deviceManager;
213             return -1;
214         }
215    
216         if(arguments.node_id_set) {
217             IAvDevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id );
218             if ( avDevice ) {
219                 if ( avDevice->setSamplingFrequency( parseSampleRate( samplerate ) ) ) {
220                     m_deviceManager->discover(arguments.verbose);
221                 } else {
222                     fprintf( stderr, "Could not set samplerate\n" );
223                 }
224             }
225         } else {
226         int i=0;
227            
228         int devices_on_bus = m_deviceManager->getNbDevices();
229         printf("  port = %d, devices_on_bus = %d\n", arguments.port, devices_on_bus);
230
231         for(i=0;i<devices_on_bus;i++) {
232             int node_id=m_deviceManager->getDeviceNodeId(i);
233             printf("  set samplerate for device = %d, node = %d\n", i, node_id);
234             IAvDevice* avDevice = m_deviceManager->getAvDevice( node_id );
235             if ( avDevice ) {
236                 if ( !avDevice->setSamplingFrequency( parseSampleRate( samplerate ) ) ) {
237                     fprintf( stderr, "Could not set samplerate\n" );
238                 }
239             }
240         }
241
242         }
243     } else {
244         printf( "unknown operation\n" );
245     }
246
247     return 0;
248 }
Note: See TracBrowser for help on using the browser.