summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp249
1 files changed, 249 insertions, 0 deletions
diff --git a/noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp b/noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp
new file mode 100644
index 0000000..24e4b7b
--- a/dev/null
+++ b/noncore/settings/networksettings2/bluetooth/bluetoothBNEPrun.cpp
@@ -0,0 +1,249 @@
1#include <qfile.h>
2#include <qfileinfo.h>
3#include <qtextstream.h>
4#include <resources.h>
5#include "bluetoothBNEPrun.h"
6
7QDict<QString> * BluetoothBNEPRun::PANConnections = 0;
8
9void BluetoothBNEPRun::detectState( NodeCollection * NC ) {
10 // unavailable : no card found
11 // available : card found and assigned to us or free
12 // up : card found and assigned to us and up
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 }
61 }
62 }
63
64 pclose( OutputOfCmd );
65 }
66
67 // check if this runtime allows connection to node
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
94 if( accepted ) {
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
112 Log(("Assigned %p\n", assignedInterface() ));
113 if( ( Run = assignedInterface() ) ) {
114 // we already have an interface assigned -> still present ?
115 if( ! Run->IsUp ) {
116 // usb is still free -> keep assignment
117 NC->setCurrentState( Available );
118 return;
119 } // else interface is up but NOT us -> some other profile
120 }
121
122 // nothing (valid) assigned to us
123 assignInterface( 0 );
124
125 // find possible interface
126 for( QDictIterator<InterfaceInfo> It(Sys.interfaces());
127 It.current();
128 ++It ) {
129 Run = It.current();
130
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 }
146 }
147 // no free found
148 Log(("None available\n" ));
149
150 NC->setCurrentState( Unavailable );
151}
152
153bool BluetoothBNEPRun::setState( NodeCollection * NC, Action_t A, bool ) {
154
155 // we only handle activate and deactivate
156 switch( A ) {
157 case Activate :
158 {
159 if( NC->currentState() != Off ) {
160 return 0;
161 }
162 InterfaceInfo * N = getInterface();
163 if( ! N ) {
164 // no interface available
165 NC->setCurrentState( Unavailable );
166 return 0;
167 }
168 // because we were OFF the interface
169 // we get back is NOT assigned
170 N->assignNode( netNode() );
171 assignInterface( N );
172 Log(("Assing %p\n", N ));
173 NC->setCurrentState( Available );
174 return 1;
175 }
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 }
185 assignedInterface()->assignNode( 0 ); // release
186 assignInterface( 0 );
187 NC->setCurrentState( Off );
188 return 1;
189 default :
190 // FT
191 break;
192 }
193 return 0;
194}
195
196bool BluetoothBNEPRun::canSetState( State_t Curr , Action_t A ) {
197 // we only handle up down activate and deactivate
198 switch( A ) {
199 case Activate :
200 { // at least available
201 if( Curr == Available ) {
202 return 1;
203 }
204 // or we can make one available
205 InterfaceInfo * N = getInterface();
206 if( ! N || N->assignedNode() != 0 ) {
207 // non available or assigned
208 return 0;
209 }
210 return 1;
211 }
212 case Deactivate :
213 return ( Curr >= Available );
214 default :
215 // FT
216 break;
217 }
218 return 0;
219}
220
221// get interface that is free or assigned to us
222InterfaceInfo * BluetoothBNEPRun::getInterface( void ) {
223
224 System & S = NSResources->system();
225 InterfaceInfo * best = 0, * Run;
226
227 for( QDictIterator<InterfaceInfo> It(S.interfaces());
228 It.current();
229 ++It ) {
230 Run = It.current();
231 if( handlesInterface( Run->Name ) &&
232 Run->CardType == ARPHRD_ETHER
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 }
242 }
243 }
244 return best; // can be 0
245}
246
247bool BluetoothBNEPRun::handlesInterface( const QString & S ) {
248 return Pat.match( S ) >= 0;
249}