wifi on linux: fix bugs and speed things up

This commit is contained in:
Roman Perepelitsa 2020-09-01 11:01:24 +02:00
parent dd5948e5e3
commit cababbeee2
5 changed files with 89 additions and 95 deletions

View file

@ -1475,15 +1475,11 @@
# Parameter | Meaning # Parameter | Meaning
# ----------------------+--------------- # ----------------------+---------------
# P9K_WIFI_SSID | service set identifier, a.k.a. network name # P9K_WIFI_SSID | service set identifier, a.k.a. network name
# P9K_WIFI_LINK_AUTH | authentication protocol such as "wpa2-psk" or "none" # P9K_WIFI_LINK_AUTH | authentication protocol such as "wpa2-psk" or "none"; empty if unknown
# P9K_WIFI_LAST_TX_RATE | wireless transmit rate in megabits per second # P9K_WIFI_LAST_TX_RATE | wireless transmit rate in megabits per second
# P9K_WIFI_RSSI | signal strength in dBm, from -120 to 0 # P9K_WIFI_RSSI | signal strength in dBm, from -120 to 0
# P9K_WIFI_NOISE | noise in dBm, from -120 to 0 # P9K_WIFI_NOISE | noise in dBm, from -120 to 0
# P9K_WIFI_BARS | signal strength in bars, from 0 to 4 (derived from P9K_WIFI_RSSI and P9K_WIFI_NOISE) # P9K_WIFI_BARS | signal strength in bars, from 0 to 4 (derived from P9K_WIFI_RSSI and P9K_WIFI_NOISE)
#
# All parameters except P9K_WIFI_BARS are extracted from the output of the following command:
#
# /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I
####################################[ time: current time ]#################################### ####################################[ time: current time ]####################################
# Current time color. # Current time color.

View file

@ -1452,15 +1452,11 @@
# Parameter | Meaning # Parameter | Meaning
# ----------------------+--------------- # ----------------------+---------------
# P9K_WIFI_SSID | service set identifier, a.k.a. network name # P9K_WIFI_SSID | service set identifier, a.k.a. network name
# P9K_WIFI_LINK_AUTH | authentication protocol such as "wpa2-psk" or "none" # P9K_WIFI_LINK_AUTH | authentication protocol such as "wpa2-psk" or "none"; empty if unknown
# P9K_WIFI_LAST_TX_RATE | wireless transmit rate in megabits per second # P9K_WIFI_LAST_TX_RATE | wireless transmit rate in megabits per second
# P9K_WIFI_RSSI | signal strength in dBm, from -120 to 0 # P9K_WIFI_RSSI | signal strength in dBm, from -120 to 0
# P9K_WIFI_NOISE | noise in dBm, from -120 to 0 # P9K_WIFI_NOISE | noise in dBm, from -120 to 0
# P9K_WIFI_BARS | signal strength in bars, from 0 to 4 (derived from P9K_WIFI_RSSI and P9K_WIFI_NOISE) # P9K_WIFI_BARS | signal strength in bars, from 0 to 4 (derived from P9K_WIFI_RSSI and P9K_WIFI_NOISE)
#
# All parameters except P9K_WIFI_BARS are extracted from the output of the following command:
#
# /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I
####################################[ time: current time ]#################################### ####################################[ time: current time ]####################################
# Current time color. # Current time color.

View file

@ -1452,15 +1452,11 @@
# Parameter | Meaning # Parameter | Meaning
# ----------------------+--------------- # ----------------------+---------------
# P9K_WIFI_SSID | service set identifier, a.k.a. network name # P9K_WIFI_SSID | service set identifier, a.k.a. network name
# P9K_WIFI_LINK_AUTH | authentication protocol such as "wpa2-psk" or "none" # P9K_WIFI_LINK_AUTH | authentication protocol such as "wpa2-psk" or "none"; empty if unknown
# P9K_WIFI_LAST_TX_RATE | wireless transmit rate in megabits per second # P9K_WIFI_LAST_TX_RATE | wireless transmit rate in megabits per second
# P9K_WIFI_RSSI | signal strength in dBm, from -120 to 0 # P9K_WIFI_RSSI | signal strength in dBm, from -120 to 0
# P9K_WIFI_NOISE | noise in dBm, from -120 to 0 # P9K_WIFI_NOISE | noise in dBm, from -120 to 0
# P9K_WIFI_BARS | signal strength in bars, from 0 to 4 (derived from P9K_WIFI_RSSI and P9K_WIFI_NOISE) # P9K_WIFI_BARS | signal strength in bars, from 0 to 4 (derived from P9K_WIFI_RSSI and P9K_WIFI_NOISE)
#
# All parameters except P9K_WIFI_BARS are extracted from the output of the following command:
#
# /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I
####################################[ time: current time ]#################################### ####################################[ time: current time ]####################################
# Current time color. # Current time color.

View file

@ -1552,15 +1552,11 @@
# Parameter | Meaning # Parameter | Meaning
# ----------------------+--------------- # ----------------------+---------------
# P9K_WIFI_SSID | service set identifier, a.k.a. network name # P9K_WIFI_SSID | service set identifier, a.k.a. network name
# P9K_WIFI_LINK_AUTH | authentication protocol such as "wpa2-psk" or "none" # P9K_WIFI_LINK_AUTH | authentication protocol such as "wpa2-psk" or "none"; empty if unknown
# P9K_WIFI_LAST_TX_RATE | wireless transmit rate in megabits per second # P9K_WIFI_LAST_TX_RATE | wireless transmit rate in megabits per second
# P9K_WIFI_RSSI | signal strength in dBm, from -120 to 0 # P9K_WIFI_RSSI | signal strength in dBm, from -120 to 0
# P9K_WIFI_NOISE | noise in dBm, from -120 to 0 # P9K_WIFI_NOISE | noise in dBm, from -120 to 0
# P9K_WIFI_BARS | signal strength in bars, from 0 to 4 (derived from P9K_WIFI_RSSI and P9K_WIFI_NOISE) # P9K_WIFI_BARS | signal strength in bars, from 0 to 4 (derived from P9K_WIFI_RSSI and P9K_WIFI_NOISE)
#
# All parameters except P9K_WIFI_BARS are extracted from the output of the following command:
#
# /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I
####################################[ time: current time ]#################################### ####################################[ time: current time ]####################################
# Current time color. # Current time color.

View file

@ -4903,19 +4903,13 @@ prompt_wifi() {
(( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1] (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
} }
_p9k_prompt_wifi_init() { _p9k_prompt_wifi_init() {
if [[ -x /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport || -f /proc/net/wireless ]]; then if [[ -x /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport ||
-r /proc/net/wireless && -n $commands[iw] ]]; then
typeset -g _p9k__wifi_on= typeset -g _p9k__wifi_on=
typeset -g P9K_WIFI_LAST_TX_RATE= typeset -g P9K_WIFI_LAST_TX_RATE=
typeset -g P9K_WIFI_SSID= typeset -g P9K_WIFI_SSID=
typeset -g P9K_WIFI_LINK_AUTH=
# possible refactor to set link_auth only on darwin
# or also possible to simply set linux link_auth to empty string and leave this scope as-is
if [[ $_p9k_os == OSX ]]; then
typeset -g P9K_WIFI_LINK_AUTH=
fi
typeset -g P9K_WIFI_RSSI= typeset -g P9K_WIFI_RSSI=
typeset -g P9K_WIFI_NOISE= typeset -g P9K_WIFI_NOISE=
typeset -g P9K_WIFI_BARS= typeset -g P9K_WIFI_BARS=
@ -4925,71 +4919,88 @@ _p9k_prompt_wifi_init() {
fi fi
} }
_p9k_prompt_wifi_compute() {
_p9k_worker_async _p9k_prompt_wifi_async _p9k_prompt_wifi_sync
}
_p9k_prompt_wifi_async() { _p9k_prompt_wifi_async() {
local last_tx_rate ssid link_auth rssi noise bars on out line v state local airport=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport
local last_tx_rate ssid link_auth rssi noise bars on out line v state iface
{ {
if [[ $_p9k_os == OSX ]]; then if [[ -x $airport ]]; then
local airport=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport out="$($airport -I)" || return 0
[[ -x $airport ]] || return 0 for line in ${${${(f)out}##[[:space:]]#}%%[[:space:]]#}; do
out="$($airport -I)" || return 0 v=${line#*: }
for line in ${${${(f)out}##[[:space:]]#}%%[[:space:]]#}; do case $line[1,-$#v-3] in
v=${line#*: } agrCtlRSSI) rssi=$v;;
case $line[1,-$#v-3] in agrCtlNoise) noise=$v;;
agrCtlRSSI) rssi=$v;; state) state=$v;;
agrCtlNoise) noise=$v;; lastTxRate) last_tx_rate=$v;;
state) state=$v;; link\ auth) link_auth=$v;;
lastTxRate) last_tx_rate=$v;; SSID) ssid=$v;;
link\ auth) link_auth=$v;; esac
SSID) ssid=$v;; done
esac if [[ $state != running || $rssi != (0|-<->) || $noise != (0|-<->) ]]; then
done rssi=
noise=
elif [[ $_p9k_os == Linux]]; then last_tx_rate=
# iw tools only output 'noise' from a dump that requires superuser and a background service to be running, which probably isn't, so a separate process is needed link_auth=
# /proc/net/wireless displays noise level up to date, w/o requiring superuser ssid=
local proc_less=/proc/net/wireless return 0
[[ -f $proc_less ]] || return 0 fi
on=1
# this method using iw is over 10x faster than the network manager method in benchmarking elif [[ -r /proc/net/wireless && -n $commands[iw] ]]; then
# it's possible some systems are still using 'wireless_tools' (iw's ancestor) but they are long deprecated anyway # Content example (https://github.com/romkatv/powerlevel10k/pull/973#issuecomment-680251804):
local device="$(cut -d\ -f2 <<< $(iw dev | grep Interface))" || return 0 #
out="$(iw dev $device link)" || return 0 # Inter-| sta-| Quality | Discarded packets | Missed | WE
# face | tus | link level noise | nwid crypt frag retry misc | beacon | 22
# 'running' state guaranteed by 'device' and 'proc_less' var assignment # wlp3s0: 0000 58. -52. -256 0 0 0 0 76 0
state='running' local -a lines
lines=(${${(f)"$(</proc/net/wireless)"}:#*\|*}) || return 0
local proc_out="$(grep $device $proc_less | tr -s ' ')" || return 0 (( $#lines == 1 )) || return 0
# using cut is more performant than awk,sed,perl, but I haven't timed against zsh expansion pattern local parts=(${=lines[1]})
rssi="${$(cut -d\ -f4 <<< $proc_out)%.*}" iface=${parts[1]%:}
noise="$(cut -d\ -f5 <<< $proc_out)" state=${parts[2]}
rssi=${parts[4]%.*}
# it's possible to get boolean from iw to check authorization status from a dump, but getting the method (if any) requires superuser noise=${parts[5]%.*}
link_auth="" if [[ -z $iface || $state != 0## || $rssi != (0|-<->) || $noise != (0|-<->) ]]; then
rssi=
for line in ${${${(f)out}##[[:space:]]#}%%[[:space:]]#}; do noise=
v=${line#*: } return 0
case ${line[1,-$#v-3]} in fi
SSID) ssid=$v;; # Output example (https://github.com/romkatv/powerlevel10k/pull/973#issuecomment-680251804):
#
# formatting here to remove ' dBm' from tail # Connected to 74:83:c2:be:76:da (on wlp3s0)
# note 'rssi' is also assigned up above, but this replacement pattern might be faster than using cut - needs benchmarking # SSID: DailyGrindGuest1
signal) rssi=${v//[^0-9-]/};; # freq: 5745
# RX: 35192066 bytes (27041 packets)
# formatting here to transform from 'xxx.x MBit/s MCS xx short GI' to 'xxx.x' where x's are integers # TX: 4090471 bytes (24287 packets)
# benchmarking shows grep here is extremely fast (under .1 msc even on older hw), but you can change to native zsh pattern if you want # signal: -52 dBm
tx\ bitrate) last_tx_rate=$(cut -d\ -f1 <<< $v);; # rx bitrate: 243.0 MBit/s VHT-MCS 6 40MHz VHT-NSS 2
esac # tx bitrate: 240.0 MBit/s VHT-MCS 5 40MHz short GI VHT-NSS 2
done #
fi # bss flags: short-slot-time
# dtim period: 1
if [[ $state != running || $rssi != (0|-<->) || $noise != (0|-<->) ]]; then # beacon int: 100
rssi= lines=(${(f)"$(command iw dev $iface link)"}) || return 0
noise= local -a match mbegin mend
last_tx_rate= for line in $lines; do
link_auth= if [[ $line == (#b)[[:space:]]#SSID:[[:space:]]##([^[:space:]]##) ]]; then
ssid= ssid=$match[1]
bars= elif [[ $line == (#b)[[:space:]]#'tx bitrate:'[[:space:]]##([^[:space:]]##)' MBit/s'* ]]; then
last_tx_rate=$match[1]
[[ $last_tx_rate == <->.<-> ]] && last_tx_rate=${${last_tx_rate%%0#}%.}
fi
done
if [[ -z $ssid || -z $last_tx_rate ]]; then
rssi=
noise=
ssid=
last_tx_rate=
return 0
fi
on=1
else
return 0 return 0
fi fi
# https://www.speedguide.net/faq/how-to-read-rssisignal-and-snrnoise-ratings-440 # https://www.speedguide.net/faq/how-to-read-rssisignal-and-snrnoise-ratings-440
@ -5006,7 +5017,6 @@ _p9k_prompt_wifi_async() {
else else
bars=0 bars=0
fi fi
on=1
} always { } always {
if [[ $_p9k__wifi_on != $on || if [[ $_p9k__wifi_on != $on ||
$P9K_WIFI_LAST_TX_RATE != $last_tx_rate || $P9K_WIFI_LAST_TX_RATE != $last_tx_rate ||
@ -5035,7 +5045,7 @@ _p9k_prompt_wifi_async() {
} }
} }
function _p9k_prompt_wifi_sync() { _p9k_prompt_wifi_sync() {
if [[ -n $REPLY ]]; then if [[ -n $REPLY ]]; then
eval $REPLY eval $REPLY
_p9k_worker_reply $REPLY _p9k_worker_reply $REPLY