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