base-files: allow setting ide-disk led trigger
[openwrt/openwrt.git] / package / base-files / files / lib / functions / uci-defaults.sh
1 #!/bin/sh
2 # Copyright (C) 2011 OpenWrt.org
3
4 UCIDEF_LEDS_CHANGED=0
5 UCIDEF_GPIO_SWITCHES_CHANGED=0
6
7 ucidef_set_led_netdev() {
8 local cfg="led_$1"
9 local name=$2
10 local sysfs=$3
11 local dev=$4
12
13 uci -q get system.$cfg && return 0
14
15 uci batch <<EOF
16 set system.$cfg='led'
17 set system.$cfg.name='$name'
18 set system.$cfg.sysfs='$sysfs'
19 set system.$cfg.trigger='netdev'
20 set system.$cfg.dev='$dev'
21 set system.$cfg.mode='link tx rx'
22 EOF
23 UCIDEF_LEDS_CHANGED=1
24 }
25
26 ucidef_set_led_usbdev() {
27 local cfg="led_$1"
28 local name=$2
29 local sysfs=$3
30 local dev=$4
31
32 uci -q get system.$cfg && return 0
33
34 uci batch <<EOF
35 set system.$cfg='led'
36 set system.$cfg.name='$name'
37 set system.$cfg.sysfs='$sysfs'
38 set system.$cfg.trigger='usbdev'
39 set system.$cfg.dev='$dev'
40 set system.$cfg.interval='50'
41 EOF
42 UCIDEF_LEDS_CHANGED=1
43 }
44
45 ucidef_set_led_wlan() {
46 local cfg="led_$1"
47 local name=$2
48 local sysfs=$3
49 local trigger=$4
50
51 uci -q get system.$cfg && return 0
52
53 uci batch <<EOF
54 set system.$cfg='led'
55 set system.$cfg.name='$name'
56 set system.$cfg.sysfs='$sysfs'
57 set system.$cfg.trigger='$trigger'
58 EOF
59 UCIDEF_LEDS_CHANGED=1
60 }
61
62 ucidef_set_led_switch() {
63 local cfg="led_$1"
64 local name=$2
65 local sysfs=$3
66 local trigger=$4
67 local port_mask=$5
68
69 uci -q get system.$cfg && return 0
70
71 uci batch <<EOF
72 set system.$cfg='led'
73 set system.$cfg.name='$name'
74 set system.$cfg.sysfs='$sysfs'
75 set system.$cfg.trigger='$trigger'
76 set system.$cfg.port_mask='$port_mask'
77 EOF
78 UCIDEF_LEDS_CHANGED=1
79 }
80
81 ucidef_set_led_default() {
82 local cfg="led_$1"
83 local name=$2
84 local sysfs=$3
85 local default=$4
86
87 uci -q get system.$cfg && return 0
88
89 uci batch <<EOF
90 set system.$cfg='led'
91 set system.$cfg.name='$name'
92 set system.$cfg.sysfs='$sysfs'
93 set system.$cfg.default='$default'
94 EOF
95 UCIDEF_LEDS_CHANGED=1
96 }
97
98 ucidef_set_led_rssi() {
99 local cfg="led_$1"
100 local name=$2
101 local sysfs=$3
102 local iface=$4
103 local minq=$5
104 local maxq=$6
105 local offset=$7
106 local factor=$8
107
108 uci -q get system.$cfg && return 0
109
110 uci batch <<EOF
111 set system.$cfg='led'
112 set system.$cfg.name='$name'
113 set system.$cfg.sysfs='$sysfs'
114 set system.$cfg.trigger='rssi'
115 set system.$cfg.iface='rssid_$iface'
116 set system.$cfg.minq='$minq'
117 set system.$cfg.maxq='$maxq'
118 set system.$cfg.offset='$offset'
119 set system.$cfg.factor='$factor'
120 EOF
121 UCIDEF_LEDS_CHANGED=1
122 }
123
124 ucidef_set_led_timer() {
125 local cfg="led_$1"
126 local name=$2
127 local sysfs=$3
128 local delayon=$4
129 local delayoff=$5
130
131 uci -q get system.$cfg && return 0
132
133 uci batch <<EOF
134 set system.$cfg='led'
135 set system.$cfg.name='$name'
136 set system.$cfg.sysfs='$sysfs'
137 set system.$cfg.trigger='timer'
138 set system.$cfg.delayon='$delayon'
139 set system.$cfg.delayoff='$delayoff'
140 EOF
141 UCIDEF_LEDS_CHANGED=1
142 }
143
144 ucidef_set_led_mmc() {
145 local cfg="led_$1"
146 local name=$2
147 local sysfs=$3
148 local trigger=$4
149
150 uci -q get system.$cfg && return 0
151
152 uci batch <<EOF
153 set system.$cfg='led'
154 set system.$cfg.name='$name'
155 set system.$cfg.sysfs='$sysfs'
156 set system.$cfg.trigger='$trigger'
157 EOF
158 UCIDEF_LEDS_CHANGED=1
159 }
160
161 ucidef_set_led_trigger_gpio() {
162 local cfg="led_$1"
163 local name=$2
164 local sysfs=$3
165 local gpio=$4
166 local inverted=$5
167
168 uci -q get system.$cfg && return 0
169
170 uci batch <<EOF
171 set system.$cfg='led'
172 set system.$cfg.name='$name'
173 set system.$cfg.sysfs='$sysfs'
174 set system.$cfg.trigger='gpio'
175 set system.$cfg.gpio='$gpio'
176 set system.$cfg.inverted='$inverted'
177 EOF
178 UCIDEF_LEDS_CHANGED=1
179 }
180
181 ucidef_set_led_ide_disk() {
182 local cfg="led_$1"
183 local name=$2
184 local sysfs=$3
185
186 uci -q get system.$cfg && return 0
187
188 uci batch <<EOF
189 set system.$cfg='led'
190 set system.$cfg.name='$name'
191 set system.$cfg.sysfs='$sysfs'
192 set system.$cfg.trigger='ide-disk'
193 EOF
194 UCIDEF_LEDS_CHANGED=1
195 }
196
197 ucidef_set_rssimon() {
198 local dev="$1"
199 local refresh="$2"
200 local threshold="$3"
201
202 local cfg="rssid_$dev"
203
204 uci -q get system.$cfg && return 0
205
206 uci batch <<EOF
207 set system.$cfg='rssid'
208 set system.$cfg.dev='$dev'
209 set system.$cfg.refresh='$refresh'
210 set system.$cfg.threshold='$threshold'
211 EOF
212 UCIDEF_LEDS_CHANGED=1
213 }
214
215 ucidef_commit_leds()
216 {
217 [ "$UCIDEF_LEDS_CHANGED" = "1" ] && uci commit system
218 }
219
220 ucidef_set_gpio_switch() {
221 local cfg="gpio_switch_$1"
222 local name="$2"
223 local gpio_pin="$3"
224 # use "0" as default value
225 local default="${4:-0}"
226
227 uci -q get "system.$cfg" && return 0
228
229 uci batch <<EOF
230 set system.$cfg='gpio_switch'
231 set system.$cfg.name='$name'
232 set system.$cfg.gpio_pin='$gpio_pin'
233 set system.$cfg.value='$default'
234 EOF
235 UCIDEF_GPIO_SWITCHES_CHANGED=1
236 }
237
238 ucidef_commit_gpio_switches()
239 {
240 [ "$UCIDEF_GPIO_SWITCHES_CHANGED" = "1" ] && uci commit system
241 }
242
243 ucidef_set_interface_loopback() {
244 uci batch <<EOF
245 set network.loopback='interface'
246 set network.loopback.ifname='lo'
247 set network.loopback.proto='static'
248 set network.loopback.ipaddr='127.0.0.1'
249 set network.loopback.netmask='255.0.0.0'
250 set network.globals='globals'
251 set network.globals.ula_prefix='auto'
252 EOF
253 }
254
255 ucidef_set_interface_raw() {
256 local cfg=$1
257 local ifname=$2
258 local proto=${3:-"none"}
259
260 uci batch <<EOF
261 set network.$cfg='interface'
262 set network.$cfg.ifname='$ifname'
263 set network.$cfg.proto='$proto'
264 EOF
265 }
266
267 ucidef_set_interface_lan() {
268 local ifname=$1
269
270 uci batch <<EOF
271 set network.lan='interface'
272 set network.lan.ifname='$ifname'
273 set network.lan.force_link=1
274 set network.lan.type='bridge'
275 set network.lan.proto='static'
276 set network.lan.ipaddr='192.168.1.1'
277 set network.lan.netmask='255.255.255.0'
278 set network.lan.ip6assign='60'
279 EOF
280 }
281
282 ucidef_set_interface_wan() {
283 local ifname=$1
284
285 uci batch <<EOF
286 set network.wan='interface'
287 set network.wan.ifname='$ifname'
288 set network.wan.proto='dhcp'
289 set network.wan6='interface'
290 set network.wan6.ifname='$ifname'
291 set network.wan6.proto='dhcpv6'
292 EOF
293 }
294
295 ucidef_set_interfaces_lan_wan() {
296 local lan_ifname=$1
297 local wan_ifname=$2
298
299 ucidef_set_interface_lan "$lan_ifname"
300 ucidef_set_interface_wan "$wan_ifname"
301 }
302
303 ucidef_set_interface_macaddr() {
304 local ifname=$1
305 local mac=$2
306
307 uci batch <<EOF
308 set network.$ifname.macaddr='$mac'
309 EOF
310 }
311
312 ucidef_add_switch() {
313 local name=$1
314 local reset=$2
315 local enable=$3
316 uci batch <<EOF
317 add network switch
318 set network.@switch[-1].name='$name'
319 set network.@switch[-1].reset='$reset'
320 set network.@switch[-1].enable_vlan='$enable'
321 EOF
322 }
323
324 ucidef_add_switch_vlan() {
325 local device=$1
326 local vlan=$2
327 local ports=$3
328 uci batch <<EOF
329 add network switch_vlan
330 set network.@switch_vlan[-1].device='$device'
331 set network.@switch_vlan[-1].vlan='$vlan'
332 set network.@switch_vlan[-1].ports='$ports'
333 EOF
334 }
335
336 ucidef_add_switch_port() {
337 local device=$1
338 local port=$2
339 uci batch <<EOF
340 add network switch_port
341 set network.@switch_port[-1].device='$device'
342 set network.@switch_port[-1].port='$port'
343 EOF
344 }
345