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.

174 lines
5.0 KiB

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