Huuuuuuuuuuuge rewrite of the Wireless Configuration
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / wireless.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 require("luci.sys")
16 require("luci.tools.webadmin")
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 m = SimpleForm("wireless", translate("wifi"))
33
34 s = m:section(Table, ifaces, translate("networks"))
35
36 function s.extedit(self, section) 
37         local device = self.map:get(section, "device") or ""
38         return luci.http.getenv("REQUEST_URI") .. "/" .. device
39 end
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         return "802." .. wireless[self.map:get(section, "device")].mode
64 end
65
66 mode = s:option(DummyValue, "mode", translate("mode"))
67 encryption = s:option(DummyValue, "encryption", translate("iwscan_encr"))
68
69 power = s:option(DummyValue, "_power", translate("power"))
70 function power.cfgvalue(self, section)
71         local ifname = self.map:get(section, "ifname")
72         return wifidata[ifname] and wifidata[ifname]["Tx-Power"] or "-"
73 end
74
75 scan = s:option(Button, "_scan", translate("scan"))
76 scan.inputstyle = "find"
77
78 function scan.cfgvalue(self, section)
79         return self.map:get(section, "ifname") or false
80 end
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 s2 = m:section(SimpleSection, translate("a_w_create"))
116 create = s2:option(ListValue, "create", translate("device"))
117 create:value("", translate("cbi_select"))
118 for k, v in pairs(wireless) do
119         if v[".type"] == "wifi-device" then
120                 create:value(k)
121         end
122 end
123
124 function create.write(self, section, value)
125         luci.model.uci.load_config("wireless")
126         luci.model.uci.section("wireless", "wifi-iface", nil, {device=value})
127         luci.model.uci.save_config("wireless")
128         luci.http.redirect(luci.http.getenv("REQUEST_URI") .. "/" .. value)
129 end
130
131 function create.cbid(self, section)
132         return "priv.cbid.create"
133 end
134
135 return m