X-Git-Url: http://git.openwrt.org/?a=blobdiff_plain;f=package%2Fbase-files%2Ffiles%2Flib%2Ffunctions%2Fuci-defaults.sh;h=06004c3bb5fa79348fdea6cc6bb477cc10e60ae3;hb=0c8f72639ff018510974dc3c4059ad23a5c5c358;hp=eb5b240ee3080e369e9ceb8ddfef5a0d6d3976de;hpb=744298b8a0dc363a802634b5ac5d08e861e65d12;p=openwrt%2Fopenwrt.git diff --git a/package/base-files/files/lib/functions/uci-defaults.sh b/package/base-files/files/lib/functions/uci-defaults.sh old mode 100644 new mode 100755 index eb5b240ee3..06004c3bb5 --- a/package/base-files/files/lib/functions/uci-defaults.sh +++ b/package/base-files/files/lib/functions/uci-defaults.sh @@ -1,268 +1,660 @@ -#!/bin/sh -# Copyright (C) 2011 OpenWrt.org +#!/bin/ash -UCIDEF_LEDS_CHANGED=0 +. /lib/functions.sh +. /usr/share/libubox/jshn.sh -ucidef_set_led_netdev() { - local cfg="led_$1" - local name=$2 - local sysfs=$3 - local dev=$4 +json_select_array() { + local _json_no_warning=1 + + json_select "$1" + [ $? = 0 ] && return + + json_add_array "$1" + json_close_array + + json_select "$1" +} + +json_select_object() { + local _json_no_warning=1 + + json_select "$1" + [ $? = 0 ] && return + + json_add_object "$1" + json_close_object + + json_select "$1" +} + +_ucidef_set_interface() { + local name="$1" + local iface="$2" + local proto="$3" + + json_select_object "$name" + json_add_string ifname "$iface" + + if ! json_is_a protocol string; then + case "$proto" in + static|dhcp|none|pppoe) : ;; + *) + case "$name" in + lan) proto="static" ;; + wan) proto="dhcp" ;; + *) proto="none" ;; + esac + ;; + esac + + json_add_string protocol "$proto" + fi + + json_select .. +} + +ucidef_set_board_id() { + json_select_object model + json_add_string id "$1" + json_select .. +} + +ucidef_set_model_name() { + json_select_object model + json_add_string name "$1" + json_select .. +} + +ucidef_set_interface_lan() { + json_select_object network + _ucidef_set_interface lan "$@" + json_select .. +} + +ucidef_set_interface_wan() { + json_select_object network + _ucidef_set_interface wan "$@" + json_select .. +} + +ucidef_set_interfaces_lan_wan() { + local lan_if="$1" + local wan_if="$2" + + json_select_object network + _ucidef_set_interface lan "$lan_if" + _ucidef_set_interface wan "$wan_if" + json_select .. +} + +ucidef_set_interface_raw() { + json_select_object network + _ucidef_set_interface "$@" + json_select .. +} + +_ucidef_add_switch_port() { + # inherited: $num $device $need_tag $want_untag $role $index $prev_role + # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5 + + n_ports=$((n_ports + 1)) + + json_select_array ports + json_add_object + json_add_int num "$num" + [ -n "$device" ] && json_add_string device "$device" + [ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag" + [ -n "$want_untag" ] && json_add_boolean want_untag "$want_untag" + [ -n "$role" ] && json_add_string role "$role" + [ -n "$index" ] && json_add_int index "$index" + json_close_object + json_select .. + + # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles() + [ -n "$device" ] && { + export "cpu$n_cpu=$n_ports" + n_cpu=$((n_cpu + 1)) + } + + # create/append object to role list + [ -n "$role" ] && { + json_select_array roles + + if [ "$role" != "$prev_role" ]; then + json_add_object + json_add_string role "$role" + json_add_string ports "$num" + json_close_object + + prev_role="$role" + n_vlan=$((n_vlan + 1)) + else + json_select_object "$n_vlan" + json_get_var port ports + json_add_string ports "$port $num" + json_select .. + fi + + json_select .. + } +} + +_ucidef_finish_switch_roles() { + # inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5 + local index role roles num device need_tag want_untag port ports + + json_select switch + json_select "$name" + json_get_keys roles roles + json_select .. + json_select .. + + for index in $roles; do + eval "port=\$cpu$(((index - 1) % n_cpu))" + + json_select switch + json_select "$name" + json_select ports + json_select "$port" + json_get_vars num device need_tag want_untag + json_select .. + json_select .. + + if [ $n_vlan -gt $n_cpu -o ${need_tag:-0} -eq 1 ]; then + num="${num}t" + device="${device}.${index}" + fi + + json_select roles + json_select "$index" + json_get_vars role ports + json_add_string ports "$ports $num" + json_add_string device "$device" + json_select .. + json_select .. + json_select .. + json_select .. + + json_select_object network + local devices + + json_select_object "$role" + # attach previous interfaces (for multi-switch devices) + json_get_var devices ifname + if ! list_contains devices "$device"; then + devices="${devices:+$devices }$device" + fi + json_select .. + + _ucidef_set_interface "$role" "$devices" + json_select .. + done +} + +ucidef_add_switch() { + local name="$1"; shift + local port num role device index need_tag prev_role + local cpu0 cpu1 cpu2 cpu3 cpu4 cpu5 + local n_cpu=0 n_vlan=0 n_ports=0 + + json_select_object switch + json_select_object "$name" + json_add_boolean enable 1 + json_add_boolean reset 1 + + for port in "$@"; do + case "$port" in + [0-9]*@*) + num="${port%%@*}" + device="${port##*@}" + need_tag=0 + want_untag=0 + [ "${num%t}" != "$num" ] && { + num="${num%t}" + need_tag=1 + } + [ "${num%u}" != "$num" ] && { + num="${num%u}" + want_untag=1 + } + ;; + [0-9]*:*:[0-9]*) + num="${port%%:*}" + index="${port##*:}" + role="${port#[0-9]*:}"; role="${role%:*}" + ;; + [0-9]*:*) + num="${port%%:*}" + role="${port##*:}" + ;; + esac + + if [ -n "$num" ] && [ -n "$device$role" ]; then + _ucidef_add_switch_port + fi + + unset num device role index need_tag want_untag + done + json_select .. + json_select .. + + _ucidef_finish_switch_roles +} + +ucidef_add_switch_attr() { + local name="$1" + local key="$2" + local val="$3" + + json_select_object switch + json_select_object "$name" - uci -q get system.$cfg && return 0 + case "$val" in + true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;; + [0-9]) json_add_int "$key" "$val" ;; + *) json_add_string "$key" "$val" ;; + esac - uci batch < /tmp/.board.json + mv /tmp/.board.json ${CFG} +}