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)
43 return (int)(wext_freq2float(in) / 1000000);
47 static inline int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
49 if( !strncmp(ifname, "mon.", 4) )
50 strncpy(wrq->ifr_name, &ifname[4], IFNAMSIZ);
52 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
54 return iwinfo_ioctl(cmd, wrq);
58 int wext_probe(const char *ifname)
62 if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
73 int wext_get_mode(const char *ifname, int *buf)
77 if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
82 *buf = IWINFO_OPMODE_ADHOC;
86 *buf = IWINFO_OPMODE_CLIENT;
90 *buf = IWINFO_OPMODE_MASTER;
94 *buf = IWINFO_OPMODE_MONITOR;
98 *buf = IWINFO_OPMODE_UNKNOWN;
108 int wext_get_ssid(const char *ifname, char *buf)
112 wrq.u.essid.pointer = (caddr_t) buf;
113 wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
114 wrq.u.essid.flags = 0;
116 if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
122 int wext_get_bssid(const char *ifname, char *buf)
126 if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
128 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
129 (uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
130 (uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
131 (uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
139 int wext_get_bitrate(const char *ifname, int *buf)
143 if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
145 *buf = (wrq.u.bitrate.value / 1000);
152 int wext_get_channel(const char *ifname, int *buf)
155 struct iw_range range;
159 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
161 if( wrq.u.freq.m >= 1000 )
163 freq = wext_freq2float(&wrq.u.freq);
164 wrq.u.data.pointer = (caddr_t) ⦥
165 wrq.u.data.length = sizeof(struct iw_range);
166 wrq.u.data.flags = 0;
168 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
170 for(i = 0; i < range.num_frequency; i++)
172 if( wext_freq2float(&range.freq[i]) == freq )
174 *buf = range.freq[i].i;
190 int wext_get_frequency(const char *ifname, int *buf)
193 struct iw_range range;
196 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
198 /* We got a channel number instead ... */
199 if( wrq.u.freq.m < 1000 )
201 channel = wrq.u.freq.m;
202 wrq.u.data.pointer = (caddr_t) ⦥
203 wrq.u.data.length = sizeof(struct iw_range);
204 wrq.u.data.flags = 0;
206 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
208 for(i = 0; i < range.num_frequency; i++)
210 if( range.freq[i].i == channel )
212 *buf = wext_freq2mhz(&range.freq[i]);
220 *buf = wext_freq2mhz(&wrq.u.freq);
228 int wext_get_txpower(const char *ifname, int *buf)
232 wrq.u.txpower.flags = 0;
234 if(wext_ioctl(ifname, SIOCGIWTXPOW, &wrq) >= 0)
236 if(wrq.u.txpower.flags & IW_TXPOW_MWATT)
237 *buf = iwinfo_mw2dbm(wrq.u.txpower.value);
239 *buf = wrq.u.txpower.value;
247 int wext_get_signal(const char *ifname, int *buf)
250 struct iw_statistics stats;
252 wrq.u.data.pointer = (caddr_t) &stats;
253 wrq.u.data.length = sizeof(struct iw_statistics);
254 wrq.u.data.flags = 1;
256 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
258 *buf = (stats.qual.updated & IW_QUAL_DBM)
259 ? (stats.qual.level - 0x100) : stats.qual.level;
267 int wext_get_noise(const char *ifname, int *buf)
270 struct iw_statistics stats;
272 wrq.u.data.pointer = (caddr_t) &stats;
273 wrq.u.data.length = sizeof(struct iw_statistics);
274 wrq.u.data.flags = 1;
276 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
278 *buf = (stats.qual.updated & IW_QUAL_DBM)
279 ? (stats.qual.noise - 0x100) : stats.qual.noise;
287 int wext_get_quality(const char *ifname, int *buf)
290 struct iw_statistics stats;
292 wrq.u.data.pointer = (caddr_t) &stats;
293 wrq.u.data.length = sizeof(struct iw_statistics);
294 wrq.u.data.flags = 1;
296 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
298 *buf = stats.qual.qual;
305 int wext_get_quality_max(const char *ifname, int *buf)
308 struct iw_range range;
310 wrq.u.data.pointer = (caddr_t) ⦥
311 wrq.u.data.length = sizeof(struct iw_range);
312 wrq.u.data.flags = 0;
314 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
316 *buf = range.max_qual.qual;
323 int wext_get_assoclist(const char *ifname, char *buf, int *len)
329 int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
332 struct iw_range range;
333 struct iwinfo_txpwrlist_entry entry;
336 wrq.u.data.pointer = (caddr_t) ⦥
337 wrq.u.data.length = sizeof(struct iw_range);
338 wrq.u.data.flags = 0;
340 if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
341 (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
342 !(range.txpower_capa & IW_TXPOW_RELATIVE)
344 for( i = 0; i < range.num_txpower; i++ )
346 if( range.txpower_capa & IW_TXPOW_MWATT )
348 entry.dbm = iwinfo_mw2dbm(range.txpower[i]);
349 entry.mw = range.txpower[i];
352 /* Madwifi does neither set mW not dBm caps, also iwlist assumes
353 * dBm if mW is not set, so don't check here... */
354 else /* if( range.txpower_capa & IW_TXPOW_DBM ) */
356 entry.dbm = range.txpower[i];
357 entry.mw = iwinfo_dbm2mw(range.txpower[i]);
360 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
363 *len = i * sizeof(entry);
370 int wext_get_freqlist(const char *ifname, char *buf, int *len)
373 struct iw_range range;
374 struct iwinfo_freqlist_entry entry;
377 wrq.u.data.pointer = (caddr_t) ⦥
378 wrq.u.data.length = sizeof(struct iw_range);
379 wrq.u.data.flags = 0;
381 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
385 for(i = 0; i < range.num_frequency; i++)
387 entry.mhz = wext_freq2mhz(&range.freq[i]);
388 entry.channel = range.freq[i].i;
389 entry.restricted = 0;
391 memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
392 bl += sizeof(struct iwinfo_freqlist_entry);
402 int wext_get_country(const char *ifname, char *buf)
408 int wext_get_countrylist(const char *ifname, char *buf, int *len)
414 int wext_get_hwmodelist(const char *ifname, int *buf)
416 char chans[IWINFO_BUFSIZE] = { 0 };
417 struct iwinfo_freqlist_entry *e = NULL;
422 if( !wext_get_freqlist(ifname, chans, &len) )
424 for( e = (struct iwinfo_freqlist_entry *)chans; e->channel; e++ )
426 if( e->channel <= 14 )
428 *buf |= IWINFO_80211_B;
429 *buf |= IWINFO_80211_G;
433 *buf |= IWINFO_80211_A;
443 int wext_get_encryption(const char *ifname, char *buf)
445 /* No reliable crypto info in wext */
449 int wext_get_mbssid_support(const char *ifname, int *buf)
451 /* No multi bssid support atm */
455 static char * wext_sysfs_ifname_file(const char *ifname, const char *path)
458 static char buf[128];
461 snprintf(buf, sizeof(buf), "/sys/class/net/%s/%s", ifname, path);
463 if ((f = fopen(buf, "r")) != NULL)
465 memset(buf, 0, sizeof(buf));
467 if (fread(buf, 1, sizeof(buf), f))
476 int wext_get_hardware_id(const char *ifname, char *buf)
479 struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf;
481 memset(id, 0, sizeof(struct iwinfo_hardware_id));
483 data = wext_sysfs_ifname_file(ifname, "device/vendor");
485 id->vendor_id = strtoul(data, NULL, 16);
487 data = wext_sysfs_ifname_file(ifname, "device/device");
489 id->device_id = strtoul(data, NULL, 16);
491 data = wext_sysfs_ifname_file(ifname, "device/subsystem_device");
493 id->subsystem_device_id = strtoul(data, NULL, 16);
495 data = wext_sysfs_ifname_file(ifname, "device/subsystem_vendor");
497 id->subsystem_vendor_id = strtoul(data, NULL, 16);
499 return (id->vendor_id > 0 && id->device_id > 0) ? 0 : -1;
502 int wext_get_hardware_name(const char *ifname, char *buf)
504 sprintf(buf, "Generic WEXT");
508 int wext_get_txpower_offset(const char *ifname, int *buf)
515 int wext_get_frequency_offset(const char *ifname, int *buf)