e9e79e3757e91231b93b966c59f332f5540f2184
[project/luci.git] / contrib / package / 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 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 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 (int)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         {
93                 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
94                 fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
95         }
96
97         if( !strncmp(ifname, "mon.", 4) )
98                 strncpy(wrq->ifr_name, &ifname[4], IFNAMSIZ);
99         else
100                 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
101
102         return ioctl(ioctl_socket, cmd, wrq);
103 }
104
105
106 int wext_probe(const char *ifname)
107 {
108         struct iwreq wrq;
109
110         if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
111                 return 1;
112
113         return 0;
114 }
115
116 void wext_close(void)
117 {
118         wext_scan_close();
119
120         if( ioctl_socket > -1 )
121                 close(ioctl_socket);
122 }
123
124 int wext_get_mode(const char *ifname, char *buf)
125 {
126         struct iwreq wrq;
127
128         if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
129         {
130                 switch(wrq.u.mode)
131                 {
132                         case 0:
133                                 sprintf(buf, "Auto");
134                                 break;
135
136                         case 1:
137                                 sprintf(buf, "Ad-Hoc");
138                                 break;
139
140                         case 2:
141                                 sprintf(buf, "Client");
142                                 break;
143
144                         case 3:
145                                 sprintf(buf, "Master");
146                                 break;
147
148                         case 4:
149                                 sprintf(buf, "Repeater");
150                                 break;
151
152                         case 5:
153                                 sprintf(buf, "Secondary");
154                                 break;
155
156                         case 6:
157                                 sprintf(buf, "Monitor");
158                                 break;
159
160                         default:
161                                 sprintf(buf, "Unknown");
162                 }
163
164                 return 0;
165         }
166
167         return -1;
168 }
169
170 int wext_get_ssid(const char *ifname, char *buf)
171 {
172         struct iwreq wrq;
173
174         wrq.u.essid.pointer = (caddr_t) buf;
175         wrq.u.essid.length  = IW_ESSID_MAX_SIZE + 1;
176         wrq.u.essid.flags   = 0;
177
178         if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
179                 return 0;
180
181         return -1;
182 }
183
184 int wext_get_bssid(const char *ifname, char *buf)
185 {
186         struct iwreq wrq;
187
188         if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
189         {
190                 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
191                         (uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
192                         (uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
193                         (uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
194
195                 return 0;
196         }
197
198         return -1;
199 }
200
201 int wext_get_bitrate(const char *ifname, int *buf)
202 {
203         struct iwreq wrq;
204
205         if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
206         {
207                 *buf = (wrq.u.bitrate.value / 1000);
208                 return 0;
209         }
210
211         return -1;
212 }
213
214 int wext_get_channel(const char *ifname, int *buf)
215 {
216         struct iwreq wrq;
217         struct iw_range range;
218         double freq;
219         int i;
220
221         if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
222         {
223                 if( wrq.u.freq.m >= 1000 )
224                 {
225                         freq = wext_freq2float(&wrq.u.freq);
226                         wrq.u.data.pointer = (caddr_t) &range;
227                         wrq.u.data.length  = sizeof(struct iw_range);
228                         wrq.u.data.flags   = 0;
229
230                         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
231                         {
232                                 for(i = 0; i < range.num_frequency; i++)
233                                 {
234                                         if( wext_freq2float(&range.freq[i]) == freq )
235                                         {
236                                                 *buf = range.freq[i].i;
237                                                 return 0;
238                                         }
239                                 }
240                         }
241                 }
242                 else
243                 {
244                         *buf = wrq.u.freq.m;
245                         return 0;
246                 }
247         }
248
249         return -1;
250 }
251
252 int wext_get_frequency(const char *ifname, int *buf)
253 {
254         struct iwreq wrq;
255         struct iw_range range;
256         int i, channel;
257
258         if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
259         {
260                 /* We got a channel number instead ... */
261                 if( wrq.u.freq.m < 1000 )
262                 {
263                         channel = wrq.u.freq.m;
264                         wrq.u.data.pointer = (caddr_t) &range;
265                         wrq.u.data.length  = sizeof(struct iw_range);
266                         wrq.u.data.flags   = 0;
267
268                         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
269                         {
270                                 for(i = 0; i < range.num_frequency; i++)
271                                 {
272                                         if( range.freq[i].i == channel )
273                                         {
274                                                 *buf = wext_freq2mhz(&range.freq[i]);
275                                                 return 0;
276                                         }
277                                 }
278                         }
279                 }
280                 else
281                 {
282                         *buf = wext_freq2mhz(&wrq.u.freq);
283                         return 0;
284                 }
285         }
286
287         return -1;
288 }
289
290 int wext_get_txpower(const char *ifname, int *buf)
291 {
292         struct iwreq wrq;
293
294         wrq.u.txpower.flags = 0;
295
296         if(wext_ioctl(ifname, SIOCGIWTXPOW, &wrq) >= 0)
297         {
298                 if(wrq.u.txpower.flags & IW_TXPOW_MWATT)
299                         *buf = wext_mw2dbm(wrq.u.txpower.value);
300                 else
301                         *buf = wrq.u.txpower.value;
302
303                 return 0;
304         }
305
306         return -1;
307 }
308
309 int wext_get_signal(const char *ifname, int *buf)
310 {
311         struct iwreq wrq;
312         struct iw_statistics stats;
313
314         wrq.u.data.pointer = (caddr_t) &stats;
315         wrq.u.data.length  = sizeof(struct iw_statistics);
316         wrq.u.data.flags   = 1;
317
318         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
319         {
320                 *buf = (stats.qual.updated & IW_QUAL_DBM)
321                         ? (stats.qual.level - 0x100) : stats.qual.level;
322
323                 return 0;
324         }
325
326         return -1;
327 }
328
329 int wext_get_noise(const char *ifname, int *buf)
330 {
331         struct iwreq wrq;
332         struct iw_statistics stats;
333
334         wrq.u.data.pointer = (caddr_t) &stats;
335         wrq.u.data.length  = sizeof(struct iw_statistics);
336         wrq.u.data.flags   = 1;
337
338         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
339         {
340                 *buf = (stats.qual.updated & IW_QUAL_DBM)
341                         ? (stats.qual.noise - 0x100) : stats.qual.noise;
342
343                 return 0;
344         }
345
346         return -1;
347 }
348
349 int wext_get_quality(const char *ifname, int *buf)
350 {
351         struct iwreq wrq;
352         struct iw_statistics stats;
353
354         wrq.u.data.pointer = (caddr_t) &stats;
355         wrq.u.data.length  = sizeof(struct iw_statistics);
356         wrq.u.data.flags   = 1;
357
358         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
359         {
360                 *buf = stats.qual.qual;
361                 return 0;
362         }
363
364         return -1;
365 }
366
367 int wext_get_quality_max(const char *ifname, int *buf)
368 {
369         struct iwreq wrq;
370         struct iw_range range;
371
372         wrq.u.data.pointer = (caddr_t) &range;
373         wrq.u.data.length  = sizeof(struct iw_range);
374         wrq.u.data.flags   = 0;
375
376         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
377         {
378                 *buf = range.max_qual.qual;
379                 return 0;
380         }
381
382         return -1;
383 }
384
385 int wext_get_assoclist(const char *ifname, char *buf, int *len)
386 {
387         /* Stub */
388         return -1;
389 }
390
391 int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
392 {
393         struct iwreq wrq;
394         struct iw_range range;
395         struct iwinfo_txpwrlist_entry entry;
396         int i;
397
398         wrq.u.data.pointer = (caddr_t) &range;
399         wrq.u.data.length  = sizeof(struct iw_range);
400         wrq.u.data.flags   = 0;
401
402         if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
403             (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
404             !(range.txpower_capa & IW_TXPOW_RELATIVE)
405         ) {
406                 for( i = 0; i < range.num_txpower; i++ )
407                 {
408                         if( range.txpower_capa & IW_TXPOW_DBM )
409                         {
410                                 entry.dbm = range.txpower[i];
411                                 entry.mw  = wext_dbm2mw(range.txpower[i]);
412                         }
413
414                         else if( range.txpower_capa & IW_TXPOW_MWATT )
415                         {
416                                 entry.dbm = wext_mw2dbm(range.txpower[i]);
417                                 entry.mw  = range.txpower[i];
418                         }
419
420                         memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
421                 }
422
423                 *len = i * sizeof(entry);
424                 return 0;
425         }
426
427         return -1;
428 }
429
430 int wext_get_freqlist(const char *ifname, char *buf, int *len)
431 {
432         struct iwreq wrq;
433         struct iw_range range;
434         struct iwinfo_freqlist_entry entry;
435         int i, bl;
436
437         wrq.u.data.pointer = (caddr_t) &range;
438         wrq.u.data.length  = sizeof(struct iw_range);
439         wrq.u.data.flags   = 0;
440
441         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
442         {
443                 bl = 0;
444
445                 for(i = 0; i < range.num_frequency; i++)
446                 {
447                         entry.mhz     = wext_freq2mhz(&range.freq[i]);
448                         entry.channel = range.freq[i].i;
449
450                         memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
451                         bl += sizeof(struct iwinfo_freqlist_entry);
452                 }
453
454                 *len = bl;
455                 return 0;
456         }
457
458         return -1;
459 }
460
461 int wext_get_country(const char *ifname, char *buf)
462 {
463         sprintf(buf, "00");
464         return 0;
465 }
466
467 int wext_get_countrylist(const char *ifname, char *buf, int *len)
468 {
469         /* Stub */
470         return -1;
471 }
472
473 int wext_get_encryption(const char *ifname, char *buf)
474 {
475         /* No reliable crypto info in wext */
476         return -1;
477 }
478
479 int wext_get_mbssid_support(const char *ifname, int *buf)
480 {
481         /* No multi bssid support atm */
482         return -1;
483 }