312fd08654c900f0ebd7066287ef89182ee96ac5
[project/luci.git] / modules / luci-mod-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-2013 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 -%>
13
14 <%-
15
16         local sys = require "luci.sys"
17         local fs = require "luci.fs"
18         local utl = require "luci.util"
19         local uci = require "luci.model.uci".cursor()
20         local ntm = require "luci.model.network"
21
22         local has_iwinfo = pcall(require, "iwinfo")
23
24         ntm.init(uci)
25
26         function guess_wifi_hw(dev)
27                 local bands = ""
28                 local ifname = dev:name()
29                 local name, idx = ifname:match("^([a-z]+)(%d+)")
30                 idx = tonumber(idx)
31
32                 if has_iwinfo then
33                         local bl = dev.iwinfo.hwmodelist
34                         if bl and next(bl) then
35                                 if bl.a then bands = bands .. "a" end
36                                 if bl.b then bands = bands .. "b" end
37                                 if bl.g then bands = bands .. "g" end
38                                 if bl.n then bands = bands .. "n" end
39                                 if bl.ac then bands = bands .. "ac" end
40                         end
41
42                         local hw = dev.iwinfo.hardware_name
43                         if hw then
44                                 return "%s 802.11%s" %{ hw, bands }
45                         end
46                 end
47
48                 -- wl.o
49                 if name == "wl" then
50                         local name = translatef("Broadcom 802.11%s Wireless Controller", bands)
51                         local nm   = 0
52
53                         local fd = nixio.open("/proc/bus/pci/devices", "r")
54                         if fd then
55                                 local ln
56                                 for ln in fd:linesource() do
57                                         if ln:match("wl$") then
58                                                 if nm == idx then
59                                                         local version = ln:match("^%S+%s+%S%S%S%S([0-9a-f]+)")
60                                                         name = translatef(
61                                                                 "Broadcom BCM%04x 802.11 Wireless Controller",
62                                                                 tonumber(version, 16)
63                                                         )
64
65                                                         break
66                                                 else
67                                                         nm = nm + 1
68                                                 end
69                                         end
70                                 end
71                                 fd:close()
72                         end
73
74                         return name
75
76                 -- madwifi
77                 elseif name == "ath" or name == "wifi" then
78                         return translatef("Atheros 802.11%s Wireless Controller", bands)
79
80                 -- ralink
81                 elseif name == "ra" then
82                         return translatef("RaLink 802.11%s Wireless Controller", bands)
83
84                 -- hermes
85                 elseif name == "eth" then
86                         return translate("Hermes 802.11b Wireless Controller")
87
88                 -- hostap
89                 elseif name == "wlan" and fs.isdirectory("/proc/net/hostap/" .. ifname) then
90                         return translate("Prism2/2.5/3 802.11b Wireless Controller")
91
92                 -- dunno yet
93                 else
94                         return translatef("Generic 802.11%s Wireless Controller", bands)
95                 end
96         end
97
98         local devices  = ntm:get_wifidevs()
99         local arpcache = { }
100         sys.net.arptable(function(e) arpcache[e["HW address"]:upper()] = e["IP address"] end)
101
102         local netlist = { }
103         local netdevs = { }
104
105         local dev
106         for _, dev in ipairs(devices) do
107                 local net
108                 for _, net in ipairs(dev:get_wifinets()) do
109                         netlist[#netlist+1] = net:id()
110                         netdevs[net:id()] = dev:name()
111                 end
112         end
113 -%>
114
115 <%+header%>
116
117 <% if not has_iwinfo then %>
118         <div class="errorbox">
119                 <strong><%:Package libiwinfo required!%></strong><br />
120                 <%_The <em>libiwinfo-lua</em> package is not installed. You must install this component for working wireless configuration!%>
121         </div>
122 <% end %>
123
124 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
125 <script type="text/javascript">//<![CDATA[
126         var wifidevs = <%=luci.http.write_json(netdevs)%>;
127         var arptable = <%=luci.http.write_json(arpcache)%>;
128
129         var is_reconnecting = false;
130
131         function nowrap(s) {
132                 return s.replace(/ /g, '&#160;');
133         }
134
135         function wifi_shutdown(id, toggle) {
136                 var reconnect = (toggle.getAttribute('active') == 'false');
137
138                 if (!reconnect && !confirm(String.format('<%:Really shut down network?\nYou might lose access to this device if you are connected via this interface.%>')))
139                         return;
140
141                 is_reconnecting = true;
142
143                 var s = document.getElementById('iw-rc-status');
144                 if (s)
145                 {
146                         s.parentNode.style.display = 'block';
147                         s.innerHTML = '<%:Waiting for changes to be applied...%>';
148                 }
149
150                 for (var net in wifidevs)
151                 {
152                         var st = document.getElementById(net + '-iw-status');
153                         if (st)
154                                 st.innerHTML = '<em><%:Wireless is restarting...%></em>';
155                 }
156
157                 XHR.get('<%=luci.dispatcher.build_url("admin", "network")%>/wireless_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, null,
158                         function(x)
159                         {
160                                 if (s)
161                                 {
162                                         s.innerHTML = reconnect
163                                                 ? '<%:Wireless restarted%>'
164                                                 : '<%:Wireless shut down%>';
165
166                                         window.setTimeout(function() {
167                                                 s.parentNode.style.display = 'none';
168                                                 is_reconnecting = false;
169                                         }, 1000);
170                                 }
171                         }
172                 );
173         }
174
175         XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "network", "wireless_status", table.concat(netlist, ","))%>', null,
176                 function(x, st)
177                 {
178                         if (st)
179                         {
180                                 var assoctable = document.getElementById('iw-assoclist');
181                                 if (assoctable)
182                                         while (assoctable.rows.length > 1)
183                                                 assoctable.rows[1].parentNode.removeChild(assoctable.rows[1]);
184
185                                 var devup = { };
186                                 var rowstyle = 1;
187
188                                 for( var i = 0; i < st.length; i++ )
189                                 {
190                                         var iw = st[i];
191                                         var is_assoc = (iw.bssid && iw.bssid != '00:00:00:00:00:00' && iw.channel && iw.mode != 'Unknown');
192                                         var p = iw.quality;
193                                         var q = is_assoc ? p : -1;
194
195                                         var icon;
196                                         if (q < 0)
197                                                 icon = "<%=resource%>/icons/signal-none.png";
198                                         else if (q == 0)
199                                                 icon = "<%=resource%>/icons/signal-0.png";
200                                         else if (q < 25)
201                                                 icon = "<%=resource%>/icons/signal-0-25.png";
202                                         else if (q < 50)
203                                                 icon = "<%=resource%>/icons/signal-25-50.png";
204                                         else if (q < 75)
205                                                 icon = "<%=resource%>/icons/signal-50-75.png";
206                                         else
207                                                 icon = "<%=resource%>/icons/signal-75-100.png";
208
209                                         if (!devup[wifidevs[iw.id]])
210                                                 devup[wifidevs[iw.id]] = is_assoc;
211
212                                         var sig = document.getElementById(iw.id + '-iw-signal');
213                                         if (sig)
214                                                 sig.innerHTML = String.format(
215                                                         '<img src="%s" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>" /><br />' +
216                                                         '<small>%d%%</small>', icon, iw.signal, iw.noise, p
217                                                 );
218
219                                         var toggle = document.getElementById(iw.id + '-iw-toggle');
220                                         if (toggle)
221                                         {
222                                                 if (!iw.disabled)
223                                                 {
224                                                         toggle.className = 'cbi-button cbi-button-reset';
225                                                         toggle.value = '<%:Disable%>';
226                                                         toggle.title = '<%:Shutdown this network%>';
227                                                 }
228                                                 else
229                                                 {
230                                                         toggle.className = 'cbi-button cbi-button-reload';
231                                                         toggle.value = '<%:Enable%>';
232                                                         toggle.title = '<%:Activate this network%>';
233                                                 }
234
235                                                 toggle.setAttribute('active', !iw.disabled);
236                                         }
237
238                                         var info = document.getElementById(iw.id + '-iw-status');
239                                         if (info)
240                                         {
241                                                 if (is_assoc)
242                                                         info.innerHTML = String.format(
243                                                                 '<strong><%:SSID%>:</strong> %h | ' +
244                                                                 '<strong><%:Mode%>:</strong> %s<br />' +
245                                                                 '<strong><%:BSSID%>:</strong> %s | ' +
246                                                                 '<strong><%:Encryption%>:</strong> %s',
247                                                                         iw.ssid, iw.mode, iw.bssid,
248                                                                         iw.encryption ? iw.encryption : '<%:None%>'
249                                                         );
250                                                 else
251                                                         info.innerHTML = String.format(
252                                                                 '<strong><%:SSID%>:</strong> %h | ' +
253                                                                 '<strong><%:Mode%>:</strong> %s<br />' +
254                                                                 '<em>%s</em>',
255                                                                         iw.ssid || '?', iw.mode,
256                                                                         is_reconnecting
257                                                                                 ? '<em><%:Wireless is restarting...%></em>'
258                                                                                 : '<em><%:Wireless is disabled or not associated%></em>'
259                                                         );
260                                         }
261
262                                         var dev = document.getElementById(wifidevs[iw.id] + '-iw-devinfo');
263                                         if (dev)
264                                         {
265                                                 if (is_assoc)
266                                                         dev.innerHTML = String.format(
267                                                                 '<strong><%:Channel%>:</strong> %s (%s <%:GHz%>) | ' +
268                                                                 '<strong><%:Bitrate%>:</strong> %s <%:Mbit/s%>',
269                                                                         iw.channel ? iw.channel : '?',
270                                                                         iw.frequency ? iw.frequency : '?',
271                                                                         iw.bitrate ? iw.bitrate : '?'
272                                                         );
273                                                 else
274                                                         dev.innerHTML = '';
275                                         }
276
277                                         if (assoctable)
278                                         {
279                                                 var assoclist = [ ];
280                                                 for( var bssid in iw.assoclist )
281                                                 {
282                                                         assoclist.push(iw.assoclist[bssid]);
283                                                         assoclist[assoclist.length-1].bssid = bssid;
284                                                 }
285
286                                                 assoclist.sort(function(a, b) { a.bssid < b.bssid });
287
288                                                 for( var j = 0; j < assoclist.length; j++ )
289                                                 {
290                                                         var tr = assoctable.insertRow(-1);
291                                                             tr.className = 'cbi-section-table-row cbi-rowstyle-' + rowstyle;
292
293                                                         var icon;
294                                                         var q = (-1 * (assoclist[j].noise - assoclist[j].signal)) / 5;
295                                                         if (q < 1)
296                                                                 icon = "<%=resource%>/icons/signal-0.png";
297                                                         else if (q < 2)
298                                                                 icon = "<%=resource%>/icons/signal-0-25.png";
299                                                         else if (q < 3)
300                                                                 icon = "<%=resource%>/icons/signal-25-50.png";
301                                                         else if (q < 4)
302                                                                 icon = "<%=resource%>/icons/signal-50-75.png";
303                                                         else
304                                                                 icon = "<%=resource%>/icons/signal-75-100.png";
305
306                                                         tr.insertCell(-1).innerHTML = String.format(
307                                                                 '<img src="%s" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>" />',
308                                                                 icon, assoclist[j].signal, assoclist[j].noise
309                                                         );
310
311                                                         tr.insertCell(-1).innerHTML = nowrap(String.format('%h', iw.ssid ? iw.ssid : '?'));
312                                                         tr.insertCell(-1).innerHTML = assoclist[j].bssid;
313
314                                                         tr.insertCell(-1).innerHTML = arptable[assoclist[j].bssid]
315                                                                 ? arptable[assoclist[j].bssid] : '?';
316
317                                                         tr.insertCell(-1).innerHTML = nowrap(String.format('%d <%:dBm%>', assoclist[j].signal));
318                                                         tr.insertCell(-1).innerHTML = nowrap(String.format('%d <%:dBm%>', assoclist[j].noise));
319
320                                                         tr.insertCell(-1).innerHTML = nowrap((assoclist[j].rx_mcs > -1)
321                                                                 ? String.format('%.1f <%:Mbit/s%>, MCS %d, %d<%:MHz%>', assoclist[j].rx_rate / 1000, assoclist[j].rx_mcs, assoclist[j].rx_40mhz ? 40 : 20)
322                                                                 : String.format('%.1f <%:Mbit/s%>', assoclist[j].rx_rate / 1000)
323                                                         );
324
325                                                         tr.insertCell(-1).innerHTML = nowrap((assoclist[j].tx_mcs > -1)
326                                                                 ? String.format('%.1f <%:Mbit/s%>, MCS %d, %d<%:MHz%>', assoclist[j].tx_rate / 1000, assoclist[j].tx_mcs, assoclist[j].tx_40mhz ? 40 : 20)
327                                                                 : String.format('%.1f <%:Mbit/s%>', assoclist[j].tx_rate / 1000)
328                                                         );
329
330                                                         rowstyle = (rowstyle == 1) ? 2 : 1;
331                                                 }
332                                         }
333                                 }
334
335                                 if (assoctable && assoctable.rows.length == 1)
336                                 {
337                                         var tr = assoctable.insertRow(-1);
338                                             tr.className = 'cbi-section-table-row';
339
340                                         var td = tr.insertCell(-1);
341                                             td.colSpan = 8;
342                                             td.innerHTML = '<br /><em><%:No information available%></em>';
343                                 }
344
345                                 for (var dev in devup)
346                                 {
347                                         var img = document.getElementById(dev + '-iw-upstate');
348                                         if (img)
349                                                 img.src = '<%=resource%>/icons/wifi_big' + (devup[dev] ? '' : '_disabled') + '.png';
350                                 }
351                         }
352                 }
353         );
354 //]]></script>
355
356 <h2><a id="content" name="content"><%:Wireless Overview%></a></h2>
357
358 <fieldset class="cbi-section" style="display:none">
359         <legend><%:Reconnecting interface%></legend>
360         <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
361         <span id="iw-rc-status"><%:Waiting for changes to be applied...%></span>
362 </fieldset>
363
364 <div class="cbi-map">
365
366         <% for _, dev in ipairs(devices) do local nets = dev:get_wifinets() %>
367         <!-- device <%=dev:name()%> -->
368         <fieldset class="cbi-section">
369                 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
370                         <!-- physical device -->
371                         <tr>
372                                 <td style="width:34px"><img src="<%=resource%>/icons/wifi_big_disabled.png" style="float:left; margin-right:10px" id="<%=dev:name()%>-iw-upstate" /></td>
373                                 <td colspan="2" style="text-align:left">
374                                         <big><strong><%=guess_wifi_hw(dev)%> (<%=dev:name()%>)</strong></big><br />
375                                         <span id="<%=dev:name()%>-iw-devinfo"></span>
376                                 </td>
377                                 <td style="width:310px;text-align:right">
378                                         <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%>" />
379                                         <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%>" />
380                                 </td>
381                         </tr>
382                         <!-- /physical device -->
383
384                         <!-- network list -->
385                         <% if #nets > 0 then %>
386                                 <% for i, net in ipairs(nets) do %>
387                                 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
388                                         <td></td>
389                                         <td class="cbi-value-field" style="width:16px; padding:3px" id="<%=net:id()%>-iw-signal">
390                                                 <img src="<%=resource%>/icons/signal-none.png" title="<%:Not associated%>" /><br />
391                                                 <small>0%</small>
392                                         </td>
393                                         <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net:id()%>-iw-status">
394                                                 <em><%:Collecting data...%></em>
395                                         </td>
396                                         <td class="cbi-value-field" style="width:310px;text-align:right">
397                                                 <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%>" />
398                                                 <input type="button" class="cbi-button cbi-button-edit" style="width:100px" onclick="location.href='<%=net:adminlink()%>'" title="<%:Edit this network%>" value="<%:Edit%>" />
399                                                 <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%>" />
400                                         </td>
401                                 </tr>
402                                 <% end %>
403                         <% else %>
404                                 <tr class="cbi-section-table-row cbi-rowstyle-2">
405                                         <td></td>
406                                         <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
407                                                 <em><%:No network configured on this device%></em>
408                                         </td>
409                                 </tr>
410                         <% end %>
411                         <!-- /network list -->
412                 </table>
413         </fieldset>
414         <!-- /device <%=dev:name()%> -->
415         <% end %>
416
417
418         <h2><a id="content" name="content"><%:Associated Stations%></a></h2>
419
420         <fieldset class="cbi-section">
421                 <table class="cbi-section-table" style="margin:10px" id="iw-assoclist">
422                         <tr class="cbi-section-table-titles">
423                                 <th class="cbi-section-table-cell"></th>
424                                 <th class="cbi-section-table-cell"><%:SSID%></th>
425                                 <th class="cbi-section-table-cell"><%:MAC-Address%></th>
426                                 <th class="cbi-section-table-cell"><%:IPv4-Address%></th>
427                                 <th class="cbi-section-table-cell"><%:Signal%></th>
428                                 <th class="cbi-section-table-cell"><%:Noise%></th>
429                                 <th class="cbi-section-table-cell"><%:RX Rate%></th>
430                                 <th class="cbi-section-table-cell"><%:TX Rate%></th>
431                         </tr>
432                         <tr class="cbi-section-table-row cbi-rowstyle-2">
433                                 <td class="cbi-value-field" colspan="8">
434                                         <em><%:Collecting data...%></em>
435                                 </td>
436                         </tr>
437                 </table>
438         </fieldset>
439 </div>
440
441 <%+footer%>