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

Revision 246, 9.8 kB (checked in by pieterpalmers, 17 years ago)

- re-enabled the Motu device and updated it to the IAvDevice changes
- created a framework streamprocessor for the motu devices

  • 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 "bebob_light/bebob_light_avdevice.h"
31 #include "bounce/bounce_avdevice.h"
32 #include "motu/motu_avdevice.h"
33
34 #include <iostream>
35 #include <unistd.h>
36
37 using namespace std;
38
39 IMPL_DEBUG_MODULE( DeviceManager, DeviceManager, DEBUG_LEVEL_NORMAL );
40
41 DeviceManager::DeviceManager()
42     : m_1394Service( 0 )
43 {
44     m_probeList.push_back( probeBeBoB );
45     m_probeList.push_back( probeMotu );
46     m_probeList.push_back( probeBounce );
47 }
48
49 DeviceManager::~DeviceManager()
50 {
51     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
52           it != m_avDevices.end();
53           ++it )
54     {
55         delete *it;
56     }
57
58     delete m_1394Service;
59 }
60
61 bool
62 DeviceManager::initialize( int port )
63 {
64     m_1394Service = new Ieee1394Service();
65     if ( !m_1394Service ) {
66         debugFatal( "Could not create Ieee1349Service object\n" );
67         return false;
68     }
69
70     if ( !m_1394Service->initialize( port ) ) {
71         debugFatal( "Could not initialize Ieee1349Service object\n" );
72         delete m_1394Service;
73         m_1394Service = 0;
74         return false;
75     }
76
77     return true;
78 }
79
80 bool
81 DeviceManager::discover( int verboseLevel )
82 {
83     if ( verboseLevel ) {
84         setDebugLevel( DEBUG_LEVEL_VERBOSE );
85     }
86     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
87           it != m_avDevices.end();
88           ++it )
89     {
90         delete *it;
91     }
92     m_avDevices.clear();
93
94     for ( fb_nodeid_t nodeId = 0;
95           nodeId < m_1394Service->getNodeCount();
96           ++nodeId )
97     {
98         ConfigRom configRom( m_1394Service, nodeId );
99         if ( !configRom.initialize() ) {
100             // \todo If a PHY on the bus in power safe mode than
101             // the config rom is missing. So this might be just
102             // such a case and we can safely skip it. But it might
103             // be there is a real software problem on our side.
104             // This should be handled more carefuly.
105             debugOutput( DEBUG_LEVEL_NORMAL,
106                          "Could not read config rom from device (node id %d). "
107                          "Skip device discovering for this node\n",
108                          nodeId );
109             continue;
110         }
111
112         if ( !configRom.isAvcDevice() ) {
113             continue;
114         }
115
116         for ( ProbeFunctionVector::iterator it = m_probeList.begin();
117               it != m_probeList.end();
118               ++it )
119         {
120             ProbeFunction func = *it;
121             IAvDevice* avDevice = func(*m_1394Service, nodeId, verboseLevel);
122             if ( avDevice ) {
123                 m_avDevices.push_back( avDevice );
124                 if (!avDevice->setId(m_avDevices.size())) {
125                     debugError("Could not set Id of AvDevice\n");
126                 }
127                 if ( verboseLevel ) {
128                     avDevice->showDevice();
129                 }
130                 break;
131             }
132         }
133
134     }
135
136     return true;
137 }
138
139
140 IAvDevice*
141 DeviceManager::probeBeBoB(Ieee1394Service& service, int id, int level)
142 {
143     IAvDevice* avDevice = new BeBoB_Light::AvDevice( service, id, level );
144     if ( !avDevice ) {
145         return NULL;
146     }
147
148     if ( !avDevice->discover() ) {
149         delete avDevice;
150         debugOutput( DEBUG_LEVEL_VERBOSE, "Not a BeBoB device...\n" );
151        return NULL;
152     }
153     debugOutput( DEBUG_LEVEL_VERBOSE, "BeBoB device discovered...\n" );
154     return avDevice;
155 }
156
157 IAvDevice*
158 DeviceManager::probeBounce(Ieee1394Service& service, int id, int level)
159 {
160     IAvDevice* avDevice = new Bounce::BounceDevice( service, id, level );
161     if ( !avDevice ) {
162         return NULL;
163     }
164
165     if ( !avDevice->discover() ) {
166         debugOutput( DEBUG_LEVEL_VERBOSE, "Not a Bounce device...\n");
167    
168         delete avDevice;
169         return NULL;
170     }
171     debugOutput( DEBUG_LEVEL_VERBOSE, "Bounce device discovered...\n");
172    
173     return avDevice;
174 }
175
176 IAvDevice*
177 DeviceManager::probeMotu(Ieee1394Service& service, int id, int level)
178 {
179     IAvDevice* avDevice = new Motu::MotuDevice( service, id, level );
180     if ( !avDevice ) {
181         return NULL;
182     }
183
184     if ( !avDevice->discover() ) {
185         delete avDevice;
186         return NULL;
187     }
188     return avDevice;
189 }
190
191 bool
192 DeviceManager::isValidNode(int node)
193 {
194     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
195           it != m_avDevices.end();
196           ++it )
197     {
198         IAvDevice* avDevice = *it;
199
200         if (avDevice->getConfigRom().getNodeId() == node) {
201                 return true;
202         }
203     }
204     return false;
205 }
206
207 int
208 DeviceManager::getNbDevices()
209 {
210     return m_avDevices.size();
211 }
212
213 int
214 DeviceManager::getDeviceNodeId( int deviceNr )
215 {
216     if ( ! ( deviceNr < getNbDevices() ) ) {
217         debugError( "Device number out of range (%d)\n", deviceNr );
218         return -1;
219     }
220
221     IAvDevice* avDevice = m_avDevices.at( deviceNr );
222
223     if ( !avDevice ) {
224         debugError( "Could not get device at position (%d)\n",  deviceNr );
225     }
226
227     return avDevice->getConfigRom().getNodeId();
228 }
229
230 IAvDevice*
231 DeviceManager::getAvDevice( int nodeId )
232 {
233     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
234           it != m_avDevices.end();
235           ++it )
236     {
237         IAvDevice* avDevice = *it;
238         if ( avDevice->getConfigRom().getNodeId() == nodeId ) {
239             return avDevice;
240         }
241     }
242
243     return 0;
244 }
245
246 IAvDevice*
247 DeviceManager::getAvDeviceByIndex( int idx )
248 {
249         return m_avDevices.at(idx);
250 }
251
252 unsigned int
253 DeviceManager::getAvDeviceCount( )
254 {
255         return m_avDevices.size();
256 }
257
258 xmlDocPtr
259 DeviceManager::getXmlDescription()
260 {
261     xmlDocPtr doc = xmlNewDoc( BAD_CAST "1.0" );
262     if ( !doc ) {
263         debugError( "Couldn't create new xml doc\n" );
264         return 0;
265     }
266
267     xmlNodePtr rootNode = xmlNewNode( 0,  BAD_CAST "FreeBobConnectionInfo" );
268     if ( !rootNode ) {
269         debugError( "Couldn't create root node\n" );
270         xmlFreeDoc( doc );
271         xmlCleanupParser();
272         return 0;
273     }
274     xmlDocSetRootElement( doc, rootNode );
275
276     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
277           it != m_avDevices.end();
278           ++it )
279     {
280         IAvDevice* avDevice = *it;
281
282         xmlNodePtr deviceNode = xmlNewChild( rootNode, 0,
283                                              BAD_CAST "Device", 0 );
284         if ( !deviceNode ) {
285             debugError( "Couldn't create device node\n" );
286             xmlFreeDoc( doc );
287             xmlCleanupParser();
288             return 0;
289         }
290
291         char* result;
292         asprintf( &result, "%d", avDevice->getConfigRom().getNodeId() );
293         if ( !xmlNewChild( deviceNode,  0,
294                            BAD_CAST "NodeId",  BAD_CAST result ) )
295         {
296             debugError( "Couldn't create 'NodeId' node" );
297             free(result);
298             return false;
299         }
300
301         std::string res = "Connection Information for "
302                           + avDevice->getConfigRom().getVendorName()
303                           +", "
304                           + avDevice->getConfigRom().getModelName()
305                           + " configuration";
306         if ( !xmlNewChild( deviceNode,
307                            0,
308                            BAD_CAST "Comment",
309                            BAD_CAST res.c_str() ) ) {
310             debugError( "Couldn't create comment node\n" );
311             xmlFreeDoc( doc );
312             xmlCleanupParser();
313             free(result);
314             return 0;
315         }
316
317         res = avDevice->getConfigRom().getVendorName();
318
319         if ( !xmlNewChild( deviceNode,
320                            0,
321                            BAD_CAST "Vendor",
322                            BAD_CAST res.c_str() ) ) {
323             debugError( "Couldn't create vendor node\n" );
324             xmlFreeDoc( doc );
325             xmlCleanupParser();
326             free(result);
327             return 0;
328         }
329
330         res = avDevice->getConfigRom().getModelName();
331
332         if ( !xmlNewChild( deviceNode,
333                            0,
334                            BAD_CAST "Model",
335                            BAD_CAST res.c_str() ) ) {
336             debugError( "Couldn't create model node\n" );
337             xmlFreeDoc( doc );
338             xmlCleanupParser();
339             free(result);
340             return 0;
341         }
342
343         asprintf( &result, "%08x%08x",
344                   ( quadlet_t )( avDevice->getConfigRom().getGuid() >> 32 ),
345                   ( quadlet_t )( avDevice->getConfigRom().getGuid() & 0xfffffff ) );
346         if ( !xmlNewChild( deviceNode,  0,
347                            BAD_CAST "GUID",  BAD_CAST result ) ) {
348             debugError( "Couldn't create 'GUID' node\n" );
349             xmlFreeDoc( doc );
350             xmlCleanupParser();
351             free(result);
352             return false;
353         }
354
355         if ( !avDevice->addXmlDescription( deviceNode ) ) {
356             debugError( "Adding XML description failed\n" );
357             xmlFreeDoc( doc );
358             xmlCleanupParser();
359             free(result);
360             return 0;
361         }
362        
363         free(result);
364     }
365
366     return doc;
367 }
368
369 bool
370 DeviceManager::deinitialize()
371 {
372     return true;
373 }
374
Note: See TracBrowser for help on using the browser.