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