22273ef6aeaad0242558dd6fd197fe535decbb6d
[project/usteer.git] / openwrt / usteer / files / etc / init.d / usteer
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2013 OpenWrt.org
3
4 START=50
5 USE_PROCD=1
6
7 NAME=usteer
8 PROG=/sbin/usteerd
9
10 . /lib/functions/network.sh
11 . /usr/share/libubox/jshn.sh
12 . /lib/functions.sh
13
14 load_ifaces() {
15 local network="$(uci get usteer.@usteer[-1].network)"
16 for n in $network; do
17 local device
18 json_load "$(ifstatus $n)"
19 json_get_var device l3_device
20 echo -n "$device "
21 done
22 }
23
24 _add_string() {
25 json_add_string "" "$1"
26 }
27
28 uci_option_to_json_string_array() {
29 local cfg="$1"
30 local option="$2"
31
32 json_add_array "$option"
33 config_list_foreach "$cfg" "$option" _add_string
34 json_close_array
35 }
36
37 uci_option_to_json_bool() {
38 local cfg="$1"
39 local option="$2"
40 local val
41
42 config_get_bool val "$cfg" $option
43 [ -n "$val" ] && json_add_boolean $option $val
44 }
45
46 uci_option_to_json_string() {
47 local cfg="$1"
48 local option="$2"
49 local val
50
51 config_get val "$cfg" "$option"
52 [ -n "$val" ] && json_add_string $option "$val"
53 }
54
55 uci_option_to_json() {
56 local cfg="$1"
57 local option="$2"
58 local val
59
60 config_get val "$cfg" $option
61 [ -n "$val" ] && json_add_int $option $val
62 }
63
64 uci_usteer() {
65 local cfg="$1"
66
67 uci_option_to_json_bool "$cfg" syslog
68 uci_option_to_json_bool "$cfg" ipv6
69 uci_option_to_json_bool "$cfg" local_mode
70 uci_option_to_json_bool "$cfg" load_kick_enabled
71 uci_option_to_json_bool "$cfg" assoc_steering
72 uci_option_to_json_string "$cfg" node_up_script
73 uci_option_to_json_string_array "$cfg" ssid_list
74 uci_option_to_json_string_array "$cfg" event_log_types
75
76 for opt in \
77 debug_level \
78 sta_block_timeout local_sta_timeout local_sta_update \
79 max_neighbor_reports max_retry_band seen_policy_timeout \
80 measurement_report_timeout \
81 load_balancing_threshold band_steering_threshold \
82 remote_update_interval remote_node_timeout\
83 min_connect_snr min_snr min_snr_kick_delay signal_diff_threshold \
84 initial_connect_delay roam_process_timeout\
85 roam_kick_delay roam_scan_tries roam_scan_timeout \
86 roam_scan_snr roam_scan_interval \
87 roam_trigger_snr roam_trigger_interval \
88 load_kick_threshold load_kick_delay load_kick_min_clients \
89 load_kick_reason_code
90 do
91 uci_option_to_json "$cfg" "$opt"
92 done
93 }
94
95
96 load_config() {
97 [ "$ENABLED" -gt 0 ] || return
98
99 ubus -t 10 wait_for usteer
100
101 json_init
102 json_add_array interfaces
103 for i in $(load_ifaces); do
104 json_add_string "" "$i"
105 done
106 json_close_array
107
108 config_load usteer
109 config_foreach uci_usteer usteer
110
111 ubus call usteer set_config "$(json_dump)"
112 }
113
114 reload_service() {
115 start
116 load_config
117 }
118
119 service_started() {
120 load_config
121 }
122
123 service_triggers() {
124 procd_add_reload_trigger usteer
125 procd_add_raw_trigger "interface.*" 2000 /etc/init.d/usteer reload
126 }
127
128 start_service()
129 {
130 local network="$(uci -q get usteer.@usteer[-1].network)"
131 ENABLED="$(uci -q get usteer.@usteer[-1].enabled)"
132 ENABLED="${ENABLED:-1}"
133
134 [ "$ENABLED" -gt 0 ] || return
135
136 procd_open_instance
137 procd_set_param command "$PROG"
138 procd_close_instance
139 }