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 #define LOG10_MAGIC 1.25892541179
27 static int ioctl_socket = -1;
29 static double wext_freq2float(const struct iw_freq *in)
32 double res = (double) in->m;
33 for(i = 0; i < in->e; i++) res *= 10;
37 static int wext_freq2mhz(const struct iw_freq *in)
48 for(i = 0; i < in->e; i++)
51 return (int)(mhz / 100000);
55 int wext_dbm2mw(int in)
62 for(k = 0; k < ip; k++) res *= 10;
63 for(k = 0; k < fp; k++) res *= LOG10_MAGIC;
68 int wext_mw2dbm(int in)
70 double fin = (double) in;
88 static int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
91 if( ioctl_socket == -1 )
92 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
94 if( !strncmp(ifname, "mon.", 4) )
95 strncpy(wrq->ifr_name, &ifname[4], IFNAMSIZ);
97 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
99 return ioctl(ioctl_socket, cmd, wrq);
103 int wext_probe(const char *ifname)
107 if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
113 int wext_get_mode(const char *ifname, char *buf)
117 if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
122 sprintf(buf, "Auto");
126 sprintf(buf, "Ad-Hoc");
130 sprintf(buf, "Client");
134 sprintf(buf, "Master");
138 sprintf(buf, "Repeater");
142 sprintf(buf, "Secondary");
146 sprintf(buf, "Monitor");
150 sprintf(buf, "Unknown");
159 int wext_get_ssid(const char *ifname, char *buf)
163 wrq.u.essid.pointer = (caddr_t) buf;
164 wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
165 wrq.u.essid.flags = 0;
167 if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
173 int wext_get_bssid(const char *ifname, char *buf)
177 if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
179 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
180 (uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
181 (uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
182 (uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
190 int wext_get_bitrate(const char *ifname, int *buf)
194 if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
196 *buf = (wrq.u.bitrate.value / 1000);
203 int wext_get_channel(const char *ifname, int *buf)
206 struct iw_range range;
210 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
212 if( wrq.u.freq.m >= 1000 )
214 freq = wext_freq2float(&wrq.u.freq);
215 wrq.u.data.pointer = (caddr_t) ⦥
216 wrq.u.data.length = sizeof(struct iw_range);
217 wrq.u.data.flags = 0;
219 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
221 for(i = 0; i < range.num_frequency; i++)
223 if( wext_freq2float(&range.freq[i]) == freq )
225 *buf = range.freq[i].i;
241 int wext_get_frequency(const char *ifname, int *buf)
244 struct iw_range range;
247 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
249 /* We got a channel number instead ... */
250 if( wrq.u.freq.m < 1000 )
252 channel = wrq.u.freq.m;
253 wrq.u.data.pointer = (caddr_t) ⦥
254 wrq.u.data.length = sizeof(struct iw_range);
255 wrq.u.data.flags = 0;
257 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
259 for(i = 0; i < range.num_frequency; i++)
261 if( range.freq[i].i == channel )
263 *buf = wext_freq2mhz(&range.freq[i]);
271 *buf = wext_freq2mhz(&wrq.u.freq);
279 int wext_get_txpower(const char *ifname, int *buf)
283 wrq.u.txpower.flags = 0;
285 if(wext_ioctl(ifname, SIOCGIWTXPOW, &wrq) >= 0)
287 if(wrq.u.txpower.flags & IW_TXPOW_MWATT)
288 *buf = wext_mw2dbm(wrq.u.txpower.value);
290 *buf = wrq.u.txpower.value;
298 int wext_get_signal(const char *ifname, int *buf)
301 struct iw_statistics stats;
303 wrq.u.data.pointer = (caddr_t) &stats;
304 wrq.u.data.length = sizeof(struct iw_statistics);
305 wrq.u.data.flags = 1;
307 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
309 *buf = (stats.qual.updated & IW_QUAL_DBM)
310 ? (stats.qual.level - 0x100) : stats.qual.level;
318 int wext_get_noise(const char *ifname, int *buf)
321 struct iw_statistics stats;
323 wrq.u.data.pointer = (caddr_t) &stats;
324 wrq.u.data.length = sizeof(struct iw_statistics);
325 wrq.u.data.flags = 1;
327 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
329 *buf = (stats.qual.updated & IW_QUAL_DBM)
330 ? (stats.qual.noise - 0x100) : stats.qual.noise;
338 int wext_get_quality(const char *ifname, int *buf)
341 struct iw_statistics stats;
343 wrq.u.data.pointer = (caddr_t) &stats;
344 wrq.u.data.length = sizeof(struct iw_statistics);
345 wrq.u.data.flags = 1;
347 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
349 *buf = stats.qual.qual;
356 int wext_get_quality_max(const char *ifname, int *buf)
359 struct iw_range range;
361 wrq.u.data.pointer = (caddr_t) ⦥
362 wrq.u.data.length = sizeof(struct iw_range);
363 wrq.u.data.flags = 0;
365 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
367 *buf = range.max_qual.qual;
374 int wext_get_assoclist(const char *ifname, char *buf, int *len)
380 int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
383 struct iw_range range;
384 struct iwinfo_txpwrlist_entry entry;
387 wrq.u.data.pointer = (caddr_t) ⦥
388 wrq.u.data.length = sizeof(struct iw_range);
389 wrq.u.data.flags = 0;
391 if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
392 (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
393 !(range.txpower_capa & IW_TXPOW_RELATIVE)
395 for( i = 0; i < range.num_txpower; i++ )
397 if( range.txpower_capa & IW_TXPOW_DBM )
399 entry.dbm = range.txpower[i];
400 entry.mw = wext_dbm2mw(range.txpower[i]);
403 else if( range.txpower_capa & IW_TXPOW_MWATT )
405 entry.dbm = wext_mw2dbm(range.txpower[i]);
406 entry.mw = range.txpower[i];
409 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
412 *len = i * sizeof(entry);
419 int wext_get_freqlist(const char *ifname, char *buf, int *len)
422 struct iw_range range;
423 struct iwinfo_freqlist_entry entry;
426 wrq.u.data.pointer = (caddr_t) ⦥
427 wrq.u.data.length = sizeof(struct iw_range);
428 wrq.u.data.flags = 0;
430 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
434 for(i = 0; i < range.num_frequency; i++)
436 entry.mhz = wext_freq2mhz(&range.freq[i]);
437 entry.channel = range.freq[i].i;
439 memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
440 bl += sizeof(struct iwinfo_freqlist_entry);
450 int wext_get_country(const char *ifname, char *buf)
456 int wext_get_countrylist(const char *ifname, char *buf, int *len)
462 int wext_get_encryption(const char *ifname, char *buf)
464 /* No reliable crypto info in wext */
468 int wext_get_mbssid_support(const char *ifname, int *buf)
470 /* No multi bssid support atm */