summaryrefslogtreecommitdiff
path: root/package/base-files/files/bin/config_generate
blob: 428c6bd9dee513aa2f3c1f1d7e62c180f0f94ec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/bin/sh

CFG=/etc/board.json

. /usr/share/libubox/jshn.sh

[ -f $CFG ] || exit 1

generate_static_network() {
	uci -q batch <<-EOF
		delete network.loopback
		set network.loopback='interface'
		set network.loopback.ifname='lo'
		set network.loopback.proto='static'
		set network.loopback.ipaddr='127.0.0.1'
		set network.loopback.netmask='255.0.0.0'
		delete network.globals
		set network.globals='globals'
		set network.globals.ula_prefix='auto'
	EOF

	if json_is_a dsl object; then
		json_select dsl
			if json_is_a atmbridge object; then
				json_select atmbridge
					local vpi vci encaps payload
					json_get_vars vpi vci encaps payload
					uci -q batch <<-EOF
						delete network.atm
						set network.atm='atm-bridge'
						set network.atm.vpi='$vpi'
						set network.atm.vci='$vci'
						set network.atm.encaps='$encaps'
						set network.atm.payload='$payload'
					EOF
				json_select ..
			fi

			if json_is_a modem object; then
				json_select modem
					local type annex firmware tone xfer_mode
					json_get_vars type annex firmware tone xfer_mode
					uci -q batch <<-EOF
						delete network.dsl
						set network.dsl='dsl'
						set network.dsl.annex='$annex'
						set network.dsl.firmware='$firmware'
						set network.dsl.tone='$tone'
						set network.dsl.xfer_mode='$xfer_mode'
					EOF
				json_select ..
			fi
		json_select ..
	fi
}

addr_offset=2
generate_network() {
	local ifname macaddr protocol type ipaddr netmask

	json_select network
		json_select "$1"
			json_get_vars ifname macaddr protocol ipaddr netmask
		json_select ..
	json_select ..

	[ -n "$ifname" ] || return

	# force bridge for multi-interface devices (and lan)
	case "$1:$ifname" in
		*\ * | lan:*) type="bridge" ;;
	esac

	uci -q batch <<-EOF
		delete network.$1
		set network.$1='interface'
		set network.$1.type='$type'
		set network.$1.ifname='$ifname'
		set network.$1.proto='none'
	EOF

	[ -n "$macaddr" ] && uci -q batch <<-EOF
		delete network.$1_dev
		set network.$1_dev='device'
		set network.$1_dev.name='$ifname'
		set network.$1_dev.macaddr='$macaddr'
	EOF

	case "$protocol" in
		static)
			local ipad
			case "$1" in
				lan) ipad=${ipaddr:-"192.168.1.1"} ;;
				*) ipad=${ipaddr:-"192.168.$((addr_offset++)).1"} ;;
			esac

			netm=${netmask:-"255.255.255.0"}

			uci -q batch <<-EOF
				set network.$1.proto='static'
				set network.$1.ipaddr='$ipad'
				set network.$1.netmask='$netm'
				set network.$1.ip6assign='60'
			EOF
		;;

		dhcp)
			# fixup IPv6 slave interface if parent is a bridge
			[ "$type" = "bridge" ] && ifname="br-$1"

			uci -q batch <<-EOF
				set network.$1.proto='dhcp'
				delete network.${1}6
				set network.${1}6='interface'
				set network.${1}6.ifname='$ifname'
				set network.${1}6.proto='dhcpv6'
			EOF
		;;

		pppoe)
			# fixup IPv6 slave interface
			ifname="pppoe-$1"

			uci -q batch <<-EOF
				set network.$1.proto='pppoe'
				set network.$1.username='username'
				set network.$1.password='password'
				set network.$1.ipv6='auto'
				delete network.${1}6
				set network.${1}6='interface'
				set network.${1}6.ifname='$ifname'
				set network.${1}6.proto='dhcpv6'
			EOF
		;;
	esac
}

generate_switch_vlans_ports() {
	local switch="$1"
	local port ports role roles num attr val

	#
	# autogenerate vlans
	#

	if json_is_a roles array; then
		json_get_keys roles roles
		json_select roles

		for role in $roles; do
			json_select "$role"
				json_get_vars ports
			json_select ..

			uci -q batch <<-EOF
				add network switch_vlan
				set network.@switch_vlan[-1].device='$switch'
				set network.@switch_vlan[-1].vlan='$role'
				set network.@switch_vlan[-1].ports='$ports'
			EOF
		done

		json_select ..
	fi


	#
	# write port specific settings
	#

	if json_is_a ports array; then
		json_get_keys ports ports
		json_select ports

		for port in $ports; do
			json_select "$port"
				json_get_vars num

				if json_is_a attr object; then
					json_get_keys attr attr
					json_select attr
						uci -q batch <<-EOF
							add network switch_port
							set network.@switch_port[-1].device='$switch'
							set network.@switch_port[-1].port=$num
						EOF

						for attr in $attr; do
							json_get_var val "$attr"
							uci -q set network.@switch_port[-1].$attr="$val"
						done
					json_select ..
				fi
			json_select ..
		done

		json_select ..
	fi
}

generate_switch() {
	local key="$1"
	local vlans

	json_select switch
	json_select "$key"
	json_get_vars enable reset blinkrate cpu_port

	uci -q batch <<-EOF
		add network switch
		set network.@switch[-1].name='$key'
		set network.@switch[-1].reset='$reset'
		set network.@switch[-1].enable_vlan='$enable'
		set network.@switch[-1].blinkrate='$blinkrate'
	EOF

	generate_switch_vlans_ports "$1"

	json_select ..
	json_select ..
}


generate_static_system() {
	uci -q batch <<-EOF
		delete system.@system[0]
		add system system
		set system.@system[-1].hostname='lede'
		set system.@system[-1].timezone='UTC'
		set system.@system[-1].ttylogin='0'

		delete system.ntp
		set system.ntp='timeserver'
		set system.ntp.enabled='1'
		set system.ntp.enable_server='0'
		add_list system.ntp.server='0.openwrt.pool.ntp.org'
		add_list system.ntp.server='1.openwrt.pool.ntp.org'
		add_list system.ntp.server='2.openwrt.pool.ntp.org'
		add_list system.ntp.server='3.openwrt.pool.ntp.org'
	EOF

	if json_is_a system object; then
		json_select system
			local hostname
			if json_get_var hostname hostname; then
				uci -q set "system.@system[-1].hostname=$hostname"
			fi

			if json_is_a ntpserver array; then
				local keys key
				json_get_keys keys ntpserver
				json_select ntpserver
					uci -q delete "system.ntp.server"

					for key in $keys; do
						local server
						if json_get_var server "$key"; then
							uci -q add_list "system.ntp.server=$server"
						fi
					done
				json_select ..
			fi
		json_select ..
	fi
}

generate_rssimon() {
	local key="$1"
	local cfg="rssid_$key"
	local refresh threshold

	json_select rssimon
	json_select "$key"
	json_get_vars refresh threshold
	json_select ..
	json_select ..

	uci -q batch <<-EOF
		delete system.$cfg
		set system.$cfg='rssid'
		set system.$cfg.dev='$key'
		set system.$cfg.refresh='$refresh'
		set system.$cfg.threshold='$threshold'
	EOF
}

generate_led() {
	local key="$1"
	local cfg="led_$key"

	json_select led
	json_select "$key"
	json_get_vars name sysfs type trigger default

	uci -q batch <<-EOF
		delete system.$cfg
		set system.$cfg='led'
		set system.$cfg.name='$name'
		set system.$cfg.sysfs='$sysfs'
		set system.$cfg.trigger='$trigger'
		set system.$cfg.default='$default'
	EOF

	case "$type" in
		gpio)
			local gpio inverted
			json_get_vars gpio inverted
			uci -q batch <<-EOF
				set system.$cfg.trigger='gpio'
				set system.$cfg.gpio='$gpio'
				set system.$cfg.inverted='$inverted'
			EOF
		;;

		netdev)
			local device mode
			json_get_vars device mode
			uci -q batch <<-EOF
				set system.$cfg.trigger='netdev'
				set system.$cfg.mode='$mode'
				set system.$cfg.dev='$device'
			EOF
		;;

		usb)
			local device
			json_get_vars device
			uci -q batch <<-EOF
				set system.$cfg.trigger='usbdev'
				set system.$cfg.interval='50'
				set system.$cfg.dev='$device'
			EOF
		;;

		rssi)
			local iface minq maxq offset factor
			json_get_vars iface minq maxq offset factor
			uci -q batch <<-EOF
				set system.$cfg.trigger='rssi'
				set system.$cfg.iface='rssid_$iface'
				set system.$cfg.minq='$minq'
				set system.$cfg.maxq='$maxq'
				set system.$cfg.offset='$offset'
				set system.$cfg.factor='$factor'
			EOF
		;;

		switch)
			local port_mask speed_mask
			json_get_vars port_mask speed_mask
			uci -q batch <<-EOF
				set system.$cfg.port_mask='$port_mask'
				set system.$cfg.speed_mask='$speed_mask'
			EOF
		;;

		portstate)
			local port_state
			json_get_vars port_state
			uci -q batch <<-EOF
				set system.$cfg.port_state='$port_state'
			EOF
		;;

		timer|oneshot)
			local delayon delayoff
			json_get_vars delayon delayoff
			uci -q batch <<-EOF
				set system.$cfg.trigger='$type'
				set system.$cfg.delayon='$delayon'
				set system.$cfg.delayoff='$delayoff'
			EOF
		;;
	esac

	json_select ..
	json_select ..
}

generate_gpioswitch() {
	local cfg="$1"

	json_select gpioswitch
		json_select "$cfg"
			local name pin default
			json_get_vars name pin default
			uci -q batch <<-EOF
				delete system.$cfg
				set system.$cfg='gpio_switch'
				set system.$cfg.name='$name'
				set system.$cfg.gpio_pin='$pin'
				set system.$cfg.default='$default'
			EOF
		json_select ..
	json_select ..
}

json_init
json_load "$(cat ${CFG})"

generate_static_network

json_get_keys keys network
for key in $keys; do generate_network $key; done

json_get_keys keys switch
for key in $keys; do generate_switch $key; done


generate_static_system

json_get_keys keys rssimon
for key in $keys; do generate_rssimon $key; done

json_get_keys keys gpioswitch
for key in $keys; do generate_gpioswitch $key; done

json_get_keys keys led
for key in $keys; do generate_led $key; done

uci commit