2 Copyright 2008-2009 Steven Barth <steven@midlink.org>
3 Copyright 2008-2015 Jo-Philipp Wich <jow@openwrt.org>
4 Licensed to the public under the Apache License 2.0.
9 local ip = require "luci.ip"
10 local fs = require "nixio.fs"
11 local utl = require "luci.util"
12 local uci = require "luci.model.uci".cursor()
13 local ntm = require "luci.model.network"
15 local has_iwinfo = pcall(require, "iwinfo")
19 function guess_wifi_hw(dev)
21 local ifname = dev:name()
22 local name, idx = ifname:match("^([a-z]+)(%d+)")
26 local bl = dev.iwinfo.hwmodelist
27 if bl and next(bl) then
28 if bl.a then bands = bands .. "a" end
29 if bl.b then bands = bands .. "b" end
30 if bl.g then bands = bands .. "g" end
31 if bl.n then bands = bands .. "n" end
32 if bl.ac then bands = bands .. "ac" end
35 local hw = dev.iwinfo.hardware_name
37 return "%s 802.11%s" %{ hw, bands }
43 local name = translatef("Broadcom 802.11%s Wireless Controller", bands)
46 local fd = nixio.open("/proc/bus/pci/devices", "r")
49 for ln in fd:linesource() do
50 if ln:match("wl$") then
52 local version = ln:match("^%S+%s+%S%S%S%S([0-9a-f]+)")
54 "Broadcom BCM%04x 802.11 Wireless Controller",
70 elseif name == "ath" or name == "wifi" then
71 return translatef("Atheros 802.11%s Wireless Controller", bands)
74 elseif name == "ra" then
75 return translatef("RaLink 802.11%s Wireless Controller", bands)
78 elseif name == "eth" then
79 return translate("Hermes 802.11b Wireless Controller")
82 elseif name == "wlan" and fs.stat("/proc/net/hostap/" .. ifname, "type") == "dir" then
83 return translate("Prism2/2.5/3 802.11b Wireless Controller")
87 return translatef("Generic 802.11%s Wireless Controller", bands)
91 local devices = ntm:get_wifidevs()
93 ip.neighbors({ family = 4 }, function(n)
94 if n.mac and n.dest then arpcache[n.mac:upper()] = n.dest:string() end
101 for _, dev in ipairs(devices) do
103 for _, net in ipairs(dev:get_wifinets()) do
104 netlist[#netlist+1] = net:id()
105 netdevs[net:id()] = dev:name()
112 <% if not has_iwinfo then %>
113 <div class="errorbox">
114 <strong><%:Package libiwinfo required!%></strong><br />
115 <%_The <em>libiwinfo-lua</em> package is not installed. You must install this component for working wireless configuration!%>
119 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
120 <script type="text/javascript">//<![CDATA[
121 var wifidevs = <%=luci.http.write_json(netdevs)%>;
122 var arptable = <%=luci.http.write_json(arpcache)%>;
124 var is_reconnecting = false;
127 return s.replace(/ /g, ' ');
130 function wifi_shutdown(id, toggle) {
131 var reconnect = (toggle.getAttribute('active') == 'false');
133 if (!reconnect && !confirm(String.format('<%:Really shut down network?\nYou might lose access to this device if you are connected via this interface.%>')))
136 is_reconnecting = true;
138 var s = document.getElementById('iw-rc-status');
141 s.parentNode.style.display = 'block';
142 s.innerHTML = '<%:Waiting for changes to be applied...%>';
145 for (var net in wifidevs)
147 var st = document.getElementById(net + '-iw-status');
149 st.innerHTML = '<em><%:Wireless is restarting...%></em>';
152 XHR.get('<%=url('admin/network')%>/wireless_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, null,
157 s.innerHTML = reconnect
158 ? '<%:Wireless restarted%>'
159 : '<%:Wireless shut down%>';
161 window.setTimeout(function() {
162 s.parentNode.style.display = 'none';
163 is_reconnecting = false;
170 XHR.poll(5, '<%=url('admin/network/wireless_status', table.concat(netlist, ","))%>', null,
175 var assoctable = document.getElementById('iw-assoclist');
177 while (assoctable.rows.length > 1)
178 assoctable.rows[1].parentNode.removeChild(assoctable.rows[1]);
183 for( var i = 0; i < st.length; i++ )
186 var is_assoc = (iw.bssid && iw.bssid != '00:00:00:00:00:00' && iw.channel && iw.mode != 'Unknown' && !iw.disabled);
188 var q = is_assoc ? p : -1;
192 icon = "<%=resource%>/icons/signal-none.png";
194 icon = "<%=resource%>/icons/signal-0.png";
196 icon = "<%=resource%>/icons/signal-0-25.png";
198 icon = "<%=resource%>/icons/signal-25-50.png";
200 icon = "<%=resource%>/icons/signal-50-75.png";
202 icon = "<%=resource%>/icons/signal-75-100.png";
204 if (!devup[wifidevs[iw.id]])
205 devup[wifidevs[iw.id]] = is_assoc;
207 var sig = document.getElementById(iw.id + '-iw-signal');
209 sig.innerHTML = String.format(
210 '<img src="%s" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>" /><br />' +
211 '<small>%d%%</small>', icon, iw.signal, iw.noise, p
214 var toggle = document.getElementById(iw.id + '-iw-toggle');
219 toggle.className = 'cbi-button cbi-button-reset';
220 toggle.value = '<%:Disable%>';
221 toggle.title = '<%:Shutdown this network%>';
225 toggle.className = 'cbi-button cbi-button-reload';
226 toggle.value = '<%:Enable%>';
227 toggle.title = '<%:Activate this network%>';
230 toggle.setAttribute('active', !iw.disabled);
233 var info = document.getElementById(iw.id + '-iw-status');
237 info.innerHTML = String.format(
238 '<strong><%:SSID%>:</strong> %h | ' +
239 '<strong><%:Mode%>:</strong> %s<br />' +
240 '<strong><%:BSSID%>:</strong> %s | ' +
241 '<strong><%:Encryption%>:</strong> %s',
242 iw.ssid, iw.mode, iw.bssid,
243 iw.encryption ? iw.encryption : '<%:None%>'
246 info.innerHTML = String.format(
247 '<strong><%:SSID%>:</strong> %h | ' +
248 '<strong><%:Mode%>:</strong> %s<br />' +
250 iw.ssid || '?', iw.mode,
252 ? '<em><%:Wireless is restarting...%></em>'
253 : '<em><%:Wireless is disabled or not associated%></em>'
257 var dev = document.getElementById(wifidevs[iw.id] + '-iw-devinfo');
261 dev.innerHTML = String.format(
262 '<strong><%:Channel%>:</strong> %s (%s <%:GHz%>) | ' +
263 '<strong><%:Bitrate%>:</strong> %s <%:Mbit/s%>',
264 iw.channel ? iw.channel : '?',
265 iw.frequency ? iw.frequency : '?',
266 iw.bitrate ? iw.bitrate : '?'
275 for( var bssid in iw.assoclist )
277 assoclist.push(iw.assoclist[bssid]);
278 assoclist[assoclist.length-1].bssid = bssid;
281 assoclist.sort(function(a, b) { a.bssid < b.bssid });
283 for( var j = 0; j < assoclist.length; j++ )
285 var tr = assoctable.insertRow(-1);
286 tr.className = 'cbi-section-table-row cbi-rowstyle-' + rowstyle;
289 var q = (-1 * (assoclist[j].noise - assoclist[j].signal)) / 5;
291 icon = "<%=resource%>/icons/signal-0.png";
293 icon = "<%=resource%>/icons/signal-0-25.png";
295 icon = "<%=resource%>/icons/signal-25-50.png";
297 icon = "<%=resource%>/icons/signal-50-75.png";
299 icon = "<%=resource%>/icons/signal-75-100.png";
301 tr.insertCell(-1).innerHTML = String.format(
302 '<img src="%s" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>" />',
303 icon, assoclist[j].signal, assoclist[j].noise
306 tr.insertCell(-1).innerHTML = nowrap(String.format('%h', iw.ssid ? iw.ssid : '?'));
307 tr.insertCell(-1).innerHTML = assoclist[j].bssid;
309 tr.insertCell(-1).innerHTML = arptable[assoclist[j].bssid]
310 ? arptable[assoclist[j].bssid] : '?';
312 tr.insertCell(-1).innerHTML = nowrap(String.format('%d <%:dBm%>', assoclist[j].signal));
313 tr.insertCell(-1).innerHTML = nowrap(String.format('%d <%:dBm%>', assoclist[j].noise));
315 tr.insertCell(-1).innerHTML = nowrap((assoclist[j].rx_mcs > -1)
316 ? String.format('%.1f <%:Mbit/s%>, MCS %d, %d<%:MHz%>', assoclist[j].rx_rate / 1000, assoclist[j].rx_mcs, assoclist[j].rx_40mhz ? 40 : 20)
317 : String.format('%.1f <%:Mbit/s%>', assoclist[j].rx_rate / 1000)
320 tr.insertCell(-1).innerHTML = nowrap((assoclist[j].tx_mcs > -1)
321 ? String.format('%.1f <%:Mbit/s%>, MCS %d, %d<%:MHz%>', assoclist[j].tx_rate / 1000, assoclist[j].tx_mcs, assoclist[j].tx_40mhz ? 40 : 20)
322 : String.format('%.1f <%:Mbit/s%>', assoclist[j].tx_rate / 1000)
325 rowstyle = (rowstyle == 1) ? 2 : 1;
330 if (assoctable && assoctable.rows.length == 1)
332 var tr = assoctable.insertRow(-1);
333 tr.className = 'cbi-section-table-row';
335 var td = tr.insertCell(-1);
337 td.innerHTML = '<br /><em><%:No information available%></em>';
340 for (var dev in devup)
342 var img = document.getElementById(dev + '-iw-upstate');
344 img.src = '<%=resource%>/icons/wifi_big' + (devup[dev] ? '' : '_disabled') + '.png';
351 <h2 name="content"><%:Wireless Overview%></h2>
353 <fieldset class="cbi-section" style="display:none">
354 <legend><%:Reconnecting interface%></legend>
355 <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
356 <span id="iw-rc-status"><%:Waiting for changes to be applied...%></span>
359 <div class="cbi-map">
361 <% for _, dev in ipairs(devices) do local nets = dev:get_wifinets() %>
362 <!-- device <%=dev:name()%> -->
363 <fieldset class="cbi-section">
364 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
365 <!-- physical device -->
367 <td style="width:34px"><img src="<%=resource%>/icons/wifi_big_disabled.png" style="float:left; margin-right:10px" id="<%=dev:name()%>-iw-upstate" /></td>
368 <td colspan="2" style="text-align:left">
369 <big><strong><%=guess_wifi_hw(dev)%> (<%=dev:name()%>)</strong></big><br />
370 <span id="<%=dev:name()%>-iw-devinfo"></span>
372 <td style="width:310px;text-align:right">
373 <input type="button" class="cbi-button cbi-button-find" style="width:100px" onclick="location.href='<%=url("admin/network/wireless_join")%>?device=<%=dev:name()%>'" title="<%:Find and join network%>" value="<%:Scan%>" />
374 <input type="button" class="cbi-button cbi-button-add" style="width:100px" onclick="location.href='<%=url("admin/network/wireless_add")%>?device=<%=dev:name()%>'" title="<%:Provide new network%>" value="<%:Add%>" />
377 <!-- /physical device -->
379 <!-- network list -->
380 <% if #nets > 0 then %>
381 <% for i, net in ipairs(nets) do %>
382 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
384 <td class="cbi-value-field" style="width:16px; padding:3px" id="<%=net:id()%>-iw-signal">
385 <img src="<%=resource%>/icons/signal-none.png" title="<%:Not associated%>" /><br />
388 <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net:id()%>-iw-status">
389 <em><%:Collecting data...%></em>
391 <td class="cbi-value-field" style="width:310px;text-align:right">
392 <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%>" />
393 <input type="button" class="cbi-button cbi-button-edit" style="width:100px" onclick="location.href='<%=net:adminlink()%>'" title="<%:Edit this network%>" value="<%:Edit%>" />
394 <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='<%=url("admin/network/wireless_delete", net:ifname())%>'" title="<%:Delete this network%>" value="<%:Remove%>" />
399 <tr class="cbi-section-table-row cbi-rowstyle-2">
401 <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
402 <em><%:No network configured on this device%></em>
406 <!-- /network list -->
409 <!-- /device <%=dev:name()%> -->
413 <h2 name="content"><%:Associated Stations%></h2>
415 <fieldset class="cbi-section">
416 <table class="cbi-section-table" style="margin:10px" id="iw-assoclist">
417 <tr class="cbi-section-table-titles">
418 <th class="cbi-section-table-cell"></th>
419 <th class="cbi-section-table-cell"><%:SSID%></th>
420 <th class="cbi-section-table-cell"><%:MAC-Address%></th>
421 <th class="cbi-section-table-cell"><%:IPv4-Address%></th>
422 <th class="cbi-section-table-cell"><%:Signal%></th>
423 <th class="cbi-section-table-cell"><%:Noise%></th>
424 <th class="cbi-section-table-cell"><%:RX Rate%></th>
425 <th class="cbi-section-table-cell"><%:TX Rate%></th>
427 <tr class="cbi-section-table-row cbi-rowstyle-2">
428 <td class="cbi-value-field" colspan="8">
429 <em><%:Collecting data...%></em>