base-files: config_generate: honour port_mask property (#20963)
[openwrt/svn-archive/archive.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
23 next_vlan=3
24 generate_network() {
25 local vlan
26
27 json_select network
28 json_select $1
29 json_get_vars ifname create_vlan macaddr
30 json_select ..
31 json_select ..
32
33 [ -n "$ifname" ] || return
34 [ "$create_vlan" -eq 1 ] && case $1 in
35 lan) vlan=1;;
36 wan) vlan=2;;
37 *)
38 vlan=$next_vlan
39 next_vlan=$((next_vlan + 1))
40 ;;
41 esac
42 [ -n "$vlan" ] && ifname=${ifname}.${vlan}
43 uci -q batch <<EOF
44 delete network.$1
45 set network.$1='interface'
46 set network.$1.ifname='$ifname'
47 set network.$1.force_link=1
48 set network.$1.proto='none'
49 set network.$1.macaddr='$macaddr'
50 EOF
51
52 case $1 in
53 lan) uci -q batch <<EOF
54 set network.$1.type='bridge'
55 set network.$1.proto='static'
56 set network.$1.ipaddr='192.168.1.1'
57 set network.$1.netmask='255.255.255.0'
58 set network.$1.ip6assign='60'
59 EOF
60 ;;
61 wan) uci -q batch <<EOF
62 set network.$1.proto='dhcp'
63 delete network.wan6
64 set network.wan6='interface'
65 set network.wan6.ifname='$ifname'
66 set network.wan6.proto='dhcpv6'
67 EOF
68 ;;
69 esac
70 }
71
72 generate_switch_vlan() {
73 local device=$1
74 local vlan=$2
75 local cpu_port=$3
76
77 case $vlan in
78 lan) vlan=1;;
79 wan) vlan=2;;
80 *) vlan=${vlan##vlan};;
81 esac
82
83 json_select vlans
84 json_select $2
85 json_get_values ports
86 json_select ..
87 json_select ..
88
89 uci -q batch <<EOF
90 add network switch_vlan
91 set network.@switch_vlan[-1].device='$device'
92 set network.@switch_vlan[-1].vlan='$vlan'
93 set network.@switch_vlan[-1].ports='$ports ${cpu_port}t'
94 EOF
95 }
96
97 generate_switch() {
98 local key=$1
99 local vlans
100
101 json_select switch
102 json_select $key
103 json_get_vars enable reset blinkrate cpu_port
104
105 uci -q batch <<EOF
106 add network switch
107 set network.@switch[-1].name='$key'
108 set network.@switch[-1].reset='$reset'
109 set network.@switch[-1].enable_vlan='$enable'
110 set network.@switch[-1].blinkrate='$blinkrate'
111 EOF
112 [ -n "$cpu_port" ] && {
113 json_get_keys vlans vlans
114 for vlan in $vlans; do generate_switch_vlan $1 $vlan $cpu_port; done
115 }
116 json_select ..
117 json_select ..
118 }
119
120 generate_led() {
121 local key=$1
122 local cfg="led_$key"
123
124 json_select led
125 json_select $key
126 json_get_vars name sysfs type trigger device interface default port_mask
127 json_select ..
128 json_select ..
129
130 uci -q batch <<EOF
131 delete system.$cfg
132 set system.$cfg='led'
133 set system.$cfg.name='$name'
134 set system.$cfg.sysfs='$sysfs'
135 set system.$cfg.dev='$device'
136 set system.$cfg.trigger='$trigger'
137 set system.$cfg.port_mask='$port_mask'
138 set system.$cfg.default='$default'
139 EOF
140 case $type in
141 netdev)
142 uci -q batch <<EOF
143 set system.$cfg.trigger='netdev'
144 set system.$cfg.mode='link tx rx'
145 EOF
146 ;;
147
148 usb)
149 uci -q batch <<EOF
150 set system.$cfg.trigger='usbdev'
151 set system.$cfg.interval='50'
152 EOF
153 ;;
154
155 esac
156 }
157
158 json_init
159 json_load "$(cat ${CFG})"
160
161 generate_static_network
162
163 json_get_keys keys network
164 for key in $keys; do generate_network $key; done
165
166 json_get_keys keys switch
167 for key in $keys; do generate_switch $key; done
168
169 json_get_keys keys led
170 for key in $keys; do generate_led $key; done
171
172 uci commit