1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 m = Map("system", translate("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), translate("Customizes the behaviour of the device <abbr title=\"Light Emitting Diode\">LED</abbr>s if possible."))
6 local sysfs_path = "/sys/class/leds/"
9 local fs = require "nixio.fs"
10 local nu = require "nixio.util"
11 local util = require "luci.util"
13 if fs.access(sysfs_path) then
14 leds = nu.consume((fs.dir(sysfs_path)))
22 s = m:section(TypedSection, "led", "")
26 function s.parse(self, ...)
27 TypedSection.parse(self, ...)
28 os.execute("/etc/init.d/led enable")
32 s:option(Value, "name", translate("Name"))
35 sysfs = s:option(ListValue, "sysfs", translate("<abbr title=\"Light Emitting Diode\">LED</abbr> Name"))
36 for k, v in ipairs(leds) do
40 s:option(Flag, "default", translate("Default state")).rmempty = false
43 trigger = s:option(ListValue, "trigger", translate("Trigger"))
45 local triggers = fs.readfile(sysfs_path .. leds[1] .. "/trigger")
46 for t in triggers:gmatch("[%w-]+") do
47 trigger:value(t, translate(t:gsub("-", "")))
51 delayon = s:option(Value, "delayon", translate ("On-State Delay"))
52 delayon:depends("trigger", "timer")
54 delayoff = s:option(Value, "delayoff", translate ("Off-State Delay"))
55 delayoff:depends("trigger", "timer")
58 dev = s:option(ListValue, "_net_dev", translate("Device"))
61 dev:depends("trigger", "netdev")
63 function dev.cfgvalue(self, section)
64 return m.uci:get("system", section, "dev")
67 function dev.write(self, section, value)
68 m.uci:set("system", section, "dev", value)
71 function dev.remove(self, section)
72 local t = trigger:formvalue(section)
73 if t ~= "netdev" and t ~= "usbdev" then
74 m.uci:delete("system", section, "dev")
78 for k, v in pairs(luci.sys.net.devices()) do
85 mode = s:option(MultiValue, "mode", translate("Trigger Mode"))
87 mode:depends("trigger", "netdev")
88 mode:value("link", translate("Link On"))
89 mode:value("tx", translate("Transmit"))
90 mode:value("rx", translate("Receive"))
93 usbdev = s:option(ListValue, "_usb_dev", translate("USB Device"))
94 usbdev:depends("trigger", "usbdev")
98 function usbdev.cfgvalue(self, section)
99 return m.uci:get("system", section, "dev")
102 function usbdev.write(self, section, value)
103 m.uci:set("system", section, "dev", value)
106 function usbdev.remove(self, section)
107 local t = trigger:formvalue(section)
108 if t ~= "netdev" and t ~= "usbdev" then
109 m.uci:delete("system", section, "dev")
114 usbport = s:option(MultiValue, "port", translate("USB Ports"))
115 usbport:depends("trigger", "usbport")
116 usbport.rmempty = true
117 usbport.widget = "checkbox"
118 usbport.cast = "table"
121 function usbport.valuelist(self, section)
122 local port, ports = nil, {}
123 for port in util.imatch(m.uci:get("system", section, "port")) do
124 local b, n = port:match("^usb(%d+)-port(%d+)$")
125 if not (b and n) then
126 b, n = port:match("^(%d+)-(%d+)$")
129 ports[#ports+1] = "usb%u-port%u" %{ tonumber(b), tonumber(n) }
135 function usbport.validate(self, value)
136 return type(value) == "string" and { value } or value
140 for p in nixio.fs.glob("/sys/bus/usb/devices/[0-9]*/manufacturer") do
141 local id = p:match("%d+-%d+")
142 local mf = nixio.fs.readfile("/sys/bus/usb/devices/" .. id .. "/manufacturer") or "?"
143 local pr = nixio.fs.readfile("/sys/bus/usb/devices/" .. id .. "/product") or "?"
144 usbdev:value(id, "%s (%s - %s)" %{ id, mf, pr })
147 for p in nixio.fs.glob("/sys/bus/usb/devices/*/usb[0-9]*-port[0-9]*") do
148 local bus, port = p:match("usb(%d+)-port(%d+)")
150 usbport:value("usb%u-port%u" %{ tonumber(bus), tonumber(port) },
151 "Hub %u, Port %u" %{ tonumber(bus), tonumber(port) })