luci-app-adblock: sync with adblock 3.5.0
[project/luci.git] / applications / luci-app-diag-devinfo / luasrc / controller / luci_diag / devinfo_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.devinfo_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 run_processes(outnets, cmdfunc)
21    i = next(outnets, nil)
22    while (i) do
23       outnets[i]["output"] = luci.sys.exec(cmdfunc(outnets, i))
24       i = next(outnets, i)
25    end
26 end
27
28 function parse_output(devmap, outnets, haslink, type, mini, debug)
29    local curnet = next(outnets, nil)
30
31    while (curnet) do
32       local output = outnets[curnet]["output"]
33       local subnet = outnets[curnet]["subnet"]
34       local ports = outnets[curnet]["ports"]
35       local interface = outnets[curnet]["interface"]
36       local netdevs = {}
37       devlines = luci.util.split(output)
38       if not devlines then
39          devlines = {}
40          table.insert(devlines, output)
41       end
42             
43       local j = nil
44       j = next(devlines, j)
45       
46       local found_a_device = false
47       
48       while (j) do
49          if devlines[j] and ( devlines[j] ~= "" ) then
50             found_a_device = true
51             local devtable
52             local row = {}
53             devtable = luci.util.split(devlines[j], ' | ')
54             row["ip"] = devtable[1]
55             if (not mini) then
56                row["mac"] = devtable[2]
57             end
58             if ( devtable[4] == 'unknown' ) then 
59                row["vendor"] = devtable[3]
60             else
61                row["vendor"] = devtable[4]
62             end
63             row["type"] = devtable[5]
64             if (not mini) then
65                row["model"] = devtable[6]
66             end
67             if (haslink) then
68                row["config_page"] = devtable[7]
69             end
70             
71             if (debug) then
72                row["raw"] = devlines[j]
73             end
74             table.insert(netdevs, row)
75          end
76          j = next(devlines, j)
77       end
78       if not found_a_device then
79          local row = {}
80          row["ip"] = curnet
81          if (not mini) then
82             row["mac"] = ""
83          end
84          if (type == "smap") then
85             row["vendor"] = luci.i18n.translate("No SIP devices")
86          else
87             row["vendor"] = luci.i18n.translate("No devices detected")
88          end
89          row["type"] = luci.i18n.translate("check other networks")
90          if (not mini) then
91             row["model"] = ""
92          end
93          if (haslink) then
94             row["config_page"] = ""
95          end
96          if (debug) then
97             row["raw"] = output
98          end
99          table.insert(netdevs, row)
100       end        
101       local s
102       if (type == "smap") then
103          if (mini) then
104             s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("SIP devices discovered for") .. " " .. curnet)
105          else
106             local interfacestring = ""
107             if ( interface ~= "" ) then
108                interfacestring = ", " .. interface
109             end
110             s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("SIP devices discovered for") .. " " .. curnet .. " (" .. subnet .. ":" .. ports .. interfacestring .. ")")
111          end
112          s.template = "diag/smapsection"
113       else
114          if (mini) then
115             s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("Devices discovered for") .. " " .. curnet)
116          else
117             local interfacestring = ""
118             if ( interface ~= "" ) then
119                interfacestring = ", " .. interface
120             end
121             s = devmap:section(luci.cbi.Table, netdevs, luci.i18n.translate("Devices discovered for") .. " " .. curnet .. " (" .. subnet .. interfacestring .. ")")
122          end
123       end
124       s:option(DummyValue, "ip", translate("IP Address"))
125       if (not mini) then
126          s:option(DummyValue, "mac", translate("MAC Address"))
127       end
128       s:option(DummyValue, "vendor", translate("Vendor"))
129       s:option(DummyValue, "type", translate("Device Type"))
130       if (not mini) then
131          s:option(DummyValue, "model", translate("Model"))
132       end
133       if (haslink) then
134          s:option(DummyValue, "config_page", translate("Link to Device"))
135       end
136       if (debug) then
137          s:option(DummyValue, "raw", translate("Raw"))
138       end
139       curnet = next(outnets, curnet)
140    end
141 end
142
143 function get_network_device(interface)
144    local state = luci.model.uci.cursor_state()
145    state:load("network")
146    local dev
147    
148    return state:get("network", interface, "ifname")
149 end
150
151
152 function cbi_add_networks(field)
153         uci.cursor():foreach("network", "interface",
154                 function (section)
155                         if section[".name"] ~= "loopback" then
156                                 field:value(section[".name"])
157                         end
158                 end
159         )
160         field.titleref = luci.dispatcher.build_url("admin", "network", "network")
161 end
162
163 function config_devinfo_scan(map, scannet)
164    local o
165    o = scannet:option(luci.cbi.Flag, "enable", translate("Enable"))
166    o.optional = false
167    o.rmempty = false
168
169    o = scannet:option(luci.cbi.Value, "interface", translate("Interface"))
170    o.optional = false
171    luci.controller.luci_diag.devinfo_common.cbi_add_networks(o)
172    
173    local scansubnet
174    scansubnet = scannet:option(luci.cbi.Value, "subnet", translate("Subnet"))
175    scansubnet.optional = false
176    
177    o = scannet:option(luci.cbi.Value, "timeout", translate("Timeout"), translate("Time to wait for responses in seconds (default 10)"))
178    o.optional = true
179    
180    o = scannet:option(luci.cbi.Value, "repeat_count", translate("Repeat Count"), translate("Number of times to send requests (default 1)"))
181    o.optional = true
182    
183    o = scannet:option(luci.cbi.Value, "sleepreq", translate("Sleep Between Requests"), translate("Milliseconds to sleep between requests (default 100)"))
184    o.optional = true
185 end