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