root/branches/libfreebob-2.0/src/devicemanager.cpp

Revision 336, 10.1 kB (checked in by pieterpalmers, 17 years ago)

- Merged the developments on trunk since branch-off:

branch occurred at rev 194
svn merge -r 194:HEAD https://svn.sourceforge.net/svnroot/freebob/trunk/libfreebob

- Modified libfreebobavc to use the messagebuffer for debug info.
- This should compile and run

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1  /* devicemanager.cpp
2  * Copyright (C) 2005,06 by Daniel Wagner
3  *
4  * This file is part of FreeBoB.
5  *
6  * FreeBoB is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * FreeBoB is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with FreeBoB; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA.
19  */
20
21 #include "fbtypes.h"
22
23 #include "devicemanager.h"
24 #include "iavdevice.h"
25 #include "configrom.h"
26
27 #include "libfreebobavc/ieee1394service.h"
28 #include "debugmodule/debugmodule.h"
29 #include "bebob/bebob_avdevice.h"
30 #include "bounce/bounce_avdevice.h"
31 #include "motu/motu_avdevice.h"
32 #include "rme/rme_avdevice.h"
33 #include "maudio/maudio_avdevice.h"
34
35 #include <iostream>
36 #include <unistd.h>
37
38 // Unit directory SpecifierID/Version identifying a true Bebob AVC device
39 #define BEBOB_AVCDEVICE_UNIT_SPECIFIER   0x0000a02d
40 #define BEBOB_AVCDEVICE_UNIT_VERSION     0x10001
41
42 // The vendor ID for MOTU devices
43 #define MOTU_VENDOR_ID                   0x000001f2
44
45 // The vendor ID for RME devices
46 #define RME_VENDOR_ID                    0x00000a35
47
48 using namespace std;
49
50 IMPL_DEBUG_MODULE( DeviceManager, DeviceManager, DEBUG_LEVEL_NORMAL );
51
52 DeviceManager::DeviceManager()
53     : m_1394Service( 0 )
54 {
55
56 }
57
58 DeviceManager::~DeviceManager()
59 {
60     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
61           it != m_avDevices.end();
62           ++it )
63     {
64         delete *it;
65     }
66
67     delete m_1394Service;
68 }
69
70 bool
71 DeviceManager::initialize( int port )
72 {
73     m_1394Service = new Ieee1394Service();
74     if ( !m_1394Service ) {
75         debugFatal( "Could not create Ieee1349Service object\n" );
76         return false;
77     }
78
79     if ( !m_1394Service->initialize( port ) ) {
80         debugFatal( "Could not initialize Ieee1349Service object\n" );
81         delete m_1394Service;
82         m_1394Service = 0;
83         return false;
84     }
85
86     return true;
87 }
88
89 bool
90 DeviceManager::discover( int verboseLevel )
91 {
92     switch ( verboseLevel ) {
93     case 3:
94         m_1394Service->setVerbose( true );
95     case 1:
96         setDebugLevel( DEBUG_LEVEL_VERBOSE );
97     }
98
99     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
100           it != m_avDevices.end();
101           ++it )
102     {
103         delete *it;
104     }
105     m_avDevices.clear();
106
107     for ( fb_nodeid_t nodeId = 0;
108           nodeId < m_1394Service->getNodeCount();
109           ++nodeId )
110     {
111         debugOutput( DEBUG_LEVEL_VERBOSE, "Probing node %d...\n", nodeId );
112        
113         std::auto_ptr<ConfigRom> configRom =
114             std::auto_ptr<ConfigRom>( new ConfigRom( *m_1394Service,
115                                                      nodeId ) );
116         if ( !configRom->initialize() ) {
117             // \todo If a PHY on the bus is in power safe mode then
118             // the config rom is missing. So this might be just
119             // such this case and we can safely skip it. But it might
120             // be there is a real software problem on our side.
121             // This should be handled more carefuly.
122             debugOutput( DEBUG_LEVEL_NORMAL,
123                          "Could not read config rom from device (node id %d). "
124                          "Skip device discovering for this node\n",
125                          nodeId );
126             continue;
127         }
128
129         IAvDevice* avDevice = getDriverForDevice( configRom,
130                                                   nodeId,
131                                                   verboseLevel );
132         if ( avDevice ) {
133             debugOutput( DEBUG_LEVEL_NORMAL,
134                          "discover: driver found for device %d\n",
135                          nodeId );
136
137             if ( !avDevice->discover() ) {
138                 debugError( "discover: could not discover device\n" );
139                 delete avDevice;
140                 continue;
141             }
142
143             if ( !avDevice->setId( m_avDevices.size() ) ) {
144                 debugError( "setting Id failed\n" );
145             }
146             if ( verboseLevel ) {
147                 avDevice->showDevice();
148             }
149
150             m_avDevices.push_back( avDevice );
151         }
152     }
153
154     return true;
155 }
156
157
158 IAvDevice*
159 DeviceManager::getDriverForDevice( std::auto_ptr<ConfigRom>( configRom ),
160                                    int id,  int level )
161 {
162     if ( BeBoB::AvDevice::probe( *configRom.get() ) ) {
163         return new BeBoB::AvDevice( configRom, *m_1394Service, id, level );
164     }
165
166     if ( MAudio::AvDevice::probe( *configRom.get() ) ) {
167         return new MAudio::AvDevice( configRom, *m_1394Service, id, level );
168     }
169
170     if ( Motu::MotuDevice::probe( *configRom.get() ) ) {
171         return new Motu::MotuDevice( configRom, *m_1394Service, id, level );
172     }
173
174     if ( Rme::RmeDevice::probe( *configRom.get() ) ) {
175         return new Rme::RmeDevice( configRom, *m_1394Service, id, level );
176     }
177
178     if ( Bounce::BounceDevice::probe( *configRom.get() ) ) {
179         return new Bounce::BounceDevice( configRom, *m_1394Service, id, level );
180     }
181
182     return 0;
183 }
184
185 bool
186 DeviceManager::isValidNode(int node)
187 {
188     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
189           it != m_avDevices.end();
190           ++it )
191     {
192         IAvDevice* avDevice = *it;
193
194         if (avDevice->getConfigRom().getNodeId() == node) {
195                 return true;
196         }
197     }
198     return false;
199 }
200
201 int
202 DeviceManager::getNbDevices()
203 {
204     return m_avDevices.size();
205 }
206
207 int
208 DeviceManager::getDeviceNodeId( int deviceNr )
209 {
210     if ( ! ( deviceNr < getNbDevices() ) ) {
211         debugError( "Device number out of range (%d)\n", deviceNr );
212         return -1;
213     }
214
215     IAvDevice* avDevice = m_avDevices.at( deviceNr );
216
217     if ( !avDevice ) {
218         debugError( "Could not get device at position (%d)\n",  deviceNr );
219     }
220
221     return avDevice->getConfigRom().getNodeId();
222 }
223
224 IAvDevice*
225 DeviceManager::getAvDevice( int nodeId )
226 {
227     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
228           it != m_avDevices.end();
229           ++it )
230     {
231         IAvDevice* avDevice = *it;
232         if ( avDevice->getConfigRom().getNodeId() == nodeId ) {
233             return avDevice;
234         }
235     }
236
237     return 0;
238 }
239
240 IAvDevice*
241 DeviceManager::getAvDeviceByIndex( int idx )
242 {
243         return m_avDevices.at(idx);
244 }
245
246 unsigned int
247 DeviceManager::getAvDeviceCount( )
248 {
249         return m_avDevices.size();
250 }
251
252 xmlDocPtr
253 DeviceManager::getXmlDescription()
254 {
255     xmlDocPtr doc = xmlNewDoc( BAD_CAST "1.0" );
256     if ( !doc ) {
257         debugError( "Couldn't create new xml doc\n" );
258         return 0;
259     }
260
261     xmlNodePtr rootNode = xmlNewNode( 0,  BAD_CAST "FreeBoBConnectionInfo" );
262     if ( !rootNode ) {
263         debugError( "Couldn't create root node\n" );
264         xmlFreeDoc( doc );
265         xmlCleanupParser();
266         return 0;
267     }
268     xmlDocSetRootElement( doc, rootNode );
269
270     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
271           it != m_avDevices.end();
272           ++it )
273     {
274         IAvDevice* avDevice = *it;
275
276         xmlNodePtr deviceNode = xmlNewChild( rootNode, 0,
277                                              BAD_CAST "Device", 0 );
278         if ( !deviceNode ) {
279             debugError( "Couldn't create device node\n" );
280             xmlFreeDoc( doc );
281             xmlCleanupParser();
282             return 0;
283         }
284
285         char* result;
286         asprintf( &result, "%d", avDevice->getConfigRom().getNodeId() );
287         if ( !xmlNewChild( deviceNode,  0,
288                            BAD_CAST "NodeId",  BAD_CAST result ) )
289         {
290             debugError( "Couldn't create 'NodeId' node" );
291             free(result);
292             return 0;
293         }
294         free( result );
295
296         std::string res = "Connection Information for "
297                           + avDevice->getConfigRom().getVendorName()
298                           +", "
299                           + avDevice->getConfigRom().getModelName()
300                           + " configuration";
301         if ( !xmlNewChild( deviceNode,
302                            0,
303                            BAD_CAST "Comment",
304                            BAD_CAST res.c_str() ) ) {
305             debugError( "Couldn't create comment node\n" );
306             xmlFreeDoc( doc );
307             xmlCleanupParser();
308             free(result);
309             return 0;
310         }
311
312         res = avDevice->getConfigRom().getVendorName();
313
314         if ( !xmlNewChild( deviceNode,
315                            0,
316                            BAD_CAST "Vendor",
317                            BAD_CAST res.c_str() ) ) {
318             debugError( "Couldn't create vendor node\n" );
319             xmlFreeDoc( doc );
320             xmlCleanupParser();
321             free(result);
322             return 0;
323         }
324
325         res = avDevice->getConfigRom().getModelName();
326
327         if ( !xmlNewChild( deviceNode,
328                            0,
329                            BAD_CAST "Model",
330                            BAD_CAST res.c_str() ) ) {
331             debugError( "Couldn't create model node\n" );
332             xmlFreeDoc( doc );
333             xmlCleanupParser();
334             free(result);
335             return 0;
336         }
337
338         asprintf( &result, "%08x%08x",
339                   ( quadlet_t )( avDevice->getConfigRom().getGuid() >> 32 ),
340                   ( quadlet_t )( avDevice->getConfigRom().getGuid() & 0xfffffff ) );
341         if ( !xmlNewChild( deviceNode,  0,
342                            BAD_CAST "GUID",  BAD_CAST result ) ) {
343             debugError( "Couldn't create 'GUID' node\n" );
344             xmlFreeDoc( doc );
345             xmlCleanupParser();
346            
347             free(result);
348             return 0;
349         }
350         free( result );
351
352
353         if ( !avDevice->addXmlDescription( deviceNode ) ) {
354             debugError( "Adding XML description failed\n" );
355             xmlFreeDoc( doc );
356             xmlCleanupParser();
357             free(result);
358             return 0;
359         }
360        
361         free(result);
362     }
363
364     return doc;
365 }
366
367 bool
368 DeviceManager::deinitialize()
369 {
370     return true;
371 }
372
Note: See TracBrowser for help on using the browser.