2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
14 module("luci.controller.admin.network", package.seeall)
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
23 uci:foreach("network", "switch",
30 local page = node("admin", "network")
31 page.target = alias("admin", "network", "network")
32 page.title = i18n("Network")
37 local page = node("admin", "network", "vlan")
38 page.target = cbi("admin_network/vlan")
39 page.title = i18n("Switch")
43 if has_wifi and has_wifi.size > 0 then
46 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
50 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
53 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
56 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
59 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
63 local page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
67 local page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
70 uci:foreach("network", "interface",
72 local ifc = section[".name"]
73 if ifc ~= "loopback" then
74 entry({"admin", "network", "network", ifc},
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")
88 local page = node("admin", "network", "hosts")
89 page.target = cbi("admin_network/hosts")
90 page.title = i18n("Hostnames")
93 local page = node("admin", "network", "routes")
94 page.target = cbi("admin_network/routes")
95 page.title = i18n("Static Routes")
101 local function param(x)
102 return luci.http.formvalue(x)
105 local function ptable(x)
107 return x and (type(x) ~= "table" and { x } or x) or {}
110 local dev = param("device")
111 local ssid = param("join")
114 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
117 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
119 local cbi = require "luci.cbi"
120 local tpl = require "luci.template"
121 local map = luci.cbi.load("admin_network/wifi_add")[1]
123 if map:parse() ~= cbi.FORM_DONE then
130 luci.template.render("admin_network/wifi_join")
135 local dev = luci.http.formvalue("device")
136 local uci = require "luci.model.uci".cursor()
137 local wlm = require "luci.model.wireless"
142 local net = wlm:add_network({
150 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless", dev, net:name()))
154 function wifi_delete(network)
155 local uci = require "luci.model.uci".cursor()
156 local wlm = require "luci.model.wireless"
159 wlm:del_network(network)
162 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
165 function wifi_status()
166 local function jsondump(x)
168 luci.http.write("null")
169 elseif type(x) == "table" then
171 if type(next(x)) == "number" then
172 luci.http.write("[ ")
173 for k, v in ipairs(x) do
176 luci.http.write(", ")
179 luci.http.write(" ]")
181 luci.http.write("{ ")
182 for k, v in pairs(x) do
183 luci.http.write("%q: " % k)
186 luci.http.write(", ")
189 luci.http.write(" }")
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))
199 local path = luci.dispatcher.context.requestpath
200 local dev = path[#path]
201 local iw = luci.sys.wifi.getiwinfo(dev)
207 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
208 "quality", "quality_max", "mode", "ssid", "bssid", "country",
209 "encryption", "mbssid_support", "ifname"
214 luci.http.prepare_content("application/json")
219 luci.http.status(404, "No such device")