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() {
31 local network=$1; shift
33 [ -z "$network" ] && return
35 json_select_object network
36 json_select_object "$network"
42 [ -n "$opt" -a -n "$val" ] || break
44 json_add_string "$opt" "$val"
47 if ! json_is_a protocol string; then
49 lan) json_add_string protocol static ;;
50 wan) json_add_string protocol dhcp ;;
51 *) json_add_string protocol none ;;
59 ucidef_set_board_id() {
60 json_select_object model
61 json_add_string id "$1"
65 ucidef_set_model_name() {
66 json_select_object model
67 json_add_string name "$1"
71 ucidef_set_interface_lan() {
72 ucidef_set_interface "lan" ifname "$1" protocol "${2:-static}"
75 ucidef_set_interface_wan() {
76 ucidef_set_interface "wan" ifname "$1" protocol "${2:-dhcp}"
79 ucidef_set_interfaces_lan_wan() {
83 ucidef_set_interface_lan "$lan_if"
84 ucidef_set_interface_wan "$wan_if"
87 _ucidef_add_switch_port() {
88 # inherited: $num $device $need_tag $want_untag $role $index $prev_role
89 # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
91 n_ports=$((n_ports + 1))
93 json_select_array ports
95 json_add_int num "$num"
96 [ -n "$device" ] && json_add_string device "$device"
97 [ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag"
98 [ -n "$want_untag" ] && json_add_boolean want_untag "$want_untag"
99 [ -n "$role" ] && json_add_string role "$role"
100 [ -n "$index" ] && json_add_int index "$index"
104 # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles()
105 [ -n "$device" ] && {
106 export "cpu$n_cpu=$n_ports"
110 # create/append object to role list
112 json_select_array roles
114 if [ "$role" != "$prev_role" ]; then
116 json_add_string role "$role"
117 json_add_string ports "$num"
121 n_vlan=$((n_vlan + 1))
123 json_select_object "$n_vlan"
124 json_get_var port ports
125 json_add_string ports "$port $num"
133 _ucidef_finish_switch_roles() {
134 # inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
135 local index role roles num device need_tag want_untag port ports
139 json_get_keys roles roles
143 for index in $roles; do
144 eval "port=\$cpu$(((index - 1) % n_cpu))"
150 json_get_vars num device need_tag want_untag
154 if [ ${need_tag:-0} -eq 1 -o ${want_untag:-0} -ne 1 ]; then
156 device="${device}.${index}"
161 json_get_vars role ports
162 json_add_string ports "$ports $num"
163 json_add_string device "$device"
169 json_select_object network
172 json_select_object "$role"
173 # attach previous interfaces (for multi-switch devices)
174 json_get_var devices ifname
175 if ! list_contains devices "$device"; then
176 devices="${devices:+$devices }$device"
181 ucidef_set_interface "$role" ifname "$devices"
185 ucidef_set_ar8xxx_switch_mib() {
190 json_select_object switch
191 json_select_object "$name"
192 json_add_int ar8xxx_mib_type $type
193 json_add_int ar8xxx_mib_poll_interval $interval
198 ucidef_add_switch() {
199 local name="$1"; shift
200 local port num role device index need_tag prev_role
201 local cpu0 cpu1 cpu2 cpu3 cpu4 cpu5
202 local n_cpu=0 n_vlan=0 n_ports=0
204 json_select_object switch
205 json_select_object "$name"
206 json_add_boolean enable 1
207 json_add_boolean reset 1
216 [ "${num%t}" != "$num" ] && {
220 [ "${num%u}" != "$num" ] && {
228 role="${port#[0-9]*:}"; role="${role%:*}"
236 if [ -n "$num" ] && [ -n "$device$role" ]; then
237 _ucidef_add_switch_port
240 unset num device role index need_tag want_untag
245 _ucidef_finish_switch_roles
248 ucidef_add_switch_attr() {
253 json_select_object switch
254 json_select_object "$name"
257 true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
258 [0-9]) json_add_int "$key" "$val" ;;
259 *) json_add_string "$key" "$val" ;;
266 ucidef_add_switch_port_attr() {
273 json_select_object switch
274 json_select_object "$name"
276 json_get_keys ports ports
277 json_select_array ports
283 if [ -n "$num" ] && [ $num -eq $port ]; then
284 json_select_object attr
287 true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
288 [0-9]) json_add_int "$key" "$val" ;;
289 *) json_add_string "$key" "$val" ;;
303 ucidef_set_interface_macaddr() {
307 ucidef_set_interface "$network" macaddr "$macaddr"
310 ucidef_add_atm_bridge() {
315 local nameprefix="$5"
317 json_select_object dsl
318 json_select_object atmbridge
319 json_add_int vpi "$vpi"
320 json_add_int vci "$vci"
321 json_add_string encaps "$encaps"
322 json_add_string payload "$payload"
323 json_add_string nameprefix "$nameprefix"
328 ucidef_add_adsl_modem() {
332 json_select_object dsl
333 json_select_object modem
334 json_add_string type "adsl"
335 json_add_string annex "$annex"
336 json_add_string firmware "$firmware"
341 ucidef_add_vdsl_modem() {
346 json_select_object dsl
347 json_select_object modem
348 json_add_string type "vdsl"
349 json_add_string annex "$annex"
350 json_add_string tone "$tone"
351 json_add_string xfer_mode "$xfer_mode"
356 ucidef_set_led_ataport() {
357 _ucidef_set_led_trigger "$1" "$2" "$3" ata"$4"
360 _ucidef_set_led_common() {
365 json_select_object led
367 json_select_object "$1"
368 json_add_string name "$name"
369 json_add_string sysfs "$sysfs"
372 ucidef_set_led_default() {
375 _ucidef_set_led_common "$1" "$2" "$3"
377 json_add_string default "$default"
383 ucidef_set_led_gpio() {
387 _ucidef_set_led_common "$1" "$2" "$3"
389 json_add_string trigger "$trigger"
390 json_add_string type gpio
391 json_add_int gpio "$gpio"
392 json_add_boolean inverted "$inverted"
398 ucidef_set_led_ide() {
399 _ucidef_set_led_trigger "$1" "$2" "$3" ide-disk
402 ucidef_set_led_netdev() {
404 local mode="${5:-link tx rx}"
406 _ucidef_set_led_common "$1" "$2" "$3"
408 json_add_string type netdev
409 json_add_string device "$dev"
410 json_add_string mode "$mode"
416 ucidef_set_led_oneshot() {
417 _ucidef_set_led_timer $1 $2 $3 "oneshot" $4 $5
420 ucidef_set_led_portstate() {
421 local port_state="$4"
423 _ucidef_set_led_common "$1" "$2" "$3"
425 json_add_string trigger port_state
426 json_add_string type portstate
427 json_add_string port_state "$port_state"
433 ucidef_set_led_rssi() {
437 local offset="${7:-0}"
438 local factor="${8:-1}"
440 _ucidef_set_led_common "$1" "$2" "$3"
442 json_add_string type rssi
443 json_add_string name "$name"
444 json_add_string iface "$iface"
445 json_add_string minq "$minq"
446 json_add_string maxq "$maxq"
447 json_add_string offset "$offset"
448 json_add_string factor "$factor"
454 ucidef_set_led_switch() {
455 local trigger_name="$4"
457 local speed_mask="$6"
460 _ucidef_set_led_common "$1" "$2" "$3"
462 json_add_string trigger "$trigger_name"
463 json_add_string type switch
464 json_add_string mode "$mode"
465 json_add_string port_mask "$port_mask"
466 json_add_string speed_mask "$speed_mask"
472 _ucidef_set_led_timer() {
473 local trigger_name="$4"
477 _ucidef_set_led_common "$1" "$2" "$3"
479 json_add_string type "$trigger_name"
480 json_add_string trigger "$trigger_name"
481 json_add_int delayon "$delayon"
482 json_add_int delayoff "$delayoff"
488 ucidef_set_led_timer() {
489 _ucidef_set_led_timer $1 $2 $3 "timer" $4 $5
492 _ucidef_set_led_trigger() {
493 local trigger_name="$4"
495 _ucidef_set_led_common "$1" "$2" "$3"
497 json_add_string trigger "$trigger_name"
503 ucidef_set_led_usbdev() {
506 _ucidef_set_led_common "$1" "$2" "$3"
508 json_add_string type usb
509 json_add_string device "$dev"
515 ucidef_set_led_usbhost() {
516 _ucidef_set_led_trigger "$1" "$2" "$3" usb-host
519 ucidef_set_led_usbport() {
527 _ucidef_set_led_common "$obj" "$name" "$sysfs"
529 json_add_string type usbport
530 json_select_array ports
532 json_add_string port "$port"
540 ucidef_set_led_wlan() {
541 _ucidef_set_led_trigger "$1" "$2" "$3" "$4"
544 ucidef_set_rssimon() {
549 json_select_object rssimon
551 json_select_object "$dev"
552 [ -n "$refresh" ] && json_add_int refresh "$refresh"
553 [ -n "$threshold" ] && json_add_int threshold "$threshold"
559 ucidef_add_gpio_switch() {
563 local default="${4:-0}"
565 json_select_object gpioswitch
566 json_select_object "$cfg"
567 json_add_string name "$name"
568 json_add_int pin "$pin"
569 json_add_int default "$default"
574 ucidef_set_hostname() {
577 json_select_object system
578 json_add_string hostname "$hostname"
582 ucidef_set_ntpserver() {
585 json_select_object system
586 json_select_array ntpserver
587 for server in "$@"; do
588 json_add_string "" "$server"
594 board_config_update() {
596 [ -f ${CFG} ] && json_load "$(cat ${CFG})"
598 # auto-initialize model id and name if applicable
599 if ! json_is_a model object; then
600 json_select_object model
601 [ -f "/tmp/sysinfo/board_name" ] && \
602 json_add_string id "$(cat /tmp/sysinfo/board_name)"
603 [ -f "/tmp/sysinfo/model" ] && \
604 json_add_string name "$(cat /tmp/sysinfo/model)"
609 board_config_flush() {
610 json_dump -i > /tmp/.board.json
611 mv /tmp/.board.json ${CFG}