Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

usbrun.cpp

Go to the documentation of this file.
00001 #include <qfile.h>
00002 #include <qfileinfo.h>
00003 #include <qtextstream.h>
00004 #include <resources.h>
00005 #include "usbrun.h"
00006 
00007 State_t USBRun::detectState( void ) {
00008 
00009     // unavailable : no card found
00010     // available : card found and assigned to us or free
00011     // up : card found and assigned to us and up
00012 
00013     NetworkSetup * NC = networkSetup();
00014     QString S = QString( "/tmp/profile-%1.up" ).arg(NC->number());
00015     System & Sys = NSResources->system();
00016     InterfaceInfo * Run;
00017     QFile F( S );
00018     Log(("Detecting for %s\n", NC->name().latin1() ));
00019 
00020     if( F.open( IO_ReadOnly ) ) {
00021       // could open file -> read interface and assign
00022       QString X;
00023       QTextStream TS(&F);
00024       X = TS.readLine();
00025       Log(("%s exists\n", S.latin1() ));
00026       // find interface
00027       if( handlesInterface( X ) ) {
00028         for( QDictIterator<InterfaceInfo> It(Sys.interfaces());
00029              It.current();
00030              ++It ) {
00031           Run = It.current();
00032           if( X == Run->Name ) {
00033             NC->assignInterface( Run );
00034             return (Run->IsUp) ? IsUp : Available;
00035           }
00036         }
00037       }
00038     } 
00039 
00040     Log(("Assigned %p\n", NC->assignedInterface() ));
00041 
00042     if( ( Run = NC->assignedInterface() ) ) {
00043       // we already have an interface assigned -> still present ?
00044       if( ! Run->IsUp ) {
00045         // usb is still free -> keep assignment
00046         return Available;
00047       } // else interface is up but NOT us -> some other profile
00048     }
00049 
00050     // nothing (valid) assigned to us
00051     NC->assignInterface( 0 );
00052 
00053     // find possible interface
00054     if( getInterface() ) {
00055       // proper type, and Not UP -> free
00056       // usb cables are currently always available when requested
00057       // until we can detect if we are plugged in
00058       return Available;
00059     }
00060 
00061     return Unavailable;
00062 }
00063 
00064 QString USBRun::setMyState( NetworkSetup * NC, Action_t, bool ) {
00065 
00066     InterfaceInfo * I = getInterface();
00067 
00068     if( ! I ) {
00069       return QString("No usb device available");
00070     }
00071 
00072     Log(( "Grabbed USB interface %s\n", I->Name.latin1() ));
00073     // grab this interface
00074     NC->assignInterface( I );
00075 
00076     return QString();
00077 }
00078 
00079 // get interface that is free or assigned to us
00080 InterfaceInfo * USBRun::getInterface( void ) {
00081 
00082     System & S = NSResources->system();
00083     InterfaceInfo * best = 0, * Run;
00084 
00085     for( QDictIterator<InterfaceInfo> It(S.interfaces());
00086          It.current();
00087          ++It ) {
00088       Run = It.current();
00089 
00090       Log(("%s %d %d=%d %d\n",
00091           Run->Name.latin1(),
00092           handlesInterface( Run->Name ),
00093               Run->CardType, ARPHRD_ETHER,
00094               ! Run->IsUp ));
00095 
00096       if( handlesInterface( Run->Name ) &&
00097           Run->CardType == ARPHRD_ETHER
00098         ) {
00099         // this is a USB card
00100         if( Run->assignedToNetworkSetup() == netNode()->networkSetup() ) {
00101           // assigned to us
00102           return Run;
00103         } else if( ! Run->IsUp &&
00104                    Run->assignedToNetworkSetup() == 0 ) {
00105           // free
00106           best = Run;
00107         } // UP or not assigned to us
00108       }
00109     }
00110     return best; // can be 0
00111 }
00112 
00113 bool USBRun::handlesInterface( const QString & S ) {
00114     return Pat.match( S ) >= 0;
00115 }
00116 
00117 bool USBRun::handlesInterface( InterfaceInfo * I ) {
00118     return handlesInterface( I->Name );
00119 }
00120 

Generated on Sat Nov 5 16:17:56 2005 for OPIE by  doxygen 1.4.2