ioctl SIOCGIWSTATS 信号电平常为 0

ioctl SIOCGIWSTATS 信号电平常为 0

我有一个 C 代码块,它获取有关网络接口的各种信息,即 SSID 和以 dBm 为单位的信号电平:

#include <ctype.h>
#include <errno.h>
#include <linux/wireless.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <time.h>

#define WIRELESS_INTERFACE "wlp2s0"

int main () {
      // Communicate using ioctl to get information
      struct iwreq wreq;
      int sockfd;
      char *ssid[32];

      // Allocate memory for the request
      memset(&wreq, 0, sizeof(struct iwreq));
      // Assign our interface name to the request
      sprintf(wreq.ifr_name, WIRELESS_INTERFACE);

      // Open socket for ioctl
      if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        fprintf(stderr, "FAILED 1");
        exit(1);
      }

      // Get SSID
      wreq.u.essid.pointer = ssid;
      wreq.u.essid.length = 32;
      if (ioctl(sockfd, SIOCGIWESSID, &wreq) == -1) {
        fprintf(stderr, "FAILED 2");
        exit(2);
      }

      fwrite(wreq.u.essid.pointer, 1, wreq.u.essid.length, stdout);

      struct iw_statistics *stats;
      int8_t signalLevel = 0;

      wreq.u.data.pointer = (struct iw_statistics *)malloc(sizeof(*stats));
      wreq.u.data.length = sizeof(*stats);
      wreq.u.data.flags = 1;
      if (ioctl(sockfd, SIOCGIWSTATS, &wreq) == -1) {
        fprintf(stderr, "FAILED 3");
        exit(3);
      }
      if (((struct iw_statistics *)wreq.u.data.pointer)->qual.updated &
          IW_QUAL_DBM) {
        fputs("\nSignal valid\n", stdout);
        // signal is measured in dBm and is valid for us to use
        signalLevel =
            ((struct iw_statistics *)wreq.u.data.pointer)->qual.level - 256;
      }
      fprintf(stdout, "\nsignalLevel %d\n", signalLevel);
}

编译并运行gcc stackoverflow.c -o stackoverflow && ./stackoverflow始终会输出以下内容:

Erebus
Signal valid

signalLevel 0

我为每个调用尝试了一个新的sockfd变量,但这不起作用。wreqioctl

我可以确认其他工具正确报告信号电平:

➜ nmcli -f IN-USE,SIGNAL,SSID device wifi

IN-USE  SIGNAL  SSID
        84      other1
        80      other2
        75      Erebus
        75      --
        75      other3
        75      Erebus
*       72      Erebus
        65      other3
        25      other5
        20      other6

然而,一种已确认使用相同 ioctl 调用的工具不起作用,iwconfig wlp2s0给出Signal level=0 dbm

有关我的系统的信息:

➜ uname -a
Linux jack-laptop 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

乌班图22.04

编辑:我相信这是一个特定于驱动程序的问题,因此添加有关无线驱动程序的信息:

02:00.0 Network controller: Qualcomm Atheros QCNFA765 (rev 01)
    Subsystem: Lenovo Device 9309
    Kernel driver in use: ath11k_pci
    Kernel modules: ath11k_pci

相关内容