root/trunk/libfreebob/src/devicemanager.cpp

Revision 188, 8.6 kB (checked in by wagi, 16 years ago)

2006-05-04 Daniel Wagner <wagi@monom.org>

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