2016-09-28 778 views
2

我奋力用以下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总是返回-1errno设置为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/LinuxUbuntu 16.04.1 LTS
这可能是可能的问题有关WiFiPower Management

+0

测试此代码时,您使用哪种连接模式? – Dmitry

+0

我没有连接到任何WiFi网络,我只想收集可用的WiFi网络属性为单一数据结构(SSID,可用频道/频率,当前频道/频率,接入点强度,安全模式) - 基本*单元信息* 'iwlist扫描'。 – KernelPanic

+0

扫描完成后,您的exec函数有问题,不是吗?你能获得更多关于你所做的事情的信息吗? – Dmitry

回答

2

看看cfg80211_wext_giwfreq。这就是为什么你得到当前通道EINVAL如果你的Wi-Fi卡没有使用任何连接模式
命令iwlist扫描工作,因为Wi-Fi卡的扫描时间切换到AP client mode

相关问题