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