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