Merge pull request #302 from chris5560/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                 XHR.get('<%=luci.dispatcher.build_url("admin", "network")%>/wireless_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, null,
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         XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "network", "wireless_status", table.concat(netlist, ","))%>', null,
171                 function(x, st)
172                 {
173                         if (st)
174                         {
175                                 var assoctable = document.getElementById('iw-assoclist');
176                                 if (assoctable)
177                                         while (assoctable.rows.length > 1)
178                                                 assoctable.rows[1].parentNode.removeChild(assoctable.rows[1]);
179
180                                 var devup = { };
181                                 var rowstyle = 1;
182
183                                 for( var i = 0; i < st.length; i++ )
184                                 {
185                                         var iw = st[i];
186                                         var is_assoc = (iw.bssid && iw.bssid != '00:00:00:00:00:00' && iw.channel && iw.mode != 'Unknown');
187                                         var p = iw.quality;
188                                         var q = is_assoc ? p : -1;
189
190                                         var icon;
191                                         if (q < 0)
192                                                 icon = "<%=resource%>/icons/signal-none.png";
193                                         else if (q == 0)
194                                                 icon = "<%=resource%>/icons/signal-0.png";
195                                         else if (q < 25)
196                                                 icon = "<%=resource%>/icons/signal-0-25.png";
197                                         else if (q < 50)
198                                                 icon = "<%=resource%>/icons/signal-25-50.png";
199                                         else if (q < 75)
200                                                 icon = "<%=resource%>/icons/signal-50-75.png";
201                                         else
202                                                 icon = "<%=resource%>/icons/signal-75-100.png";
203
204                                         if (!devup[wifidevs[iw.id]])
205                                                 devup[wifidevs[iw.id]] = is_assoc;
206
207                                         var sig = document.getElementById(iw.id + '-iw-signal');
208                                         if (sig)
209                                                 sig.innerHTML = String.format(
210                                                         '<img src="%s" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>" /><br />' +
211                                                         '<small>%d%%</small>', icon, iw.signal, iw.noise, p
212                                                 );
213
214                                         var toggle = document.getElementById(iw.id + '-iw-toggle');
215                                         if (toggle)
216                                         {
217                                                 if (!iw.disabled)
218                                                 {
219                                                         toggle.className = 'cbi-button cbi-button-reset';
220                                                         toggle.value = '<%:Disable%>';
221                                                         toggle.title = '<%:Shutdown this network%>';
222                                                 }
223                                                 else
224                                                 {
225                                                         toggle.className = 'cbi-button cbi-button-reload';
226                                                         toggle.value = '<%:Enable%>';
227                                                         toggle.title = '<%:Activate this network%>';
228                                                 }
229
230                                                 toggle.setAttribute('active', !iw.disabled);
231                                         }
232
233                                         var info = document.getElementById(iw.id + '-iw-status');
234                                         if (info)
235                                         {
236                                                 if (is_assoc)
237                                                         info.innerHTML = String.format(
238                                                                 '<strong><%:SSID%>:</strong> %h | ' +
239                                                                 '<strong><%:Mode%>:</strong> %s<br />' +
240                                                                 '<strong><%:BSSID%>:</strong> %s | ' +
241                                                                 '<strong><%:Encryption%>:</strong> %s',
242                                                                         iw.ssid, iw.mode, iw.bssid,
243                                                                         iw.encryption ? iw.encryption : '<%:None%>'
244                                                         );
245                                                 else
246                                                         info.innerHTML = String.format(
247                                                                 '<strong><%:SSID%>:</strong> %h | ' +
248                                                                 '<strong><%:Mode%>:</strong> %s<br />' +
249                                                                 '<em>%s</em>',
250                                                                         iw.ssid || '?', iw.mode,
251                                                                         is_reconnecting
252                                                                                 ? '<em><%:Wireless is restarting...%></em>'
253                                                                                 : '<em><%:Wireless is disabled or not associated%></em>'
254                                                         );
255                                         }
256
257                                         var dev = document.getElementById(wifidevs[iw.id] + '-iw-devinfo');
258                                         if (dev)
259                                         {
260                                                 if (is_assoc)
261                                                         dev.innerHTML = String.format(
262                                                                 '<strong><%:Channel%>:</strong> %s (%s <%:GHz%>) | ' +
263                                                                 '<strong><%:Bitrate%>:</strong> %s <%:Mbit/s%>',
264                                                                         iw.channel ? iw.channel : '?',
265                                                                         iw.frequency ? iw.frequency : '?',
266                                                                         iw.bitrate ? iw.bitrate : '?'
267                                                         );
268                                                 else
269                                                         dev.innerHTML = '';
270                                         }
271
272                                         if (assoctable)
273                                         {
274                                                 var assoclist = [ ];
275                                                 for( var bssid in iw.assoclist )
276                                                 {
277                                                         assoclist.push(iw.assoclist[bssid]);
278                                                         assoclist[assoclist.length-1].bssid = bssid;
279                                                 }
280
281                                                 assoclist.sort(function(a, b) { a.bssid < b.bssid });
282
283                                                 for( var j = 0; j < assoclist.length; j++ )
284                                                 {
285                                                         var tr = assoctable.insertRow(-1);
286                                                             tr.className = 'cbi-section-table-row cbi-rowstyle-' + rowstyle;
287
288                                                         var icon;
289                                                         var q = (-1 * (assoclist[j].noise - assoclist[j].signal)) / 5;
290                                                         if (q < 1)
291                                                                 icon = "<%=resource%>/icons/signal-0.png";
292                                                         else if (q < 2)
293                                                                 icon = "<%=resource%>/icons/signal-0-25.png";
294                                                         else if (q < 3)
295                                                                 icon = "<%=resource%>/icons/signal-25-50.png";
296                                                         else if (q < 4)
297                                                                 icon = "<%=resource%>/icons/signal-50-75.png";
298                                                         else
299                                                                 icon = "<%=resource%>/icons/signal-75-100.png";
300
301                                                         tr.insertCell(-1).innerHTML = String.format(
302                                                                 '<img src="%s" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>" />',
303                                                                 icon, assoclist[j].signal, assoclist[j].noise
304                                                         );
305
306                                                         tr.insertCell(-1).innerHTML = nowrap(String.format('%h', iw.ssid ? iw.ssid : '?'));
307                                                         tr.insertCell(-1).innerHTML = assoclist[j].bssid;
308
309                                                         tr.insertCell(-1).innerHTML = arptable[assoclist[j].bssid]
310                                                                 ? arptable[assoclist[j].bssid] : '?';
311
312                                                         tr.insertCell(-1).innerHTML = nowrap(String.format('%d <%:dBm%>', assoclist[j].signal));
313                                                         tr.insertCell(-1).innerHTML = nowrap(String.format('%d <%:dBm%>', assoclist[j].noise));
314
315                                                         tr.insertCell(-1).innerHTML = nowrap((assoclist[j].rx_mcs > -1)
316                                                                 ? String.format('%.1f <%:Mbit/s%>, MCS %d, %d<%:MHz%>', assoclist[j].rx_rate / 1000, assoclist[j].rx_mcs, assoclist[j].rx_40mhz ? 40 : 20)
317                                                                 : String.format('%.1f <%:Mbit/s%>', assoclist[j].rx_rate / 1000)
318                                                         );
319
320                                                         tr.insertCell(-1).innerHTML = nowrap((assoclist[j].tx_mcs > -1)
321                                                                 ? String.format('%.1f <%:Mbit/s%>, MCS %d, %d<%:MHz%>', assoclist[j].tx_rate / 1000, assoclist[j].tx_mcs, assoclist[j].tx_40mhz ? 40 : 20)
322                                                                 : String.format('%.1f <%:Mbit/s%>', assoclist[j].tx_rate / 1000)
323                                                         );
324
325                                                         rowstyle = (rowstyle == 1) ? 2 : 1;
326                                                 }
327                                         }
328                                 }
329
330                                 if (assoctable && assoctable.rows.length == 1)
331                                 {
332                                         var tr = assoctable.insertRow(-1);
333                                             tr.className = 'cbi-section-table-row';
334
335                                         var td = tr.insertCell(-1);
336                                             td.colSpan = 8;
337                                             td.innerHTML = '<br /><em><%:No information available%></em>';
338                                 }
339
340                                 for (var dev in devup)
341                                 {
342                                         var img = document.getElementById(dev + '-iw-upstate');
343                                         if (img)
344                                                 img.src = '<%=resource%>/icons/wifi_big' + (devup[dev] ? '' : '_disabled') + '.png';
345                                 }
346                         }
347                 }
348         );
349 //]]></script>
350
351 <h2><a id="content" name="content"><%:Wireless Overview%></a></h2>
352
353 <fieldset class="cbi-section" style="display:none">
354         <legend><%:Reconnecting interface%></legend>
355         <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
356         <span id="iw-rc-status"><%:Waiting for changes to be applied...%></span>
357 </fieldset>
358
359 <div class="cbi-map">
360
361         <% for _, dev in ipairs(devices) do local nets = dev:get_wifinets() %>
362         <!-- device <%=dev:name()%> -->
363         <fieldset class="cbi-section">
364                 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
365                         <!-- physical device -->
366                         <tr>
367                                 <td style="width:34px"><img src="<%=resource%>/icons/wifi_big_disabled.png" style="float:left; margin-right:10px" id="<%=dev:name()%>-iw-upstate" /></td>
368                                 <td colspan="2" style="text-align:left">
369                                         <big><strong><%=guess_wifi_hw(dev)%> (<%=dev:name()%>)</strong></big><br />
370                                         <span id="<%=dev:name()%>-iw-devinfo"></span>
371                                 </td>
372                                 <td style="width:310px;text-align:right">
373                                         <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%>" />
374                                         <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%>" />
375                                 </td>
376                         </tr>
377                         <!-- /physical device -->
378
379                         <!-- network list -->
380                         <% if #nets > 0 then %>
381                                 <% for i, net in ipairs(nets) do %>
382                                 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
383                                         <td></td>
384                                         <td class="cbi-value-field" style="width:16px; padding:3px" id="<%=net:id()%>-iw-signal">
385                                                 <img src="<%=resource%>/icons/signal-none.png" title="<%:Not associated%>" /><br />
386                                                 <small>0%</small>
387                                         </td>
388                                         <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net:id()%>-iw-status">
389                                                 <em><%:Collecting data...%></em>
390                                         </td>
391                                         <td class="cbi-value-field" style="width:310px;text-align:right">
392                                                 <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%>" />
393                                                 <input type="button" class="cbi-button cbi-button-edit" style="width:100px" onclick="location.href='<%=net:adminlink()%>'" title="<%:Edit this network%>" value="<%:Edit%>" />
394                                                 <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%>" />
395                                         </td>
396                                 </tr>
397                                 <% end %>
398                         <% else %>
399                                 <tr class="cbi-section-table-row cbi-rowstyle-2">
400                                         <td></td>
401                                         <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
402                                                 <em><%:No network configured on this device%></em>
403                                         </td>
404                                 </tr>
405                         <% end %>
406                         <!-- /network list -->
407                 </table>
408         </fieldset>
409         <!-- /device <%=dev:name()%> -->
410         <% end %>
411
412
413         <h2><a id="content" name="content"><%:Associated Stations%></a></h2>
414
415         <fieldset class="cbi-section">
416                 <table class="cbi-section-table" style="margin:10px" id="iw-assoclist">
417                         <tr class="cbi-section-table-titles">
418                                 <th class="cbi-section-table-cell"></th>
419                                 <th class="cbi-section-table-cell"><%:SSID%></th>
420                                 <th class="cbi-section-table-cell"><%:MAC-Address%></th>
421                                 <th class="cbi-section-table-cell"><%:IPv4-Address%></th>
422                                 <th class="cbi-section-table-cell"><%:Signal%></th>
423                                 <th class="cbi-section-table-cell"><%:Noise%></th>
424                                 <th class="cbi-section-table-cell"><%:RX Rate%></th>
425                                 <th class="cbi-section-table-cell"><%:TX Rate%></th>
426                         </tr>
427                         <tr class="cbi-section-table-row cbi-rowstyle-2">
428                                 <td class="cbi-value-field" colspan="8">
429                                         <em><%:Collecting data...%></em>
430                                 </td>
431                         </tr>
432                 </table>
433         </fieldset>
434 </div>
435
436 <%+footer%>