You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.9 KiB

  1. # /bin/sh
  2. setled() {
  3. if [ -e /sys/class/leds/${led_sysfs}/brightness ]
  4. then
  5. [ $led_default -eq 1 ] && echo 1 >/sys/class/leds/${led_sysfs}/brightness
  6. [ $led_default -eq 1 ] || echo 0 >/sys/class/leds/${led_sysfs}/brightness
  7. echo $led_trigger > /sys/class/leds/${led_sysfs}/trigger
  8. [ "$led_trigger" == "netdev" -a -n "$led_dev" ] && {
  9. echo $led_dev > /sys/class/leds/${led_sysfs}/device_name
  10. echo $led_mode > /sys/class/leds/${led_sysfs}/mode
  11. }
  12. [ "$led_trigger" == "timer" ] && {
  13. [ -n "$led_delayon" ] && echo $led_delayon > /sys/class/leds/${led_sysfs}/delay_on
  14. [ -n "$led_delayoff" ] && echo $led_delayoff > /sys/class/leds/${led_sysfs}/delay_off
  15. }
  16. else
  17. logger -p user.err -t "wifitoggle" "led: ${led_sysfs} not found"
  18. fi
  19. }
  20. load_wireless() {
  21. disabled="$(uci get wireless.${1}.disabled)"
  22. }
  23. save_wireless() {
  24. [ "$device" = "all" -o "$device" = "${1}" ] && {
  25. uci set "wireless.${1}.disabled=$disabled"
  26. }
  27. }
  28. setwifi() {
  29. setled
  30. config_load wireless
  31. config_foreach save_wireless wifi-device
  32. if ubus list network.wireless >/dev/null 2>/dev/null; then
  33. ubus call network reload
  34. else
  35. wifi
  36. fi
  37. }
  38. save_system() {
  39. config_get sysfs $1 sysfs
  40. [ "$sysfs" = "$led_sysfs" ] && {
  41. uci set "system.${1}.trigger=$led_trigger"
  42. uci set "system.${1}.dev=$led_dev"
  43. uci set "system.${1}.mode=$led_mode"
  44. uci set "system.${1}.default=$led_default"
  45. uci set "system.${1}.delayon=$led_delayon"
  46. uci set "system.${1}.delayoff=$led_delayoff"
  47. ledset=1
  48. }
  49. }
  50. commit_changes() {
  51. ledset=""
  52. config_load system
  53. config_foreach save_system led
  54. [ "$ledset" -eq 1 ] || {
  55. section=$(uci add system led)
  56. uci set "system.${section}.sysfs=$led_sysfs"
  57. uci set "system.${section}.trigger=$led_trigger"
  58. uci set "system.${section}.dev=$led_dev"
  59. uci set "system.${section}.mode=$led_mode"
  60. uci set "system.${section}.default=$led_default"
  61. uci set "system.${section}.delayon=$led_delayon"
  62. uci set "system.${section}.delayoff=$led_delayoff"
  63. }
  64. uci commit
  65. }
  66. enable() {
  67. led_trigger="$led_enable_trigger"
  68. led_trigger="$led_enable_trigger"
  69. led_dev="$led_enable_dev"
  70. led_mode="$led_enable_mode"
  71. led_default="$led_enable_default"
  72. led_delayon="$led_enable_delayon"
  73. led_delayoff="$led_enable_delayoff"
  74. disabled=0
  75. setwifi
  76. [ "$timer" -le 0 -a "$persistent" -eq 1 ] && commit_changes
  77. logger -p user.info -t "wifitoggle" "device($device) enabled"
  78. [ "$timer" -gt 0 ] && {
  79. sleep "$timer" && disable && rm /tmp/run/wirelesstoggle_${1}.pid &
  80. echo $! > /tmp/run/wirelesstoggle_${1}.pid
  81. logger -p user.info -t "wifitoggle" "auto-disable timer started($timer seconds)"
  82. }
  83. }
  84. disable() {
  85. led_trigger="$led_disable_trigger"
  86. led_trigger="$led_disable_trigger"
  87. led_dev="$led_disable_dev"
  88. led_mode="$led_disable_mode"
  89. led_default="$led_disable_default"
  90. led_delayon="$led_disable_delayon"
  91. led_delayoff="$led_disable_delayoff"
  92. disabled=1
  93. setwifi
  94. [ "$timer" -le 0 -a "$persistent" -eq 1 ] && commit_changes
  95. logger -p user.info -t "wifitoggle" "device($device) disabled"
  96. }
  97. load_wifitoggle() {
  98. config_get device $1 device "all"
  99. config_get button $1 button "wps"
  100. config_get action $1 action "pressed"
  101. config_get_bool persistent $1 persistent "0"
  102. config_get timer $1 timer "0"
  103. config_get led_sysfs $1 led_sysfs
  104. config_get led_enable_trigger $1 led_enable_trigger "none"
  105. config_get led_enable_dev $1 led_enable_dev
  106. config_get led_enable_mode $1 led_enable_mode "link"
  107. config_get_bool led_enable_default $1 led_enable_default "1"
  108. config_get led_enable_delayon $1 led_enable_delayon
  109. config_get led_enable_delayoff $1 led_enable_delayoff
  110. config_get led_disable_trigger $1 led_disable_trigger "none"
  111. config_get led_disable_dev $1 led_disable_dev
  112. config_get led_disable_mode $1 led_disable_mode "link"
  113. config_get_bool led_disable_default $1 led_disable_default "0"
  114. config_get led_disable_delayon $1 led_disable_delayon
  115. config_get led_disable_delayoff $1 led_disable_delayoff
  116. [ "$ACTION" = "$action" -a "$BUTTON" = "$button" ] && {
  117. [ -f /tmp/run/wirelesstoggle_${1}.pid ] && read PID < /tmp/run/wirelesstoggle_${1}.pid && kill $PID && rm /tmp/run/wirelesstoggle_${1}.pid
  118. if [ "$device" = "all" ]
  119. then
  120. config_load wireless
  121. config_foreach load_wireless wifi-device
  122. else
  123. disabled="$(uci get wireless."$device".disabled)"
  124. fi
  125. if [ "$disabled" = "1" ]
  126. then
  127. enable $1
  128. else
  129. disable
  130. fi
  131. }
  132. }
  133. config_load wifitoggle
  134. config_foreach load_wifitoggle wifitoggle