8e64d1422cc893ae1200518b73ce655030f0f878
[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 update_status = function() {
127                 iwxhr.get('<%=luci.dispatcher.build_url("admin", "network", "wireless_status", table.concat(netlist, ","))%>', null,
128                         function(x, st)
129                         {
130                                 if (st)
131                                 {
132                                         var assoctable = document.getElementById('iw-assoclist');
133                                         if (assoctable)
134                                                 while (assoctable.rows.length > 1)
135                                                         assoctable.rows[1].parentNode.removeChild(assoctable.rows[1]);
136
137                                         var devup = { };
138                                         var rowstyle = 1;
139
140                                         for( var i = 0; i < st.length; i++ )
141                                         {
142                                                 var iw = st[i];
143                                                 var is_assoc = (iw.bssid && iw.bssid != '00:00:00:00:00:00' && iw.channel);
144                                                 var p = iw.quality;
145                                                 var q = is_assoc ? p : -1;
146
147                                                 var icon;
148                                                 if (q < 0)
149                                                         icon = "<%=resource%>/icons/signal-none.png";
150                                                 else if (q == 0)
151                                                         icon = "<%=resource%>/icons/signal-0.png";
152                                                 else if (q < 25)
153                                                         icon = "<%=resource%>/icons/signal-0-25.png";
154                                                 else if (q < 50)
155                                                         icon = "<%=resource%>/icons/signal-25-50.png";
156                                                 else if (q < 75)
157                                                         icon = "<%=resource%>/icons/signal-50-75.png";
158                                                 else
159                                                         icon = "<%=resource%>/icons/signal-75-100.png";
160
161                                                 if (!devup[wifidevs[iw.id]])
162                                                         devup[wifidevs[iw.id]] = is_assoc;
163
164                                                 var sig = document.getElementById(iw.id + '-iw-signal');
165                                                 if (sig)
166                                                         sig.innerHTML = String.format(
167                                                                 '<img src="%s" title="<%:Signal%>: %d dBm / <%:Noise%>: %d dBm" /><br />' +
168                                                                 '<small>%d%%</small>', icon, iw.signal, iw.noise, p
169                                                         );
170
171                                                 var info = document.getElementById(iw.id + '-iw-status');
172                                                 if (info)
173                                                 {
174                                                         if (is_assoc)
175                                                                 info.innerHTML = String.format(
176                                                                         '<strong><%:SSID%>:</strong> %h | ' +
177                                                                         '<strong><%:Mode%>:</strong> %s<br />' +
178                                                                         '<strong><%:BSSID%>:</strong> %s | ' +
179                                                                         '<strong><%:Encryption%>:</strong> %s',
180                                                                                 iw.ssid, iw.mode, iw.bssid,
181                                                                                 iw.encryption ? iw.encryption : '<%:None%>'
182                                                                 );
183                                                         else
184                                                                 info.innerHTML = String.format(
185                                                                         '<strong><%:SSID%>:</strong> %h | ' +
186                                                                         '<strong><%:Mode%>:</strong> %s<br />' +
187                                                                         '<em><%:Wireless is disabled or not associated%></em>',
188                                                                                 iw.ssid || '?', iw.mode
189                                                                 );
190                                                 }
191
192                                                 var dev = document.getElementById(wifidevs[iw.id] + '-iw-devinfo');
193                                                 if (dev)
194                                                 {
195                                                         if (is_assoc)
196                                                                 dev.innerHTML = String.format(
197                                                                         '<strong><%:Channel%>:</strong> %s (%s GHz) | ' +
198                                                                         '<strong><%:Bitrate%>:</strong> %s Mb/s',
199                                                                                 iw.channel ? iw.channel : '?',
200                                                                                 iw.frequency ? iw.frequency : '?',
201                                                                                 iw.bitrate ? iw.bitrate : '?'
202                                                                 );
203                                                         else
204                                                                 dev.innerHTML = '';
205                                                 }
206
207                                                 if (assoctable)
208                                                 {
209                                                         var assoclist = [ ];
210                                                         for( var bssid in iw.assoclist )
211                                                         {
212                                                                 assoclist.push(iw.assoclist[bssid]);
213                                                                 assoclist[assoclist.length-1].bssid = bssid;
214                                                         }
215
216                                                         assoclist.sort(function(a, b) { a.bssid < b.bssid });
217
218                                                         for( var j = 0; j < assoclist.length; j++ )
219                                                         {
220                                                                 var tr = assoctable.insertRow(-1);
221                                                                     tr.className = 'cbi-section-table-row cbi-rowstyle-' + rowstyle;
222
223                                                                 var icon;
224                                                                 var q = (-1 * (assoclist[j].noise - assoclist[j].signal)) / 5;
225                                                                 if (q < 1)
226                                                                         icon = "<%=resource%>/icons/signal-0.png";
227                                                                 else if (q < 2)
228                                                                         icon = "<%=resource%>/icons/signal-0-25.png";
229                                                                 else if (q < 3)
230                                                                         icon = "<%=resource%>/icons/signal-25-50.png";
231                                                                 else if (q < 4)
232                                                                         icon = "<%=resource%>/icons/signal-50-75.png";
233                                                                 else
234                                                                         icon = "<%=resource%>/icons/signal-75-100.png";
235
236                                                                 tr.insertCell(-1).innerHTML = String.format(
237                                                                         '<img src="%s" title="<%:Signal%>: %d dBm / <%:Noise%>: %d dBm" />',
238                                                                         icon, assoclist[j].signal, assoclist[j].noise
239                                                                 );
240
241                                                                 tr.insertCell(-1).innerHTML = iw.ssid ? iw.ssid : '?';
242                                                                 tr.insertCell(-1).innerHTML = assoclist[j].bssid;
243
244                                                                 tr.insertCell(-1).innerHTML = arptable[assoclist[j].bssid]
245                                                                         ? arptable[assoclist[j].bssid] : '?';
246
247                                                                 tr.insertCell(-1).innerHTML = String.format('%d dBm', assoclist[j].signal);
248                                                                 tr.insertCell(-1).innerHTML = String.format('%d dBm', assoclist[j].noise);
249
250                                                                 rowstyle = (rowstyle == 1) ? 2 : 1;
251                                                         }
252                                                 }
253                                         }
254
255                                         if (assoctable && assoctable.rows.length == 1)
256                                         {
257                                                 var tr = assoctable.insertRow(-1);
258                                                     tr.className = 'cbi-section-table-row';
259
260                                                 var td = tr.insertCell(-1);
261                                                     td.colSpan = 6;
262                                                     td.innerHTML = '<br /><em><%:No information available%></em>';
263                                         }
264
265                                         for (var dev in devup)
266                                         {
267                                                 var img = document.getElementById(dev + '-iw-upstate');
268                                                 if (img)
269                                                         img.src = '<%=resource%>/icons/wifi_big' + (devup[dev] ? '' : '_disabled') + '.png';
270                                         }
271                                 }
272
273                                 window.setTimeout(update_status, 5000);
274                         }
275                 )
276         };
277
278         update_status();
279 //]]></script>
280
281 <h2><a id="content" name="content"><%:Wireless Overview%></a></h2>
282
283 <div class="cbi-map">
284
285         <% for _, dev in ipairs(devices) do local nets = dev:get_wifinets() %>
286         <!-- device <%=dev:name()%> -->
287         <fieldset class="cbi-section">
288                 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
289                         <!-- physical device -->
290                         <tr>
291                                 <td style="width:34px"><img src="<%=resource%>/icons/wifi_big_disabled.png" style="float:left; margin-right:10px" id="<%=dev:name()%>-iw-upstate" /></td>
292                                 <td colspan="2" style="text-align:left">
293                                         <big><strong><%=guess_wifi_hw(dev:name())%> (<%=dev:name()%>)</strong></big><br />
294                                         <span id="<%=dev:name()%>-iw-devinfo"></span>
295                                 </td>
296                                 <td style="width:40px">
297                                         <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>
298                                         <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>
299                                 </td>
300                         </tr>
301                         <!-- /physical device -->
302
303                         <!-- network list -->
304                         <% if #nets > 0 then %>
305                                 <% for i, net in ipairs(nets) do %>
306                                 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
307                                         <td></td>
308                                         <td class="cbi-value-field" style="width:16px; padding:3px" id="<%=net:id()%>-iw-signal">
309                                                 <img src="<%=resource%>/icons/signal-none.png" title="<%:Not associated%>" /><br />
310                                                 <small>0%</small>
311                                         </td>
312                                         <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net:id()%>-iw-status">
313                                                 <em><%:Collecting data...%></em>
314                                         </td>
315                                         <td class="cbi-value-field" style="width:40px">
316                                                 <a href="<%=net:adminlink()%>"><img style="border:none" src="<%=resource%>/cbi/edit.gif" alt="<%:Edit this network%>" title="<%:Edit this network%>" /></a>
317                                                 <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>
318                                         </td>
319                                 </tr>
320                                 <% end %>
321                         <% else %>
322                                 <tr class="cbi-section-table-row cbi-rowstyle-2">
323                                         <td></td>
324                                         <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
325                                                 <em><%:No network configured on this device%></em>
326                                         </td>
327                                 </tr>
328                         <% end %>
329                         <!-- /network list -->
330                 </table>
331         </fieldset>
332         <!-- /device <%=dev:name()%> -->
333         <% end %>
334
335
336         <h2><a id="content" name="content"><%:Associated Stations%></a></h2>
337
338         <fieldset class="cbi-section">
339                 <table class="cbi-section-table" style="margin:10px; width:50%" id="iw-assoclist">
340                         <tr class="cbi-section-table-titles">
341                                 <th class="cbi-section-table-cell"></th>
342                                 <th class="cbi-section-table-cell"><%:SSID%></th>
343                                 <th class="cbi-section-table-cell"><%:MAC%></th>
344                                 <th class="cbi-section-table-cell"><%:Address%></th>
345                                 <th class="cbi-section-table-cell"><%:Signal%></th>
346                                 <th class="cbi-section-table-cell"><%:Noise%></th>
347                         </tr>
348                         <tr class="cbi-section-table-row cbi-rowstyle-2">
349                                 <td class="cbi-value-field" colspan="6">
350                                         <em><%:Collecting data...%></em>
351                                 </td>
352                         </tr>
353                 </table>
354         </fieldset>
355 </div>
356
357 <%+footer%>