2010年9月15日 星期三

取得 wifi parameters for linux kernel

想抓取wifi的資訊可利用linux kernel的struct netdev
所提供的struct iw_handler_def wireless_handlers來拿取
這是透過wireless_handlers內有個standard的指標函數,
可提供wireless device driver實作,
因此我們可透過standard指標函數來拿取wireless相關資訊

struct net_device
{
...
const struct iw_handler_def * wireless_handlers;
...
}


struct iw_handler_def
{
...
/* Array of handlers for standard ioctls
* We will call dev->wireless_handlers->standard[ioctl - SIOCSIWCOMMIT]
*/
const iw_handler * standard;
...
}


typedef int (*iw_handler)(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra);


以zd1211rw driver為例:

#define WX(x) [(x)-SIOCIWFIRST]

static const iw_handler zd_standard_iw_handlers[] = {
WX(SIOCGIWNAME) = iw_get_name,
WX(SIOCGIWNICKN) = iw_get_nick,
WX(SIOCSIWFREQ) = iw_set_freq,
WX(SIOCGIWFREQ) = iw_get_freq,
...
};


可以看到這些Wireless Identification的定義:

/* Wireless Identification */
#define SIOCSIWCOMMIT 0x8B00
#define SIOCGIWNAME 0x8B01

#define SIOCSIWNWID 0x8B02 /* set network id (pre-802.11) */
#define SIOCGIWNWID 0x8B03 /* get network id (the cell) */
#define SIOCSIWFREQ 0x8B04 /* set channel/frequency (Hz) */
#define SIOCGIWFREQ 0x8B05 /* get channel/frequency (Hz) */
...
wireless.h:335:#define SIOCIWFIRSTPRIV 0x8BE0
wireless.h:351:#define SIOCIWFIRST 0x8B00


倘若我們想抓取frequency(channel) of current link ,

可直接call function
net_dev -> wireless_handlers ->
standard[SIOCGIWFREQ - SIOCSIWCOMMIT](net_dev, NULL, (void *) & my_freq, NULL);


然後透過my_freq.m就能抓取現在的freq

struct iw_freq my_freq;

struct iw_freq
{
__s32 m; /* Mantissa */
__s16 e; /* Exponent */
__u8 i; /* List index (when in range struct) */
__u8 flags; /* Flags (fixed/auto) */
};


進而推出wifi的rssi