[luci] Blank page after submit...
Grzegorz Głowacki
ralwing at o2.pl
Mon Feb 6 12:29:42 CET 2012
Hi,
I have eventually created a form which contains 2 simpleforms, with 2 submit buttons, but after i click submit i can only see a blank page. When i traced the execution of write functions, it seems like it's broken in the middle of the code execution, and the blank page is sent to the user. It mostly happens in function config, after "eth0" interface has been deleted. There is also another possibility, it infinitely deletes "wifi" interface. Why is that?
Below is some of my source code.
Thank You very much;)
--
Grzegorz
my controller index function:
function index()
page = entry({"admin", "qs"}, arcombine(template("admin_qs/wifi_overview"), cbi("admin_qs/wifi")), "Quick setup", 30)
page.dependent=false
page.subindex = true
page.leaf = true
local uci = require("luci.model.uci").cursor()
local net = require "luci.model.network".init(uci)
local wdev
for _, wdev in ipairs(net:get_wifidevs()) do
local wnet
for _, wnet in ipairs(wdev:get_wifinets()) do
page = entry(
{"admin", "qs", wnet:id()},
cbi("admin_qs/wifi", {autoapply=true}),
wdev:name() .. ": " .. wnet:shortname()
)
page.leaf = true
end
end
end
my cbi model:
local uci = require "luci.model.uci".cursor()
local nw = require "luci.model.network"
local fw = require "luci.model.firewall"
local sys = require "luci.sys"
local os = require "os"
local http = require "luci.http"
local util = require("luci.util")
local datatypes = require("luci.cbi.datatypes")
local class = util.class
local instanceof = util.instanceof
nw.init(uci)
fw.init(uci)
if luci.dispatcher.context.requested.path and #luci.dispatcher.context.requested.path > 0 then
net_name=luci.dispatcher.context.requested.path[#luci.dispatcher.context.requested.path]
end
arg[1] = arg[1] or net_name
assert(arg[1])
net_name = arg[1]
local wnet = nw:get_wifinet(net_name)
assert(wnet)
local wdev = wnet:get_device()
-------------------- View --------------------
f = SimpleForm("wireless", translate("Wizard"),
translate("This wizard will assist you in setting up your AP or client device."))
st = f:field( DummyValue, "__status", translate("Status"))
st.template = "admin_network/wifi_status"
st.ifname = net_name
local en = f:field( Flag, "disabled",translate("Enable device"))
en.enabled = "0"
en.disabled = "1"
en.rmempty = false
function en.cfgvalue(self, section)
return uci:get("wireless", wdev:name(), "disabled") or "1"
end
function en.write(self, sec, value)
if not value then
value = Flag.formvalue( )
end
if value then
uci:set("wireless", wdev:name(), "disabled", value)
uci:save("wireless")
end
end
function en.parse(self, section)
local value = self:formvalue(section)
value = not value and "1" or "0"
self:write(section, value)
end
local chan = f:field( ListValue, "channel", translate("Channel"))
chan:value("auto", translate("auto"))
chan.rmempty = false
for _, f in ipairs(sys.wifi.channels(device)) do
if not f.restricted then
chan:value(f.channel)
end
end
function chan.cfgvalue(self, section)
return uci:get("wireless", wdev:name(), "channel")
end
function chan.write(self, sec, value)
if value then
uci:set("wireless", wdev:name(), "channel", value)
uci:save("wireless")
end
end
local iw = luci.sys.wifi.getiwinfo(net_name)
local tx_powers = iw.txpwrlist or { }
local tx_power = tostring(
(iw.txpower and iw.txpower > 0 and iw.txpower) or
(#tx_powers > 0 and tx_powers[#tx_powers].dbm)
)
tp = f:field(
(tx_powers and #tx_powers > 0) and ListValue or Value,
"txpower", translate("Transmit Power"), "dBm")
tp.rmempty = false
tp.default = tx_power
for _, p in ipairs(tx_powers or {}) do
tp:value(p.dbm, "%i dBm (%i mW)" %{ p.dbm, p.mw })
end
mode = f:field( ListValue, "mode", translate("Mode"))
mode.override_values = true
mode:value("ap", translate("Access Point"))
mode:value("sta", translate("Client"))
mode:value("ap-wds", "%s (%s)" % {translate("Access Point"), translate("WDS")})
mode:value("sta-wds", "%s (%s)" % {translate("Client"), translate("WDS")})
function mode.write(self, section, value)
if value then
if value == "ap-wds" then
wnet:set("mode", "ap")
wnet:set( "wds", 1)
elseif value == "sta-wds" then
wnet:set("mode", "sta")
wnet:set( "wds", 1)
else
wnet:set("mode", value)
uci:foreach("wireless", "wifi-iface",
function(s)
if s.device == wdev:name() then
uci:delete("wireless", s['.name'], "wds")
return false
end
end)
end
end
uci:save("wireless")
uci:commit("wireless")
end
function mode.cfgvalue(self, section)
local mode = wnet:mode( )
local wds = wnet:get("wds") == "1"
assert(mode)
-- sys.exec("echo \" mode.cfgvalue:" .. mode .. " \" >> /tmp/logik")
if mode == "ap" and wds then
return "ap-wds"
elseif mode == "sta" and wds then
return "sta-wds"
else
return mode
end
end
ssid = f:field( Value, "ssid", translate("<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
function ssid.cfgvalue(self, section)
val = nil
uci:foreach("wireless", "wifi-iface",
function(s)
if s.device == wdev:name() then
val = uci:get("wireless", s['.name'], "ssid")
return false
end
end)
return val or ""
end
function ssid.write(self, sec, value)
if value then
uci:foreach("wireless", "wifi-iface",
function(s)
if s.device == wdev:name() then
uci:set("wireless", s['.name'], "ssid", value )
return false
end
end)
uci:set("wireless", wdev:name(), "ssid", value)
uci:save("wireless")
end
end
hidden = f:field( Flag, "hidden", translate("Hide <abbr title=\"Extended Service Set Identifier\">ESSID</abbr>"))
hidden:depends({mode="ap"})
hidden:depends({mode="ap-wds"})
hidden.rmempty = true
function hidden.cfgvalue(self, section)
value = nil
uci:foreach("wireless", "wifi-iface",
function(s)
if s.device == wdev:name() then
value = uci:get("wireless", s['.name'], "hidden")
end
return false
end)
return value or "0"
end
function hidden.write(self, sec, value)
if not value then
value = "0"
end
if value then
uci:foreach("wireless", "wifi-iface",
function(s)
if s.device == wdev:name() then
if value == "1" then
uci:set("wireless", s['.name'], "hidden", "1")
else
uci:delete("wireless", s['.name'], "hidden")
end
return false
end
end)
uci:save("wireless")
-- uci:commit("wireless")
end
end
function hidden.parse(self, section)
local value = self:formvalue(section)
value = value and "1" or "0"
self:write(section, value)
end
function if_wifi( section )
sys.exec("echo \" if_wifi " .. section .. " \" >> /tmp/logik")
opt_wifi = { proto=p_wifi:formvalue( section ), netmask=nm_wifi:formvalue( section ), ipaddr=ipaddr_wifi:formvalue( section ), gwaddr=gw_wifi:formvalue( section ) }
sys.exec("echo \"WIFI 1:" .. opt_wifi.proto .. " 2:" ..opt_wifi.netmask .. " 3:" .. opt_wifi.ipaddr .. " \" >> /tmp/logik")
wifi=nw:add_network( "wifi", opt_wifi)
sys.exec("echo \" add_nw \" >> /tmp/logik")
assert(wifi)
nw:save("network")
sys.exec("echo \" ci_nw \" >> /tmp/logik")
wdev:set("disabled",false)
wdev:set("auto",true)
wifi:add_interface(net_name)
nw:save("wireless")
sys.exec("echo \" ci_wifi \" >> /tmp/logik")
return wifi
end
function if_eth0( section )
sys.exec("echo \" if_eth0 " .. section .. " \" >> /tmp/logik")
opt_eth0 = { ifname="eth0", proto=p_eth0:formvalue( section ), netmask=nm_eth0:formvalue( section ), ipaddr=ipaddr_eth0:formvalue( section ), gwaddr=gw_eth0:formvalue( section ) }
sys.exec("echo \"ETH0 1:" .. opt_eth0.proto .. " 2:" ..opt_eth0.netmask .. " 3:" .. opt_eth0.ipaddr .. " \" >> /tmp/logik")
eth0=nw:add_network( "eth0", opt_eth0)
sys.exec("echo \" add_nw \" >> /tmp/logik")
assert(eth0)
eth0:add_interface("eth0")
sys.exec("echo \" add_itf \" >> /tmp/logik")
nw:save("network")
sys.exec("echo \" ci_nw \" >> /tmp/logik")
return eth0
end
------------------- MODE == CLIENT -------------------
function wifi_sta( section )
sys.exec("echo \" wifi_sta " .. section .. " \" >> /tmp/logik")
if_eth0( section )
if_wifi( section )
defaults = fw:get_defaults()
assert(defaults)
defaults:set("syn_flood", "1")
defaults:set("input", "ACCEPT")
defaults:set("output", "ACCEPT")
defaults:set("forward", "REJECT")
wifi = fw:add_zone("wifi")
assert(wifi)
wifi:set("input", "ACCEPT")
wifi:set("output", "ACCEPT")
wifi:set("forward", "REJECT")
wifi:set("masq", "1")
wifi:add_network("wifi")
eth0 = fw:add_zone("eth0")
assert(eth0)
eth0:set("input", "ACCEPT")
eth0:set("output", "ACCEPT")
eth0:set("forward", "REJECT")
eth0:add_network("eth0")
eth0:add_forwarding_to("wifi")
uci:save("firewall")
end
------------------- MODE == ACCESS POINT -------------------
function wifi_ap( section )
sys.exec("echo \" wifi_ap " .. section .. " \" >> /tmp/logik")
if_eth0( section )
if_wifi( section )
defaults = fw:get_defaults()
assert(defaults)
defaults:set("syn_flood", "1")
defaults:set("input", "ACCEPT")
defaults:set("output", "ACCEPT")
defaults:set("forward", "REJECT")
eth0 = fw:add_zone("eth0")
assert(eth0)
eth0:set("input", "ACCEPT")
eth0:set("output", "ACCEPT")
eth0:set("forward", "REJECT")
eth0:set("masq", "1")
eth0:add_network("eth0")
wifi = fw:add_zone("wifi")
assert(wifi)
wifi:set("input", "ACCEPT")
wifi:set("output", "ACCEPT")
wifi:set("forward", "REJECT")
wifi:add_network("wifi")
wifi:add_forwarding_to("eth0")
uci:save("firewall")
end
------------------- MODE == WDS -------------------
function wifi_wds( section )
sys.exec("echo \" if_wifi_sta " .. section .. " \" >> /tmp/logik")
eth0 = if_eth0( section )
eth0:set("type", "bridge")
eth0:add_interface("wifi")
defaults = fw:get_defaults()
assert(defaults)
defaults:set("syn_flood", "1")
defaults:set("input", "ACCEPT")
defaults:set("output", "ACCEPT")
defaults:set("forward", "REJECT")
defaults:set("drop_invalid","1")
uci:save("network")
uci:save("wireless")
uci:save("firewall")
end
function config( section )
sys.exec("echo \" config \" >> /tmp/logik")
radio_mode = mode:cfgvalue(nil)
sys.exec("echo \" MODE: " .. tostring( radio_mode ) .. " \" >> /tmp/logik")
assert( radio_mode == "ap" or radio_mode == "sta" or radio_mode == "ap-wds" or radio_mode == "sta-wds" )
uci:foreach("network", "interface",
function (section)
local ifc = section[".name"]
if ifc ~= "loopback" then
sys.exec("echo \" delete " .. ifc .. " \" >> /tmp/logik")
uci:delete("network",ifc,"type")
net = nw:del_network(ifc)
if net then
nw:save("network")
nw:save("wireless")
end
end
end
)
sys.exec("echo \" delete firewall rules and zones \" >> /tmp/logik")
uci:delete_all("firewall", "rule")
uci:delete_all("firewall", "zone")
fw:save("firewall")
if radio_mode == "ap" then
wifi_ap( section )
elseif radio_mode == "sta" then
wifi_sta( section )
else
wifi_wds( section )
end
end
fn = SimpleForm("network", translate("Network"))
section_wifi = fn:field( DummyValue, "__section_wifi", translate("Wireless"))
p_wifi = fn:field(ListValue, "proto_wifi", translate("Wireless protocol"), translate ("The protocol to use for internet connectivity."))
p_wifi:value("static", translate("static", "static"))
p_wifi:value("dhcp", translate("dhcp", "dhcp"))
ipaddr_wifi = fn:field(Value, "ipaddr_wifi", translate("Wireless Ip address"))
ipaddr_wifi.datatype = "ip4addr"
function ipaddr_wifi.cfgvalue(self, section)
sys.exec("echo \" ipaddr_wifi.cfgvalue \" >> /tmp/logik")
return uci:get("network", "wifi", "ipaddr")
end
function ipaddr_wifi.write(self, section, value)
uci:set("network", "wifi", "ipaddr", value)
config(section)
end
function ipaddr_wifi.parse(self, section)
local value = self:formvalue(section)
if value then
self:write(section, value)
end
end
nm_wifi = fn:field(Value, "netmask_wifi", translate("Wireless netmask"))
nm_wifi:value("255.255.255.0")
nm_wifi:value("255.255.0.0")
nm_wifi:value("255.0.0.0")
nm_wifi.datatype = "ip4addr"
function nm_wifi.cfgvalue(self, section)
sys.exec("echo \" nm_wifi.cfgvalue\" >> /tmp/logik")
return uci:get("network", "wifi", "netmask")
end
function nm_wifi.write(self, section, value)
sys.exec("echo \" nm_wifi.write " .. tostring(value) .. " form: " .. self:formvalue(section) .. " \" >> /tmp/logik")
end
-----------------------------ETH0----------------------------------------------
section_eth0 = fn:field( DummyValue, "__section_eth", translate("<H2>Ethernet</H2>"))
p_eth0 = fn:field(ListValue, "proto_eth0", translate("Ethernet protocol"), translate ("The protocol to use for internet connectivity."))
p_eth0:value("static", translate("static", "static"))
p_eth0:value("dhcp", translate("dhcp", "dhcp"))
ipaddr_eth0 = fn:field(Value, "ipaddr_eth0", translate("Ethernet Ip address"))
ipaddr_eth0.datatype = "ip4addr"
function ipaddr_eth0.cfgvalue(self, section)
sys.exec("echo \" ipaddr_eth0.cfgvalue \" >> /tmp/logik")
return uci:get("network", "eth0", "ipaddr")
end
function ipaddr_eth0.write(self, section, value)
sys.exec("echo \" ipaddr_eth0.write " .. tostring(value) .. " form: " .. self:formvalue(section) .. "\" >> /tmp/logik")
uci:set("network", "eth0", "ipaddr", value)
-- if_wifi_sta(section)
end
function ipaddr_eth0.parse(self, section)
local value = self:formvalue(section)
if value then
self:write(section, value)
end
end
nm_eth0 = fn:field(Value, "netmask_eth0", translate("Ethernet Netmask"))
nm_eth0:value("255.255.255.0")
nm_eth0:value("255.255.0.0")
nm_eth0:value("255.0.0.0")
nm_eth0.datatype = "ip4addr"
function nm_eth0.cfgvalue(self, section)
sys.exec("echo \" nm_eth0.cfgvalue\" >> /tmp/logik")
return uci:get("network", "eth0", "netmask")
end
function nm_eth0.write(self, section, value)
sys.exec("echo \" nm_eth0.write " .. tostring(value) .. " form: " .. self:formvalue(section) .. " \" >> /tmp/logik")
end
return f, fn
More information about the luci
mailing list