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 )
93 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
94 fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
97 if( !strncmp(ifname, "mon.", 4) )
98 strncpy(wrq->ifr_name, &ifname[4], IFNAMSIZ);
100 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
102 return ioctl(ioctl_socket, cmd, wrq);
106 int wext_probe(const char *ifname)
110 if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
116 void wext_close(void)
120 if( ioctl_socket > -1 )
124 int wext_get_mode(const char *ifname, char *buf)
128 if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
133 sprintf(buf, "Auto");
137 sprintf(buf, "Ad-Hoc");
141 sprintf(buf, "Client");
145 sprintf(buf, "Master");
149 sprintf(buf, "Repeater");
153 sprintf(buf, "Secondary");
157 sprintf(buf, "Monitor");
161 sprintf(buf, "Unknown");
170 int wext_get_ssid(const char *ifname, char *buf)
174 wrq.u.essid.pointer = (caddr_t) buf;
175 wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
176 wrq.u.essid.flags = 0;
178 if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
184 int wext_get_bssid(const char *ifname, char *buf)
188 if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
190 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
191 (uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
192 (uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
193 (uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
201 int wext_get_bitrate(const char *ifname, int *buf)
205 if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
207 *buf = (wrq.u.bitrate.value / 1000);
214 int wext_get_channel(const char *ifname, int *buf)
217 struct iw_range range;
221 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
223 if( wrq.u.freq.m >= 1000 )
225 freq = wext_freq2float(&wrq.u.freq);
226 wrq.u.data.pointer = (caddr_t) ⦥
227 wrq.u.data.length = sizeof(struct iw_range);
228 wrq.u.data.flags = 0;
230 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
232 for(i = 0; i < range.num_frequency; i++)
234 if( wext_freq2float(&range.freq[i]) == freq )
236 *buf = range.freq[i].i;
252 int wext_get_frequency(const char *ifname, int *buf)
255 struct iw_range range;
258 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
260 /* We got a channel number instead ... */
261 if( wrq.u.freq.m < 1000 )
263 channel = wrq.u.freq.m;
264 wrq.u.data.pointer = (caddr_t) ⦥
265 wrq.u.data.length = sizeof(struct iw_range);
266 wrq.u.data.flags = 0;
268 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
270 for(i = 0; i < range.num_frequency; i++)
272 if( range.freq[i].i == channel )
274 *buf = wext_freq2mhz(&range.freq[i]);
282 *buf = wext_freq2mhz(&wrq.u.freq);
290 int wext_get_txpower(const char *ifname, int *buf)
294 wrq.u.txpower.flags = 0;
296 if(wext_ioctl(ifname, SIOCGIWTXPOW, &wrq) >= 0)
298 if(wrq.u.txpower.flags & IW_TXPOW_MWATT)
299 *buf = wext_mw2dbm(wrq.u.txpower.value);
301 *buf = wrq.u.txpower.value;
309 int wext_get_signal(const char *ifname, int *buf)
312 struct iw_statistics stats;
314 wrq.u.data.pointer = (caddr_t) &stats;
315 wrq.u.data.length = sizeof(struct iw_statistics);
316 wrq.u.data.flags = 1;
318 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
320 *buf = (stats.qual.updated & IW_QUAL_DBM)
321 ? (stats.qual.level - 0x100) : stats.qual.level;
329 int wext_get_noise(const char *ifname, int *buf)
332 struct iw_statistics stats;
334 wrq.u.data.pointer = (caddr_t) &stats;
335 wrq.u.data.length = sizeof(struct iw_statistics);
336 wrq.u.data.flags = 1;
338 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
340 *buf = (stats.qual.updated & IW_QUAL_DBM)
341 ? (stats.qual.noise - 0x100) : stats.qual.noise;
349 int wext_get_quality(const char *ifname, int *buf)
352 struct iw_statistics stats;
354 wrq.u.data.pointer = (caddr_t) &stats;
355 wrq.u.data.length = sizeof(struct iw_statistics);
356 wrq.u.data.flags = 1;
358 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
360 *buf = stats.qual.qual;
367 int wext_get_quality_max(const char *ifname, int *buf)
370 struct iw_range range;
372 wrq.u.data.pointer = (caddr_t) ⦥
373 wrq.u.data.length = sizeof(struct iw_range);
374 wrq.u.data.flags = 0;
376 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
378 *buf = range.max_qual.qual;
385 int wext_get_assoclist(const char *ifname, char *buf, int *len)
391 int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
394 struct iw_range range;
395 struct iwinfo_txpwrlist_entry entry;
398 wrq.u.data.pointer = (caddr_t) ⦥
399 wrq.u.data.length = sizeof(struct iw_range);
400 wrq.u.data.flags = 0;
402 if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
403 (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
404 !(range.txpower_capa & IW_TXPOW_RELATIVE)
406 for( i = 0; i < range.num_txpower; i++ )
408 if( range.txpower_capa & IW_TXPOW_DBM )
410 entry.dbm = range.txpower[i];
411 entry.mw = wext_dbm2mw(range.txpower[i]);
414 else if( range.txpower_capa & IW_TXPOW_MWATT )
416 entry.dbm = wext_mw2dbm(range.txpower[i]);
417 entry.mw = range.txpower[i];
420 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
423 *len = i * sizeof(entry);
430 int wext_get_freqlist(const char *ifname, char *buf, int *len)
433 struct iw_range range;
434 struct iwinfo_freqlist_entry entry;
437 wrq.u.data.pointer = (caddr_t) ⦥
438 wrq.u.data.length = sizeof(struct iw_range);
439 wrq.u.data.flags = 0;
441 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
445 for(i = 0; i < range.num_frequency; i++)
447 entry.mhz = wext_freq2mhz(&range.freq[i]);
448 entry.channel = range.freq[i].i;
450 memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
451 bl += sizeof(struct iwinfo_freqlist_entry);
461 int wext_get_country(const char *ifname, char *buf)
467 int wext_get_countrylist(const char *ifname, char *buf, int *len)
473 int wext_get_encryption(const char *ifname, char *buf)
475 /* No reliable crypto info in wext */
479 int wext_get_mbssid_support(const char *ifname, int *buf)
481 /* No multi bssid support atm */