modules/admin-full: change workflow of wifi config page; display only one network...
[project/luci.git] / modules / admin-full / luasrc / controller / admin / network.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14 module("luci.controller.admin.network", package.seeall)
15
16 function index()
17         require("luci.i18n")
18         local uci = require("luci.model.uci").cursor()
19         local i18n = luci.i18n.translate
20
21         local page  = node("admin", "network")
22         page.target = alias("admin", "network", "network")
23         page.title  = i18n("network")
24         page.order  = 50
25         page.index  = true
26
27         local page  = node("admin", "network", "vlan")
28         page.target = cbi("admin_network/vlan")
29         page.title  = i18n("a_n_switch")
30         page.order  = 20
31
32         local page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("wifi"), 15)
33         page.i18n   = "wifi"
34         page.leaf = true
35         page.subindex = true
36
37         uci:foreach("wireless", "wifi-device",
38                 function (section)
39                         local ifc = section[".name"]
40                                 entry({"admin", "network", "wireless", ifc},
41                                  true,
42                                  ifc:upper()).i18n = "wifi"
43                 end
44         )
45
46         local page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
47         page.i18n = "wifi"
48         page.leaf = true
49
50         local page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("interfaces", "Schnittstellen"), 10)
51         page.leaf   = true
52         page.subindex = true
53
54         local page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
55         page.leaf = true
56
57         uci:foreach("network", "interface",
58                 function (section)
59                         local ifc = section[".name"]
60                         if ifc ~= "loopback" then
61                                 entry({"admin", "network", "network", ifc},
62                                  true,
63                                  ifc:upper())
64                         end
65                 end
66         )
67
68         local page  = node("admin", "network", "dhcp")
69         page.target = cbi("admin_network/dhcp")
70         page.title  = "DHCP"
71         page.order  = 30
72         page.subindex = true
73
74         entry(
75          {"admin", "network", "dhcp", "leases"},
76          cbi("admin_network/dhcpleases"),
77          i18n("dhcp_leases")
78         )
79
80         local page  = node("admin", "network", "hosts")
81         page.target = cbi("admin_network/hosts")
82         page.title  = i18n("hostnames", "Hostnames")
83         page.order  = 40
84
85         local page  = node("admin", "network", "routes")
86         page.target = cbi("admin_network/routes")
87         page.title  = i18n("a_n_routes_static")
88         page.order  = 50
89
90 end
91
92 function wifi_join()
93         local function param(x)
94                 return luci.http.formvalue(x)
95         end
96
97         local function ptable(x)
98                 x = param(x)
99                 return x and (type(x) ~= "table" and { x } or x) or {}
100         end
101
102         local dev  = param("device")
103         local ssid = param("join")
104
105         if dev and ssid then
106                 local wep     = (tonumber(param("wep")) == 1)
107                 local wpa     = tonumber(param("wpa_version")) or 0
108                 local channel = tonumber(param("channel"))
109                 local mode    = param("mode")
110                 local bssid   = param("bssid")
111
112                 local confirm = (param("confirm") == "1")
113                 local cancel  = param("cancel") and true or false
114
115                 if confirm and not cancel then
116                         local fixed_bssid = (param("fixed_bssid") == "1")
117                         local replace_net = (param("replace_net") == "1")
118                         local autoconnect = (param("autoconnect") == "1")
119                         local attach_intf = param("attach_intf")
120
121                         local uci = require "luci.model.uci".cursor()
122
123                         if replace_net then
124                                 uci:delete_all("wireless", "wifi-iface")
125                         end
126
127                         local wificonf = {
128                                 device  = dev,
129                                 mode    = (mode == "Ad-Hoc" and "adhoc" or "sta"),
130                                 ssid    = ssid
131                         }
132
133                         if attach_intf and uci:get("network", attach_intf, "ifname") then
134                                 -- target network already has a interface, make it a bridge
135                                 uci:set("network", attach_intf, "type", "bridge")
136                                 uci:save("network")
137                                 uci:commit("network")
138
139                                 if autoconnect then
140                                         require "luci.sys".call("/sbin/ifup " .. attach_intf)
141                                 end
142                         end
143
144                         if fixed_bssid then
145                                 wificonf.bssid = bssid
146                         end
147
148                         if wep then
149                                 wificonf.encryption = "wep"
150                                 wificonf.key = param("key")
151                         elseif wpa > 0 then
152                                 wificonf.encryption = param("wpa_suite")
153                                 wificonf.key = param("key")
154                         end
155
156                         uci:section("wireless", "wifi-iface", nil, wificonf)
157                         uci:delete("wireless", dev, "disabled")
158                         uci:set("wireless", dev, "channel", channel)
159
160                         uci:save("wireless")
161                         uci:commit("wireless")
162
163                         if autoconnect then
164                                 require "luci.sys".call("/sbin/wifi")
165                         end
166
167                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless", dev))
168                 elseif cancel then
169                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
170                 else
171                         luci.template.render("admin_network/wifi_join_settings")
172                 end
173         else
174                 luci.template.render("admin_network/wifi_join")
175         end
176 end