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 == "ra" then
71 return translatef("RaLink 802.11%s Wireless Controller", bands)
74 elseif name == "eth" then
75 return translate("Hermes 802.11b Wireless Controller")
78 elseif name == "wlan" and fs.stat("/proc/net/hostap/" .. ifname, "type") == "dir" then
79 return translate("Prism2/2.5/3 802.11b Wireless Controller")
83 return translatef("Generic 802.11%s Wireless Controller", bands)
87 local devices = ntm:get_wifidevs()
92 for _, dev in ipairs(devices) do
94 for _, net in ipairs(dev:get_wifinets()) do
95 netlist[#netlist+1] = net:id()
96 netdevs[net:id()] = dev:name()
103 <% if not has_iwinfo then %>
104 <div class="errorbox">
105 <strong><%:Package libiwinfo required!%></strong><br />
106 <%_The <em>libiwinfo-lua</em> package is not installed. You must install this component for working wireless configuration!%>
110 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
111 <script type="text/javascript">//<![CDATA[
112 var wifidevs = <%=luci.http.write_json(netdevs)%>;
114 var is_reconnecting = false;
117 return s.replace(/ /g, ' ');
120 function wifirate(bss, rx) {
121 var p = rx ? 'rx_' : 'tx_',
122 s = '%.1f <%:Mbit/s%>, %d<%:MHz%>'
123 .format(bss[p+'rate'] / 1000, bss[p+'mhz']),
124 ht = bss[p+'ht'], vht = bss[p+'vht'],
125 mhz = bss[p+'mhz'], nss = bss[p+'nss'],
126 mcs = bss[p+'mcs'], sgi = bss[p+'short_gi'];
129 if (vht) s += ', VHT-MCS %d'.format(mcs);
130 if (nss) s += ', VHT-NSS %d'.format(nss);
131 if (ht) s += ', MCS %s'.format(mcs);
132 if (sgi) s += ', <%:Short GI%>';
138 function wifi_shutdown(id, toggle) {
139 var reconnect = (toggle.getAttribute('active') == 'false');
141 if (!reconnect && !confirm(String.format('<%:Really shut down network?\nYou might lose access to this device if you are connected via this interface.%>')))
144 is_reconnecting = true;
146 var s = document.getElementById('iw-rc-status');
149 s.parentNode.style.display = 'block';
150 s.innerHTML = '<%:Waiting for changes to be applied...%>';
153 for (var net in wifidevs)
155 var st = document.getElementById(net + '-iw-status');
157 st.innerHTML = '<em><%:Wireless is restarting...%></em>';
160 (new XHR()).post('<%=url('admin/network')%>/wireless_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, { token: '<%=token%>' },
165 s.innerHTML = reconnect
166 ? '<%:Wireless restarted%>'
167 : '<%:Wireless shut down%>';
169 window.setTimeout(function() {
170 s.parentNode.style.display = 'none';
171 is_reconnecting = false;
178 function wifi_delete(id) {
179 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.%>'))
182 (new XHR()).post('<%=url('admin/network/wireless_delete')%>/' + id, { token: '<%=token%>' },
184 location.href = '<%=url('admin/network/wireless')%>';
189 var hosts = <%=luci.http.write_json(luci.sys.net.host_hints())%>;
191 XHR.poll(5, '<%=url('admin/network/wireless_status', table.concat(netlist, ","))%>', null,
196 var assoctable = document.getElementById('iw-assoclist');
198 while (assoctable.rows.length > 1)
199 assoctable.rows[1].parentNode.removeChild(assoctable.rows[1]);
204 for( var i = 0; i < st.length; i++ )
207 var is_assoc = (iw.bssid && iw.bssid != '00:00:00:00:00:00' && iw.channel && iw.mode != 'Unknown' && !iw.disabled);
209 var q = is_assoc ? p : -1;
213 icon = "<%=resource%>/icons/signal-none.png";
215 icon = "<%=resource%>/icons/signal-0.png";
217 icon = "<%=resource%>/icons/signal-0-25.png";
219 icon = "<%=resource%>/icons/signal-25-50.png";
221 icon = "<%=resource%>/icons/signal-50-75.png";
223 icon = "<%=resource%>/icons/signal-75-100.png";
225 if (!devup[wifidevs[iw.id]])
226 devup[wifidevs[iw.id]] = is_assoc;
228 var sig = document.getElementById(iw.id + '-iw-signal');
230 sig.innerHTML = String.format(
231 '<span class="ifacebadge" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>"><img src="%s" /> %d%%</span>',
232 iw.signal, iw.noise, icon, p
235 var toggle = document.getElementById(iw.id + '-iw-toggle');
240 toggle.className = 'cbi-button cbi-button-reset';
241 toggle.value = '<%:Disable%>';
242 toggle.title = '<%:Shutdown this network%>';
246 toggle.className = 'cbi-button cbi-button-reload';
247 toggle.value = '<%:Enable%>';
248 toggle.title = '<%:Activate this network%>';
251 toggle.setAttribute('active', !iw.disabled);
254 var info = document.getElementById(iw.id + '-iw-status');
258 info.innerHTML = String.format(
259 '<strong><%:SSID%>:</strong> %h | ' +
260 '<strong><%:Mode%>:</strong> %s<br />' +
261 '<strong><%:BSSID%>:</strong> %s | ' +
262 '<strong><%:Encryption%>:</strong> %s',
263 iw.ssid, iw.mode, iw.bssid,
264 iw.encryption ? iw.encryption : '<%:None%>'
267 info.innerHTML = String.format(
268 '<strong><%:SSID%>:</strong> %h | ' +
269 '<strong><%:Mode%>:</strong> %s<br />' +
271 iw.ssid || '?', iw.mode,
273 ? '<em><%:Wireless is restarting...%></em>'
274 : '<em><%:Wireless is disabled or not associated%></em>'
278 var dev = document.getElementById(wifidevs[iw.id] + '-iw-devinfo');
282 dev.innerHTML = String.format(
283 '<strong><%:Channel%>:</strong> %s (%s <%:GHz%>) | ' +
284 '<strong><%:Bitrate%>:</strong> %s <%:Mbit/s%>',
285 iw.channel ? iw.channel : '?',
286 iw.frequency ? iw.frequency : '?',
287 iw.bitrate ? iw.bitrate : '?'
296 for( var bssid in iw.assoclist )
298 assoclist.push(iw.assoclist[bssid]);
299 assoclist[assoclist.length-1].bssid = bssid;
302 assoclist.sort(function(a, b) { a.bssid < b.bssid });
304 for( var j = 0; j < assoclist.length; j++ )
306 var tr = assoctable.insertRow(-1);
307 tr.className = 'cbi-section-table-row cbi-rowstyle-' + rowstyle;
310 var q = (-1 * (assoclist[j].noise - assoclist[j].signal)) / 5;
312 icon = "<%=resource%>/icons/signal-0.png";
314 icon = "<%=resource%>/icons/signal-0-25.png";
316 icon = "<%=resource%>/icons/signal-25-50.png";
318 icon = "<%=resource%>/icons/signal-50-75.png";
320 icon = "<%=resource%>/icons/signal-75-100.png";
322 tr.insertCell(-1).innerHTML = String.format(
323 '<span class="ifacebadge" title="%q"><img src="<%=resource%>/icons/wifi.png" /> %h</span>',
324 iw.device.name, iw.ifname
327 tr.insertCell(-1).innerHTML = nowrap(String.format('%h', iw.ssid ? iw.ssid : '?'));
328 tr.insertCell(-1).innerHTML = assoclist[j].bssid;
330 var host = hosts[assoclist[j].bssid];
332 tr.insertCell(-1).innerHTML = String.format(
333 '<div style="max-width:200px;overflow:hidden;text-overflow:ellipsis">%s</div>',
334 ((host.name && (host.ipv4 || host.ipv6))
335 ? '%h (%s)'.format(host.name, host.ipv4 || host.ipv6)
336 : '%h'.format(host.name || host.ipv4 || host.ipv6)).nobr()
339 tr.insertCell(-1).innerHTML = '?';
341 tr.insertCell(-1).innerHTML = String.format(
342 '<span class="ifacebadge" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%> / <%:SNR%>: %d"><img src="%s" /> %d / %d <%:dBm%></span>',
343 assoclist[j].signal, assoclist[j].noise, assoclist[j].signal - assoclist[j].noise,
345 assoclist[j].signal, assoclist[j].noise
348 tr.insertCell(-1).innerHTML = nowrap(wifirate(assoclist[j], true)) + '<br />' + nowrap(wifirate(assoclist[j], false));
350 rowstyle = (rowstyle == 1) ? 2 : 1;
355 if (assoctable && assoctable.rows.length == 1)
357 var tr = assoctable.insertRow(-1);
358 tr.className = 'cbi-section-table-row';
360 var td = tr.insertCell(-1);
362 td.innerHTML = '<br /><em><%:No information available%></em>';
365 for (var dev in devup)
367 var img = document.getElementById(dev + '-iw-upstate');
369 img.src = '<%=resource%>/icons/wifi_big' + (devup[dev] ? '' : '_disabled') + '.png';
376 <h2 name="content"><%:Wireless Overview%></h2>
378 <fieldset class="cbi-section" style="display:none">
379 <legend><%:Reconnecting interface%></legend>
380 <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
381 <span id="iw-rc-status"><%:Waiting for changes to be applied...%></span>
384 <div class="cbi-map">
386 <% for _, dev in ipairs(devices) do local nets = dev:get_wifinets() %>
387 <!-- device <%=dev:name()%> -->
388 <fieldset class="cbi-section">
389 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
390 <!-- physical device -->
392 <td style="width:34px"><img src="<%=resource%>/icons/wifi_big_disabled.png" style="float:left; margin-right:10px" id="<%=dev:name()%>-iw-upstate" /></td>
393 <td colspan="2" style="text-align:left">
394 <big><strong><%=guess_wifi_hw(dev)%> (<%=dev:name()%>)</strong></big><br />
395 <span id="<%=dev:name()%>-iw-devinfo"></span>
397 <td style="width:310px;text-align:right">
398 <form action="<%=url('admin/network/wireless_join')%>" method="post" class="inline">
399 <input type="hidden" name="device" value="<%=dev:name()%>" />
400 <input type="hidden" name="token" value="<%=token%>" />
401 <input type="submit" class="cbi-button cbi-button-find" style="width:100px" title="<%:Find and join network%>" value="<%:Scan%>" />
403 <form action="<%=url('admin/network/wireless_add')%>" method="post" class="inline">
404 <input type="hidden" name="device" value="<%=dev:name()%>" />
405 <input type="hidden" name="token" value="<%=token%>" />
406 <input type="submit" class="cbi-button cbi-button-add" style="width:100px" title="<%:Provide new network%>" value="<%:Add%>" />
410 <!-- /physical device -->
412 <!-- network list -->
413 <% if #nets > 0 then %>
414 <% for i, net in ipairs(nets) do %>
415 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
417 <td class="cbi-value-field" style="vertical-align:middle; padding:3px" id="<%=net:id()%>-iw-signal">
418 <span class="ifacebadge" title="<%:Not associated%>"><img src="<%=resource%>/icons/signal-none.png" /> 0%</span>
420 <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net:id()%>-iw-status">
421 <em><%:Collecting data...%></em>
423 <td class="cbi-value-field" style="width:310px;text-align:right">
424 <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%>" />
425 <input type="button" class="cbi-button cbi-button-edit" style="width:100px" onclick="location.href='<%=net:adminlink()%>'" title="<%:Edit this network%>" value="<%:Edit%>" />
426 <input type="button" class="cbi-button cbi-button-remove" style="width:100px" onclick="wifi_delete('<%=net:id()%>')" title="<%:Delete this network%>" value="<%:Remove%>" />
431 <tr class="cbi-section-table-row cbi-rowstyle-2">
433 <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
434 <em><%:No network configured on this device%></em>
438 <!-- /network list -->
441 <!-- /device <%=dev:name()%> -->
445 <h2><%:Associated Stations%></h2>
447 <fieldset class="cbi-section">
448 <table class="cbi-section-table valign-middle" style="margin:10px" id="iw-assoclist">
449 <tr class="cbi-section-table-titles">
450 <th class="cbi-section-table-cell"></th>
451 <th class="cbi-section-table-cell"><%:SSID%></th>
452 <th class="cbi-section-table-cell"><%:MAC-Address%></th>
453 <th class="cbi-section-table-cell"><%:Host%></th>
454 <th class="cbi-section-table-cell"><%:Signal%> / <%:Noise%></th>
455 <th class="cbi-section-table-cell"><%:RX Rate%> / <%:TX Rate%></th>
457 <tr class="cbi-section-table-row cbi-rowstyle-2">
458 <td class="cbi-value-field" colspan="6">
459 <em><%:Collecting data...%></em>