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