set network.globals='globals'
set network.globals.ula_prefix='auto'
EOF
+
+ if json_is_a dsl object; then
+ json_select dsl
+ if json_is_a atmbridge object; then
+ json_select atmbridge
+ local vpi vci encaps payload
+ json_get_vars vpi vci encaps payload
+ uci -q batch <<-EOF
+ delete network.atm
+ set network.atm='atm-bridge'
+ set network.atm.vpi='$vpi'
+ set network.atm.vci='$vci'
+ set network.atm.encaps='$encaps'
+ set network.atm.payload='$payload'
+ EOF
+ json_select ..
+ fi
+
+ if json_is_a modem object; then
+ json_select modem
+ local type annex firmware tone xfer_mode
+ json_get_vars type annex firmware tone xfer_mode
+ uci -q batch <<-EOF
+ delete network.dsl
+ set network.dsl='$type'
+ set network.dsl.annex='$annex'
+ set network.dsl.firmware='$firmware'
+ set network.dsl.tone='$tone'
+ set network.dsl.xfer_mode='$xfer_mode'
+ EOF
+ json_select ..
+ fi
+ json_select ..
+ fi
}
-next_vlan=3
+addr_offset=2
generate_network() {
- local vlan
+ local ifname macaddr protocol type
json_select network
- json_select "$1"
- json_get_vars ifname create_vlan macaddr
- json_select ..
+ json_select "$1"
+ json_get_vars ifname macaddr protocol
+ json_select ..
json_select ..
[ -n "$ifname" ] || return
- if [ "${create_vlan:-0}" -eq 1 ]; then
- case "$1" in
- lan) vlan=1;;
- wan) vlan=2;;
- *)
- vlan=$next_vlan
- next_vlan=$((next_vlan + 1))
- ;;
- esac
- fi
-
- [ -n "$vlan" ] && ifname=${ifname}.${vlan}
+ # force bridge for multi-interface devices (and lan)
+ case "$1:$ifname" in
+ *\ * | lan:*) type="bridge" ;;
+ esac
uci -q batch <<-EOF
delete network.$1
set network.$1='interface'
+ set network.$1.type='$type'
set network.$1.ifname='$ifname'
- set network.$1.force_link=1
set network.$1.proto='none'
- set network.$1.macaddr='$macaddr'
EOF
- case "$1" in
- lan)
+ [ -n "$macaddr" ] && uci -q batch <<-EOF
+ delete network.$1_dev
+ set network.$1_dev='device'
+ set network.$1_dev.name='$ifname'
+ set network.$1_dev.macaddr='$macaddr'
+ EOF
+
+ case "$protocol" in
+ static)
+ local ipaddr
+ case "$1" in
+ lan) ipaddr="192.168.1.1" ;;
+ *) ipaddr="192.168.$((addr_offset++)).1" ;;
+ esac
+
uci -q batch <<-EOF
- set network.$1.type='bridge'
set network.$1.proto='static'
- set network.$1.ipaddr='192.168.1.1'
+ set network.$1.ipaddr='$ipaddr'
set network.$1.netmask='255.255.255.0'
set network.$1.ip6assign='60'
EOF
;;
- wan)
+
+ dhcp)
+ # fixup IPv6 slave interface if parent is a bridge
+ [ "$type" = "bridge" ] && ifname="br-$1"
+
uci -q batch <<-EOF
set network.$1.proto='dhcp'
- delete network.wan6
- set network.wan6='interface'
- set network.wan6.ifname='$ifname'
- set network.wan6.proto='dhcpv6'
+ delete network.${1}6
+ set network.${1}6='interface'
+ set network.${1}6.ifname='$ifname'
+ set network.${1}6.proto='dhcpv6'
EOF
;;
- esac
-}
-generate_switch_vlan() {
- local device="$1"
- local vlan="$2"
- local cpu_port="$3"
+ pppoe)
+ # fixup IPv6 slave interface
+ ifname="pppoe-$1"
- case "$vlan" in
- lan) vlan=1;;
- wan) vlan=2;;
- *) vlan="${vlan##vlan}";;
+ uci -q batch <<-EOF
+ set network.$1.proto='pppoe'
+ set network.$1.username='username'
+ set network.$1.password='password'
+ set network.$1.ipv6='auto'
+ delete network.${1}6
+ set network.${1}6='interface'
+ set network.${1}6.ifname='$ifname'
+ set network.${1}6.proto='dhcpv6'
+ EOF
+ ;;
esac
-
- json_select vlans
- json_select "$2"
- json_get_values ports
- json_select ..
- json_select ..
-
- uci -q batch <<-EOF
- add network switch_vlan
- set network.@switch_vlan[-1].device='$device'
- set network.@switch_vlan[-1].vlan='$vlan'
- set network.@switch_vlan[-1].ports='$ports ${cpu_port}t'
- EOF
}
-calculate_switch_vlans() {
+generate_switch_vlans_ports() {
local switch="$1"
- local ports port attr val
- local prev_role
- local num device role index
- local n_cpu=0 cpu0 cpu1 cpu2 cpu3
- local dev0 dev1 dev2 dev3 role0 role1 role2 role3
- local n_vlan=0 vlan_off=-1
- local vlan_ports cpu_port
+ local port ports role roles num attr val
- json_get_keys ports ports
-
- json_select ports
-
- # gather all cpu ports and count vlans
- for port in $ports; do
- json_select "$port"
- json_get_vars num device role
+ #
+ # autogenerate vlans
+ #
- if json_is_a attr object; then
- json_get_keys attr attr
- json_select attr
+ json_get_keys roles roles
+ json_select roles
- uci -q batch <<-EOF
- add network switch_port
- set network.@switch_port[-1].device='$switch'
- set network.@switch_port[-1].port=$num
- EOF
+ for role in $roles; do
+ json_select "$role"
+ json_get_vars ports
+ json_select ..
- for attr in $attr; do
- json_get_var val "$attr"
- uci -q set network.@switch_port[-1].$attr="$val"
- done
+ uci -q batch <<-EOF
+ add network switch_vlan
+ set network.@switch_vlan[-1].device='$switch'
+ set network.@switch_vlan[-1].vlan='$role'
+ set network.@switch_vlan[-1].ports='$ports'
+ EOF
+ done
- json_select ..
- fi
+ json_select ..
- json_select ..
- if [ -n "$num" ] && [ -n "$device" ]; then
- export "cpu$n_cpu=$num"
- export "dev$n_cpu=$device"
- n_cpu=$((n_cpu + 1))
- elif [ -n "$num" ] && [ -n "$role" ] && [ "$role" != "$prev_role" ]; then
- export "role$n_vlan=$role"
- n_vlan=$((n_vlan + 1))
- prev_role="$role"
- fi
- done
+ #
+ # write port specific settings
+ #
- unset prev_role
+ json_get_keys ports ports
+ json_select ports
- # autogenerate vlans
- for port in $ports ""; do
- if [ -n "$port" ]; then
- json_select "$port"
- json_get_vars num device role
- json_select ..
- else
- num="-"; role="-"
- fi
-
- if [ -n "$num" ] && [ -n "$role" ]; then
- if [ "$role" != "$prev_role" ]; then
- if [ -n "$vlan_ports" ]; then
- let cpu_port="cpu$((vlan_off % n_cpu))"
- [ $n_vlan -gt $n_cpu ] && cpu_port="${cpu_port}t"
+ for port in $ports; do
+ json_select "$port"
+ json_get_vars num
+ if json_is_a attr object; then
+ json_get_keys attr attr
+ json_select attr
uci -q batch <<-EOF
- add network switch_vlan
- set network.@switch_vlan[-1].device='$switch'
- set network.@switch_vlan[-1].vlan='$((vlan_off + 1))'
- set network.@switch_vlan[-1].ports='$vlan_ports $cpu_port'
+ add network switch_port
+ set network.@switch_port[-1].device='$switch'
+ set network.@switch_port[-1].port=$num
EOF
- fi
- vlan_off=$((vlan_off + 1))
- vlan_ports="$num"
- prev_role="$role"
- else
- vlan_ports="$vlan_ports $num"
+ for attr in $attr; do
+ json_get_var val "$attr"
+ uci -q set network.@switch_port[-1].$attr="$val"
+ done
+ json_select ..
fi
-
- fi
+ json_select ..
done
json_select ..
-
- # autogenerate interfaces
- vlan_off=0; while [ $vlan_off -lt $n_vlan ]; do
- eval role="\$role$((vlan_off))"
- eval device="\$dev$((vlan_off++ % n_cpu))"
- [ $n_vlan -gt $n_cpu ] && device="$device.$vlan_off"
-
- # quirk: append ifnames for subsequent switches
- case "$switch" in switch[1-9])
- local prev_devs="$(uci -q get "network.$role.ifname")"
- if echo "$prev_devs" | grep -wq "$device"; then
- device="$prev_devs"
- else
- device="$prev_devs $device"
- fi
- ;; esac
-
- uci -q batch <<-EOF
- set network.$role='interface'
- set network.$role.ifname='$device'
- EOF
-
- case $role in
- lan)
- uci -q batch <<-EOF
- set network.lan.type='bridge'
- set network.lan.proto='static'
- set network.lan.ipaddr='192.168.1.1'
- set network.lan.netmask='255.255.255.0'
- set network.lan.ip6assign='60'
- EOF
- ;;
-
- wan)
- uci -q batch <<-EOF
- set network.wan.proto='dhcp'
- set network.wan6='interface'
- set network.wan6.ifname='$device'
- set network.wan6.proto='dhcpv6'
- EOF
- ;;
-
- *)
- uci -q batch <<-EOF
- set network.$role.force_link='1'
- set network.$role.proto='none'
- EOF
- ;;
- esac
- done
}
generate_switch() {
set network.@switch[-1].blinkrate='$blinkrate'
EOF
- if [ -n "$cpu_port" ]; then
- json_get_keys vlans vlans
- for vlan in $vlans; do generate_switch_vlan $1 $vlan $cpu_port; done
- elif json_is_a ports array; then
- calculate_switch_vlans $1
- fi
+ generate_switch_vlans_ports "$1"
json_select ..
json_select ..
json_select led
json_select "$key"
- json_get_vars name sysfs type trigger device default port_mask
+ json_get_vars name sysfs type trigger default
uci -q batch <<-EOF
delete system.$cfg
set system.$cfg='led'
set system.$cfg.name='$name'
set system.$cfg.sysfs='$sysfs'
- set system.$cfg.dev='$device'
set system.$cfg.trigger='$trigger'
- set system.$cfg.port_mask='$port_mask'
set system.$cfg.default='$default'
EOF
case "$type" in
+ gpio)
+ local gpio inverted
+ json_get_vars gpio inverted
+ uci -q batch <<-EOF
+ set system.$cfg.trigger='gpio'
+ set system.$cfg.gpio='$gpio'
+ set system.$cfg.inverted='$inverted'
+ EOF
+ ;;
+
netdev)
+ local device
+ json_get_vars device
uci -q batch <<-EOF
set system.$cfg.trigger='netdev'
set system.$cfg.mode='link tx rx'
+ set system.$cfg.dev='$device'
EOF
;;
usb)
+ local device
+ json_get_vars device
uci -q batch <<-EOF
set system.$cfg.trigger='usbdev'
set system.$cfg.interval='50'
+ set system.$cfg.dev='$device'
EOF
;;
rssi)
- local minq maxq offset factor
+ local iface minq maxq offset factor
json_get_vars iface minq maxq offset factor
uci -q batch <<-EOF
set system.$cfg.trigger='rssi'
set system.$cfg.factor='$factor'
EOF
;;
+
+ switch)
+ local port_mask
+ json_get_vars port_mask
+ uci -q batch <<-EOF
+ set system.$cfg.port_mask='$port_mask'
+ EOF
+ ;;
+
+ timer)
+ local delayon delayoff
+ json_get_vars delayon delayoff
+ uci -q batch <<-EOF
+ set system.$cfg.trigger='timer'
+ set system.$cfg.delayon='$delayon'
+ set system.$cfg.delayoff='$delayoff'
+ EOF
+ ;;
esac
json_select ..
json_select ..
}
+generate_gpioswitch() {
+ local cfg="$1"
+
+ json_select gpioswitch
+ json_select "$cfg"
+ local name pin default
+ json_get_vars name pin default
+ uci -q batch <<-EOF
+ delete system.$cfg
+ set system.$cfg='gpio_switch'
+ set system.$cfg.name='$name'
+ set system.$cfg.gpio_pin='$pin'
+ set system.$cfg.default='$default'
+ EOF
+ json_select ..
+ json_select ..
+}
+
json_init
json_load "$(cat ${CFG})"
json_get_keys keys rssimon
for key in $keys; do generate_rssimon $key; done
+json_get_keys keys gpioswitch
+for key in $keys; do generate_gpioswitch $key; done
+
json_get_keys keys led
for key in $keys; do generate_led $key; done