author | benmeyer <benmeyer> | 2002-10-22 14:48:26 (UTC) |
---|---|---|
committer | benmeyer <benmeyer> | 2002-10-22 14:48:26 (UTC) |
commit | 4fbca156e2bede574f458f9d96cc650c75219973 (patch) (unidiff) | |
tree | 85a0a0a3ad44f744c55ba2076471438b4c25fc21 /noncore/net/networksetup/wlan/wextensions.cpp | |
parent | 916da240f6ff546fd01ea803bf2301ee2c6a836b (diff) | |
download | opie-4fbca156e2bede574f458f9d96cc650c75219973.zip opie-4fbca156e2bede574f458f9d96cc650c75219973.tar.gz opie-4fbca156e2bede574f458f9d96cc650c75219973.tar.bz2 |
Added wireless extensions abstraction
Diffstat (limited to 'noncore/net/networksetup/wlan/wextensions.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/networksetup/wlan/wextensions.cpp | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/noncore/net/networksetup/wlan/wextensions.cpp b/noncore/net/networksetup/wlan/wextensions.cpp new file mode 100644 index 0000000..f45ebf2 --- a/dev/null +++ b/noncore/net/networksetup/wlan/wextensions.cpp | |||
@@ -0,0 +1,158 @@ | |||
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 <math.h> | ||
11 | |||
12 | #define PROCNETWIRELESS "/proc/net/wireless" | ||
13 | #define IW_LOWER 140 | ||
14 | #define IW_UPPER 200 | ||
15 | |||
16 | /** | ||
17 | * Constructor. Sets hasWirelessExtensions | ||
18 | */ | ||
19 | WExtensions::WExtensions(QString interfaceName){ | ||
20 | interface = interfaceName; | ||
21 | fd = socket( AF_INET, SOCK_DGRAM, 0 ); | ||
22 | |||
23 | const char* buffer[200]; | ||
24 | memset( &iwr, 0, sizeof( iwr ) ); | ||
25 | iwr.u.essid.pointer = (caddr_t) buffer; | ||
26 | iwr.u.essid.length = IW_ESSID_MAX_SIZE; | ||
27 | iwr.u.essid.flags = 0; | ||
28 | |||
29 | // check if it is an IEEE 802.11 standard conform | ||
30 | // wireless device by sending SIOCGIWESSID | ||
31 | // which also gives back the Extended Service Set ID | ||
32 | // (see IEEE 802.11 for more information) | ||
33 | |||
34 | const char* iname = interface.latin1(); | ||
35 | strcpy( iwr.ifr_ifrn.ifrn_name, (const char *)iname ); | ||
36 | if ( 0 == ioctl( fd, SIOCGIWESSID, &iwr ) ) | ||
37 | hasWirelessExtensions = true; | ||
38 | else | ||
39 | hasWirelessExtensions = false; | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * @return QString the station name of the access point. | ||
44 | */ | ||
45 | QString WExtensions::station(){ | ||
46 | if(!hasWirelessExtensions) | ||
47 | return QString(); | ||
48 | const char* buffer[200]; | ||
49 | iwr.u.data.pointer = (caddr_t) buffer; | ||
50 | iwr.u.data.length = IW_ESSID_MAX_SIZE; | ||
51 | iwr.u.data.flags = 0; | ||
52 | if ( 0 == ioctl( fd, SIOCGIWNICKN, &iwr )){ | ||
53 | iwr.u.data.pointer[(unsigned int) iwr.u.data.length-1] = '\0'; | ||
54 | return QString(iwr.u.data.pointer); | ||
55 | } | ||
56 | return QString(); | ||
57 | } | ||
58 | |||
59 | /** | ||
60 | * @return QString the essid of the host 802.11 access point. | ||
61 | */ | ||
62 | QString WExtensions::essid(){ | ||
63 | if(!hasWirelessExtensions) | ||
64 | return QString(); | ||
65 | if ( 0 == ioctl( fd, SIOCGIWESSID, &iwr )){ | ||
66 | iwr.u.essid.pointer[(unsigned int) iwr.u.essid.length-1] = '\0'; | ||
67 | return QString(iwr.u.essid.pointer); | ||
68 | } | ||
69 | return QString(); | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * @return QString the mode of interface | ||
74 | */ | ||
75 | QString WExtensions::mode(){ | ||
76 | if(!hasWirelessExtensions) | ||
77 | return QString(); | ||
78 | if ( 0 == ioctl( fd, SIOCGIWMODE, &iwr ) ) | ||
79 | return QString("%1").arg(iwr.u.mode == IW_MODE_ADHOC ? "Ad-Hoc" : "Managed"); | ||
80 | return QString(); | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * Get the frequency that the interface is running at. | ||
85 | * @return int the frequency that the interfacae is running at. | ||
86 | */ | ||
87 | double WExtensions::frequency(){ | ||
88 | if(!hasWirelessExtensions) | ||
89 | return 0; | ||
90 | if ( 0 == ioctl( fd, SIOCGIWFREQ, &iwr )) | ||
91 | return (double( iwr.u.freq.m ) * pow( 10, iwr.u.freq.e ) / 1000000000); | ||
92 | return 0;; | ||
93 | } | ||
94 | |||
95 | /** | ||
96 | * @return QString the AccessPoint that the interface is connected to. | ||
97 | */ | ||
98 | QString WExtensions::ap(){ | ||
99 | if(!hasWirelessExtensions) | ||
100 | return QString(); | ||
101 | if ( 0 == ioctl( fd, SIOCGIWAP, &iwr )){ | ||
102 | QString ap; | ||
103 | ap = ap.sprintf( "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", | ||
104 | iwr.u.ap_addr.sa_data[0]&0xff, | ||
105 | iwr.u.ap_addr.sa_data[1]&0xff, | ||
106 | iwr.u.ap_addr.sa_data[2]&0xff, | ||
107 | iwr.u.ap_addr.sa_data[3]&0xff, | ||
108 | iwr.u.ap_addr.sa_data[4]&0xff, | ||
109 | iwr.u.ap_addr.sa_data[5]&0xff ); | ||
110 | return ap; | ||
111 | } | ||
112 | else return QString(); | ||
113 | |||
114 | } | ||
115 | |||
116 | bool WExtensions::stats(int &signal, int &noise, int &quality){ | ||
117 | // gather link quality from /proc/net/wireless | ||
118 | if(!QFile::exists(PROCNETWIRELESS)) | ||
119 | return false; | ||
120 | |||
121 | char c; | ||
122 | QString status; | ||
123 | QString name; | ||
124 | |||
125 | QFile wfile( PROCNETWIRELESS ); | ||
126 | if(!wfile.open( IO_ReadOnly )) | ||
127 | return false; | ||
128 | |||
129 | QTextStream wstream( &wfile ); | ||
130 | wstream.readLine(); // skip the first two lines | ||
131 | wstream.readLine(); // because they only contain headers | ||
132 | while(!wstream.atEnd()){ | ||
133 | wstream >> name >> status >> quality >> c >> signal >> c >> noise; | ||
134 | if(name == interface){ | ||
135 | if ( quality > 92 ) | ||
136 | #ifdef MDEBUG | ||
137 | qDebug( "WIFIAPPLET: D'oh! Quality %d > estimated max!\n", quality ); | ||
138 | #endif | ||
139 | if ( ( signal > IW_UPPER ) || ( signal < IW_LOWER ) ) | ||
140 | #ifdef MDEBUG | ||
141 | qDebug( "WIFIAPPLET: Doh! Strength %d > estimated max!\n", signal ); | ||
142 | #endif | ||
143 | if ( ( noise > IW_UPPER ) || ( noise < IW_LOWER ) ) | ||
144 | #ifdef MDEBUG | ||
145 | qDebug( "WIFIAPPLET: Doh! Noise %d > estimated max!\n", noise ); | ||
146 | #endif | ||
147 | return true; | ||
148 | } | ||
149 | } | ||
150 | |||
151 | qDebug("Card no longer present"); | ||
152 | quality = -1; | ||
153 | signal = IW_LOWER; | ||
154 | noise = IW_LOWER; | ||
155 | return false; | ||
156 | } | ||
157 | |||
158 | // wextensions.cpp | ||