wifi_overview.htm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <%#
  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.
  5. -%>
  6. <%-
  7. local ip = require "luci.ip"
  8. local fs = require "nixio.fs"
  9. local utl = require "luci.util"
  10. local uci = require "luci.model.uci".cursor()
  11. local ntm = require "luci.model.network"
  12. local has_iwinfo = pcall(require, "iwinfo")
  13. ntm.init(uci)
  14. function guess_wifi_hw(dev)
  15. local bands = ""
  16. local ifname = dev:name()
  17. local name, idx = ifname:match("^([a-z]+)(%d+)")
  18. idx = tonumber(idx)
  19. if has_iwinfo then
  20. local bl = dev.iwinfo.hwmodelist
  21. if bl and next(bl) then
  22. if bl.a then bands = bands .. "a" end
  23. if bl.b then bands = bands .. "b" end
  24. if bl.g then bands = bands .. "g" end
  25. if bl.n then bands = bands .. "n" end
  26. if bl.ac then bands = bands .. "ac" end
  27. end
  28. local hw = dev.iwinfo.hardware_name
  29. if hw then
  30. return "%s 802.11%s" %{ hw, bands }
  31. end
  32. end
  33. -- wl.o
  34. if name == "wl" then
  35. local name = translatef("Broadcom 802.11%s Wireless Controller", bands)
  36. local nm = 0
  37. local fd = nixio.open("/proc/bus/pci/devices", "r")
  38. if fd then
  39. local ln
  40. for ln in fd:linesource() do
  41. if ln:match("wl$") then
  42. if nm == idx then
  43. local version = ln:match("^%S+%s+%S%S%S%S([0-9a-f]+)")
  44. name = translatef(
  45. "Broadcom BCM%04x 802.11 Wireless Controller",
  46. tonumber(version, 16)
  47. )
  48. break
  49. else
  50. nm = nm + 1
  51. end
  52. end
  53. end
  54. fd:close()
  55. end
  56. return name
  57. -- madwifi
  58. elseif name == "ath" or name == "wifi" then
  59. return translatef("Atheros 802.11%s Wireless Controller", bands)
  60. -- ralink
  61. elseif name == "ra" then
  62. return translatef("RaLink 802.11%s Wireless Controller", bands)
  63. -- hermes
  64. elseif name == "eth" then
  65. return translate("Hermes 802.11b Wireless Controller")
  66. -- hostap
  67. elseif name == "wlan" and fs.stat("/proc/net/hostap/" .. ifname, "type") == "dir" then
  68. return translate("Prism2/2.5/3 802.11b Wireless Controller")
  69. -- dunno yet
  70. else
  71. return translatef("Generic 802.11%s Wireless Controller", bands)
  72. end
  73. end
  74. local devices = ntm:get_wifidevs()
  75. local arpcache = { }
  76. ip.neighbors({ family = 4 }, function(n)
  77. if n.mac and n.dest then arpcache[n.mac:upper()] = n.dest:string() end
  78. end)
  79. local netlist = { }
  80. local netdevs = { }
  81. local dev
  82. for _, dev in ipairs(devices) do
  83. local net
  84. for _, net in ipairs(dev:get_wifinets()) do
  85. netlist[#netlist+1] = net:id()
  86. netdevs[net:id()] = dev:name()
  87. end
  88. end
  89. -%>
  90. <%+header%>
  91. <% if not has_iwinfo then %>
  92. <div class="errorbox">
  93. <strong><%:Package libiwinfo required!%></strong><br />
  94. <%_The <em>libiwinfo-lua</em> package is not installed. You must install this component for working wireless configuration!%>
  95. </div>
  96. <% end %>
  97. <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
  98. <script type="text/javascript">//<![CDATA[
  99. var wifidevs = <%=luci.http.write_json(netdevs)%>;
  100. var arptable = <%=luci.http.write_json(arpcache)%>;
  101. var is_reconnecting = false;
  102. function nowrap(s) {
  103. return s.replace(/ /g, '&#160;');
  104. }
  105. function wifi_shutdown(id, toggle) {
  106. var reconnect = (toggle.getAttribute('active') == 'false');
  107. if (!reconnect && !confirm(String.format('<%:Really shut down network?\nYou might lose access to this device if you are connected via this interface.%>')))
  108. return;
  109. is_reconnecting = true;
  110. var s = document.getElementById('iw-rc-status');
  111. if (s)
  112. {
  113. s.parentNode.style.display = 'block';
  114. s.innerHTML = '<%:Waiting for changes to be applied...%>';
  115. }
  116. for (var net in wifidevs)
  117. {
  118. var st = document.getElementById(net + '-iw-status');
  119. if (st)
  120. st.innerHTML = '<em><%:Wireless is restarting...%></em>';
  121. }
  122. XHR.get('<%=luci.dispatcher.build_url("admin", "network")%>/wireless_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, null,
  123. function(x)
  124. {
  125. if (s)
  126. {
  127. s.innerHTML = reconnect
  128. ? '<%:Wireless restarted%>'
  129. : '<%:Wireless shut down%>';
  130. window.setTimeout(function() {
  131. s.parentNode.style.display = 'none';
  132. is_reconnecting = false;
  133. }, 1000);
  134. }
  135. }
  136. );
  137. }
  138. XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "network", "wireless_status", table.concat(netlist, ","))%>', null,
  139. function(x, st)
  140. {
  141. if (st)
  142. {
  143. var assoctable = document.getElementById('iw-assoclist');
  144. if (assoctable)
  145. while (assoctable.rows.length > 1)
  146. assoctable.rows[1].parentNode.removeChild(assoctable.rows[1]);
  147. var devup = { };
  148. var rowstyle = 1;
  149. for( var i = 0; i < st.length; i++ )
  150. {
  151. var iw = st[i];
  152. var is_assoc = (iw.bssid && iw.bssid != '00:00:00:00:00:00' && iw.channel && iw.mode != 'Unknown' && !iw.disabled);
  153. var p = iw.quality;
  154. var q = is_assoc ? p : -1;
  155. var icon;
  156. if (q < 0)
  157. icon = "<%=resource%>/icons/signal-none.png";
  158. else if (q == 0)
  159. icon = "<%=resource%>/icons/signal-0.png";
  160. else if (q < 25)
  161. icon = "<%=resource%>/icons/signal-0-25.png";
  162. else if (q < 50)
  163. icon = "<%=resource%>/icons/signal-25-50.png";
  164. else if (q < 75)
  165. icon = "<%=resource%>/icons/signal-50-75.png";
  166. else
  167. icon = "<%=resource%>/icons/signal-75-100.png";
  168. if (!devup[wifidevs[iw.id]])
  169. devup[wifidevs[iw.id]] = is_assoc;
  170. var sig = document.getElementById(iw.id + '-iw-signal');
  171. if (sig)
  172. sig.innerHTML = String.format(
  173. '<img src="%s" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>" /><br />' +
  174. '<small>%d%%</small>', icon, iw.signal, iw.noise, p
  175. );
  176. var toggle = document.getElementById(iw.id + '-iw-toggle');
  177. if (toggle)
  178. {
  179. if (!iw.disabled)
  180. {
  181. toggle.className = 'cbi-button cbi-button-reset';
  182. toggle.value = '<%:Disable%>';
  183. toggle.title = '<%:Shutdown this network%>';
  184. }
  185. else
  186. {
  187. toggle.className = 'cbi-button cbi-button-reload';
  188. toggle.value = '<%:Enable%>';
  189. toggle.title = '<%:Activate this network%>';
  190. }
  191. toggle.setAttribute('active', !iw.disabled);
  192. }
  193. var info = document.getElementById(iw.id + '-iw-status');
  194. if (info)
  195. {
  196. if (is_assoc)
  197. info.innerHTML = String.format(
  198. '<strong><%:SSID%>:</strong> %h | ' +
  199. '<strong><%:Mode%>:</strong> %s<br />' +
  200. '<strong><%:BSSID%>:</strong> %s | ' +
  201. '<strong><%:Encryption%>:</strong> %s',
  202. iw.ssid, iw.mode, iw.bssid,
  203. iw.encryption ? iw.encryption : '<%:None%>'
  204. );
  205. else
  206. info.innerHTML = String.format(
  207. '<strong><%:SSID%>:</strong> %h | ' +
  208. '<strong><%:Mode%>:</strong> %s<br />' +
  209. '<em>%s</em>',
  210. iw.ssid || '?', iw.mode,
  211. is_reconnecting
  212. ? '<em><%:Wireless is restarting...%></em>'
  213. : '<em><%:Wireless is disabled or not associated%></em>'
  214. );
  215. }
  216. var dev = document.getElementById(wifidevs[iw.id] + '-iw-devinfo');
  217. if (dev)
  218. {
  219. if (is_assoc)
  220. dev.innerHTML = String.format(
  221. '<strong><%:Channel%>:</strong> %s (%s <%:GHz%>) | ' +
  222. '<strong><%:Bitrate%>:</strong> %s <%:Mbit/s%>',
  223. iw.channel ? iw.channel : '?',
  224. iw.frequency ? iw.frequency : '?',
  225. iw.bitrate ? iw.bitrate : '?'
  226. );
  227. else
  228. dev.innerHTML = '';
  229. }
  230. if (assoctable)
  231. {
  232. var assoclist = [ ];
  233. for( var bssid in iw.assoclist )
  234. {
  235. assoclist.push(iw.assoclist[bssid]);
  236. assoclist[assoclist.length-1].bssid = bssid;
  237. }
  238. assoclist.sort(function(a, b) { a.bssid < b.bssid });
  239. for( var j = 0; j < assoclist.length; j++ )
  240. {
  241. var tr = assoctable.insertRow(-1);
  242. tr.className = 'cbi-section-table-row cbi-rowstyle-' + rowstyle;
  243. var icon;
  244. var q = (-1 * (assoclist[j].noise - assoclist[j].signal)) / 5;
  245. if (q < 1)
  246. icon = "<%=resource%>/icons/signal-0.png";
  247. else if (q < 2)
  248. icon = "<%=resource%>/icons/signal-0-25.png";
  249. else if (q < 3)
  250. icon = "<%=resource%>/icons/signal-25-50.png";
  251. else if (q < 4)
  252. icon = "<%=resource%>/icons/signal-50-75.png";
  253. else
  254. icon = "<%=resource%>/icons/signal-75-100.png";
  255. tr.insertCell(-1).innerHTML = String.format(
  256. '<img src="%s" title="<%:Signal%>: %d <%:dBm%> / <%:Noise%>: %d <%:dBm%>" />',
  257. icon, assoclist[j].signal, assoclist[j].noise
  258. );
  259. tr.insertCell(-1).innerHTML = nowrap(String.format('%h', iw.ssid ? iw.ssid : '?'));
  260. tr.insertCell(-1).innerHTML = assoclist[j].bssid;
  261. tr.insertCell(-1).innerHTML = arptable[assoclist[j].bssid]
  262. ? arptable[assoclist[j].bssid] : '?';
  263. tr.insertCell(-1).innerHTML = nowrap(String.format('%d <%:dBm%>', assoclist[j].signal));
  264. tr.insertCell(-1).innerHTML = nowrap(String.format('%d <%:dBm%>', assoclist[j].noise));
  265. tr.insertCell(-1).innerHTML = nowrap((assoclist[j].rx_mcs > -1)
  266. ? String.format('%.1f <%:Mbit/s%>, MCS %d, %d<%:MHz%>', assoclist[j].rx_rate / 1000, assoclist[j].rx_mcs, assoclist[j].rx_40mhz ? 40 : 20)
  267. : String.format('%.1f <%:Mbit/s%>', assoclist[j].rx_rate / 1000)
  268. );
  269. tr.insertCell(-1).innerHTML = nowrap((assoclist[j].tx_mcs > -1)
  270. ? String.format('%.1f <%:Mbit/s%>, MCS %d, %d<%:MHz%>', assoclist[j].tx_rate / 1000, assoclist[j].tx_mcs, assoclist[j].tx_40mhz ? 40 : 20)
  271. : String.format('%.1f <%:Mbit/s%>', assoclist[j].tx_rate / 1000)
  272. );
  273. rowstyle = (rowstyle == 1) ? 2 : 1;
  274. }
  275. }
  276. }
  277. if (assoctable && assoctable.rows.length == 1)
  278. {
  279. var tr = assoctable.insertRow(-1);
  280. tr.className = 'cbi-section-table-row';
  281. var td = tr.insertCell(-1);
  282. td.colSpan = 8;
  283. td.innerHTML = '<br /><em><%:No information available%></em>';
  284. }
  285. for (var dev in devup)
  286. {
  287. var img = document.getElementById(dev + '-iw-upstate');
  288. if (img)
  289. img.src = '<%=resource%>/icons/wifi_big' + (devup[dev] ? '' : '_disabled') + '.png';
  290. }
  291. }
  292. }
  293. );
  294. //]]></script>
  295. <h2><a id="content" name="content"><%:Wireless Overview%></a></h2>
  296. <fieldset class="cbi-section" style="display:none">
  297. <legend><%:Reconnecting interface%></legend>
  298. <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
  299. <span id="iw-rc-status"><%:Waiting for changes to be applied...%></span>
  300. </fieldset>
  301. <div class="cbi-map">
  302. <% for _, dev in ipairs(devices) do local nets = dev:get_wifinets() %>
  303. <!-- device <%=dev:name()%> -->
  304. <fieldset class="cbi-section">
  305. <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
  306. <!-- physical device -->
  307. <tr>
  308. <td style="width:34px"><img src="<%=resource%>/icons/wifi_big_disabled.png" style="float:left; margin-right:10px" id="<%=dev:name()%>-iw-upstate" /></td>
  309. <td colspan="2" style="text-align:left">
  310. <big><strong><%=guess_wifi_hw(dev)%> (<%=dev:name()%>)</strong></big><br />
  311. <span id="<%=dev:name()%>-iw-devinfo"></span>
  312. </td>
  313. <td style="width:310px;text-align:right">
  314. <input type="button" class="cbi-button cbi-button-find" style="width:100px" onclick="location.href='<%=luci.dispatcher.build_url("admin/network/wireless_join")%>?device=<%=dev:name()%>'" title="<%:Find and join network%>" value="<%:Scan%>" />
  315. <input type="button" class="cbi-button cbi-button-add" style="width:100px" onclick="location.href='<%=luci.dispatcher.build_url("admin/network/wireless_add")%>?device=<%=dev:name()%>'" title="<%:Provide new network%>" value="<%:Add%>" />
  316. </td>
  317. </tr>
  318. <!-- /physical device -->
  319. <!-- network list -->
  320. <% if #nets > 0 then %>
  321. <% for i, net in ipairs(nets) do %>
  322. <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
  323. <td></td>
  324. <td class="cbi-value-field" style="width:16px; padding:3px" id="<%=net:id()%>-iw-signal">
  325. <img src="<%=resource%>/icons/signal-none.png" title="<%:Not associated%>" /><br />
  326. <small>0%</small>
  327. </td>
  328. <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net:id()%>-iw-status">
  329. <em><%:Collecting data...%></em>
  330. </td>
  331. <td class="cbi-value-field" style="width:310px;text-align:right">
  332. <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%>" />
  333. <input type="button" class="cbi-button cbi-button-edit" style="width:100px" onclick="location.href='<%=net:adminlink()%>'" title="<%:Edit this network%>" value="<%:Edit%>" />
  334. <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='<%=luci.dispatcher.build_url("admin/network/wireless_delete", net:ifname())%>'" title="<%:Delete this network%>" value="<%:Remove%>" />
  335. </td>
  336. </tr>
  337. <% end %>
  338. <% else %>
  339. <tr class="cbi-section-table-row cbi-rowstyle-2">
  340. <td></td>
  341. <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
  342. <em><%:No network configured on this device%></em>
  343. </td>
  344. </tr>
  345. <% end %>
  346. <!-- /network list -->
  347. </table>
  348. </fieldset>
  349. <!-- /device <%=dev:name()%> -->
  350. <% end %>
  351. <h2><a id="content" name="content"><%:Associated Stations%></a></h2>
  352. <fieldset class="cbi-section">
  353. <table class="cbi-section-table" style="margin:10px" id="iw-assoclist">
  354. <tr class="cbi-section-table-titles">
  355. <th class="cbi-section-table-cell"></th>
  356. <th class="cbi-section-table-cell"><%:SSID%></th>
  357. <th class="cbi-section-table-cell"><%:MAC-Address%></th>
  358. <th class="cbi-section-table-cell"><%:IPv4-Address%></th>
  359. <th class="cbi-section-table-cell"><%:Signal%></th>
  360. <th class="cbi-section-table-cell"><%:Noise%></th>
  361. <th class="cbi-section-table-cell"><%:RX Rate%></th>
  362. <th class="cbi-section-table-cell"><%:TX Rate%></th>
  363. </tr>
  364. <tr class="cbi-section-table-row cbi-rowstyle-2">
  365. <td class="cbi-value-field" colspan="8">
  366. <em><%:Collecting data...%></em>
  367. </td>
  368. </tr>
  369. </table>
  370. </fieldset>
  371. </div>
  372. <%+footer%>