libiwinfo: move duplicated coded into iwinfo_utils.[ch]
[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 static double wext_freq2float(const struct iw_freq *in)
26 {
27         int             i;
28         double  res = (double) in->m;
29         for(i = 0; i < in->e; i++) res *= 10;
30         return res;
31 }
32
33 static inline int wext_freq2mhz(const struct iw_freq *in)
34 {
35         int i, mhz;
36
37         if( in->e == 6 )
38         {
39                 return in->m;
40         }
41         else
42         {
43                 mhz = in->m;
44                 for(i = 0; i < in->e; i++)
45                         mhz *= 10;
46
47                 return (int)(mhz / 100000);
48         }
49 }
50
51 static inline int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
52 {
53         if( !strncmp(ifname, "mon.", 4) )
54                 strncpy(wrq->ifr_name, &ifname[4], IFNAMSIZ);
55         else
56                 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
57
58         return iwinfo_ioctl(cmd, wrq);
59 }
60
61
62 int wext_probe(const char *ifname)
63 {
64         struct iwreq wrq;
65
66         if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
67                 return 1;
68
69         return 0;
70 }
71
72 void wext_close(void)
73 {
74         /* Nop */
75 }
76
77 int wext_get_mode(const char *ifname, char *buf)
78 {
79         struct iwreq wrq;
80
81         if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
82         {
83                 switch(wrq.u.mode)
84                 {
85                         case 0:
86                                 sprintf(buf, "Auto");
87                                 break;
88
89                         case 1:
90                                 sprintf(buf, "Ad-Hoc");
91                                 break;
92
93                         case 2:
94                                 sprintf(buf, "Client");
95                                 break;
96
97                         case 3:
98                                 sprintf(buf, "Master");
99                                 break;
100
101                         case 4:
102                                 sprintf(buf, "Repeater");
103                                 break;
104
105                         case 5:
106                                 sprintf(buf, "Secondary");
107                                 break;
108
109                         case 6:
110                                 sprintf(buf, "Monitor");
111                                 break;
112
113                         default:
114                                 sprintf(buf, "Unknown");
115                 }
116
117                 return 0;
118         }
119
120         return -1;
121 }
122
123 int wext_get_ssid(const char *ifname, char *buf)
124 {
125         struct iwreq wrq;
126
127         wrq.u.essid.pointer = (caddr_t) buf;
128         wrq.u.essid.length  = IW_ESSID_MAX_SIZE + 1;
129         wrq.u.essid.flags   = 0;
130
131         if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
132                 return 0;
133
134         return -1;
135 }
136
137 int wext_get_bssid(const char *ifname, char *buf)
138 {
139         struct iwreq wrq;
140
141         if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
142         {
143                 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
144                         (uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
145                         (uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
146                         (uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
147
148                 return 0;
149         }
150
151         return -1;
152 }
153
154 int wext_get_bitrate(const char *ifname, int *buf)
155 {
156         struct iwreq wrq;
157
158         if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
159         {
160                 *buf = (wrq.u.bitrate.value / 1000);
161                 return 0;
162         }
163
164         return -1;
165 }
166
167 int wext_get_channel(const char *ifname, int *buf)
168 {
169         struct iwreq wrq;
170         struct iw_range range;
171         double freq;
172         int i;
173
174         if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
175         {
176                 if( wrq.u.freq.m >= 1000 )
177                 {
178                         freq = wext_freq2float(&wrq.u.freq);
179                         wrq.u.data.pointer = (caddr_t) &range;
180                         wrq.u.data.length  = sizeof(struct iw_range);
181                         wrq.u.data.flags   = 0;
182
183                         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
184                         {
185                                 for(i = 0; i < range.num_frequency; i++)
186                                 {
187                                         if( wext_freq2float(&range.freq[i]) == freq )
188                                         {
189                                                 *buf = range.freq[i].i;
190                                                 return 0;
191                                         }
192                                 }
193                         }
194                 }
195                 else
196                 {
197                         *buf = wrq.u.freq.m;
198                         return 0;
199                 }
200         }
201
202         return -1;
203 }
204
205 int wext_get_frequency(const char *ifname, int *buf)
206 {
207         struct iwreq wrq;
208         struct iw_range range;
209         int i, channel;
210
211         if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
212         {
213                 /* We got a channel number instead ... */
214                 if( wrq.u.freq.m < 1000 )
215                 {
216                         channel = wrq.u.freq.m;
217                         wrq.u.data.pointer = (caddr_t) &range;
218                         wrq.u.data.length  = sizeof(struct iw_range);
219                         wrq.u.data.flags   = 0;
220
221                         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
222                         {
223                                 for(i = 0; i < range.num_frequency; i++)
224                                 {
225                                         if( range.freq[i].i == channel )
226                                         {
227                                                 *buf = wext_freq2mhz(&range.freq[i]);
228                                                 return 0;
229                                         }
230                                 }
231                         }
232                 }
233                 else
234                 {
235                         *buf = wext_freq2mhz(&wrq.u.freq);
236                         return 0;
237                 }
238         }
239
240         return -1;
241 }
242
243 int wext_get_txpower(const char *ifname, int *buf)
244 {
245         struct iwreq wrq;
246
247         wrq.u.txpower.flags = 0;
248
249         if(wext_ioctl(ifname, SIOCGIWTXPOW, &wrq) >= 0)
250         {
251                 if(wrq.u.txpower.flags & IW_TXPOW_MWATT)
252                         *buf = iwinfo_mw2dbm(wrq.u.txpower.value);
253                 else
254                         *buf = wrq.u.txpower.value;
255
256                 return 0;
257         }
258
259         return -1;
260 }
261
262 int wext_get_signal(const char *ifname, int *buf)
263 {
264         struct iwreq wrq;
265         struct iw_statistics stats;
266
267         wrq.u.data.pointer = (caddr_t) &stats;
268         wrq.u.data.length  = sizeof(struct iw_statistics);
269         wrq.u.data.flags   = 1;
270
271         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
272         {
273                 *buf = (stats.qual.updated & IW_QUAL_DBM)
274                         ? (stats.qual.level - 0x100) : stats.qual.level;
275
276                 return 0;
277         }
278
279         return -1;
280 }
281
282 int wext_get_noise(const char *ifname, int *buf)
283 {
284         struct iwreq wrq;
285         struct iw_statistics stats;
286
287         wrq.u.data.pointer = (caddr_t) &stats;
288         wrq.u.data.length  = sizeof(struct iw_statistics);
289         wrq.u.data.flags   = 1;
290
291         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
292         {
293                 *buf = (stats.qual.updated & IW_QUAL_DBM)
294                         ? (stats.qual.noise - 0x100) : stats.qual.noise;
295
296                 return 0;
297         }
298
299         return -1;
300 }
301
302 int wext_get_quality(const char *ifname, int *buf)
303 {
304         struct iwreq wrq;
305         struct iw_statistics stats;
306
307         wrq.u.data.pointer = (caddr_t) &stats;
308         wrq.u.data.length  = sizeof(struct iw_statistics);
309         wrq.u.data.flags   = 1;
310
311         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
312         {
313                 *buf = stats.qual.qual;
314                 return 0;
315         }
316
317         return -1;
318 }
319
320 int wext_get_quality_max(const char *ifname, int *buf)
321 {
322         struct iwreq wrq;
323         struct iw_range range;
324
325         wrq.u.data.pointer = (caddr_t) &range;
326         wrq.u.data.length  = sizeof(struct iw_range);
327         wrq.u.data.flags   = 0;
328
329         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
330         {
331                 *buf = range.max_qual.qual;
332                 return 0;
333         }
334
335         return -1;
336 }
337
338 int wext_get_assoclist(const char *ifname, char *buf, int *len)
339 {
340         /* Stub */
341         return -1;
342 }
343
344 int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
345 {
346         struct iwreq wrq;
347         struct iw_range range;
348         struct iwinfo_txpwrlist_entry entry;
349         int i;
350
351         wrq.u.data.pointer = (caddr_t) &range;
352         wrq.u.data.length  = sizeof(struct iw_range);
353         wrq.u.data.flags   = 0;
354
355         if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
356             (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
357             !(range.txpower_capa & IW_TXPOW_RELATIVE)
358         ) {
359                 for( i = 0; i < range.num_txpower; i++ )
360                 {
361                         if( range.txpower_capa & IW_TXPOW_MWATT )
362                         {
363                                 entry.dbm = iwinfo_mw2dbm(range.txpower[i]);
364                                 entry.mw  = range.txpower[i];
365                         }
366
367                         /* Madwifi does neither set mW not dBm caps, also iwlist assumes
368                          * dBm if mW is not set, so don't check here... */
369                         else /* if( range.txpower_capa & IW_TXPOW_DBM ) */
370                         {
371                                 entry.dbm = range.txpower[i];
372                                 entry.mw  = iwinfo_dbm2mw(range.txpower[i]);
373                         }
374
375                         memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
376                 }
377
378                 *len = i * sizeof(entry);
379                 return 0;
380         }
381
382         return -1;
383 }
384
385 int wext_get_freqlist(const char *ifname, char *buf, int *len)
386 {
387         struct iwreq wrq;
388         struct iw_range range;
389         struct iwinfo_freqlist_entry entry;
390         int i, bl;
391
392         wrq.u.data.pointer = (caddr_t) &range;
393         wrq.u.data.length  = sizeof(struct iw_range);
394         wrq.u.data.flags   = 0;
395
396         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
397         {
398                 bl = 0;
399
400                 for(i = 0; i < range.num_frequency; i++)
401                 {
402                         entry.mhz        = wext_freq2mhz(&range.freq[i]);
403                         entry.channel    = range.freq[i].i;
404                         entry.restricted = 0;
405
406                         memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
407                         bl += sizeof(struct iwinfo_freqlist_entry);
408                 }
409
410                 *len = bl;
411                 return 0;
412         }
413
414         return -1;
415 }
416
417 int wext_get_country(const char *ifname, char *buf)
418 {
419         sprintf(buf, "00");
420         return 0;
421 }
422
423 int wext_get_countrylist(const char *ifname, char *buf, int *len)
424 {
425         /* Stub */
426         return -1;
427 }
428
429 int wext_get_hwmodelist(const char *ifname, int *buf)
430 {
431         char chans[IWINFO_BUFSIZE] = { 0 };
432         struct iwinfo_freqlist_entry *e = NULL;
433         int len = 0;
434
435         if( !wext_get_freqlist(ifname, chans, &len) )
436         {
437                 for( e = (struct iwinfo_freqlist_entry *)chans; e->channel; e++ )
438                 {
439                         if( e->channel <= 14 )
440                         {
441                                 *buf |= IWINFO_80211_B;
442                                 *buf |= IWINFO_80211_G;
443                         }
444                         else
445                         {
446                                 *buf |= IWINFO_80211_A;
447                         }
448                 }
449
450                 return 0;
451         }
452
453         return -1;
454 }
455
456 int wext_get_encryption(const char *ifname, char *buf)
457 {
458         /* No reliable crypto info in wext */
459         return -1;
460 }
461
462 int wext_get_mbssid_support(const char *ifname, int *buf)
463 {
464         /* No multi bssid support atm */
465         return -1;
466 }