1 -- Authors: Relu Patrascu <ikoflexer@gmail.com>
2 -- License: LGPL, version 2.1 or later
3 -- Last Changed: 2004-11-17
7 -- Copyright (c) Relu Patrascu 2004.
9 -- Ion is free software; you can redistribute it and/or modify it under
10 -- the terms of the GNU Lesser General Public License as published by
11 -- the Free Software Foundation; either version 2.1 of the License, or
12 -- (at your option) any later version.
16 -- Neither am I a lua expert nor do I have the time to invest in writing
17 -- better code here. I simply needed this utility and it works OK for me.
18 -- I give no guarantees of any kind for this code. Suggestions for
19 -- improvement are welcome.
20 -- ikoflexer at gmail dot com
23 -- ikoflexer at gmail dot com
25 -- Made it return a space " " instead of empty string ""
26 -- when /proc/net/wireless doesn't report any interface.
27 -- Otherwise old info lingers in the status bar.
29 if not statusd_iwinfo
then
30 statusd_iwinfo
= { interval
= 10*1000 }
35 local function get_iwinfo_iwcfg()
36 local f
= io
.open('/proc/net/wireless', 'r')
40 -- first 2 lines -- headers
43 -- the third line has wifi info
44 local s
= f
:read('*l')
46 local st
, en
, iface
= 0, 0, 0
50 st
, en
, iface
= string.find(s
, '(%w+):')
51 local f1
= io
.popen("/sbin/iwconfig " .. iface
)
55 local iwOut
= f1
:read('*a')
57 st
,en
,proto
= string.find(iwOut
, '(802.11[%-]*%a*)')
58 st
,en
,ssid
= string.find(iwOut
, 'ESSID[=:]"([%w+%s*]*)"', en
)
59 st
,en
,bitrate
= string.find(iwOut
, 'Bit Rate[=:]([%s%w%.]*%/%a+)', en
)
60 bitrate
= string.gsub(bitrate
, "%s", "")
61 st
,en
,linkq
= string.find(iwOut
, 'Link Quality[=:](%d+%/%d+)', en
)
62 st
,en
,signal
= string.find(iwOut
, 'Signal level[=:](%-%d+)', en
)
63 st
,en
,noise
= string.find(iwOut
, 'Noise level[=:](%-%d+)', en
)
65 return proto
, ssid
, bitrate
, linkq
, signal
, noise
69 local function update_iwinfo()
70 local proto
, ssid
, bitrate
, linkq
, signal
, noise
= get_iwinfo_iwcfg()
72 -- In case get_iwinfo_iwcfg doesn't return any values we don't want stupid lua
73 -- errors about concatenating nil values.
75 bitrate
= bitrate
or "N/A"
76 linkq
= linkq
or "N/A"
77 signal
= signal
or "N/A"
78 noise
= noise
or "N/A"
79 proto
= proto
or "N/A"
81 statusd
.inform("iwinfo_cfg", ssid
.." "..bitrate
.." "..linkq
.." "..signal
.."/"..noise
.."dBm "..proto
)
82 statusd
.inform("iwinfo_ssid", ssid
)
83 statusd
.inform("iwinfo_bitrate", bitrate
)
84 statusd
.inform("iwinfo_linkq", linkq
)
85 statusd
.inform("iwinfo_signal", signal
)
86 statusd
.inform("iwinfo_noise", noise
)
87 statusd
.inform("iwinfo_proto", proto
)
88 iwinfo_timer
:set(statusd_iwinfo
.interval
, update_iwinfo
)
92 iwinfo_timer
= statusd
.create_timer()