Debian Codegraph @

vicious/widgets/wifiiw_linux.lua

raw symbols SCIP
1 -- Wi-Fi widget type for GNU/Linux using iw
2 -- Copyright (C) 2016 Marius M. <[email protected]>
3 -- Copyright (C) 2017 mutlusun <[email protected]>
4 -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m>
5 -- Copyright (C) 2019 Xaver Hellauer <[email protected]>
6 --
7 -- This file is part of Vicious.
8 --
9 -- Vicious is free software: you can redistribute it and/or modify
10 -- it under the terms of the GNU General Public License as
11 -- published by the Free Software Foundation, either version 2 of the
12 -- License, or (at your option) any later version.
13 --
14 -- Vicious is distributed in the hope that it will be useful,
15 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
16 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 -- GNU General Public License for more details.
18 --
19 -- You should have received a copy of the GNU General Public License
20 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
21
22 -- {{{ Grab environment
23 local type = type
24 local tonumber = tonumber
25
26 local helpers = require("vicious.helpers")
27 local spawn = require("vicious.spawn")
28 -- }}}
29
30 -- Wifiiw: provides wireless information for a requested interface
31 -- using iw instead of deprecated iwconfig
32 -- vicious.widgets.wifiiw
33 local wifiiw_linux = {}
34
35 local LINK = "PATH=$PATH:/sbin/:/usr/sbin:/usr/local/sbin iw dev %s link"
36 local INFO = "PATH=$PATH:/sbin/:/usr/sbin:/usr/local/sbin iw dev %s info"
37
38 -- {{{ Wireless widget type
39 function wifiiw_linux.async(format, warg, callback)
40 if type(warg) ~= "string" then return callback{} end
41 local winfo = {}
42
43 local function parse_link(stdout)
44 winfo["{bssid}"] = stdout:match"Connected to ([%x:]*)" or "N/A"
45 winfo["{ssid}"] = stdout:match"SSID: ([^\n]*)" or "N/A"
46 winfo["{freq}"] = tonumber(stdout:match"freq: (%d+)" or 0)
47 winfo["{sign}"] = -- Signal level can be negative; w/o unit (dBm)
48 tonumber(stdout:match"signal: (%-?%d+)" or 0)
49 winfo["{linp}"] = -- Link Quality (-100dBm->0%, -50dBm->100%)
50 winfo["{sign}"] ~= 0 and 200 + winfo["{sign}"]*2 or 0
51 winfo["{rate}"] = -- Transmission rate, without unit (Mb/s)
52 tonumber(stdout:match"tx bitrate: ([%d%.]+)" or 0)
53 end
54
55 local function parse_info(stdout)
56 winfo["{mode}"] = stdout:match"type ([^\n]*)" or "N/A"
57 winfo["{chan}"] = tonumber(stdout:match"channel (%d+)" or 0)
58 -- Transmission power, without unit (dBm)
59 winfo["{txpw}"] = tonumber(stdout:match"txpower (%-?%d+)" or 0)
60 end
61
62 spawn.easy_async_with_shell(
63 LINK:format(warg),
64 function (std_out, std_err, exit_reason, exit_code)
65 parse_link(std_out)
66 spawn.easy_async_with_shell(
67 INFO:format(warg),
68 function (stdout, stderr, exitreason, exitcode)
69 parse_info(stdout)
70 callback(winfo)
71 end)
72 end)
73 end
74 -- }}}
75
76 return helpers.setasyncall(wifiiw_linux)