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