Small bugfixes and improvements
[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 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 t2 = m:section(Table, {}, translate("iwscan"), translate("iwscan1"))
84
85 function scan.write(self, section)
86 t2.render = t2._render
87 local ifname = self.map:get(section, "ifname")
88 luci.util.update(t2.data, luci.sys.wifi.iwscan(ifname))
89 end
90
91 t2._render = t2.render
92 t2.render = function() end
93
94 t2:option(DummyValue, "Quality", translate("iwscan_link"))
95 essid = t2:option(DummyValue, "ESSID", "ESSID")
96 function essid.cfgvalue(self, section)
97 return luci.util.pcdata(self.map:get(section, "ESSID"))
98 end
99
100 t2:option(DummyValue, "Address", "BSSID")
101 t2:option(DummyValue, "Mode", translate("mode"))
102 chan = t2:option(DummyValue, "channel", translate("channel"))
103 function chan.cfgvalue(self, section)
104 return self.map:get(section, "Channel")
105 or self.map:get(section, "Frequency")
106 or "-"
107 end
108
109 t2:option(DummyValue, "Encryption key", translate("iwscan_encr"))
110
111 t2:option(DummyValue, "Signal level", translate("iwscan_signal"))
112
113 t2:option(DummyValue, "Noise level", translate("iwscan_noise"))
114
115
116 s2 = m:section(SimpleSection, translate("a_w_create"))
117 create = s2:option(ListValue, "create", translate("device"))
118 create:value("", translate("cbi_select"))
119 for k, v in pairs(wireless) do
120 if v[".type"] == "wifi-device" then
121 create:value(k)
122 end
123 end
124
125 function create.write(self, section, value)
126 luci.model.uci.load_config("wireless")
127 luci.model.uci.section("wireless", "wifi-iface", nil, {device=value})
128 luci.model.uci.save_config("wireless")
129 luci.http.redirect(luci.http.getenv("REQUEST_URI") .. "/" .. value)
130 end
131
132 function create.cbid(self, section)
133 return "priv.cbid.create"
134 end
135
136 return m