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 <dbus-c++/dbus.h> |
---|
39 |
#include "controlserver.h" |
---|
40 |
#include "libcontrol/BasicElements.h" |
---|
41 |
|
---|
42 |
#include <signal.h> |
---|
43 |
|
---|
44 |
#include <argp.h> |
---|
45 |
#include <stdlib.h> |
---|
46 |
#include <stdio.h> |
---|
47 |
#include <string.h> |
---|
48 |
|
---|
49 |
#include <vector> |
---|
50 |
#include <string> |
---|
51 |
#include <iostream> |
---|
52 |
#include <sstream> |
---|
53 |
|
---|
54 |
using namespace std; |
---|
55 |
|
---|
56 |
DECLARE_GLOBAL_DEBUG_MODULE; |
---|
57 |
|
---|
58 |
// DBUS stuff |
---|
59 |
DBus::BusDispatcher dispatcher; |
---|
60 |
|
---|
61 |
// signal handler |
---|
62 |
int run=1; |
---|
63 |
static void sighandler (int sig) |
---|
64 |
{ |
---|
65 |
run = 0; |
---|
66 |
dispatcher.leave(); |
---|
67 |
} |
---|
68 |
|
---|
69 |
// global's |
---|
70 |
const char *argp_program_version = PACKAGE_STRING; |
---|
71 |
const char *argp_program_bug_address = PACKAGE_BUGREPORT; |
---|
72 |
|
---|
73 |
// Program documentation. |
---|
74 |
static char doc[] = "ffado-dbus-server -- expose the mixer features of connected FFADO devices through DBus\n\n" |
---|
75 |
; |
---|
76 |
|
---|
77 |
// A description of the arguments we accept. |
---|
78 |
static char args_doc[] = ""; |
---|
79 |
|
---|
80 |
struct arguments |
---|
81 |
{ |
---|
82 |
short silent; |
---|
83 |
short verbose; |
---|
84 |
int use_cache; |
---|
85 |
int port; |
---|
86 |
int node_id; |
---|
87 |
int node_id_set; |
---|
88 |
char* args[2]; |
---|
89 |
}; |
---|
90 |
|
---|
91 |
// The options we understand. |
---|
92 |
static struct argp_option options[] = { |
---|
93 |
{"quiet", 'q', 0, 0, "Don't produce any output" }, |
---|
94 |
{"silent", 's', 0, OPTION_ALIAS }, |
---|
95 |
|
---|
96 |
{"verbose", 'v', "level", 0, "Produce verbose output" }, |
---|
97 |
{"cache", 'c', "enable", 0, "Use AVC model cache (default=disabled)" }, |
---|
98 |
|
---|
99 |
|
---|
100 |
{"node", 'n', "id", 0, "Only expose mixer of a device on a specific node" }, |
---|
101 |
{"port", 'p', "nr", 0, "IEEE1394 Port to use" }, |
---|
102 |
{ 0 } |
---|
103 |
}; |
---|
104 |
|
---|
105 |
//------------------------------------------------------------- |
---|
106 |
|
---|
107 |
// Parse a single option. |
---|
108 |
static error_t |
---|
109 |
parse_opt( int key, char* arg, struct argp_state* state ) |
---|
110 |
{ |
---|
111 |
// Get the input argument from `argp_parse', which we |
---|
112 |
// know is a pointer to our arguments structure. |
---|
113 |
struct arguments* arguments = ( struct arguments* ) state->input; |
---|
114 |
char* tail; |
---|
115 |
|
---|
116 |
switch (key) { |
---|
117 |
case 'q': case 's': |
---|
118 |
arguments->silent = 1; |
---|
119 |
break; |
---|
120 |
case 'v': |
---|
121 |
if (arg) { |
---|
122 |
arguments->verbose = strtol( arg, &tail, 0 ); |
---|
123 |
if ( errno ) { |
---|
124 |
debugError( "Could not parse 'verbose' argument\n" ); |
---|
125 |
return ARGP_ERR_UNKNOWN; |
---|
126 |
} |
---|
127 |
} |
---|
128 |
break; |
---|
129 |
case 'c': |
---|
130 |
if (arg) { |
---|
131 |
arguments->use_cache = strtol( arg, &tail, 0 ); |
---|
132 |
if ( errno ) { |
---|
133 |
fprintf( stderr, "Could not parse 'cache' argument\n" ); |
---|
134 |
return ARGP_ERR_UNKNOWN; |
---|
135 |
} |
---|
136 |
} |
---|
137 |
break; |
---|
138 |
case 'p': |
---|
139 |
if (arg) { |
---|
140 |
arguments->port = strtol( arg, &tail, 0 ); |
---|
141 |
if ( errno ) { |
---|
142 |
debugError( "Could not parse 'port' argument\n" ); |
---|
143 |
return ARGP_ERR_UNKNOWN; |
---|
144 |
} |
---|
145 |
} else { |
---|
146 |
if ( errno ) { |
---|
147 |
debugError("Could not parse 'port' argumen\n" ); |
---|
148 |
return ARGP_ERR_UNKNOWN; |
---|
149 |
} |
---|
150 |
} |
---|
151 |
break; |
---|
152 |
case 'n': |
---|
153 |
if (arg) { |
---|
154 |
arguments->node_id = strtol( arg, &tail, 0 ); |
---|
155 |
if ( errno ) { |
---|
156 |
debugError( "Could not parse 'node' argument\n" ); |
---|
157 |
return ARGP_ERR_UNKNOWN; |
---|
158 |
} |
---|
159 |
arguments->node_id_set=1; |
---|
160 |
} else { |
---|
161 |
if ( errno ) { |
---|
162 |
debugError("Could not parse 'node' argumen\n" ); |
---|
163 |
return ARGP_ERR_UNKNOWN; |
---|
164 |
} |
---|
165 |
} |
---|
166 |
break; |
---|
167 |
case ARGP_KEY_ARG: |
---|
168 |
if (state->arg_num >= 1) { |
---|
169 |
// Too many arguments. |
---|
170 |
argp_usage( state ); |
---|
171 |
} |
---|
172 |
arguments->args[state->arg_num] = arg; |
---|
173 |
break; |
---|
174 |
case ARGP_KEY_END: |
---|
175 |
if (state->arg_num < 0) { |
---|
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 |
return retval; |
---|
193 |
} |
---|
194 |
|
---|
195 |
int |
---|
196 |
main( int argc, char **argv ) |
---|
197 |
{ |
---|
198 |
struct arguments arguments; |
---|
199 |
|
---|
200 |
// Default values. |
---|
201 |
arguments.silent = 0; |
---|
202 |
arguments.verbose = 0; |
---|
203 |
arguments.use_cache = 0; |
---|
204 |
arguments.port = 0; |
---|
205 |
arguments.node_id = 0; |
---|
206 |
arguments.node_id_set = 0; // if we don't specify a node, discover all |
---|
207 |
arguments.args[0] = ""; |
---|
208 |
arguments.args[1] = ""; |
---|
209 |
|
---|
210 |
setDebugLevel(arguments.verbose); |
---|
211 |
|
---|
212 |
// Parse our arguments; every option seen by `parse_opt' will |
---|
213 |
// be reflected in `arguments'. |
---|
214 |
if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) { |
---|
215 |
debugError("Could not parse command line\n" ); |
---|
216 |
return exitfunction(-1); |
---|
217 |
} |
---|
218 |
|
---|
219 |
debugOutput( DEBUG_LEVEL_NORMAL, "verbose level = %d\n", arguments.verbose); |
---|
220 |
debugOutput( DEBUG_LEVEL_NORMAL, "Using ffado library version: %s\n\n", ffado_get_version() ); |
---|
221 |
|
---|
222 |
DeviceManager *m_deviceManager = new DeviceManager(); |
---|
223 |
if ( !m_deviceManager ) { |
---|
224 |
debugError("Could not allocate device manager\n" ); |
---|
225 |
return exitfunction(-1); |
---|
226 |
} |
---|
227 |
if ( !m_deviceManager->initialize() ) { |
---|
228 |
debugError("Could not initialize device manager\n" ); |
---|
229 |
delete m_deviceManager; |
---|
230 |
return exitfunction(-1); |
---|
231 |
} |
---|
232 |
if ( arguments.verbose ) { |
---|
233 |
m_deviceManager->setVerboseLevel(arguments.verbose); |
---|
234 |
} |
---|
235 |
if ( !m_deviceManager->discover(arguments.use_cache) ) { |
---|
236 |
debugError("Could not discover devices\n" ); |
---|
237 |
delete m_deviceManager; |
---|
238 |
return exitfunction(-1); |
---|
239 |
} |
---|
240 |
|
---|
241 |
signal (SIGINT, sighandler); |
---|
242 |
|
---|
243 |
DBus::_init_threading(); |
---|
244 |
|
---|
245 |
// test DBUS stuff |
---|
246 |
DBus::default_dispatcher = &dispatcher; |
---|
247 |
|
---|
248 |
DBus::Connection conn = DBus::Connection::SessionBus(); |
---|
249 |
conn.request_name("org.ffado.Control"); |
---|
250 |
|
---|
251 |
DBusControl::Container *container |
---|
252 |
= new DBusControl::Container(conn, "/org/ffado/Control/DeviceManager", *m_deviceManager); |
---|
253 |
|
---|
254 |
printMessage("DBUS test service running\n"); |
---|
255 |
printMessage("press ctrl-c to stop it & continue\n"); |
---|
256 |
|
---|
257 |
dispatcher.enter(); |
---|
258 |
|
---|
259 |
delete container; |
---|
260 |
|
---|
261 |
signal (SIGINT, SIG_DFL); |
---|
262 |
|
---|
263 |
printMessage("server stopped\n"); |
---|
264 |
delete m_deviceManager; |
---|
265 |
return exitfunction(0); |
---|
266 |
} |
---|