base-files: add ucidef_set_interface_wan() and ucidef_add_switch_ports()
[openwrt/svn-archive/archive.git] / package / base-files / files / lib / functions / uci-defaults-new.sh
1 #!/bin/ash
2
3 CFG=/etc/board.json
4
5 . /usr/share/libubox/jshn.sh
6
7 json_select_array() {
8 local _json_no_warning=1
9
10 json_select "$1"
11 [ $? = 0 ] && return
12
13 json_add_array $1
14 json_close_array
15
16 json_select "$1"
17 }
18
19 json_select_object() {
20 local _json_no_warning=1
21
22 json_select "$1"
23 [ $? = 0 ] && return
24
25 json_add_object $1
26 json_close_object
27
28 json_select "$1"
29 }
30
31 _ucidef_set_interface() {
32 local name=$1
33 local iface=$2
34
35 json_select_object $name
36 json_add_string ifname "${iface%%.*}"
37 [ "$iface" = "${iface%%.*}" ] || json_add_boolean create_vlan 1
38 json_select ..
39 }
40
41 ucidef_set_interface_loopback()
42 {
43 # stub
44 local a=$1
45 }
46
47 ucidef_set_interface_lan() {
48 local lan_if=$1
49
50 json_select_object network
51 _ucidef_set_interface lan $lan_if
52 json_select ..
53 }
54
55 ucidef_set_interface_wan() {
56 local wan_if=$1
57
58 json_select_object network
59 _ucidef_set_interface wan $wan_if
60 json_select ..
61 }
62
63 ucidef_set_interfaces_lan_wan() {
64 local lan_if=$1
65 local wan_if=$2
66
67 json_select_object network
68 _ucidef_set_interface lan $lan_if
69 _ucidef_set_interface wan $wan_if
70 json_select ..
71 }
72
73 ucidef_add_switch() {
74 local name=$1
75 local reset=$2
76 local enable=$3
77
78 json_select_object switch
79
80 json_select_object $name
81 json_add_boolean enable $enable
82 json_add_boolean reset $reset
83 json_select ..
84
85 json_select ..
86 }
87
88 ucidef_add_switch_attr() {
89 local name=$1
90 local key=$2
91 local val=$3
92
93 json_select_object switch
94
95 json_select_object $name
96 json_add_string $key $val
97 json_select ..
98
99 json_select ..
100 }
101
102 ucidef_add_switch_ports() {
103 local name="$1"; shift
104 local port num role dev idx
105
106 json_select_object switch
107 json_select_object "$name"
108 json_select_array ports
109
110 for port in "$@"; do
111 case "$port" in
112 [0-9]*@*)
113 num="${port%%@*}"
114 dev="${port##*@}"
115 ;;
116 [0-9]*:*:[0-9]*)
117 num="${port%%:*}"
118 idx="${port##*:}"
119 role="${port#[0-9]*:}"; role="${role%:*}"
120 ;;
121 [0-9]*:*)
122 num="${port%%:*}"
123 role="${port##*:}"
124 ;;
125 esac
126
127 if [ -n "$num" ] && [ -n "$dev$role" ]; then
128 json_add_object
129 json_add_int num "$num"
130 [ -n "$dev" ] && json_add_string device "$dev"
131 [ -n "$role" ] && json_add_string role "$role"
132 [ -n "$idx" ] && json_add_int index "$idx"
133 json_close_object
134 fi
135
136 unset num dev role idx
137 done
138
139 json_select ..
140 json_select ..
141 json_select ..
142 }
143
144 ucidef_add_switch_vlan() {
145 local name=$1
146 local vlan=$2
147 local ports=$3
148 local cpu_port=''
149
150 case $vlan in
151 1) vlan=lan;;
152 2) vlan=wan;;
153 *) vlan=vlan$vlan;;
154 esac
155
156 json_select_object switch
157 json_select_object $name
158 json_select_object vlans
159
160 json_add_array $vlan
161 for p in $ports; do
162 if [ ${p%t} != $p ]; then
163 cpu_port=$p
164 else
165 json_add_int "" $p
166 fi
167 done
168 json_close_array
169
170 json_select ..
171 [ -n "$cpu_port" ] && json_add_int cpu_port $cpu_port
172 json_select ..
173 json_select ..
174 }
175
176 ucidef_set_interface_macaddr() {
177 local network=$1
178 local macaddr=$2
179
180 json_select_object network
181
182 json_select $network
183 [ $? -eq 0 ] || {
184 json_select ..
185 return
186 }
187
188 json_add_string macaddr $macaddr
189 json_select ..
190
191 json_select ..
192 }
193
194 ucidef_set_led_netdev() {
195 local cfg="led_$1"
196 local name=$2
197 local sysfs=$3
198 local dev=$4
199
200 json_select_object led
201
202 json_select_object $1
203 json_add_string name $name
204 json_add_string type netdev
205 json_add_string sysfs $sysfs
206 json_add_string device $dev
207 json_select ..
208
209 json_select ..
210 }
211
212 ucidef_set_led_interface() {
213 local name=$1
214 local sysfs=$2
215
216 json_select_object led
217
218 json_select_object $1
219 json_add_string name $name
220 json_add_string type interface
221 json_add_string sysfs $sysfs
222 json_add_string interface $name
223 json_select ..
224
225 json_select ..
226 }
227
228 ucidef_set_led_usbdev() {
229 local cfg="led_$1"
230 local name=$2
231 local sysfs=$3
232 local dev=$4
233
234 json_select_object led
235
236 json_select_object $1
237 json_add_string name $name
238 json_add_string type usb
239 json_add_string sysfs $sysfs
240 json_add_string device $dev
241 json_select ..
242
243 json_select ..
244 }
245
246 ucidef_set_led_wlan() {
247 local cfg="led_$1"
248 local name=$2
249 local sysfs=$3
250 local trigger=$4
251
252 json_select_object led
253
254 json_select_object $1
255 json_add_string name $name
256 json_add_string type trigger
257 json_add_string sysfs $sysfs
258 json_add_string trigger $trigger
259 json_select ..
260
261 json_select ..
262 }
263
264 ucidef_set_led_switch() {
265 local cfg="led_$1"
266 local name=$2
267 local sysfs=$3
268 local trigger=$4
269 local port_mask=$5
270
271 json_select_object led
272
273 json_select_object $1
274 json_add_string name $name
275 json_add_string type switch
276 json_add_string sysfs $sysfs
277 json_add_string trigger $trigger
278 json_add_string port_mask $port_mask
279 json_select ..
280
281 json_select ..
282 }
283
284 ucidef_set_led_default() {
285 local cfg="led_$1"
286 local name=$2
287 local sysfs=$3
288 local default=$4
289
290 json_select_object led
291
292 json_select_object $1
293 json_add_string name $name
294 json_add_string sysfs $sysfs
295 json_add_string default $default
296 json_select ..
297
298 json_select ..
299 }
300
301 ucidef_set_led_rssi() {
302 local cfg="led_$1"
303 local name=$2
304 local sysfs=$3
305 local iface=$4
306 local minq=$5
307 local maxq=$6
308 local offset=$7
309 local factor=$8
310
311 json_select_object led
312
313 json_select_object rssi
314 json_select_object $1
315 json_add_string name $name
316 json_add_string sysfs $sysfs
317 json_add_string minq $minq
318 json_add_string maxq $maxq
319 json_add_string offset $offset
320 json_add_string factor $factor
321 json_select ..
322 json_select ..
323
324 json_select ..
325 }
326
327 ucidef_set_rssimon() {
328 local dev="$1"
329 local refresh="$2"
330 local threshold="$3"
331
332 json_select_object led
333
334 json_select_object rssi
335 json_add_string type rssi
336 json_add_string dev $dev
337 json_add_string threshold $threshold
338 json_select ..
339
340 json_select ..
341
342 }
343
344 board_config_update() {
345 json_init
346 [ -f ${CFG} ] && json_load "$(cat ${CFG})"
347 }
348
349 board_config_flush() {
350 json_dump -i > /tmp/.board.json
351 mv /tmp/.board.json ${CFG}
352 }