00001 #include <qfile.h> 00002 #include <qfileinfo.h> 00003 #include <qtextstream.h> 00004 #include <resources.h> 00005 #include "bluetoothBNEPrun.h" 00006 00007 using Opietooth2::OTGateway; 00008 using Opietooth2::OTPANNetworkSetup; 00009 using Opietooth2::PANNetworkSetupVector; 00010 00011 BluetoothBNEPRun::BluetoothBNEPRun( ANetNodeInstance * NNI, 00012 BluetoothBNEPData & D ) : 00013 RuntimeInfo( NNI ), 00014 Data( D), 00015 Pat( "bnep[0-6]" ) { 00016 OT = 0; 00017 } 00018 00019 BluetoothBNEPRun::~BluetoothBNEPRun( void ) { 00020 if( OT ) { 00021 OTGateway::releaseOTGateway(); 00022 } 00023 } 00024 00025 State_t BluetoothBNEPRun::detectState( void ) { 00026 00027 /* 00028 00029 need to detect 00030 00031 1. for any PAN NetworkSetup that is found if that 00032 PAN is connected. 00033 00034 if it is connected it is not available (since we do 00035 not manage IP settings and we are called to detect 00036 the state we knwo that we do not have an UP NetworkSetup) 00037 00038 2. if it not connected and we allow any NetworkSetup we 00039 are available or if that PAN NetworkSetup is to a device 00040 with a correct address 00041 00042 3. if it is not connected and the address do not match or 00043 we do not accept any address, we are Unavailable but 00044 not DOWN. I.e a new NetworkSetup could perhaps be created 00045 00046 */ 00047 00048 if( ! OT ) { 00049 OT = OTGateway::getOTGateway(); 00050 } 00051 00052 if( ! OT->isEnabled() ) { 00053 return Unavailable; 00054 } 00055 00056 // if there is a PAN NetworkSetup that is UP but not 00057 // yet configured (no ifup) the we are available 00058 return ( hasFreePANNetworkSetup() ) ? Available : Unknown; 00059 } 00060 00061 QString BluetoothBNEPRun::setMyState( NetworkSetup * NC, Action_t A, bool ) { 00062 00063 if( A == Activate ) { 00064 if( hasFreePANNetworkSetup( 1 ) ) { 00065 // we have now an assignedinterface 00066 } else { 00067 return QString("TODO : Start PAND"); 00068 } 00069 00070 Log(( "Assigned interface" )); 00071 NC->setCurrentState( Available ); 00072 00073 return QString(); 00074 } 00075 00076 if( A == Deactivate ) { 00077 // nothing to do 00078 NC->setCurrentState( Off ); 00079 return QString(); 00080 } 00081 return QString(); 00082 } 00083 00084 bool BluetoothBNEPRun::handlesInterface( const QString & S ) { 00085 return Pat.match( S ) >= 0; 00086 } 00087 00088 bool BluetoothBNEPRun::handlesInterface( InterfaceInfo * I ) { 00089 return handlesInterface( I->Name ); 00090 } 00091 00092 bool BluetoothBNEPRun::hasFreePANNetworkSetup( bool Grab ) { 00093 00094 if( ! OT ) { 00095 OT = OTGateway::getOTGateway(); 00096 } 00097 00098 // load PAN NetworkSetups 00099 OTPANNetworkSetup * C; 00100 InterfaceInfo * Run; 00101 InterfaceInfo * Candidate = 0; // reuse this interface 00102 PANNetworkSetupVector Conns = OT->getPANNetworkSetups(); 00103 System & Sys = NSResources->system(); 00104 bool IsValid; 00105 00106 for( unsigned int i = 0; 00107 i < Conns.count(); 00108 i ++ ) { 00109 C = Conns[i]; 00110 00111 if( Data.AllowAll ) { 00112 // we allow all 00113 IsValid = 1; 00114 } else { 00115 // is this PAN NetworkSetup connecting to a Peer 00116 // we allow ? 00117 IsValid = 0; 00118 for ( QStringList::Iterator it = Data.BDAddress.begin(); 00119 it != Data.BDAddress.end(); 00120 ++ it ) { 00121 if( C->ConnectedTo == (*it) ) { 00122 // this is a NetworkSetup we could accept 00123 IsValid = 1; 00124 break; 00125 } 00126 } 00127 } 00128 00129 if( ! IsValid ) { 00130 Log(("%s to %s not acceptable\n", 00131 C->Device.latin1(), 00132 C->ConnectedTo.latin1() )); 00133 // don't bother checking this address 00134 // it is not acceptable 00135 continue; 00136 } 00137 00138 // is this PAN NetworkSetup available to us ? 00139 Run = Sys.findInterface( C->Device ); 00140 00141 if( Run && Run->IsUp ) { 00142 // this PAN NetworkSetup is up 00143 Log(("%s acceptable but unavailable\n", 00144 C->Device.latin1() )); 00145 // find others 00146 continue; 00147 } 00148 00149 // we at least have a possible interface 00150 if( ! Candidate ) { 00151 Candidate = Run; 00152 } 00153 } 00154 00155 if( Candidate ) { 00156 if ( Grab ) { 00157 netNode()->networkSetup()->assignInterface( Candidate ); 00158 } 00159 return 1; 00160 } 00161 00162 // no free PAN 00163 return 0; 00164 } 00165
1.4.2