2014-08-18 13:09:17 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
CFG=/etc/board.json
|
|
|
|
|
|
|
|
. /usr/share/libubox/jshn.sh
|
|
|
|
|
|
|
|
[ -f $CFG ] || exit 1
|
|
|
|
|
|
|
|
generate_static_network() {
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
delete network.loopback
|
|
|
|
set network.loopback='interface'
|
|
|
|
set network.loopback.ifname='lo'
|
|
|
|
set network.loopback.proto='static'
|
|
|
|
set network.loopback.ipaddr='127.0.0.1'
|
|
|
|
set network.loopback.netmask='255.0.0.0'
|
|
|
|
delete network.globals
|
|
|
|
set network.globals='globals'
|
|
|
|
set network.globals.ula_prefix='auto'
|
|
|
|
EOF
|
2015-12-04 09:56:55 +00:00
|
|
|
|
|
|
|
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
|
2014-08-18 13:09:17 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 22:27:06 +00:00
|
|
|
addr_offset=2
|
2014-08-18 13:09:17 +00:00
|
|
|
generate_network() {
|
2015-12-03 22:27:06 +00:00
|
|
|
local ifname macaddr protocol type
|
2014-08-18 13:09:17 +00:00
|
|
|
|
|
|
|
json_select network
|
2015-12-03 16:25:19 +00:00
|
|
|
json_select "$1"
|
2015-12-03 22:27:06 +00:00
|
|
|
json_get_vars ifname macaddr protocol
|
2015-12-03 16:25:19 +00:00
|
|
|
json_select ..
|
2014-08-18 13:09:17 +00:00
|
|
|
json_select ..
|
|
|
|
|
|
|
|
[ -n "$ifname" ] || return
|
2015-11-26 12:04:32 +00:00
|
|
|
|
2015-12-03 16:25:19 +00:00
|
|
|
# force bridge for multi-interface devices (and lan)
|
|
|
|
case "$1:$ifname" in
|
|
|
|
*\ * | lan:*) type="bridge" ;;
|
|
|
|
esac
|
2015-11-26 12:04:32 +00:00
|
|
|
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
delete network.$1
|
|
|
|
set network.$1='interface'
|
2015-12-03 16:25:19 +00:00
|
|
|
set network.$1.type='$type'
|
2015-11-26 12:04:32 +00:00
|
|
|
set network.$1.ifname='$ifname'
|
|
|
|
set network.$1.proto='none'
|
|
|
|
set network.$1.macaddr='$macaddr'
|
|
|
|
EOF
|
|
|
|
|
2015-12-03 22:27:06 +00:00
|
|
|
case "$protocol" in
|
|
|
|
static)
|
|
|
|
local ipaddr
|
|
|
|
case "$1" in
|
|
|
|
lan) ipaddr="192.168.1.1" ;;
|
|
|
|
*) ipaddr="192.168.$((addr_offset++)).1" ;;
|
|
|
|
esac
|
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
set network.$1.proto='static'
|
2015-12-03 22:27:06 +00:00
|
|
|
set network.$1.ipaddr='$ipaddr'
|
2015-11-26 12:04:32 +00:00
|
|
|
set network.$1.netmask='255.255.255.0'
|
|
|
|
set network.$1.ip6assign='60'
|
|
|
|
EOF
|
2014-08-18 13:09:17 +00:00
|
|
|
;;
|
2015-12-03 22:27:06 +00:00
|
|
|
|
|
|
|
dhcp)
|
|
|
|
# fixup IPv6 slave interface if parent is a bridge
|
|
|
|
[ "$type" = "bridge" ] && ifname="br-$1"
|
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
set network.$1.proto='dhcp'
|
2015-12-03 22:27:06 +00:00
|
|
|
delete network.${1}6
|
2015-12-04 09:56:55 +00:00
|
|
|
set network.${1}6='interface'
|
|
|
|
set network.${1}6.ifname='$ifname'
|
|
|
|
set network.${1}6.proto='dhcpv6'
|
|
|
|
EOF
|
|
|
|
;;
|
|
|
|
|
|
|
|
pppoe)
|
|
|
|
# fixup IPv6 slave interface
|
|
|
|
ifname="pppoe-$1"
|
|
|
|
|
|
|
|
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
|
2015-12-03 22:27:06 +00:00
|
|
|
set network.${1}6='interface'
|
|
|
|
set network.${1}6.ifname='$ifname'
|
|
|
|
set network.${1}6.proto='dhcpv6'
|
2015-11-26 12:04:32 +00:00
|
|
|
EOF
|
2014-08-18 13:09:17 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2015-12-03 16:25:19 +00:00
|
|
|
generate_switch_vlans_ports() {
|
|
|
|
local switch="$1"
|
|
|
|
local port ports role roles num attr val
|
2015-11-26 12:04:32 +00:00
|
|
|
|
2015-12-03 16:25:19 +00:00
|
|
|
#
|
|
|
|
# autogenerate vlans
|
|
|
|
#
|
|
|
|
|
|
|
|
json_get_keys roles roles
|
|
|
|
json_select roles
|
|
|
|
|
|
|
|
for role in $roles; do
|
|
|
|
json_select "$role"
|
|
|
|
json_get_vars ports
|
|
|
|
json_select ..
|
|
|
|
|
|
|
|
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
|
2014-08-18 13:09:17 +00:00
|
|
|
|
|
|
|
json_select ..
|
|
|
|
|
|
|
|
|
2015-12-03 16:25:19 +00:00
|
|
|
#
|
|
|
|
# write port specific settings
|
|
|
|
#
|
2015-11-24 21:16:38 +00:00
|
|
|
|
|
|
|
json_get_keys ports ports
|
|
|
|
json_select ports
|
|
|
|
|
|
|
|
for port in $ports; do
|
|
|
|
json_select "$port"
|
2015-12-03 16:25:19 +00:00
|
|
|
json_get_vars num
|
2015-11-24 21:16:38 +00:00
|
|
|
|
2015-12-03 16:25:19 +00:00
|
|
|
if json_is_a attr object; then
|
|
|
|
json_get_keys attr attr
|
|
|
|
json_select attr
|
2015-11-24 21:16:38 +00:00
|
|
|
uci -q batch <<-EOF
|
2015-12-03 16:25:19 +00:00
|
|
|
add network switch_port
|
|
|
|
set network.@switch_port[-1].device='$switch'
|
|
|
|
set network.@switch_port[-1].port=$num
|
2015-11-24 21:16:38 +00:00
|
|
|
EOF
|
|
|
|
|
2015-12-03 16:25:19 +00:00
|
|
|
for attr in $attr; do
|
|
|
|
json_get_var val "$attr"
|
|
|
|
uci -q set network.@switch_port[-1].$attr="$val"
|
|
|
|
done
|
|
|
|
json_select ..
|
2015-11-24 21:16:38 +00:00
|
|
|
fi
|
2015-12-03 16:25:19 +00:00
|
|
|
json_select ..
|
2015-11-24 21:16:38 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
json_select ..
|
|
|
|
}
|
|
|
|
|
2014-08-18 13:09:17 +00:00
|
|
|
generate_switch() {
|
2015-11-26 12:04:32 +00:00
|
|
|
local key="$1"
|
2014-08-18 13:09:17 +00:00
|
|
|
local vlans
|
|
|
|
|
|
|
|
json_select switch
|
2015-11-26 12:04:32 +00:00
|
|
|
json_select "$key"
|
2014-08-18 13:09:17 +00:00
|
|
|
json_get_vars enable reset blinkrate cpu_port
|
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
add network switch
|
|
|
|
set network.@switch[-1].name='$key'
|
|
|
|
set network.@switch[-1].reset='$reset'
|
|
|
|
set network.@switch[-1].enable_vlan='$enable'
|
|
|
|
set network.@switch[-1].blinkrate='$blinkrate'
|
|
|
|
EOF
|
2015-11-24 21:16:38 +00:00
|
|
|
|
2015-12-03 16:25:19 +00:00
|
|
|
generate_switch_vlans_ports "$1"
|
2015-11-24 21:16:38 +00:00
|
|
|
|
2014-08-18 13:09:17 +00:00
|
|
|
json_select ..
|
|
|
|
json_select ..
|
|
|
|
}
|
|
|
|
|
2015-11-26 12:04:43 +00:00
|
|
|
generate_rssimon() {
|
|
|
|
local key="$1"
|
|
|
|
local cfg="rssid_$key"
|
|
|
|
local refresh threshold
|
|
|
|
|
|
|
|
json_select rssimon
|
|
|
|
json_select "$key"
|
|
|
|
json_get_vars refresh threshold
|
|
|
|
json_select ..
|
|
|
|
json_select ..
|
|
|
|
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
delete system.$cfg
|
|
|
|
set system.$cfg='rssid'
|
|
|
|
set system.$cfg.dev='$key'
|
|
|
|
set system.$cfg.refresh='$refresh'
|
|
|
|
set system.$cfg.threshold='$threshold'
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2014-08-18 13:09:17 +00:00
|
|
|
generate_led() {
|
2015-11-26 12:04:32 +00:00
|
|
|
local key="$1"
|
2014-08-18 13:09:17 +00:00
|
|
|
local cfg="led_$key"
|
|
|
|
|
|
|
|
json_select led
|
2015-11-26 12:04:32 +00:00
|
|
|
json_select "$key"
|
2015-11-26 13:53:02 +00:00
|
|
|
json_get_vars name sysfs type trigger default
|
2014-08-18 13:09:17 +00:00
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
delete system.$cfg
|
|
|
|
set system.$cfg='led'
|
|
|
|
set system.$cfg.name='$name'
|
|
|
|
set system.$cfg.sysfs='$sysfs'
|
|
|
|
set system.$cfg.trigger='$trigger'
|
|
|
|
set system.$cfg.default='$default'
|
|
|
|
EOF
|
|
|
|
|
|
|
|
case "$type" in
|
2015-11-26 13:53:06 +00:00
|
|
|
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
|
|
|
|
;;
|
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
netdev)
|
2015-11-26 13:53:02 +00:00
|
|
|
local device
|
|
|
|
json_get_vars device
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
set system.$cfg.trigger='netdev'
|
|
|
|
set system.$cfg.mode='link tx rx'
|
2015-11-26 13:53:02 +00:00
|
|
|
set system.$cfg.dev='$device'
|
2015-11-26 12:04:32 +00:00
|
|
|
EOF
|
|
|
|
;;
|
2014-08-18 13:09:17 +00:00
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
usb)
|
2015-11-26 13:53:02 +00:00
|
|
|
local device
|
|
|
|
json_get_vars device
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
set system.$cfg.trigger='usbdev'
|
|
|
|
set system.$cfg.interval='50'
|
2015-11-26 13:53:02 +00:00
|
|
|
set system.$cfg.dev='$device'
|
2015-11-26 12:04:32 +00:00
|
|
|
EOF
|
|
|
|
;;
|
2015-11-26 12:04:43 +00:00
|
|
|
|
|
|
|
rssi)
|
2015-11-26 13:53:02 +00:00
|
|
|
local iface minq maxq offset factor
|
2015-11-26 12:04:43 +00:00
|
|
|
json_get_vars iface minq maxq offset factor
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set system.$cfg.trigger='rssi'
|
|
|
|
set system.$cfg.iface='rssid_$iface'
|
|
|
|
set system.$cfg.minq='$minq'
|
|
|
|
set system.$cfg.maxq='$maxq'
|
|
|
|
set system.$cfg.offset='$offset'
|
|
|
|
set system.$cfg.factor='$factor'
|
|
|
|
EOF
|
|
|
|
;;
|
2015-11-26 13:53:02 +00:00
|
|
|
|
|
|
|
switch)
|
|
|
|
local port_mask
|
|
|
|
json_get_vars port_mask
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set system.$cfg.port_mask='$port_mask'
|
|
|
|
EOF
|
|
|
|
;;
|
2015-12-03 22:27:02 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
;;
|
2014-08-18 13:09:17 +00:00
|
|
|
esac
|
2015-11-26 12:04:43 +00:00
|
|
|
|
|
|
|
json_select ..
|
|
|
|
json_select ..
|
2014-08-18 13:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json_init
|
|
|
|
json_load "$(cat ${CFG})"
|
|
|
|
|
|
|
|
generate_static_network
|
|
|
|
|
|
|
|
json_get_keys keys network
|
|
|
|
for key in $keys; do generate_network $key; done
|
|
|
|
|
|
|
|
json_get_keys keys switch
|
|
|
|
for key in $keys; do generate_switch $key; done
|
|
|
|
|
2015-11-26 12:04:43 +00:00
|
|
|
json_get_keys keys rssimon
|
|
|
|
for key in $keys; do generate_rssimon $key; done
|
|
|
|
|
2014-08-18 13:09:17 +00:00
|
|
|
json_get_keys keys led
|
|
|
|
for key in $keys; do generate_led $key; done
|
|
|
|
|
|
|
|
uci commit
|