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