Diffstat (limited to 'noncore/settings/networksettings2/usb/usbrun.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings2/usb/usbrun.cpp | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/noncore/settings/networksettings2/usb/usbrun.cpp b/noncore/settings/networksettings2/usb/usbrun.cpp new file mode 100644 index 0000000..49b5a77 --- a/dev/null +++ b/noncore/settings/networksettings2/usb/usbrun.cpp | |||
@@ -0,0 +1,171 @@ | |||
1 | #include <qfile.h> | ||
2 | #include <qfileinfo.h> | ||
3 | #include <qtextstream.h> | ||
4 | #include <resources.h> | ||
5 | #include "usbrun.h" | ||
6 | |||
7 | void USBRun::detectState( NodeCollection * NC ) { | ||
8 | // unavailable : no card found | ||
9 | // available : card found and assigned to us or free | ||
10 | // up : card found and assigned to us and up | ||
11 | QString S = QString( "/tmp/profile-%1.up" ).arg(NC->number()); | ||
12 | System & Sys = NSResources->system(); | ||
13 | InterfaceInfo * Run; | ||
14 | QFile F( S ); | ||
15 | |||
16 | if( F.open( IO_ReadOnly ) ) { | ||
17 | // could open file -> read interface and assign | ||
18 | QString X; | ||
19 | QTextStream TS(&F); | ||
20 | X = TS.readLine(); | ||
21 | // find interface | ||
22 | if( handlesInterface( X ) ) { | ||
23 | for( QDictIterator<InterfaceInfo> It(Sys.interfaces()); | ||
24 | It.current(); | ||
25 | ++It ) { | ||
26 | Run = It.current(); | ||
27 | if( X == Run->Name ) { | ||
28 | Run->assignNode( netNode() ); | ||
29 | assignInterface( Run ); | ||
30 | NC->setCurrentState( IsUp ); | ||
31 | return; | ||
32 | } | ||
33 | } | ||
34 | } | ||
35 | } | ||
36 | |||
37 | fprintf( stderr, "NUP\n" ); | ||
38 | // definitively not up | ||
39 | assignInterface( 0 ); | ||
40 | |||
41 | // find possible interface | ||
42 | for( QDictIterator<InterfaceInfo> It(Sys.interfaces()); | ||
43 | It.current(); | ||
44 | ++It ) { | ||
45 | Run = It.current(); | ||
46 | fprintf( stderr, "%s %d %d=%d %d\n", | ||
47 | Run->Name.latin1(), | ||
48 | handlesInterface( Run->Name ), | ||
49 | Run->CardType, ARPHRD_ETHER, | ||
50 | ! Run->IsUp ); | ||
51 | if( handlesInterface( Run->Name ) && | ||
52 | Run->CardType == ARPHRD_ETHER && | ||
53 | ! Run->IsUp | ||
54 | ) { | ||
55 | fprintf( stderr, "OFF\n" ); | ||
56 | // proper type, and Not UP -> free | ||
57 | NC->setCurrentState( Off ); | ||
58 | return; | ||
59 | } | ||
60 | } | ||
61 | // no free found | ||
62 | fprintf( stderr, "UNA\n" ); | ||
63 | |||
64 | NC->setCurrentState( Unavailable ); | ||
65 | } | ||
66 | |||
67 | bool USBRun::setState( NodeCollection * NC, Action_t A ) { | ||
68 | |||
69 | // we only handle activate and deactivate | ||
70 | switch( A ) { | ||
71 | case Activate : | ||
72 | { | ||
73 | if( NC->currentState() != Off ) { | ||
74 | return 0; | ||
75 | } | ||
76 | InterfaceInfo * N = getInterface(); | ||
77 | if( ! N ) { | ||
78 | // no interface available | ||
79 | NC->setCurrentState( Unavailable ); | ||
80 | return 0; | ||
81 | } | ||
82 | // because we were OFF the interface | ||
83 | // we get back is NOT assigned | ||
84 | N->assignNode( netNode() ); | ||
85 | assignInterface( N ); | ||
86 | NC->setCurrentState( Available ); | ||
87 | return 1; | ||
88 | } | ||
89 | case Deactivate : | ||
90 | if( NC->currentState() == IsUp ) { | ||
91 | // bring down first | ||
92 | if( ! connection()->setState( Down ) ) | ||
93 | // could not ... | ||
94 | return 0; | ||
95 | } | ||
96 | if( NC->currentState() != Available ) { | ||
97 | return 1; | ||
98 | } | ||
99 | assignedInterface()->assignNode( 0 ); // release | ||
100 | assignInterface( 0 ); | ||
101 | NC->setCurrentState( Off ); | ||
102 | return 1; | ||
103 | default : | ||
104 | // FT | ||
105 | break; | ||
106 | } | ||
107 | return 0; | ||
108 | } | ||
109 | |||
110 | bool USBRun::canSetState( State_t Curr, Action_t A ) { | ||
111 | // we only handle up down activate and deactivate | ||
112 | switch( A ) { | ||
113 | case Activate : | ||
114 | { // at least available | ||
115 | if( Curr == Available ) { | ||
116 | return 1; | ||
117 | } | ||
118 | // or we can make one available | ||
119 | InterfaceInfo * N = getInterface(); | ||
120 | if( ! N || N->assignedNode() != 0 ) { | ||
121 | // non available or assigned | ||
122 | return 0; | ||
123 | } | ||
124 | return 1; | ||
125 | } | ||
126 | case Deactivate : | ||
127 | return ( Curr >= Available ); | ||
128 | default : | ||
129 | // FT | ||
130 | break; | ||
131 | } | ||
132 | return 0; | ||
133 | } | ||
134 | |||
135 | // get interface that is free or assigned to us | ||
136 | InterfaceInfo * USBRun::getInterface( void ) { | ||
137 | |||
138 | System & S = NSResources->system(); | ||
139 | InterfaceInfo * best = 0, * Run; | ||
140 | QRegExp R( "usb[0-9abcdef]" ); | ||
141 | |||
142 | for( QDictIterator<InterfaceInfo> It(S.interfaces()); | ||
143 | It.current(); | ||
144 | ++It ) { | ||
145 | Run = It.current(); | ||
146 | if( handlesInterface( Run->Name ) && | ||
147 | Run->CardType == ARPHRD_ETHER | ||
148 | ) { | ||
149 | // this is a USB card | ||
150 | if( Run->assignedNode() == netNode() ) { | ||
151 | // assigned to us | ||
152 | return Run; | ||
153 | } else if( Run->assignedNode() == 0 ) { | ||
154 | // free | ||
155 | best = Run; | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | return best; // can be 0 | ||
160 | } | ||
161 | |||
162 | bool USBRun::handlesInterface( const QString & S ) { | ||
163 | return Pat.match( S ) >= 0; | ||
164 | } | ||
165 | |||
166 | QString USBRun::genNic( long nr ) { | ||
167 | QString S; | ||
168 | S.sprintf( "usbf" ); | ||
169 | return S; | ||
170 | } | ||
171 | |||