我奋力用以下Qt/C++
方法来读取使用Wireless Extension Tools for Linux library Wifi网卡的当前频道:现在SIOCGIWFREQ ioctl返回错误22 EINVAL - 无效的参数,为什么?
const UeTypeCurrentFrequencyChannel& UeNetworkManager::ueCurrentFrequencyChannel(const QString& interfaceName)
{
static UeTypeCurrentFrequencyChannel result=UeTypeCurrentFrequencyChannel();
iwreq wrq;
iw_range range;
int kernelSocket=iw_sockets_open();
if(iw_get_range_info(kernelSocket,
interfaceName.toLocal8Bit().constData(),
&range)>=0)
{
if(iw_get_ext(kernelSocket,
interfaceName.toLocal8Bit().constData(),
SIOCGIWFREQ,
&wrq)>=0)
{
//BUG current frequency and channel is not read, it might be becuase of nonroot credentials - it returns error 22 (EINVAL - Invalid argument), why?
result.first=iw_freq2float(&(wrq.u.freq));
result.second=iw_freq_to_channel(result.first.toDouble(),
&range);
qDebug() << Q_FUNC_INFO
<< "success - current channel:"
<< result;
}
else
{
result.first=QString(interfaceName);
result.second=QString(tr("current channel read failed."));
qDebug() << Q_FUNC_INFO
<< "error (iw_get_ext):"
<< errno;
} // if
}
else
{
result.first=QString(interfaceName);
result.second=QString(tr("current channel read failed."));
qDebug() << Q_FUNC_INFO
<< "error (iw_get_range_info):"
<< errno;
} // if
iw_sockets_close(kernelSocket);
return result;
} // ueCurrentFrequencyChannel
,iw_get_ext
总是返回-1
和errno
设置为22
价值。我已经看到了在errno.h并搜索错误22
和我有以下错误的声明:
#define EINVAL 22 /* Invalid argument */
。
为什么我在尝试使用ioctl
查询网络适配器的当前频道时出现Invalid argument
错误?我启动的应用程序包含此代码为普通用户和根,同样的结果任何时候:
static const UeTypeCurrentFrequencyChannel& UeNetworkManager::ueCurrentFrequencyChannel(const QString&) error(iw_get_ext): 22
为什么,我错过什么?
附录1:
如果我使用iwlist
从终端,我得到所有需要的信息,无论如果我运行它普通用户或根扫描的WiFi卡和网络。
附录#2: 代码已经升级,iw_get_range_info()
的作品。
附录#3: 情况刚刚好;在随机的场合,我得到更迭:
static const UeTypeCurrentFrequencyChannel& UeNetworkManager::ueCurrentFrequencyChannel(const QString&) success - current channel: QPair(QVariant(double, 2.412e+09),QVariant(int, 1))
其相同iwlist scan output
:
wlan0 Scan completed :
Cell 01 - Address: 64:6E:EA:22:21:D4
Channel:1
Frequency:2.412 GHz (Channel 1)
编程后/测试应用程序的一段时间后,再次出现错误,但是iwlist scan
仍然有效。我正在使用内核Linux workstation 4.4.0-38-generiC#57-Ubuntu SMP Tue Sep 6 15:42:33 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
的Ubuntu 16.04.1 LTS
。
这可能是可能的问题有关WiFi
卡Power Management
?
测试此代码时,您使用哪种连接模式? – Dmitry
我没有连接到任何WiFi网络,我只想收集可用的WiFi网络属性为单一数据结构(SSID,可用频道/频率,当前频道/频率,接入点强度,安全模式) - 基本*单元信息* 'iwlist扫描'。 – KernelPanic
扫描完成后,您的exec函数有问题,不是吗?你能获得更多关于你所做的事情的信息吗? – Dmitry