Skip to content

Commit

Permalink
return None if interface is down
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Oct 5, 2020
1 parent 66a2cef commit a2bdca9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
35 changes: 20 additions & 15 deletions psutil/_pslinux.py
Expand Up @@ -1196,24 +1196,29 @@ def wifi_ifaces():
bssid = cext.wifi_card_bssid(nic, fd)
proto = cext.wifi_card_proto(nic, fd)
mode = cext.wifi_card_mode(nic, fd)
freq = int(cext.wifi_card_frequency(nic, fd))
freq = cext.wifi_card_frequency(nic, fd)
if freq is not None:
freq = int(freq)
bitrate = cext.wifi_card_bitrate(nic, fd)
txpower = cext.wifi_card_txpower(nic, fd)

qual_curr, sig_curr = cext.wifi_card_stats(nic, fd)
qual_max, sig_max = cext.wifi_card_ranges(nic, fd)
qual_perc = usage_percent(qual_curr, qual_max, round_=1)

# This is how wavemon does it:
# https://github.com/bmegli/wifi-scan/issues/18
# https://github.com/uoaerg/wavemon/blob/master/scan_scr.c#L35
# sig_max is supposed to be -110.
if sig_curr < sig_max:
sig_perc = 0
elif sig_curr > -40:
sig_perc = 70
else:
sig_perc = sig_curr + abs(sig_max)
qual_perc = sig_curr = sig_perc = None
stats = cext.wifi_card_stats(nic, fd)
if stats is not None:
qual_curr, sig_curr = stats
qual_max, sig_max = cext.wifi_card_ranges(nic, fd)
qual_perc = usage_percent(qual_curr, qual_max, round_=1)

# This is how wavemon does it:
# https://github.com/bmegli/wifi-scan/issues/18
# https://github.com/uoaerg/wavemon/blob/master/scan_scr.c#L35
# sig_max is supposed to be -110.
if sig_curr < sig_max:
sig_perc = 0
elif sig_curr > -40:
sig_perc = 70
else:
sig_perc = sig_curr + abs(sig_max)

nt = dict(
essid=essid,
Expand Down
33 changes: 30 additions & 3 deletions psutil/arch/linux/wifi.c
Expand Up @@ -42,6 +42,33 @@ ioctl_request(char *ifname, int request, struct iwreq *pwrq, int sock) {
}


static int
wifi_card_exists(char *ifname, int sock) {
struct iwreq wrq;
int ret;

ret = ioctl_request(ifname, SIOCGIWNAME, &wrq, sock);
if (ret == 0)
return 1;
else
return 0;
}


static PyObject*
handle_ioctl_err(char *ifname, int sock, char *syscall) {
if ((errno == ENOTSUP) || (errno == EINVAL)) {
if (wifi_card_exists(ifname, sock) == 1) {
PyErr_Clear();
psutil_debug("%s failed; converting to None", syscall);
Py_RETURN_NONE;
}
return NULL;
}
return NULL;
}


static char *
convert_macaddr(unsigned char *ptr) {
static char buff[64];
Expand Down Expand Up @@ -182,7 +209,7 @@ psutil_wifi_card_frequency(PyObject* self, PyObject* args) {
if (! PyArg_ParseTuple(args, "si", &ifname, &sock))
return NULL;
if (ioctl_request(ifname, SIOCGIWFREQ, &wrq, sock) != 0)
return NULL;
return handle_ioctl_err(ifname, sock, "ioctl(SIOCGIWFREQ)");
// expressed in Mb/sec
freq = freq2double(wrq.u.freq);
return Py_BuildValue("d", freq);
Expand All @@ -198,7 +225,7 @@ psutil_wifi_card_bitrate(PyObject* self, PyObject* args) {
if (! PyArg_ParseTuple(args, "si", &ifname, &sock))
return NULL;
if (ioctl_request(ifname, SIOCGIWRATE, &wrq, sock) != 0)
return NULL;
return handle_ioctl_err(ifname, sock, "ioctl(SIOCGIWRATE)");
// Expressed in Mb/sec.
return Py_BuildValue("d", (double)wrq.u.bitrate.value / 1000 / 1000);
}
Expand Down Expand Up @@ -276,7 +303,7 @@ psutil_wifi_card_stats(PyObject* self, PyObject* args) {
wrq.u.data.flags = 1;

if (ioctl_request(ifname, SIOCGIWSTATS, &wrq, sock) != 0)
return NULL;
return handle_ioctl_err(ifname, sock, "ioctl(SIOCGIWSTATS)");

// substract 256 in order to match /proc/net/wireless
return Py_BuildValue(
Expand Down

0 comments on commit a2bdca9

Please sign in to comment.