0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-12-22 11:03:48 +01:00

Minor changes to UCI error handling.

This commit is contained in:
Wouter R 2013-12-23 15:33:55 +01:00
parent 8fda75664c
commit 1fe9da5985
2 changed files with 40 additions and 38 deletions

View File

@ -46,12 +46,12 @@ function M._global_GET(request, response)
for k,v in pairs(request:getAll()) do for k,v in pairs(request:getAll()) do
local r,m = settings.get(k) local r,m = settings.get(k)
if r ~= nil then if r ~= nil then
response:addData(k, r) response:addData(k, r)
else else
response:addData(k, "could not read key ('" .. m .. "')") response:addData(k, "could not read key ('" .. m .. "')")
response:setError(m) response:setError(m)
return false; return
end end
end end
end end
@ -75,7 +75,7 @@ function M._global_POST(request, response)
log:info(" m: "..utils.dump(m)) log:info(" m: "..utils.dump(m))
elseif r == nil then elseif r == nil then
response:setError(m) response:setError(m)
return false; return
end end
end end
response:addData("validation",validation) response:addData("validation",validation)
@ -83,7 +83,7 @@ function M._global_POST(request, response)
local substitutedSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) local substitutedSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
response:addData("substituted_ssid",substitutedSsid) response:addData("substituted_ssid",substitutedSsid)
-- we now call signin seperatly trough cgi-bin -- we now call signin seperately trough cgi-bin
--[[log:info("API:Network:try signing in") --[[log:info("API:Network:try signing in")
if signin.signin() then if signin.signin() then
log:info("API:Network:signin successfull") log:info("API:Network:signin successfull")
@ -101,7 +101,7 @@ function M.all_GET(request, response)
end end
else else
response:setError(msg) response:setError(msg)
return false; return
end end
end end
@ -121,10 +121,10 @@ function M.reset_POST(request, response)
local r,m = settings.reset(k); local r,m = settings.reset(k);
if r ~= nil then if r ~= nil then
response:addData(k, "ok") response:addData(k, "ok")
else else
response:addData(k, "could not reset key ('" .. m .. "')") response:addData(k, "could not reset key ('" .. m .. "')")
response:setError(m) response:setError(m)
return false; return
end end
end end
end end
@ -133,14 +133,14 @@ end
function M.resetall_POST(request, response) function M.resetall_POST(request, response)
if not operationsAccessOrFail(request, response) then return end if not operationsAccessOrFail(request, response) then return end
response:setSuccess() response:setSuccess()
local rv, msg = settings.resetAll() local rv, msg = settings.resetAll()
if(rv == nil) then if(rv == nil) then
response:setError(msg) response:setError(msg)
return false return
end end
for k,v in pairs(settings.getAll()) do for k,v in pairs(settings.getAll()) do
response:addData(k,v) response:addData(k,v)
end end

View File

@ -156,31 +156,32 @@ end]]--
--- Returns the value of the requested key if it exists. --- Returns the value of the requested key if it exists.
-- @p key The key to return the associated value for. -- @p key The key to return the associated value for.
-- @return The associated value, beware (!) that this may be boolean false for keys of 'bool' type. -- @return The associated value, beware (!) that this may be boolean false for keys of 'bool' type, or nil if the key could not be read because of a UCI error.
-- @treturn string Message in case of error.
function M.get(key) function M.get(key)
--log:info("settings:get: "..utils.dump(key)) --log:info("settings:get: "..utils.dump(key))
key = replaceDots(key) key = replaceDots(key)
local base = getBaseKeyTable(key) local base = getBaseKeyTable(key)
if not base then return nil,ERR_NO_SUCH_KEY end if not base then return nil,ERR_NO_SUCH_KEY end
local section = UCI_CONFIG_SECTION; local section = UCI_CONFIG_SECTION;
if base.subSection ~= nil then if base.subSection ~= nil then
section = M.get(base.subSection) section = M.get(base.subSection)
end end
local uciV,msg = uci:get(UCI_CONFIG_NAME, section, key) local uciV,msg = uci:get(UCI_CONFIG_NAME, section, key)
if not uciV and msg ~= nil then if not uciV and msg ~= nil then
local errorMSG = "Issue reading setting '"..utils.dump(key).."': "..utils.dump(msg); local errorMSG = "Issue reading setting '"..utils.dump(key).."': "..utils.dump(msg);
log:info(errorMSG) log:info(errorMSG)
return nil, errorMSG; return nil, errorMSG;
end end
local uciV = fromUciValue(uciV, base.type) local uciV = fromUciValue(uciV, base.type)
if uciV ~= nil then if uciV ~= nil then
-- returning value from uci -- returning value from uci
return uciV return uciV
elseif base.subSection ~= nil then elseif base.subSection ~= nil then
local subDefault = base["default_"..section] local subDefault = base["default_"..section]
if subDefault ~= nil then if subDefault ~= nil then
-- returning subsection default value -- returning subsection default value
@ -192,14 +193,15 @@ function M.get(key)
end end
--- Returns all configuration keys with their current values. --- Returns all configuration keys with their current values.
-- @treturn table A table containing a key/value pair for each configuration key. -- @return A table containing a key/value pair for each configuration key, or nil if a UCI error occured.
-- @return string Message in case of error.
function M.getAll() function M.getAll()
local result = {} local result = {}
for k,_ in pairs(baseconfig) do for k,_ in pairs(baseconfig) do
if not k:match('^[A-Z_]*$') then --TEMP: skip 'constants', which should be moved anyway if not k:match('^[A-Z_]*$') then --TEMP: skip 'constants', which should be moved anyway
local key = replaceUnderscores(k) local key = replaceUnderscores(k)
local v, msg = M.get(key) local v, msg = M.get(key)
if not uciV and msg ~= nil then if not v and msg ~= nil then
return nil, msg return nil, msg
else else
result[key] = v result[key] = v
@ -249,7 +251,7 @@ function M.set(key, value)
local base = getBaseKeyTable(key) local base = getBaseKeyTable(key)
if not base then return false,ERR_NO_SUCH_KEY end if not base then return false,ERR_NO_SUCH_KEY end
--log:info(" base.type: "..utils.dump(base.type)) --log:info(" base.type: "..utils.dump(base.type))
if base.type == 'bool' then if base.type == 'bool' then
if value ~= "" then if value ~= "" then
@ -271,7 +273,7 @@ function M.set(key, value)
end end
local section = UCI_CONFIG_SECTION; local section = UCI_CONFIG_SECTION;
if base.subSection ~= nil then if base.subSection ~= nil then
section = M.get(base.subSection) section = M.get(base.subSection)
local rv, msg = uci:set(UCI_CONFIG_NAME, section, UCI_CONFIG_TYPE) local rv, msg = uci:set(UCI_CONFIG_NAME, section, UCI_CONFIG_TYPE)
if not rv and msg ~= nil then if not rv and msg ~= nil then
@ -296,12 +298,12 @@ function M.set(key, value)
return nil, errorMSG; return nil, errorMSG;
end end
end end
uci:commit(UCI_CONFIG_NAME) uci:commit(UCI_CONFIG_NAME)
return true return true
end end
--- Reset all settings to their default values --- Reset all settings to their default values
-- @string key The key to set. -- @string key The key to set.
-- @treturn bool|nil True if everything went well, nil in case of error. -- @treturn bool|nil True if everything went well, nil in case of error.
function M.resetAll() function M.resetAll()
@ -314,8 +316,8 @@ function M.resetAll()
log:info(errorMSG) log:info(errorMSG)
return nil, errorMSG; return nil, errorMSG;
end end
for key,value in pairs(allSections) do for key,value in pairs(allSections) do
if key ~= "system" and not key:match('^[A-Z_]*$') then --TEMP: skip 'constants', which should be moved anyway if key ~= "system" and not key:match('^[A-Z_]*$') then --TEMP: skip 'constants', which should be moved anyway
local rv, msg = uci:delete(UCI_CONFIG_NAME,key) local rv, msg = uci:delete(UCI_CONFIG_NAME,key)
if not rv and msg ~= nil then if not rv and msg ~= nil then
@ -323,33 +325,33 @@ function M.resetAll()
log:info(errorMSG) log:info(errorMSG)
return nil, errorMSG; return nil, errorMSG;
end end
end end
end end
-- reset all to defaults -- reset all to defaults
for k,_ in pairs(baseconfig) do for k,_ in pairs(baseconfig) do
if not k:match('^[A-Z_]*$') then --TEMP: skip 'constants', which should be moved anyway if not k:match('^[A-Z_]*$') then --TEMP: skip 'constants', which should be moved anyway
M.reset(k) M.reset(k)
end end
end end
uci:commit(UCI_CONFIG_NAME) uci:commit(UCI_CONFIG_NAME)
return true return true
end end
--- Reset setting to default value --- Reset setting to default value
-- @string key The key to reset. -- @string key The key to reset.
-- @treturn bool|nil True if everything went well, nil in case of error. -- @treturn bool|nil True if everything went well, nil in case of error.
function M.reset(key) function M.reset(key)
log:info("settings:reset: "..utils.dump(key)) log:info("settings:reset: "..utils.dump(key))
-- delete -- delete
key = replaceDots(key) key = replaceDots(key)
local base = getBaseKeyTable(key) local base = getBaseKeyTable(key)
if not base then return nil,ERR_NO_SUCH_KEY end if not base then return nil,ERR_NO_SUCH_KEY end
local section = UCI_CONFIG_SECTION; local section = UCI_CONFIG_SECTION;
if base.subSection ~= nil then if base.subSection ~= nil then
section = M.get(base.subSection) section = M.get(base.subSection)
end end
local rv, msg = uci:delete(UCI_CONFIG_NAME, section, key) local rv, msg = uci:delete(UCI_CONFIG_NAME, section, key)
@ -358,10 +360,10 @@ function M.reset(key)
log:info(errorMSG) log:info(errorMSG)
return nil, errorMSG; return nil, errorMSG;
end end
-- reuse get logic to retrieve default and set it. -- reuse get logic to retrieve default and set it.
M.set(key,M.get(key)) M.set(key,M.get(key))
uci:commit(UCI_CONFIG_NAME) uci:commit(UCI_CONFIG_NAME)
return true return true
end end
@ -369,7 +371,7 @@ end
--- Returns a UCI configuration key from the system section. --- Returns a UCI configuration key from the system section.
-- @string key The key for which to return the value, must be non-empty. -- @string key The key for which to return the value, must be non-empty.
-- @return Requested value or false if it does not exist or nil on invalid key. -- @return Requested value or false if it does not exist or nil on UCI error.
function M.getSystemKey(key) function M.getSystemKey(key)
if type(key) ~= 'string' or key:len() == 0 then return nil end if type(key) ~= 'string' or key:len() == 0 then return nil end
local v,msg = uci:get(UCI_CONFIG_NAME, UCI_CONFIG_SYSTEM_SECTION, key) local v,msg = uci:get(UCI_CONFIG_NAME, UCI_CONFIG_SYSTEM_SECTION, key)
@ -377,7 +379,7 @@ function M.getSystemKey(key)
local errorMSG = "Issue getting system setting '"..utils.dump(key).."' in section '"..UCI_CONFIG_SYSTEM_SECTION.."': "..utils.dump(msg); local errorMSG = "Issue getting system setting '"..utils.dump(key).."' in section '"..UCI_CONFIG_SYSTEM_SECTION.."': "..utils.dump(msg);
return nil, errorMSG; return nil, errorMSG;
end end
return v or false return v or false
end end