base-files: gpio switch: check if direction can be set
[openwrt/openwrt.git] / package / base-files / files / etc / init.d / gpio_switch
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2015 OpenWrt.org
3
4 START=94
5 STOP=10
6 USE_PROCD=1
7
8
9 load_gpio_switch()
10 {
11 local name
12 local gpio_pin
13 local value
14
15 config_get gpio_pin "$1" gpio_pin
16 config_get name "$1" name
17 config_get value "$1" value 0
18
19 local gpio_path="/sys/class/gpio/gpio${gpio_pin}"
20 # export GPIO pin for access
21 [ -d "$gpio_path" ] || {
22 echo "$gpio_pin" >/sys/class/gpio/export
23 # we need to wait a bit until the GPIO appears
24 [ -d "$gpio_path" ] || sleep 1
25 }
26
27 # direction attribute only exists if the kernel supports changing the
28 # direction of a GPIO
29 if [ -e "${gpio_path}/direction" ]; then
30 # set the pin to output with high or low pin value
31 { [ "$value" = "0" ] && echo "low" || echo "high"; } >"$gpio_path/direction"
32 else
33 { [ "$value" = "0" ] && echo "0" || echo "1"; } >"$gpio_path/value"
34 fi
35 }
36
37 service_triggers()
38 {
39 procd_add_reload_trigger "system"
40 }
41
42 start_service()
43 {
44 [ -e /sys/class/gpio/ ] && {
45 config_load system
46 config_foreach load_gpio_switch gpio_switch
47 }
48 }