ar71xx: ubnt-(xm,xw): add rssileds package
[openwrt/openwrt.git] / package / base-files / files / bin / config_generate
1 #!/bin/sh
2
3 CFG=/etc/board.json
4
5 . /usr/share/libubox/jshn.sh
6
7 [ -s $CFG ] || /bin/board_detect || exit 1
8 [ -s /etc/config/network -a -s /etc/config/system ] && exit 0
9
10 generate_static_network() {
11 uci -q batch <<-EOF
12 delete network.loopback
13 set network.loopback='interface'
14 set network.loopback.ifname='lo'
15 set network.loopback.proto='static'
16 set network.loopback.ipaddr='127.0.0.1'
17 set network.loopback.netmask='255.0.0.0'
18 EOF
19 [ -e /proc/sys/net/ipv6 ] && {
20 uci -q batch <<-EOF
21 delete network.globals
22 set network.globals='globals'
23 set network.globals.ula_prefix='auto'
24 EOF
25 }
26
27 if json_is_a dsl object; then
28 json_select dsl
29 if json_is_a atmbridge object; then
30 json_select atmbridge
31 local vpi vci encaps payload nameprefix
32 json_get_vars vpi vci encaps payload nameprefix
33 uci -q batch <<-EOF
34 delete network.atm
35 set network.atm='atm-bridge'
36 set network.atm.vpi='$vpi'
37 set network.atm.vci='$vci'
38 set network.atm.encaps='$encaps'
39 set network.atm.payload='$payload'
40 set network.atm.nameprefix='$nameprefix'
41 EOF
42 json_select ..
43 fi
44
45 if json_is_a modem object; then
46 json_select modem
47 local type annex firmware tone xfer_mode
48 json_get_vars type annex firmware tone xfer_mode
49 uci -q batch <<-EOF
50 delete network.dsl
51 set network.dsl='dsl'
52 set network.dsl.annex='$annex'
53 set network.dsl.firmware='$firmware'
54 set network.dsl.tone='$tone'
55 set network.dsl.xfer_mode='$xfer_mode'
56 EOF
57 json_select ..
58 fi
59 json_select ..
60 fi
61 }
62
63 addr_offset=2
64 generate_network() {
65 local ifname macaddr protocol type ipaddr netmask
66
67 json_select network
68 json_select "$1"
69 json_get_vars ifname macaddr protocol ipaddr netmask
70 json_select ..
71 json_select ..
72
73 [ -n "$ifname" ] || return
74
75 # force bridge for multi-interface devices (and lan)
76 case "$1:$ifname" in
77 *\ * | lan:*) type="bridge" ;;
78 esac
79
80 uci -q batch <<-EOF
81 delete network.$1
82 set network.$1='interface'
83 set network.$1.type='$type'
84 set network.$1.ifname='$ifname'
85 set network.$1.proto='none'
86 EOF
87
88 [ -n "$macaddr" ] && uci -q batch <<-EOF
89 delete network.$1_dev
90 set network.$1_dev='device'
91 set network.$1_dev.name='$ifname'
92 set network.$1_dev.macaddr='$macaddr'
93 EOF
94
95 case "$protocol" in
96 static)
97 local ipad
98 case "$1" in
99 lan) ipad=${ipaddr:-"192.168.1.1"} ;;
100 *) ipad=${ipaddr:-"192.168.$((addr_offset++)).1"} ;;
101 esac
102
103 netm=${netmask:-"255.255.255.0"}
104
105 uci -q batch <<-EOF
106 set network.$1.proto='static'
107 set network.$1.ipaddr='$ipad'
108 set network.$1.netmask='$netm'
109 EOF
110 [ -e /proc/sys/net/ipv6 ] && uci set network.$1.ip6assign='60'
111 ;;
112
113 dhcp)
114 # fixup IPv6 slave interface if parent is a bridge
115 [ "$type" = "bridge" ] && ifname="br-$1"
116
117 uci set network.$1.proto='dhcp'
118 [ -e /proc/sys/net/ipv6 ] && {
119 uci -q batch <<-EOF
120 delete network.${1}6
121 set network.${1}6='interface'
122 set network.${1}6.ifname='$ifname'
123 set network.${1}6.proto='dhcpv6'
124 EOF
125 }
126 ;;
127
128 pppoe)
129 uci -q batch <<-EOF
130 set network.$1.proto='pppoe'
131 set network.$1.username='username'
132 set network.$1.password='password'
133 EOF
134 [ -e /proc/sys/net/ipv6 ] && {
135 uci -q batch <<-EOF
136 set network.$1.ipv6='1'
137 delete network.${1}6
138 set network.${1}6='interface'
139 set network.${1}6.ifname='@${1}'
140 set network.${1}6.proto='dhcpv6'
141 EOF
142 }
143 ;;
144 esac
145 }
146
147 generate_switch_vlans_ports() {
148 local switch="$1"
149 local port ports role roles num attr val
150
151 #
152 # autogenerate vlans
153 #
154
155 if json_is_a roles array; then
156 json_get_keys roles roles
157 json_select roles
158
159 for role in $roles; do
160 json_select "$role"
161 json_get_vars ports
162 json_select ..
163
164 uci -q batch <<-EOF
165 add network switch_vlan
166 set network.@switch_vlan[-1].device='$switch'
167 set network.@switch_vlan[-1].vlan='$role'
168 set network.@switch_vlan[-1].ports='$ports'
169 EOF
170 done
171
172 json_select ..
173 fi
174
175
176 #
177 # write port specific settings
178 #
179
180 if json_is_a ports array; then
181 json_get_keys ports ports
182 json_select ports
183
184 for port in $ports; do
185 json_select "$port"
186 json_get_vars num
187
188 if json_is_a attr object; then
189 json_get_keys attr attr
190 json_select attr
191 uci -q batch <<-EOF
192 add network switch_port
193 set network.@switch_port[-1].device='$switch'
194 set network.@switch_port[-1].port=$num
195 EOF
196
197 for attr in $attr; do
198 json_get_var val "$attr"
199 uci -q set network.@switch_port[-1].$attr="$val"
200 done
201 json_select ..
202 fi
203 json_select ..
204 done
205
206 json_select ..
207 fi
208 }
209
210 generate_switch() {
211 local key="$1"
212 local vlans
213
214 json_select switch
215 json_select "$key"
216 json_get_vars enable reset blinkrate cpu_port \
217 ar8xxx_mib_type ar8xxx_mib_poll_interval
218
219 uci -q batch <<-EOF
220 add network switch
221 set network.@switch[-1].name='$key'
222 set network.@switch[-1].reset='$reset'
223 set network.@switch[-1].enable_vlan='$enable'
224 set network.@switch[-1].blinkrate='$blinkrate'
225 set network.@switch[-1].ar8xxx_mib_type='$ar8xxx_mib_type'
226 set network.@switch[-1].ar8xxx_mib_poll_interval='$ar8xxx_mib_poll_interval'
227 EOF
228
229 generate_switch_vlans_ports "$1"
230
231 json_select ..
232 json_select ..
233 }
234
235
236 generate_static_system() {
237 uci -q batch <<-EOF
238 delete system.@system[0]
239 add system system
240 set system.@system[-1].hostname='OpenWrt'
241 set system.@system[-1].timezone='UTC'
242 set system.@system[-1].ttylogin='0'
243 set system.@system[-1].log_size='64'
244 set system.@system[-1].urandom_seed='0'
245
246 delete system.ntp
247 set system.ntp='timeserver'
248 set system.ntp.enabled='1'
249 set system.ntp.enable_server='0'
250 add_list system.ntp.server='0.openwrt.pool.ntp.org'
251 add_list system.ntp.server='1.openwrt.pool.ntp.org'
252 add_list system.ntp.server='2.openwrt.pool.ntp.org'
253 add_list system.ntp.server='3.openwrt.pool.ntp.org'
254 EOF
255
256 if json_is_a system object; then
257 json_select system
258 local hostname
259 if json_get_var hostname hostname; then
260 uci -q set "system.@system[-1].hostname=$hostname"
261 fi
262
263 if json_is_a ntpserver array; then
264 local keys key
265 json_get_keys keys ntpserver
266 json_select ntpserver
267 uci -q delete "system.ntp.server"
268
269 for key in $keys; do
270 local server
271 if json_get_var server "$key"; then
272 uci -q add_list "system.ntp.server=$server"
273 fi
274 done
275 json_select ..
276 fi
277 json_select ..
278 fi
279 }
280
281 generate_rssimon() {
282 local key="$1"
283 local cfg="rssid_$key"
284 local refresh threshold
285
286 json_select rssimon
287 json_select "$key"
288 json_get_vars refresh threshold
289 json_select ..
290 json_select ..
291
292 uci -q batch <<-EOF
293 delete system.$cfg
294 set system.$cfg='rssid'
295 set system.$cfg.dev='$key'
296 set system.$cfg.refresh='$refresh'
297 set system.$cfg.threshold='$threshold'
298 EOF
299 }
300
301 generate_led() {
302 local key="$1"
303 local cfg="led_$key"
304
305 json_select led
306 json_select "$key"
307 json_get_vars name sysfs type trigger default
308
309 uci -q batch <<-EOF
310 delete system.$cfg
311 set system.$cfg='led'
312 set system.$cfg.name='$name'
313 set system.$cfg.sysfs='$sysfs'
314 set system.$cfg.trigger='$trigger'
315 set system.$cfg.default='$default'
316 EOF
317
318 case "$type" in
319 gpio)
320 local gpio inverted
321 json_get_vars gpio inverted
322 uci -q batch <<-EOF
323 set system.$cfg.trigger='gpio'
324 set system.$cfg.gpio='$gpio'
325 set system.$cfg.inverted='$inverted'
326 EOF
327 ;;
328
329 netdev)
330 local device mode
331 json_get_vars device mode
332 uci -q batch <<-EOF
333 set system.$cfg.trigger='netdev'
334 set system.$cfg.mode='$mode'
335 set system.$cfg.dev='$device'
336 EOF
337 ;;
338
339 usb)
340 local device
341 json_get_vars device
342 uci -q batch <<-EOF
343 set system.$cfg.trigger='usbdev'
344 set system.$cfg.interval='50'
345 set system.$cfg.dev='$device'
346 EOF
347 ;;
348
349 usbport)
350 local ports port
351 json_get_values ports ports
352 uci set system.$cfg.trigger='usbport'
353 for port in $ports; do
354 uci add_list system.$cfg.port=$port
355 done
356 ;;
357
358 rssi)
359 local iface minq maxq offset factor
360 json_get_vars iface minq maxq offset factor
361 uci -q batch <<-EOF
362 set system.$cfg.trigger='rssi'
363 set system.$cfg.iface='rssid_$iface'
364 set system.$cfg.minq='$minq'
365 set system.$cfg.maxq='$maxq'
366 set system.$cfg.offset='$offset'
367 set system.$cfg.factor='$factor'
368 EOF
369 ;;
370
371 switch)
372 local port_mask speed_mask mode
373 json_get_vars port_mask speed_mask mode
374 uci -q batch <<-EOF
375 set system.$cfg.port_mask='$port_mask'
376 set system.$cfg.speed_mask='$speed_mask'
377 set system.$cfg.mode='$mode'
378 EOF
379 ;;
380
381 portstate)
382 local port_state
383 json_get_vars port_state
384 uci -q batch <<-EOF
385 set system.$cfg.port_state='$port_state'
386 EOF
387 ;;
388
389 timer|oneshot)
390 local delayon delayoff
391 json_get_vars delayon delayoff
392 uci -q batch <<-EOF
393 set system.$cfg.trigger='$type'
394 set system.$cfg.delayon='$delayon'
395 set system.$cfg.delayoff='$delayoff'
396 EOF
397 ;;
398 esac
399
400 json_select ..
401 json_select ..
402 }
403
404 generate_gpioswitch() {
405 local cfg="$1"
406
407 json_select gpioswitch
408 json_select "$cfg"
409 local name pin default
410 json_get_vars name pin default
411 uci -q batch <<-EOF
412 delete system.$cfg
413 set system.$cfg='gpio_switch'
414 set system.$cfg.name='$name'
415 set system.$cfg.gpio_pin='$pin'
416 set system.$cfg.value='$default'
417 EOF
418 json_select ..
419 json_select ..
420 }
421
422 json_init
423 json_load "$(cat ${CFG})"
424
425 if [ ! -s /etc/config/network ]; then
426 touch /etc/config/network
427 generate_static_network
428
429 json_get_keys keys network
430 for key in $keys; do generate_network $key; done
431
432 json_get_keys keys switch
433 for key in $keys; do generate_switch $key; done
434 fi
435
436 if [ ! -s /etc/config/system ]; then
437 touch /etc/config/system
438 generate_static_system
439
440 json_get_keys keys rssimon
441 for key in $keys; do generate_rssimon $key; done
442
443 json_get_keys keys gpioswitch
444 for key in $keys; do generate_gpioswitch $key; done
445
446 json_get_keys keys led
447 for key in $keys; do generate_led $key; done
448 fi
449 uci commit