modules/admin-full: cope with undefined scan list in wifi_join
[project/luci.git] / modules / admin-full / luasrc / view / admin_network / wifi_join.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12
13 -%>
14
15 <%-
16
17 local sys = require "luci.sys"
18 local utl = require "luci.util"
19
20 function guess_wifi_signal(info)
21 local scale = (100 / (info.quality_max or 100) * (info.quality or 0))
22 local icon
23
24 if not info.bssid or info.bssid == "00:00:00:00:00:00" then
25 icon = resource .. "/icons/signal-none.png"
26 elseif scale < 15 then
27 icon = resource .. "/icons/signal-0.png"
28 elseif scale < 35 then
29 icon = resource .. "/icons/signal-0-25.png"
30 elseif scale < 55 then
31 icon = resource .. "/icons/signal-25-50.png"
32 elseif scale < 75 then
33 icon = resource .. "/icons/signal-50-75.png"
34 else
35 icon = resource .. "/icons/signal-75-100.png"
36 end
37
38 return icon
39 end
40
41 function percent_wifi_signal(info)
42 local qc = info.quality or 0
43 local qm = info.quality_max or 0
44
45 if info.bssid and qc > 0 and qm > 0 then
46 return math.floor((100 / qm) * qc)
47 else
48 return 0
49 end
50 end
51
52 function format_wifi_encryption(info)
53 if info.wep == true then
54 return "WEP"
55 elseif info.wpa > 0 then
56 return translatef("<abbr title='Pairwise: %s / Group: %s'>%s - %s</abbr>",
57 table.concat(info.pair_ciphers, ", "),
58 table.concat(info.group_ciphers, ", "),
59 (info.wpa == 3) and translate("mixed WPA/WPA2")
60 or (info.wpa == 2 and "WPA2" or "WPA"),
61 table.concat(info.auth_suites, ", ")
62 )
63 elseif info.enabled then
64 return "<em>%s</em>" % translate("unknown")
65 else
66 return "<em>%s</em>" % translate("open")
67 end
68 end
69
70 local dev = luci.http.formvalue("device")
71 local iw = luci.sys.wifi.getiwinfo(dev)
72
73 if not iw then
74 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
75 return
76 end
77
78
79 function scanlist(times)
80 local i, k, v
81 local l = { }
82 local s = { }
83
84 for i = 1, times do
85 for k, v in ipairs(iw.scanlist or { }) do
86 if not s[v.bssid] then
87 l[#l+1] = v
88 s[v.bssid] = true
89 end
90 end
91 end
92
93 return l
94 end
95 -%>
96
97 <%+header%>
98
99 <h2><a id="content" name="content"><%:Join Network: Wireless Scan%></a></h2>
100
101 <div class="cbi-map">
102 <fieldset class="cbi-section">
103 <table class="cbi-section-table" style="empty-cells:hide">
104 <!-- scan list -->
105 <% for i, net in ipairs(scanlist(3)) do net.encryption = net.encryption or { } %>
106 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
107 <td class="cbi-value-field" style="width:16px; padding:3px">
108 <abbr title="<%:Signal%>: <%=net.signal%> <%:dB%> / <%:Quality%>: <%=net.quality%>/<%=net.quality_max%>">
109 <img src="<%=guess_wifi_signal(net)%>" /><br />
110 <small><%=percent_wifi_signal(net)%>%</small>
111 </abbr>
112 </td>
113 <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
114 <big><strong><%=net.ssid and utl.pcdata(net.ssid) or "<em>%s</em>" % translate("hidden")%></strong></big><br />
115 <strong>Channel:</strong> <%=net.channel%> |
116 <strong>Mode:</strong> <%=net.mode%> |
117 <strong>BSSID:</strong> <%=net.bssid%> |
118 <strong>Encryption:</strong> <%=format_wifi_encryption(net.encryption)%>
119 </td>
120 <td class="cbi-value-field" style="width:40px">
121 <form action="<%=REQUEST_URI%>" method="post">
122 <input type="hidden" name="device" value="<%=utl.pcdata(dev)%>" />
123 <input type="hidden" name="join" value="<%=utl.pcdata(net.ssid)%>" />
124 <input type="hidden" name="mode" value="<%=net.mode%>" />
125 <input type="hidden" name="bssid" value="<%=net.bssid%>" />
126 <input type="hidden" name="channel" value="<%=net.channel%>" />
127 <input type="hidden" name="wep" value="<%=net.encryption.wep and 1 or 0%>" />
128 <% if net.encryption.wpa then %>
129 <input type="hidden" name="wpa_version" value="<%=net.encryption.wpa%>" />
130 <% for _, v in ipairs(net.encryption.auth_suites) do %><input type="hidden" name="wpa_suites" value="<%=v%>" />
131 <% end; for _, v in ipairs(net.encryption.group_ciphers) do %><input type="hidden" name="wpa_group" value="<%=v%>" />
132 <% end; for _, v in ipairs(net.encryption.pair_ciphers) do %><input type="hidden" name="wpa_pairwise" value="<%=v%>" />
133 <% end; end %>
134
135 <input type="hidden" name="clbridge" value="<%=iw.type == "wl" and 1 or 0%>" />
136
137 <input class="cbi-button cbi-button-apply" type="submit" value="<%:Join Network%>" />
138 </form>
139 </td>
140 </tr>
141 <% end %>
142 <!-- /scan list -->
143 </table>
144 </fieldset>
145 </div>
146 <div class="cbi-page-actions right">
147 <form class="inline" action="<%=luci.dispatcher.build_url("admin/network/wireless")%>" method="get">
148 <input class="cbi-button cbi-button-reset" type="submit" value="<%:Back to overview%>" />
149 </form>
150 <form class="inline" action="<%=REQUEST_URI%>" method="get">
151 <input type="hidden" name="device" value="<%=utl.pcdata(dev)%>" />
152 <input class="cbi-button cbi-input-find" type="submit" value="<%:Repeat scan%>" />
153 </form>
154 </div>
155
156 <%+footer%>