modules/admin-full: IE 8 JavaScript fixes...
[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-2010 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 utl = require "luci.util"
20         local uci = require "luci.model.uci".cursor()
21         local ntm = require "luci.model.network"
22
23         local has_iwinfo = pcall(require, "iwinfo")
24
25         ntm.init(uci)
26
27         function guess_wifi_hw(ifname)
28                 local bands = ""
29                 local name, idx = ifname:match("^([a-z]+)(%d+)")
30                 idx = tonumber(idx)
31
32                 if has_iwinfo then
33                         local iw = luci.sys.wifi.getiwinfo(ifname)
34                         local bl = iw.hwmodelist
35                         if bl and next(bl) then
36                                 if bl.a then bands = bands .. "a" end
37                                 if bl.b then bands = bands .. "b" end
38                                 if bl.g then bands = bands .. "g" end
39                                 if bl.n then bands = bands .. "n" end
40                         end
41                 end
42
43                 -- wl.o
44                 if name == "wl" then
45                         local name = "Broadcom 802.11%s Wireless Controller" % bands
46                         local nm   = 0
47
48                         local fd = nixio.open("/proc/bus/pci/devices", "r")
49                         if fd then
50                                 local ln
51                                 for ln in fd:linesource() do
52                                         if ln:match("wl$") then
53                                                 if nm == idx then
54                                                         local version = ln:match("^%S+%s+%S%S%S%S([0-9a-f]+)")
55                                                         name = string.format(
56                                                                 "Broadcom BCM%04x 802.11 Wireless Controller",
57                                                                 tonumber(version, 16)
58                                                         )
59
60                                                         break
61                                                 else
62                                                         nm = nm + 1
63                                                 end
64                                         end
65                                 end
66                                 fd:close()
67                         end
68
69                         return name
70
71                 -- madwifi
72                 elseif name == "ath" or name == "wifi" then
73                         return "Atheros 802.11%s Wireless Controller" % bands
74
75                 -- ralink
76                 elseif name == "ra" then
77                         return "RaLink 802.11%s Wireless Controller" % bands
78
79                 -- prism?
80                 elseif name == "eth" then
81                         return "Prism 802.11%s Wireless Controller" % bands
82
83                 -- dunno yet
84                 else
85                         return "Generic 802.11%s Wireless Controller" % bands
86                 end
87         end
88
89         local devices  = ntm:get_wifidevs()
90         local arpcache = { }
91         sys.net.arptable(function(e) arpcache[e["HW address"]:upper()] = e["IP address"] end)
92
93         local netlist = { }
94         local netdevs = { }
95
96         local dev
97         for _, dev in ipairs(devices) do
98                 local net
99                 for _, net in ipairs(dev:get_wifinets()) do
100                         netlist[#netlist+1] = net:ifname()
101                         netdevs[net:ifname()] = dev:name()
102                 end
103         end
104 -%>
105
106 <%+header%>
107
108 <% if not has_iwinfo then %>
109         <div class="errorbox">
110                 <strong><%:Package libiwinfo required!%></strong><br />
111                 <%_The <em>libiwinfo</em> package is not installed. You must install this component for working wireless configuration!%>
112         </div>
113 <% end %>
114
115 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
116 <script type="text/javascript">//<![CDATA[
117         var iwxhr = new XHR();
118         var wifidevs = <%=luci.http.write_json(netdevs)%>;
119         var arptable = <%=luci.http.write_json(arpcache)%>;
120
121         var update_status = function() {
122                 iwxhr.get('<%=luci.dispatcher.build_url("admin", "network", "wireless_status", table.concat(netlist, ","))%>', null,
123                         function(x, st)
124                         {
125                                 if (st)
126                                 {
127                                         var assoctable = document.getElementById('iw-assoclist');
128                                         if (assoctable)
129                                                 while (assoctable.rows.length > 1)
130                                                         assoctable.rows[1].parentNode.removeChild(assoctable.rows[1]);
131
132                                         var devup = { };
133
134                                         for( var i = 0; i < st.length; i++ )
135                                         {
136                                                 var iw = st[i];
137                                                 var is_assoc = (iw.bssid && iw.channel);
138                                                 var p = (100 / iw.quality_max * iw.quality);
139                                                 var q = is_assoc ? p : -1;
140
141                                                 var icon;
142                                                 if (q < 0)
143                                                         icon = "<%=resource%>/icons/signal-none.png";
144                                                 else if (q == 0)
145                                                         icon = "<%=resource%>/icons/signal-0.png";
146                                                 else if (q < 25)
147                                                         icon = "<%=resource%>/icons/signal-0-25.png";
148                                                 else if (q < 50)
149                                                         icon = "<%=resource%>/icons/signal-25-50.png";
150                                                 else if (q < 75)
151                                                         icon = "<%=resource%>/icons/signal-50-75.png";
152                                                 else
153                                                         icon = "<%=resource%>/icons/signal-75-100.png";
154
155                                                 if (!devup[wifidevs[iw.id]])
156                                                         devup[wifidevs[iw.id]] = is_assoc;
157
158                                                 var sig = document.getElementById(iw.id + '-iw-signal');
159                                                 if (sig)
160                                                         sig.innerHTML = String.format(
161                                                                 '<img src="%s" title="<%:Signal%>: %d dBm / <%:Noise%>: %d dBm" /><br />' +
162                                                                 '<small>%d%%</small>', icon, iw.signal, iw.noise, p
163                                                         );
164
165                                                 var info = document.getElementById(iw.id + '-iw-status');
166                                                 if (info)
167                                                 {
168                                                         if (is_assoc)
169                                                                 info.innerHTML = String.format(
170                                                                         '<strong><%:SSID%>:</strong> %h | ' +
171                                                                         '<strong><%:Mode%>:</strong> %s<br />' +
172                                                                         '<strong><%:BSSID%>:</strong> %s | ' +
173                                                                         '<strong><%:Encryption%>:</strong> %s',
174                                                                                 iw.ssid, iw.mode, iw.bssid,
175                                                                                 iw.encryption ? iw.encryption.description : '<%:None%>'
176                                                                 );
177                                                         else
178                                                                 info.innerHTML = String.format(
179                                                                         '<strong><%:SSID%>:</strong> %h | ' +
180                                                                         '<strong><%:Mode%>:</strong> %s<br />' +
181                                                                         '<em><%:Wireless is disabled or not associated%></em>',
182                                                                                 iw.ssid || '?', iw.mode
183                                                                 );
184                                                 }
185
186                                                 var dev = document.getElementById(wifidevs[iw.id] + '-iw-devinfo');
187                                                 if (dev)
188                                                 {
189                                                         if (is_assoc)
190                                                                 dev.innerHTML = String.format(
191                                                                         '<strong><%:Channel%>:</strong> %s (%s GHz) | ' +
192                                                                         '<strong><%:Bitrate%>:</strong> %s Mb/s',
193                                                                                 iw.channel ? iw.channel : '?',
194                                                                                 iw.frequency ? iw.frequency / 1000 : '?',
195                                                                                 iw.bitrate ? iw.bitrate / 1000 : '?'
196                                                                 );
197                                                         else
198                                                                 dev.innerHTML = '';
199                                                 }
200
201                                                 if (assoctable)
202                                                 {
203                                                         var assoclist = [ ];
204                                                         for( var bssid in iw.assoclist )
205                                                         {
206                                                                 assoclist.push(iw.assoclist[bssid]);
207                                                                 assoclist[assoclist.length-1].bssid = bssid;
208                                                         }
209
210                                                         assoclist.sort(function(a, b) { a.bssid < b.bssid });
211
212                                                         for( var j = 0; j < assoclist.length; j++ )
213                                                         {
214                                                                 var tr = assoctable.insertRow(-1);
215                                                                     tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((j % 2) + (i % 2));
216
217                                                                 var icon;
218                                                                 var q = (-1 * (assoclist[j].noise - assoclist[j].signal)) / 5;
219                                                                 if (q < 1)
220                                                                         icon = "<%=resource%>/icons/signal-0.png";
221                                                                 else if (q < 2)
222                                                                         icon = "<%=resource%>/icons/signal-0-25.png";
223                                                                 else if (q < 3)
224                                                                         icon = "<%=resource%>/icons/signal-25-50.png";
225                                                                 else if (q < 4)
226                                                                         icon = "<%=resource%>/icons/signal-50-75.png";
227                                                                 else
228                                                                         icon = "<%=resource%>/icons/signal-75-100.png";
229
230                                                                 tr.insertCell(-1).innerHTML = String.format(
231                                                                         '<img src="%s" title="<%:Signal%>: %d dBm / <%:Noise%>: %d dBm" />',
232                                                                         icon, assoclist[j].signal, assoclist[j].noise
233                                                                 );
234
235                                                                 tr.insertCell(-1).innerHTML = iw.ssid ? iw.ssid : '?';
236                                                                 tr.insertCell(-1).innerHTML = assoclist[j].bssid;
237
238                                                                 tr.insertCell(-1).innerHTML = arptable[assoclist[j].bssid]
239                                                                         ? arptable[assoclist[j].bssid] : '?';
240
241                                                                 tr.insertCell(-1).innerHTML = String.format('%d dBm', assoclist[j].signal);
242                                                                 tr.insertCell(-1).innerHTML = String.format('%d dBm', assoclist[j].noise);
243                                                         }
244                                                 }
245                                         }
246
247                                         if (assoctable && assoctable.rows.length == 1)
248                                         {
249                                                 var tr = assoctable.insertRow(-1);
250                                                     tr.className = 'cbi-section-table-row';
251
252                                                 var td = tr.insertCell(-1);
253                                                     td.colSpan = 6;
254                                                     td.innerHTML = '<br /><em><%:No information available%></em>';
255                                         }
256
257                                         for (var dev in devup)
258                                         {
259                                                 var img = document.getElementById(dev + '-iw-upstate');
260                                                 if (img)
261                                                         img.src = '<%=resource%>/icons/wifi_big' + (devup[dev] ? '' : '_disabled') + '.png';
262                                         }
263                                 }
264
265                                 window.setTimeout(update_status, 5000);
266                         }
267                 )
268         };
269
270         update_status();
271 //]]></script>
272
273 <h2><a id="content" name="content"><%:Wireless Overview%></a></h2>
274
275 <div class="cbi-map">
276
277         <% for _, dev in ipairs(devices) do local nets = dev:get_wifinets() %>
278         <!-- device <%=dev:name()%> -->
279         <fieldset class="cbi-section">
280                 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
281                         <!-- physical device -->
282                         <tr>
283                                 <td style="width:34px"><img src="<%=resource%>/icons/wifi_big_disabled.png" style="float:left; margin-right:10px" id="<%=dev:name()%>-iw-upstate" /></td>
284                                 <td colspan="2" style="text-align:left">
285                                         <big><strong><%=guess_wifi_hw(dev:name())%> (<%=dev:name()%>)</strong></big><br />
286                                         <span id="<%=dev:name()%>-iw-devinfo"></span>
287                                 </td>
288                                 <td style="width:40px">
289                                         <a href="<%=luci.dispatcher.build_url("admin/network/wireless_join")%>?device=<%=dev:name()%>"><img style="border:none" src="<%=resource%>/cbi/find.gif" alt="<%:Find and join network%>" title="<%:Find and join network%>" /></a>
290                                         <a href="<%=luci.dispatcher.build_url("admin/network/wireless_add")%>?device=<%=dev:name()%>"><img style="border:none" src="<%=resource%>/cbi/add.gif" alt="<%:Provide new network%>" title="<%:Provide new network%>" /></a>
291                                 </td>
292                         </tr>
293                         <!-- /physical device -->
294
295                         <!-- network list -->
296                         <% if #nets > 0 then %>
297                                 <% for i, net in ipairs(nets) do %>
298                                 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
299                                         <td></td>
300                                         <td class="cbi-value-field" style="width:16px; padding:3px" id="<%=net:ifname()%>-iw-signal">
301                                                 <img src="<%=resource%>/icons/signal-none.png" title="<%:Not associated%>" /><br />
302                                                 <small>0%</small>
303                                         </td>
304                                         <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net:ifname()%>-iw-status">
305                                                 <em><%:Collecting data...%></em>
306                                         </td>
307                                         <td class="cbi-value-field" style="width:40px">
308                                                 <a href="<%=net:adminlink()%>"><img style="border:none" src="<%=resource%>/cbi/edit.gif" alt="<%:Edit this network%>" title="<%:Edit this network%>" /></a>
309                                                 <a href="<%=luci.dispatcher.build_url("admin/network/wireless_delete", net:ifname())%>" onclick="return 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.%>')"><img style="border:none" src="<%=resource%>/cbi/remove.gif" alt="<%:Delete this network%>" title="<%:Delete this network%>" /></a>
310                                         </td>
311                                 </tr>
312                                 <% end %>
313                         <% else %>
314                                 <tr class="cbi-section-table-row cbi-rowstyle-2">
315                                         <td></td>
316                                         <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
317                                                 <em><%:No network configured on this device%></em>
318                                         </td>
319                                 </tr>
320                         <% end %>
321                         <!-- /network list -->
322                 </table>
323         </fieldset>
324         <!-- /device <%=dev:name()%> -->
325         <% end %>
326
327
328         <h2><a id="content" name="content"><%:Associated Stations%></a></h2>
329
330         <fieldset class="cbi-section">
331                 <table class="cbi-section-table" style="margin:10px; width:50%" id="iw-assoclist">
332                         <tr class="cbi-section-table-titles">
333                                 <th class="cbi-section-table-cell"></th>
334                                 <th class="cbi-section-table-cell"><%:SSID%></th>
335                                 <th class="cbi-section-table-cell"><%:MAC%></th>
336                                 <th class="cbi-section-table-cell"><%:Address%></th>
337                                 <th class="cbi-section-table-cell"><%:Signal%></th>
338                                 <th class="cbi-section-table-cell"><%:Noise%></th>
339                         </tr>
340                         <tr class="cbi-section-table-row cbi-rowstyle-2">
341                                 <td class="cbi-value-field" colspan="6">
342                                         <em><%:Collecting data...%></em>
343                                 </td>
344                         </tr>
345                 </table>
346         </fieldset>
347 </div>
348
349 <%+footer%>