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