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

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

2006-06-27 Daniel Wagner <wagi@monom.org>

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