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