f3eed8e2cbc4f9a3398066943dc451d9b24a9a41
[project/luci.git] / modules / admin-mini / luasrc / model / cbi / mini / wifi.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 -- Data init --
17
18 local wlcursor = luci.model.uci.cursor_state()
19 local wireless = wlcursor:get_all("wireless")
20 local wifidata = luci.sys.wifi.getiwconfig()
21 local wifidevs = {}
22 local ifaces = {}
23
24 for k, v in pairs(wireless) do
25         if v[".type"] == "wifi-iface" then
26                 table.insert(ifaces, v)
27         end
28 end
29
30 wlcursor:foreach("wireless", "wifi-device",
31         function(section)
32                 table.insert(wifidevs, section[".name"])
33         end)
34
35
36 -- Main Map --
37
38 m = Map("wireless", translate("wifi"), translate("a_w_devices1"))
39 m:chain("network")
40
41
42 -- Status Table --
43 s = m:section(Table, ifaces, translate("networks"))
44
45 link = s:option(DummyValue, "_link", translate("link"))
46 function link.cfgvalue(self, section)
47         local ifname = self.map:get(section, "ifname")
48         return wifidata[ifname] and wifidata[ifname]["Link Quality"] or "-"
49 end
50
51 essid = s:option(DummyValue, "ssid", "ESSID")
52
53 bssid = s:option(DummyValue, "_bsiid", "BSSID")
54 function bssid.cfgvalue(self, section)
55         local ifname = self.map:get(section, "ifname")
56         return (wifidata[ifname] and (wifidata[ifname].Cell 
57          or wifidata[ifname]["Access Point"])) or "-"
58 end
59
60 channel = s:option(DummyValue, "channel", translate("channel"))
61 function channel.cfgvalue(self, section)
62         return wireless[self.map:get(section, "device")].channel
63 end
64
65 protocol = s:option(DummyValue, "_mode", translate("protocol"))
66 function protocol.cfgvalue(self, section)
67         local mode = wireless[self.map:get(section, "device")].mode
68         return mode and "802." .. mode
69 end
70
71 mode = s:option(DummyValue, "mode", translate("mode"))
72 encryption = s:option(DummyValue, "encryption", translate("iwscan_encr"))
73
74 power = s:option(DummyValue, "_power", translate("power"))
75 function power.cfgvalue(self, section)
76         local ifname = self.map:get(section, "ifname")
77         return wifidata[ifname] and wifidata[ifname]["Tx-Power"] or "-"
78 end
79
80 scan = s:option(Button, "_scan", translate("scan"))
81 scan.inputstyle = "find"
82
83 function scan.cfgvalue(self, section)
84         return self.map:get(section, "ifname") or false
85 end
86
87 -- WLAN-Scan-Table --
88
89 t2 = m:section(Table, {}, translate("iwscan"), translate("iwscan1"))
90
91 function scan.write(self, section)
92         m.autoapply = false
93         t2.render = t2._render
94         local ifname = self.map:get(section, "ifname")
95         luci.util.update(t2.data, luci.sys.wifi.iwscan(ifname))
96 end
97
98 t2._render = t2.render
99 t2.render = function() end
100
101 t2:option(DummyValue, "Quality", translate("iwscan_link"))
102 essid = t2:option(DummyValue, "ESSID", "ESSID")
103 function essid.cfgvalue(self, section)
104         return self.map:get(section, "ESSID")
105 end
106
107 t2:option(DummyValue, "Address", "BSSID")
108 t2:option(DummyValue, "Mode", translate("mode"))
109 chan = t2:option(DummyValue, "channel", translate("channel"))
110 function chan.cfgvalue(self, section)
111         return self.map:get(section, "Channel")
112             or self.map:get(section, "Frequency")
113             or "-"
114 end 
115
116 t2:option(DummyValue, "Encryption key", translate("iwscan_encr"))
117
118 t2:option(DummyValue, "Signal level", translate("iwscan_signal"))
119
120 t2:option(DummyValue, "Noise level", translate("iwscan_noise"))
121
122
123
124 if #wifidevs < 1 then
125         return m
126 end
127
128 -- Config Section --
129
130 s = m:section(NamedSection, wifidevs[1], "wifi-device", translate("devices"))
131 s.addremove = false
132
133 en = s:option(Flag, "disabled", translate("enable"))
134 en.rmempty = false
135 en.enabled = "0"
136 en.disabled = "1"
137
138 function en.cfgvalue(self, section)
139         return Flag.cfgvalue(self, section) or "0"
140 end
141
142
143 local hwtype = m:get(wifidevs[1], "type")
144
145 if hwtype == "atheros" then
146         mode = s:option(ListValue, "hwmode", translate("mode"))
147         mode.override_values = true
148         mode:value("", "auto")
149         mode:value("11b", "802.11b")
150         mode:value("11g", "802.11g")
151         mode:value("11a", "802.11a")
152         mode:value("11bg", "802.11b+g")
153         mode.rmempty = true
154 end
155
156
157 ch = s:option(Value, "channel", translate("a_w_channel"))
158 for i=1, 14 do
159         ch:value(i, i .. " (2.4 GHz)")
160 end
161
162
163 s = m:section(TypedSection, "wifi-iface", translate("m_n_local"))
164 s.anonymous = true
165 s.addremove = false
166
167 s:option(Value, "ssid", translate("a_w_netid"))
168
169 bssid = s:option(Value, "bssid", translate("wifi_bssid"))
170
171 local devs = {}
172 luci.model.uci.cursor():foreach("wireless", "wifi-device",
173         function (section)
174                 table.insert(devs, section[".name"])
175         end)
176         
177 if #devs > 1 then
178         device = s:option(DummyValue, "device", translate("device"))
179 else
180         s.defaults.device = devs[1]
181 end
182
183 mode = s:option(ListValue, "mode", translate("mode"))
184 mode.override_values = true
185 mode:value("ap", translate("m_w_ap"))
186 mode:value("adhoc", translate("m_w_adhoc"))
187 mode:value("sta", translate("m_w_client"))
188
189 function mode.write(self, section, value)
190         if value == "sta" then
191                 -- ToDo: Move this away
192                 if not m.uci:get("network", "wan") then
193                         m.uci:set("network", "wan", "proto", "none")
194                         m.uci:set("network", "wan", "ifname", " ")
195                 end
196
197                 local oldif = m.uci:get("network", "wan", "ifname")
198                 if oldif and oldif ~= " " then
199                         m.uci:set("network", "wan", "_ifname", oldif)
200                 end
201                 m.uci:set("network", "wan", "ifname", " ")
202
203                 self.map:set(section, "network", "wan")
204         else
205                 if m.uci:get("network", "wan", "_ifname") then
206                         m.uci:set("network", "wan", "ifname", m.uci:get("network", "wan", "_ifname"))
207                 end
208                 self.map:set(section, "network", "lan")
209         end
210
211         return ListValue.write(self, section, value)
212 end
213
214 encr = s:option(ListValue, "encryption", translate("encryption"))
215 encr.override_values = true
216 encr:value("none", "No Encryption")
217 encr:value("wep", "WEP")
218
219 if hwtype == "atheros" or hwtype == "mac80211" then
220         local supplicant = luci.fs.mtime("/usr/sbin/wpa_supplicant")
221         local hostapd = luci.fs.mtime("/usr/sbin/hostapd")
222
223         if hostapd and supplicant then
224                 encr:value("psk", "WPA-PSK")
225                 encr:value("psk2", "WPA2-PSK")
226                 encr:value("wpa", "WPA-Radius", {mode="ap"})
227                 encr:value("wpa2i", "WPA2-Radius", {mode="ap"})
228         elseif hostapd and not supplicant then
229                 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="adhoc"})
230                 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="adhoc"})
231                 encr:value("wpa", "WPA-Radius", {mode="ap"})
232                 encr:value("wpa2i", "WPA2-Radius", {mode="ap"})
233                 encr.description = translate("wifi_wpareq")
234         elseif not hostapd and supplicant then
235                 encr:value("psk", "WPA-PSK", {mode="sta"})
236                 encr:value("psk2", "WPA2-PSK", {mode="sta"})
237                 encr.description = translate("wifi_wpareq")
238         else
239                 encr.description = translate("wifi_wpareq")
240         end
241 elseif hwtype == "broadcom" then
242         encr:value("psk", "WPA-PSK")
243         encr:value("psk2", "WPA2-PSK")
244 end
245
246 key = s:option(Value, "key", translate("key"))
247 key:depends("encryption", "wep")
248 key:depends("encryption", "psk")
249 key:depends("encryption", "wpa")
250 key:depends("encryption", "psk2")
251 key:depends("encryption", "wpa2i")
252 key.rmempty = true
253
254 server = s:option(Value, "server", translate("a_w_radiussrv"))
255 server:depends("encryption", "wpa")
256 server:depends("encryption", "wpa2i")
257 server.rmempty = true
258
259 port = s:option(Value, "port", translate("a_w_radiusport"))
260 port:depends("encryption", "wpa")
261 port:depends("encryption", "wpa2i")
262 port.rmempty = true
263
264
265 if hwtype == "atheros" or hwtype == "broadcom" then
266         iso = s:option(Flag, "isolate", translate("a_w_apisolation"), translate("a_w_apisolation1"))
267         iso.rmempty = true
268         iso:depends("mode", "ap")
269         
270         hide = s:option(Flag, "hidden", translate("a_w_hideessid"))
271         hide.rmempty = true
272         hide:depends("mode", "ap")
273 end
274
275 if hwtype == "mac80211" or hwtype == "atheros" then
276         bssid:depends({mode="adhoc"})
277 end
278
279 if hwtype == "broadcom" then
280         bssid:depends({mode="wds"})
281 end
282
283
284 return m