【Wireless】通过WE扩展获取当前信道
2016-04-15 15:35
351 查看
通过无线的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;
}
以下通过获取当前无线信道的实现函数,增加对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;
}
相关文章推荐
- JSPatch使用
- MFC VC++多线程间通信
- 二叉平衡查找树:
- 调用http://apistore.baidu.com网站的接口
- IntelliJ IDEA 2016.1(64) 注册码
- NSInvalidArgumentException’, reason: ‘data parameter is nil
- HTML5新特性之WebSocket
- SQL查询中in、exists、not in、not exists的用法与区别
- Java Oracle数据库连接
- 虚拟内存与物理内存
- 各种浏览器的内核是什么
- Windows PHP/phpStudy 连接 甲骨文Oracle 数据库 oci8
- 缓存文件的路径及特点
- Android OkHttp完全解析 是时候来了解OkHttp了
- iOS开发------程序实现国际化Localizable
- ViewController里代码组织规范
- maven搭建ssh
- How to access a value defined in the application.properties file in Spring Boot ( Externalized Conf)
- HDU5437 Alisha’s Party(优先队列+模拟)
- 二叉树的创建和遍历