leds.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. 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."))
  4. local sysfs_path = "/sys/class/leds/"
  5. local leds = {}
  6. local fs = require "nixio.fs"
  7. local util = require "nixio.util"
  8. if fs.access(sysfs_path) then
  9. leds = util.consume((fs.dir(sysfs_path)))
  10. end
  11. if #leds == 0 then
  12. return m
  13. end
  14. s = m:section(TypedSection, "led", "")
  15. s.anonymous = true
  16. s.addremove = true
  17. function s.parse(self, ...)
  18. TypedSection.parse(self, ...)
  19. os.execute("/etc/init.d/led enable")
  20. end
  21. s:option(Value, "name", translate("Name"))
  22. sysfs = s:option(ListValue, "sysfs", translate("<abbr title=\"Light Emitting Diode\">LED</abbr> Name"))
  23. for k, v in ipairs(leds) do
  24. sysfs:value(v)
  25. end
  26. s:option(Flag, "default", translate("Default state")).rmempty = false
  27. trigger = s:option(ListValue, "trigger", translate("Trigger"))
  28. local triggers = fs.readfile(sysfs_path .. leds[1] .. "/trigger")
  29. for t in triggers:gmatch("[%w-]+") do
  30. trigger:value(t, translate(t:gsub("-", "")))
  31. end
  32. delayon = s:option(Value, "delayon", translate ("On-State Delay"))
  33. delayon:depends("trigger", "timer")
  34. delayoff = s:option(Value, "delayoff", translate ("Off-State Delay"))
  35. delayoff:depends("trigger", "timer")
  36. dev = s:option(ListValue, "_net_dev", translate("Device"))
  37. dev.rmempty = true
  38. dev:value("")
  39. dev:depends("trigger", "netdev")
  40. function dev.cfgvalue(self, section)
  41. return m.uci:get("system", section, "dev")
  42. end
  43. function dev.write(self, section, value)
  44. m.uci:set("system", section, "dev", value)
  45. end
  46. function dev.remove(self, section)
  47. local t = trigger:formvalue(section)
  48. if t ~= "netdev" and t ~= "usbdev" then
  49. m.uci:delete("system", section, "dev")
  50. end
  51. end
  52. for k, v in pairs(luci.sys.net.devices()) do
  53. if v ~= "lo" then
  54. dev:value(v)
  55. end
  56. end
  57. mode = s:option(MultiValue, "mode", translate("Trigger Mode"))
  58. mode.rmempty = true
  59. mode:depends("trigger", "netdev")
  60. mode:value("link", translate("Link On"))
  61. mode:value("tx", translate("Transmit"))
  62. mode:value("rx", translate("Receive"))
  63. usbdev = s:option(ListValue, "_usb_dev", translate("USB Device"))
  64. usbdev:depends("trigger", "usbdev")
  65. usbdev.rmempty = true
  66. usbdev:value("")
  67. function usbdev.cfgvalue(self, section)
  68. return m.uci:get("system", section, "dev")
  69. end
  70. function usbdev.write(self, section, value)
  71. m.uci:set("system", section, "dev", value)
  72. end
  73. function usbdev.remove(self, section)
  74. local t = trigger:formvalue(section)
  75. if t ~= "netdev" and t ~= "usbdev" then
  76. m.uci:delete("system", section, "dev")
  77. end
  78. end
  79. for p in nixio.fs.glob("/sys/bus/usb/devices/[0-9]*/manufacturer") do
  80. local id = p:match("%d+-%d+")
  81. local mf = nixio.fs.readfile("/sys/bus/usb/devices/" .. id .. "/manufacturer") or "?"
  82. local pr = nixio.fs.readfile("/sys/bus/usb/devices/" .. id .. "/product") or "?"
  83. usbdev:value(id, "%s (%s - %s)" %{ id, mf, pr })
  84. end
  85. return m