1 |
/* |
---|
2 |
* Copyright (C) 2005-2008 by Pieter Palmers |
---|
3 |
* Copyright (C) 2005-2008 by Daniel Wagner |
---|
4 |
* Copyright (C) 2012 Bent Bisballe Nyeng |
---|
5 |
* |
---|
6 |
* This file is part of FFADO |
---|
7 |
* FFADO = Free FireWire (pro-)audio drivers for Linux |
---|
8 |
* |
---|
9 |
* FFADO is based upon FreeBoB |
---|
10 |
* |
---|
11 |
* This program is free software: you can redistribute it and/or modify |
---|
12 |
* it under the terms of the GNU General Public License as published by |
---|
13 |
* the Free Software Foundation, either version 2 of the License, or |
---|
14 |
* (at your option) version 3 of the License. |
---|
15 |
* |
---|
16 |
* This program is distributed in the hope that it will be useful, |
---|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 |
* GNU General Public License for more details. |
---|
20 |
* |
---|
21 |
* You should have received a copy of the GNU General Public License |
---|
22 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 |
* |
---|
24 |
*/ |
---|
25 |
|
---|
26 |
#include "config.h" |
---|
27 |
#include "devicemanager.h" |
---|
28 |
|
---|
29 |
#include "dice/dice_avdevice.h" |
---|
30 |
|
---|
31 |
using namespace Dice; |
---|
32 |
|
---|
33 |
#include <argp.h> |
---|
34 |
#include <iostream> |
---|
35 |
#include <cstdlib> |
---|
36 |
#include <cstring> |
---|
37 |
#include <unistd.h> |
---|
38 |
|
---|
39 |
#include <signal.h> |
---|
40 |
int run; |
---|
41 |
|
---|
42 |
static void sighandler(int sig) |
---|
43 |
{ |
---|
44 |
run = 0; |
---|
45 |
} |
---|
46 |
|
---|
47 |
using namespace std; |
---|
48 |
using namespace Util; |
---|
49 |
|
---|
50 |
DECLARE_GLOBAL_DEBUG_MODULE; |
---|
51 |
|
---|
52 |
#define MAX_ARGS 1000 |
---|
53 |
|
---|
54 |
//////////////////////////////////////////////// |
---|
55 |
// arg parsing |
---|
56 |
//////////////////////////////////////////////// |
---|
57 |
const char *argp_program_version = "ffado-set-nickname 0.1"; |
---|
58 |
const char *argp_program_bug_address = "<ffado-devel@lists.sf.net>"; |
---|
59 |
static char doc[] = "ffado-set-nickname -- Set device nickname from commandline."; |
---|
60 |
static char args_doc[] = "NODE_ID"; |
---|
61 |
static struct argp_option options[] = { |
---|
62 |
{"nickname", 'N', "NICKNAME", 0, "Set device nickname" }, |
---|
63 |
{"verbose", 'v', "LEVEL", 0, "Produce verbose output" }, |
---|
64 |
{"port", 'p', "PORT", 0, "Set port" }, |
---|
65 |
{"node", 'n', "NODE", 0, "Set node" }, |
---|
66 |
{ 0 } |
---|
67 |
}; |
---|
68 |
|
---|
69 |
struct arguments |
---|
70 |
{ |
---|
71 |
arguments() |
---|
72 |
: nargs ( 0 ) |
---|
73 |
, verbose( -1/*DEBUG_LEVEL_MESSAGE*/ ) |
---|
74 |
, port( -1 ) |
---|
75 |
, node( -1 ) |
---|
76 |
, counts( -1 ) |
---|
77 |
{ |
---|
78 |
args[0] = 0; |
---|
79 |
} |
---|
80 |
|
---|
81 |
char* args[MAX_ARGS]; |
---|
82 |
int nargs; |
---|
83 |
int verbose; |
---|
84 |
int port; |
---|
85 |
int node; |
---|
86 |
int counts; |
---|
87 |
std::string nickname; |
---|
88 |
} arguments; |
---|
89 |
|
---|
90 |
// Parse a single option. |
---|
91 |
static error_t |
---|
92 |
parse_opt( int key, char* arg, struct argp_state* state ) |
---|
93 |
{ |
---|
94 |
// Get the input argument from `argp_parse', which we |
---|
95 |
// know is a pointer to our arguments structure. |
---|
96 |
struct arguments* arguments = ( struct arguments* ) state->input; |
---|
97 |
|
---|
98 |
char* tail; |
---|
99 |
errno = 0; |
---|
100 |
switch (key) { |
---|
101 |
case 'v': |
---|
102 |
arguments->verbose = strtol(arg, &tail, 0); |
---|
103 |
break; |
---|
104 |
case 'p': |
---|
105 |
arguments->port = strtol(arg, &tail, 0); |
---|
106 |
if (errno) { |
---|
107 |
perror("argument parsing failed:"); |
---|
108 |
return errno; |
---|
109 |
} |
---|
110 |
break; |
---|
111 |
case 'n': |
---|
112 |
arguments->node = strtol(arg, &tail, 0); |
---|
113 |
if (errno) { |
---|
114 |
perror("argument parsing failed:"); |
---|
115 |
return errno; |
---|
116 |
} |
---|
117 |
break; |
---|
118 |
case 'N': |
---|
119 |
arguments->nickname = arg; |
---|
120 |
break; |
---|
121 |
case ARGP_KEY_ARG: |
---|
122 |
if (state->arg_num >= MAX_ARGS) { |
---|
123 |
// Too many arguments. |
---|
124 |
argp_usage (state); |
---|
125 |
} |
---|
126 |
arguments->args[state->arg_num] = arg; |
---|
127 |
arguments->nargs++; |
---|
128 |
break; |
---|
129 |
case ARGP_KEY_END: |
---|
130 |
if(arguments->nargs<0) { |
---|
131 |
printf("not enough arguments\n"); |
---|
132 |
return -1; |
---|
133 |
} |
---|
134 |
|
---|
135 |
break; |
---|
136 |
default: |
---|
137 |
return ARGP_ERR_UNKNOWN; |
---|
138 |
} |
---|
139 |
return 0; |
---|
140 |
} |
---|
141 |
|
---|
142 |
static struct argp argp = { options, parse_opt, args_doc, doc }; |
---|
143 |
|
---|
144 |
/////////////////////////// |
---|
145 |
// main |
---|
146 |
////////////////////////// |
---|
147 |
int |
---|
148 |
main(int argc, char **argv) |
---|
149 |
{ |
---|
150 |
run=1; |
---|
151 |
|
---|
152 |
signal (SIGINT, sighandler); |
---|
153 |
signal (SIGPIPE, sighandler); |
---|
154 |
|
---|
155 |
// arg parsing |
---|
156 |
if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) { |
---|
157 |
printMessage("Could not parse command line\n" ); |
---|
158 |
exit(-1); |
---|
159 |
} |
---|
160 |
errno = 0; |
---|
161 |
|
---|
162 |
DeviceManager *m_deviceManager = new DeviceManager(); |
---|
163 |
if ( !m_deviceManager ) { |
---|
164 |
printMessage("Could not allocate device manager\n" ); |
---|
165 |
return -1; |
---|
166 |
} |
---|
167 |
|
---|
168 |
if ( arguments.verbose ) { |
---|
169 |
m_deviceManager->setVerboseLevel(arguments.verbose); |
---|
170 |
} |
---|
171 |
|
---|
172 |
if ( !m_deviceManager->initialize() ) { |
---|
173 |
printMessage("Could not initialize device manager\n" ); |
---|
174 |
delete m_deviceManager; |
---|
175 |
return -1; |
---|
176 |
} |
---|
177 |
|
---|
178 |
char s[1024]; |
---|
179 |
if(arguments.node > -1) { |
---|
180 |
snprintf(s, 1024, "hw:%d,%d", arguments.port, arguments.node); |
---|
181 |
if ( !m_deviceManager->addSpecString(s) ) { |
---|
182 |
printMessage("Could not add spec string %s to device manager\n", s ); |
---|
183 |
delete m_deviceManager; |
---|
184 |
return -1; |
---|
185 |
} |
---|
186 |
} else { |
---|
187 |
snprintf(s, 1024, "hw:%d", arguments.port); |
---|
188 |
if ( !m_deviceManager->addSpecString(s) ) { |
---|
189 |
printMessage("Could not add spec string %s to device manager\n", s ); |
---|
190 |
delete m_deviceManager; |
---|
191 |
return -1; |
---|
192 |
} |
---|
193 |
} |
---|
194 |
|
---|
195 |
if ( !m_deviceManager->discover(false) ) { |
---|
196 |
printMessage("Could not discover devices\n" ); |
---|
197 |
delete m_deviceManager; |
---|
198 |
return -1; |
---|
199 |
} |
---|
200 |
|
---|
201 |
if(m_deviceManager->getAvDeviceCount() == 0) { |
---|
202 |
printMessage("No devices found\n"); |
---|
203 |
delete m_deviceManager; |
---|
204 |
return -1; |
---|
205 |
} |
---|
206 |
|
---|
207 |
Dice::Device* avDevice = dynamic_cast<Dice::Device*>(m_deviceManager->getAvDeviceByIndex(0)); |
---|
208 |
|
---|
209 |
if(avDevice == NULL) { |
---|
210 |
printMessage("Device is not a DICE device\n" ); |
---|
211 |
delete m_deviceManager; |
---|
212 |
return -1; |
---|
213 |
} |
---|
214 |
|
---|
215 |
if(arguments.nickname != "") { |
---|
216 |
printf("Setting nickname to '%s'\n", arguments.nickname.c_str()); |
---|
217 |
avDevice->setNickname(arguments.nickname); |
---|
218 |
} else { |
---|
219 |
printf("Nickname: '%s'\n", avDevice->getNickname().c_str()); |
---|
220 |
} |
---|
221 |
|
---|
222 |
// cleanup |
---|
223 |
delete m_deviceManager; |
---|
224 |
return 0; |
---|
225 |
} |
---|
226 |
|
---|