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
00010
00011
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
00022 QString X;
00023 QTextStream TS(&F);
00024 X = TS.readLine();
00025 Log(("%s exists\n", S.latin1() ));
00026
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
00044 if( ! Run->IsUp ) {
00045
00046 return Available;
00047 }
00048 }
00049
00050
00051 NC->assignInterface( 0 );
00052
00053
00054 if( getInterface() ) {
00055
00056
00057
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
00074 NC->assignInterface( I );
00075
00076 return QString();
00077 }
00078
00079
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
00100 if( Run->assignedToNetworkSetup() == netNode()->networkSetup() ) {
00101
00102 return Run;
00103 } else if( ! Run->IsUp &&
00104 Run->assignedToNetworkSetup() == 0 ) {
00105
00106 best = Run;
00107 }
00108 }
00109 }
00110 return best;
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