2013-08-06 35 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等!