【Wireless】通过WE扩展获取当前信道

通过无线的WE扩展可以获取无线的相关参数, Wireless Extension (WE)是一组通用的API,能在用户空间对通用Wireless LANs进行配置和统计。它的好处在于仅通过一组单一的工具就能对各种各样的Wireless LANs进行管理,不过它们是什么类型,只要其驱动支持Wireless Extension就行;另一个好处就是不用重启驱动或Linux就能改变这些参数
以下通过获取当前无线信道的实现函数,增加对WE扩展的理解
/*
 * A frequency
 * For numbers lower than 10^9, we encode the number in 'm' and
 * set 'e' to 0
 * For number greater than 10^9, we divide it by the lowest power
 * of 10 to get 'm' lower than 10^9, with 'm'= f / (10^'e')...
 * The power of 10 is in 'e', the result of the division is in 'm'.
 */
struct iw_freq
{
__s32 m; /* Mantissa */
__s16 e; /* Exponent */
__u8 i; /* List index (when in range struct) */
__u8 flags; /* Flags (fixed/auto) */
};
对频率f的保持分为两种情况,小于10^9和大于10^9。
小于10^9,频率值f通过m保存,e设置为0;
大于10^9,通过10的指数将频率值f保存为小于10^9部分m(m = f/(10^e)),e是10的指数,通过m = f/(10^e)后m小于10^9的整除值;
比如2412000000,大于10^9,所以除以10后,变成241200000,小于10^9,所以e = 1,m = 241200000;


int ath_get_cur_channel(char *ifname)
{
struct iwreq iwr;
int i;
int j;
static int s = 0;
int res, exp = 6;
int channel = 0;

if (!ifname)
{
return -1;
}


if (s <= 0)
{
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
perror("socket(SOCK_DRAGM)");
return -1;
}
}


memset(&iwr, 0, sizeof(iwr));
strncpy(iwr.ifr_name, ifname, sizeof(iwr.ifr_name));


/* Try to obtain channel information. If get the wrong channel information, maybe the wlan driver doesn't
complete the initialization of channel. In this case, we need to try again until exceed the max times.
The default max times is 3. 
*/
for(i = 0; i < 3; i++)
{
if (ioctl(s, SIOCGIWFREQ, &iwr) < 0)
{
perror("unable to get channel information");
return;
}


/* return freq in hz */
qca_echom("exp = %d, iwr.u.freq.e = %d\r\n", exp, iwr.u.freq.e);
exp -= iwr.u.freq.e;
res = iwr.u.freq.m;
qca_echom("exp = %d, res = %d\r\n", exp, res);
for(j = 0; j < exp; j++)
{
res = res / 10;
}
/* convert to channel num, copy from wlan driver function ath_hal_mhz2ieee */
if (res == 2484)
{
        channel = 14;
}
    else if (res < 2484)
    {
        channel = (res - 2407) / 5;
    }
    else if (res < 5000) 
{
        if (res > 4900) 
{
            channel = (res - 4000) / 5;
       
else
{
            channel = 15 + ((res - 2512) / 20);
        }
    }
else 
{
    channel = (res - 5000) / 5;
}


/* 
     Judge the channel num whether in valid or not. If the channel num is right, terminate the loop and return.
     Otherwise, sleep 1s and try again.
    */ 
    if(channel > 0 && channel < 15) // 2.4G
    {
    return channel;
    }
    if(channel > 35 && channel < 166) // 5G
    {
    return channel;
    }
    sleep(1);
    }
return -1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值