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