root/trunk/libffado/tests/test-ffado.cpp

Revision 795, 11.2 kB (checked in by ppalmers, 15 years ago)

small bugfixes + debug print changes

Line 
1 /*
2  * Copyright (C) 2005-2007 by Daniel Wagner
3  * Copyright (C) 2005-2007 by Pieter Palmers
4  *
5  * This file is part of FFADO
6  * FFADO = Free Firewire (pro-)audio drivers for linux
7  *
8  * FFADO is based upon FreeBoB.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 /*
26  * This version uses the CPP API
27  */
28
29 #include <config.h>
30
31 #include "libffado/ffado.h"
32
33 #include "debugmodule/debugmodule.h"
34 #include "fbtypes.h"
35 #include "devicemanager.h"
36 #include "ffadodevice.h"
37
38 #include <signal.h>
39
40 #include <argp.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44
45 #include <vector>
46 #include <string>
47 #include <iostream>
48 #include <sstream>
49
50 using namespace std;
51
52 DECLARE_GLOBAL_DEBUG_MODULE;
53
54 // global's
55 const char *argp_program_version = PACKAGE_STRING;
56 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
57
58 // Program documentation.
59 static char doc[] = "FFADO -- a driver for Firewire Audio devices (test application)\n\n"
60                     "OPERATION: Discover\n"
61                     "           SetSamplerate samplerate\n"
62                     "           SetClockSource [id]\n"
63                     ;
64
65 // A description of the arguments we accept.
66 static char args_doc[] = "OPERATION";
67
68 struct arguments
69 {
70     short    silent;
71     long int verbose;
72     long int port;
73     long int node_id;
74     long int node_id_set;
75     char* args[2];
76 };
77
78 // The options we understand.
79 static struct argp_option options[] = {
80     {"quiet",    'q',       0,    0,  "Don't produce any output" },
81     {"silent",   's',       0,    OPTION_ALIAS },
82
83     {"verbose",  'v', "level",    0,  "Produce verbose output" },
84
85
86     {"node",     'n',    "id",    0,  "Node to use" },
87     {"port",     'p',    "nr",    0,  "IEEE1394 Port to use" },
88     { 0 }
89 };
90
91 //-------------------------------------------------------------
92
93 // Parse a single option.
94 static error_t
95 parse_opt( int key, char* arg, struct argp_state* state )
96 {
97     // Get the input argument from `argp_parse', which we
98     // know is a pointer to our arguments structure.
99     struct arguments* arguments = ( struct arguments* ) state->input;
100     char* tail;
101
102     switch (key) {
103     case 'q': case 's':
104         arguments->silent = 1;
105         break;
106     case 'v':
107         if (arg) {
108             arguments->verbose = strtol( arg, &tail, 0 );
109             if ( errno ) {
110                 fprintf( stderr,  "Could not parse 'verbose' argument\n" );
111                 return ARGP_ERR_UNKNOWN;
112             }
113         }
114         break;
115     case 'p':
116         if (arg) {
117             arguments->port = strtol( arg, &tail, 0 );
118             if ( errno ) {
119                 fprintf( stderr,  "Could not parse 'port' argument\n" );
120                 return ARGP_ERR_UNKNOWN;
121             }
122         } else {
123             if ( errno ) {
124                 fprintf( stderr, "Could not parse 'port' argumen\n" );
125                 return ARGP_ERR_UNKNOWN;
126             }
127         }
128         break;
129     case 'n':
130         if (arg) {
131             arguments->node_id = strtol( arg, &tail, 0 );
132             if ( errno ) {
133                 fprintf( stderr,  "Could not parse 'node' argument\n" );
134                 return ARGP_ERR_UNKNOWN;
135             }
136             arguments->node_id_set=1;
137         } else {
138             if ( errno ) {
139                 fprintf( stderr, "Could not parse 'node' argumen\n" );
140                 return ARGP_ERR_UNKNOWN;
141             }
142         }
143         break;
144     case ARGP_KEY_ARG:
145         if (state->arg_num >= 3) {
146             // Too many arguments.
147             argp_usage( state );
148         }
149         arguments->args[state->arg_num] = arg;
150         break;
151     case ARGP_KEY_END:
152         if (state->arg_num < 1) {
153         // Not enough arguments.
154         argp_usage( state );
155         }
156         break;
157     default:
158         return ARGP_ERR_UNKNOWN;
159     }
160     return 0;
161 }
162
163 // Our argp parser.
164 static struct argp argp = { options, parse_opt, args_doc, doc };
165
166 int exitfunction( int retval ) {
167     debugOutput( DEBUG_LEVEL_NORMAL, "Debug output flushed...\n" );
168     flushDebugOutput();
169    
170     return retval;
171 }
172
173 int
174 main( int argc, char **argv )
175 {
176     struct arguments arguments;
177
178     // Default values.
179     arguments.silent      = 0;
180     arguments.verbose     = 0;
181     arguments.port        = 0;
182     arguments.node_id     = 0;
183     arguments.node_id_set = 0; // if we don't specify a node, discover all
184     arguments.args[0]     = "";
185     arguments.args[1]     = "";
186
187     // Parse our arguments; every option seen by `parse_opt' will
188     // be reflected in `arguments'.
189     if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) {
190         fprintf( stderr, "Could not parse command line\n" );
191         return exitfunction(-1);
192     }
193
194     printf("verbose level = %d\n", arguments.verbose);
195     setDebugLevel(arguments.verbose);
196
197     printf( "Using ffado library version: %s\n\n", ffado_get_version() );
198
199     if ( strcmp( arguments.args[0], "Discover" ) == 0 ) {
200         DeviceManager *m_deviceManager = new DeviceManager();
201         if ( !m_deviceManager ) {
202             fprintf( stderr, "Could not allocate device manager\n" );
203             return exitfunction(-1);
204         }
205         if ( arguments.verbose ) {
206             m_deviceManager->setVerboseLevel(arguments.verbose);
207         }
208         if ( !m_deviceManager->initialize() ) {
209             fprintf( stderr, "Could not initialize device manager\n" );
210             delete m_deviceManager;
211             return exitfunction(-1);
212         }
213         if ( arguments.verbose ) {
214             m_deviceManager->setVerboseLevel(arguments.verbose);
215         }
216         if ( !m_deviceManager->discover() ) {
217             fprintf( stderr, "Could not discover devices\n" );
218             delete m_deviceManager;
219             return exitfunction(-1);
220         }
221         delete m_deviceManager;
222         return exitfunction(0);
223     } else if ( strcmp( arguments.args[0], "SetSamplerate" ) == 0 ) {
224         char* tail;
225         int samplerate = strtol( arguments.args[1], &tail, 0 );
226         if ( errno ) {
227             fprintf( stderr,  "Could not parse samplerate argument\n" );
228             return exitfunction(-1);
229         }
230
231         DeviceManager *m_deviceManager = new DeviceManager();
232         if ( !m_deviceManager ) {
233             fprintf( stderr, "Could not allocate device manager\n" );
234             return exitfunction(-1);
235         }
236         if ( arguments.verbose ) {
237             m_deviceManager->setVerboseLevel(arguments.verbose);
238         }
239         if ( !m_deviceManager->initialize() ) {
240             fprintf( stderr, "Could not initialize device manager\n" );
241             delete m_deviceManager;
242             return exitfunction(-1);
243         }
244         if ( arguments.verbose ) {
245             m_deviceManager->setVerboseLevel(arguments.verbose);
246         }
247         if ( !m_deviceManager->discover() ) {
248             fprintf( stderr, "Could not discover devices\n" );
249             delete m_deviceManager;
250             return exitfunction(-1);
251         }
252
253         if(arguments.node_id_set) {
254             FFADODevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id );
255             if ( avDevice ) {
256                 avDevice->setVerboseLevel(arguments.verbose);
257                 if ( ! avDevice->setSamplingFrequency( samplerate ) ) {
258                     fprintf( stderr, "Could not set samplerate\n" );
259                 }
260             }
261         } else {
262             int i=0;
263
264             int devices_on_bus = m_deviceManager->getNbDevices();
265             printf("  port = %d, devices_on_bus = %d\n", arguments.port, devices_on_bus);
266
267             for(i=0;i<devices_on_bus;i++) {
268                 int node_id=m_deviceManager->getDeviceNodeId(i);
269                 printf("  set samplerate for device = %d, node = %d\n", i, node_id);
270                 FFADODevice* avDevice = m_deviceManager->getAvDevice( node_id );
271                 if ( avDevice ) {
272                     avDevice->setVerboseLevel(arguments.verbose);
273                     if ( !avDevice->setSamplingFrequency( samplerate ) ) {
274                         fprintf( stderr, "Could not set samplerate\n" );
275                     }
276                 }
277             }
278         }
279         delete m_deviceManager;
280         return exitfunction(0);
281     } else if ( strcmp( arguments.args[0], "SetClockSource" ) == 0 ) {
282         char* tail;
283         unsigned int targetid = (unsigned int)strtol( arguments.args[1], &tail, 0 );
284         if ( errno ) {
285             fprintf( stderr,  "Could not parse clock source argument\n" );
286             targetid=0xFFFF;
287         }
288         DeviceManager *m_deviceManager = new DeviceManager();
289         if ( !m_deviceManager ) {
290             fprintf( stderr, "Could not allocate device manager\n" );
291             return exitfunction(-1);
292         }
293         if ( arguments.verbose ) {
294             m_deviceManager->setVerboseLevel(arguments.verbose);
295         }
296         if ( !m_deviceManager->initialize() ) {
297             fprintf( stderr, "Could not initialize device manager\n" );
298             delete m_deviceManager;
299             return exitfunction(-1);
300         }
301         if ( arguments.verbose ) {
302             m_deviceManager->setVerboseLevel(arguments.verbose);
303         }
304         if ( !m_deviceManager->discover() ) {
305             fprintf( stderr, "Could not discover devices\n" );
306             delete m_deviceManager;
307             return exitfunction(-1);
308         }
309
310         if(arguments.node_id_set) {
311             FFADODevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id );
312             if ( avDevice ) {
313                 FFADODevice::ClockSource s;
314            
315                 avDevice->setVerboseLevel(arguments.verbose);
316                 FFADODevice::ClockSourceVector sources=avDevice->getSupportedClockSources();
317                 for ( FFADODevice::ClockSourceVector::const_iterator it
318                         = sources.begin();
319                     it != sources.end();
320                     ++it )
321                 {
322                     FFADODevice::ClockSource c=*it;
323                     printf( " Type: %s, Id: %d, Valid: %d, Active: %d, Description: %s\n",
324                         FFADODevice::ClockSourceTypeToString(c.type), c.id, c.valid, c.active, c.description.c_str());
325                    
326                     if (c.id==targetid) {
327                         s=*it;
328                     }
329                 }
330                
331                 if (s.type != FFADODevice::eCT_Invalid) {
332                     printf("  set clock source to %d\n", s.id);
333                     if ( ! avDevice->setActiveClockSource( s ) ) {
334                         fprintf( stderr, "Could not set clock source\n" );
335                     }
336                 } else {
337                     printf("  no clock source with id %d found\n", targetid);
338                 }
339             }
340         } else {
341             fprintf( stderr, "please specify a node\n" );
342         }
343         delete m_deviceManager;
344         return exitfunction(0);
345     }
346 }
Note: See TracBrowser for help on using the browser.