luci-app-ushare: remove from repo
[project/luci.git] / applications / luci-app-diag-devinfo / luasrc / controller / luci_diag / netdiscover_common.lua
1 -- Copyright 2009 Daniel Dickinson
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.luci_diag.netdiscover_common", package.seeall)
5
6 require("luci.i18n")
7 require("luci.util")
8 require("luci.sys")
9 require("luci.cbi")
10 require("luci.model.uci")
11
12 local translate = luci.i18n.translate
13 local DummyValue = luci.cbi.DummyValue
14 local SimpleSection = luci.cbi.SimpleSection
15
16 function index()
17         return -- no-op
18 end
19          
20 function get_params()
21
22    local netdiscover_uci = luci.model.uci.cursor()
23    netdiscover_uci:load("luci_devinfo")
24    local nettable = netdiscover_uci:get_all("luci_devinfo")
25
26    local i 
27    local subnet
28    local netdout
29
30    local outnets = {}
31
32    i = next(nettable, nil)
33
34    while (i) do
35       if (netdiscover_uci:get("luci_devinfo", i) == "netdiscover_scannet") then 
36          local scannet = netdiscover_uci:get_all("luci_devinfo", i)
37          if scannet["subnet"] and (scannet["subnet"] ~= "") and scannet["enable"] and ( scannet["enable"] == "1") then
38             local output = ""
39             local outrow = {}
40             outrow["interface"] = scannet["interface"]
41             outrow["timeout"] = 10
42             local timeout = tonumber(scannet["timeout"]) 
43             if timeout and ( timeout > 0 ) then
44                outrow["timeout"] = scannet["timeout"]
45             end
46
47             outrow["repeat_count"] = 1
48             local repcount = tonumber(scannet["repeat_count"]) 
49             if repcount and ( repcount > 0 ) then
50                outrow["repeat_count"] = scannet["repeat_count"]
51             end
52
53             outrow["sleepreq"] = 100
54             local repcount = tonumber(scannet["sleepreq"]) 
55             if repcount and ( repcount > 0 ) then
56                outrow["sleepreq"] = scannet["sleepreq"]
57             end
58
59             outrow["subnet"] = scannet["subnet"]    
60             outrow["output"] = output
61             outnets[i] = outrow
62          end
63       end
64       i = next(nettable, i)
65    end
66    return outnets
67 end
68
69 function command_function(outnets, i)
70    local interface = luci.controller.luci_diag.devinfo_common.get_network_device(outnets[i]["interface"])
71    
72    return "/usr/bin/netdiscover-to-devinfo " .. outnets[i]["subnet"] .. " " .. interface .. " " .. outnets[i]["timeout"] .. " -r " .. outnets[i]["repeat_count"] .. " -s " .. outnets[i]["sleepreq"] .. " </dev/null"
73 end
74
75 function action_links(netdiscovermap, mini) 
76    s = netdiscovermap:section(SimpleSection, "", translate("Actions")) 
77    b = s:option(DummyValue, "_config", translate("Configure Scans"))
78    b.value = ""
79    if (mini) then
80       b.titleref = luci.dispatcher.build_url("mini", "network", "netdiscover_devinfo_config")
81    else
82       b.titleref = luci.dispatcher.build_url("admin", "network", "diag_config", "netdiscover_devinfo_config")
83    end
84    b = s:option(DummyValue, "_scans", translate("Repeat Scans (this can take a few minutes)"))
85    b.value = ""
86    if (mini) then
87       b.titleref = luci.dispatcher.build_url("mini", "diag", "netdiscover_devinfo")
88    else
89       b.titleref = luci.dispatcher.build_url("admin", "status", "netdiscover_devinfo")
90    end
91 end