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