4256d3ce4d8912ed0f2582780fb7ea4f054a3b8a
[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 [ -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
60
61 json_select network
62 json_select "$1"
63 json_get_vars ifname macaddr protocol
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 ipaddr
92 case "$1" in
93 lan) ipaddr="192.168.1.1" ;;
94 *) ipaddr="192.168.$((addr_offset++)).1" ;;
95 esac
96
97 uci -q batch <<-EOF
98 set network.$1.proto='static'
99 set network.$1.ipaddr='$ipaddr'
100 set network.$1.netmask='255.255.255.0'
101 set network.$1.ip6assign='60'
102 EOF
103 ;;
104
105 dhcp)
106 # fixup IPv6 slave interface if parent is a bridge
107 [ "$type" = "bridge" ] && ifname="br-$1"
108
109 uci -q batch <<-EOF
110 set network.$1.proto='dhcp'
111 delete network.${1}6
112 set network.${1}6='interface'
113 set network.${1}6.ifname='$ifname'
114 set network.${1}6.proto='dhcpv6'
115 EOF
116 ;;
117
118 pppoe)
119 # fixup IPv6 slave interface
120 ifname="pppoe-$1"
121
122 uci -q batch <<-EOF
123 set network.$1.proto='pppoe'
124 set network.$1.username='username'
125 set network.$1.password='password'
126 set network.$1.ipv6='auto'
127 delete network.${1}6
128 set network.${1}6='interface'
129 set network.${1}6.ifname='$ifname'
130 set network.${1}6.proto='dhcpv6'
131 EOF
132 ;;
133 esac
134 }
135
136 generate_switch_vlans_ports() {
137 local switch="$1"
138 local port ports role roles num attr val
139
140 #
141 # autogenerate vlans
142 #
143
144 json_get_keys roles roles
145 json_select roles
146
147 for role in $roles; do
148 json_select "$role"
149 json_get_vars ports
150 json_select ..
151
152 uci -q batch <<-EOF
153 add network switch_vlan
154 set network.@switch_vlan[-1].device='$switch'
155 set network.@switch_vlan[-1].vlan='$role'
156 set network.@switch_vlan[-1].ports='$ports'
157 EOF
158 done
159
160 json_select ..
161
162
163 #
164 # write port specific settings
165 #
166
167 json_get_keys ports ports
168 json_select ports
169
170 for port in $ports; do
171 json_select "$port"
172 json_get_vars num
173
174 if json_is_a attr object; then
175 json_get_keys attr attr
176 json_select attr
177 uci -q batch <<-EOF
178 add network switch_port
179 set network.@switch_port[-1].device='$switch'
180 set network.@switch_port[-1].port=$num
181 EOF
182
183 for attr in $attr; do
184 json_get_var val "$attr"
185 uci -q set network.@switch_port[-1].$attr="$val"
186 done
187 json_select ..
188 fi
189 json_select ..
190 done
191
192 json_select ..
193 }
194
195 generate_switch() {
196 local key="$1"
197 local vlans
198
199 json_select switch
200 json_select "$key"
201 json_get_vars enable reset blinkrate cpu_port
202
203 uci -q batch <<-EOF
204 add network switch
205 set network.@switch[-1].name='$key'
206 set network.@switch[-1].reset='$reset'
207 set network.@switch[-1].enable_vlan='$enable'
208 set network.@switch[-1].blinkrate='$blinkrate'
209 EOF
210
211 generate_switch_vlans_ports "$1"
212
213 json_select ..
214 json_select ..
215 }
216
217 generate_rssimon() {
218 local key="$1"
219 local cfg="rssid_$key"
220 local refresh threshold
221
222 json_select rssimon
223 json_select "$key"
224 json_get_vars refresh threshold
225 json_select ..
226 json_select ..
227
228 uci -q batch <<-EOF
229 delete system.$cfg
230 set system.$cfg='rssid'
231 set system.$cfg.dev='$key'
232 set system.$cfg.refresh='$refresh'
233 set system.$cfg.threshold='$threshold'
234 EOF
235 }
236
237 generate_led() {
238 local key="$1"
239 local cfg="led_$key"
240
241 json_select led
242 json_select "$key"
243 json_get_vars name sysfs type trigger default
244
245 uci -q batch <<-EOF
246 delete system.$cfg
247 set system.$cfg='led'
248 set system.$cfg.name='$name'
249 set system.$cfg.sysfs='$sysfs'
250 set system.$cfg.trigger='$trigger'
251 set system.$cfg.default='$default'
252 EOF
253
254 case "$type" in
255 gpio)
256 local gpio inverted
257 json_get_vars gpio inverted
258 uci -q batch <<-EOF
259 set system.$cfg.trigger='gpio'
260 set system.$cfg.gpio='$gpio'
261 set system.$cfg.inverted='$inverted'
262 EOF
263 ;;
264
265 netdev)
266 local device mode
267 json_get_vars device mode
268 uci -q batch <<-EOF
269 set system.$cfg.trigger='netdev'
270 set system.$cfg.mode='$mode'
271 set system.$cfg.dev='$device'
272 EOF
273 ;;
274
275 usb)
276 local device
277 json_get_vars device
278 uci -q batch <<-EOF
279 set system.$cfg.trigger='usbdev'
280 set system.$cfg.interval='50'
281 set system.$cfg.dev='$device'
282 EOF
283 ;;
284
285 rssi)
286 local iface minq maxq offset factor
287 json_get_vars iface minq maxq offset factor
288 uci -q batch <<-EOF
289 set system.$cfg.trigger='rssi'
290 set system.$cfg.iface='rssid_$iface'
291 set system.$cfg.minq='$minq'
292 set system.$cfg.maxq='$maxq'
293 set system.$cfg.offset='$offset'
294 set system.$cfg.factor='$factor'
295 EOF
296 ;;
297
298 switch)
299 local port_mask speed_mask
300 json_get_vars port_mask speed_mask
301 uci -q batch <<-EOF
302 set system.$cfg.port_mask='$port_mask'
303 set system.$cfg.speed_mask='$speed_mask'
304 EOF
305 ;;
306
307 timer|oneshot)
308 local delayon delayoff
309 json_get_vars delayon delayoff
310 uci -q batch <<-EOF
311 set system.$cfg.trigger='$type'
312 set system.$cfg.delayon='$delayon'
313 set system.$cfg.delayoff='$delayoff'
314 EOF
315 ;;
316 esac
317
318 json_select ..
319 json_select ..
320 }
321
322 generate_gpioswitch() {
323 local cfg="$1"
324
325 json_select gpioswitch
326 json_select "$cfg"
327 local name pin default
328 json_get_vars name pin default
329 uci -q batch <<-EOF
330 delete system.$cfg
331 set system.$cfg='gpio_switch'
332 set system.$cfg.name='$name'
333 set system.$cfg.gpio_pin='$pin'
334 set system.$cfg.default='$default'
335 EOF
336 json_select ..
337 json_select ..
338 }
339
340 json_init
341 json_load "$(cat ${CFG})"
342
343 generate_static_network
344
345 json_get_keys keys network
346 for key in $keys; do generate_network $key; done
347
348 json_get_keys keys switch
349 for key in $keys; do generate_switch $key; done
350
351 json_get_keys keys rssimon
352 for key in $keys; do generate_rssimon $key; done
353
354 json_get_keys keys gpioswitch
355 for key in $keys; do generate_gpioswitch $key; done
356
357 json_get_keys keys led
358 for key in $keys; do generate_led $key; done
359
360 uci commit