root/branches/libffado-2.0/src/bebob/bebob_avdevice.cpp

Revision 1321, 22.3 kB (checked in by wagi, 12 years ago)

Implement getMinimum and getMaximum for the generic mixer.
This makes the input volume fader working for the FA-101.

Line 
1 /*
2  * Copyright (C) 2005-2008 by Daniel Wagner
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "config.h"
25
26 #include "devicemanager.h"
27 #include "bebob/bebob_avdevice.h"
28 #include "bebob/bebob_avdevice_subunit.h"
29 #include "bebob/bebob_mixer.h"
30
31 #include "bebob/focusrite/focusrite_saffire.h"
32 #include "bebob/focusrite/focusrite_saffirepro.h"
33 #include "bebob/terratec/terratec_device.h"
34 #include "bebob/mackie/onyxmixer.h"
35 #include "bebob/edirol/edirol_fa101.h"
36 #include "bebob/edirol/edirol_fa66.h"
37 #include "bebob/esi/quatafire610.h"
38
39 #include "libieee1394/configrom.h"
40 #include "libieee1394/ieee1394service.h"
41
42 #include "libavc/general/avc_plug_info.h"
43 #include "libavc/general/avc_extended_plug_info.h"
44 #include "libavc/general/avc_subunit_info.h"
45 #include "libavc/streamformat/avc_extended_stream_format.h"
46 #include "libutil/cmd_serialize.h"
47 #include "libavc/avc_definitions.h"
48
49 #include "debugmodule/debugmodule.h"
50
51 #include <iostream>
52 #include <sstream>
53 #include <unistd.h>
54 #include <cstdlib>
55 #include <cstring>
56 #include <sys/stat.h>
57
58 using namespace AVC;
59
60 namespace BeBoB {
61
62 AvDevice::AvDevice( DeviceManager& d, std::auto_ptr< ConfigRom >( configRom ) )
63     : GenericAVC::AvDevice( d, configRom )
64     , m_Mixer ( 0 )
65 {
66     debugOutput( DEBUG_LEVEL_VERBOSE, "Created BeBoB::AvDevice (NodeID %d)\n",
67                  getConfigRom().getNodeId() );
68
69     // DM1500 based devices seem to upset the linux1394 stack when commands are
70     // sent too fast.
71     if (AVC::AVCCommand::getSleepAfterAVCCommand() < 200) {
72         AVC::AVCCommand::setSleepAfterAVCCommand( 200 );
73     }
74
75 }
76
77 AvDevice::~AvDevice()
78 {
79     destroyMixer();
80 }
81
82 bool
83 AvDevice::probe( Util::Configuration& c, ConfigRom& configRom, bool generic )
84 {
85     if(generic) {
86         return false;
87         // try a bebob-specific command to check for the firmware
88         ExtendedPlugInfoCmd extPlugInfoCmd( configRom.get1394Service() );
89         UnitPlugAddress unitPlugAddress( UnitPlugAddress::ePT_PCR,
90                                          configRom.getNodeId() );
91         extPlugInfoCmd.setPlugAddress( PlugAddress( PlugAddress::ePD_Input,
92                                                     PlugAddress::ePAM_Unit,
93                                                     unitPlugAddress ) );
94         extPlugInfoCmd.setNodeId( configRom.getNodeId() );
95         extPlugInfoCmd.setCommandType( AVCCommand::eCT_Status );
96         extPlugInfoCmd.setVerbose( configRom.getVerboseLevel() );
97         ExtendedPlugInfoInfoType extendedPlugInfoInfoType(
98             ExtendedPlugInfoInfoType::eIT_NoOfChannels );
99         extendedPlugInfoInfoType.initialize();
100         extPlugInfoCmd.setInfoType( extendedPlugInfoInfoType );
101    
102         if ( !extPlugInfoCmd.fire() ) {
103             debugError( "Number of channels command failed\n" );
104             return false;
105         }
106    
107         ExtendedPlugInfoInfoType* infoType = extPlugInfoCmd.getInfoType();
108         if ( infoType
109             && infoType->m_plugNrOfChns )
110         {
111             return true;
112         }
113         return false;
114     } else {
115         // check if device is in supported devices list
116         unsigned int vendorId = configRom.getNodeVendorId();
117         unsigned int modelId = configRom.getModelId();
118
119         Util::Configuration::VendorModelEntry vme = c.findDeviceVME( vendorId, modelId );
120         return c.isValid(vme) && vme.driver == Util::Configuration::eD_BeBoB;
121     }
122 }
123
124 FFADODevice *
125 AvDevice::createDevice(DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
126 {
127     unsigned int vendorId = configRom->getNodeVendorId();
128     unsigned int modelId = configRom->getModelId();
129
130     switch (vendorId) {
131         case FW_VENDORID_MACKIE:
132             if (modelId == 0x00010065 ) {
133                 return new Mackie::OnyxMixerDevice(d, configRom);
134             }
135         case FW_VENDORID_EDIROL:
136             switch (modelId) {
137                 case 0x00010048:
138                     return new Edirol::EdirolFa101Device(d, configRom);
139                 case 0x00010049:
140                     return new Edirol::EdirolFa66Device(d, configRom);
141                 default:
142                     return new AvDevice(d, configRom);
143             }
144         case FW_VENDORID_ESI:
145             if (modelId == 0x00010064) {
146                 return new ESI::QuataFireDevice(d, configRom);
147             }
148             break;
149         case FW_VENDORID_TERRATEC:
150             switch(modelId) {
151                 case 0x00000003:
152                     return new Terratec::Phase88Device(d, configRom);
153                 default: // return a plain BeBoB device
154                     return new AvDevice(d, configRom);
155             }
156         case FW_VENDORID_FOCUSRITE:
157             switch(modelId) {
158                 case 0x00000003:
159                 case 0x00000006:
160                     return new Focusrite::SaffireProDevice(d, configRom);
161                 case 0x00000000:
162                     return new Focusrite::SaffireDevice(d, configRom);
163                 default: // return a plain BeBoB device
164                     return new AvDevice(d, configRom);
165            }
166         default:
167             return new AvDevice(d, configRom);
168     }
169     return NULL;
170 }
171
172 #define BEBOB_CHECK_AND_ADD_SR(v, x) \
173     { if(supportsSamplingFrequency(x)) \
174       v.push_back(x); }
175 bool
176 AvDevice::discover()
177 {
178     unsigned int vendorId = getConfigRom().getNodeVendorId();
179     unsigned int modelId = getConfigRom().getModelId();
180
181     Util::Configuration &c = getDeviceManager().getConfiguration();
182     Util::Configuration::VendorModelEntry vme = c.findDeviceVME( vendorId, modelId );
183
184     if (c.isValid(vme) && vme.driver == Util::Configuration::eD_BeBoB) {
185         debugOutput( DEBUG_LEVEL_VERBOSE, "found %s %s\n",
186                      vme.vendor_name.c_str(),
187                      vme.model_name.c_str());
188     } else {
189         debugWarning("Using generic BeBoB support for unsupported device '%s %s'\n",
190                      getConfigRom().getVendorName().c_str(), getConfigRom().getModelName().c_str());
191     }
192
193     if ( !Unit::discover() ) {
194         debugError( "Could not discover unit\n" );
195         return false;
196     }
197
198     if((getAudioSubunit( 0 ) == NULL)) {
199         debugError( "Unit doesn't have an Audio subunit.\n");
200         return false;
201     }
202     if((getMusicSubunit( 0 ) == NULL)) {
203         debugError( "Unit doesn't have a Music subunit.\n");
204         return false;
205     }
206
207     if(!buildMixer()) {
208         debugWarning("Could not build mixer\n");
209     }
210     return true;
211 }
212
213 bool
214 AvDevice::buildMixer()
215 {
216     debugOutput(DEBUG_LEVEL_VERBOSE, "Building a generic BeBoB mixer...\n");
217     // create a Mixer
218     // this removes the mixer if it already exists
219     // note: a mixer self-registers to it's parent
220     delete m_Mixer;
221
222     // create the mixer & register it
223     if(getAudioSubunit(0) == NULL) {
224         debugWarning("Could not find audio subunit, mixer not available.\n");
225         m_Mixer = NULL;
226     } else {
227         m_Mixer = new Mixer(*this);
228     }
229     if (m_Mixer) m_Mixer->setVerboseLevel(getDebugLevel());
230     return m_Mixer != NULL;
231 }
232
233 bool
234 AvDevice::destroyMixer()
235 {
236     delete m_Mixer;
237     return true;
238 }
239
240 bool
241 AvDevice::setSelectorFBValue(int id, int value) {
242     FunctionBlockCmd fbCmd( get1394Service(),
243                             FunctionBlockCmd::eFBT_Selector,
244                             id,
245                             FunctionBlockCmd::eCA_Current );
246     fbCmd.setNodeId( getNodeId() );
247     fbCmd.setSubunitId( 0x00 );
248     fbCmd.setCommandType( AVCCommand::eCT_Control );
249     fbCmd.m_pFBSelector->m_inputFbPlugNumber = (value & 0xFF);
250     fbCmd.setVerboseLevel( getDebugLevel() );
251
252     if ( !fbCmd.fire() ) {
253         debugError( "cmd failed\n" );
254         return false;
255     }
256
257 //     if ( getDebugLevel() >= DEBUG_LEVEL_NORMAL ) {
258 //         Util::CoutSerializer se;
259 //         fbCmd.serialize( se );
260 //     }
261 //     
262     if((fbCmd.getResponse() != AVCCommand::eR_Accepted)) {
263         debugWarning("fbCmd.getResponse() != AVCCommand::eR_Accepted\n");
264     }
265
266     return (fbCmd.getResponse() == AVCCommand::eR_Accepted);
267 }
268
269 int
270 AvDevice::getSelectorFBValue(int id) {
271
272     FunctionBlockCmd fbCmd( get1394Service(),
273                             FunctionBlockCmd::eFBT_Selector,
274                             id,
275                             FunctionBlockCmd::eCA_Current );
276     fbCmd.setNodeId( getNodeId() );
277     fbCmd.setSubunitId( 0x00 );
278     fbCmd.setCommandType( AVCCommand::eCT_Status );
279     fbCmd.m_pFBSelector->m_inputFbPlugNumber = 0xFF;
280     fbCmd.setVerboseLevel( getDebugLevel() );
281
282     if ( !fbCmd.fire() ) {
283         debugError( "cmd failed\n" );
284         return -1;
285     }
286    
287 //     if ( getDebugLevel() >= DEBUG_LEVEL_NORMAL ) {
288 //         Util::CoutSerializer se;
289 //         fbCmd.serialize( se );
290 //     }
291
292     if((fbCmd.getResponse() != AVCCommand::eR_Implemented)) {
293         debugWarning("fbCmd.getResponse() != AVCCommand::eR_Implemented\n");
294     }
295    
296     return fbCmd.m_pFBSelector->m_inputFbPlugNumber;
297 }
298
299 bool
300 AvDevice::setFeatureFBVolumeCurrent(int id, int channel, int v) {
301
302     FunctionBlockCmd fbCmd( get1394Service(),
303                             FunctionBlockCmd::eFBT_Feature,
304                             id,
305                             FunctionBlockCmd::eCA_Current );
306     fbCmd.setNodeId( getNodeId() );
307     fbCmd.setSubunitId( 0x00 );
308     fbCmd.setCommandType( AVCCommand::eCT_Control );
309     fbCmd.m_pFBFeature->m_audioChannelNumber = channel;
310     fbCmd.m_pFBFeature->m_controlSelector = FunctionBlockFeature::eCSE_Feature_Volume;
311     fbCmd.m_pFBFeature->m_pVolume->m_volume = v;
312     fbCmd.setVerboseLevel( getDebugLevel() );
313
314     if ( !fbCmd.fire() ) {
315         debugError( "cmd failed\n" );
316         return false;
317     }
318
319 //     if ( getDebugLevel() >= DEBUG_LEVEL_NORMAL ) {
320 //         Util::CoutSerializer se;
321 //         fbCmd.serialize( se );
322 //     }
323    
324     if((fbCmd.getResponse() != AVCCommand::eR_Accepted)) {
325         debugWarning("fbCmd.getResponse() != AVCCommand::eR_Accepted\n");
326     }
327
328     return (fbCmd.getResponse() == AVCCommand::eR_Accepted);
329 }
330
331 int
332 AvDevice::getFeatureFBVolumeValue(int id, int channel, FunctionBlockCmd::EControlAttribute controlAttribute)
333 {
334     FunctionBlockCmd fbCmd( get1394Service(),
335                             FunctionBlockCmd::eFBT_Feature,
336                             id,
337                             controlAttribute);
338     fbCmd.setNodeId( getNodeId() );
339     fbCmd.setSubunitId( 0x00 );
340     fbCmd.setCommandType( AVCCommand::eCT_Status );
341     fbCmd.m_pFBFeature->m_audioChannelNumber = channel;
342     fbCmd.m_pFBFeature->m_controlSelector = FunctionBlockFeature::eCSE_Feature_Volume;
343     fbCmd.m_pFBFeature->m_pVolume->m_volume = 0;
344     fbCmd.setVerboseLevel( getDebugLevel() );
345
346     if ( !fbCmd.fire() ) {
347         debugError( "cmd failed\n" );
348         return 0;
349     }
350    
351 //     if ( getDebugLevel() >= DEBUG_LEVEL_NORMAL ) {
352 //         Util::CoutSerializer se;
353 //         fbCmd.serialize( se );
354 //     }
355
356     if((fbCmd.getResponse() != AVCCommand::eR_Implemented)) {
357         debugWarning("fbCmd.getResponse() != AVCCommand::eR_Implemented\n");
358     }
359    
360     int16_t volume=(int16_t)(fbCmd.m_pFBFeature->m_pVolume->m_volume);
361    
362     return volume;
363 }
364
365 int
366 AvDevice::getFeatureFBVolumeMinimum(int id, int channel)
367 {
368     return getFeatureFBVolumeValue(id, channel, AVC::FunctionBlockCmd::eCA_Minimum);
369 }
370
371 int
372 AvDevice::getFeatureFBVolumeMaximum(int id, int channel)
373 {
374     return getFeatureFBVolumeValue(id, channel, AVC::FunctionBlockCmd::eCA_Maximum);
375 }
376
377 int
378 AvDevice::getFeatureFBVolumeCurrent(int id, int channel)
379 {
380     return getFeatureFBVolumeValue(id, channel, AVC::FunctionBlockCmd::eCA_Current);   
381 }
382
383 void
384 AvDevice::showDevice()
385 {
386     debugOutput(DEBUG_LEVEL_NORMAL, "Device is a BeBoB device\n");
387     GenericAVC::AvDevice::showDevice();
388     flushDebugOutput();
389 }
390
391 void
392 AvDevice::setVerboseLevel(int l)
393 {
394     if (m_Mixer) m_Mixer->setVerboseLevel( l );
395     GenericAVC::AvDevice::setVerboseLevel( l );
396     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting verbose level to %d...\n", l );
397 }
398
399 AVC::Subunit*
400 AvDevice::createSubunit(AVC::Unit& unit,
401                         AVC::ESubunitType type,
402                         AVC::subunit_t id )
403 {
404     AVC::Subunit* s=NULL;
405     switch (type) {
406         case eST_Audio:
407             s=new BeBoB::SubunitAudio(unit, id );
408             break;
409         case eST_Music:
410             s=new BeBoB::SubunitMusic(unit, id );
411             break;
412         default:
413             s=NULL;
414             break;
415     }
416     if(s) s->setVerboseLevel(getDebugLevel());
417     return s;
418 }
419
420
421 AVC::Plug *
422 AvDevice::createPlug( AVC::Unit* unit,
423                       AVC::Subunit* subunit,
424                       AVC::function_block_type_t functionBlockType,
425                       AVC::function_block_type_t functionBlockId,
426                       AVC::Plug::EPlugAddressType plugAddressType,
427                       AVC::Plug::EPlugDirection plugDirection,
428                       AVC::plug_id_t plugId,
429                       int globalId )
430 {
431
432     Plug *p= new BeBoB::Plug( unit,
433                               subunit,
434                               functionBlockType,
435                               functionBlockId,
436                               plugAddressType,
437                               plugDirection,
438                               plugId,
439                               globalId );
440     if (p) p->setVerboseLevel(getDebugLevel());
441     return p;
442 }
443
444 bool
445 AvDevice::propagatePlugInfo() {
446     // we don't have to propagate since we discover things
447     // another way
448     debugOutput(DEBUG_LEVEL_VERBOSE, "Skip plug info propagation\n");
449     return true;
450 }
451
452 uint8_t
453 AvDevice::getConfigurationIdSampleRate()
454 {
455     ExtendedStreamFormatCmd extStreamFormatCmd( get1394Service() );
456     UnitPlugAddress unitPlugAddress( UnitPlugAddress::ePT_PCR, 0 );
457     extStreamFormatCmd.setPlugAddress( PlugAddress( PlugAddress::ePD_Input,
458                                                     PlugAddress::ePAM_Unit,
459                                                     unitPlugAddress ) );
460
461     extStreamFormatCmd.setNodeId( getNodeId() );
462     extStreamFormatCmd.setCommandType( AVCCommand::eCT_Status );
463     extStreamFormatCmd.setVerbose( getDebugLevel() );
464
465     if ( !extStreamFormatCmd.fire() ) {
466         debugError( "Stream format command failed\n" );
467         return 0;
468     }
469
470     FormatInformation* formatInfo =
471         extStreamFormatCmd.getFormatInformation();
472     FormatInformationStreamsCompound* compoundStream
473         = dynamic_cast< FormatInformationStreamsCompound* > (
474             formatInfo->m_streams );
475     if ( compoundStream ) {
476         debugOutput(DEBUG_LEVEL_VERBOSE, "Sample rate 0x%02x\n",
477                     compoundStream->m_samplingFrequency );
478         return compoundStream->m_samplingFrequency;
479     }
480
481     debugError( "Could not retrieve sample rate\n" );
482     return 0;
483 }
484
485 uint8_t
486 AvDevice::getConfigurationIdNumberOfChannel( PlugAddress::EPlugDirection ePlugDirection )
487 {
488     ExtendedPlugInfoCmd extPlugInfoCmd( get1394Service() );
489     UnitPlugAddress unitPlugAddress( UnitPlugAddress::ePT_PCR,
490                                      0 );
491     extPlugInfoCmd.setPlugAddress( PlugAddress( ePlugDirection,
492                                                 PlugAddress::ePAM_Unit,
493                                                 unitPlugAddress ) );
494     extPlugInfoCmd.setNodeId( getNodeId() );
495     extPlugInfoCmd.setCommandType( AVCCommand::eCT_Status );
496     extPlugInfoCmd.setVerbose( getDebugLevel() );
497     ExtendedPlugInfoInfoType extendedPlugInfoInfoType(
498         ExtendedPlugInfoInfoType::eIT_NoOfChannels );
499     extendedPlugInfoInfoType.initialize();
500     extPlugInfoCmd.setInfoType( extendedPlugInfoInfoType );
501
502     if ( !extPlugInfoCmd.fire() ) {
503         debugError( "Number of channels command failed\n" );
504         return 0;
505     }
506
507     ExtendedPlugInfoInfoType* infoType = extPlugInfoCmd.getInfoType();
508     if ( infoType
509          && infoType->m_plugNrOfChns )
510     {
511         debugOutput(DEBUG_LEVEL_VERBOSE, "Number of channels 0x%02x\n",
512                     infoType->m_plugNrOfChns->m_nrOfChannels );
513         return infoType->m_plugNrOfChns->m_nrOfChannels;
514     }
515
516     debugError( "Could not retrieve number of channels\n" );
517     return 0;
518 }
519
520 uint16_t
521 AvDevice::getConfigurationIdSyncMode()
522 {
523     SignalSourceCmd signalSourceCmd( get1394Service() );
524     SignalUnitAddress signalUnitAddr;
525     signalUnitAddr.m_plugId = 0x01;
526     signalSourceCmd.setSignalDestination( signalUnitAddr );
527     signalSourceCmd.setNodeId( getNodeId() );
528     signalSourceCmd.setSubunitType( eST_Unit  );
529     signalSourceCmd.setSubunitId( 0xff );
530     signalSourceCmd.setVerbose( getDebugLevel() );
531
532     signalSourceCmd.setCommandType( AVCCommand::eCT_Status );
533
534     if ( !signalSourceCmd.fire() ) {
535         debugError( "Signal source command failed\n" );
536         return 0;
537     }
538
539     SignalAddress* pSyncPlugSignalAddress = signalSourceCmd.getSignalSource();
540     SignalSubunitAddress* pSyncPlugSubunitAddress
541         = dynamic_cast<SignalSubunitAddress*>( pSyncPlugSignalAddress );
542     if ( pSyncPlugSubunitAddress ) {
543         debugOutput(DEBUG_LEVEL_VERBOSE, "Sync mode 0x%02x\n",
544                     ( pSyncPlugSubunitAddress->m_subunitType << 3
545                       | pSyncPlugSubunitAddress->m_subunitId ) << 8
546                     | pSyncPlugSubunitAddress->m_plugId );
547
548         return ( pSyncPlugSubunitAddress->m_subunitType << 3
549                  | pSyncPlugSubunitAddress->m_subunitId ) << 8
550             | pSyncPlugSubunitAddress->m_plugId;
551     }
552
553     SignalUnitAddress* pSyncPlugUnitAddress
554       = dynamic_cast<SignalUnitAddress*>( pSyncPlugSignalAddress );
555     if ( pSyncPlugUnitAddress ) {
556         debugOutput(DEBUG_LEVEL_VERBOSE, "Sync mode 0x%02x\n",
557                       0xff << 8 | pSyncPlugUnitAddress->m_plugId );
558
559         return ( 0xff << 8 | pSyncPlugUnitAddress->m_plugId );
560     }
561
562     debugError( "Could not retrieve sync mode\n" );
563     return 0;
564 }
565
566 uint64_t
567 AvDevice::getConfigurationId()
568 {
569     // create a unique configuration id.
570     uint64_t id = 0;
571     id = getConfigurationIdSampleRate();
572     id |= getConfigurationIdNumberOfChannel( PlugAddress::ePD_Input ) << 8;
573     id |= getConfigurationIdNumberOfChannel( PlugAddress::ePD_Output ) << 16;
574     id |= ((uint64_t)getConfigurationIdSyncMode()) << 24;
575     return id;
576 }
577
578 bool
579 AvDevice::serialize( std::string basePath,
580                      Util::IOSerialize& ser ) const
581 {
582     bool result;
583     result  = GenericAVC::AvDevice::serialize( basePath, ser );
584     return result;
585 }
586
587 bool
588 AvDevice::deserialize( std::string basePath,
589                        Util::IODeserialize& deser )
590 {
591     bool result;
592     result  = GenericAVC::AvDevice::deserialize( basePath, deser );
593     return result;
594 }
595
596 std::string
597 AvDevice::getCachePath()
598 {
599     std::string cachePath;
600     char* pCachePath;
601
602     string path = CACHEDIR;
603     if ( path.size() && path[0] == '~' ) {
604         path.erase( 0, 1 ); // remove ~
605         path.insert( 0, getenv( "HOME" ) ); // prepend the home path
606     }
607
608     if ( asprintf( &pCachePath, "%s/cache/",  path.c_str() ) < 0 ) {
609         debugError( "Could not create path string for cache pool (trying '/var/cache/libffado' instead)\n" );
610         cachePath == "/var/cache/libffado/";
611     } else {
612         cachePath = pCachePath;
613         free( pCachePath );
614     }
615     return cachePath;
616 }
617
618 bool
619 AvDevice::loadFromCache()
620 {
621     std::string sDevicePath = getCachePath() + getConfigRom().getGuidString();
622
623     char* configId;
624     asprintf(&configId, "%016llx", getConfigurationId() );
625     if ( !configId ) {
626         debugError( "could not create id string\n" );
627         return false;
628     }
629
630     std::string sFileName = sDevicePath + "/" + configId + ".xml";
631     free( configId );
632     debugOutput( DEBUG_LEVEL_NORMAL, "filename %s\n", sFileName.c_str() );
633
634     struct stat buf;
635     if ( stat( sFileName.c_str(), &buf ) != 0 ) {
636         debugOutput( DEBUG_LEVEL_NORMAL,  "\"%s\" does not exist\n",  sFileName.c_str() );
637         return false;
638     } else {
639         if ( !S_ISREG( buf.st_mode ) ) {
640             debugOutput( DEBUG_LEVEL_NORMAL,  "\"%s\" is not a regular file\n",  sFileName.c_str() );
641             return false;
642         }
643     }
644
645     Util::XMLDeserialize deser( sFileName, getDebugLevel() );
646
647     if (!deser.isValid()) {
648         debugOutput( DEBUG_LEVEL_NORMAL, "cache not valid: %s\n",
649                      sFileName.c_str() );
650         return false;
651     }
652
653     bool result = deserialize( "", deser );
654     if ( result ) {
655         debugOutput( DEBUG_LEVEL_NORMAL, "could create valid bebob driver from %s\n",
656                      sFileName.c_str() );
657     }
658
659     if(result) {
660         buildMixer();
661     }
662
663     return result;
664 }
665
666 bool
667 AvDevice::saveCache()
668 {
669     // the path looks like this:
670     // PATH_TO_CACHE + GUID + CONFIGURATION_ID
671     string tmp_path = getCachePath() + getConfigRom().getGuidString();
672
673     // the following piece should do something like
674     // 'mkdir -p some/path/with/some/dirs/which/do/not/exist'
675     vector<string> tokens;
676     tokenize( tmp_path, tokens, "/" );
677     string path;
678     for ( vector<string>::const_iterator it = tokens.begin();
679           it != tokens.end();
680           ++it )
681     {
682         path +=  "/" + *it;
683
684         struct stat buf;
685         if ( stat( path.c_str(), &buf ) == 0 ) {
686             if ( !S_ISDIR( buf.st_mode ) ) {
687                 debugError( "\"%s\" is not a directory\n",  path.c_str() );
688                 return false;
689             }
690         } else {
691             if (  mkdir( path.c_str(), S_IRWXU | S_IRWXG ) != 0 ) {
692                 debugError( "Could not create \"%s\" directory\n", path.c_str() );
693                 return false;
694             }
695         }
696     }
697
698     // come up with an unique file name for the current settings
699     char* configId;
700     asprintf(&configId, "%016llx", BeBoB::AvDevice::getConfigurationId() );
701     if ( !configId ) {
702         debugError( "Could not create id string\n" );
703         return false;
704     }
705     string filename = path + "/" + configId + ".xml";
706     free( configId );
707     debugOutput( DEBUG_LEVEL_NORMAL, "filename %s\n", filename.c_str() );
708
709     Util::XMLSerialize ser( filename );
710     return serialize( "", ser );
711 }
712
713 } // end of namespace
Note: See TracBrowser for help on using the browser.