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 int ioctl_socket = -1;
27 static double wext_freq2float(const struct iw_freq *in)
30 double res = (double) in->m;
31 for(i = 0; i < in->e; i++) res *= 10;
35 static int wext_freq2mhz(const struct iw_freq *in)
46 for(i = 0; i < in->e; i++)
49 return (int)(mhz / 100000);
53 static int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
56 if( ioctl_socket == -1 )
57 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
59 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
60 return ioctl(ioctl_socket, cmd, wrq);
64 int wext_probe(const char *ifname)
68 if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
74 int wext_get_mode(const char *ifname, char *buf)
78 if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
87 sprintf(buf, "Ad-Hoc");
91 sprintf(buf, "Client");
95 sprintf(buf, "Master");
99 sprintf(buf, "Repeater");
103 sprintf(buf, "Secondary");
107 sprintf(buf, "Monitor");
111 sprintf(buf, "Unknown");
120 int wext_get_ssid(const char *ifname, char *buf)
124 wrq.u.essid.pointer = (caddr_t) buf;
125 wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
126 wrq.u.essid.flags = 0;
128 if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
134 int wext_get_bssid(const char *ifname, char *buf)
138 if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
140 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
141 (uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
142 (uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
143 (uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
151 int wext_get_bitrate(const char *ifname, int *buf)
155 if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
157 *buf = wrq.u.bitrate.value;
164 int wext_get_channel(const char *ifname, int *buf)
168 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
170 /* FIXME: iwlib has some strange workarounds here, maybe we need them as well... */
171 *buf = (int) wext_freq2float(&wrq.u.freq);
178 int wext_get_frequency(const char *ifname, int *buf)
181 struct iw_range range;
184 if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
186 /* We got a channel number instead ... */
187 if( wrq.u.freq.m < 1000 )
189 channel = wrq.u.freq.m;
190 wrq.u.data.pointer = (caddr_t) ⦥
191 wrq.u.data.length = sizeof(struct iw_range);
192 wrq.u.data.flags = 0;
194 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
196 for(i = 0; i < range.num_frequency; i++)
198 if( range.freq[i].i == channel )
200 *buf = wext_freq2mhz(&range.freq[i]);
208 *buf = wext_freq2mhz(&wrq.u.freq);
216 int wext_get_signal(const char *ifname, int *buf)
219 struct iw_statistics stats;
221 wrq.u.data.pointer = (caddr_t) &stats;
222 wrq.u.data.length = sizeof(struct iw_statistics);
223 wrq.u.data.flags = 1;
225 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
227 *buf = (stats.qual.level - 0x100);
234 int wext_get_noise(const char *ifname, int *buf)
237 struct iw_statistics stats;
239 wrq.u.data.pointer = (caddr_t) &stats;
240 wrq.u.data.length = sizeof(struct iw_statistics);
241 wrq.u.data.flags = 1;
243 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
245 *buf = (stats.qual.noise - 0x100);
252 int wext_get_quality(const char *ifname, int *buf)
255 struct iw_statistics stats;
257 wrq.u.data.pointer = (caddr_t) &stats;
258 wrq.u.data.length = sizeof(struct iw_statistics);
259 wrq.u.data.flags = 1;
261 if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
263 *buf = stats.qual.qual;
270 int wext_get_quality_max(const char *ifname, int *buf)
273 struct iw_range range;
275 wrq.u.data.pointer = (caddr_t) ⦥
276 wrq.u.data.length = sizeof(struct iw_range);
277 wrq.u.data.flags = 0;
279 if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
281 *buf = range.max_qual.qual;
288 int wext_get_enctype(const char *ifname, char *buf)
294 int wext_get_assoclist(const char *ifname, char *buf, int *len)