root/trunk/libfreebob/src/bebob/bebob_functionblock.cpp

Revision 378, 14.3 kB (checked in by wagi, 16 years ago)

FunctionBlock?: De/Serialize added

Line 
1 /* bebob_functionblock.cpp
2  * Copyright (C) 2006,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 "bebob/bebob_functionblock.h"
22 #include "bebob/bebob_avdevice_subunit.h"
23 #include "bebob/bebob_avdevice.h"
24 #include "configrom.h"
25
26
27 IMPL_DEBUG_MODULE( BeBoB::FunctionBlock, BeBoB::FunctionBlock, DEBUG_LEVEL_NORMAL );
28
29 BeBoB::FunctionBlock::FunctionBlock(
30     AvDeviceSubunit& subunit,
31     function_block_type_t type,
32     function_block_type_t subtype,
33     function_block_id_t id,
34     ESpecialPurpose purpose,
35     no_of_input_plugs_t nrOfInputPlugs,
36     no_of_output_plugs_t nrOfOutputPlugs,
37     int verbose )
38     : m_subunit( &subunit )
39     , m_type( type )
40     , m_subtype( subtype )
41     , m_id( id )
42     , m_purpose( purpose )
43     , m_nrOfInputPlugs( nrOfInputPlugs )
44     , m_nrOfOutputPlugs( nrOfOutputPlugs )
45     , m_verbose( verbose )
46 {
47     setDebugLevel( verbose );
48 }
49
50 BeBoB::FunctionBlock::FunctionBlock( const FunctionBlock& rhs )
51     : m_subunit( rhs.m_subunit )
52     , m_type( rhs.m_type )
53     , m_subtype( rhs.m_subtype )
54     , m_id( rhs.m_id )
55     , m_purpose( rhs.m_purpose )
56     , m_nrOfInputPlugs( rhs.m_nrOfInputPlugs )
57     , m_nrOfOutputPlugs( rhs.m_nrOfOutputPlugs )
58     , m_verbose( rhs.m_verbose )
59 {
60 }
61
62 BeBoB::FunctionBlock::FunctionBlock()
63 {
64 }
65
66 BeBoB::FunctionBlock::~FunctionBlock()
67 {
68     for ( AvPlugVector::iterator it = m_plugs.begin();
69           it != m_plugs.end();
70           ++it )
71     {
72         delete *it;
73     }
74
75 }
76
77 bool
78 BeBoB::FunctionBlock::discover()
79 {
80     debugOutput( DEBUG_LEVEL_VERBOSE,
81                  "discover function block %s (nr of input plugs = %d, "
82                  "nr of output plugs = %d)\n",
83                  getName(),
84                  m_nrOfInputPlugs,
85                  m_nrOfOutputPlugs );
86
87     if ( !discoverPlugs( AvPlug::eAPD_Input, m_nrOfInputPlugs ) ) {
88         debugError( "Could not discover input plug for '%s'\n",
89                     getName() );
90         return false;
91     }
92
93     if ( !discoverPlugs( AvPlug::eAPD_Output, m_nrOfOutputPlugs ) ) {
94         debugError( "Could not discover output plugs for '%s'\n",
95                     getName() );
96         return false;
97     }
98
99     return true;
100 }
101
102 bool
103 BeBoB::FunctionBlock::discoverPlugs( AvPlug::EAvPlugDirection plugDirection,
104                                      plug_id_t plugMaxId )
105 {
106     for ( int plugId = 0; plugId < plugMaxId; ++plugId ) {
107         AvPlug* plug = new AvPlug(
108             m_subunit->getAvDevice().get1394Service(),
109             m_subunit->getAvDevice().getConfigRom(),
110             m_subunit->getAvDevice().getPlugManager(),
111             m_subunit->getSubunitType(),
112             m_subunit->getSubunitId(),
113             m_type,
114             m_id,
115             AvPlug::eAPA_FunctionBlockPlug,
116             plugDirection,
117             plugId,
118             m_verbose );
119
120         if ( !plug || !plug->discover() ) {
121             debugError( "plug discovering failed for plug %d\n",
122                         plugId );
123             delete plug;
124             return false;
125         }
126
127         debugOutput( DEBUG_LEVEL_NORMAL, "plug '%s' found\n",
128                      plug->getName() );
129         m_plugs.push_back( plug );
130     }
131
132     return true;
133 }
134
135 bool
136 BeBoB::FunctionBlock::discoverConnections()
137 {
138     debugOutput( DEBUG_LEVEL_VERBOSE,
139                  "discover connections function block %s\n",
140                  getName() );
141
142     for ( AvPlugVector::iterator it = m_plugs.begin();
143           it != m_plugs.end();
144           ++it )
145     {
146         AvPlug* plug = *it;
147         if ( !plug->discoverConnections() ) {
148             debugError( "Could not discover plug connections\n" );
149             return false;
150         }
151     }
152     return true;
153 }
154
155 bool
156 serializeAvPlugVector( Glib::ustring basePath,
157                        Util::IOSerialize& ser,
158                        const BeBoB::AvPlugVector& vec )
159 {
160     bool result = true;
161     int i = 0;
162     for ( BeBoB::AvPlugVector::const_iterator it = vec.begin();
163           it != vec.end();
164           ++it )
165     {
166         std::ostringstream strstrm;
167         strstrm << basePath << i;
168         result &= ser.write( strstrm.str(), ( *it )->getGlobalId() );
169         i++;
170     }
171     return result;
172 }
173
174 bool
175 deserializeAvPlugVector( Glib::ustring basePath,
176                          Util::IODeserialize& deser,
177                          BeBoB::AvDevice& avDevice,
178                          BeBoB::AvPlugVector& vec )
179 {
180     int i = 0;
181     bool bFinished = false;
182     bool result = true;
183     do {
184         plug_id_t plugId;
185         std::ostringstream strstrm;
186         strstrm << basePath << i;
187
188         result &= deser.read( strstrm.str(), plugId );
189         BeBoB::AvPlug* pPlug = avDevice.getPlugManager().getPlug( plugId );
190
191         if ( pPlug ) {
192             vec.push_back( pPlug );
193             i++;
194         } else {
195             bFinished = true;
196         }
197     } while ( !bFinished );
198
199     return result;
200 }
201
202 bool
203 BeBoB::FunctionBlock::serialize( Glib::ustring basePath, Util::IOSerialize& ser ) const
204 {
205     bool result;
206
207     result  = ser.write( basePath + "m_type", m_type );
208     result &= ser.write( basePath + "m_subtype", m_subtype );
209     result &= ser.write( basePath + "m_id", m_id );
210     result &= ser.write( basePath + "m_purpose", m_purpose );
211     result &= ser.write( basePath + "m_nrOfInputPlugs", m_nrOfInputPlugs );
212     result &= ser.write( basePath + "m_nrOfOutputPlugs", m_nrOfOutputPlugs );
213     result &= ser.write( basePath + "m_verbose", m_verbose );
214     result &= serializeAvPlugVector( basePath + "m_plugs", ser, m_plugs );
215
216     return result;
217 }
218
219 BeBoB::FunctionBlock*
220 BeBoB::FunctionBlock::deserialize( Glib::ustring basePath,
221                                    Util::IODeserialize& deser,
222                                    AvDevice& avDevice,
223                                    AvDeviceSubunit& subunit )
224 {
225     bool result;
226     function_block_type_t type;
227     function_block_type_t subtype;
228     FunctionBlock* pFB = 0;
229
230     result  = deser.read( basePath + "m_type", type );
231     result &= deser.read( basePath + "m_subtype", subtype );
232     if ( !result ) {
233         return 0;
234     }
235
236     switch ( type ) {
237     case ExtendedSubunitInfoCmd::eFBT_AudioSubunitSelector:
238         pFB = new FunctionBlockSelector;
239         break;
240     case ExtendedSubunitInfoCmd::eFBT_AudioSubunitFeature:
241         pFB = new FunctionBlockFeature;
242         break;
243     case ExtendedSubunitInfoCmd::eFBT_AudioSubunitProcessing:
244         if ( subtype == ExtendedSubunitInfoCmd::ePT_EnhancedMixer ) {
245             pFB = new FunctionBlockEnhancedMixer;
246         } else {
247             pFB = new FunctionBlockProcessing;
248         }
249         break;
250     case ExtendedSubunitInfoCmd::eFBT_AudioSubunitCodec:
251         pFB = new FunctionBlockCodec;
252         break;
253     default:
254         pFB = 0;
255     }
256
257     if ( !pFB ) {
258         return 0;
259     }
260
261     pFB->m_subunit = &subunit;
262     pFB->m_type = type;
263     pFB->m_subtype = subtype;
264
265     result &= deser.read( basePath + "m_id", pFB->m_id );
266     result &= deser.read( basePath + "m_purpose", pFB->m_purpose );
267     result &= deser.read( basePath + "m_nrOfInputPlugs", pFB->m_nrOfInputPlugs );
268     result &= deser.read( basePath + "m_nrOfOutputPlugs", pFB->m_nrOfOutputPlugs );
269     result &= deser.read( basePath + "m_verbose", pFB->m_verbose );
270     result &= deserializeAvPlugVector( basePath + "m_plugs", deser, avDevice, pFB->m_plugs );
271
272     return 0;
273 }
274
275 ///////////////////////
276
277 BeBoB::FunctionBlockSelector::FunctionBlockSelector(
278     AvDeviceSubunit& subunit,
279     function_block_id_t id,
280     ESpecialPurpose purpose,
281     no_of_input_plugs_t nrOfInputPlugs,
282     no_of_output_plugs_t nrOfOutputPlugs,
283     int verbose )
284     : FunctionBlock( subunit,
285                      eFBT_AudioSubunitSelector,
286                      0,
287                      id,
288                      purpose,
289                      nrOfInputPlugs,
290                      nrOfOutputPlugs,
291                      verbose )
292 {
293 }
294
295 BeBoB::FunctionBlockSelector::FunctionBlockSelector(
296     const FunctionBlockSelector& rhs )
297     : FunctionBlock( rhs )
298 {
299 }
300
301 BeBoB::FunctionBlockSelector::FunctionBlockSelector()
302     : FunctionBlock()
303 {
304 }
305
306 BeBoB::FunctionBlockSelector::~FunctionBlockSelector()
307 {
308 }
309
310 const char*
311 BeBoB::FunctionBlockSelector::getName()
312 {
313     return "Selector";
314 }
315
316 bool
317 BeBoB::FunctionBlockSelector::serializeChild( Glib::ustring basePath,
318                                               Util::IOSerialize& ser ) const
319 {
320     return true;
321 }
322
323 bool
324 BeBoB::FunctionBlockSelector::deserializeChild( Glib::ustring basePath,
325                                                 Util::IODeserialize& deser,
326                                                 AvDevice& avDevice )
327 {
328     return true;
329 }
330
331 ///////////////////////
332
333 BeBoB::FunctionBlockFeature::FunctionBlockFeature(
334     AvDeviceSubunit& subunit,
335     function_block_id_t id,
336     ESpecialPurpose purpose,
337     no_of_input_plugs_t nrOfInputPlugs,
338     no_of_output_plugs_t nrOfOutputPlugs,
339     int verbose )
340     : FunctionBlock( subunit,
341                      eFBT_AudioSubunitFeature,
342                      0,
343                      id,
344                      purpose,
345                      nrOfInputPlugs,
346                      nrOfOutputPlugs,
347                      verbose )
348 {
349 }
350
351 BeBoB::FunctionBlockFeature::FunctionBlockFeature(
352     const FunctionBlockFeature& rhs )
353     : FunctionBlock( rhs )
354 {
355 }
356
357 BeBoB::FunctionBlockFeature::FunctionBlockFeature()
358     : FunctionBlock()
359 {
360 }
361
362 BeBoB::FunctionBlockFeature::~FunctionBlockFeature()
363 {
364 }
365
366 const char*
367 BeBoB::FunctionBlockFeature::getName()
368 {
369     return "Feature";
370 }
371
372 bool
373 BeBoB::FunctionBlockFeature::serializeChild( Glib::ustring basePath,
374                                              Util::IOSerialize& ser ) const
375 {
376     return true;
377 }
378
379 bool
380 BeBoB::FunctionBlockFeature::deserializeChild( Glib::ustring basePath,
381                                                Util::IODeserialize& deser,
382                                                AvDevice& avDevice )
383 {
384     return true;
385 }
386
387 ///////////////////////
388
389 BeBoB::FunctionBlockEnhancedMixer::FunctionBlockEnhancedMixer(
390     AvDeviceSubunit& subunit,
391     function_block_id_t id,
392     ESpecialPurpose purpose,
393     no_of_input_plugs_t nrOfInputPlugs,
394     no_of_output_plugs_t nrOfOutputPlugs,
395     int verbose )
396     : FunctionBlock( subunit,
397                      eFBT_AudioSubunitProcessing,
398                      ExtendedSubunitInfoCmd::ePT_EnhancedMixer,
399                      id,
400                      purpose,
401                      nrOfInputPlugs,
402                      nrOfOutputPlugs,
403                      verbose )
404 {
405 }
406
407 BeBoB::FunctionBlockEnhancedMixer::FunctionBlockEnhancedMixer(
408     const FunctionBlockEnhancedMixer& rhs )
409     : FunctionBlock( rhs )
410 {
411 }
412
413 BeBoB::FunctionBlockEnhancedMixer::FunctionBlockEnhancedMixer()
414     : FunctionBlock()
415 {
416 }
417
418 BeBoB::FunctionBlockEnhancedMixer::~FunctionBlockEnhancedMixer()
419 {
420 }
421
422 const char*
423 BeBoB::FunctionBlockEnhancedMixer::getName()
424 {
425     return "EnhancedMixer";
426 }
427
428 bool
429 BeBoB::FunctionBlockEnhancedMixer::serializeChild( Glib::ustring basePath,
430                                                    Util::IOSerialize& ser ) const
431 {
432     return true;
433 }
434
435 bool
436 BeBoB::FunctionBlockEnhancedMixer::deserializeChild( Glib::ustring basePath,
437                                                      Util::IODeserialize& deser,
438                                                      AvDevice& avDevice )
439 {
440     return true;
441 }
442
443 ///////////////////////
444
445 BeBoB::FunctionBlockProcessing::FunctionBlockProcessing(
446     AvDeviceSubunit& subunit,
447     function_block_id_t id,
448     ESpecialPurpose purpose,
449     no_of_input_plugs_t nrOfInputPlugs,
450     no_of_output_plugs_t nrOfOutputPlugs,
451     int verbose )
452     : FunctionBlock( subunit,
453                      eFBT_AudioSubunitProcessing,
454                      0,
455                      id,
456                      purpose,
457                      nrOfInputPlugs,
458                      nrOfOutputPlugs,
459                      verbose )
460 {
461 }
462
463 BeBoB::FunctionBlockProcessing::FunctionBlockProcessing(
464     const FunctionBlockProcessing& rhs )
465     : FunctionBlock( rhs )
466 {
467 }
468
469 BeBoB::FunctionBlockProcessing::FunctionBlockProcessing()
470     : FunctionBlock()
471 {
472 }
473
474 BeBoB::FunctionBlockProcessing::~FunctionBlockProcessing()
475 {
476 }
477
478 const char*
479 BeBoB::FunctionBlockProcessing::getName()
480 {
481     return "Dummy Processing";
482 }
483
484 bool
485 BeBoB::FunctionBlockProcessing::serializeChild( Glib::ustring basePath,
486                                                 Util::IOSerialize& ser ) const
487 {
488     return true;
489 }
490
491 bool
492 BeBoB::FunctionBlockProcessing::deserializeChild( Glib::ustring basePath,
493                                                   Util::IODeserialize& deser,
494                                                   AvDevice& avDevice )
495 {
496     return true;
497 }
498
499 ///////////////////////
500
501 BeBoB::FunctionBlockCodec::FunctionBlockCodec(
502     AvDeviceSubunit& subunit,
503     function_block_id_t id,
504     ESpecialPurpose purpose,
505     no_of_input_plugs_t nrOfInputPlugs,
506     no_of_output_plugs_t nrOfOutputPlugs,
507     int verbose )
508     : FunctionBlock( subunit,
509                      eFBT_AudioSubunitCodec,
510                      0,
511                      id,
512                      purpose,
513                      nrOfInputPlugs,
514                      nrOfOutputPlugs,
515                      verbose )
516 {
517 }
518
519 BeBoB::FunctionBlockCodec::FunctionBlockCodec( const FunctionBlockCodec& rhs )
520     : FunctionBlock( rhs )
521 {
522 }
523
524 BeBoB::FunctionBlockCodec::FunctionBlockCodec()
525     : FunctionBlock()
526 {
527 }
528
529 BeBoB::FunctionBlockCodec::~FunctionBlockCodec()
530 {
531 }
532
533 const char*
534 BeBoB::FunctionBlockCodec::getName()
535 {
536     return "Dummy Codec";
537 }
538
539 bool
540 BeBoB::FunctionBlockCodec::serializeChild( Glib::ustring basePath,
541                                            Util::IOSerialize& ser ) const
542 {
543     return true;
544 }
545
546 bool
547 BeBoB::FunctionBlockCodec::deserializeChild( Glib::ustring basePath,
548                                              Util::IODeserialize& deser,
549                                              AvDevice& avDevice )
550 {
551     return true;
552 }
Note: See TracBrowser for help on using the browser.