Small bugfixes and improvements
[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 luci.model.uci.load_state("wireless")
19 local wireless = luci.model.uci.get_all("wireless")
20 luci.model.uci.unload("wireless")
21
22 local wifidata = luci.sys.wifi.getiwconfig()
23 local ifaces = {}
24
25 for k, v in pairs(wireless) do
26         if v[".type"] == "wifi-iface" then
27                 table.insert(ifaces, v)
28         end
29 end
30
31
32 -- Main Map --
33
34 m = Map("wireless", translate("wifi"), translate("a_w_devices1"))
35 m:chain("network")
36
37
38 -- Status Table --
39 s = m:section(Table, ifaces, translate("networks"))
40
41 link = s:option(DummyValue, "_link", translate("link"))
42 function link.cfgvalue(self, section)
43         local ifname = self.map:get(section, "ifname")
44         return wifidata[ifname] and wifidata[ifname]["Link Quality"] or "-"
45 end
46
47 essid = s:option(DummyValue, "ssid", "ESSID")
48
49 bssid = s:option(DummyValue, "_bsiid", "BSSID")
50 function bssid.cfgvalue(self, section)
51         local ifname = self.map:get(section, "ifname")
52         return (wifidata[ifname] and (wifidata[ifname].Cell 
53          or wifidata[ifname]["Access Point"])) or "-"
54 end
55
56 channel = s:option(DummyValue, "channel", translate("channel"))
57 function channel.cfgvalue(self, section)
58         return wireless[self.map:get(section, "device")].channel
59 end
60
61 protocol = s:option(DummyValue, "_mode", translate("protocol"))
62 function protocol.cfgvalue(self, section)
63         local mode = wireless[self.map:get(section, "device")].mode
64         return mode and "802." .. mode
65 end
66
67 mode = s:option(DummyValue, "mode", translate("mode"))
68 encryption = s:option(DummyValue, "encryption", translate("iwscan_encr"))
69
70 power = s:option(DummyValue, "_power", translate("power"))
71 function power.cfgvalue(self, section)
72         local ifname = self.map:get(section, "ifname")
73         return wifidata[ifname] and wifidata[ifname]["Tx-Power"] or "-"
74 end
75
76 scan = s:option(Button, "_scan", translate("scan"))
77 scan.inputstyle = "find"
78
79 function scan.cfgvalue(self, section)
80         return self.map:get(section, "ifname") or false
81 end
82
83 -- WLAN-Scan-Table --
84
85 t2 = m:section(Table, {}, translate("iwscan"), translate("iwscan1"))
86
87 function scan.write(self, section)
88         t2.render = t2._render
89         local ifname = self.map:get(section, "ifname")
90         luci.util.update(t2.data, luci.sys.wifi.iwscan(ifname))
91 end
92
93 t2._render = t2.render
94 t2.render = function() end
95
96 t2:option(DummyValue, "Quality", translate("iwscan_link"))
97 essid = t2:option(DummyValue, "ESSID", "ESSID")
98 function essid.cfgvalue(self, section)
99         return luci.util.pcdata(self.map:get(section, "ESSID"))
100 end
101
102 t2:option(DummyValue, "Address", "BSSID")
103 t2:option(DummyValue, "Mode", translate("mode"))
104 chan = t2:option(DummyValue, "channel", translate("channel"))
105 function chan.cfgvalue(self, section)
106         return self.map:get(section, "Channel")
107             or self.map:get(section, "Frequency")
108             or "-"
109 end 
110
111 t2:option(DummyValue, "Encryption key", translate("iwscan_encr"))
112
113 t2:option(DummyValue, "Signal level", translate("iwscan_signal"))
114
115 t2:option(DummyValue, "Noise level", translate("iwscan_noise"))
116
117
118 -- Config Section --
119
120 s = m:section(TypedSection, "wifi-device", translate("devices"))
121
122 en = s:option(Flag, "disabled", translate("enable"))
123 en.enabled = "0"
124 en.disabled = "1"
125
126 function en.cfgvalue(self, section)
127         return Flag.cfgvalue(self, section) or "0"
128 end
129
130
131 mode = s:option(ListValue, "mode", translate("mode"))
132 mode:value("", "standard")
133 mode:value("11b", "802.11b")
134 mode:value("11g", "802.11g")
135 mode:value("11a", "802.11a")
136 mode:value("11bg", "802.11b+g")
137 mode.rmempty = true
138
139 s:option(Value, "channel", translate("a_w_channel"))
140
141
142
143 s = m:section(TypedSection, "wifi-iface", translate("m_n_local"))
144 s.anonymous = true
145
146 s:option(Value, "ssid", translate("a_w_netid")).maxlength = 32
147
148 local devs = {}
149 luci.model.uci.foreach("wireless", "wifi-device",
150         function (section)
151                 table.insert(devs, section[".name"])
152         end)
153         
154 if #devs > 1 then
155         device = s:option(DummyValue, "device", translate("device"))
156 else
157         s.defaults.device = devs[1]
158 end
159
160 mode = s:option(ListValue, "mode", translate("mode"))
161 mode:value("ap", translate("m_w_ap"))
162 mode:value("adhoc", translate("m_w_adhoc"))
163 mode:value("sta", translate("m_w_client"))
164
165 function mode.write(self, section, value)
166         if value == "sta" then
167                 -- ToDo: Move this away
168                 if not luci.model.uci.get("network", "wan") then
169                         luci.model.uci.set("network", "wan", "proto", "none")
170                         luci.model.uci.set("network", "wan", "ifname", " ")
171                 end
172
173                 local oldif = luci.model.uci.get("network", "wan", "ifname")
174                 if oldif and oldif ~= " " then
175                         luci.model.uci.set("network", "wan", "_ifname", oldif)
176                 end
177                 luci.model.uci.set("network", "wan", "ifname", " ")
178
179                 self.map:set(section, "network", "wan")
180         else
181                 if luci.model.uci.get("network", "wan", "_ifname") then
182                         luci.model.uci.set("network", "wan", "ifname", luci.model.uci.get("network", "wan", "_ifname"))
183                 end
184                 self.map:set(section, "network", "lan")
185         end
186
187         return ListValue.write(self, section, value)
188 end
189
190 encr = s:option(ListValue, "encryption", translate("encryption"))
191 encr:value("none", "keine")
192 encr:value("wep", "WEP")
193 encr:value("psk", "WPA-PSK")
194 encr:value("wpa", "WPA-Radius")
195 encr:value("psk2", "WPA2-PSK")
196 encr:value("wpa2", "WPA2-Radius")
197
198 key = s:option(Value, "key", translate("key"))
199 key:depends("encryption", "wep")
200 key:depends("encryption", "psk")
201 key:depends("encryption", "wpa")
202 key:depends("encryption", "psk2")
203 key:depends("encryption", "wpa2")
204 key.rmempty = true
205
206 server = s:option(Value, "server", translate("a_w_radiussrv"))
207 server:depends("encryption", "wpa")
208 server:depends("encryption", "wpa2")
209 server.rmempty = true
210
211 port = s:option(Value, "port", translate("a_w_radiusport"))
212 port:depends("encryption", "wpa")
213 port:depends("encryption", "wpa2")
214 port.rmempty = true
215
216 iso = s:option(Flag, "isolate", translate("a_w_apisolation"), translate("a_w_apisolation1"))
217 iso.rmempty = true
218 iso:depends("mode", "ap")
219
220 hide = s:option(Flag, "hidden", translate("a_w_hideessid"))
221 hide.rmempty = true
222 hide:depends("mode", "ap")
223
224 return m