2013-08-06 37 views
3

在c程序中使用linux的無線工具,我做了一次網絡掃描,希望找到每個檢測到的網絡的信號強度(dBm)。Wireless.h如何打印出信號電平?

我發現下面的wireless.h

struct iw_quality 
{ 
    __u8  qual;  /* link quality (%retries, SNR, 
         %missed beacons or better...) */ 
    __u8  level;  /* signal level (dBm) */ 
    __u8  noise;  /* noise level (dBm) */ 
    __u8  updated; /* Flags to know if updated */ 
}; 

我打印出在我的C程序這樣的 「級別」:

printf("Transmit power: %lu ", result->stats.qual.level); 

也試過%d ,但得到了相同的輸出。

結果我得到的是看起來像178,190,201,189等數...

有沒有機會,這些數字是在瓦?根據瓦特 - > dBm轉換器,178瓦特是大約。 52.50dBm?

應該改爲什麼?因爲我認爲dBm可以轉化爲-80dBm或其他。

我需要轉換這些數字嗎?我如何獲得正確的輸出?

謝謝!

======= UPDATE =========

我注意到一些奇怪的行爲。當我通過我的程序使用wireless.h的level屬性時,我記錄的「最強」值約爲-50dBm,而使用相同的路由器時,當我運行「iw wlan0 scan」時,我收到約-14dBm作爲最強信號。

有誰知道是什麼原因造成這種差異?

+0

是__u8一個unsigned int?爲什麼使用一個無符號的int來表示一個預期爲負的值?

+0

-80 dBm是移動網絡的典型代表,您使用的是? –

+0

@SList我不知道。 __u8意味着對嗎?也許我正在尋找錯誤的頭文件?但它在評論中明確指出,這是針對dBm的。我正在掃描WiFi接入點,我給出的-80僅僅是一個例子。我只知道它應該是消極的。 – bubbly

回答

3

看起來你找到了正確的答案。爲了記錄,這是source(iwlib.c)關於編碼所說的內容。

/* People are very often confused by the 8 bit arithmetic happening 
    * here. 
    * All the values here are encoded in a 8 bit integer. 8 bit integers 
    * are either unsigned [0 ; 255], signed [-128 ; +127] or 
    * negative [-255 ; 0]. 
    * Further, on 8 bits, 0x100 == 256 == 0. 
    * 
    * Relative/percent values are always encoded unsigned, between 0 and 255. 
    * Absolute/dBm values are always encoded between -192 and 63. 
    * (Note that up to version 28 of Wireless Tools, dBm used to be 
    * encoded always negative, between -256 and -1). 
    * 
    * How do we separate relative from absolute values ? 
    * The old way is to use the range to do that. As of WE-19, we have 
    * an explicit IW_QUAL_DBM flag in updated... 
    * The range allow to specify the real min/max of the value. As the 
    * range struct only specify one bound of the value, we assume that 
    * the other bound is 0 (zero). 
    * For relative values, range is [0 ; range->max]. 
    * For absolute values, range is [range->max ; 63]. 
    * 
    * Let's take two example : 
    * 1) value is 75%. qual->value = 75 ; range->max_qual.value = 100 
    * 2) value is -54dBm. noise floor of the radio is -104dBm. 
    * qual->value = -54 = 202 ; range->max_qual.value = -104 = 152 
    * 
    * Jean II 
    */ 

level和實施例2下noise下降,並且可以與流延到一個符號的8位值進行解碼。

+0

我使用無線工具29,所以根據這個編碼是不一樣的正常dBm期望。它說他們在-192和63之間?我如何利用IW_QUAL_DBM標誌來顯示dBm? – bubbly

3

對於將來的記錄,這是從評論中解決的,這要感謝相關人員。

我只需要將unsigned int轉換爲已簽名的int並解決。

更改了打印線,以這樣的:

printf("Transmit power: %d ", (int8_t) result->stats.qual.level); 

現在看起來像178的數值,200轉向至-80,-69等!