base-files: use "ports" array in board.json network for bridges
[openwrt/openwrt.git] / package / base-files / files / lib / functions / uci-defaults.sh
old mode 100755 (executable)
new mode 100644 (file)
index 3126fe6..407a9c7
@@ -1,5 +1,3 @@
-#!/bin/ash
-
 . /lib/functions.sh
 . /usr/share/libubox/jshn.sh
 
@@ -27,30 +25,39 @@ json_select_object() {
        json_select "$1"
 }
 
-_ucidef_set_interface() {
-       local name="$1"
-       local iface="$2"
-       local proto="$3"
+ucidef_set_interface() {
+       local network=$1; shift
 
-       json_select_object "$name"
-       json_add_string ifname "$iface"
-
-       if ! json_is_a protocol string || [ -n "$proto" ]; then
-               case "$proto" in
-                       static|dhcp|none|pppoe) : ;;
-                       *)
-                               case "$name" in
-                                       lan) proto="static" ;;
-                                       wan) proto="dhcp" ;;
-                                       *) proto="none" ;;
-                               esac
-                       ;;
-               esac
+       [ -z "$network" ] && return
+
+       json_select_object network
+       json_select_object "$network"
+
+       while [ -n "$1" ]; do
+               local opt=$1; shift
+               local val=$1; shift
+
+               [ -n "$opt" -a -n "$val" ] || break
 
-               json_add_string protocol "$proto"
+               [ "$opt" = "ifname" -a "$val" != "${val/ //}" ] && {
+                       json_select_array "ports"
+                       for e in $val; do json_add_string "" "$e"; done
+                       json_close_array
+               } || {
+                       json_add_string "$opt" "$val"
+               }
+       done
+
+       if ! json_is_a protocol string; then
+               case "$network" in
+                       lan) json_add_string protocol static ;;
+                       wan) json_add_string protocol dhcp ;;
+                       *) json_add_string protocol none ;;
+               esac
        fi
 
        json_select ..
+       json_select ..
 }
 
 ucidef_set_board_id() {
@@ -65,31 +72,45 @@ ucidef_set_model_name() {
        json_select ..
 }
 
-ucidef_set_interface_lan() {
-       json_select_object network
-       _ucidef_set_interface lan "$@"
+ucidef_set_compat_version() {
+       json_select_object system
+       json_add_string compat_version "${1:-1.0}"
        json_select ..
 }
 
+ucidef_set_interface_lan() {
+       ucidef_set_interface "lan" ifname "$1" protocol "${2:-static}"
+}
+
 ucidef_set_interface_wan() {
-       json_select_object network
-       _ucidef_set_interface wan "$@"
-       json_select ..
+       ucidef_set_interface "wan" ifname "$1" protocol "${2:-dhcp}"
 }
 
 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"
+       ucidef_set_interface_lan "$lan_if"
+       ucidef_set_interface_wan "$wan_if"
+}
+
+ucidef_set_bridge_device() {
+       json_select_object bridge
+       json_add_string name "${1:switch0}"
        json_select ..
 }
 
-ucidef_set_interface_raw() {
-       json_select_object network
-       _ucidef_set_interface "$@"
+ucidef_set_bridge_mac() {
+       json_select_object bridge
+       json_add_string macaddr "${1}"
+       json_select ..
+}
+
+ucidef_set_network_device_mac() {
+       json_select_object "network-device"
+       json_select_object "${1}"
+       json_add_string macaddr "${2}"
+       json_select ..
        json_select ..
 }
 
@@ -185,12 +206,25 @@ _ucidef_finish_switch_roles() {
                                        devices="${devices:+$devices }$device"
                                fi
                        json_select ..
-
-                       _ucidef_set_interface "$role" "$devices"
                json_select ..
+
+               ucidef_set_interface "$role" ifname "$devices"
        done
 }
 
+ucidef_set_ar8xxx_switch_mib() {
+       local name="$1"
+       local type="$2"
+       local interval="$3"
+
+       json_select_object switch
+               json_select_object "$name"
+                       json_add_int ar8xxx_mib_type $type
+                       json_add_int ar8xxx_mib_poll_interval $interval
+               json_select ..
+       json_select ..
+}
+
 ucidef_add_switch() {
        local name="$1"; shift
        local port num role device index need_tag prev_role
@@ -300,17 +334,14 @@ ucidef_set_interface_macaddr() {
        local network="$1"
        local macaddr="$2"
 
-       json_select_object network
-
-       json_select "$network"
-       [ $? -eq 0 ] || {
-               json_select ..
-               return
-       }
+       ucidef_set_interface "$network" macaddr "$macaddr"
+}
 
-       json_add_string macaddr "$macaddr"
-       json_select ..
+ucidef_set_label_macaddr() {
+       local macaddr="$1"
 
+       json_select_object system
+               json_add_string label_macaddr "$macaddr"
        json_select ..
 }
 
@@ -403,7 +434,7 @@ ucidef_set_led_gpio() {
 }
 
 ucidef_set_led_ide() {
-       _ucidef_set_led_trigger "$1" "$2" "$3" ide-disk
+       _ucidef_set_led_trigger "$1" "$2" "$3" disk-activity
 }
 
 ucidef_set_led_netdev() {
@@ -462,11 +493,13 @@ ucidef_set_led_switch() {
        local trigger_name="$4"
        local port_mask="$5"
        local speed_mask="$6"
+       local mode="$7"
 
        _ucidef_set_led_common "$1" "$2" "$3"
 
        json_add_string trigger "$trigger_name"
        json_add_string type switch
+       json_add_string mode "$mode"
        json_add_string port_mask "$port_mask"
        json_add_string speed_mask "$speed_mask"
        json_select ..
@@ -481,6 +514,7 @@ _ucidef_set_led_timer() {
 
        _ucidef_set_led_common "$1" "$2" "$3"
 
+       json_add_string type "$trigger_name"
        json_add_string trigger "$trigger_name"
        json_add_int delayon "$delayon"
        json_add_int delayoff "$delayoff"
@@ -569,7 +603,7 @@ ucidef_add_gpio_switch() {
        json_select_object gpioswitch
                json_select_object "$cfg"
                        json_add_string name "$name"
-                       json_add_int pin "$pin"
+                       json_add_string pin "$pin"
                        json_add_int default "$default"
                json_select ..
        json_select ..
@@ -611,6 +645,5 @@ board_config_update() {
 }
 
 board_config_flush() {
-       json_dump -i > /tmp/.board.json
-       mv /tmp/.board.json ${CFG}
+       json_dump -i -o ${CFG}
 }