modules/admin-full: rework wifi configuration
[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         local has_wifi = nixio.fs.stat("/etc/config/wireless")
21         local has_switch = false
22
23         uci:foreach("network", "switch",
24                 function(s)
25                         has_switch = true
26                         return false
27                 end
28         )
29
30         local page  = node("admin", "network")
31         page.target = alias("admin", "network", "network")
32         page.title  = i18n("Network")
33         page.order  = 50
34         page.index  = true
35
36         if has_switch then
37                 local page  = node("admin", "network", "vlan")
38                 page.target = cbi("admin_network/vlan")
39                 page.title  = i18n("Switch")
40                 page.order  = 20
41         end
42
43         if has_wifi and has_wifi.size > 0 then
44                 local page
45
46                 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
47                 page.leaf = true
48                 page.subindex = true
49
50                 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
51                 page.leaf = true
52
53                 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
54                 page.leaf = true
55
56                 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
57                 page.leaf = true
58
59                 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
60                 page.leaf = true
61         end
62
63         local page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
64         page.leaf   = true
65         page.subindex = true
66
67         local page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
68         page.leaf = true
69
70         uci:foreach("network", "interface",
71                 function (section)
72                         local ifc = section[".name"]
73                         if ifc ~= "loopback" then
74                                 entry({"admin", "network", "network", ifc},
75                                  true,
76                                  ifc:upper())
77                         end
78                 end
79         )
80
81         if nixio.fs.access("/etc/config/dhcp") then
82                 local page  = node("admin", "network", "dhcpleases")
83                 page.target = cbi("admin_network/dhcpleases")
84                 page.title  = i18n("DHCP Leases")
85                 page.order  = 30
86         end
87
88         local page  = node("admin", "network", "hosts")
89         page.target = cbi("admin_network/hosts")
90         page.title  = i18n("Hostnames")
91         page.order  = 40
92
93         local page  = node("admin", "network", "routes")
94         page.target = cbi("admin_network/routes")
95         page.title  = i18n("Static Routes")
96         page.order  = 50
97
98 end
99
100 function wifi_join()
101         local function param(x)
102                 return luci.http.formvalue(x)
103         end
104
105         local function ptable(x)
106                 x = param(x)
107                 return x and (type(x) ~= "table" and { x } or x) or {}
108         end
109
110         local dev  = param("device")
111         local ssid = param("join")
112
113         if dev and ssid then
114                 local cancel  = (param("cancel") or param("cbi.cancel")) and true or false
115
116                 if cancel then
117                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
118                 else
119                         local cbi = require "luci.cbi"
120                         local tpl = require "luci.template"
121                         local map = luci.cbi.load("admin_network/wifi_add")[1]
122
123                         if map:parse() ~= cbi.FORM_DONE then
124                                 tpl.render("header")
125                                 map:render()
126                                 tpl.render("footer")
127                         end
128                 end
129         else
130                 luci.template.render("admin_network/wifi_join")
131         end
132 end
133
134 function wifi_add()
135         local dev = luci.http.formvalue("device")
136         local uci = require "luci.model.uci".cursor()
137         local wlm = require "luci.model.wireless"
138
139         if dev then
140                 wlm.init(uci)
141
142                 local net = wlm:add_network({
143                         device     = dev,
144                         mode       = "ap",
145                         ssid       = "OpenWrt",
146                         encryption = "none"
147                 })
148
149                 uci:save("wireless")
150                 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless", dev, net:name()))
151         end
152 end
153
154 function wifi_delete(network)
155         local uci = require "luci.model.uci".cursor()
156         local wlm = require "luci.model.wireless"
157
158         wlm.init(uci)
159         wlm:del_network(network)
160
161         uci:save("wireless")
162         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
163 end
164
165 function wifi_status()
166         local function jsondump(x)
167                 if x == nil then
168                         luci.http.write("null")
169                 elseif type(x) == "table" then
170                         local k, v
171                         if type(next(x)) == "number" then
172                                 luci.http.write("[ ")
173                                 for k, v in ipairs(x) do
174                                         jsondump(v)
175                                         if next(x, k) then
176                                                 luci.http.write(", ")
177                                         end
178                                 end
179                                 luci.http.write(" ]")
180                         else
181                                 luci.http.write("{ ")
182                                 for k, v in pairs(x) do
183                                 luci.http.write("%q: " % k)
184                                         jsondump(v)
185                                         if next(x, k) then
186                                                 luci.http.write(", ")
187                                         end
188                                 end
189                                 luci.http.write(" }")
190                         end
191                 elseif type(x) == "number" or type(x) == "boolean" then
192                         luci.http.write(tostring(x))
193                 elseif type(x) == "string" then
194                         luci.http.write("%q" % tostring(x))
195                 end
196         end
197
198
199         local path = luci.dispatcher.context.requestpath
200         local dev  = path[#path]
201         local iw   = luci.sys.wifi.getiwinfo(dev)
202
203         if iw then
204                 local f
205                 local j = { }
206                 for _, f in ipairs({
207                         "channel", "frequency", "txpower", "bitrate", "signal", "noise",
208                         "quality", "quality_max", "mode", "ssid", "bssid", "country",
209                         "encryption", "mbssid_support", "ifname"
210                 }) do
211                         j[f] = iw[f]
212                 end
213
214                 luci.http.prepare_content("application/json")
215                 jsondump(j)
216                 return
217         end
218
219         luci.http.status(404, "No such device")
220 end