leds.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 nu = require "nixio.util"
  8. local util = require "luci.util"
  9. if fs.access(sysfs_path) then
  10. leds = nu.consume((fs.dir(sysfs_path)))
  11. end
  12. if #leds == 0 then
  13. return m
  14. end
  15. s = m:section(TypedSection, "led", "")
  16. s.anonymous = true
  17. s.addremove = true
  18. function s.parse(self, ...)
  19. TypedSection.parse(self, ...)
  20. os.execute("/etc/init.d/led enable")
  21. end
  22. s:option(Value, "name", translate("Name"))
  23. sysfs = s:option(ListValue, "sysfs", translate("<abbr title=\"Light Emitting Diode\">LED</abbr> Name"))
  24. for k, v in ipairs(leds) do
  25. sysfs:value(v)
  26. end
  27. s:option(Flag, "default", translate("Default state")).rmempty = false
  28. trigger = s:option(ListValue, "trigger", translate("Trigger"))
  29. local triggers = fs.readfile(sysfs_path .. leds[1] .. "/trigger")
  30. for t in triggers:gmatch("[%w-]+") do
  31. trigger:value(t, translate(t:gsub("-", "")))
  32. end
  33. delayon = s:option(Value, "delayon", translate ("On-State Delay"))
  34. delayon:depends("trigger", "timer")
  35. delayoff = s:option(Value, "delayoff", translate ("Off-State Delay"))
  36. delayoff:depends("trigger", "timer")
  37. dev = s:option(ListValue, "_net_dev", translate("Device"))
  38. dev.rmempty = true
  39. dev:value("")
  40. dev:depends("trigger", "netdev")
  41. function dev.cfgvalue(self, section)
  42. return m.uci:get("system", section, "dev")
  43. end
  44. function dev.write(self, section, value)
  45. m.uci:set("system", section, "dev", value)
  46. end
  47. function dev.remove(self, section)
  48. local t = trigger:formvalue(section)
  49. if t ~= "netdev" and t ~= "usbdev" then
  50. m.uci:delete("system", section, "dev")
  51. end
  52. end
  53. for k, v in pairs(luci.sys.net.devices()) do
  54. if v ~= "lo" then
  55. dev:value(v)
  56. end
  57. end
  58. mode = s:option(MultiValue, "mode", translate("Trigger Mode"))
  59. mode.rmempty = true
  60. mode:depends("trigger", "netdev")
  61. mode:value("link", translate("Link On"))
  62. mode:value("tx", translate("Transmit"))
  63. mode:value("rx", translate("Receive"))
  64. usbdev = s:option(ListValue, "_usb_dev", translate("USB Device"))
  65. usbdev:depends("trigger", "usbdev")
  66. usbdev.rmempty = true
  67. usbdev:value("")
  68. function usbdev.cfgvalue(self, section)
  69. return m.uci:get("system", section, "dev")
  70. end
  71. function usbdev.write(self, section, value)
  72. m.uci:set("system", section, "dev", value)
  73. end
  74. function usbdev.remove(self, section)
  75. local t = trigger:formvalue(section)
  76. if t ~= "netdev" and t ~= "usbdev" then
  77. m.uci:delete("system", section, "dev")
  78. end
  79. end
  80. usbport = s:option(MultiValue, "port", translate("USB Ports"))
  81. usbport:depends("trigger", "usbport")
  82. usbport.rmempty = true
  83. usbport.widget = "checkbox"
  84. usbport.cast = "table"
  85. usbport.size = 1
  86. function usbport.valuelist(self, section)
  87. local port, ports = nil, {}
  88. for port in util.imatch(m.uci:get("system", section, "port")) do
  89. local b, n = port:match("^usb(%d+)-port(%d+)$")
  90. if not (b and n) then
  91. b, n = port:match("^(%d+)-(%d+)$")
  92. end
  93. if b and n then
  94. ports[#ports+1] = "usb%u-port%u" %{ tonumber(b), tonumber(n) }
  95. end
  96. end
  97. return ports
  98. end
  99. function usbport.validate(self, value)
  100. return type(value) == "string" and { value } or value
  101. end
  102. for p in nixio.fs.glob("/sys/bus/usb/devices/[0-9]*/manufacturer") do
  103. local id = p:match("%d+-%d+")
  104. local mf = nixio.fs.readfile("/sys/bus/usb/devices/" .. id .. "/manufacturer") or "?"
  105. local pr = nixio.fs.readfile("/sys/bus/usb/devices/" .. id .. "/product") or "?"
  106. usbdev:value(id, "%s (%s - %s)" %{ id, mf, pr })
  107. end
  108. for p in nixio.fs.glob("/sys/bus/usb/devices/*/usb[0-9]*-port[0-9]*") do
  109. local bus, port = p:match("usb(%d+)-port(%d+)")
  110. if bus and port then
  111. usbport:value("usb%u-port%u" %{ tonumber(bus), tonumber(port) },
  112. "Hub %u, Port %u" %{ tonumber(bus), tonumber(port) })
  113. end
  114. end
  115. return m