2016-09-28 777 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

相關問題