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