admin-mini: Override UVL values for wireless mode
[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         t2.render = t2._render
93         local ifname = self.map:get(section, "ifname")
94         luci.util.update(t2.data, luci.sys.wifi.iwscan(ifname))
95 end
96
97 t2._render = t2.render
98 t2.render = function() end
99
100 t2:option(DummyValue, "Quality", translate("iwscan_link"))
101 essid = t2:option(DummyValue, "ESSID", "ESSID")
102 function essid.cfgvalue(self, section)
103         return self.map:get(section, "ESSID")
104 end
105
106 t2:option(DummyValue, "Address", "BSSID")
107 t2:option(DummyValue, "Mode", translate("mode"))
108 chan = t2:option(DummyValue, "channel", translate("channel"))
109 function chan.cfgvalue(self, section)
110         return self.map:get(section, "Channel")
111             or self.map:get(section, "Frequency")
112             or "-"
113 end 
114
115 t2:option(DummyValue, "Encryption key", translate("iwscan_encr"))
116
117 t2:option(DummyValue, "Signal level", translate("iwscan_signal"))
118
119 t2:option(DummyValue, "Noise level", translate("iwscan_noise"))
120
121
122
123 if #wifidevs < 1 then
124         return m
125 end
126
127 -- Config Section --
128
129 s = m:section(NamedSection, wifidevs[1], "wifi-device", translate("devices"))
130
131 en = s:option(Flag, "disabled", translate("enable"))
132 en.enabled = "0"
133 en.disabled = "1"
134
135 function en.cfgvalue(self, section)
136         return Flag.cfgvalue(self, section) or "0"
137 end
138
139
140 local hwtype = m:get(wifidevs[1], "type")
141
142 if hwtype == "atheros" then
143         mode = s:option(ListValue, "mode", translate("mode"))
144         mode.override_values = true
145         mode:value("", "auto")
146         mode:value("11b", "802.11b")
147         mode:value("11g", "802.11g")
148         mode:value("11a", "802.11a")
149         mode:value("11bg", "802.11b+g")
150         mode.rmempty = true
151 end
152
153
154 ch = s:option(Value, "channel", translate("a_w_channel"))
155 for i=1, 14 do
156         ch:value(i, i .. " (2.4 GHz)")
157 end
158
159
160 s = m:section(TypedSection, "wifi-iface", translate("m_n_local"))
161 s.anonymous = true
162
163 s:option(Value, "ssid", translate("a_w_netid"))
164
165 bssid = s:option(Value, "bssid", translate("wifi_bssid"))
166
167 local devs = {}
168 luci.model.uci.cursor():foreach("wireless", "wifi-device",
169         function (section)
170                 table.insert(devs, section[".name"])
171         end)
172         
173 if #devs > 1 then
174         device = s:option(DummyValue, "device", translate("device"))
175 else
176         s.defaults.device = devs[1]
177 end
178
179 mode = s:option(ListValue, "mode", translate("mode"))
180 mode.override_values = true
181 mode:value("ap", translate("m_w_ap"))
182 mode:value("adhoc", translate("m_w_adhoc"))
183 mode:value("sta", translate("m_w_client"))
184
185 function mode.write(self, section, value)
186         if value == "sta" then
187                 -- ToDo: Move this away
188                 if not m.uci:get("network", "wan") then
189                         m.uci:set("network", "wan", "proto", "none")
190                         m.uci:set("network", "wan", "ifname", " ")
191                 end
192
193                 local oldif = m.uci:get("network", "wan", "ifname")
194                 if oldif and oldif ~= " " then
195                         m.uci:set("network", "wan", "_ifname", oldif)
196                 end
197                 m.uci:set("network", "wan", "ifname", " ")
198
199                 self.map:set(section, "network", "wan")
200         else
201                 if m.uci:get("network", "wan", "_ifname") then
202                         m.uci:set("network", "wan", "ifname", m.uci:get("network", "wan", "_ifname"))
203                 end
204                 self.map:set(section, "network", "lan")
205         end
206
207         return ListValue.write(self, section, value)
208 end
209
210 encr = s:option(ListValue, "encryption", translate("encryption"))
211 encr.override_values = true
212 encr:value("none", "No Encryption")
213 encr:value("wep", "WEP")
214
215 if hwtype == "atheros" or hwtype == "mac80211" then
216         local supplicant = luci.fs.mtime("/usr/sbin/wpa_supplicant")
217         local hostapd = luci.fs.mtime("/usr/sbin/hostapd")
218
219         if hostapd and supplicant then
220                 encr:value("psk", "WPA-PSK")
221                 encr:value("psk2", "WPA2-PSK")
222                 encr:value("wpa", "WPA-Radius", {mode="ap"})
223                 encr:value("wpa2i", "WPA2-Radius", {mode="ap"})
224         elseif hostapd and not supplicant then
225                 encr:value("psk", "WPA-PSK", {mode="ap"}, {mode="adhoc"})
226                 encr:value("psk2", "WPA2-PSK", {mode="ap"}, {mode="adhoc"})
227                 encr:value("wpa", "WPA-Radius", {mode="ap"})
228                 encr:value("wpa2i", "WPA2-Radius", {mode="ap"})
229                 encr.description = translate("wifi_wpareq")
230         elseif not hostapd and supplicant then
231                 encr:value("psk", "WPA-PSK", {mode="sta"})
232                 encr:value("psk2", "WPA2-PSK", {mode="sta"})
233                 encr.description = translate("wifi_wpareq")
234         else
235                 encr.description = translate("wifi_wpareq")
236         end
237 elseif hwtype == "broadcom" then
238         encr:value("psk", "WPA-PSK")
239         encr:value("psk2", "WPA2-PSK")
240 end
241
242 key = s:option(Value, "key", translate("key"))
243 key:depends("encryption", "wep")
244 key:depends("encryption", "psk")
245 key:depends("encryption", "wpa")
246 key:depends("encryption", "psk2")
247 key:depends("encryption", "wpa2i")
248 key.rmempty = true
249
250 server = s:option(Value, "server", translate("a_w_radiussrv"))
251 server:depends("encryption", "wpa")
252 server:depends("encryption", "wpa2i")
253 server.rmempty = true
254
255 port = s:option(Value, "port", translate("a_w_radiusport"))
256 port:depends("encryption", "wpa")
257 port:depends("encryption", "wpa2i")
258 port.rmempty = true
259
260
261 if hwtype == "atheros" or hwtype == "broadcom" then
262         iso = s:option(Flag, "isolate", translate("a_w_apisolation"), translate("a_w_apisolation1"))
263         iso.rmempty = true
264         iso:depends("mode", "ap")
265         
266         hide = s:option(Flag, "hidden", translate("a_w_hideessid"))
267         hide.rmempty = true
268         hide:depends("mode", "ap")
269 end
270
271 if hwtype == "mac80211" or hwtype == "atheros" then
272         bssid:depends({mode="adhoc"})
273 end
274
275 if hwtype == "broadcom" then
276         bssid:depends({mode="wds"})
277 end
278
279
280 return m