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

Revision 197, 9.0 kB (checked in by pieterpalmers, 16 years ago)

- implemented first steps of MOTU device discovery

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