2017-12-01 00:21:33 -06:00
|
|
|
---------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License v2
|
|
|
|
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local tonumber = tonumber
|
|
|
|
local math = { ceil = math.ceil }
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local helpers = require("vicious.helpers")
|
|
|
|
local io = {
|
2018-05-18 22:36:34 -05:00
|
|
|
open = io.open,
|
|
|
|
popen = io.popen
|
2017-12-01 00:21:33 -06:00
|
|
|
}
|
|
|
|
local string = {
|
2018-05-18 22:36:34 -05:00
|
|
|
find = string.find,
|
|
|
|
match = string.match
|
2017-12-01 00:21:33 -06:00
|
|
|
}
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
|
|
|
-- Wifi: provides wireless information for a requested interface
|
|
|
|
-- vicious.widgets.wifi
|
|
|
|
local wifi = {}
|
|
|
|
|
|
|
|
|
|
|
|
-- {{{ Variable definitions
|
|
|
|
local iwconfig = "iwconfig"
|
|
|
|
local iwcpaths = { "/sbin", "/usr/sbin", "/usr/local/sbin", "/usr/bin" }
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
|
|
|
-- {{{ Wireless widget type
|
|
|
|
local function worker(format, warg)
|
2018-05-18 22:36:34 -05:00
|
|
|
if not warg then return end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2018-05-18 22:36:34 -05:00
|
|
|
-- Default values
|
|
|
|
local winfo = {
|
|
|
|
["{ssid}"] = "N/A",
|
|
|
|
["{mode}"] = "N/A",
|
|
|
|
["{chan}"] = 0,
|
|
|
|
["{rate}"] = 0,
|
|
|
|
["{link}"] = 0,
|
|
|
|
["{linp}"] = 0,
|
|
|
|
["{sign}"] = 0
|
|
|
|
}
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2018-05-18 22:36:34 -05:00
|
|
|
-- Sbin paths aren't in user PATH, search for the binary
|
|
|
|
if iwconfig == "iwconfig" then
|
|
|
|
for _, p in ipairs(iwcpaths) do
|
|
|
|
local f = io.open(p .. "/iwconfig", "rb")
|
|
|
|
if f then
|
|
|
|
iwconfig = p .. "/iwconfig"
|
|
|
|
f:close()
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
|
2018-05-18 22:36:34 -05:00
|
|
|
-- Get data from iwconfig where available
|
|
|
|
local f = io.popen(iwconfig .." ".. helpers.shellquote(warg) .. " 2>&1")
|
|
|
|
local iw = f:read("*all")
|
|
|
|
f:close()
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2018-05-18 22:36:34 -05:00
|
|
|
-- iwconfig wasn't found, isn't executable, or non-wireless interface
|
|
|
|
if iw == nil or string.find(iw, "No such device") then
|
|
|
|
return winfo
|
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2018-05-18 22:36:34 -05:00
|
|
|
-- Output differs from system to system, some stats can be
|
|
|
|
-- separated by =, and not all drivers report all stats
|
|
|
|
winfo["{ssid}"] = -- SSID can have almost anything in it
|
2017-12-17 12:06:39 -06:00
|
|
|
helpers.escape(string.match(iw, 'ESSID[=:]"(.-)"') or winfo["{ssid}"])
|
2018-05-18 22:36:34 -05:00
|
|
|
winfo["{mode}"] = -- Modes are simple, but also match the "-" in Ad-Hoc
|
2017-12-17 12:06:39 -06:00
|
|
|
string.match(iw, "Mode[=:]([%w%-]*)") or winfo["{mode}"]
|
2018-05-18 22:36:34 -05:00
|
|
|
winfo["{chan}"] = -- Channels are plain digits
|
2017-12-17 12:06:39 -06:00
|
|
|
tonumber(string.match(iw, "Channel[=:]([%d]+)") or winfo["{chan}"])
|
2018-05-18 22:36:34 -05:00
|
|
|
winfo["{rate}"] = -- Bitrate can start with a space, we don't want to display Mb/s
|
2017-12-17 12:06:39 -06:00
|
|
|
tonumber(string.match(iw, "Bit Rate[=:]([%s]?[%d%.]*)") or winfo["{rate}"])
|
2018-05-18 22:36:34 -05:00
|
|
|
winfo["{link}"] = -- Link quality can contain a slash (32/70), match only the first number
|
2017-12-17 12:06:39 -06:00
|
|
|
tonumber(string.match(iw, "Link Quality[=:]([%d]+)") or winfo["{link}"])
|
2018-05-18 22:36:34 -05:00
|
|
|
winfo["{sign}"] = -- Signal level can be a negative value, don't display decibel notation
|
2017-12-17 12:06:39 -06:00
|
|
|
tonumber(string.match(iw, "Signal level[=:]([%-]?[%d]+)") or winfo["{sign}"])
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2018-05-18 22:36:34 -05:00
|
|
|
-- Link quality percentage if quality was available
|
|
|
|
if winfo["{link}"] ~= 0 then winfo["{linp}"] = math.ceil(winfo["{link}"] / 0.7) end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2018-05-18 22:36:34 -05:00
|
|
|
return winfo
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
return setmetatable(wifi, { __call = function(_, ...) return worker(...) end })
|