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
, char *buf
)
77 if(wext_ioctl(ifname
, SIOCGIWMODE
, &wrq
) >= 0)
86 sprintf(buf
, "Ad-Hoc");
90 sprintf(buf
, "Client");
94 sprintf(buf
, "Master");
98 sprintf(buf
, "Repeater");
102 sprintf(buf
, "Secondary");
106 sprintf(buf
, "Monitor");
110 sprintf(buf
, "Unknown");
119 int wext_get_ssid(const char *ifname
, char *buf
)
123 wrq
.u
.essid
.pointer
= (caddr_t
) buf
;
124 wrq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+ 1;
125 wrq
.u
.essid
.flags
= 0;
127 if(wext_ioctl(ifname
, SIOCGIWESSID
, &wrq
) >= 0)
133 int wext_get_bssid(const char *ifname
, char *buf
)
137 if(wext_ioctl(ifname
, SIOCGIWAP
, &wrq
) >= 0)
139 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
140 (uint8_t)wrq
.u
.ap_addr
.sa_data
[0], (uint8_t)wrq
.u
.ap_addr
.sa_data
[1],
141 (uint8_t)wrq
.u
.ap_addr
.sa_data
[2], (uint8_t)wrq
.u
.ap_addr
.sa_data
[3],
142 (uint8_t)wrq
.u
.ap_addr
.sa_data
[4], (uint8_t)wrq
.u
.ap_addr
.sa_data
[5]);
150 int wext_get_bitrate(const char *ifname
, int *buf
)
154 if(wext_ioctl(ifname
, SIOCGIWRATE
, &wrq
) >= 0)
156 *buf
= (wrq
.u
.bitrate
.value
/ 1000);
163 int wext_get_channel(const char *ifname
, int *buf
)
166 struct iw_range range
;
170 if(wext_ioctl(ifname
, SIOCGIWFREQ
, &wrq
) >= 0)
172 if( wrq
.u
.freq
.m
>= 1000 )
174 freq
= wext_freq2float(&wrq
.u
.freq
);
175 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
176 wrq
.u
.data
.length
= sizeof(struct iw_range
);
177 wrq
.u
.data
.flags
= 0;
179 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
181 for(i
= 0; i
< range
.num_frequency
; i
++)
183 if( wext_freq2float(&range
.freq
[i
]) == freq
)
185 *buf
= range
.freq
[i
].i
;
201 int wext_get_frequency(const char *ifname
, int *buf
)
204 struct iw_range range
;
207 if(wext_ioctl(ifname
, SIOCGIWFREQ
, &wrq
) >= 0)
209 /* We got a channel number instead ... */
210 if( wrq
.u
.freq
.m
< 1000 )
212 channel
= wrq
.u
.freq
.m
;
213 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
214 wrq
.u
.data
.length
= sizeof(struct iw_range
);
215 wrq
.u
.data
.flags
= 0;
217 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
219 for(i
= 0; i
< range
.num_frequency
; i
++)
221 if( range
.freq
[i
].i
== channel
)
223 *buf
= wext_freq2mhz(&range
.freq
[i
]);
231 *buf
= wext_freq2mhz(&wrq
.u
.freq
);
239 int wext_get_txpower(const char *ifname
, int *buf
)
243 wrq
.u
.txpower
.flags
= 0;
245 if(wext_ioctl(ifname
, SIOCGIWTXPOW
, &wrq
) >= 0)
247 if(wrq
.u
.txpower
.flags
& IW_TXPOW_MWATT
)
248 *buf
= iwinfo_mw2dbm(wrq
.u
.txpower
.value
);
250 *buf
= wrq
.u
.txpower
.value
;
258 int wext_get_signal(const char *ifname
, int *buf
)
261 struct iw_statistics stats
;
263 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
264 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
265 wrq
.u
.data
.flags
= 1;
267 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
269 *buf
= (stats
.qual
.updated
& IW_QUAL_DBM
)
270 ? (stats
.qual
.level
- 0x100) : stats
.qual
.level
;
278 int wext_get_noise(const char *ifname
, int *buf
)
281 struct iw_statistics stats
;
283 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
284 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
285 wrq
.u
.data
.flags
= 1;
287 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
289 *buf
= (stats
.qual
.updated
& IW_QUAL_DBM
)
290 ? (stats
.qual
.noise
- 0x100) : stats
.qual
.noise
;
298 int wext_get_quality(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
.qual
;
316 int wext_get_quality_max(const char *ifname
, int *buf
)
319 struct iw_range range
;
321 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
322 wrq
.u
.data
.length
= sizeof(struct iw_range
);
323 wrq
.u
.data
.flags
= 0;
325 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
327 *buf
= range
.max_qual
.qual
;
334 int wext_get_assoclist(const char *ifname
, char *buf
, int *len
)
340 int wext_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
343 struct iw_range range
;
344 struct iwinfo_txpwrlist_entry entry
;
347 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
348 wrq
.u
.data
.length
= sizeof(struct iw_range
);
349 wrq
.u
.data
.flags
= 0;
351 if( (wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0) &&
352 (range
.num_txpower
> 0) && (range
.num_txpower
<= IW_MAX_TXPOWER
) &&
353 !(range
.txpower_capa
& IW_TXPOW_RELATIVE
)
355 for( i
= 0; i
< range
.num_txpower
; i
++ )
357 if( range
.txpower_capa
& IW_TXPOW_MWATT
)
359 entry
.dbm
= iwinfo_mw2dbm(range
.txpower
[i
]);
360 entry
.mw
= range
.txpower
[i
];
363 /* Madwifi does neither set mW not dBm caps, also iwlist assumes
364 * dBm if mW is not set, so don't check here... */
365 else /* if( range.txpower_capa & IW_TXPOW_DBM ) */
367 entry
.dbm
= range
.txpower
[i
];
368 entry
.mw
= iwinfo_dbm2mw(range
.txpower
[i
]);
371 memcpy(&buf
[i
*sizeof(entry
)], &entry
, sizeof(entry
));
374 *len
= i
* sizeof(entry
);
381 int wext_get_freqlist(const char *ifname
, char *buf
, int *len
)
384 struct iw_range range
;
385 struct iwinfo_freqlist_entry entry
;
388 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
389 wrq
.u
.data
.length
= sizeof(struct iw_range
);
390 wrq
.u
.data
.flags
= 0;
392 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
396 for(i
= 0; i
< range
.num_frequency
; i
++)
398 entry
.mhz
= wext_freq2mhz(&range
.freq
[i
]);
399 entry
.channel
= range
.freq
[i
].i
;
400 entry
.restricted
= 0;
402 memcpy(&buf
[bl
], &entry
, sizeof(struct iwinfo_freqlist_entry
));
403 bl
+= sizeof(struct iwinfo_freqlist_entry
);
413 int wext_get_country(const char *ifname
, char *buf
)
419 int wext_get_countrylist(const char *ifname
, char *buf
, int *len
)
425 int wext_get_hwmodelist(const char *ifname
, int *buf
)
427 char chans
[IWINFO_BUFSIZE
] = { 0 };
428 struct iwinfo_freqlist_entry
*e
= NULL
;
433 if( !wext_get_freqlist(ifname
, chans
, &len
) )
435 for( e
= (struct iwinfo_freqlist_entry
*)chans
; e
->channel
; e
++ )
437 if( e
->channel
<= 14 )
439 *buf
|= IWINFO_80211_B
;
440 *buf
|= IWINFO_80211_G
;
444 *buf
|= IWINFO_80211_A
;
454 int wext_get_encryption(const char *ifname
, char *buf
)
456 /* No reliable crypto info in wext */
460 int wext_get_mbssid_support(const char *ifname
, int *buf
)
462 /* No multi bssid support atm */
466 static char * wext_sysfs_ifname_file(const char *ifname
, const char *path
)
469 static char buf
[128];
472 snprintf(buf
, sizeof(buf
), "/sys/class/net/%s/%s", ifname
, path
);
474 if ((f
= fopen(buf
, "r")) != NULL
)
476 memset(buf
, 0, sizeof(buf
));
478 if (fread(buf
, 1, sizeof(buf
), f
))
487 int wext_get_hardware_id(const char *ifname
, char *buf
)
490 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
492 memset(id
, 0, sizeof(struct iwinfo_hardware_id
));
494 data
= wext_sysfs_ifname_file(ifname
, "device/vendor");
496 id
->vendor_id
= strtoul(data
, NULL
, 16);
498 data
= wext_sysfs_ifname_file(ifname
, "device/device");
500 id
->device_id
= strtoul(data
, NULL
, 16);
502 data
= wext_sysfs_ifname_file(ifname
, "device/subsystem_device");
504 id
->subsystem_device_id
= strtoul(data
, NULL
, 16);
506 data
= wext_sysfs_ifname_file(ifname
, "device/subsystem_vendor");
508 id
->subsystem_vendor_id
= strtoul(data
, NULL
, 16);
510 return (id
->vendor_id
> 0 && id
->device_id
> 0) ? 0 : -1;
513 int wext_get_hardware_name(const char *ifname
, char *buf
)
515 sprintf(buf
, "Generic WEXT");
519 int wext_get_txpower_offset(const char *ifname
, int *buf
)
526 int wext_get_frequency_offset(const char *ifname
, int *buf
)