-rw-r--r-- | noncore/settings/networksettings/wlan/wextensions.cpp | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/noncore/settings/networksettings/wlan/wextensions.cpp b/noncore/settings/networksettings/wlan/wextensions.cpp index fe21f02..8a9db66 100644 --- a/noncore/settings/networksettings/wlan/wextensions.cpp +++ b/noncore/settings/networksettings/wlan/wextensions.cpp | |||
@@ -1,207 +1,211 @@ | |||
1 | #include "wextensions.h" | 1 | #include "wextensions.h" |
2 | 2 | ||
3 | /* OPIE */ | 3 | /* OPIE */ |
4 | #include <opie2/odebug.h> | 4 | #include <opie2/odebug.h> |
5 | using namespace Opie::Core; | 5 | using namespace Opie::Core; |
6 | 6 | ||
7 | /* QT */ | 7 | /* QT */ |
8 | #include <qfile.h> | 8 | #include <qfile.h> |
9 | #include <qtextstream.h> | 9 | #include <qtextstream.h> |
10 | 10 | ||
11 | /* STD */ | 11 | /* STD */ |
12 | #include <arpa/inet.h> | 12 | #include <arpa/inet.h> |
13 | #include <sys/socket.h> | 13 | #include <sys/socket.h> |
14 | #include <sys/ioctl.h> | 14 | #include <sys/ioctl.h> |
15 | #include <math.h> | 15 | #include <math.h> |
16 | 16 | ||
17 | #define PROCNETWIRELESS "/proc/net/wireless" | 17 | #define PROCNETWIRELESS "/proc/net/wireless" |
18 | #define IW_LOWER 0 | 18 | #define IW_LOWER 0 |
19 | #define IW_UPPER 256 | 19 | #define IW_UPPER 256 |
20 | 20 | ||
21 | #warning This is duplicated code. Use libopienet2! | 21 | #warning This is duplicated code. Use libopienet2! |
22 | 22 | ||
23 | /** | 23 | /** |
24 | * Constructor. Sets hasWirelessExtensions | 24 | * Constructor. Sets hasWirelessExtensions |
25 | */ | 25 | */ |
26 | WExtensions::WExtensions(QString interfaceName): hasWirelessExtensions(false), interface(interfaceName) { | 26 | WExtensions::WExtensions(QString interfaceName): hasWirelessExtensions(false), interface(interfaceName) { |
27 | fd = socket( AF_INET, SOCK_DGRAM, 0 ); | 27 | fd = socket( AF_INET, SOCK_DGRAM, 0 ); |
28 | if(fd == -1) | 28 | if(fd == -1) |
29 | return; | 29 | return; |
30 | 30 | ||
31 | const char* buffer[200]; | 31 | const char* buffer[200]; |
32 | memset( &iwr, 0, sizeof( iwr ) ); | 32 | memset( &iwr, 0, sizeof( iwr ) ); |
33 | iwr.u.essid.pointer = (caddr_t) buffer; | 33 | iwr.u.essid.pointer = (caddr_t) buffer; |
34 | iwr.u.essid.length = IW_ESSID_MAX_SIZE; | 34 | iwr.u.essid.length = IW_ESSID_MAX_SIZE; |
35 | iwr.u.essid.flags = 0; | 35 | iwr.u.essid.flags = 0; |
36 | 36 | ||
37 | // check if it is an IEEE 802.11 standard conform | 37 | // check if it is an IEEE 802.11 standard conform |
38 | // wireless device by sending SIOCGIWESSID | 38 | // wireless device by sending SIOCGIWESSID |
39 | // which also gives back the Extended Service Set ID | 39 | // which also gives back the Extended Service Set ID |
40 | // (see IEEE 802.11 for more information) | 40 | // (see IEEE 802.11 for more information) |
41 | 41 | ||
42 | const char* iname = interface.latin1(); | 42 | const char* iname = interface.latin1(); |
43 | strcpy( iwr.ifr_ifrn.ifrn_name, (const char *)iname ); | 43 | strcpy( iwr.ifr_ifrn.ifrn_name, (const char *)iname ); |
44 | if ( 0 == ioctl( fd, SIOCGIWESSID, &iwr ) ) | 44 | if ( 0 == ioctl( fd, SIOCGIWESSID, &iwr ) ) |
45 | hasWirelessExtensions = true; | 45 | hasWirelessExtensions = true; |
46 | } | 46 | } |
47 | 47 | ||
48 | /** | 48 | /** |
49 | * @return QString the station name of the access point. | 49 | * @return QString the station name of the access point. |
50 | */ | 50 | */ |
51 | QString WExtensions::station(){ | 51 | QString WExtensions::station(){ |
52 | if(!hasWirelessExtensions) | 52 | if(!hasWirelessExtensions) |
53 | return QString(); | 53 | return QString(); |
54 | const char* buffer[200]; | 54 | const char* buffer[200]; |
55 | iwr.u.data.pointer = (caddr_t) buffer; | 55 | iwr.u.data.pointer = (caddr_t) buffer; |
56 | iwr.u.data.length = IW_ESSID_MAX_SIZE; | 56 | iwr.u.data.length = IW_ESSID_MAX_SIZE; |
57 | iwr.u.data.flags = 0; | 57 | iwr.u.data.flags = 0; |
58 | if ( 0 == ioctl( fd, SIOCGIWNICKN, &iwr )){ | 58 | if ( 0 == ioctl( fd, SIOCGIWNICKN, &iwr )){ |
59 | iwr.u.data.pointer[(unsigned int) iwr.u.data.length-1] = '\0'; | 59 | buffer[(unsigned int) iwr.u.data.length-1] = '\0'; |
60 | return QString(iwr.u.data.pointer); | 60 | return (const char*) buffer; |
61 | } | 61 | } |
62 | return QString(); | 62 | return QString::null; |
63 | } | 63 | } |
64 | 64 | ||
65 | /** | 65 | /** |
66 | * @return QString the essid of the host 802.11 access point. | 66 | * @return QString the essid of the host 802.11 access point. |
67 | */ | 67 | */ |
68 | QString WExtensions::essid(){ | 68 | QString WExtensions::essid(){ |
69 | if(!hasWirelessExtensions) | 69 | if(!hasWirelessExtensions) |
70 | return QString(); | 70 | return QString(); |
71 | const char* buffer[200]; | ||
72 | iwr.u.data.pointer = (caddr_t) buffer; | ||
73 | iwr.u.data.length = IW_ESSID_MAX_SIZE; | ||
74 | iwr.u.data.flags = 0; | ||
71 | if ( 0 == ioctl( fd, SIOCGIWESSID, &iwr )){ | 75 | if ( 0 == ioctl( fd, SIOCGIWESSID, &iwr )){ |
72 | iwr.u.essid.pointer[(unsigned int) iwr.u.essid.length] = '\0'; | 76 | buffer[(unsigned int) iwr.u.essid.length] = '\0'; |
73 | return QString(iwr.u.essid.pointer); | 77 | return (const char*) buffer; |
74 | } | 78 | } |
75 | return QString(); | 79 | return QString::null; |
76 | } | 80 | } |
77 | 81 | ||
78 | /** | 82 | /** |
79 | * @return QString the mode of interface | 83 | * @return QString the mode of interface |
80 | */ | 84 | */ |
81 | QString WExtensions::mode(){ | 85 | QString WExtensions::mode(){ |
82 | if(!hasWirelessExtensions) | 86 | if(!hasWirelessExtensions) |
83 | return QString(); | 87 | return QString(); |
84 | if ( 0 == ioctl( fd, SIOCGIWMODE, &iwr ) ) | 88 | if ( 0 == ioctl( fd, SIOCGIWMODE, &iwr ) ) |
85 | return QString("%1").arg(iwr.u.mode == IW_MODE_ADHOC ? "Ad-Hoc" : "Managed"); | 89 | return QString("%1").arg(iwr.u.mode == IW_MODE_ADHOC ? "Ad-Hoc" : "Managed"); |
86 | return QString(); | 90 | return QString(); |
87 | } | 91 | } |
88 | 92 | ||
89 | /** | 93 | /** |
90 | * Get the frequency that the interface is running at. | 94 | * Get the frequency that the interface is running at. |
91 | * @return int the frequency that the interfacae is running at. | 95 | * @return int the frequency that the interfacae is running at. |
92 | */ | 96 | */ |
93 | double WExtensions::frequency(){ | 97 | double WExtensions::frequency(){ |
94 | if(!hasWirelessExtensions) | 98 | if(!hasWirelessExtensions) |
95 | return 0; | 99 | return 0; |
96 | if ( 0 == ioctl( fd, SIOCGIWFREQ, &iwr )) | 100 | if ( 0 == ioctl( fd, SIOCGIWFREQ, &iwr )) |
97 | return (double( iwr.u.freq.m ) * pow( 10, iwr.u.freq.e ) / 1000000000); | 101 | return (double( iwr.u.freq.m ) * pow( 10, iwr.u.freq.e ) / 1000000000); |
98 | return 0; | 102 | return 0; |
99 | } | 103 | } |
100 | 104 | ||
101 | /** | 105 | /** |
102 | * Get the channel that the interface is running at. | 106 | * Get the channel that the interface is running at. |
103 | * @return int the channel that the interfacae is running at. | 107 | * @return int the channel that the interfacae is running at. |
104 | */ | 108 | */ |
105 | int WExtensions::channel(){ | 109 | int WExtensions::channel(){ |
106 | if(!hasWirelessExtensions) | 110 | if(!hasWirelessExtensions) |
107 | return 0; | 111 | return 0; |
108 | if ( 0 != ioctl( fd, SIOCGIWFREQ, &iwr )) | 112 | if ( 0 != ioctl( fd, SIOCGIWFREQ, &iwr )) |
109 | return 0; | 113 | return 0; |
110 | 114 | ||
111 | // http://www.elanix.com/pdf/an137e.pdf | 115 | // http://www.elanix.com/pdf/an137e.pdf |
112 | 116 | ||
113 | double num = (double( iwr.u.freq.m ) * pow( 10, iwr.u.freq.e ) / 1000000000); | 117 | double num = (double( iwr.u.freq.m ) * pow( 10, iwr.u.freq.e ) / 1000000000); |
114 | double left = 2.401; | 118 | double left = 2.401; |
115 | double right = 2.416; | 119 | double right = 2.416; |
116 | for(int channel = 1; channel<= 15; channel++){ | 120 | for(int channel = 1; channel<= 15; channel++){ |
117 | if( num >= left && num <= right ) | 121 | if( num >= left && num <= right ) |
118 | return channel; | 122 | return channel; |
119 | left += 0.005; | 123 | left += 0.005; |
120 | right += 0.005; | 124 | right += 0.005; |
121 | } | 125 | } |
122 | odebug << QString("Unknown frequency: %1, returning -1 for the channel.").arg(num).latin1() << oendl; | 126 | odebug << QString("Unknown frequency: %1, returning -1 for the channel.").arg(num).latin1() << oendl; |
123 | return -1; | 127 | return -1; |
124 | } | 128 | } |
125 | 129 | ||
126 | /*** | 130 | /*** |
127 | * Get the current rate that the card is transmiting at. | 131 | * Get the current rate that the card is transmiting at. |
128 | * @return double the rate, 0 if error. | 132 | * @return double the rate, 0 if error. |
129 | */ | 133 | */ |
130 | double WExtensions::rate(){ | 134 | double WExtensions::rate(){ |
131 | if(!hasWirelessExtensions) | 135 | if(!hasWirelessExtensions) |
132 | return 0; | 136 | return 0; |
133 | if(0 == ioctl(fd, SIOCGIWRATE, &iwr)){ | 137 | if(0 == ioctl(fd, SIOCGIWRATE, &iwr)){ |
134 | return ((double)iwr.u.bitrate.value)/1000000; | 138 | return ((double)iwr.u.bitrate.value)/1000000; |
135 | } | 139 | } |
136 | return 0; | 140 | return 0; |
137 | } | 141 | } |
138 | 142 | ||
139 | 143 | ||
140 | /** | 144 | /** |
141 | * @return QString the AccessPoint that the interface is connected to. | 145 | * @return QString the AccessPoint that the interface is connected to. |
142 | */ | 146 | */ |
143 | QString WExtensions::ap(){ | 147 | QString WExtensions::ap(){ |
144 | if(!hasWirelessExtensions) | 148 | if(!hasWirelessExtensions) |
145 | return QString(); | 149 | return QString(); |
146 | if ( 0 == ioctl( fd, SIOCGIWAP, &iwr )){ | 150 | if ( 0 == ioctl( fd, SIOCGIWAP, &iwr )){ |
147 | QString ap; | 151 | QString ap; |
148 | ap = ap.sprintf( "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", | 152 | ap = ap.sprintf( "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", |
149 | iwr.u.ap_addr.sa_data[0]&0xff, | 153 | iwr.u.ap_addr.sa_data[0]&0xff, |
150 | iwr.u.ap_addr.sa_data[1]&0xff, | 154 | iwr.u.ap_addr.sa_data[1]&0xff, |
151 | iwr.u.ap_addr.sa_data[2]&0xff, | 155 | iwr.u.ap_addr.sa_data[2]&0xff, |
152 | iwr.u.ap_addr.sa_data[3]&0xff, | 156 | iwr.u.ap_addr.sa_data[3]&0xff, |
153 | iwr.u.ap_addr.sa_data[4]&0xff, | 157 | iwr.u.ap_addr.sa_data[4]&0xff, |
154 | iwr.u.ap_addr.sa_data[5]&0xff ); | 158 | iwr.u.ap_addr.sa_data[5]&0xff ); |
155 | return ap; | 159 | return ap; |
156 | } | 160 | } |
157 | else return QString(); | 161 | else return QString(); |
158 | } | 162 | } |
159 | 163 | ||
160 | /** | 164 | /** |
161 | * Get the stats for interfaces | 165 | * Get the stats for interfaces |
162 | * @param signal the signal strength of interface | 166 | * @param signal the signal strength of interface |
163 | * @param noise the noise level of the interface | 167 | * @param noise the noise level of the interface |
164 | * @param quality the quality level of the interface | 168 | * @param quality the quality level of the interface |
165 | * @return bool true if successful | 169 | * @return bool true if successful |
166 | */ | 170 | */ |
167 | bool WExtensions::stats(int &signal, int &noise, int &quality){ | 171 | bool WExtensions::stats(int &signal, int &noise, int &quality){ |
168 | // gather link quality from /proc/net/wireless | 172 | // gather link quality from /proc/net/wireless |
169 | if(!QFile::exists(PROCNETWIRELESS)) | 173 | if(!QFile::exists(PROCNETWIRELESS)) |
170 | return false; | 174 | return false; |
171 | 175 | ||
172 | char c; | 176 | char c; |
173 | QString status; | 177 | QString status; |
174 | QString name; | 178 | QString name; |
175 | 179 | ||
176 | QFile wfile( PROCNETWIRELESS ); | 180 | QFile wfile( PROCNETWIRELESS ); |
177 | if(!wfile.open( IO_ReadOnly )) | 181 | if(!wfile.open( IO_ReadOnly )) |
178 | return false; | 182 | return false; |
179 | 183 | ||
180 | QTextStream wstream( &wfile ); | 184 | QTextStream wstream( &wfile ); |
181 | wstream.readLine(); // skip the first two lines | 185 | wstream.readLine(); // skip the first two lines |
182 | wstream.readLine(); // because they only contain headers | 186 | wstream.readLine(); // because they only contain headers |
183 | while(!wstream.atEnd()){ | 187 | while(!wstream.atEnd()){ |
184 | wstream >> name >> status >> quality >> c >> signal >> c >> noise; | 188 | wstream >> name >> status >> quality >> c >> signal >> c >> noise; |
185 | if(name == QString("%1:").arg(interface)){ | 189 | if(name == QString("%1:").arg(interface)){ |
186 | if ( quality > 92 ) | 190 | if ( quality > 92 ) |
187 | odebug << "WIFIAPPLET: D'oh! Quality " << quality << " > estimated max!\n" << oendl; | 191 | odebug << "WIFIAPPLET: D'oh! Quality " << quality << " > estimated max!\n" << oendl; |
188 | if ( ( signal > IW_UPPER ) || ( signal < IW_LOWER ) ) | 192 | if ( ( signal > IW_UPPER ) || ( signal < IW_LOWER ) ) |
189 | odebug << "WIFIAPPLET: Doh! Strength " << signal << " > estimated max!\n" << oendl; | 193 | odebug << "WIFIAPPLET: Doh! Strength " << signal << " > estimated max!\n" << oendl; |
190 | if ( ( noise > IW_UPPER ) || ( noise < IW_LOWER ) ) | 194 | if ( ( noise > IW_UPPER ) || ( noise < IW_LOWER ) ) |
191 | odebug << "WIFIAPPLET: Doh! Noise " << noise << " > estimated max!\n" << oendl; | 195 | odebug << "WIFIAPPLET: Doh! Noise " << noise << " > estimated max!\n" << oendl; |
192 | //odebug << QString("q:%1, s:%2, n:%3").arg(quality).arg(signal).arg(noise).latin1() << oendl; | 196 | //odebug << QString("q:%1, s:%2, n:%3").arg(quality).arg(signal).arg(noise).latin1() << oendl; |
193 | signal = ( ( signal-IW_LOWER ) * 100 ) / IW_UPPER; | 197 | signal = ( ( signal-IW_LOWER ) * 100 ) / IW_UPPER; |
194 | noise = ( ( noise-IW_LOWER ) * 100 ) / IW_UPPER; | 198 | noise = ( ( noise-IW_LOWER ) * 100 ) / IW_UPPER; |
195 | quality = ( quality*100 ) / 92; | 199 | quality = ( quality*100 ) / 92; |
196 | return true; | 200 | return true; |
197 | } | 201 | } |
198 | } | 202 | } |
199 | 203 | ||
200 | odebug << "WExtensions::statsCard no longer present." << oendl; | 204 | odebug << "WExtensions::statsCard no longer present." << oendl; |
201 | quality = -1; | 205 | quality = -1; |
202 | signal = IW_LOWER; | 206 | signal = IW_LOWER; |
203 | noise = IW_LOWER; | 207 | noise = IW_LOWER; |
204 | return false; | 208 | return false; |
205 | } | 209 | } |
206 | 210 | ||
207 | // wextensions.cpp | 211 | // wextensions.cpp |