Debian Codegraph @

flaw/wifi.lua

raw symbols SCIP
1 -- flaw, a Lua OO management framework for Awesome WM widgets.
2 -- Copyright (C) 2011 David Soulayrol <david.soulayrol AT gmail DOT net>
3
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of the GNU General Public License as published by
6 -- the Free Software Foundation, either version 3 of the License, or
7 -- (at your option) any later version.
8
9 -- This program is distributed in the hope that it will be useful,
10 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
11 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 -- GNU General Public License for more details.
13
14 -- You should have received a copy of the GNU General Public License
15 -- along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 -- Grab environment.
19 local io = io
20 local string = string
21 local tonumber = tonumber
22
23 local lfs = require('lfs')
24
25 local flaw = {
26 gadget = require('flaw.gadget'),
27 helper = require('flaw.helper'),
28 provider = require('flaw.provider')
29 }
30
31
32 --- Wifi information.
33 --
34 -- <p>This module contains a provider for wifi information and two
35 -- gadgets: a text gadget and an icon gadget.</p>
36 --
37 -- <h2>Gadgets</h2>
38 --
39 -- <p>The ID of the gadgets designates the network device to be
40 -- monitored. Wifi gadgets provide no custom parameters. See the <a
41 -- href="flaw.gadget.html">gadget</a> module documentation to learn
42 -- about standard gadgets parameters.</p>
43 --
44 -- <h3>Icon Gadget</h3>
45 --
46 -- <p>The wifi icon gadget can be instantiated by indexing the
47 -- gadget module with <code>icon.wifi</code>. Here is an exemple
48 -- which assumes the icon path is stored in a <b>beautiful</b>
49 -- property.</p>
50 --
51 -- <div class='example'>
52 -- g = flaw.gadget.icon.wifi(<br/>
53 -- &nbsp;&nbsp;&nbsp;'wlan0', {}, { image = image(beautiful.wifi_icon) })<br/>
54 -- </div>
55 --
56 -- <h3>Text Gadget</h3>
57 --
58 -- <p>The wifi text gadget can be instantiated by indexing the
59 -- gadget module with <code>text.wifi</code>. By default, the
60 -- gadget pattern is <code>$essid: $rate Mb/s</code>. See the
61 -- provider's documentation below to learn about the available
62 -- variables.</p>
63 --
64 -- <div class='example'>
65 -- g = flaw.gadget.text.wifi('wlan1')
66 -- </div>
67 --
68 -- <h2>Provider</h2>
69 --
70 -- <p>The wifi provider loads its information from
71 -- <code>iwconfig</code> output (generally from the wireless-tools
72 -- package). The use of <code>iwconfig</code> was prefered to
73 -- <code>iwgetid</code> (which output if far simpler to parse) because
74 -- it returns all data at once and so limits the number of processes
75 -- to launch.</p>
76 --
77 -- <p>The wifi provider stores the data for each configured wireless
78 -- device at once. Data is composed of the following fields.</p>
79 --
80 -- <ul>
81 --
82 -- <li><code>essid</code>
83 --
84 -- <p>The name of the network the device is connected to (or
85 -- <code>N/A</code>).</p></li>
86 --
87 -- <li><code>mode</code>
88 --
89 -- <p>The mode of the wireless connection (generally
90 -- <code>Managed</code> or <code>Ad-Hoc</code>).</p></li>
91 --
92 -- <li><code>channel</code>
93 --
94 -- <p>The channel used by the wireless connection, as a
95 -- number</p></li>
96 --
97 -- <li><code>freq</code>
98 --
99 -- <p>The frequency used by the wireless adapter as a number in
100 -- MHz.</p></li>
101 --
102 -- <li><code>ap</code>
103 --
104 -- <p>The wireless access-point MAC address.</p></li>
105 --
106 -- <li><code>rate</code>
107 --
108 -- <p>The connection bit rate, as a number in Mb/s.</p></li>
109 --
110 -- <li><code>quality</code>
111 --
112 -- <p>The link quality as returned by <code>iwconfig</code>, in its
113 -- fraction format.</p></li>
114 --
115 -- <li><code>level</code>
116 --
117 -- <p>Ths signal level as a number.</p></li>
118 --
119 -- </ul>
120 --
121 --
122 -- @author David Soulayrol &lt;david.soulayrol AT gmail DOT com&gt;
123 -- @copyright 2011, David Soulayrol
124 module('flaw.wifi')
125
126
127 --- The wifi provider prototype.
128 --
129 -- <p>The wifi provider type is set to <code>wifi._NAME</code>.</p>
130 --
131 -- @class table
132 -- @name WifiProvider
133 WifiProvider = flaw.provider.CyclicProvider:new{ type = _NAME }
134
135
136 --- Parse one entry from <code>iwconfig</code> output.
137 --
138 -- @param bloc the text representing information for one device. The
139 -- first characters of this bloc are the name of the device
140 -- which is analysed.
141 function WifiProvider:parse_adapter_info(bloc)
142 local adapter = string.match(bloc, '([%w%d]+)')
143 self.data[adapter] = {}
144
145 self.data[adapter].essid =
146 string.match(bloc, 'ESSID[=:]"(.-)"') or 'N/A'
147 self.data[adapter].ap =
148 string.match(bloc, 'Access Point[=:] ([%w%d%:]+)') or 'No Access-Point'
149 self.data[adapter].mode =
150 string.match(bloc, "Mode[=:]([%w%-]*)") or ''
151 self.data[adapter].freq =
152 tonumber(string.match(bloc, "Frequency[=:]([%d%.]+)")) or 0
153 self.data[adapter].channel =
154 tonumber(string.match(bloc, "Channel[=:]([%d]+)")) or 0
155 self.data[adapter].rate =
156 tonumber(string.match(bloc, "Bit Rate[=:]([%s]*[%d%.]*)")) or 0
157 self.data[adapter].quality =
158 string.match(bloc, "Link Quality[=:]([%d]+/[%d]+)") or 'N/A'
159 self.data[adapter].level =
160 tonumber(string.match(bloc, "Signal level[=:]([%-]?[%d]+)")) or 0
161 end
162
163 --- Callback for provider refresh.
164 function WifiProvider:do_refresh()
165 local file = io.popen('/sbin/iwconfig 2> /dev/null', 'r')
166 local content = ''
167 local line = false
168
169 if file then
170 while line ~= nil do
171 line = file:read()
172 if line == '' then
173 self:parse_adapter_info(content)
174 elseif line ~= nil then
175 content = content .. line
176 end
177 end
178 file:close()
179 end
180 end
181
182 --- A factory for wifi providers.
183 --
184 -- <p>Only one provider is built for all wireless adapters. Its data
185 -- is a map indexed by the name of the wireless device.</p>
186 --
187 -- @return a brand new wireless provider, or the existing one if found
188 -- in the providers cache.
189 function provider_factory()
190 local p = flaw.provider.get(_NAME, '')
191 -- Create the provider if necessary.
192 if p == nil then
193 p = WifiProvider:new{ id = '', data = {} }
194 flaw.provider.add(p)
195 end
196 return p
197 end
198
199 --- Module initialization routine.
200 --
201 -- <p>This method tests the available information on the system. If
202 -- wireless devices can can be found, it registers gadgets and returns
203 -- the module; otherwise it simply returns nil.</p>
204 --
205 -- @return return this module if it can be used on the system,
206 -- false otherwise.
207 function init()
208 local file = '/proc/net/wireless'
209 if lfs.attributes(file, 'mode') == 'file' then
210 local t = {}
211 if flaw.helper.file.load_state_file('', file, t) > 0 then
212
213 -- An icon gadget prototype for wifi status display.
214 flaw.gadget.register.icon(_M)
215
216 -- A Text gadget prototype for wifi status display.
217 flaw.gadget.register.text(_M, { pattern = '$essid: $rate Mb/s' })
218
219 return _M
220 end
221 end
222 end