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