512e0ed8bd9c15dd82560a847dde3420fcaa9b2c
[openwrt/openwrt.git] / package / base-files / files / lib / functions / uci-defaults.sh
1 #!/bin/ash
2
3 CFG=/etc/board.json
4
5 . /lib/functions.sh
6 . /usr/share/libubox/jshn.sh
7
8 json_select_array() {
9 local _json_no_warning=1
10
11 json_select "$1"
12 [ $? = 0 ] && return
13
14 json_add_array "$1"
15 json_close_array
16
17 json_select "$1"
18 }
19
20 json_select_object() {
21 local _json_no_warning=1
22
23 json_select "$1"
24 [ $? = 0 ] && return
25
26 json_add_object "$1"
27 json_close_object
28
29 json_select "$1"
30 }
31
32 _ucidef_set_interface() {
33 local name="$1"
34 local iface="$2"
35 local proto="$3"
36
37 json_select_object "$name"
38 json_add_string ifname "$iface"
39
40 if ! json_is_a protocol string; then
41 case "$proto" in
42 static|dhcp|none|pppoe) : ;;
43 *)
44 case "$name" in
45 lan) proto="static" ;;
46 wan) proto="dhcp" ;;
47 *) proto="none" ;;
48 esac
49 ;;
50 esac
51
52 json_add_string protocol "$proto"
53 fi
54
55 json_select ..
56 }
57
58 ucidef_set_board_id() {
59 json_select_object model
60 json_add_string id "$1"
61 json_select ..
62 }
63
64 ucidef_set_model_name() {
65 json_select_object model
66 json_add_string name "$1"
67 json_select ..
68 }
69
70 ucidef_set_interface_lan() {
71 json_select_object network
72 _ucidef_set_interface lan "$@"
73 json_select ..
74 }
75
76 ucidef_set_interface_wan() {
77 json_select_object network
78 _ucidef_set_interface wan "$@"
79 json_select ..
80 }
81
82 ucidef_set_interfaces_lan_wan() {
83 local lan_if="$1"
84 local wan_if="$2"
85
86 json_select_object network
87 _ucidef_set_interface lan "$lan_if"
88 _ucidef_set_interface wan "$wan_if"
89 json_select ..
90 }
91
92 _ucidef_add_switch_port() {
93 # inherited: $num $device $need_tag $role $index $prev_role
94 # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
95
96 n_ports=$((n_ports + 1))
97
98 json_select_array ports
99 json_add_object
100 json_add_int num "$num"
101 [ -n "$device" ] && json_add_string device "$device"
102 [ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag"
103 [ -n "$role" ] && json_add_string role "$role"
104 [ -n "$index" ] && json_add_int index "$index"
105 json_close_object
106 json_select ..
107
108 # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles()
109 [ -n "$device" ] && {
110 export "cpu$n_cpu=$n_ports"
111 n_cpu=$((n_cpu + 1))
112 }
113
114 # create/append object to role list
115 [ -n "$role" ] && {
116 json_select_array roles
117
118 if [ "$role" != "$prev_role" ]; then
119 json_add_object
120 json_add_string role "$role"
121 json_add_string ports "$num"
122 json_close_object
123
124 prev_role="$role"
125 n_vlan=$((n_vlan + 1))
126 else
127 json_select_object "$n_vlan"
128 json_get_var port ports
129 json_add_string ports "$port $num"
130 json_select ..
131 fi
132
133 json_select ..
134 }
135 }
136
137 _ucidef_finish_switch_roles() {
138 # inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
139 local index role roles num device need_tag port ports
140
141 json_select switch
142 json_select "$name"
143 json_get_keys roles roles
144 json_select ..
145 json_select ..
146
147 for index in $roles; do
148 eval "port=\$cpu$(((index - 1) % n_cpu))"
149
150 json_select switch
151 json_select "$name"
152 json_select ports
153 json_select "$port"
154 json_get_vars num device need_tag
155 json_select ..
156 json_select ..
157
158 if [ $n_vlan -gt $n_cpu -o ${need_tag:-0} -eq 1 ]; then
159 num="${num}t"
160 device="${device}.${index}"
161 fi
162
163 json_select roles
164 json_select "$index"
165 json_get_vars role ports
166 json_add_string ports "$ports $num"
167 json_add_string device "$device"
168 json_select ..
169 json_select ..
170 json_select ..
171 json_select ..
172
173 json_select_object network
174 local devices
175
176 json_select_object "$role"
177 # attach previous interfaces (for multi-switch devices)
178 json_get_var devices ifname
179 if ! list_contains devices "$device"; then
180 devices="${devices:+$devices }$device"
181 fi
182 json_select ..
183
184 _ucidef_set_interface "$role" "$devices"
185 json_select ..
186 done
187 }
188
189 ucidef_add_switch() {
190 local name="$1"; shift
191 local port num role device index need_tag prev_role
192 local cpu0 cpu1 cpu2 cpu3 cpu4 cpu5
193 local n_cpu=0 n_vlan=0 n_ports=0
194
195 json_select_object switch
196 json_select_object "$name"
197 json_add_boolean enable 1
198 json_add_boolean reset 1
199
200 for port in "$@"; do
201 case "$port" in
202 [0-9]*@*)
203 num="${port%%@*}"
204 device="${port##*@}"
205 need_tag=0
206 [ "${num%t}" != "$num" ] && {
207 num="${num%t}"
208 need_tag=1
209 }
210 ;;
211 [0-9]*:*:[0-9]*)
212 num="${port%%:*}"
213 index="${port##*:}"
214 role="${port#[0-9]*:}"; role="${role%:*}"
215 ;;
216 [0-9]*:*)
217 num="${port%%:*}"
218 role="${port##*:}"
219 ;;
220 esac
221
222 if [ -n "$num" ] && [ -n "$device$role" ]; then
223 _ucidef_add_switch_port
224 fi
225
226 unset num device role index need_tag
227 done
228 json_select ..
229 json_select ..
230
231 _ucidef_finish_switch_roles
232 }
233
234 ucidef_add_switch_attr() {
235 local name="$1"
236 local key="$2"
237 local val="$3"
238
239 json_select_object switch
240 json_select_object "$name"
241
242 case "$val" in
243 true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
244 [0-9]) json_add_int "$key" "$val" ;;
245 *) json_add_string "$key" "$val" ;;
246 esac
247
248 json_select ..
249 json_select ..
250 }
251
252 ucidef_add_switch_port_attr() {
253 local name="$1"
254 local port="$2"
255 local key="$3"
256 local val="$4"
257 local ports i num
258
259 json_select_object switch
260 json_select_object "$name"
261
262 json_get_keys ports ports
263 json_select_array ports
264
265 for i in $ports; do
266 json_select "$i"
267 json_get_var num num
268
269 if [ -n "$num" ] && [ $num -eq $port ]; then
270 json_select_object attr
271
272 case "$val" in
273 true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
274 [0-9]) json_add_int "$key" "$val" ;;
275 *) json_add_string "$key" "$val" ;;
276 esac
277
278 json_select ..
279 fi
280
281 json_select ..
282 done
283
284 json_select ..
285 json_select ..
286 json_select ..
287 }
288
289 ucidef_set_interface_macaddr() {
290 local network="$1"
291 local macaddr="$2"
292
293 json_select_object network
294
295 json_select "$network"
296 [ $? -eq 0 ] || {
297 json_select ..
298 return
299 }
300
301 json_add_string macaddr "$macaddr"
302 json_select ..
303
304 json_select ..
305 }
306
307 ucidef_add_atm_bridge() {
308 local vpi="$1"
309 local vci="$2"
310 local encaps="$3"
311 local payload="$4"
312
313 json_select_object dsl
314 json_select_object atmbridge
315 json_add_int vpi "$vpi"
316 json_add_int vci "$vci"
317 json_add_string encaps "$encaps"
318 json_add_string payload "$payload"
319 json_select ..
320 json_select ..
321 }
322
323 ucidef_add_adsl_modem() {
324 local annex="$1"
325 local firmware="$2"
326
327 json_select_object dsl
328 json_select_object modem
329 json_add_string type "adsl"
330 json_add_string annex "$annex"
331 json_add_string firmware "$firmware"
332 json_select ..
333 json_select ..
334 }
335
336 ucidef_add_vdsl_modem() {
337 local annex="$1"
338 local firmware="$2"
339 local tone="$3"
340 local xfer_mode="$4"
341
342 json_select_object dsl
343 json_select_object modem
344 json_add_string type "vdsl"
345 json_add_string annex "$annex"
346 json_add_string firmware "$firmware"
347 json_add_string tone "$tone"
348 json_add_string xfer_mode "$xfer_mode"
349 json_select ..
350 json_select ..
351 }
352
353 ucidef_set_led_netdev() {
354 local cfg="led_$1"
355 local name="$2"
356 local sysfs="$3"
357 local dev="$4"
358 local mode="${5:-link tx rx}"
359
360 json_select_object led
361
362 json_select_object "$1"
363 json_add_string name "$name"
364 json_add_string type netdev
365 json_add_string sysfs "$sysfs"
366 json_add_string device "$dev"
367 json_add_string mode "$mode"
368 json_select ..
369
370 json_select ..
371 }
372
373 ucidef_set_led_usbdev() {
374 local cfg="led_$1"
375 local name="$2"
376 local sysfs="$3"
377 local dev="$4"
378
379 json_select_object led
380
381 json_select_object "$1"
382 json_add_string name "$name"
383 json_add_string type usb
384 json_add_string sysfs "$sysfs"
385 json_add_string device "$dev"
386 json_select ..
387
388 json_select ..
389 }
390
391 ucidef_set_led_wlan() {
392 local cfg="led_$1"
393 local name="$2"
394 local sysfs="$3"
395 local trigger="$4"
396
397 json_select_object led
398
399 json_select_object "$1"
400 json_add_string name "$name"
401 json_add_string type trigger
402 json_add_string sysfs "$sysfs"
403 json_add_string trigger "$trigger"
404 json_select ..
405
406 json_select ..
407 }
408
409 ucidef_set_led_switch() {
410 local cfg="led_$1"
411 local name="$2"
412 local sysfs="$3"
413 local trigger="$4"
414 local port_mask="$5"
415 local speed_mask="$6"
416
417 json_select_object led
418
419 json_select_object "$1"
420 json_add_string name "$name"
421 json_add_string type switch
422 json_add_string sysfs "$sysfs"
423 json_add_string trigger "$trigger"
424 json_add_string port_mask "$port_mask"
425 json_add_string speed_mask "$speed_mask"
426 json_select ..
427
428 json_select ..
429 }
430
431 ucidef_set_led_default() {
432 local cfg="led_$1"
433 local name="$2"
434 local sysfs="$3"
435 local default="$4"
436
437 json_select_object led
438
439 json_select_object "$1"
440 json_add_string name "$name"
441 json_add_string sysfs "$sysfs"
442 json_add_string default "$default"
443 json_select ..
444
445 json_select ..
446 }
447
448 ucidef_set_led_gpio() {
449 local cfg="led_$1"
450 local name="$2"
451 local sysfs="$3"
452 local gpio="$4"
453 local inverted="$5"
454
455 json_select_object led
456
457 json_select_object "$1"
458 json_add_string type gpio
459 json_add_string name "$name"
460 json_add_string sysfs "$sysfs"
461 json_add_string trigger "$trigger"
462 json_add_int gpio "$gpio"
463 json_add_boolean inverted "$inverted"
464 json_select ..
465
466 json_select ..
467 }
468
469 ucidef_set_led_ide() {
470 local cfg="led_$1"
471 local name="$2"
472 local sysfs="$3"
473
474 json_select_object led
475
476 json_select_object "$1"
477 json_add_string name "$name"
478 json_add_string sysfs "$sysfs"
479 json_add_string trigger ide-disk
480 json_select ..
481
482 json_select ..
483 }
484
485 __ucidef_set_led_timer() {
486 local cfg="led_$1"
487 local name="$2"
488 local sysfs="$3"
489 local trigger="$4"
490 local delayon="$5"
491 local delayoff="$6"
492
493 json_select_object led
494
495 json_select_object "$1"
496 json_add_string type "$trigger"
497 json_add_string name "$name"
498 json_add_string sysfs "$sysfs"
499 json_add_int delayon "$delayon"
500 json_add_int delayoff "$delayoff"
501 json_select ..
502
503 json_select ..
504 }
505
506 ucidef_set_led_oneshot() {
507 __ucidef_set_led_timer $1 $2 $3 "oneshot" $4 $5
508 }
509
510 ucidef_set_led_timer() {
511 __ucidef_set_led_timer $1 $2 $3 "timer" $4 $5
512 }
513
514 ucidef_set_led_rssi() {
515 local cfg="led_$1"
516 local name="$2"
517 local sysfs="$3"
518 local iface="$4"
519 local minq="$5"
520 local maxq="$6"
521 local offset="$7"
522 local factor="$8"
523
524 json_select_object led
525
526 json_select_object "$1"
527 json_add_string type rssi
528 json_add_string name "$name"
529 json_add_string iface "$iface"
530 json_add_string sysfs "$sysfs"
531 json_add_string minq "$minq"
532 json_add_string maxq "$maxq"
533 json_add_string offset "$offset"
534 json_add_string factor "$factor"
535 json_select ..
536
537 json_select ..
538 }
539
540 ucidef_set_rssimon() {
541 local dev="$1"
542 local refresh="$2"
543 local threshold="$3"
544
545 json_select_object rssimon
546
547 json_select_object "$dev"
548 [ -n "$refresh" ] && json_add_int refresh "$refresh"
549 [ -n "$threshold" ] && json_add_int threshold "$threshold"
550 json_select ..
551
552 json_select ..
553
554 }
555
556 ucidef_add_gpio_switch() {
557 local cfg="$1"
558 local name="$2"
559 local pin="$3"
560 local default="${4:-0}"
561
562 json_select_object gpioswitch
563 json_select_object "$cfg"
564 json_add_string name "$name"
565 json_add_int pin "$pin"
566 json_add_int default "$default"
567 json_select ..
568 json_select ..
569 }
570
571 board_config_update() {
572 json_init
573 [ -f ${CFG} ] && json_load "$(cat ${CFG})"
574
575 # auto-initialize model id and name if applicable
576 if ! json_is_a model object; then
577 json_select_object model
578 [ -f "/tmp/sysinfo/board_name" ] && \
579 json_add_string id "$(cat /tmp/sysinfo/board_name)"
580 [ -f "/tmp/sysinfo/model" ] && \
581 json_add_string name "$(cat /tmp/sysinfo/model)"
582 json_select ..
583 fi
584 }
585
586 board_config_flush() {
587 json_dump -i > /tmp/.board.json
588 mv /tmp/.board.json ${CFG}
589 }