1 |
/* |
---|
2 |
* Copyright (C) 2005-2008 by Pieter Palmers |
---|
3 |
* |
---|
4 |
* This file is part of FFADO |
---|
5 |
* FFADO = Free FireWire (pro-)audio drivers for Linux |
---|
6 |
* |
---|
7 |
* FFADO is based upon FreeBoB. |
---|
8 |
* |
---|
9 |
* This program is free software: you can redistribute it and/or modify |
---|
10 |
* it under the terms of the GNU General Public License as published by |
---|
11 |
* the Free Software Foundation, either version 2 of the License, or |
---|
12 |
* (at your option) version 3 of the License. |
---|
13 |
* |
---|
14 |
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. |
---|
21 |
* |
---|
22 |
*/ |
---|
23 |
|
---|
24 |
#include "PortManager.h" |
---|
25 |
#include "Port.h" |
---|
26 |
|
---|
27 |
#include <assert.h> |
---|
28 |
|
---|
29 |
#include <iostream> |
---|
30 |
#include <sstream> |
---|
31 |
|
---|
32 |
|
---|
33 |
namespace Streaming { |
---|
34 |
|
---|
35 |
IMPL_DEBUG_MODULE( PortManager, PortManager, DEBUG_LEVEL_NORMAL ); |
---|
36 |
|
---|
37 |
PortManager::PortManager() |
---|
38 |
{ |
---|
39 |
} |
---|
40 |
|
---|
41 |
PortManager::~PortManager() |
---|
42 |
{ |
---|
43 |
flushDebugOutput(); |
---|
44 |
// delete all ports that are still registered to the manager |
---|
45 |
while (m_Ports.size()) { |
---|
46 |
// This will also remove the port from m_Ports via |
---|
47 |
// PortManager::unregister(). |
---|
48 |
delete m_Ports.front(); |
---|
49 |
} |
---|
50 |
for ( Util::FunctorVectorIterator it = m_UpdateHandlers.begin(); |
---|
51 |
it != m_UpdateHandlers.end(); |
---|
52 |
++it ) |
---|
53 |
{ |
---|
54 |
Util::Functor* func = *it; |
---|
55 |
delete func; |
---|
56 |
} |
---|
57 |
} |
---|
58 |
|
---|
59 |
bool |
---|
60 |
PortManager::makeNameUnique(Port *port) |
---|
61 |
{ |
---|
62 |
bool done = false; |
---|
63 |
int idx = 0; |
---|
64 |
std::string portname_orig = port->getName(); |
---|
65 |
|
---|
66 |
while(!done && idx < 10000) { |
---|
67 |
bool is_unique=true; |
---|
68 |
|
---|
69 |
for ( PortVectorIterator it = m_Ports.begin(); |
---|
70 |
it != m_Ports.end(); |
---|
71 |
++it ) |
---|
72 |
{ |
---|
73 |
is_unique &= !((*it)->getName() == port->getName()); |
---|
74 |
} |
---|
75 |
|
---|
76 |
if (is_unique) { |
---|
77 |
done = true; |
---|
78 |
} else { |
---|
79 |
std::ostringstream portname; |
---|
80 |
portname << portname_orig << idx++; |
---|
81 |
port->setName(portname.str()); |
---|
82 |
} |
---|
83 |
} |
---|
84 |
|
---|
85 |
if(idx < 10000) return true; |
---|
86 |
else return false; |
---|
87 |
} |
---|
88 |
|
---|
89 |
/** |
---|
90 |
* |
---|
91 |
* @param port |
---|
92 |
* @return |
---|
93 |
*/ |
---|
94 |
bool |
---|
95 |
PortManager::registerPort(Port *port) |
---|
96 |
{ |
---|
97 |
assert(port); |
---|
98 |
|
---|
99 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Adding port %s, type: %d, dir: %d\n", |
---|
100 |
port->getName().c_str(), port->getPortType(), port->getDirection()); |
---|
101 |
|
---|
102 |
port->setVerboseLevel(getDebugLevel()); |
---|
103 |
|
---|
104 |
if (makeNameUnique(port)) { |
---|
105 |
m_Ports.push_back(port); |
---|
106 |
callUpdateHandlers(); |
---|
107 |
return true; |
---|
108 |
} else { |
---|
109 |
return false; |
---|
110 |
} |
---|
111 |
} |
---|
112 |
|
---|
113 |
bool |
---|
114 |
PortManager::unregisterPort(Port *port) |
---|
115 |
{ |
---|
116 |
assert(port); |
---|
117 |
debugOutput( DEBUG_LEVEL_VERBOSE, "unregistering port %s\n",port->getName().c_str()); |
---|
118 |
|
---|
119 |
for ( PortVectorIterator it = m_Ports.begin(); |
---|
120 |
it != m_Ports.end(); |
---|
121 |
++it ) |
---|
122 |
{ |
---|
123 |
if(*it == port) { |
---|
124 |
m_Ports.erase(it); |
---|
125 |
callUpdateHandlers(); |
---|
126 |
return true; |
---|
127 |
} |
---|
128 |
} |
---|
129 |
|
---|
130 |
debugOutput( DEBUG_LEVEL_VERBOSE, "port %s not found \n",port->getName().c_str()); |
---|
131 |
|
---|
132 |
return false; //not found |
---|
133 |
} |
---|
134 |
|
---|
135 |
int |
---|
136 |
PortManager::getPortCount(enum Port::E_PortType type) |
---|
137 |
{ |
---|
138 |
int count=0; |
---|
139 |
|
---|
140 |
for ( PortVectorIterator it = m_Ports.begin(); |
---|
141 |
it != m_Ports.end(); |
---|
142 |
++it ) |
---|
143 |
{ |
---|
144 |
if ( (*it)->getPortType() == type ) { |
---|
145 |
count++; |
---|
146 |
} |
---|
147 |
} |
---|
148 |
return count; |
---|
149 |
} |
---|
150 |
|
---|
151 |
int |
---|
152 |
PortManager::getPortCount() |
---|
153 |
{ |
---|
154 |
int count = 0; |
---|
155 |
|
---|
156 |
count += m_Ports.size(); |
---|
157 |
|
---|
158 |
return count; |
---|
159 |
} |
---|
160 |
|
---|
161 |
Port * |
---|
162 |
PortManager::getPortAtIdx(unsigned int index) |
---|
163 |
{ |
---|
164 |
|
---|
165 |
return m_Ports.at(index); |
---|
166 |
|
---|
167 |
} |
---|
168 |
|
---|
169 |
void |
---|
170 |
PortManager::setVerboseLevel(int i) |
---|
171 |
{ |
---|
172 |
setDebugLevel(i); |
---|
173 |
for ( PortVectorIterator it = m_Ports.begin(); |
---|
174 |
it != m_Ports.end(); |
---|
175 |
++it ) |
---|
176 |
{ |
---|
177 |
(*it)->setVerboseLevel(i); |
---|
178 |
} |
---|
179 |
} |
---|
180 |
|
---|
181 |
|
---|
182 |
bool |
---|
183 |
PortManager::resetPorts() |
---|
184 |
{ |
---|
185 |
debugOutput( DEBUG_LEVEL_VERBOSE, "reset ports\n"); |
---|
186 |
|
---|
187 |
for ( PortVectorIterator it = m_Ports.begin(); |
---|
188 |
it != m_Ports.end(); |
---|
189 |
++it ) |
---|
190 |
{ |
---|
191 |
if(!(*it)->reset()) { |
---|
192 |
debugFatal("Could not reset port %s",(*it)->getName().c_str()); |
---|
193 |
return false; |
---|
194 |
} |
---|
195 |
} |
---|
196 |
return true; |
---|
197 |
} |
---|
198 |
|
---|
199 |
bool |
---|
200 |
PortManager::initPorts() |
---|
201 |
{ |
---|
202 |
debugOutput( DEBUG_LEVEL_VERBOSE, "init ports\n"); |
---|
203 |
|
---|
204 |
for ( PortVectorIterator it = m_Ports.begin(); |
---|
205 |
it != m_Ports.end(); |
---|
206 |
++it ) |
---|
207 |
{ |
---|
208 |
if(!(*it)->init()) { |
---|
209 |
debugFatal("Could not init port %s\n", (*it)->getName().c_str()); |
---|
210 |
return false; |
---|
211 |
} |
---|
212 |
} |
---|
213 |
return true; |
---|
214 |
} |
---|
215 |
|
---|
216 |
bool |
---|
217 |
PortManager::preparePorts() |
---|
218 |
{ |
---|
219 |
debugOutput( DEBUG_LEVEL_VERBOSE, "preparing ports\n"); |
---|
220 |
|
---|
221 |
for ( PortVectorIterator it = m_Ports.begin(); |
---|
222 |
it != m_Ports.end(); |
---|
223 |
++it ) |
---|
224 |
{ |
---|
225 |
if(!(*it)->prepare()) { |
---|
226 |
debugFatal("Could not prepare port %s",(*it)->getName().c_str()); |
---|
227 |
return false; |
---|
228 |
} |
---|
229 |
} |
---|
230 |
return true; |
---|
231 |
} |
---|
232 |
|
---|
233 |
bool |
---|
234 |
PortManager::addPortManagerUpdateHandler( Util::Functor* functor ) |
---|
235 |
{ |
---|
236 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Adding PortManagerUpdate handler (%p)\n", functor); |
---|
237 |
m_UpdateHandlers.push_back( functor ); |
---|
238 |
return true; |
---|
239 |
} |
---|
240 |
|
---|
241 |
bool |
---|
242 |
PortManager::remPortManagerUpdateHandler( Util::Functor* functor ) |
---|
243 |
{ |
---|
244 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Removing PortManagerUpdate handler (%p)\n", functor); |
---|
245 |
|
---|
246 |
for ( Util::FunctorVectorIterator it = m_UpdateHandlers.begin(); |
---|
247 |
it != m_UpdateHandlers.end(); |
---|
248 |
++it ) |
---|
249 |
{ |
---|
250 |
if ( *it == functor ) { |
---|
251 |
debugOutput(DEBUG_LEVEL_VERBOSE, " found\n"); |
---|
252 |
m_UpdateHandlers.erase( it ); |
---|
253 |
return true; |
---|
254 |
} |
---|
255 |
} |
---|
256 |
debugOutput(DEBUG_LEVEL_VERBOSE, " not found\n"); |
---|
257 |
return false; |
---|
258 |
} |
---|
259 |
|
---|
260 |
Util::Functor* |
---|
261 |
PortManager::getUpdateHandlerForPtr(void *ptr) |
---|
262 |
{ |
---|
263 |
for ( Util::FunctorVectorIterator it = m_UpdateHandlers.begin(); |
---|
264 |
it != m_UpdateHandlers.end(); |
---|
265 |
++it ) |
---|
266 |
{ |
---|
267 |
if ( (*it)->matchCallee(ptr) ) { |
---|
268 |
debugOutput(DEBUG_LEVEL_VERBOSE, " found\n"); |
---|
269 |
return *it; |
---|
270 |
} |
---|
271 |
} |
---|
272 |
return NULL; |
---|
273 |
} |
---|
274 |
|
---|
275 |
void |
---|
276 |
PortManager::callUpdateHandlers() |
---|
277 |
{ |
---|
278 |
for ( Util::FunctorVectorIterator it = m_UpdateHandlers.begin(); |
---|
279 |
it != m_UpdateHandlers.end(); |
---|
280 |
++it ) |
---|
281 |
{ |
---|
282 |
Util::Functor* func = *it; |
---|
283 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Calling PortManagerUpdate handler (%p)\n", func); |
---|
284 |
( *func )(); |
---|
285 |
} |
---|
286 |
} |
---|
287 |
|
---|
288 |
} |
---|