Wireless.h如何打印信号电平?

在ac程序中使用linux的无线工具,我做了网络扫描,希望找到每个检测到的网络的信号强度(dBm)。

我在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的最强值信号。

有谁知道是什么导致这种差异?

看起来你找到了正确的答案。 对于记录,这是源 (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 noise属于示例2,并且可以使用强制转换为带符号的8位值进行解码。

为了将来的记录,感谢相关人员从评论中解决了这个问题。

我只需要将unsigned int转换为已签名的int并解决了。

将打印行更改为:

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

现在看起来像178,200的值变成-80,-69等!