| 1 | -- Wi-Fi widget type for GNU/Linux using iwconfig |
| 2 | -- Copyright (C) 2010 Adrian C. <[email protected]> |
| 3 | -- Copyright (C) 2017 mutlusun <[email protected]> |
| 4 | -- Copyright (C) 2018-2019 Nguyễn Gia Phong <[email protected]m> |
| 5 | -- |
| 6 | -- This file is part of Vicious. |
| 7 | -- |
| 8 | -- Vicious is free software: you can redistribute it and/or modify |
| 9 | -- it under the terms of the GNU General Public License as |
| 10 | -- published by the Free Software Foundation, either version 2 of the |
| 11 | -- License, or (at your option) any later version. |
| 12 | -- |
| 13 | -- Vicious is distributed in the hope that it will be useful, |
| 14 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | -- GNU General Public License for more details. |
| 17 | -- |
| 18 | -- You should have received a copy of the GNU General Public License |
| 19 | -- along with Vicious. If not, see <https://www.gnu.org/licenses/>. |
| 20 | |
| 21 | -- {{{ Grab environment |
| 22 | local type = type |
| 23 | local tonumber = tonumber |
| 24 | local math = { floor = math.floor } |
| 25 | |
| 26 | local helpers = require"vicious.helpers" |
| 27 | local spawn = require"vicious.spawn" |
| 28 | -- }}} |
| 29 | |
| 30 | -- Wifi: provides wireless information for a requested interface using iwconfig |
| 31 | -- vicious.widgets.wifi |
| 32 | local wifi_linux = {} |
| 33 | |
| 34 | -- {{{ Wireless widget type |
| 35 | local function parser(stdout, stderr, exitreason, exitcode) |
| 36 | local winfo = {} |
| 37 | -- Output differs from system to system, stats can be separated by |
| 38 | -- either = or :. Some stats may not be supported by driver. |
| 39 | -- SSID can have almost anything in it. |
| 40 | winfo["{ssid}"] = stdout:match'ESSID[=:]"(.-)"' or "N/A" |
| 41 | -- Modes are simple, but also match the "-" in Ad-Hoc |
| 42 | winfo["{mode}"] = stdout:match"Mode[=:]([%w%-]+)" or "N/A" |
| 43 | winfo["{chan}"] = tonumber(stdout:match"Channel[=:](%d+)" or 0) |
| 44 | winfo["{rate}"] = -- Bitrate without unit (Mb/s) |
| 45 | tonumber(stdout:match"Bit Rate[=:]%s?([%d%.]+)" or 0) |
| 46 | winfo["{freq}"] = -- Frequency in MHz (is output always in GHz?) |
| 47 | tonumber(stdout:match"Frequency[=:]%s?([%d%.]+)" or 0) * 1000 |
| 48 | winfo["{txpw}"] = -- Transmission power in dBm |
| 49 | tonumber(stdout:match"Tx%-Power[=:](%d+)" or 0) |
| 50 | winfo["{link}"] = -- Link quality over 70 |
| 51 | tonumber(stdout:match"Link Quality[=:](%d+)" or 0) |
| 52 | winfo["{linp}"] = -- Link quality percentage if quality was available |
| 53 | winfo["{link}"] ~= 0 and math.floor(winfo["{link}"]/0.7 + 0.5) or 0 |
| 54 | -- Signal level without unit (dBm), can be negative value |
| 55 | winfo["{sign}"] = tonumber(stdout:match"Signal level[=:](%-?%d+)" or 0) |
| 56 | return winfo |
| 57 | end |
| 58 | |
| 59 | function wifi_linux.async(format, warg, callback) |
| 60 | if type(warg) ~= "string" then return callback{} end |
| 61 | spawn.easy_async_with_shell( |
| 62 | "PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin iwconfig " .. warg, |
| 63 | function (...) callback(parser(...)) end) |
| 64 | end |
| 65 | -- }}} |
| 66 | |
| 67 | return helpers.setasyncall(wifi_linux) |