summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp328
1 files changed, 120 insertions, 208 deletions
diff --git a/noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp b/noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp
index 24e4b7b..9d4ae97 100644
--- a/noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp
+++ b/noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp
@@ -4,246 +4,158 @@
4#include <resources.h> 4#include <resources.h>
5#include "bluetoothBNEPrun.h" 5#include "bluetoothBNEPrun.h"
6 6
7QDict<QString> * BluetoothBNEPRun::PANConnections = 0; 7BluetoothBNEPRun::BluetoothBNEPRun( ANetNodeInstance * NNI,
8 8 BluetoothBNEPData & D ) :
9void BluetoothBNEPRun::detectState( NodeCollection * NC ) { 9 RuntimeInfo( NNI ),
10 // unavailable : no card found 10 Data( D),
11 // available : card found and assigned to us or free 11 Pat( "bnep[0-6]" ) {
12 // up : card found and assigned to us and up 12 OT = 0;
13 QString S = QString( "/tmp/profile-%1.up" ).arg(NC->number());
14 System & Sys = NSResources->system();
15 InterfaceInfo * Run;
16 QFile F( S );
17
18 Log(("Detecting for %s\n", NC->name().latin1() ));
19
20 if( F.open( IO_ReadOnly ) ) {
21 // could open file -> read interface and assign
22 QString X;
23 bool accepted = 0;
24 QTextStream TS(&F);
25 X = TS.readLine();
26 Log(("%s exists : %s\n", S.latin1(), X.latin1() ));
27 // find interface
28 if( handlesInterface( X ) ) {
29
30 Log(("Handles interface %s, PANC %p\n", X.latin1(), PANConnections ));
31 if( PANConnections == 0 ) {
32 // load connections that are active
33 // format : bnep0 00:60:57:02:71:A2 PANU
34 FILE * OutputOfCmd = popen( "pand --show", "r" ) ;
35
36 PANConnections = new QDict<QString>;
37
38 if( OutputOfCmd ) {
39 char ch;
40 // could fork
41 // read all data
42 QString Line = "";
43 while( 1 ) {
44 if( fread( &ch, 1, 1, OutputOfCmd ) < 1 ) {
45 // eof
46 break;
47 }
48 if( ch == '\n' || ch == '\r' ) {
49 if( ! Line.isEmpty() ) {
50 if( Line.startsWith( "bnep" ) ) {
51 QStringList SL = QStringList::split( " ", Line );
52 Log(("Detected PAN %s %s\n",
53 SL[0].latin1(), SL[1].latin1() ));
54 PANConnections->insert( SL[0], new QString(SL[1]));
55 }
56 Line="";
57 }
58 } else {
59 Line += ch;
60 } 13 }
14
15BluetoothBNEPRun::~BluetoothBNEPRun( void ) {
16 if( OT ) {
17 OTGateway::releaseOTGateway();
61 } 18 }
62 } 19 }
63 20
64 pclose( OutputOfCmd ); 21State_t BluetoothBNEPRun::detectState( void ) {
65 }
66 22
67 // check if this runtime allows connection to node 23 /*
68 if( ! Data.AllowAll ) {
69 // has addresses
70 for ( QStringList::Iterator it = Data.BDAddress.begin();
71 ! accepted && it != Data.BDAddress.end();
72 ++ it ) {
73 for( QDictIterator<QString> it2( *(PANConnections) );
74 it2.current();
75 ++ it2 ) {
76 if( X == it2.currentKey() &&
77 (*it) == *(it2.current())
78 ) {
79 // found
80 Log(("%s accepts connections to %s\n",
81 NC->name().latin1(),
82 it2.current()->latin1() ));
83 accepted = 1;
84 break;
85 }
86 }
87 }
88 } else {
89 Log(("%s accepts any connection\n", NC->name().latin1() ));
90 // accept any
91 accepted = 1;
92 }
93 24
94 if( accepted ) { 25 need to detect
95 // matches and is allowed for this node
96 for( QDictIterator<InterfaceInfo> It(Sys.interfaces());
97 It.current();
98 ++It ) {
99 Run = It.current();
100 if( X == Run->Name ) {
101 Log(("%s Assigned %p\n", NC->name().latin1(), Run ));
102 Run->assignNode( netNode() );
103 assignInterface( Run );
104 NC->setCurrentState( IsUp );
105 return;
106 }
107 }
108 }
109 }
110 }
111 26
112 Log(("Assigned %p\n", assignedInterface() )); 27 1. for any PAN connection that is found if that
113 if( ( Run = assignedInterface() ) ) { 28 PAN is connected.
114 // we already have an interface assigned -> still present ? 29
115 if( ! Run->IsUp ) { 30 if it is connected it is not available (since we do
116 // usb is still free -> keep assignment 31 not manage IP settings and we are called to detect
117 NC->setCurrentState( Available ); 32 the state we knwo that we do not have an UP connection)
118 return; 33
119 } // else interface is up but NOT us -> some other profile 34 2. if it not connected and we allow any connection we
120 } 35 are available or if that PAN connection is to a device
121 36 with a correct address
122 // nothing (valid) assigned to us 37
123 assignInterface( 0 ); 38 3. if it is not connected and the address do not match or
124 39 we do not accept any address, we are Unavailable but
125 // find possible interface 40 not DOWN. I.e a new connection could perhaps be created
126 for( QDictIterator<InterfaceInfo> It(Sys.interfaces()); 41
127 It.current(); 42 */
128 ++It ) { 43
129 Run = It.current(); 44 if( ! OT ) {
130 45 OT = OTGateway::getOTGateway();
131 Log(("%s %d %d=%d %d\n",
132 Run->Name.latin1(),
133 handlesInterface( Run->Name ),
134 Run->CardType, ARPHRD_ETHER,
135 ! Run->IsUp ));
136
137 if( handlesInterface( Run->Name ) &&
138 Run->CardType == ARPHRD_ETHER &&
139 ! Run->IsUp
140 ) {
141 Log(("Released(OFF)\n" ));
142 // proper type, and Not UP -> free
143 NC->setCurrentState( Off );
144 return;
145 } 46 }
47
48 if( ! OT->isEnabled() ) {
49 return Unavailable;
146 } 50 }
147 // no free found
148 Log(("None available\n" ));
149 51
150 NC->setCurrentState( Unavailable ); 52 // if there is a PAN connection that is UP but not
53 // yet configured (no ifup) the we are available
54 return ( hasFreePANConnection() ) ? Available : Unknown;
151} 55}
152 56
153bool BluetoothBNEPRun::setState( NodeCollection * NC, Action_t A, bool ) { 57QString BluetoothBNEPRun::setMyState( NodeCollection * NC, Action_t A, bool ) {
154 58
155 // we only handle activate and deactivate 59 if( A == Activate ) {
156 switch( A ) { 60 if( hasFreePANConnection( 1 ) ) {
157 case Activate : 61 // we have now an assignedinterface
158 { 62 } else {
159 if( NC->currentState() != Off ) { 63 return QString("TODO : Start PAND");
160 return 0;
161 }
162 InterfaceInfo * N = getInterface();
163 if( ! N ) {
164 // no interface available
165 NC->setCurrentState( Unavailable );
166 return 0;
167 } 64 }
168 // because we were OFF the interface 65
169 // we get back is NOT assigned 66 Log(( "Assigned interface" ));
170 N->assignNode( netNode() );
171 assignInterface( N );
172 Log(("Assing %p\n", N ));
173 NC->setCurrentState( Available ); 67 NC->setCurrentState( Available );
174 return 1; 68
175 } 69 return QString();
176 case Deactivate :
177 if( NC->currentState() == IsUp ) {
178 // bring down first
179 if( ! connection()->setState( Down ) )
180 // could not ...
181 return 0;
182 } else if( NC->currentState() != Available ) {
183 return 1;
184 } 70 }
185 assignedInterface()->assignNode( 0 ); // release 71
186 assignInterface( 0 ); 72 if( A == Deactivate ) {
73 // nothing to do
187 NC->setCurrentState( Off ); 74 NC->setCurrentState( Off );
188 return 1; 75 return QString();
189 default :
190 // FT
191 break;
192 } 76 }
193 return 0; 77 return QString();
194} 78}
195 79
196bool BluetoothBNEPRun::canSetState( State_t Curr , Action_t A ) { 80bool BluetoothBNEPRun::handlesInterface( const QString & S ) {
197 // we only handle up down activate and deactivate 81 return Pat.match( S ) >= 0;
198 switch( A ) {
199 case Activate :
200 { // at least available
201 if( Curr == Available ) {
202 return 1;
203 } 82 }
204 // or we can make one available 83
205 InterfaceInfo * N = getInterface(); 84bool BluetoothBNEPRun::handlesInterface( InterfaceInfo * I ) {
206 if( ! N || N->assignedNode() != 0 ) { 85 return handlesInterface( I->Name );
207 // non available or assigned
208 return 0;
209 } 86 }
210 return 1; 87
88bool BluetoothBNEPRun::hasFreePANConnection( bool Grab ) {
89
90 if( ! OT ) {
91 OT = OTGateway::getOTGateway();
211 } 92 }
212 case Deactivate : 93
213 return ( Curr >= Available ); 94 // load PAN connections
214 default : 95 OTPANConnection * C;
215 // FT 96 InterfaceInfo * Run;
97 InterfaceInfo * Candidate = 0; // reuse this interface
98 PANConnectionVector Conns = OT->getPANConnections();
99 System & Sys = NSResources->system();
100 bool IsValid;
101
102 for( unsigned int i = 0;
103 i < Conns.count();
104 i ++ ) {
105 C = Conns[i];
106
107 if( Data.AllowAll ) {
108 // we allow all
109 IsValid = 1;
110 } else {
111 // is this PAN connection connecting to a Peer
112 // we allow ?
113 IsValid = 0;
114 for ( QStringList::Iterator it = Data.BDAddress.begin();
115 it != Data.BDAddress.end();
116 ++ it ) {
117 if( C->ConnectedTo == (*it) ) {
118 // this is a connection we could accept
119 IsValid = 1;
216 break; 120 break;
217 } 121 }
218 return 0; 122 }
219} 123}
220 124
221// get interface that is free or assigned to us 125 if( ! IsValid ) {
222InterfaceInfo * BluetoothBNEPRun::getInterface( void ) { 126 Log(("%s to %s not acceptable\n",
127 C->Device.latin1(),
128 C->ConnectedTo.latin1() ));
129 // don't bother checking this address
130 // it is not acceptable
131 continue;
132 }
223 133
224 System & S = NSResources->system(); 134 // is this PAN connection available to us ?
225 InterfaceInfo * best = 0, * Run; 135 Run = Sys.findInterface( C->Device );
226 136
227 for( QDictIterator<InterfaceInfo> It(S.interfaces()); 137 if( Run && Run->IsUp ) {
228 It.current(); 138 // this PAN connection is up
229 ++It ) { 139 Log(("%s acceptable but unavailable\n",
230 Run = It.current(); 140 C->Device.latin1() ));
231 if( handlesInterface( Run->Name ) && 141 // find others
232 Run->CardType == ARPHRD_ETHER 142 continue;
233 ) {
234 // this is a bluetooth card
235 if( Run->assignedNode() == netNode() ) {
236 // assigned to us
237 return Run;
238 } else if( Run->assignedNode() == 0 ) {
239 // free
240 best = Run;
241 } 143 }
144
145 // we at least have a possible interface
146 if( ! Candidate ) {
147 Candidate = Run;
242 } 148 }
243 } 149 }
244 return best; // can be 0 150
151 if( Candidate ) {
152 if ( Grab ) {
153 netNode()->connection()->assignInterface( Candidate );
154 }
155 return 1;
245} 156}
246 157
247bool BluetoothBNEPRun::handlesInterface( const QString & S ) { 158 // no free PAN
248 return Pat.match( S ) >= 0; 159 return 0;
249} 160}
161