2 * iwinfo - Wireless Information Library - Linux Wireless Extension Backend
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
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.
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.
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/.
18 * Parts of this code are derived from the Linux wireless tools, iwlib.c,
19 * iwlist.c and iwconfig.c in particular.
23 #include "iwinfo_wext.h"
25 static double wext_freq2float(const struct iw_freq *in)
28 double res = (double) in->m;
29 for(i = 0; i < in->e; i++) res *= 10;
33 static inline int wext_freq2mhz(const struct iw_freq *in)
44 for(i = 0; i < in->e; i++)
47 return (int)(mhz / 100000);
51 static inline int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
53 if( !strncmp(ifname, "mon.", 4) )
54 strncpy(wrq->ifr_name, &ifname[4], IFNAMSIZ);
56 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
58 return iwinfo_ioctl(cmd, wrq);
62 int wext_probe(const char *ifname)
66 if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
77 int wext_get_mode(const char *ifname, char *buf)
81 if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
90 sprintf(buf, "Ad-Hoc");
94 sprintf(buf, "Client");
98 sprintf(buf, "Master");
102 sprintf(buf, "Repeater");
106 sprintf(buf, "Secondary");
110 sprintf(buf, "Monitor");
114 sprintf(buf, "Unknown");
123 int wext_get_ssid(const char *ifname, char *buf)
127 wrq.u.essid.pointer = (caddr_t) buf;
128 wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
129 wrq.u.essid.flags = 0;
131 if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
137 int wext_get_bssid(const char *ifname, char *buf)
141 if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
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]);
154 int wext_get_bitrate(const char *ifname, int *buf)
158 if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
160 *buf = (wrq.u.bitrate.value / 1000);
167 int wext_get_channel(const char *ifname, int *buf)
170 struct iw_range range;
174 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
176 if( wrq.u.freq.m >= 1000 )
178 freq = wext_freq2float(&wrq.u.freq);
179 wrq.u.data.pointer = (caddr_t) ⦥
180 wrq.u.data.length = sizeof(struct iw_range);
181 wrq.u.data.flags = 0;
183 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
185 for(i = 0; i < range.num_frequency; i++)
187 if( wext_freq2float(&range.freq[i]) == freq )
189 *buf = range.freq[i].i;
205 int wext_get_frequency(const char *ifname, int *buf)
208 struct iw_range range;
211 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
213 /* We got a channel number instead ... */
214 if( wrq.u.freq.m < 1000 )
216 channel = wrq.u.freq.m;
217 wrq.u.data.pointer = (caddr_t) ⦥
218 wrq.u.data.length = sizeof(struct iw_range);
219 wrq.u.data.flags = 0;
221 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
223 for(i = 0; i < range.num_frequency; i++)
225 if( range.freq[i].i == channel )
227 *buf = wext_freq2mhz(&range.freq[i]);
235 *buf = wext_freq2mhz(&wrq.u.freq);
243 int wext_get_txpower(const char *ifname, int *buf)
247 wrq.u.txpower.flags = 0;
249 if(wext_ioctl(ifname, SIOCGIWTXPOW, &wrq) >= 0)
251 if(wrq.u.txpower.flags & IW_TXPOW_MWATT)
252 *buf = iwinfo_mw2dbm(wrq.u.txpower.value);
254 *buf = wrq.u.txpower.value;
262 int wext_get_signal(const char *ifname, int *buf)
265 struct iw_statistics stats;
267 wrq.u.data.pointer = (caddr_t) &stats;
268 wrq.u.data.length = sizeof(struct iw_statistics);
269 wrq.u.data.flags = 1;
271 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
273 *buf = (stats.qual.updated & IW_QUAL_DBM)
274 ? (stats.qual.level - 0x100) : stats.qual.level;
282 int wext_get_noise(const char *ifname, int *buf)
285 struct iw_statistics stats;
287 wrq.u.data.pointer = (caddr_t) &stats;
288 wrq.u.data.length = sizeof(struct iw_statistics);
289 wrq.u.data.flags = 1;
291 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
293 *buf = (stats.qual.updated & IW_QUAL_DBM)
294 ? (stats.qual.noise - 0x100) : stats.qual.noise;
302 int wext_get_quality(const char *ifname, int *buf)
305 struct iw_statistics stats;
307 wrq.u.data.pointer = (caddr_t) &stats;
308 wrq.u.data.length = sizeof(struct iw_statistics);
309 wrq.u.data.flags = 1;
311 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
313 *buf = stats.qual.qual;
320 int wext_get_quality_max(const char *ifname, int *buf)
323 struct iw_range range;
325 wrq.u.data.pointer = (caddr_t) ⦥
326 wrq.u.data.length = sizeof(struct iw_range);
327 wrq.u.data.flags = 0;
329 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
331 *buf = range.max_qual.qual;
338 int wext_get_assoclist(const char *ifname, char *buf, int *len)
344 int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
347 struct iw_range range;
348 struct iwinfo_txpwrlist_entry entry;
351 wrq.u.data.pointer = (caddr_t) ⦥
352 wrq.u.data.length = sizeof(struct iw_range);
353 wrq.u.data.flags = 0;
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)
359 for( i = 0; i < range.num_txpower; i++ )
361 if( range.txpower_capa & IW_TXPOW_MWATT )
363 entry.dbm = iwinfo_mw2dbm(range.txpower[i]);
364 entry.mw = range.txpower[i];
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 ) */
371 entry.dbm = range.txpower[i];
372 entry.mw = iwinfo_dbm2mw(range.txpower[i]);
375 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
378 *len = i * sizeof(entry);
385 int wext_get_freqlist(const char *ifname, char *buf, int *len)
388 struct iw_range range;
389 struct iwinfo_freqlist_entry entry;
392 wrq.u.data.pointer = (caddr_t) ⦥
393 wrq.u.data.length = sizeof(struct iw_range);
394 wrq.u.data.flags = 0;
396 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
400 for(i = 0; i < range.num_frequency; i++)
402 entry.mhz = wext_freq2mhz(&range.freq[i]);
403 entry.channel = range.freq[i].i;
404 entry.restricted = 0;
406 memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
407 bl += sizeof(struct iwinfo_freqlist_entry);
417 int wext_get_country(const char *ifname, char *buf)
423 int wext_get_countrylist(const char *ifname, char *buf, int *len)
429 int wext_get_hwmodelist(const char *ifname, int *buf)
431 char chans[IWINFO_BUFSIZE] = { 0 };
432 struct iwinfo_freqlist_entry *e = NULL;
435 if( !wext_get_freqlist(ifname, chans, &len) )
437 for( e = (struct iwinfo_freqlist_entry *)chans; e->channel; e++ )
439 if( e->channel <= 14 )
441 *buf |= IWINFO_80211_B;
442 *buf |= IWINFO_80211_G;
446 *buf |= IWINFO_80211_A;
456 int wext_get_encryption(const char *ifname, char *buf)
458 /* No reliable crypto info in wext */
462 int wext_get_mbssid_support(const char *ifname, int *buf)
464 /* No multi bssid support atm */