387cb13b4466367ab32338900f1679b42fa91bf0
[project/luci.git] / libs / iwinfo / src / iwinfo_wext.c
1 /*
2 * iwinfo - Wireless Information Library - Linux Wireless Extension Backend
3 *
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * The iwinfo library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * The iwinfo library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
17 *
18 * Parts of this code are derived from the Linux wireless tools, iwlib.c,
19 * iwlist.c and iwconfig.c in particular.
20 */
21
22 #include "iwinfo.h"
23 #include "iwinfo_wext.h"
24
25 #define LOG10_MAGIC 1.25892541179
26
27 static int ioctl_socket = -1;
28
29 static double wext_freq2float(const struct iw_freq *in)
30 {
31 int i;
32 double res = (double) in->m;
33 for(i = 0; i < in->e; i++) res *= 10;
34 return res;
35 }
36
37 static int wext_freq2mhz(const struct iw_freq *in)
38 {
39 int i, mhz;
40
41 if( in->e == 6 )
42 {
43 return in->m;
44 }
45 else
46 {
47 mhz = in->m;
48 for(i = 0; i < in->e; i++)
49 mhz *= 10;
50
51 return (int)(mhz / 100000);
52 }
53 }
54
55 static int wext_dbm2mw(int in)
56 {
57 double res = 1.0;
58 int ip = in / 10;
59 int fp = in % 10;
60 int k;
61
62 for(k = 0; k < ip; k++) res *= 10;
63 for(k = 0; k < fp; k++) res *= LOG10_MAGIC;
64
65 return (int)res;
66 }
67
68 static int wext_mw2dbm(int in)
69 {
70 double fin = (double) in;
71 int res = 0;
72
73 while(fin > 10.0)
74 {
75 res += 10;
76 fin /= 10.0;
77 }
78
79 while(fin > 1.000001)
80 {
81 res += 1;
82 fin /= LOG10_MAGIC;
83 }
84
85 return res;
86 }
87
88 static int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
89 {
90 /* prepare socket */
91 if( ioctl_socket == -1 )
92 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
93
94 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
95 return ioctl(ioctl_socket, cmd, wrq);
96 }
97
98
99 int wext_probe(const char *ifname)
100 {
101 struct iwreq wrq;
102
103 if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
104 return 1;
105
106 return 0;
107 }
108
109 int wext_get_mode(const char *ifname, char *buf)
110 {
111 struct iwreq wrq;
112
113 if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
114 {
115 switch(wrq.u.mode)
116 {
117 case 0:
118 sprintf(buf, "Auto");
119 break;
120
121 case 1:
122 sprintf(buf, "Ad-Hoc");
123 break;
124
125 case 2:
126 sprintf(buf, "Client");
127 break;
128
129 case 3:
130 sprintf(buf, "Master");
131 break;
132
133 case 4:
134 sprintf(buf, "Repeater");
135 break;
136
137 case 5:
138 sprintf(buf, "Secondary");
139 break;
140
141 case 6:
142 sprintf(buf, "Monitor");
143 break;
144
145 default:
146 sprintf(buf, "Unknown");
147 }
148
149 return 0;
150 }
151
152 return -1;
153 }
154
155 int wext_get_ssid(const char *ifname, char *buf)
156 {
157 struct iwreq wrq;
158
159 wrq.u.essid.pointer = (caddr_t) buf;
160 wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
161 wrq.u.essid.flags = 0;
162
163 if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
164 return 0;
165
166 return -1;
167 }
168
169 int wext_get_bssid(const char *ifname, char *buf)
170 {
171 struct iwreq wrq;
172
173 if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
174 {
175 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
176 (uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
177 (uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
178 (uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
179
180 return 0;
181 }
182
183 return -1;
184 }
185
186 int wext_get_bitrate(const char *ifname, int *buf)
187 {
188 struct iwreq wrq;
189
190 if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
191 {
192 *buf = (wrq.u.bitrate.value / 1000);
193 return 0;
194 }
195
196 return -1;
197 }
198
199 int wext_get_channel(const char *ifname, int *buf)
200 {
201 struct iwreq wrq;
202 struct iw_range range;
203 double freq;
204 int i;
205
206 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
207 {
208 if( wrq.u.freq.m >= 1000 )
209 {
210 freq = wext_freq2float(&wrq.u.freq);
211 wrq.u.data.pointer = (caddr_t) &range;
212 wrq.u.data.length = sizeof(struct iw_range);
213 wrq.u.data.flags = 0;
214
215 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
216 {
217 for(i = 0; i < range.num_frequency; i++)
218 {
219 if( wext_freq2float(&range.freq[i]) == freq )
220 {
221 *buf = range.freq[i].i;
222 return 0;
223 }
224 }
225 }
226 }
227 else
228 {
229 *buf = wrq.u.freq.m;
230 return 0;
231 }
232 }
233
234 return -1;
235 }
236
237 int wext_get_frequency(const char *ifname, int *buf)
238 {
239 struct iwreq wrq;
240 struct iw_range range;
241 int i, channel;
242
243 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
244 {
245 /* We got a channel number instead ... */
246 if( wrq.u.freq.m < 1000 )
247 {
248 channel = wrq.u.freq.m;
249 wrq.u.data.pointer = (caddr_t) &range;
250 wrq.u.data.length = sizeof(struct iw_range);
251 wrq.u.data.flags = 0;
252
253 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
254 {
255 for(i = 0; i < range.num_frequency; i++)
256 {
257 if( range.freq[i].i == channel )
258 {
259 *buf = wext_freq2mhz(&range.freq[i]);
260 return 0;
261 }
262 }
263 }
264 }
265 else
266 {
267 *buf = wext_freq2mhz(&wrq.u.freq);
268 return 0;
269 }
270 }
271
272 return -1;
273 }
274
275 int wext_get_signal(const char *ifname, int *buf)
276 {
277 struct iwreq wrq;
278 struct iw_statistics stats;
279
280 wrq.u.data.pointer = (caddr_t) &stats;
281 wrq.u.data.length = sizeof(struct iw_statistics);
282 wrq.u.data.flags = 1;
283
284 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
285 {
286 *buf = (stats.qual.level - 0x100);
287 return 0;
288 }
289
290 return -1;
291 }
292
293 int wext_get_noise(const char *ifname, int *buf)
294 {
295 struct iwreq wrq;
296 struct iw_statistics stats;
297
298 wrq.u.data.pointer = (caddr_t) &stats;
299 wrq.u.data.length = sizeof(struct iw_statistics);
300 wrq.u.data.flags = 1;
301
302 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
303 {
304 *buf = (stats.qual.noise - 0x100);
305 return 0;
306 }
307
308 return -1;
309 }
310
311 int wext_get_quality(const char *ifname, int *buf)
312 {
313 struct iwreq wrq;
314 struct iw_statistics stats;
315
316 wrq.u.data.pointer = (caddr_t) &stats;
317 wrq.u.data.length = sizeof(struct iw_statistics);
318 wrq.u.data.flags = 1;
319
320 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
321 {
322 *buf = stats.qual.qual;
323 return 0;
324 }
325
326 return -1;
327 }
328
329 int wext_get_quality_max(const char *ifname, int *buf)
330 {
331 struct iwreq wrq;
332 struct iw_range range;
333
334 wrq.u.data.pointer = (caddr_t) &range;
335 wrq.u.data.length = sizeof(struct iw_range);
336 wrq.u.data.flags = 0;
337
338 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
339 {
340 *buf = range.max_qual.qual;
341 return 0;
342 }
343
344 return -1;
345 }
346
347 int wext_get_enctype(const char *ifname, char *buf)
348 {
349 /* Stub */
350 return -1;
351 }
352
353 int wext_get_assoclist(const char *ifname, char *buf, int *len)
354 {
355 /* Stub */
356 return -1;
357 }
358
359 int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
360 {
361 struct iwreq wrq;
362 struct iw_range range;
363 struct iwinfo_txpwrlist_entry entry;
364 int i;
365
366 wrq.u.data.pointer = (caddr_t) &range;
367 wrq.u.data.length = sizeof(struct iw_range);
368 wrq.u.data.flags = 0;
369
370 if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
371 (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
372 !(range.txpower_capa & IW_TXPOW_RELATIVE)
373 ) {
374 for( i = 0; i < range.num_txpower; i++ )
375 {
376 if( range.txpower_capa & IW_TXPOW_MWATT )
377 {
378 entry.dbm = wext_mw2dbm(range.txpower[i]);
379 entry.mw = range.txpower[i];
380 }
381 else
382 {
383 entry.dbm = range.txpower[i];
384 entry.mw = wext_dbm2mw(range.txpower[i]);
385 }
386
387 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
388 }
389
390 *len = i * sizeof(entry);
391 return 0;
392 }
393
394 return -1;
395 }
396
397 int wext_get_mbssid_support(const char *ifname, int *buf)
398 {
399 /* No multi bssid support atm */
400 return -1;
401 }
402