1 |
/* |
---|
2 |
* Copyright (C) 2005-2007 by by Daniel Wagner |
---|
3 |
* Copyright (C) 2005-2007 by 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 |
* FFADO 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) any later version. |
---|
14 |
* FFADO is distributed in the hope that it will be useful, |
---|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 |
* GNU General Public License for more details. |
---|
18 |
* |
---|
19 |
* You should have received a copy of the GNU General Public License |
---|
20 |
* along with FFADO; if not, write to the Free Software |
---|
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
22 |
* MA 02111-1307 USA. |
---|
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 |
short verbose; |
---|
72 |
int port; |
---|
73 |
int node_id; |
---|
74 |
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 |
setDebugLevel(arguments.verbose); |
---|
188 |
|
---|
189 |
// Parse our arguments; every option seen by `parse_opt' will |
---|
190 |
// be reflected in `arguments'. |
---|
191 |
if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) { |
---|
192 |
fprintf( stderr, "Could not parse command line\n" ); |
---|
193 |
return exitfunction(-1); |
---|
194 |
} |
---|
195 |
|
---|
196 |
printf("verbose level = %d\n", arguments.verbose); |
---|
197 |
|
---|
198 |
printf( "Using ffado library version: %s\n\n", ffado_get_version() ); |
---|
199 |
|
---|
200 |
if ( strcmp( arguments.args[0], "Discover" ) == 0 ) { |
---|
201 |
DeviceManager *m_deviceManager = new DeviceManager(); |
---|
202 |
if ( !m_deviceManager ) { |
---|
203 |
fprintf( stderr, "Could not allocate device manager\n" ); |
---|
204 |
return exitfunction(-1); |
---|
205 |
} |
---|
206 |
if ( arguments.verbose ) { |
---|
207 |
m_deviceManager->setVerboseLevel(arguments.verbose); |
---|
208 |
} |
---|
209 |
if ( !m_deviceManager->initialize( arguments.port ) ) { |
---|
210 |
fprintf( stderr, "Could not initialize device manager\n" ); |
---|
211 |
delete m_deviceManager; |
---|
212 |
return exitfunction(-1); |
---|
213 |
} |
---|
214 |
if ( arguments.verbose ) { |
---|
215 |
m_deviceManager->setVerboseLevel(arguments.verbose); |
---|
216 |
} |
---|
217 |
if ( !m_deviceManager->discover() ) { |
---|
218 |
fprintf( stderr, "Could not discover devices\n" ); |
---|
219 |
delete m_deviceManager; |
---|
220 |
return exitfunction(-1); |
---|
221 |
} |
---|
222 |
delete m_deviceManager; |
---|
223 |
return exitfunction(0); |
---|
224 |
} else if ( strcmp( arguments.args[0], "SetSamplerate" ) == 0 ) { |
---|
225 |
char* tail; |
---|
226 |
int samplerate = strtol( arguments.args[1], &tail, 0 ); |
---|
227 |
if ( errno ) { |
---|
228 |
fprintf( stderr, "Could not parse samplerate argument\n" ); |
---|
229 |
return exitfunction(-1); |
---|
230 |
} |
---|
231 |
|
---|
232 |
DeviceManager *m_deviceManager = new DeviceManager(); |
---|
233 |
if ( !m_deviceManager ) { |
---|
234 |
fprintf( stderr, "Could not allocate device manager\n" ); |
---|
235 |
return exitfunction(-1); |
---|
236 |
} |
---|
237 |
if ( arguments.verbose ) { |
---|
238 |
m_deviceManager->setVerboseLevel(arguments.verbose); |
---|
239 |
} |
---|
240 |
if ( !m_deviceManager->initialize( arguments.port ) ) { |
---|
241 |
fprintf( stderr, "Could not initialize device manager\n" ); |
---|
242 |
delete m_deviceManager; |
---|
243 |
return exitfunction(-1); |
---|
244 |
} |
---|
245 |
if ( arguments.verbose ) { |
---|
246 |
m_deviceManager->setVerboseLevel(arguments.verbose); |
---|
247 |
} |
---|
248 |
if ( !m_deviceManager->discover() ) { |
---|
249 |
fprintf( stderr, "Could not discover devices\n" ); |
---|
250 |
delete m_deviceManager; |
---|
251 |
return exitfunction(-1); |
---|
252 |
} |
---|
253 |
|
---|
254 |
if(arguments.node_id_set) { |
---|
255 |
FFADODevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id ); |
---|
256 |
if ( avDevice ) { |
---|
257 |
avDevice->setVerboseLevel(arguments.verbose); |
---|
258 |
if ( ! avDevice->setSamplingFrequency( samplerate ) ) { |
---|
259 |
fprintf( stderr, "Could not set samplerate\n" ); |
---|
260 |
} |
---|
261 |
} |
---|
262 |
} else { |
---|
263 |
int i=0; |
---|
264 |
|
---|
265 |
int devices_on_bus = m_deviceManager->getNbDevices(); |
---|
266 |
printf(" port = %d, devices_on_bus = %d\n", arguments.port, devices_on_bus); |
---|
267 |
|
---|
268 |
for(i=0;i<devices_on_bus;i++) { |
---|
269 |
int node_id=m_deviceManager->getDeviceNodeId(i); |
---|
270 |
printf(" set samplerate for device = %d, node = %d\n", i, node_id); |
---|
271 |
FFADODevice* avDevice = m_deviceManager->getAvDevice( node_id ); |
---|
272 |
if ( avDevice ) { |
---|
273 |
avDevice->setVerboseLevel(arguments.verbose); |
---|
274 |
if ( !avDevice->setSamplingFrequency( samplerate ) ) { |
---|
275 |
fprintf( stderr, "Could not set samplerate\n" ); |
---|
276 |
} |
---|
277 |
} |
---|
278 |
} |
---|
279 |
} |
---|
280 |
delete m_deviceManager; |
---|
281 |
return exitfunction(0); |
---|
282 |
} else if ( strcmp( arguments.args[0], "SetClockSource" ) == 0 ) { |
---|
283 |
char* tail; |
---|
284 |
unsigned int targetid = (unsigned int)strtol( arguments.args[1], &tail, 0 ); |
---|
285 |
if ( errno ) { |
---|
286 |
fprintf( stderr, "Could not parse clock source argument\n" ); |
---|
287 |
targetid=0xFFFF; |
---|
288 |
} |
---|
289 |
DeviceManager *m_deviceManager = new DeviceManager(); |
---|
290 |
if ( !m_deviceManager ) { |
---|
291 |
fprintf( stderr, "Could not allocate device manager\n" ); |
---|
292 |
return exitfunction(-1); |
---|
293 |
} |
---|
294 |
if ( arguments.verbose ) { |
---|
295 |
m_deviceManager->setVerboseLevel(arguments.verbose); |
---|
296 |
} |
---|
297 |
if ( !m_deviceManager->initialize( arguments.port ) ) { |
---|
298 |
fprintf( stderr, "Could not initialize device manager\n" ); |
---|
299 |
delete m_deviceManager; |
---|
300 |
return exitfunction(-1); |
---|
301 |
} |
---|
302 |
if ( arguments.verbose ) { |
---|
303 |
m_deviceManager->setVerboseLevel(arguments.verbose); |
---|
304 |
} |
---|
305 |
if ( !m_deviceManager->discover() ) { |
---|
306 |
fprintf( stderr, "Could not discover devices\n" ); |
---|
307 |
delete m_deviceManager; |
---|
308 |
return exitfunction(-1); |
---|
309 |
} |
---|
310 |
|
---|
311 |
if(arguments.node_id_set) { |
---|
312 |
FFADODevice* avDevice = m_deviceManager->getAvDevice( arguments.node_id ); |
---|
313 |
if ( avDevice ) { |
---|
314 |
FFADODevice::ClockSource s; |
---|
315 |
|
---|
316 |
avDevice->setVerboseLevel(arguments.verbose); |
---|
317 |
FFADODevice::ClockSourceVector sources=avDevice->getSupportedClockSources(); |
---|
318 |
for ( FFADODevice::ClockSourceVector::const_iterator it |
---|
319 |
= sources.begin(); |
---|
320 |
it != sources.end(); |
---|
321 |
++it ) |
---|
322 |
{ |
---|
323 |
FFADODevice::ClockSource c=*it; |
---|
324 |
printf( " Type: %s, Id: %d, Valid: %d, Active: %d, Description: %s\n", |
---|
325 |
FFADODevice::ClockSourceTypeToString(c.type), c.id, c.valid, c.active, c.description.c_str()); |
---|
326 |
|
---|
327 |
if (c.id==targetid) { |
---|
328 |
s=*it; |
---|
329 |
} |
---|
330 |
} |
---|
331 |
|
---|
332 |
if (s.type != FFADODevice::eCT_Invalid) { |
---|
333 |
printf(" set clock source to %d\n", s.id); |
---|
334 |
if ( ! avDevice->setActiveClockSource( s ) ) { |
---|
335 |
fprintf( stderr, "Could not set clock source\n" ); |
---|
336 |
} |
---|
337 |
} else { |
---|
338 |
printf(" no clock source with id %d found\n", targetid); |
---|
339 |
} |
---|
340 |
} |
---|
341 |
} else { |
---|
342 |
fprintf( stderr, "please specify a node\n" ); |
---|
343 |
} |
---|
344 |
delete m_deviceManager; |
---|
345 |
return exitfunction(0); |
---|
346 |
} |
---|
347 |
} |
---|