root/branches/streaming-rework/src/devicemanager.cpp

Revision 435, 16.0 kB (checked in by pieterpalmers, 15 years ago)

src/devicemanager:
- start OSC server for the device manager

src/devicemanager,
src/iavdevice,
src/libieee1394/configrom:
- inherit from OscNode? to become Osc'able

src/bounce,
src/libstreaming/AmdtpStreamProcessor,
src/libstreaming/AmdtpSlaveStreamProcessor:
- fixed bounce device implementation, now working

src/bebob:
- fixed midi bug

General:
- removed 'intermediate XML'
- removed obsolete tests
- removed obsolete files
- removed obsolete API calls

  • 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,07 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
26 #include "libieee1394/configrom.h"
27 #include "libieee1394/ieee1394service.h"
28
29 #include "debugmodule/debugmodule.h"
30
31 #include <iostream>
32 #include <sstream>
33
34 #include <unistd.h>
35
36 #include "libstreaming/StreamProcessor.h"
37
38 #ifdef ENABLE_BEBOB
39     #include "bebob/bebob_avdevice.h"
40     #include "maudio/maudio_avdevice.h"
41 #endif
42
43 #ifdef ENABLE_BOUNCE
44     #include "bounce/bounce_avdevice.h"
45     #include "bounce/bounce_slave_avdevice.h"
46 #endif
47
48 #ifdef ENABLE_MOTU
49 #include "motu/motu_avdevice.h"
50 #endif
51
52 #ifdef ENABLE_RME
53 #include "rme/rme_avdevice.h"
54 #endif
55
56 #ifdef ENABLE_DICE
57 #include "dice/dice_avdevice.h"
58 #endif
59
60 #ifdef ENABLE_METRIC_HALO
61 #include "metrichalo/mh_avdevice.h"
62 #endif
63
64 using namespace std;
65
66 IMPL_DEBUG_MODULE( DeviceManager, DeviceManager, DEBUG_LEVEL_NORMAL );
67
68 DeviceManager::DeviceManager()
69     : OscNode("devicemanager")
70     , m_1394Service( 0 )
71     , m_oscServer( NULL )
72 {
73     addOption(Util::OptionContainer::Option("slaveMode",false));
74     addOption(Util::OptionContainer::Option("snoopMode",false));
75 }
76
77 DeviceManager::~DeviceManager()
78 {
79     if (m_oscServer) {
80         m_oscServer->stop();
81         delete m_oscServer;
82     }
83
84     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
85           it != m_avDevices.end();
86           ++it )
87     {
88         delete *it;
89     }
90
91     delete m_1394Service;
92 }
93
94 bool
95 DeviceManager::initialize( int port )
96 {
97     m_1394Service = new Ieee1394Service();
98     if ( !m_1394Service ) {
99         debugFatal( "Could not create Ieee1349Service object\n" );
100         return false;
101     }
102
103     if ( !m_1394Service->initialize( port ) ) {
104         debugFatal( "Could not initialize Ieee1349Service object\n" );
105         delete m_1394Service;
106         m_1394Service = 0;
107         return false;
108     }
109    
110     m_oscServer = new OSC::OscServer("17820");
111    
112     if (!m_oscServer) {
113         debugFatal("failed to create osc server\n");
114         delete m_1394Service;
115         m_1394Service = 0;
116         return false;
117     }
118    
119     if (!m_oscServer->init()) {
120         debugFatal("failed to init osc server\n");
121         delete m_oscServer;
122         m_oscServer = NULL;
123         delete m_1394Service;
124         m_1394Service = 0;
125         return false;
126     }
127    
128     if (!m_oscServer->registerAtRootNode(this)) {
129         debugFatal("failed to register devicemanager at server\n");
130         delete m_oscServer;
131         m_oscServer = NULL;
132         delete m_1394Service;
133         m_1394Service = 0;
134         return false;
135     }
136    
137     if (!m_oscServer->start()) {
138         debugFatal("failed to start osc server\n");
139         delete m_oscServer;
140         m_oscServer = NULL;
141         delete m_1394Service;
142         m_1394Service = 0;
143         return false;
144     }
145
146     return true;
147 }
148
149 bool
150 DeviceManager::discover( int verboseLevel )
151 {
152     bool slaveMode=false;
153     if(!getOption("slaveMode", slaveMode)) {
154         debugWarning("Could not retrieve slaveMode parameter, defauling to false\n");
155     }
156     bool snoopMode=false;
157     if(!getOption("snoopMode", snoopMode)) {
158         debugWarning("Could not retrieve snoopMode parameter, defauling to false\n");
159     }
160
161     setDebugLevel( verboseLevel );
162     m_1394Service->setVerbose( verboseLevel );
163
164     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
165           it != m_avDevices.end();
166           ++it )
167     {
168         if (!removeChildOscNode(*it)) {
169             debugWarning("failed to unregister AvDevice from OSC server\n");
170         }
171         delete *it;
172     }
173     m_avDevices.clear();
174
175     if (!slaveMode) {
176         for ( fb_nodeid_t nodeId = 0;
177               nodeId < m_1394Service->getNodeCount();
178               ++nodeId )
179         {
180             debugOutput( DEBUG_LEVEL_VERBOSE, "Probing node %d...\n", nodeId );
181    
182             if (nodeId == m_1394Service->getLocalNodeId()) {
183                 debugOutput( DEBUG_LEVEL_VERBOSE, "Skipping local node (%d)...\n", nodeId );
184                 continue;
185             }
186    
187             std::auto_ptr<ConfigRom> configRom =
188                 std::auto_ptr<ConfigRom>( new ConfigRom( *m_1394Service,
189                                                          nodeId ) );
190             if ( !configRom->initialize() ) {
191                 // \todo If a PHY on the bus is in power safe mode then
192                 // the config rom is missing. So this might be just
193                 // such this case and we can safely skip it. But it might
194                 // be there is a real software problem on our side.
195                 // This should be handled more carefuly.
196                 debugOutput( DEBUG_LEVEL_NORMAL,
197                              "Could not read config rom from device (node id %d). "
198                              "Skip device discovering for this node\n",
199                              nodeId );
200                 continue;
201             }
202    
203             IAvDevice* avDevice = getDriverForDevice( configRom,
204                                                       nodeId );
205             if ( avDevice ) {
206                 debugOutput( DEBUG_LEVEL_NORMAL,
207                              "discover: driver found for device %d\n",
208                              nodeId );
209
210                 avDevice->setVerboseLevel( verboseLevel );
211                
212                 if ( !avDevice->discover() ) {
213                     debugError( "discover: could not discover device\n" );
214                     delete avDevice;
215                     continue;
216                 }
217
218                 if ( !avDevice->setId( m_avDevices.size() ) ) {
219                     debugError( "setting Id failed\n" );
220                 }
221                
222                 if (snoopMode) {
223                     debugOutput( DEBUG_LEVEL_VERBOSE,
224                                  "Enabling snoop mode on node %d...\n", nodeId );
225
226                     if(!avDevice->setOption("snoopMode", snoopMode)) {
227                         debugWarning("Could not set snoop mode for device on node %d\n", nodeId);
228                         delete avDevice;
229                         continue;
230                     }
231                 }
232                
233                 if ( verboseLevel ) {
234                     avDevice->showDevice();
235                 }
236
237                 m_avDevices.push_back( avDevice );
238                
239                 if (!addChildOscNode(avDevice)) {
240                     debugWarning("failed to register AvDevice at OSC server\n");
241                 }
242
243             }
244         }
245         return true;
246        
247     } else { // slave mode
248         fb_nodeid_t nodeId = m_1394Service->getLocalNodeId();
249         debugOutput( DEBUG_LEVEL_VERBOSE, "Starting in slave mode on node %d...\n", nodeId );
250
251         std::auto_ptr<ConfigRom> configRom =
252             std::auto_ptr<ConfigRom>( new ConfigRom( *m_1394Service,
253                                                      nodeId ) );
254         if ( !configRom->initialize() ) {
255             // \todo If a PHY on the bus is in power safe mode then
256             // the config rom is missing. So this might be just
257             // such this case and we can safely skip it. But it might
258             // be there is a real software problem on our side.
259             // This should be handled more carefuly.
260             debugOutput( DEBUG_LEVEL_NORMAL,
261                          "Could not read config rom from device (node id %d). "
262                          "Skip device discovering for this node\n",
263                          nodeId );
264             return false;
265         }
266
267         IAvDevice* avDevice = getSlaveDriver( configRom );
268         if ( avDevice ) {
269             debugOutput( DEBUG_LEVEL_NORMAL,
270                          "discover: driver found for device %d\n",
271                          nodeId );
272            
273             avDevice->setVerboseLevel( verboseLevel );
274
275             if ( !avDevice->discover() ) {
276                 debugError( "discover: could not discover device\n" );
277                 delete avDevice;
278                 return false;
279             }
280
281             if ( !avDevice->setId( m_avDevices.size() ) ) {
282                 debugError( "setting Id failed\n" );
283             }
284             if ( verboseLevel ) {
285                 avDevice->showDevice();
286             }
287            
288
289             m_avDevices.push_back( avDevice );
290         }
291    
292         return true;
293     }
294 }
295
296
297 IAvDevice*
298 DeviceManager::getDriverForDevice( std::auto_ptr<ConfigRom>( configRom ),
299                                    int id )
300 {
301 #ifdef ENABLE_BEBOB
302     debugOutput( DEBUG_LEVEL_VERBOSE, "Trying BeBoB...\n" );
303     if ( BeBoB::AvDevice::probe( *configRom.get() ) ) {
304         return new BeBoB::AvDevice( configRom, *m_1394Service, id );
305     }
306 #endif
307
308 #ifdef ENABLE_BEBOB
309     debugOutput( DEBUG_LEVEL_VERBOSE, "Trying M-Audio...\n" );
310     if ( MAudio::AvDevice::probe( *configRom.get() ) ) {
311         return new MAudio::AvDevice( configRom, *m_1394Service, id );
312     }
313 #endif
314
315 #ifdef ENABLE_MOTU
316     debugOutput( DEBUG_LEVEL_VERBOSE, "Trying Motu...\n" );
317     if ( Motu::MotuDevice::probe( *configRom.get() ) ) {
318         return new Motu::MotuDevice( configRom, *m_1394Service, id );
319     }
320 #endif
321
322 #ifdef ENABLE_DICE
323     debugOutput( DEBUG_LEVEL_VERBOSE, "Trying Dice...\n" );
324     if ( Dice::DiceAvDevice::probe( *configRom.get() ) ) {
325         return new Dice::DiceAvDevice( configRom, *m_1394Service, id );
326     }
327 #endif
328
329 #ifdef ENABLE_METRIC_HALO
330     debugOutput( DEBUG_LEVEL_VERBOSE, "Trying Metric Halo...\n" );
331     if ( MetricHalo::MHAvDevice::probe( *configRom.get() ) ) {
332         return new MetricHalo::MHAvDevice( configRom, *m_1394Service, id );
333     }
334 #endif
335
336 #ifdef ENABLE_RME
337     debugOutput( DEBUG_LEVEL_VERBOSE, "Trying RME...\n" );
338     if ( Rme::RmeDevice::probe( *configRom.get() ) ) {
339         return new Rme::RmeDevice( configRom, *m_1394Service, id );
340     }
341 #endif
342
343 #ifdef ENABLE_BOUNCE
344     debugOutput( DEBUG_LEVEL_VERBOSE, "Trying Bounce...\n" );
345     if ( Bounce::BounceDevice::probe( *configRom.get() ) ) {
346         return new Bounce::BounceDevice( configRom, *m_1394Service, id );
347     }
348 #endif
349
350     return 0;
351 }
352
353 IAvDevice*
354 DeviceManager::getSlaveDriver( std::auto_ptr<ConfigRom>( configRom ) )
355 {
356
357 #ifdef ENABLE_BOUNCE
358     if ( Bounce::BounceSlaveDevice::probe( *configRom.get() ) ) {
359         return new Bounce::BounceSlaveDevice( configRom, *m_1394Service );
360     }
361 #endif
362
363     return 0;
364 }
365
366 bool
367 DeviceManager::isValidNode(int node)
368 {
369     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
370           it != m_avDevices.end();
371           ++it )
372     {
373         IAvDevice* avDevice = *it;
374
375         if (avDevice->getConfigRom().getNodeId() == node) {
376                 return true;
377         }
378     }
379     return false;
380 }
381
382 int
383 DeviceManager::getNbDevices()
384 {
385     return m_avDevices.size();
386 }
387
388 int
389 DeviceManager::getDeviceNodeId( int deviceNr )
390 {
391     if ( ! ( deviceNr < getNbDevices() ) ) {
392         debugError( "Device number out of range (%d)\n", deviceNr );
393         return -1;
394     }
395
396     IAvDevice* avDevice = m_avDevices.at( deviceNr );
397
398     if ( !avDevice ) {
399         debugError( "Could not get device at position (%d)\n",  deviceNr );
400     }
401
402     return avDevice->getConfigRom().getNodeId();
403 }
404
405 IAvDevice*
406 DeviceManager::getAvDevice( int nodeId )
407 {
408     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
409           it != m_avDevices.end();
410           ++it )
411     {
412         IAvDevice* avDevice = *it;
413         if ( avDevice->getConfigRom().getNodeId() == nodeId ) {
414             return avDevice;
415         }
416     }
417
418     return 0;
419 }
420
421 IAvDevice*
422 DeviceManager::getAvDeviceByIndex( int idx )
423 {
424         return m_avDevices.at(idx);
425 }
426
427 unsigned int
428 DeviceManager::getAvDeviceCount( )
429 {
430         return m_avDevices.size();
431 }
432
433 /**
434  * Return the streamprocessor that is to be used as
435  * the sync source.
436  *
437  * Algorithm still to be determined
438  *
439  * @return StreamProcessor that is sync source
440  */
441 Streaming::StreamProcessor *
442 DeviceManager::getSyncSource() {
443     IAvDevice* device = getAvDeviceByIndex(0);
444
445     bool slaveMode=false;
446     if(!getOption("slaveMode", slaveMode)) {
447         debugWarning("Could not retrieve slaveMode parameter, defauling to false\n");
448     }
449    
450     #warning TEST CODE FOR BOUNCE DEVICE !!
451     // this makes the bounce slave use the xmit SP as sync source
452     if (slaveMode) {
453         return device->getStreamProcessorByIndex(1);
454     } else {
455         return device->getStreamProcessorByIndex(0);
456     }
457    
458 }
459
460 bool
461 DeviceManager::deinitialize()
462 {
463     return true;
464 }
465
466 bool
467 DeviceManager::saveCache( Glib::ustring fileName )
468 {
469     int i;
470     i=0; // avoids unused warning
471    
472     for ( IAvDeviceVectorIterator it = m_avDevices.begin();
473           it != m_avDevices.end();
474           ++it )
475     {
476         IAvDevice* pAvDevice;
477         pAvDevice = *it; // avoids unused warning
478        
479         #ifdef ENABLE_BEBOB
480         BeBoB::AvDevice* pBeBoBDevice = reinterpret_cast< BeBoB::AvDevice* >( pAvDevice );
481         if ( pBeBoBDevice ) {
482             // Let's use a very simple path to find the devices later.
483             // Since under hood there will an xml node name this should
484             // adhere the needed naming rules. Of course we could give it
485             // a good node name and add an attribute but that would mean
486             // we have to expose a bigger interface from IOSerialize/IODesialize,
487             // which is just not needed. KISS.
488             ostringstream strstrm;
489             strstrm << "id" << i;
490             Glib::ustring basePath = "BeBoB/" + strstrm.str() + "/";
491
492             Util::XMLSerialize ser( fileName );
493             pBeBoBDevice->serialize( basePath, ser );
494
495             i++;
496             std::cout << "a bebob device serialized" << std::endl;
497         }
498         #endif
499     }
500     return true;
501 }
502
503 bool
504 DeviceManager::loadCache( Glib::ustring fileName )
505 {
506     int i;
507     i=0; // avoids unused warning
508     Util::XMLDeserialize deser( fileName );
509
510     typedef std::vector<ConfigRom*> ConfigRomVector;
511     ConfigRomVector configRoms;
512
513     for ( fb_nodeid_t nodeId = 0;
514           nodeId < m_1394Service->getNodeCount();
515           ++nodeId )
516     {
517         ConfigRom* pConfigRom  =  new ConfigRom( *m_1394Service, nodeId );
518         if ( !pConfigRom->initialize() ) {
519             // \todo If a PHY on the bus is in power safe mode then
520             // the config rom is missing. So this might be just
521             // such this case and we can safely skip it. But it might
522             // be there is a real software problem on our side.
523             // This should be handled more carefuly.
524             debugOutput( DEBUG_LEVEL_NORMAL,
525                          "Could not read config rom from device (node id %d). "
526                          "Skip device discovering for this node\n",
527                          nodeId );
528             delete pConfigRom;
529             continue;
530         }
531         configRoms.push_back( pConfigRom );
532     }
533
534     #ifdef ENABLE_BEBOB
535     BeBoB::AvDevice* pBeBoBDevice = 0;
536     do {
537         ostringstream strstrm;
538         strstrm << "id" << i;
539         pBeBoBDevice = BeBoB::AvDevice::deserialize(
540             "BeBoB/" + strstrm.str() + "/",
541             deser,
542             *m_1394Service );
543
544         ++i;
545         if ( pBeBoBDevice ) {
546             for (ConfigRomVector::iterator it = configRoms.begin();
547                  it != configRoms.end();
548                  ++it )
549             {
550                 ConfigRom* pConfigRom = *it;
551
552                 if ( pBeBoBDevice->getConfigRom() == *pConfigRom ) {
553                     pBeBoBDevice->getConfigRom().setNodeId( pConfigRom->getNodeId() );
554                     // m_avDevices.push_back( pBeBoBDevice );
555
556                 }
557             }
558         }
559     } while ( pBeBoBDevice );
560     #endif
561    
562     return true;
563 }
Note: See TracBrowser for help on using the browser.