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

Revision 1371, 16.8 kB (checked in by ppalmers, 15 years ago)

* implement our own code to do FCP transactions. the code from libavc had too much side-effects.
* remove libavc1394 as a dependency
* set the SPLIT_TIMEOUT value for the host controller such that late responses by the DM1x00 based devices are not discarded. Should fix the issues with FA-101 discovery. (re:
#155, #162)

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     // Default values.
243     arguments.nargs       = 0;
244     arguments.silent      = 0;
245     arguments.verbose     = 0;
246     arguments.use_cache   = 1;
247     arguments.port        = 0;
248     arguments.node_id     = 0;
249     arguments.node_id_set = 0; // if we don't specify a node, discover all
250     arguments.args[0]     = "";
251     arguments.args[1]     = "";
252
253     // Parse our arguments; every option seen by `parse_opt' will
254     // be reflected in `arguments'.
255     if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) {
256         fprintf( stderr, "Could not parse command line\n" );
257         return exitfunction(-1);
258     }
259
260     printf("verbose level = %ld\n", arguments.verbose);
261     setDebugLevel(arguments.verbose);
262
263     printf( "Using ffado library version: %s\n\n", ffado_get_version() );
264
265     if ( strcmp( arguments.args[0], "Discover" ) == 0 ) {
266         DeviceManager *m_deviceManager = new DeviceManager();
267         if ( !m_deviceManager ) {
268             fprintf( stderr, "Could not allocate device manager\n" );
269             return exitfunction(-1);
270         }
271         if ( arguments.verbose ) {
272             m_deviceManager->setVerboseLevel(arguments.verbose);
273         }
274         if ( !m_deviceManager->initialize() ) {
275             fprintf( stderr, "Could not initialize device manager\n" );
276             delete m_deviceManager;
277             return exitfunction(-1);
278         }
279         if ( arguments.verbose ) {
280             m_deviceManager->setVerboseLevel(arguments.verbose);
281         }
282         if ( !m_deviceManager->discover(arguments.use_cache) ) {
283             fprintf( stderr, "Could not discover devices\n" );
284             delete m_deviceManager;
285             return exitfunction(-1);
286         }
287         delete m_deviceManager;
288         return exitfunction(0);
289     } else if ( strcmp( arguments.args[0], "BusReset" ) == 0 ) {
290         busreset(arguments.port);
291     } else if ( strcmp( arguments.args[0], "ListDevices" ) == 0 ) {
292         unsigned int nb_ports = Ieee1394Service::detectNbPorts();
293         for (unsigned int i=0;i<nb_ports;i++) {
294             printDeviceList(i);
295         }
296     } else if ( strcmp( arguments.args[0], "SetSamplerate" ) == 0 ) {
297         char* tail;
298         int samplerate = strtol( arguments.args[1], &tail, 0 );
299         if ( errno ) {
300             fprintf( stderr,  "Could not parse samplerate argument\n" );
301             return exitfunction(-1);
302         }
303
304         DeviceManager *m_deviceManager = new DeviceManager();
305         if ( !m_deviceManager ) {
306             fprintf( stderr, "Could not allocate device manager\n" );
307             return exitfunction(-1);
308         }
309         if ( arguments.verbose ) {
310             m_deviceManager->setVerboseLevel(arguments.verbose);
311         }
312         if ( !m_deviceManager->initialize() ) {
313             fprintf( stderr, "Could not initialize device manager\n" );
314             delete m_deviceManager;
315             return exitfunction(-1);
316         }
317         if ( arguments.verbose ) {
318             m_deviceManager->setVerboseLevel(arguments.verbose);
319         }
320         if ( !m_deviceManager->discover(arguments.use_cache) ) {
321             fprintf( stderr, "Could not discover devices\n" );
322             delete m_deviceManager;
323             return exitfunction(-1);
324         }
325
326         if(arguments.node_id_set) {
327             FFADODevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id );
328             if ( avDevice ) {
329                 avDevice->setVerboseLevel(arguments.verbose);
330                 if ( ! avDevice->setSamplingFrequency( samplerate ) ) {
331                     fprintf( stderr, "Could not set samplerate\n" );
332                 }
333             }
334         } else {
335             int i=0;
336
337             int devices_on_bus = m_deviceManager->getNbDevices();
338             printf("  port = %ld, devices_on_bus = %d\n", arguments.port, devices_on_bus);
339
340             for(i=0;i<devices_on_bus;i++) {
341                 int node_id=m_deviceManager->getDeviceNodeId(i);
342                 printf("  set samplerate for device = %d, node = %d\n", i, node_id);
343                 FFADODevice* avDevice = m_deviceManager->getAvDevice( node_id );
344                 if ( avDevice ) {
345                     avDevice->setVerboseLevel(arguments.verbose);
346                     if ( !avDevice->setSamplingFrequency( samplerate ) ) {
347                         fprintf( stderr, "Could not set samplerate\n" );
348                     }
349                 }
350             }
351         }
352         delete m_deviceManager;
353         return exitfunction(0);
354     } else if ( strcmp( arguments.args[0], "SetClockSource" ) == 0 ) {
355         char* tail;
356         unsigned int targetid = (unsigned int)strtol( arguments.args[1], &tail, 0 );
357         if ( errno ) {
358             fprintf( stderr,  "Could not parse clock source argument\n" );
359             targetid=0xFFFF;
360         }
361         DeviceManager *m_deviceManager = new DeviceManager();
362         if ( !m_deviceManager ) {
363             fprintf( stderr, "Could not allocate device manager\n" );
364             return exitfunction(-1);
365         }
366         if ( arguments.verbose ) {
367             m_deviceManager->setVerboseLevel(arguments.verbose);
368         }
369         if ( !m_deviceManager->initialize() ) {
370             fprintf( stderr, "Could not initialize device manager\n" );
371             delete m_deviceManager;
372             return exitfunction(-1);
373         }
374         if ( arguments.verbose ) {
375             m_deviceManager->setVerboseLevel(arguments.verbose);
376         }
377         if ( !m_deviceManager->discover(arguments.use_cache) ) {
378             fprintf( stderr, "Could not discover devices\n" );
379             delete m_deviceManager;
380             return exitfunction(-1);
381         }
382
383         if(arguments.node_id_set) {
384             FFADODevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id );
385             if ( avDevice ) {
386                 FFADODevice::ClockSource s;
387            
388                 avDevice->setVerboseLevel(arguments.verbose);
389                 FFADODevice::ClockSourceVector sources=avDevice->getSupportedClockSources();
390                 for ( FFADODevice::ClockSourceVector::const_iterator it
391                         = sources.begin();
392                     it != sources.end();
393                     ++it )
394                 {
395                     FFADODevice::ClockSource c=*it;
396                     printf( " Type: %s, Id: %d, Valid: %d, Active: %d, Description: %s\n",
397                         FFADODevice::ClockSourceTypeToString(c.type), c.id, c.valid, c.active, c.description.c_str());
398                    
399                     if (c.id==targetid) {
400                         s=*it;
401                     }
402                 }
403                
404                 if (s.type != FFADODevice::eCT_Invalid) {
405                     printf("  set clock source to %d\n", s.id);
406                     if ( ! avDevice->setActiveClockSource( s ) ) {
407                         fprintf( stderr, "Could not set clock source\n" );
408                     }
409                 } else {
410                     printf("  no clock source with id %d found\n", targetid);
411                 }
412             }
413         } else {
414             fprintf( stderr, "please specify a node\n" );
415         }
416         delete m_deviceManager;
417         return exitfunction(0);
418     } else if ( strcmp( arguments.args[0], "SetSplitTimeout" ) == 0 ) {
419         char* tail;
420         int usecs = strtol( arguments.args[1], &tail, 0 );
421         if ( errno ) {
422             fprintf( stderr,  "Could not parse timeout argument\n" );
423             return exitfunction(-1);
424         }
425
426         Ieee1394Service service;
427         // switch off all messages since they mess up the list
428         service.setVerboseLevel(arguments.verbose);
429         if ( !service.initialize( arguments.port ) ) {
430             printf("Could not initialize IEEE 1394 service on port %ld\n", arguments.port);
431             return exitfunction(-1);
432         }
433
434         nodeid_t nodeid;
435         if(arguments.node_id_set) {
436             nodeid = arguments.node_id;
437         } else {
438             nodeid = service.getLocalNodeId();
439         }
440        
441         if (!service.setSplitTimeoutUsecs(nodeid, usecs)) {
442             printf("Failed to set SPLIT_TIMEOUT to %u for node %X on port %ld\n",
443                    usecs, nodeid, arguments.port);
444             return exitfunction(-1);
445         }
446
447         return exitfunction(0);
448     } else if ( strcmp( arguments.args[0], "GetSplitTimeout" ) == 0 ) {
449         Ieee1394Service service;
450         // switch off all messages since they mess up the list
451         service.setVerboseLevel(arguments.verbose);
452         if ( !service.initialize( arguments.port ) ) {
453             printf("Could not initialize IEEE 1394 service on port %ld\n", arguments.port);
454             return exitfunction(-1);
455         }
456
457         nodeid_t nodeid;
458         if(arguments.node_id_set) {
459             nodeid = arguments.node_id;
460         } else {
461             nodeid = service.getLocalNodeId();
462         }
463         int usecs = service.getSplitTimeoutUsecs(nodeid);
464         if (usecs < 0) {
465             printf("Failed to get SPLIT_TIMEOUT for node %X on port %ld\n",
466                    nodeid, arguments.port);
467             return exitfunction(-1);
468         }
469         printf("SPLIT_TIMEOUT for node %X on port %ld is %u\n",
470                nodeid, arguments.port, usecs);
471
472         return exitfunction(0);
473     } else if ( strcmp( arguments.args[0], "SytCalcTest" ) == 0 ) {
474         if (arguments.nargs < 4) {
475             fprintf( stderr,"Not enough arguments\n");
476             return -1;
477         }
478         uint64_t syt_timestamp = strtol(arguments.args[1], NULL, 0);
479         if (errno) {
480             fprintf( stderr,"syt_timestamp parsing failed: %s\n",
481                      strerror(errno));
482             return errno;
483         }
484         uint32_t rcv_cycle = strtol(arguments.args[2], NULL, 0);
485         if (errno) {
486             fprintf( stderr,"rcv_cycle parsing failed: %s\n",
487                      strerror(errno));
488             return errno;
489         }
490         uint64_t ctr_now = strtoll(arguments.args[3], NULL, 0);
491         if (errno) {
492             fprintf( stderr,"ctr_now parsing failed: %s\n",
493                      strerror(errno));
494             return errno;
495         }
496         uint64_t result_rcv = sytRecvToFullTicks(syt_timestamp, rcv_cycle, ctr_now);
497         uint64_t result_xmt = sytXmitToFullTicks(syt_timestamp, rcv_cycle, ctr_now);
498         printf("RCV: 0x%010llX %010llu  XMT: 0x%010llX %010llu CTR: %010llu\n",
499                result_rcv, result_rcv, result_xmt, result_xmt, CYCLE_TIMER_TO_TICKS(ctr_now));
500
501     } else {
502         fprintf( stderr, "please specify a command\n" );
503     }
504 }
Note: See TracBrowser for help on using the browser.