modules/admin-*: Added hints for Wifi Channels
[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 ch = s:option(Value, "channel", translate("a_w_channel"))
140 for i=1, 14 do
141         ch:value(i, i .. " (2.4 GHz)")
142 end
143
144
145 s = m:section(TypedSection, "wifi-iface", translate("m_n_local"))
146 s.anonymous = true
147
148 s:option(Value, "ssid", translate("a_w_netid")).maxlength = 32
149
150 local devs = {}
151 luci.model.uci.foreach("wireless", "wifi-device",
152         function (section)
153                 table.insert(devs, section[".name"])
154         end)
155         
156 if #devs > 1 then
157         device = s:option(DummyValue, "device", translate("device"))
158 else
159         s.defaults.device = devs[1]
160 end
161
162 mode = s:option(ListValue, "mode", translate("mode"))
163 mode:value("ap", translate("m_w_ap"))
164 mode:value("adhoc", translate("m_w_adhoc"))
165 mode:value("sta", translate("m_w_client"))
166
167 function mode.write(self, section, value)
168         if value == "sta" then
169                 -- ToDo: Move this away
170                 if not luci.model.uci.get("network", "wan") then
171                         luci.model.uci.set("network", "wan", "proto", "none")
172                         luci.model.uci.set("network", "wan", "ifname", " ")
173                 end
174
175                 local oldif = luci.model.uci.get("network", "wan", "ifname")
176                 if oldif and oldif ~= " " then
177                         luci.model.uci.set("network", "wan", "_ifname", oldif)
178                 end
179                 luci.model.uci.set("network", "wan", "ifname", " ")
180
181                 self.map:set(section, "network", "wan")
182         else
183                 if luci.model.uci.get("network", "wan", "_ifname") then
184                         luci.model.uci.set("network", "wan", "ifname", luci.model.uci.get("network", "wan", "_ifname"))
185                 end
186                 self.map:set(section, "network", "lan")
187         end
188
189         return ListValue.write(self, section, value)
190 end
191
192 encr = s:option(ListValue, "encryption", translate("encryption"))
193 encr:value("none", "keine")
194 encr:value("wep", "WEP")
195 encr:value("psk", "WPA-PSK")
196 encr:value("wpa", "WPA-Radius")
197 encr:value("psk2", "WPA2-PSK")
198 encr:value("wpa2", "WPA2-Radius")
199
200 key = s:option(Value, "key", translate("key"))
201 key:depends("encryption", "wep")
202 key:depends("encryption", "psk")
203 key:depends("encryption", "wpa")
204 key:depends("encryption", "psk2")
205 key:depends("encryption", "wpa2")
206 key.rmempty = true
207
208 server = s:option(Value, "server", translate("a_w_radiussrv"))
209 server:depends("encryption", "wpa")
210 server:depends("encryption", "wpa2")
211 server.rmempty = true
212
213 port = s:option(Value, "port", translate("a_w_radiusport"))
214 port:depends("encryption", "wpa")
215 port:depends("encryption", "wpa2")
216 port.rmempty = true
217
218 iso = s:option(Flag, "isolate", translate("a_w_apisolation"), translate("a_w_apisolation1"))
219 iso.rmempty = true
220 iso:depends("mode", "ap")
221
222 hide = s:option(Flag, "hidden", translate("a_w_hideessid"))
223 hide.rmempty = true
224 hide:depends("mode", "ap")
225
226 return m