modules/admin-full: fix some small style quirks
[project/luci.git] / modules / admin-full / luasrc / view / admin_network / wifi_overview.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2008-2009 Steven Barth <steven@midlink.org>
4 Copyright 2008-2011 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 -%>
15
16 <%-
17
18         local sys = require "luci.sys"
19         local fs = require "luci.fs"
20         local utl = require "luci.util"
21         local uci = require "luci.model.uci".cursor()
22         local ntm = require "luci.model.network"
23
24         local has_iwinfo = pcall(require, "iwinfo")
25
26         ntm.init(uci)
27
28         function guess_wifi_hw(ifname)
29                 local bands = ""
30                 local name, idx = ifname:match("^([a-z]+)(%d+)")
31                 idx = tonumber(idx)
32
33                 if has_iwinfo then
34                         local iw = luci.sys.wifi.getiwinfo(ifname)
35                         local bl = iw.hwmodelist
36                         if bl and next(bl) then
37                                 if bl.a then bands = bands .. "a" end
38                                 if bl.b then bands = bands .. "b" end
39                                 if bl.g then bands = bands .. "g" end
40                                 if bl.n then bands = bands .. "n" end
41                         end
42                 end
43
44                 -- wl.o
45                 if name == "wl" then
46                         local name = "Broadcom 802.11%s Wireless Controller" % bands
47                         local nm   = 0
48
49                         local fd = nixio.open("/proc/bus/pci/devices", "r")
50                         if fd then
51                                 local ln
52                                 for ln in fd:linesource() do
53                                         if ln:match("wl$") then
54                                                 if nm == idx then
55                                                         local version = ln:match("^%S+%s+%S%S%S%S([0-9a-f]+)")
56                                                         name = string.format(
57                                                                 "Broadcom BCM%04x 802.11 Wireless Controller",
58                                                                 tonumber(version, 16)
59                                                         )
60
61                                                         break
62                                                 else
63                                                         nm = nm + 1
64                                                 end
65                                         end
66                                 end
67                                 fd:close()
68                         end
69
70                         return name
71
72                 -- madwifi
73                 elseif name == "ath" or name == "wifi" then
74                         return "Atheros 802.11%s Wireless Controller" % bands
75
76                 -- ralink
77                 elseif name == "ra" then
78                         return "RaLink 802.11%s Wireless Controller" % bands
79
80                 -- hermes
81                 elseif name == "eth" then
82                         return "Hermes 802.11b Wireless Controller"
83
84                 -- hostap
85                 elseif name == "wlan" and fs.isdirectory("/proc/net/hostap/" .. ifname) then
86                         return "Prism2/2.5/3 802.11b Wireless Controller"
87
88                 -- dunno yet
89                 else
90                         return "Generic 802.11%s Wireless Controller" % bands
91                 end
92         end
93
94         local devices  = ntm:get_wifidevs()
95         local arpcache = { }
96         sys.net.arptable(function(e) arpcache[e["HW address"]:upper()] = e["IP address"] end)
97
98         local netlist = { }
99         local netdevs = { }
100
101         local dev
102         for _, dev in ipairs(devices) do
103                 local net
104                 for _, net in ipairs(dev:get_wifinets()) do
105                         netlist[#netlist+1] = net:id()
106                         netdevs[net:id()] = dev:name()
107                 end
108         end
109 -%>
110
111 <%+header%>
112
113 <% if not has_iwinfo then %>
114         <div class="errorbox">
115                 <strong><%:Package libiwinfo required!%></strong><br />
116                 <%_The <em>libiwinfo</em> package is not installed. You must install this component for working wireless configuration!%>
117         </div>
118 <% end %>
119
120 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
121 <script type="text/javascript">//<![CDATA[
122         var iwxhr = new XHR();
123         var wifidevs = <%=luci.http.write_json(netdevs)%>;
124         var arptable = <%=luci.http.write_json(arpcache)%>;
125
126         var is_reconnecting = false;
127
128         function wifi_shutdown(id, toggle) {
129                 var reconnect = (toggle.getAttribute('active') == 'false');
130
131                 if (!reconnect && !confirm(String.format('<%:Really shutdown network ?\nYou might loose access to this router 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 router...%>';
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                 var rcxhr = new XHR();
151                 rcxhr.get('<%=luci.dispatcher.build_url("admin", "network")%>/wireless_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, null,
152                         function(x)
153                         {
154                                 if (s)
155                                 {
156                                         s.innerHTML = reconnect
157                                                 ? '<%:Wireless restarted%>'
158                                                 : '<%:Wireless shut down%>';
159
160                                         window.setTimeout(function() {
161                                                 s.parentNode.style.display = 'none';
162                                                 is_reconnecting = false;
163                                         }, 1000);
164                                 }
165                         }
166                 );
167         }
168
169         var update_status = function() {
170                 iwxhr.get('<%=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);
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 (is_assoc)
218                                                         {
219                                                                 toggle.style.backgroundImage = 'url(<%=resource%>/cbi/reset.gif)';
220                                                                 toggle.value = '<%:Disable%>';
221                                                                 toggle.title = '<%:Shutdown this network%>';
222                                                         }
223                                                         else
224                                                         {
225                                                                 toggle.style.backgroundImage = 'url(<%=resource%>/cbi/reload.gif)';
226                                                                 toggle.value = '<%:Enable%>';
227                                                                 toggle.title = '<%:Activate this network%>';
228
229                                                         }
230
231                                                         toggle.setAttribute('active', is_assoc);
232                                                 }
233
234                                                 var info = document.getElementById(iw.id + '-iw-status');
235                                                 if (info)
236                                                 {
237                                                         if (is_assoc)
238                                                                 info.innerHTML = String.format(
239                                                                         '<strong><%:SSID%>:</strong> %h | ' +
240                                                                         '<strong><%:Mode%>:</strong> %s<br />' +
241                                                                         '<strong><%:BSSID%>:</strong> %s | ' +
242                                                                         '<strong><%:Encryption%>:</strong> %s',
243                                                                                 iw.ssid, iw.mode, iw.bssid,
244                                                                                 iw.encryption ? iw.encryption : '<%:None%>'
245                                                                 );
246                                                         else
247                                                                 info.innerHTML = String.format(
248                                                                         '<strong><%:SSID%>:</strong> %h | ' +
249                                                                         '<strong><%:Mode%>:</strong> %s<br />' +
250                                                                         '<em>%s</em>',
251                                                                                 iw.ssid || '?', iw.mode,
252                                                                                 is_reconnecting
253                                                                                         ? '<em><%:Wireless is restarting...%></em>'
254                                                                                         : '<em><%:Wireless is disabled or not associated%></em>'
255                                                                 );
256                                                 }
257
258                                                 var dev = document.getElementById(wifidevs[iw.id] + '-iw-devinfo');
259                                                 if (dev)
260                                                 {
261                                                         if (is_assoc)
262                                                                 dev.innerHTML = String.format(
263                                                                         '<strong><%:Channel%>:</strong> %s (%s GHz) | ' +
264                                                                         '<strong><%:Bitrate%>:</strong> %s Mb/s',
265                                                                                 iw.channel ? iw.channel : '?',
266                                                                                 iw.frequency ? iw.frequency : '?',
267                                                                                 iw.bitrate ? iw.bitrate : '?'
268                                                                 );
269                                                         else
270                                                                 dev.innerHTML = '';
271                                                 }
272
273                                                 if (assoctable)
274                                                 {
275                                                         var assoclist = [ ];
276                                                         for( var bssid in iw.assoclist )
277                                                         {
278                                                                 assoclist.push(iw.assoclist[bssid]);
279                                                                 assoclist[assoclist.length-1].bssid = bssid;
280                                                         }
281
282                                                         assoclist.sort(function(a, b) { a.bssid < b.bssid });
283
284                                                         for( var j = 0; j < assoclist.length; j++ )
285                                                         {
286                                                                 var tr = assoctable.insertRow(-1);
287                                                                     tr.className = 'cbi-section-table-row cbi-rowstyle-' + rowstyle;
288
289                                                                 var icon;
290                                                                 var q = (-1 * (assoclist[j].noise - assoclist[j].signal)) / 5;
291                                                                 if (q < 1)
292                                                                         icon = "<%=resource%>/icons/signal-0.png";
293                                                                 else if (q < 2)
294                                                                         icon = "<%=resource%>/icons/signal-0-25.png";
295                                                                 else if (q < 3)
296                                                                         icon = "<%=resource%>/icons/signal-25-50.png";
297                                                                 else if (q < 4)
298                                                                         icon = "<%=resource%>/icons/signal-50-75.png";
299                                                                 else
300                                                                         icon = "<%=resource%>/icons/signal-75-100.png";
301
302                                                                 tr.insertCell(-1).innerHTML = String.format(
303                                                                         '<img src="%s" title="<%:Signal%>: %d dBm / <%:Noise%>: %d dBm" />',
304                                                                         icon, assoclist[j].signal, assoclist[j].noise
305                                                                 );
306
307                                                                 tr.insertCell(-1).innerHTML = iw.ssid ? iw.ssid : '?';
308                                                                 tr.insertCell(-1).innerHTML = assoclist[j].bssid;
309
310                                                                 tr.insertCell(-1).innerHTML = arptable[assoclist[j].bssid]
311                                                                         ? arptable[assoclist[j].bssid] : '?';
312
313                                                                 tr.insertCell(-1).innerHTML = String.format('%d dBm', assoclist[j].signal);
314                                                                 tr.insertCell(-1).innerHTML = String.format('%d dBm', assoclist[j].noise);
315
316                                                                 rowstyle = (rowstyle == 1) ? 2 : 1;
317                                                         }
318                                                 }
319                                         }
320
321                                         if (assoctable && assoctable.rows.length == 1)
322                                         {
323                                                 var tr = assoctable.insertRow(-1);
324                                                     tr.className = 'cbi-section-table-row';
325
326                                                 var td = tr.insertCell(-1);
327                                                     td.colSpan = 6;
328                                                     td.innerHTML = '<br /><em><%:No information available%></em>';
329                                         }
330
331                                         for (var dev in devup)
332                                         {
333                                                 var img = document.getElementById(dev + '-iw-upstate');
334                                                 if (img)
335                                                         img.src = '<%=resource%>/icons/wifi_big' + (devup[dev] ? '' : '_disabled') + '.png';
336                                         }
337                                 }
338
339                                 window.setTimeout(update_status, 5000);
340                         }
341                 )
342         };
343
344         update_status();
345 //]]></script>
346
347 <h2><a id="content" name="content"><%:Wireless Overview%></a></h2>
348
349 <fieldset class="cbi-section" style="display:none">
350         <legend><%:Reconnecting interface%></legend>
351         <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
352         <span id="iw-rc-status"><%:Waiting for router...%></span>
353 </fieldset>
354
355 <div class="cbi-map">
356
357         <% for _, dev in ipairs(devices) do local nets = dev:get_wifinets() %>
358         <!-- device <%=dev:name()%> -->
359         <fieldset class="cbi-section">
360                 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
361                         <!-- physical device -->
362                         <tr>
363                                 <td style="width:34px"><img src="<%=resource%>/icons/wifi_big_disabled.png" style="float:left; margin-right:10px" id="<%=dev:name()%>-iw-upstate" /></td>
364                                 <td colspan="2" style="text-align:left">
365                                         <big><strong><%=guess_wifi_hw(dev:name())%> (<%=dev:name()%>)</strong></big><br />
366                                         <span id="<%=dev:name()%>-iw-devinfo"></span>
367                                 </td>
368                                 <td style="width:250px;text-align:right">
369                                         <input type="button" class="cbi-button cbi-button-add" style="width:80px;background-image:url(<%=resource%>/cbi/find.gif)" onclick="location.href='<%=luci.dispatcher.build_url("admin/network/wireless_join")%>?device=<%=dev:name()%>'" title="<%:Find and join network%>" value="<%:Scan%>" />
370                                         <input type="button" class="cbi-button cbi-button-add" style="width:80px;background-image:url(<%=resource%>/cbi/add.gif)" onclick="location.href='<%=luci.dispatcher.build_url("admin/network/wireless_add")%>?device=<%=dev:name()%>'" title="<%:Provide new network%>" value="<%:Add%>" />
371                                 </td>
372                         </tr>
373                         <!-- /physical device -->
374
375                         <!-- network list -->
376                         <% if #nets > 0 then %>
377                                 <% for i, net in ipairs(nets) do %>
378                                 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
379                                         <td></td>
380                                         <td class="cbi-value-field" style="width:16px; padding:3px" id="<%=net:id()%>-iw-signal">
381                                                 <img src="<%=resource%>/icons/signal-none.png" title="<%:Not associated%>" /><br />
382                                                 <small>0%</small>
383                                         </td>
384                                         <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net:id()%>-iw-status">
385                                                 <em><%:Collecting data...%></em>
386                                         </td>
387                                         <td class="cbi-value-field" style="width:250px;text-align:right">
388                                                 <input id="<%=net:id()%>-iw-toggle" type="button" class="cbi-button cbi-button-add" style="width:80px;background-image:url(<%=resource%>/cbi/reload.gif)" onclick="wifi_shutdown('<%=net:id()%>', this)" title="<%:Delete this network%>" value="<%:Enable%>" />
389                                                 <input type="button" class="cbi-button cbi-button-add" style="width:80px;background-image:url(<%=resource%>/cbi/edit.gif)" onclick="location.href='<%=net:adminlink()%>'" title="<%:Edit this network%>" value="<%:Edit%>" />
390                                                 <input type="button" class="cbi-button cbi-button-add" style="width:80px;background-image:url(<%=resource%>/cbi/remove.gif)" onclick="if (confirm('<%:Really delete this wireless network? The deletion cannot be undone!\nYou might loose access to this router 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%>" />
391                                         </td>
392                                 </tr>
393                                 <% end %>
394                         <% else %>
395                                 <tr class="cbi-section-table-row cbi-rowstyle-2">
396                                         <td></td>
397                                         <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
398                                                 <em><%:No network configured on this device%></em>
399                                         </td>
400                                 </tr>
401                         <% end %>
402                         <!-- /network list -->
403                 </table>
404         </fieldset>
405         <!-- /device <%=dev:name()%> -->
406         <% end %>
407
408
409         <h2><a id="content" name="content"><%:Associated Stations%></a></h2>
410
411         <fieldset class="cbi-section">
412                 <table class="cbi-section-table" style="margin:10px; width:50%" id="iw-assoclist">
413                         <tr class="cbi-section-table-titles">
414                                 <th class="cbi-section-table-cell"></th>
415                                 <th class="cbi-section-table-cell"><%:SSID%></th>
416                                 <th class="cbi-section-table-cell"><%:MAC%></th>
417                                 <th class="cbi-section-table-cell"><%:Address%></th>
418                                 <th class="cbi-section-table-cell"><%:Signal%></th>
419                                 <th class="cbi-section-table-cell"><%:Noise%></th>
420                         </tr>
421                         <tr class="cbi-section-table-row cbi-rowstyle-2">
422                                 <td class="cbi-value-field" colspan="6">
423                                         <em><%:Collecting data...%></em>
424                                 </td>
425                         </tr>
426                 </table>
427         </fieldset>
428 </div>
429
430 <%+footer%>