4 .
/usr
/share
/libubox
/jshn.sh
7 local _json_no_warning
=1
18 json_select_object
() {
19 local _json_no_warning
=1
30 ucidef_set_interface
() {
33 [ -z "$network" ] && return
35 json_select_object network
36 json_select_object
"$network"
44 [ -n "$opt" -a -n "$val" ] ||
break
46 json_add_string
"$opt" "$val"
53 ucidef_set_board_id
() {
54 json_select_object model
55 json_add_string id
"$1"
59 ucidef_set_model_name
() {
60 json_select_object model
61 json_add_string name
"$1"
65 ucidef_set_interface_lan
() {
66 ucidef_set_interface
"lan" ifname
"$1" protocol
"${2:-static}"
69 ucidef_set_interface_wan
() {
70 ucidef_set_interface
"wan" ifname
"$1" protocol
"${2:-dhcp}"
73 ucidef_set_interfaces_lan_wan
() {
77 ucidef_set_interface_lan
"$lan_if"
78 ucidef_set_interface_wan
"$wan_if"
81 _ucidef_add_switch_port
() {
82 # inherited: $num $device $need_tag $want_untag $role $index $prev_role
83 # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
85 n_ports
=$
((n_ports
+ 1))
87 json_select_array ports
89 json_add_int num
"$num"
90 [ -n "$device" ] && json_add_string device
"$device"
91 [ -n "$need_tag" ] && json_add_boolean need_tag
"$need_tag"
92 [ -n "$want_untag" ] && json_add_boolean want_untag
"$want_untag"
93 [ -n "$role" ] && json_add_string role
"$role"
94 [ -n "$index" ] && json_add_int index
"$index"
98 # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles()
100 export "cpu$n_cpu=$n_ports"
104 # create/append object to role list
106 json_select_array roles
108 if [ "$role" != "$prev_role" ]; then
110 json_add_string role
"$role"
111 json_add_string ports
"$num"
115 n_vlan
=$
((n_vlan
+ 1))
117 json_select_object
"$n_vlan"
118 json_get_var port ports
119 json_add_string ports
"$port $num"
127 _ucidef_finish_switch_roles
() {
128 # inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
129 local index role roles num device need_tag want_untag port ports
133 json_get_keys roles roles
137 for index
in $roles; do
138 eval "port=\$cpu$(((index - 1) % n_cpu))"
144 json_get_vars num device need_tag want_untag
148 if [ ${need_tag:-0} -eq 1 -o ${want_untag:-0} -ne 1 ]; then
150 device
="${device}.${index}"
155 json_get_vars role ports
156 json_add_string ports
"$ports $num"
157 json_add_string device
"$device"
163 json_select_object network
166 json_select_object
"$role"
167 # attach previous interfaces (for multi-switch devices)
168 json_get_var devices ifname
169 if ! list_contains devices
"$device"; then
170 devices
="${devices:+$devices }$device"
175 ucidef_set_interface
"$role" ifname
"$devices"
179 ucidef_add_switch
() {
180 local name
="$1"; shift
181 local port num role device index need_tag prev_role
182 local cpu0 cpu1 cpu2 cpu3 cpu4 cpu5
183 local n_cpu
=0 n_vlan
=0 n_ports
=0
185 json_select_object switch
186 json_select_object
"$name"
187 json_add_boolean
enable 1
188 json_add_boolean
reset 1
197 [ "${num%t}" != "$num" ] && {
201 [ "${num%u}" != "$num" ] && {
209 role
="${port#[0-9]*:}"; role
="${role%:*}"
217 if [ -n "$num" ] && [ -n "$device$role" ]; then
218 _ucidef_add_switch_port
221 unset num device role index need_tag want_untag
226 _ucidef_finish_switch_roles
229 ucidef_add_switch_attr
() {
234 json_select_object switch
235 json_select_object
"$name"
238 true|false
) [ "$val" != "true" ]; json_add_boolean
"$key" $?
;;
239 [0-9]) json_add_int
"$key" "$val" ;;
240 *) json_add_string
"$key" "$val" ;;
247 ucidef_add_switch_port_attr
() {
254 json_select_object switch
255 json_select_object
"$name"
257 json_get_keys ports ports
258 json_select_array ports
264 if [ -n "$num" ] && [ $num -eq $port ]; then
265 json_select_object attr
268 true|false
) [ "$val" != "true" ]; json_add_boolean
"$key" $?
;;
269 [0-9]) json_add_int
"$key" "$val" ;;
270 *) json_add_string
"$key" "$val" ;;
284 ucidef_set_interface_macaddr
() {
288 ucidef_set_interface
"$network" macaddr
"$macaddr"
291 ucidef_add_atm_bridge
() {
296 local nameprefix
="$5"
298 json_select_object dsl
299 json_select_object atmbridge
300 json_add_int vpi
"$vpi"
301 json_add_int vci
"$vci"
302 json_add_string encaps
"$encaps"
303 json_add_string payload
"$payload"
304 json_add_string nameprefix
"$nameprefix"
309 ucidef_add_adsl_modem
() {
313 json_select_object dsl
314 json_select_object modem
315 json_add_string
type "adsl"
316 json_add_string annex
"$annex"
317 json_add_string firmware
"$firmware"
322 ucidef_add_vdsl_modem
() {
327 json_select_object dsl
328 json_select_object modem
329 json_add_string
type "vdsl"
330 json_add_string annex
"$annex"
331 json_add_string tone
"$tone"
332 json_add_string xfer_mode
"$xfer_mode"
337 ucidef_set_led_ataport
() {
338 _ucidef_set_led_trigger
"$1" "$2" "$3" ata
"$4"
341 _ucidef_set_led_common
() {
346 json_select_object led
348 json_select_object
"$1"
349 json_add_string name
"$name"
350 json_add_string sysfs
"$sysfs"
353 ucidef_set_led_default
() {
356 _ucidef_set_led_common
"$1" "$2" "$3"
358 json_add_string default
"$default"
364 ucidef_set_led_gpio
() {
368 _ucidef_set_led_common
"$1" "$2" "$3"
370 json_add_string trigger
"$trigger"
371 json_add_string
type gpio
372 json_add_int gpio
"$gpio"
373 json_add_boolean inverted
"$inverted"
379 ucidef_set_led_ide
() {
380 _ucidef_set_led_trigger
"$1" "$2" "$3" ide-disk
383 ucidef_set_led_netdev
() {
385 local mode
="${5:-link tx rx}"
387 _ucidef_set_led_common
"$1" "$2" "$3"
389 json_add_string
type netdev
390 json_add_string device
"$dev"
391 json_add_string mode
"$mode"
397 ucidef_set_led_oneshot
() {
398 _ucidef_set_led_timer
$1 $2 $3 "oneshot" $4 $5
401 ucidef_set_led_portstate
() {
402 local port_state
="$4"
404 _ucidef_set_led_common
"$1" "$2" "$3"
406 json_add_string trigger port_state
407 json_add_string
type portstate
408 json_add_string port_state
"$port_state"
414 ucidef_set_led_rssi
() {
418 local offset
="${7:-0}"
419 local factor="${8:-1}"
421 _ucidef_set_led_common
"$1" "$2" "$3"
423 json_add_string
type rssi
424 json_add_string name
"$name"
425 json_add_string iface
"$iface"
426 json_add_string minq
"$minq"
427 json_add_string maxq
"$maxq"
428 json_add_string offset
"$offset"
429 json_add_string
factor "$factor"
435 ucidef_set_led_switch
() {
436 local trigger_name
="$4"
438 local speed_mask
="$6"
440 _ucidef_set_led_common
"$1" "$2" "$3"
442 json_add_string trigger
"$trigger_name"
443 json_add_string
type switch
444 json_add_string port_mask
"$port_mask"
445 json_add_string speed_mask
"$speed_mask"
451 _ucidef_set_led_timer
() {
452 local trigger_name
="$4"
456 _ucidef_set_led_common
"$1" "$2" "$3"
458 json_add_string trigger
"$trigger_name"
459 json_add_int delayon
"$delayon"
460 json_add_int delayoff
"$delayoff"
466 ucidef_set_led_timer
() {
467 _ucidef_set_led_timer
$1 $2 $3 "timer" $4 $5
470 _ucidef_set_led_trigger
() {
471 local trigger_name
="$4"
473 _ucidef_set_led_common
"$1" "$2" "$3"
475 json_add_string trigger
"$trigger_name"
481 ucidef_set_led_usbdev
() {
484 _ucidef_set_led_common
"$1" "$2" "$3"
486 json_add_string
type usb
487 json_add_string device
"$dev"
493 ucidef_set_led_usbhost
() {
494 _ucidef_set_led_trigger
"$1" "$2" "$3" usb-host
497 ucidef_set_led_usbport
() {
505 _ucidef_set_led_common
"$obj" "$name" "$sysfs"
507 json_add_string
type usbport
508 json_select_array ports
510 json_add_string port
"$port"
518 ucidef_set_led_wlan
() {
519 _ucidef_set_led_trigger
"$1" "$2" "$3" "$4"
522 ucidef_set_rssimon
() {
527 json_select_object rssimon
529 json_select_object
"$dev"
530 [ -n "$refresh" ] && json_add_int refresh
"$refresh"
531 [ -n "$threshold" ] && json_add_int threshold
"$threshold"
537 ucidef_add_gpio_switch
() {
541 local default
="${4:-0}"
543 json_select_object gpioswitch
544 json_select_object
"$cfg"
545 json_add_string name
"$name"
546 json_add_int pin
"$pin"
547 json_add_int default
"$default"
552 ucidef_set_hostname
() {
555 json_select_object system
556 json_add_string hostname
"$hostname"
560 ucidef_set_ntpserver
() {
563 json_select_object system
564 json_select_array ntpserver
565 for server
in "$@"; do
566 json_add_string
"" "$server"
572 board_config_update
() {
574 [ -f ${CFG} ] && json_load
"$(cat ${CFG})"
576 # auto-initialize model id and name if applicable
577 if ! json_is_a model object
; then
578 json_select_object model
579 [ -f "/tmp/sysinfo/board_name" ] && \
580 json_add_string id
"$(cat /tmp/sysinfo/board_name)"
581 [ -f "/tmp/sysinfo/model" ] && \
582 json_add_string name
"$(cat /tmp/sysinfo/model)"
587 board_config_flush
() {
588 json_dump
-i > /tmp
/.board.json
589 mv /tmp
/.board.json
${CFG}