root/branches/libffado-2.0/tests/test-ffado.cpp

Revision 1722, 17.7 kB (checked in by ppalmers, 2 years ago)

fix format warnings for extreme debugging

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 #include "libieee1394/cycletimer.h"
51
52 using namespace std;
53
54 DECLARE_GLOBAL_DEBUG_MODULE;
55
56 #define MAX_ARGS 5
57
58 // global's
59 const char *argp_program_version = PACKAGE_STRING;
60 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
61
62 // Program documentation.
63 static char doc[] = "FFADO -- a driver for Firewire Audio devices (test application)\n\n"
64                     "OPERATION: Discover\n"
65                     "           SetSamplerate samplerate\n"
66                     "           SetClockSource [id]\n"
67                     "           BusReset\n"
68                     "           ListDevices\n"
69                     "           SetSplitTimeout timeout_usec\n"
70                     "           GetSplitTimeout\n"
71                     ;
72
73 // A description of the arguments we accept.
74 static char args_doc[] = "OPERATION";
75
76 struct arguments
77 {
78     unsigned int nargs;
79     short    silent;
80     long int verbose;
81     long int port;
82     long int use_cache;
83     long int node_id;
84     long int node_id_set;
85     const char* args[MAX_ARGS];
86 };
87
88 // The options we understand.
89 static struct argp_option options[] = {
90     {"quiet",    'q',       0,    0,  "Don't produce any output" },
91     {"silent",   's',       0,    OPTION_ALIAS },
92
93     {"verbose",  'v', "level",    0,  "Produce verbose output" },
94 #if ENABLE_DISCOVERY_CACHE
95     {"cache",    'c', "enable",   0,  "Use AVC model cache" },
96 #endif
97
98     {"node",     'n',    "id",    0,  "Node to use" },
99     {"port",     'p',    "nr",    0,  "IEEE1394 Port to use" },
100     { 0 }
101 };
102
103 //-------------------------------------------------------------
104
105 // Parse a single option.
106 static error_t
107 parse_opt( int key, char* arg, struct argp_state* state )
108 {
109     // Get the input argument from `argp_parse', which we
110     // know is a pointer to our arguments structure.
111     struct arguments* arguments = ( struct arguments* ) state->input;
112     char* tail;
113
114     errno = 0;
115     switch (key) {
116     case 'q': case 's':
117         arguments->silent = 1;
118         break;
119     case 'v':
120         if (arg) {
121             arguments->verbose = strtol( arg, &tail, 0 );
122             if ( errno ) {
123                 fprintf( stderr,  "Could not parse 'verbose' argument\n" );
124                 return ARGP_ERR_UNKNOWN;
125             }
126         }
127         break;
128     case 'c':
129         if (arg) {
130             arguments->use_cache = strtol( arg, &tail, 0 );
131             if ( errno ) {
132                 fprintf( stderr,  "Could not parse 'cache' argument\n" );
133                 return ARGP_ERR_UNKNOWN;
134             }
135         }
136         break;
137     case 'p':
138         if (arg) {
139             arguments->port = strtol( arg, &tail, 0 );
140             if ( errno ) {
141                 fprintf( stderr,  "Could not parse 'port' argument\n" );
142                 return ARGP_ERR_UNKNOWN;
143             }
144         } else {
145             if ( errno ) {
146                 fprintf( stderr, "Could not parse 'port' argumen\n" );
147                 return ARGP_ERR_UNKNOWN;
148             }
149         }
150         break;
151     case 'n':
152         if (arg) {
153             arguments->node_id = strtol( arg, &tail, 0 );
154             if ( errno ) {
155                 fprintf( stderr,  "Could not parse 'node' argument\n" );
156                 return ARGP_ERR_UNKNOWN;
157             }
158             arguments->node_id_set=1;
159         } else {
160             if ( errno ) {
161                 fprintf( stderr, "Could not parse 'node' argumen\n" );
162                 return ARGP_ERR_UNKNOWN;
163             }
164         }
165         break;
166     case ARGP_KEY_ARG:
167         if (state->arg_num >= MAX_ARGS) {
168             // Too many arguments.
169             argp_usage( state );
170         }
171         arguments->args[state->arg_num] = arg;
172         arguments->nargs++;
173         break;
174     case ARGP_KEY_END:
175         if (state->arg_num < 1) {
176         // Not enough arguments.
177         argp_usage( state );
178         }
179         break;
180     default:
181         return ARGP_ERR_UNKNOWN;
182     }
183     return 0;
184 }
185
186 // Our argp parser.
187 static struct argp argp = { options, parse_opt, args_doc, doc };
188
189 int exitfunction( int retval ) {
190     debugOutput( DEBUG_LEVEL_NORMAL, "Debug output flushed...\n" );
191     flushDebugOutput();
192
193     return retval;
194 }
195
196 void
197 printDeviceList(unsigned int port)
198 {
199     Ieee1394Service service;
200     // switch off all messages since they mess up the list
201     service.setVerboseLevel(0);
202     if ( !service.initialize( port ) ) {
203         printf("Could not initialize IEEE 1394 service on port %d\n", port);
204         exit(-1);
205     }
206
207     printf("=== 1394 PORT %d ===\n", port);
208     printf("  Node id  GUID                  VendorId     ModelId   Vendor - Model\n");
209     for (int i = 0; i < service.getNodeCount(); i++) {
210         ConfigRom crom(service, i);
211         if (!crom.initialize())
212             break;
213
214         printf("  %2d       0x%s  0x%08X  0x%08X   %s - %s\n",
215                i, crom.getGuidString().c_str(),
216                crom.getNodeVendorId(), crom.getModelId(),
217                crom.getVendorName().c_str(),
218                crom.getModelName().c_str());
219     }
220 }
221
222 void
223 busreset(unsigned int port)
224 {
225     Ieee1394Service service;
226     // switch off all messages since they mess up the list
227     service.setVerboseLevel(0);
228     if ( !service.initialize( port ) ) {
229         printf("Could not initialize IEEE 1394 service on port %d\n", port);
230         exit(-1);
231     }
232
233     printf("Doing busreset on port %d\n", port);
234     service.doBusReset();
235 }
236
237 int
238 main( int argc, char **argv )
239 {
240     struct arguments arguments;
241
242     printf("-----------------------------------------------\n");
243     printf("FFADO test and diagnostic utility\n");
244     printf("Part of the FFADO project -- www.ffado.org\n");
245     printf("Version: %s\n", PACKAGE_VERSION);
246     printf("(C) 2008, Daniel Wagner, Pieter Palmers\n");
247     printf("This program comes with ABSOLUTELY NO WARRANTY.\n");
248     printf("-----------------------------------------------\n\n");
249
250     // check the library version
251     const char *libversion = ffado_get_version();
252     const char *progversion = PACKAGE_STRING;
253     if(strcmp(libversion, progversion) != 0) {
254         printf("Library version mismatch. (required: %s, present: %s)\n", progversion, libversion);
255         printf("Please run this application against the exact corresponding library\n");
256         printf("it was compiled for. The most common cause for this is having more\n");
257         printf("than one version of libffado installed.\n\n");
258         return exitfunction(-1);
259     }
260
261     // Default values.
262     arguments.nargs       = 0;
263     arguments.silent      = 0;
264     arguments.verbose     = 0;
265     arguments.use_cache   = 1;
266     arguments.port        = 0;
267     arguments.node_id     = 0;
268     arguments.node_id_set = 0; // if we don't specify a node, discover all
269     arguments.args[0]     = "";
270     arguments.args[1]     = "";
271
272     // Parse our arguments; every option seen by `parse_opt' will
273     // be reflected in `arguments'.
274     if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) {
275         fprintf( stderr, "Could not parse command line\n" );
276         return exitfunction(-1);
277     }
278     setDebugLevel(arguments.verbose);
279
280     if ( strcmp( arguments.args[0], "Discover" ) == 0 ) {
281         DeviceManager *m_deviceManager = new DeviceManager();
282         if ( !m_deviceManager ) {
283             fprintf( stderr, "Could not allocate device manager\n" );
284             return exitfunction(-1);
285         }
286         if ( arguments.verbose ) {
287             m_deviceManager->setVerboseLevel(arguments.verbose);
288         }
289         if ( !m_deviceManager->initialize() ) {
290             fprintf( stderr, "Could not initialize device manager\n" );
291             delete m_deviceManager;
292             return exitfunction(-1);
293         }
294         if ( arguments.verbose ) {
295             m_deviceManager->setVerboseLevel(arguments.verbose);
296         }
297         if ( !m_deviceManager->discover(arguments.use_cache) ) {
298             fprintf( stderr, "Could not discover devices\n" );
299             delete m_deviceManager;
300             return exitfunction(-1);
301         }
302         delete m_deviceManager;
303         return exitfunction(0);
304     } else if ( strcmp( arguments.args[0], "BusReset" ) == 0 ) {
305         busreset(arguments.port);
306     } else if ( strcmp( arguments.args[0], "ListDevices" ) == 0 ) {
307         unsigned int nb_ports = Ieee1394Service::detectNbPorts();
308         for (unsigned int i=0;i<nb_ports;i++) {
309             printDeviceList(i);
310         }
311     } else if ( strcmp( arguments.args[0], "SetSamplerate" ) == 0 ) {
312         char* tail;
313         int samplerate = strtol( arguments.args[1], &tail, 0 );
314         if ( errno ) {
315             fprintf( stderr,  "Could not parse samplerate argument\n" );
316             return exitfunction(-1);
317         }
318
319         DeviceManager *m_deviceManager = new DeviceManager();
320         if ( !m_deviceManager ) {
321             fprintf( stderr, "Could not allocate device manager\n" );
322             return exitfunction(-1);
323         }
324         if ( arguments.verbose ) {
325             m_deviceManager->setVerboseLevel(arguments.verbose);
326         }
327         if ( !m_deviceManager->initialize() ) {
328             fprintf( stderr, "Could not initialize device manager\n" );
329             delete m_deviceManager;
330             return exitfunction(-1);
331         }
332         if ( arguments.verbose ) {
333             m_deviceManager->setVerboseLevel(arguments.verbose);
334         }
335         if ( !m_deviceManager->discover(arguments.use_cache) ) {
336             fprintf( stderr, "Could not discover devices\n" );
337             delete m_deviceManager;
338             return exitfunction(-1);
339         }
340
341         if(arguments.node_id_set) {
342             FFADODevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id );
343             if ( avDevice ) {
344                 avDevice->setVerboseLevel(arguments.verbose);
345                 if ( ! avDevice->setSamplingFrequency( samplerate ) ) {
346                     fprintf( stderr, "Could not set samplerate\n" );
347                 }
348             }
349         } else {
350             int i=0;
351
352             int devices_on_bus = m_deviceManager->getNbDevices();
353             printf("  port = %d, devices_on_bus = %d\n", (int)arguments.port, devices_on_bus);
354
355             for(i=0;i<devices_on_bus;i++) {
356                 int node_id=m_deviceManager->getDeviceNodeId(i);
357                 printf("  set samplerate for device = %d, node = %d\n", i, node_id);
358                 FFADODevice* avDevice = m_deviceManager->getAvDevice( node_id );
359                 if ( avDevice ) {
360                     avDevice->setVerboseLevel(arguments.verbose);
361                     if ( !avDevice->setSamplingFrequency( samplerate ) ) {
362                         fprintf( stderr, "Could not set samplerate\n" );
363                     }
364                 }
365             }
366         }
367         delete m_deviceManager;
368         return exitfunction(0);
369     } else if ( strcmp( arguments.args[0], "SetClockSource" ) == 0 ) {
370         char* tail;
371         unsigned int targetid = (unsigned int)strtol( arguments.args[1], &tail, 0 );
372         if ( errno ) {
373             fprintf( stderr,  "Could not parse clock source argument\n" );
374             targetid=0xFFFF;
375         }
376         DeviceManager *m_deviceManager = new DeviceManager();
377         if ( !m_deviceManager ) {
378             fprintf( stderr, "Could not allocate device manager\n" );
379             return exitfunction(-1);
380         }
381         if ( arguments.verbose ) {
382             m_deviceManager->setVerboseLevel(arguments.verbose);
383         }
384         if ( !m_deviceManager->initialize() ) {
385             fprintf( stderr, "Could not initialize device manager\n" );
386             delete m_deviceManager;
387             return exitfunction(-1);
388         }
389         if ( arguments.verbose ) {
390             m_deviceManager->setVerboseLevel(arguments.verbose);
391         }
392         if ( !m_deviceManager->discover(arguments.use_cache) ) {
393             fprintf( stderr, "Could not discover devices\n" );
394             delete m_deviceManager;
395             return exitfunction(-1);
396         }
397
398         if(arguments.node_id_set) {
399             FFADODevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id );
400             if ( avDevice ) {
401                 FFADODevice::ClockSource s;
402            
403                 avDevice->setVerboseLevel(arguments.verbose);
404                 FFADODevice::ClockSourceVector sources=avDevice->getSupportedClockSources();
405                 for ( FFADODevice::ClockSourceVector::const_iterator it
406                         = sources.begin();
407                     it != sources.end();
408                     ++it )
409                 {
410                     FFADODevice::ClockSource c=*it;
411                     printf( " Type: %s, Id: %d, Valid: %d, Active: %d, Description: %s\n",
412                         FFADODevice::ClockSourceTypeToString(c.type), c.id, c.valid, c.active, c.description.c_str());
413                    
414                     if (c.id==targetid) {
415                         s=*it;
416                     }
417                 }
418                
419                 if (s.type != FFADODevice::eCT_Invalid) {
420                     printf("  set clock source to %d\n", s.id);
421                     if ( ! avDevice->setActiveClockSource( s ) ) {
422                         fprintf( stderr, "Could not set clock source\n" );
423                     }
424                 } else {
425                     printf("  no clock source with id %d found\n", targetid);
426                 }
427             }
428         } else {
429             fprintf( stderr, "please specify a node\n" );
430         }
431         delete m_deviceManager;
432         return exitfunction(0);
433     } else if ( strcmp( arguments.args[0], "SetSplitTimeout" ) == 0 ) {
434         char* tail;
435         int usecs = strtol( arguments.args[1], &tail, 0 );
436         if ( errno ) {
437             fprintf( stderr,  "Could not parse timeout argument\n" );
438             return exitfunction(-1);
439         }
440
441         Ieee1394Service service;
442         // switch off all messages since they mess up the list
443         service.setVerboseLevel(arguments.verbose);
444         if ( !service.initialize( arguments.port ) ) {
445             printf("Could not initialize IEEE 1394 service on port %d\n", (int)arguments.port);
446             return exitfunction(-1);
447         }
448
449         nodeid_t nodeid;
450         if(arguments.node_id_set) {
451             nodeid = arguments.node_id;
452         } else {
453             nodeid = service.getLocalNodeId();
454         }
455        
456         if (!service.setSplitTimeoutUsecs(nodeid, usecs)) {
457             printf("Failed to set SPLIT_TIMEOUT to %u for node %X on port %d\n",
458                    usecs, nodeid, (int)arguments.port);
459             return exitfunction(-1);
460         }
461
462         return exitfunction(0);
463     } else if ( strcmp( arguments.args[0], "GetSplitTimeout" ) == 0 ) {
464         Ieee1394Service service;
465         // switch off all messages since they mess up the list
466         service.setVerboseLevel(arguments.verbose);
467         if ( !service.initialize( arguments.port ) ) {
468             printf("Could not initialize IEEE 1394 service on port %d\n", (int)arguments.port);
469             return exitfunction(-1);
470         }
471
472         nodeid_t nodeid;
473         if(arguments.node_id_set) {
474             nodeid = arguments.node_id;
475         } else {
476             nodeid = service.getLocalNodeId();
477         }
478         int usecs = service.getSplitTimeoutUsecs(nodeid);
479         if (usecs < 0) {
480             printf("Failed to get SPLIT_TIMEOUT for node %X on port %d\n",
481                    nodeid, (int)arguments.port);
482             return exitfunction(-1);
483         }
484         printf("SPLIT_TIMEOUT for node %X on port %d is %u\n",
485                nodeid, (int)arguments.port, usecs);
486
487         return exitfunction(0);
488     } else if ( strcmp( arguments.args[0], "SytCalcTest" ) == 0 ) {
489         if (arguments.nargs < 4) {
490             fprintf( stderr,"Not enough arguments\n");
491             return -1;
492         }
493         uint64_t syt_timestamp = strtol(arguments.args[1], NULL, 0);
494         if (errno) {
495             fprintf( stderr,"syt_timestamp parsing failed: %s\n",
496                      strerror(errno));
497             return errno;
498         }
499         uint32_t rcv_cycle = strtol(arguments.args[2], NULL, 0);
500         if (errno) {
501             fprintf( stderr,"rcv_cycle parsing failed: %s\n",
502                      strerror(errno));
503             return errno;
504         }
505         uint64_t ctr_now = strtoll(arguments.args[3], NULL, 0);
506         if (errno) {
507             fprintf( stderr,"ctr_now parsing failed: %s\n",
508                      strerror(errno));
509             return errno;
510         }
511         uint64_t result_rcv = sytRecvToFullTicks(syt_timestamp, rcv_cycle, ctr_now);
512         uint64_t result_xmt = sytXmitToFullTicks(syt_timestamp, rcv_cycle, ctr_now);
513         printf("RCV: 0x%010"PRIX64" %010"PRIu64"  XMT: 0x%010"PRIX64" %010"PRIu64" CTR: %010"PRIu64"\n",
514                result_rcv, result_rcv, result_xmt, result_xmt, CYCLE_TIMER_TO_TICKS(ctr_now));
515
516     } else {
517         fprintf( stderr, "please specify a command\n" );
518     }
519 }
Note: See TracBrowser for help on using the browser.