modules/admin-full: display wifi rates in assoclist tables
[project/luci.git] / modules / admin-full / luasrc / view / admin_network / wifi_overview.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2008-2009 Steven Barth <steven@midlink.org>
4 Copyright 2008-2011 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 -%>
15
16 <%-
17
18 local sys = require "luci.sys"
19 local fs = require "luci.fs"
20 local utl = require "luci.util"
21 local uci = require "luci.model.uci".cursor()
22 local ntm = require "luci.model.network"
23
24 local has_iwinfo = pcall(require, "iwinfo")
25
26 ntm.init(uci)
27
28 function guess_wifi_hw(ifname)
29 local bands = ""
30 local name, idx = ifname:match("^([a-z]+)(%d+)")
31 idx = tonumber(idx)
32
33 if has_iwinfo then
34 local iw = luci.sys.wifi.getiwinfo(ifname)
35 local bl = iw.hwmodelist
36 if bl and next(bl) then
37 if bl.a then bands = bands .. "a" end
38 if bl.b then bands = bands .. "b" end
39 if bl.g then bands = bands .. "g" end
40 if bl.n then bands = bands .. "n" end
41 end
42 end
43
44 -- wl.o
45 if name == "wl" then
46 local name = "Broadcom 802.11%s Wireless Controller" % bands
47 local nm = 0
48
49 local fd = nixio.open("/proc/bus/pci/devices", "r")
50 if fd then
51 local ln
52 for ln in fd:linesource() do
53 if ln:match("wl$") then
54 if nm == idx then
55 local version = ln:match("^%S+%s+%S%S%S%S([0-9a-f]+)")
56 name = string.format(
57 "Broadcom BCM%04x 802.11 Wireless Controller",
58 tonumber(version, 16)
59 )
60
61 break
62 else
63 nm = nm + 1
64 end
65 end
66 end
67 fd:close()
68 end
69
70 return name
71
72 -- madwifi
73 elseif name == "ath" or name == "wifi" then
74 return "Atheros 802.11%s Wireless Controller" % bands
75
76 -- ralink
77 elseif name == "ra" then
78 return "RaLink 802.11%s Wireless Controller" % bands
79
80 -- hermes
81 elseif name == "eth" then
82 return "Hermes 802.11b Wireless Controller"
83
84 -- hostap
85 elseif name == "wlan" and fs.isdirectory("/proc/net/hostap/" .. ifname) then
86 return "Prism2/2.5/3 802.11b Wireless Controller"
87
88 -- dunno yet
89 else
90 return "Generic 802.11%s Wireless Controller" % bands
91 end
92 end
93
94 local devices = ntm:get_wifidevs()
95 local arpcache = { }
96 sys.net.arptable(function(e) arpcache[e["HW address"]:upper()] = e["IP address"] end)
97
98 local netlist = { }
99 local netdevs = { }
100
101 local dev
102 for _, dev in ipairs(devices) do
103 local net
104 for _, net in ipairs(dev:get_wifinets()) do
105 netlist[#netlist+1] = net:id()
106 netdevs[net:id()] = dev:name()
107 end
108 end
109 -%>
110
111 <%+header%>
112
113 <% if not has_iwinfo then %>
114 <div class="errorbox">
115 <strong><%:Package libiwinfo required!%></strong><br />
116 <%_The <em>libiwinfo-lua</em> package is not installed. You must install this component for working wireless configuration!%>
117 </div>
118 <% end %>
119
120 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
121 <script type="text/javascript">//<![CDATA[
122 var wifidevs = <%=luci.http.write_json(netdevs)%>;
123 var arptable = <%=luci.http.write_json(arpcache)%>;
124
125 var is_reconnecting = false;
126
127 function wifi_shutdown(id, toggle) {
128 var reconnect = (toggle.getAttribute('active') == 'false');
129
130 if (!reconnect && !confirm(String.format('<%:Really shutdown network ?\nYou might lose access to this router if you are connected via this interface.%>')))
131 return;
132
133 is_reconnecting = true;
134
135 var s = document.getElementById('iw-rc-status');
136 if (s)
137 {
138 s.parentNode.style.display = 'block';
139 s.innerHTML = '<%:Waiting for router...%>';
140 }
141
142 for (var net in wifidevs)
143 {
144 var st = document.getElementById(net + '-iw-status');
145 if (st)
146 st.innerHTML = '<em><%:Wireless is restarting...%></em>';
147 }
148
149 XHR.get('<%=luci.dispatcher.build_url("admin", "network")%>/wireless_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, null,
150 function(x)
151 {
152 if (s)
153 {
154 s.innerHTML = reconnect
155 ? '<%:Wireless restarted%>'
156 : '<%:Wireless shut down%>';
157
158 window.setTimeout(function() {
159 s.parentNode.style.display = 'none';
160 is_reconnecting = false;
161 }, 1000);
162 }
163 }
164 );
165 }
166
167 XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "network", "wireless_status", table.concat(netlist, ","))%>', null,
168 function(x, st)
169 {
170 if (st)
171 {
172 var assoctable = document.getElementById('iw-assoclist');
173 if (assoctable)
174 while (assoctable.rows.length > 1)
175 assoctable.rows[1].parentNode.removeChild(assoctable.rows[1]);
176
177 var devup = { };
178 var rowstyle = 1;
179
180 for( var i = 0; i < st.length; i++ )
181 {
182 var iw = st[i];
183 var is_assoc = (iw.bssid && iw.bssid != '00:00:00:00:00:00' && iw.channel);
184 var p = iw.quality;
185 var q = is_assoc ? p : -1;
186
187 var icon;
188 if (q < 0)
189 icon = "<%=resource%>/icons/signal-none.png";
190 else if (q == 0)
191 icon = "<%=resource%>/icons/signal-0.png";
192 else if (q < 25)
193 icon = "<%=resource%>/icons/signal-0-25.png";
194 else if (q < 50)
195 icon = "<%=resource%>/icons/signal-25-50.png";
196 else if (q < 75)
197 icon = "<%=resource%>/icons/signal-50-75.png";
198 else
199 icon = "<%=resource%>/icons/signal-75-100.png";
200
201 if (!devup[wifidevs[iw.id]])
202 devup[wifidevs[iw.id]] = is_assoc;
203
204 var sig = document.getElementById(iw.id + '-iw-signal');
205 if (sig)
206 sig.innerHTML = String.format(
207 '<img src="%s" title="<%:Signal%>: %d dBm / <%:Noise%>: %d dBm" /><br />' +
208 '<small>%d%%</small>', icon, iw.signal, iw.noise, p
209 );
210
211 var toggle = document.getElementById(iw.id + '-iw-toggle');
212 if (toggle)
213 {
214 if (is_assoc)
215 {
216 toggle.className = 'cbi-button cbi-button-reset';
217 toggle.value = '<%:Disable%>';
218 toggle.title = '<%:Shutdown this network%>';
219 }
220 else
221 {
222 toggle.className = 'cbi-button cbi-button-reload';
223 toggle.value = '<%:Enable%>';
224 toggle.title = '<%:Activate this network%>';
225 }
226
227 toggle.setAttribute('active', is_assoc);
228 }
229
230 var info = document.getElementById(iw.id + '-iw-status');
231 if (info)
232 {
233 if (is_assoc)
234 info.innerHTML = String.format(
235 '<strong><%:SSID%>:</strong> %h | ' +
236 '<strong><%:Mode%>:</strong> %s<br />' +
237 '<strong><%:BSSID%>:</strong> %s | ' +
238 '<strong><%:Encryption%>:</strong> %s',
239 iw.ssid, iw.mode, iw.bssid,
240 iw.encryption ? iw.encryption : '<%:None%>'
241 );
242 else
243 info.innerHTML = String.format(
244 '<strong><%:SSID%>:</strong> %h | ' +
245 '<strong><%:Mode%>:</strong> %s<br />' +
246 '<em>%s</em>',
247 iw.ssid || '?', iw.mode,
248 is_reconnecting
249 ? '<em><%:Wireless is restarting...%></em>'
250 : '<em><%:Wireless is disabled or not associated%></em>'
251 );
252 }
253
254 var dev = document.getElementById(wifidevs[iw.id] + '-iw-devinfo');
255 if (dev)
256 {
257 if (is_assoc)
258 dev.innerHTML = String.format(
259 '<strong><%:Channel%>:</strong> %s (%s GHz) | ' +
260 '<strong><%:Bitrate%>:</strong> %s Mb/s',
261 iw.channel ? iw.channel : '?',
262 iw.frequency ? iw.frequency : '?',
263 iw.bitrate ? iw.bitrate : '?'
264 );
265 else
266 dev.innerHTML = '';
267 }
268
269 if (assoctable)
270 {
271 var assoclist = [ ];
272 for( var bssid in iw.assoclist )
273 {
274 assoclist.push(iw.assoclist[bssid]);
275 assoclist[assoclist.length-1].bssid = bssid;
276 }
277
278 assoclist.sort(function(a, b) { a.bssid < b.bssid });
279
280 for( var j = 0; j < assoclist.length; j++ )
281 {
282 var tr = assoctable.insertRow(-1);
283 tr.className = 'cbi-section-table-row cbi-rowstyle-' + rowstyle;
284
285 var icon;
286 var q = (-1 * (assoclist[j].noise - assoclist[j].signal)) / 5;
287 if (q < 1)
288 icon = "<%=resource%>/icons/signal-0.png";
289 else if (q < 2)
290 icon = "<%=resource%>/icons/signal-0-25.png";
291 else if (q < 3)
292 icon = "<%=resource%>/icons/signal-25-50.png";
293 else if (q < 4)
294 icon = "<%=resource%>/icons/signal-50-75.png";
295 else
296 icon = "<%=resource%>/icons/signal-75-100.png";
297
298 tr.insertCell(-1).innerHTML = String.format(
299 '<img src="%s" title="<%:Signal%>: %d dBm / <%:Noise%>: %d dBm" />',
300 icon, assoclist[j].signal, assoclist[j].noise
301 );
302
303 tr.insertCell(-1).innerHTML = String.format('%h', iw.ssid ? iw.ssid : '?');
304 tr.insertCell(-1).innerHTML = assoclist[j].bssid;
305
306 tr.insertCell(-1).innerHTML = arptable[assoclist[j].bssid]
307 ? arptable[assoclist[j].bssid] : '?';
308
309 tr.insertCell(-1).innerHTML = String.format('%d dBm', assoclist[j].signal);
310 tr.insertCell(-1).innerHTML = String.format('%d dBm', assoclist[j].noise);
311
312 tr.insertCell(-1).innerHTML = (assoclist[j].rx_mcs > -1)
313 ? String.format('%.1f Mbit/s, MCS %d, %dMHz', assoclist[j].rx_rate / 1000, assoclist[j].rx_mcs, assoclist[j].rx_40mhz ? 40 : 20)
314 : String.format('%.1f Mbit/s', assoclist[j].rx_rate / 1000)
315 ;
316
317 tr.insertCell(-1).innerHTML = (assoclist[j].tx_mcs > -1)
318 ? String.format('%.1f Mbit/s, MCS %d, %dMHz', assoclist[j].tx_rate / 1000, assoclist[j].tx_mcs, assoclist[j].tx_40mhz ? 40 : 20)
319 : String.format('%.1f Mbit/s', assoclist[j].tx_rate / 1000)
320 ;
321
322 rowstyle = (rowstyle == 1) ? 2 : 1;
323 }
324 }
325 }
326
327 if (assoctable && assoctable.rows.length == 1)
328 {
329 var tr = assoctable.insertRow(-1);
330 tr.className = 'cbi-section-table-row';
331
332 var td = tr.insertCell(-1);
333 td.colSpan = 8;
334 td.innerHTML = '<br /><em><%:No information available%></em>';
335 }
336
337 for (var dev in devup)
338 {
339 var img = document.getElementById(dev + '-iw-upstate');
340 if (img)
341 img.src = '<%=resource%>/icons/wifi_big' + (devup[dev] ? '' : '_disabled') + '.png';
342 }
343 }
344 }
345 );
346 //]]></script>
347
348 <h2><a id="content" name="content"><%:Wireless Overview%></a></h2>
349
350 <fieldset class="cbi-section" style="display:none">
351 <legend><%:Reconnecting interface%></legend>
352 <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
353 <span id="iw-rc-status"><%:Waiting for router...%></span>
354 </fieldset>
355
356 <div class="cbi-map">
357
358 <% for _, dev in ipairs(devices) do local nets = dev:get_wifinets() %>
359 <!-- device <%=dev:name()%> -->
360 <fieldset class="cbi-section">
361 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
362 <!-- physical device -->
363 <tr>
364 <td style="width:34px"><img src="<%=resource%>/icons/wifi_big_disabled.png" style="float:left; margin-right:10px" id="<%=dev:name()%>-iw-upstate" /></td>
365 <td colspan="2" style="text-align:left">
366 <big><strong><%=guess_wifi_hw(dev:name())%> (<%=dev:name()%>)</strong></big><br />
367 <span id="<%=dev:name()%>-iw-devinfo"></span>
368 </td>
369 <td style="width:310px;text-align:right">
370 <input type="button" class="cbi-button cbi-button-find" style="width:100px" onclick="location.href='<%=luci.dispatcher.build_url("admin/network/wireless_join")%>?device=<%=dev:name()%>'" title="<%:Find and join network%>" value="<%:Scan%>" />
371 <input type="button" class="cbi-button cbi-button-add" style="width:100px" onclick="location.href='<%=luci.dispatcher.build_url("admin/network/wireless_add")%>?device=<%=dev:name()%>'" title="<%:Provide new network%>" value="<%:Add%>" />
372 </td>
373 </tr>
374 <!-- /physical device -->
375
376 <!-- network list -->
377 <% if #nets > 0 then %>
378 <% for i, net in ipairs(nets) do %>
379 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
380 <td></td>
381 <td class="cbi-value-field" style="width:16px; padding:3px" id="<%=net:id()%>-iw-signal">
382 <img src="<%=resource%>/icons/signal-none.png" title="<%:Not associated%>" /><br />
383 <small>0%</small>
384 </td>
385 <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net:id()%>-iw-status">
386 <em><%:Collecting data...%></em>
387 </td>
388 <td class="cbi-value-field" style="width:310px;text-align:right">
389 <input id="<%=net:id()%>-iw-toggle" type="button" class="cbi-button cbi-button-reload" style="width:100px" onclick="wifi_shutdown('<%=net:id()%>', this)" title="<%:Delete this network%>" value="<%:Enable%>" />
390 <input type="button" class="cbi-button cbi-button-edit" style="width:100px" onclick="location.href='<%=net:adminlink()%>'" title="<%:Edit this network%>" value="<%:Edit%>" />
391 <input type="button" class="cbi-button cbi-button-remove" style="width:100px" onclick="if (confirm('<%:Really delete this wireless network? The deletion cannot be undone!\nYou might lose access to this router if you are connected via this network.%>')) location.href='<%=luci.dispatcher.build_url("admin/network/wireless_delete", net:ifname())%>'" title="<%:Delete this network%>" value="<%:Remove%>" />
392 </td>
393 </tr>
394 <% end %>
395 <% else %>
396 <tr class="cbi-section-table-row cbi-rowstyle-2">
397 <td></td>
398 <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
399 <em><%:No network configured on this device%></em>
400 </td>
401 </tr>
402 <% end %>
403 <!-- /network list -->
404 </table>
405 </fieldset>
406 <!-- /device <%=dev:name()%> -->
407 <% end %>
408
409
410 <h2><a id="content" name="content"><%:Associated Stations%></a></h2>
411
412 <fieldset class="cbi-section">
413 <table class="cbi-section-table" style="margin:10px" id="iw-assoclist">
414 <tr class="cbi-section-table-titles">
415 <th class="cbi-section-table-cell"></th>
416 <th class="cbi-section-table-cell"><%:SSID%></th>
417 <th class="cbi-section-table-cell"><%:MAC%></th>
418 <th class="cbi-section-table-cell"><%:Address%></th>
419 <th class="cbi-section-table-cell"><%:Signal%></th>
420 <th class="cbi-section-table-cell"><%:Noise%></th>
421 <th class="cbi-section-table-cell"><%:RX Rate%></th>
422 <th class="cbi-section-table-cell"><%:TX Rate%></th>
423 </tr>
424 <tr class="cbi-section-table-row cbi-rowstyle-2">
425 <td class="cbi-value-field" colspan="8">
426 <em><%:Collecting data...%></em>
427 </td>
428 </tr>
429 </table>
430 </fieldset>
431 </div>
432
433 <%+footer%>