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

Revision 864, 11.6 kB (checked in by ppalmers, 16 years ago)

update license to GPLv2 or GPLv3 instead of GPLv2 or any later version. Update copyrights to reflect the new year

Line 
1 /*
2  * Copyright (C) 2005-2008 by Daniel Wagner
3  * Copyright (C) 2005-2008 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 2 of the License, or
13  * (at your option) version 3 of the License.
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 use_cache;
74     long int node_id;
75     long int node_id_set;
76     char* args[2];
77 };
78
79 // The options we understand.
80 static struct argp_option options[] = {
81     {"quiet",    'q',       0,    0,  "Don't produce any output" },
82     {"silent",   's',       0,    OPTION_ALIAS },
83
84     {"verbose",  'v', "level",    0,  "Produce verbose output" },
85     {"cache",    'c', "enable",   0,  "Use AVC model cache (default=enabled)" },
86
87
88     {"node",     'n',    "id",    0,  "Node to use" },
89     {"port",     'p',    "nr",    0,  "IEEE1394 Port to use" },
90     { 0 }
91 };
92
93 //-------------------------------------------------------------
94
95 // Parse a single option.
96 static error_t
97 parse_opt( int key, char* arg, struct argp_state* state )
98 {
99     // Get the input argument from `argp_parse', which we
100     // know is a pointer to our arguments structure.
101     struct arguments* arguments = ( struct arguments* ) state->input;
102     char* tail;
103
104     switch (key) {
105     case 'q': case 's':
106         arguments->silent = 1;
107         break;
108     case 'v':
109         if (arg) {
110             arguments->verbose = strtol( arg, &tail, 0 );
111             if ( errno ) {
112                 fprintf( stderr,  "Could not parse 'verbose' argument\n" );
113                 return ARGP_ERR_UNKNOWN;
114             }
115         }
116         break;
117     case 'c':
118         if (arg) {
119             arguments->use_cache = strtol( arg, &tail, 0 );
120             if ( errno ) {
121                 fprintf( stderr,  "Could not parse 'cache' argument\n" );
122                 return ARGP_ERR_UNKNOWN;
123             }
124         }
125         break;     
126     case 'p':
127         if (arg) {
128             arguments->port = strtol( arg, &tail, 0 );
129             if ( errno ) {
130                 fprintf( stderr,  "Could not parse 'port' argument\n" );
131                 return ARGP_ERR_UNKNOWN;
132             }
133         } else {
134             if ( errno ) {
135                 fprintf( stderr, "Could not parse 'port' argumen\n" );
136                 return ARGP_ERR_UNKNOWN;
137             }
138         }
139         break;
140     case 'n':
141         if (arg) {
142             arguments->node_id = strtol( arg, &tail, 0 );
143             if ( errno ) {
144                 fprintf( stderr,  "Could not parse 'node' argument\n" );
145                 return ARGP_ERR_UNKNOWN;
146             }
147             arguments->node_id_set=1;
148         } else {
149             if ( errno ) {
150                 fprintf( stderr, "Could not parse 'node' argumen\n" );
151                 return ARGP_ERR_UNKNOWN;
152             }
153         }
154         break;
155     case ARGP_KEY_ARG:
156         if (state->arg_num >= 3) {
157             // Too many arguments.
158             argp_usage( state );
159         }
160         arguments->args[state->arg_num] = arg;
161         break;
162     case ARGP_KEY_END:
163         if (state->arg_num < 1) {
164         // Not enough arguments.
165         argp_usage( state );
166         }
167         break;
168     default:
169         return ARGP_ERR_UNKNOWN;
170     }
171     return 0;
172 }
173
174 // Our argp parser.
175 static struct argp argp = { options, parse_opt, args_doc, doc };
176
177 int exitfunction( int retval ) {
178     debugOutput( DEBUG_LEVEL_NORMAL, "Debug output flushed...\n" );
179     flushDebugOutput();
180    
181     return retval;
182 }
183
184 int
185 main( int argc, char **argv )
186 {
187     struct arguments arguments;
188
189     // Default values.
190     arguments.silent      = 0;
191     arguments.verbose     = 0;
192     arguments.use_cache   = 1;
193     arguments.port        = 0;
194     arguments.node_id     = 0;
195     arguments.node_id_set = 0; // if we don't specify a node, discover all
196     arguments.args[0]     = "";
197     arguments.args[1]     = "";
198
199     // Parse our arguments; every option seen by `parse_opt' will
200     // be reflected in `arguments'.
201     if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) {
202         fprintf( stderr, "Could not parse command line\n" );
203         return exitfunction(-1);
204     }
205
206     printf("verbose level = %d\n", arguments.verbose);
207     setDebugLevel(arguments.verbose);
208
209     printf( "Using ffado library version: %s\n\n", ffado_get_version() );
210
211     if ( strcmp( arguments.args[0], "Discover" ) == 0 ) {
212         DeviceManager *m_deviceManager = new DeviceManager();
213         if ( !m_deviceManager ) {
214             fprintf( stderr, "Could not allocate device manager\n" );
215             return exitfunction(-1);
216         }
217         if ( arguments.verbose ) {
218             m_deviceManager->setVerboseLevel(arguments.verbose);
219         }
220         if ( !m_deviceManager->initialize() ) {
221             fprintf( stderr, "Could not initialize device manager\n" );
222             delete m_deviceManager;
223             return exitfunction(-1);
224         }
225         if ( arguments.verbose ) {
226             m_deviceManager->setVerboseLevel(arguments.verbose);
227         }
228         if ( !m_deviceManager->discover(arguments.use_cache) ) {
229             fprintf( stderr, "Could not discover devices\n" );
230             delete m_deviceManager;
231             return exitfunction(-1);
232         }
233         delete m_deviceManager;
234         return exitfunction(0);
235     } else if ( strcmp( arguments.args[0], "SetSamplerate" ) == 0 ) {
236         char* tail;
237         int samplerate = strtol( arguments.args[1], &tail, 0 );
238         if ( errno ) {
239             fprintf( stderr,  "Could not parse samplerate argument\n" );
240             return exitfunction(-1);
241         }
242
243         DeviceManager *m_deviceManager = new DeviceManager();
244         if ( !m_deviceManager ) {
245             fprintf( stderr, "Could not allocate device manager\n" );
246             return exitfunction(-1);
247         }
248         if ( arguments.verbose ) {
249             m_deviceManager->setVerboseLevel(arguments.verbose);
250         }
251         if ( !m_deviceManager->initialize() ) {
252             fprintf( stderr, "Could not initialize device manager\n" );
253             delete m_deviceManager;
254             return exitfunction(-1);
255         }
256         if ( arguments.verbose ) {
257             m_deviceManager->setVerboseLevel(arguments.verbose);
258         }
259         if ( !m_deviceManager->discover(arguments.use_cache) ) {
260             fprintf( stderr, "Could not discover devices\n" );
261             delete m_deviceManager;
262             return exitfunction(-1);
263         }
264
265         if(arguments.node_id_set) {
266             FFADODevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id );
267             if ( avDevice ) {
268                 avDevice->setVerboseLevel(arguments.verbose);
269                 if ( ! avDevice->setSamplingFrequency( samplerate ) ) {
270                     fprintf( stderr, "Could not set samplerate\n" );
271                 }
272             }
273         } else {
274             int i=0;
275
276             int devices_on_bus = m_deviceManager->getNbDevices();
277             printf("  port = %d, devices_on_bus = %d\n", arguments.port, devices_on_bus);
278
279             for(i=0;i<devices_on_bus;i++) {
280                 int node_id=m_deviceManager->getDeviceNodeId(i);
281                 printf("  set samplerate for device = %d, node = %d\n", i, node_id);
282                 FFADODevice* avDevice = m_deviceManager->getAvDevice( node_id );
283                 if ( avDevice ) {
284                     avDevice->setVerboseLevel(arguments.verbose);
285                     if ( !avDevice->setSamplingFrequency( samplerate ) ) {
286                         fprintf( stderr, "Could not set samplerate\n" );
287                     }
288                 }
289             }
290         }
291         delete m_deviceManager;
292         return exitfunction(0);
293     } else if ( strcmp( arguments.args[0], "SetClockSource" ) == 0 ) {
294         char* tail;
295         unsigned int targetid = (unsigned int)strtol( arguments.args[1], &tail, 0 );
296         if ( errno ) {
297             fprintf( stderr,  "Could not parse clock source argument\n" );
298             targetid=0xFFFF;
299         }
300         DeviceManager *m_deviceManager = new DeviceManager();
301         if ( !m_deviceManager ) {
302             fprintf( stderr, "Could not allocate device manager\n" );
303             return exitfunction(-1);
304         }
305         if ( arguments.verbose ) {
306             m_deviceManager->setVerboseLevel(arguments.verbose);
307         }
308         if ( !m_deviceManager->initialize() ) {
309             fprintf( stderr, "Could not initialize device manager\n" );
310             delete m_deviceManager;
311             return exitfunction(-1);
312         }
313         if ( arguments.verbose ) {
314             m_deviceManager->setVerboseLevel(arguments.verbose);
315         }
316         if ( !m_deviceManager->discover(arguments.use_cache) ) {
317             fprintf( stderr, "Could not discover devices\n" );
318             delete m_deviceManager;
319             return exitfunction(-1);
320         }
321
322         if(arguments.node_id_set) {
323             FFADODevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id );
324             if ( avDevice ) {
325                 FFADODevice::ClockSource s;
326            
327                 avDevice->setVerboseLevel(arguments.verbose);
328                 FFADODevice::ClockSourceVector sources=avDevice->getSupportedClockSources();
329                 for ( FFADODevice::ClockSourceVector::const_iterator it
330                         = sources.begin();
331                     it != sources.end();
332                     ++it )
333                 {
334                     FFADODevice::ClockSource c=*it;
335                     printf( " Type: %s, Id: %d, Valid: %d, Active: %d, Description: %s\n",
336                         FFADODevice::ClockSourceTypeToString(c.type), c.id, c.valid, c.active, c.description.c_str());
337                    
338                     if (c.id==targetid) {
339                         s=*it;
340                     }
341                 }
342                
343                 if (s.type != FFADODevice::eCT_Invalid) {
344                     printf("  set clock source to %d\n", s.id);
345                     if ( ! avDevice->setActiveClockSource( s ) ) {
346                         fprintf( stderr, "Could not set clock source\n" );
347                     }
348                 } else {
349                     printf("  no clock source with id %d found\n", targetid);
350                 }
351             }
352         } else {
353             fprintf( stderr, "please specify a node\n" );
354         }
355         delete m_deviceManager;
356         return exitfunction(0);
357     }
358 }
Note: See TracBrowser for help on using the browser.