vicious/wifi.lua

64 lines
2.2 KiB
Lua
Raw Normal View History

2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- Licensed under the GNU General Public License v2
2010-01-02 21:21:54 +01:00
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- {{{ Grab environment
local tonumber = tonumber
local io = { popen = io.popen }
local setmetatable = setmetatable
local string = {
find = string.find,
match = string.match
}
-- }}}
-- Wifi: provides wireless information for a requested interface
module("vicious.wifi")
-- {{{ Wireless widget type
2009-08-07 17:41:10 +02:00
local function worker(format, iface)
2009-09-14 17:25:23 +02:00
-- Get data from iwconfig (where available)
local f = io.popen("iwconfig " .. iface)
local iw = f:read("*all")
f:close()
2009-09-14 17:25:23 +02:00
-- Default values
local winfo = {
["{ssid}"] = "N/A",
["{mode}"] = "N/A",
["{chan}"] = "N/A",
2009-12-27 20:32:25 +01:00
["{rate}"] = 0,
["{link}"] = 0,
2009-12-27 20:32:25 +01:00
["{sign}"] = 0
}
-- Check if iwconfig wasn't found, can't be executed or the
-- interface is not a wireless one
if iw == nil or string.find(iw, "No such device") then
return winfo
end
-- Output differs from system to system, some stats can be
-- separated by =, and not all drivers report all stats
2009-10-04 00:54:27 +02:00
winfo["{ssid}"] = -- SSID can have almost anything in it
string.match(iw, 'ESSID[=:]"([%w%p]+[%s]*[%w%p]*]*)"') or winfo["{ssid}"]
winfo["{mode}"] = -- Modes are simple, but also match the "-" in Ad-Hoc
string.match(iw, "Mode[=:]([%w%-]*)") or winfo["{mode}"]
winfo["{chan}"] = -- Channels are plain digits
tonumber(string.match(iw, "Channel[=:]([%d]+)") or winfo["{chan}"])
2009-12-27 20:32:25 +01:00
winfo["{rate}"] = -- Bitrate can start with a space, we don't want to display Mb/s
tonumber(string.match(iw, "Bit Rate[=:]([%s]?[%d%.]*)") or winfo["{rate}"])
winfo["{link}"] = -- Link quality can contain a slash (32/70), match only the first number
tonumber(string.match(iw, "Link Quality[=:]([%d]+)") or winfo["{link}"])
2009-12-27 20:32:25 +01:00
winfo["{sign}"] = -- Signal level can be a negative value, don't display decibel notation
tonumber(string.match(iw, "Signal level[=:]([%-]?[%d]+)") or winfo["{sign}"])
2009-10-04 00:54:27 +02:00
return winfo
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })