2 .
/usr
/share
/libubox
/jshn.sh
5 local _json_no_warning
=1
16 json_select_object
() {
17 local _json_no_warning
=1
28 ucidef_set_interface
() {
29 local network
=$1; shift
31 [ -z "$network" ] && return
33 json_select_object network
34 json_select_object
"$network"
40 [ -n "$opt" -a -n "$val" ] ||
break
42 [ "$opt" = "device" -a "$val" != "${val/ //}" ] && {
43 json_select_array
"ports"
44 for e
in $val; do json_add_string
"" "$e"; done
47 json_add_string
"$opt" "$val"
51 if ! json_is_a protocol string
; then
53 lan
) json_add_string protocol static
;;
54 wan
) json_add_string protocol dhcp
;;
55 *) json_add_string protocol none
;;
63 ucidef_set_board_id
() {
64 json_select_object model
65 json_add_string id
"$1"
69 ucidef_set_model_name
() {
70 json_select_object model
71 json_add_string name
"$1"
75 ucidef_set_compat_version
() {
76 json_select_object system
77 json_add_string compat_version
"${1:-1.0}"
81 ucidef_set_interface_lan
() {
82 ucidef_set_interface
"lan" device
"$1" protocol
"${2:-static}"
85 ucidef_set_interface_wan
() {
86 ucidef_set_interface
"wan" device
"$1" protocol
"${2:-dhcp}"
89 ucidef_set_interfaces_lan_wan
() {
93 ucidef_set_interface_lan
"$lan_if"
94 ucidef_set_interface_wan
"$wan_if"
97 ucidef_set_bridge_device
() {
98 json_select_object bridge
99 json_add_string name
"${1:-switch0}"
103 ucidef_set_bridge_mac
() {
104 json_select_object bridge
105 json_add_string macaddr
"${1}"
109 _ucidef_set_network_device_common
() {
110 json_select_object
"network_device"
111 json_select_object
"${1}"
112 json_add_string
"${2}" "${3}"
117 ucidef_set_network_device_mac
() {
118 _ucidef_set_network_device_common
$1 macaddr
$2
121 ucidef_set_network_device_path
() {
122 _ucidef_set_network_device_common
$1 path
$2
125 ucidef_set_network_device_gro
() {
126 _ucidef_set_network_device_common
$1 gro
$2
129 ucidef_set_network_device_conduit
() {
130 _ucidef_set_network_device_common
$1 conduit
$2
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
137 n_ports
=$
((n_ports
+ 1))
139 json_select_array ports
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"
150 # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles()
151 [ -n "$device" ] && {
152 export "cpu$n_cpu=$n_ports"
156 # create/append object to role list
158 json_select_array roles
160 if [ "$role" != "$prev_role" ]; then
162 json_add_string role
"$role"
163 json_add_string ports
"$num"
167 n_vlan
=$
((n_vlan
+ 1))
169 json_select_object
"$n_vlan"
170 json_get_var port ports
171 json_add_string ports
"$port $num"
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
185 json_get_keys roles roles
189 for index
in $roles; do
190 eval "port=\$cpu$(((index - 1) % n_cpu))"
196 json_get_vars num device need_tag want_untag
200 if [ ${need_tag:-0} -eq 1 -o ${want_untag:-0} -ne 1 ]; then
202 device
="${device}.${index}"
207 json_get_vars role ports
208 json_add_string ports
"$ports $num"
209 json_add_string device
"$device"
215 json_select_object network
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"
227 ucidef_set_interface
"$role" device
"$devices"
231 ucidef_set_ar8xxx_switch_mib
() {
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
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
250 json_select_object switch
251 json_select_object
"$name"
252 json_add_boolean
enable 1
253 json_add_boolean
reset 1
262 [ "${num%t}" != "$num" ] && {
266 [ "${num%u}" != "$num" ] && {
274 role
="${port#[0-9]*:}"; role
="${role%:*}"
282 if [ -n "$num" ] && [ -n "$device$role" ]; then
283 _ucidef_add_switch_port
286 unset num device role index need_tag want_untag
291 _ucidef_finish_switch_roles
294 ucidef_add_switch_attr
() {
299 json_select_object switch
300 json_select_object
"$name"
303 true|false
) [ "$val" != "true" ]; json_add_boolean
"$key" $?
;;
304 [0-9]) json_add_int
"$key" "$val" ;;
305 *) json_add_string
"$key" "$val" ;;
312 ucidef_add_switch_port_attr
() {
319 json_select_object switch
320 json_select_object
"$name"
322 json_get_keys ports ports
323 json_select_array ports
329 if [ -n "$num" ] && [ $num -eq $port ]; then
330 json_select_object attr
333 true|false
) [ "$val" != "true" ]; json_add_boolean
"$key" $?
;;
334 [0-9]) json_add_int
"$key" "$val" ;;
335 *) json_add_string
"$key" "$val" ;;
349 ucidef_set_interface_macaddr
() {
353 ucidef_set_interface
"$network" macaddr
"$macaddr"
356 ucidef_set_label_macaddr
() {
359 json_select_object system
360 json_add_string label_macaddr
"$macaddr"
364 ucidef_add_atm_bridge
() {
369 local nameprefix
="$5"
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"
382 ucidef_add_adsl_modem
() {
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"
395 ucidef_add_vdsl_modem
() {
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"
410 ucidef_set_led_ataport
() {
411 _ucidef_set_led_trigger
"$1" "$2" "$3" ata
"$4"
414 _ucidef_set_led_common
() {
419 json_select_object led
421 json_select_object
"$1"
422 json_add_string name
"$name"
423 json_add_string sysfs
"$sysfs"
426 ucidef_set_led_default
() {
429 _ucidef_set_led_common
"$1" "$2" "$3"
431 json_add_string default
"$default"
437 ucidef_set_led_heartbeat
() {
438 _ucidef_set_led_common
"$1" "$2" "$3"
440 json_add_string trigger heartbeat
446 ucidef_set_led_gpio
() {
450 _ucidef_set_led_common
"$1" "$2" "$3"
452 json_add_string trigger
"$trigger"
453 json_add_string
type gpio
454 json_add_int gpio
"$gpio"
455 json_add_boolean inverted
"$inverted"
461 ucidef_set_led_ide
() {
462 _ucidef_set_led_trigger
"$1" "$2" "$3" disk-activity
465 ucidef_set_led_netdev
() {
467 local mode
="${5:-link tx rx}"
469 _ucidef_set_led_common
"$1" "$2" "$3"
471 json_add_string
type netdev
472 json_add_string device
"$dev"
473 json_add_string mode
"$mode"
479 ucidef_set_led_oneshot
() {
480 _ucidef_set_led_timer
$1 $2 $3 "oneshot" $4 $5
483 ucidef_set_led_portstate
() {
484 local port_state
="$4"
486 _ucidef_set_led_common
"$1" "$2" "$3"
488 json_add_string trigger port_state
489 json_add_string
type portstate
490 json_add_string port_state
"$port_state"
496 ucidef_set_led_rssi
() {
500 local offset
="${7:-0}"
501 local factor="${8:-1}"
503 _ucidef_set_led_common
"$1" "$2" "$3"
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"
517 ucidef_set_led_switch
() {
518 local trigger_name
="$4"
520 local speed_mask
="$6"
523 _ucidef_set_led_common
"$1" "$2" "$3"
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"
535 _ucidef_set_led_timer
() {
536 local trigger_name
="$4"
540 _ucidef_set_led_common
"$1" "$2" "$3"
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"
551 ucidef_set_led_timer
() {
552 _ucidef_set_led_timer
$1 $2 $3 "timer" $4 $5
555 _ucidef_set_led_trigger
() {
556 local trigger_name
="$4"
558 _ucidef_set_led_common
"$1" "$2" "$3"
560 json_add_string trigger
"$trigger_name"
566 ucidef_set_led_usbdev
() {
569 _ucidef_set_led_common
"$1" "$2" "$3"
571 json_add_string
type usb
572 json_add_string device
"$dev"
578 ucidef_set_led_usbhost
() {
579 _ucidef_set_led_trigger
"$1" "$2" "$3" usb-host
582 ucidef_set_led_usbport
() {
590 _ucidef_set_led_common
"$obj" "$name" "$sysfs"
592 json_add_string
type usbport
593 json_select_array ports
595 json_add_string port
"$port"
603 ucidef_set_led_wlan
() {
604 _ucidef_set_led_trigger
"$1" "$2" "$3" "$4"
607 ucidef_set_rssimon
() {
612 json_select_object rssimon
614 json_select_object
"$dev"
615 [ -n "$refresh" ] && json_add_int refresh
"$refresh"
616 [ -n "$threshold" ] && json_add_int threshold
"$threshold"
622 ucidef_add_gpio_switch
() {
626 local default
="${4:-0}"
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"
637 ucidef_set_hostname
() {
640 json_select_object system
641 json_add_string hostname
"$hostname"
645 ucidef_set_ntpserver
() {
648 json_select_object system
649 json_select_array ntpserver
650 for server
in "$@"; do
651 json_add_string
"" "$server"
658 json_select_object poe
659 json_add_string
"budget" "$1"
660 json_select_array ports
662 json_add_string
"" "$port"
669 local path
="$1"; shift
671 ucidef_wlan_idx
=${ucidef_wlan_idx:-0}
673 json_select_object wlan
674 json_select_object
"wl$ucidef_wlan_idx"
675 json_add_string path
"$path"
680 ucidef_wlan_idx
="$((ucidef_wlan_idx + 1))"
683 board_config_update
() {
685 [ -f ${CFG} ] && json_load
"$(cat ${CFG})"
687 # auto-initialize model id and name if applicable
688 if ! json_is_a model object
; then
689 json_select_object model
690 [ -f "/tmp/sysinfo/board_name" ] && \
691 json_add_string id
"$(cat /tmp/sysinfo/board_name)"
692 [ -f "/tmp/sysinfo/model" ] && \
693 json_add_string name
"$(cat /tmp/sysinfo/model)"
698 board_config_flush
() {
699 json_dump
-i -o ${CFG}