Rework LuCI build system
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_system / leds.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
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 $Id$
13 ]]--
14 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."))
15
16 local sysfs_path = "/sys/class/leds/"
17 local leds = {}
18
19 local fs = require "nixio.fs"
20 local util = require "nixio.util"
21
22 if fs.access(sysfs_path) then
23 leds = util.consume((fs.dir(sysfs_path)))
24 end
25
26 if #leds == 0 then
27 return m
28 end
29
30
31 s = m:section(TypedSection, "led", "")
32 s.anonymous = true
33 s.addremove = true
34
35 function s.parse(self, ...)
36 TypedSection.parse(self, ...)
37 os.execute("/etc/init.d/led enable")
38 end
39
40
41 s:option(Value, "name", translate("Name"))
42
43
44 sysfs = s:option(ListValue, "sysfs", translate("<abbr title=\"Light Emitting Diode\">LED</abbr> Name"))
45 for k, v in ipairs(leds) do
46 sysfs:value(v)
47 end
48
49 s:option(Flag, "default", translate("Default state")).rmempty = false
50
51
52 trigger = s:option(ListValue, "trigger", translate("Trigger"))
53
54 local triggers = fs.readfile(sysfs_path .. leds[1] .. "/trigger")
55 for t in triggers:gmatch("[%w-]+") do
56 trigger:value(t, translate(t:gsub("-", "")))
57 end
58
59
60 delayon = s:option(Value, "delayon", translate ("On-State Delay"))
61 delayon:depends("trigger", "timer")
62
63 delayoff = s:option(Value, "delayoff", translate ("Off-State Delay"))
64 delayoff:depends("trigger", "timer")
65
66
67 dev = s:option(ListValue, "_net_dev", translate("Device"))
68 dev.rmempty = true
69 dev:value("")
70 dev:depends("trigger", "netdev")
71
72 function dev.cfgvalue(self, section)
73 return m.uci:get("system", section, "dev")
74 end
75
76 function dev.write(self, section, value)
77 m.uci:set("system", section, "dev", value)
78 end
79
80 function dev.remove(self, section)
81 local t = trigger:formvalue(section)
82 if t ~= "netdev" and t ~= "usbdev" then
83 m.uci:delete("system", section, "dev")
84 end
85 end
86
87 for k, v in pairs(luci.sys.net.devices()) do
88 if v ~= "lo" then
89 dev:value(v)
90 end
91 end
92
93
94 mode = s:option(MultiValue, "mode", translate("Trigger Mode"))
95 mode.rmempty = true
96 mode:depends("trigger", "netdev")
97 mode:value("link", translate("Link On"))
98 mode:value("tx", translate("Transmit"))
99 mode:value("rx", translate("Receive"))
100
101
102 usbdev = s:option(ListValue, "_usb_dev", translate("USB Device"))
103 usbdev:depends("trigger", "usbdev")
104 usbdev.rmempty = true
105 usbdev:value("")
106
107 function usbdev.cfgvalue(self, section)
108 return m.uci:get("system", section, "dev")
109 end
110
111 function usbdev.write(self, section, value)
112 m.uci:set("system", section, "dev", value)
113 end
114
115 function usbdev.remove(self, section)
116 local t = trigger:formvalue(section)
117 if t ~= "netdev" and t ~= "usbdev" then
118 m.uci:delete("system", section, "dev")
119 end
120 end
121
122 for p in nixio.fs.glob("/sys/bus/usb/devices/[0-9]*/manufacturer") do
123 local id = p:match("%d+-%d+")
124 local mf = nixio.fs.readfile("/sys/bus/usb/devices/" .. id .. "/manufacturer") or "?"
125 local pr = nixio.fs.readfile("/sys/bus/usb/devices/" .. id .. "/product") or "?"
126 usbdev:value(id, "%s (%s - %s)" %{ id, mf, pr })
127 end
128
129 return m