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

Revision 241, 8.8 kB (checked in by pieterpalmers, 18 years ago)

* configure.ac: Version bump to 1.0.0

* Changed all FreeBob? to FreeBoB
* Removed all .cvsignore
* Added Pieter to AUTHORS
* Updated NEWS and README (release canditate date added)

by Daniel Wagner

  • 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
122                 if ( !avDevice->setId( m_avDevices.size() ) ) {
123                     debugError( "setting Id failed\n" );
124                 }
125                 if ( verboseLevel ) {
126                     avDevice->showDevice();
127                 }
128                 break;
129             }
130         }
131
132     }
133
134     return true;
135 }
136
137
138 IAvDevice*
139 DeviceManager::probeBeBoB(Ieee1394Service& service, int id, int level)
140 {
141     IAvDevice* avDevice = new BeBoB_Light::AvDevice( service, id, level );
142     if ( !avDevice ) {
143         return 0;
144     }
145
146     if ( !avDevice->discover() ) {
147         delete avDevice;
148         return 0;
149     }
150     return avDevice;
151 }
152
153 IAvDevice*
154 DeviceManager::probeBounce(Ieee1394Service& service, int id, int level)
155 {
156     IAvDevice* avDevice = new Bounce::BounceDevice( service, id, level );
157     if ( !avDevice ) {
158         return 0;
159     }
160
161     if ( !avDevice->discover() ) {
162         delete avDevice;
163         return 0;
164     }
165     return avDevice;
166 }
167
168 bool
169 DeviceManager::isValidNode(int node)
170 {
171     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
172           it != m_avDevices.end();
173           ++it )
174     {
175         IAvDevice* avDevice = *it;
176
177         if (avDevice->getConfigRom().getNodeId() == node) {
178                 return true;
179         }
180     }
181     return false;
182 }
183
184 int
185 DeviceManager::getNbDevices()
186 {
187     return m_avDevices.size();
188 }
189
190 int
191 DeviceManager::getDeviceNodeId( int deviceNr )
192 {
193     if ( ! ( deviceNr < getNbDevices() ) ) {
194         debugError( "Device number out of range (%d)\n", deviceNr );
195         return -1;
196     }
197
198     IAvDevice* avDevice = m_avDevices.at( deviceNr );
199
200     if ( !avDevice ) {
201         debugError( "Could not get device at position (%d)\n",  deviceNr );
202     }
203
204     return avDevice->getConfigRom().getNodeId();
205 }
206
207 IAvDevice*
208 DeviceManager::getAvDevice( int nodeId )
209 {
210     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
211           it != m_avDevices.end();
212           ++it )
213     {
214         IAvDevice* avDevice = *it;
215         if ( avDevice->getConfigRom().getNodeId() == nodeId ) {
216             return avDevice;
217         }
218     }
219
220     return 0;
221 }
222
223 xmlDocPtr
224 DeviceManager::getXmlDescription()
225 {
226     xmlDocPtr doc = xmlNewDoc( BAD_CAST "1.0" );
227     if ( !doc ) {
228         debugError( "Couldn't create new xml doc\n" );
229         return 0;
230     }
231
232     xmlNodePtr rootNode = xmlNewNode( 0,  BAD_CAST "FreeBoBConnectionInfo" );
233     if ( !rootNode ) {
234         debugError( "Couldn't create root node\n" );
235         xmlFreeDoc( doc );
236         xmlCleanupParser();
237         return 0;
238     }
239     xmlDocSetRootElement( doc, rootNode );
240
241     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
242           it != m_avDevices.end();
243           ++it )
244     {
245         IAvDevice* avDevice = *it;
246
247         xmlNodePtr deviceNode = xmlNewChild( rootNode, 0,
248                                              BAD_CAST "Device", 0 );
249         if ( !deviceNode ) {
250             debugError( "Couldn't create device node\n" );
251             xmlFreeDoc( doc );
252             xmlCleanupParser();
253             return 0;
254         }
255
256         char* result;
257         asprintf( &result, "%d", avDevice->getConfigRom().getNodeId() );
258         if ( !xmlNewChild( deviceNode,  0,
259                            BAD_CAST "NodeId",  BAD_CAST result ) )
260         {
261             debugError( "Couldn't create 'NodeId' node" );
262             free(result);
263             return 0;
264         }
265         free( result );
266
267         std::string res = "Connection Information for "
268                           + avDevice->getConfigRom().getVendorName()
269                           +", "
270                           + avDevice->getConfigRom().getModelName()
271                           + " configuration";
272         if ( !xmlNewChild( deviceNode,
273                            0,
274                            BAD_CAST "Comment",
275                            BAD_CAST res.c_str() ) ) {
276             debugError( "Couldn't create comment node\n" );
277             xmlFreeDoc( doc );
278             xmlCleanupParser();
279             return 0;
280         }
281
282         res = avDevice->getConfigRom().getVendorName();
283
284         if ( !xmlNewChild( deviceNode,
285                            0,
286                            BAD_CAST "Vendor",
287                            BAD_CAST res.c_str() ) ) {
288             debugError( "Couldn't create vendor node\n" );
289             xmlFreeDoc( doc );
290             xmlCleanupParser();
291             return 0;
292         }
293
294         res = avDevice->getConfigRom().getModelName();
295
296         if ( !xmlNewChild( deviceNode,
297                            0,
298                            BAD_CAST "Model",
299                            BAD_CAST res.c_str() ) ) {
300             debugError( "Couldn't create model node\n" );
301             xmlFreeDoc( doc );
302             xmlCleanupParser();
303             return 0;
304         }
305
306         asprintf( &result, "%08x%08x",
307                   ( quadlet_t )( avDevice->getConfigRom().getGuid() >> 32 ),
308                   ( quadlet_t )( avDevice->getConfigRom().getGuid() & 0xfffffff ) );
309         if ( !xmlNewChild( deviceNode,  0,
310                            BAD_CAST "GUID",  BAD_CAST result ) ) {
311             debugError( "Couldn't create 'GUID' node\n" );
312             xmlFreeDoc( doc );
313             xmlCleanupParser();
314             free(result);
315             return 0;
316         }
317         free( result );
318
319
320         if ( !avDevice->addXmlDescription( deviceNode ) ) {
321             debugError( "Adding XML description failed\n" );
322             xmlFreeDoc( doc );
323             xmlCleanupParser();
324             return 0;
325         }
326     }
327
328     return doc;
329 }
330
331 bool
332 DeviceManager::deinitialize()
333 {
334     return true;
335 }
336
Note: See TracBrowser for help on using the browser.