#!/bin/sh

TMP_CFG="switch"

dec2hex(){
    printf "0x%x" $1
}

# read switch reg
# $1:reg address
swconfig_reg_read()
{
	val=`swconfig dev switch0 reg get $1`
	tmp=${val##*\[}
	echo ${tmp%%\]*}
}

swconfig_reg_write()
{
	val=`swconfig dev switch0 reg set $1 $2 2>/dev/null`
}

swconfig_phy_reg_read()
{
	val=`swconfig dev switch0 phy_reg get $(dec2hex $1) $2`
	tmp=${val##*\[}
	echo ${tmp%%\]*}
}

swconfig_phy_reg_write()
{
	swconfig dev switch0 phy_reg set $(dec2hex $1) $2-$3
}


# read switch reg
# $1:switch id
# $2:reg address
swconfig_reg_read_switch()
{
	val=`swconfig dev $1 reg get $2`
	tmp=${val##*\[}
	echo ${tmp%%\]*}
}

# $1:switch id
# $2:reg address
# $3:reg value
swconfig_reg_write_switch()
{
	val=`swconfig dev $1 reg set $2 $3 2>/dev/null`
}

# $1:switch id
# $2:phy_index
# $3:reg offset
swconfig_phy_reg_read_switch()
{
	val=`swconfig dev $1 phy_reg get $(dec2hex $2) $3`
	tmp=${val##*\[}
	echo ${tmp%%\]*}
}

# $1:switch id
# $2:phy_index
# $3:reg offset
# $3:reg value
swconfig_phy_reg_write_switch()
{
	swconfig dev $1 phy_reg set $(dec2hex $2) $3-$4
}

# read switch type
swconfig_switch_type_read()
{
	val=`swconfig dev switch0 help`
	tmp=${val%%)*}
	echo ${tmp##*(} | cut -d " " -f 2
}

# $1:switch id
swconfig_switch_type_read_switch()
{
	val=`swconfig dev $1 help`
	tmp=${val%%)*}
	echo ${tmp##*(} | cut -d " " -f 2
}

swconfig_port_info()
{
	val=`swconfig dev switch0 port 1 show 2>/dev/null`
}

# clear all mibs counter
swconfig_reset_all_mibs()
{
	echo "flush all" >>/tmp/test.lua
	val=`swconfig dev switch0 set reset_mibs 2>/dev/null`
}

# clear port mib counter
# $1:portid
swconfig_reset_port_mib()
{
	local switch_id
	switch_id=`get_switch_dev $1`
	if [ -z "$switch_id" ]; then
		switch_id="switch0"
	fi

	val=`swconfig dev $switch_id port $1 set reset_mib 2>/dev/null`
}

# get port mib counter
# $1:portid
swconfig_get_port_mib()
{
	local switch_id
	local portid
	switch_id=`get_switch_dev $1`
	if [ -z "$switch_id" ]; then
		switch_id="switch0"
	fi
	portid=`get_phy_index $1`
	swconfig dev $switch_id port $portid get mib
	#val=`swconfig dev switch0 port $1 get mib`
	#echo $val
}


# set bit $2 of $1 to $3
set_bit_value()
{
	local value=$1
	local bit=$2
	local val=$3
	local tmp=1

	echo $(($(($value|$(($tmp << $bit)))) & $(($(($val << $bit)) ^ $((~$(($tmp << $bit))))))))

}

# get bit $2 of $1
get_bit_value()
{
	local value=$1
	local bit=$2
	local tmp=1

	echo $(($(($(($tmp << $bit)) & $value)) >> $bit))
}

#$1:port ID
#for Realtek 8370MB, like as WVR458/WAR458
port_map_manual()
{
	local phy_id

	case $1 in
		"1")
			phy_id=1
			;;
		"2")
			phy_id=0
			;;
		"3")
			phy_id=3
			;;
		"4")
			phy_id=2
			;;
		"5")	
			phy_id=5
			;;
		"6")
			phy_id=4		
			;;
		"7")
			phy_id=7
			;;
		"8")
			phy_id=6
			;;
		"9")
			phy_id=9
			;;
		*)		
			;;
	esac

	echo "$phy_id"
}

# get physical port ID
# $1:port_id
get_phy_port_id()
{
	local phy_id=0
	local section="port"$1

	phy_id=`uci get switch.port_map.$section 2>/dev/null`

	# unlikely...
	if [ -z "$phy_id" ];then
		#wvr450/wvr900/wvr1300...
		phy_id=$(($1-1))		
	fi

	echo $phy_id
}

# get PHY index
# $1: port_id
get_PHY_index()
{
	local phy_index=0
	local section="port"$1

	phy_index=`uci get switch.phy_map.$section 2>/dev/null`

	echo $phy_index
}

# get switch dev
# $1: port_id
get_switch_dev()
{
	local switch_dev
	local section="port"$1

	switch_dev=`uci get switch.$section.switch_dev 2>/dev/null`

	echo $switch_dev
}

# get phy_index
# $1: port_id
get_phy_index()
{
	local phy_index
	local section="port"$1
	if [ -f "/etc/config/switch_new" ]; then
		phy_index=`uci get switch_new.$section.phy_index 2>/dev/null`
	else
		phy_index=`uci get switch.$section.phy_index 2>/dev/null`
	fi

	echo $phy_index
}

# get physical port ID
# $1:port_id
get_port_index()
{
	local port_index
	local section="port"$1
	if [ -f "/etc/config/switch_new" ]; then
		port_index=`uci get switch_new.$section.port_id 2>/dev/null`
	else
		port_index=`uci get switch.$section.port_id 2>/dev/null`
	fi

	echo $port_index
}

vlan_config_sync()
{
	echo vlan_config_sync > /dev/console
	switch_num=`uci get switch.global.switch_num`
	for i in `seq 1 $switch_num`
	do
		if [ -f "/etc/config/vlan_new" ]; then
			port_list=`uci get vlan_new.switch0.port_list`
		else
			cmd="uci get vlan$(($i-1)).switch$(($i-1)).port_list"
			port_list=`$cmd`
		fi
		echo $port_list
	    for j in $port_list
	    do
	    	echo "j:$j"
			if [ -f "/etc/config/vlan_new" ]; then
				lport=`uci get vlan_new.port$j.logical`
			else
				cmd="uci get vlan$(($i-1)).port$j.logical"
				lport=`$cmd`
				echo $lport 
				if [ $lport == "none" ]; then
					break
				fi
			fi
	        cmd="uci get lports.port$lport.pvid"
	        pvid=`$cmd`
	        echo $pvid
	        uci set vlan$(($i-1)).port$j.pvid=$pvid
	        cmd="uci get lports.port$lport.pvid"
	        pvid=`$cmd`
	        echo $pvid
	        uci set vlan$(($i-1)).port$j.pvid=$pvid
	    done

	    config_load "vlan$(($i-1))"

	    _delete_vlan()
	    {
	    	config_get name "$1" name
	    	echo "delete $name"
	    	uci delete vlan$(($i-1)).$name
	    }
	    config_foreach _delete_vlan switch_vlan
		config_foreach _delete_vlan switch_child_vlan

	    swconfig dev switch0 set clear_vlan_wan 1

	    config_load lports

	    _add_vlan()
	    {
	    	config_get name "$1" name
	    	echo "$name"
	    	uci set vlan$(($i-1)).$name=switch_vlan
	    	uci set vlan$(($i-1)).$name.name=$name
	    	config_get vlan "$1" vlan
	    	uci set vlan$(($i-1)).$name.vlan=$vlan	
	    	config_get desc "$1" desc
	    	echo "$desc"
	    	uci set vlan$(($i-1)).$name.desc=$desc
	    	config_get users "$1" users
	    	echo "$users"
	    	if [ "$users" == "wan" ]; then
	    		swconfig dev switch0 set vlan_wan $vlan
	    	fi
	    	uci set vlan$(($i-1)).$name.users=$users
	    	config_get ports "$1" ports
	    	echo "ports:$ports"
	    	vlan_port=""
	    	for k in $ports
	    	do
				len=${#k}
	    		tag=${k:$(($len-1)):1}
	    		echo "taqg:$tag" 
	    		if [ "$tag" == "t" ]; then
	    			k=${k:0:$(($len-1))}
	    		fi
	    		cmd="get_phy_index $k"
	    		echo $cmd
	    		phy_port=`$cmd`
	    		if [ "$tag" == "t" ]; then
	    			phy_port="$phy_port""t"
	    		fi
	    		echo "phy_port:$phy_port"    		
	    		vlan_port="$vlan_port $phy_port"
	    	done

	    	echo $vlan_port
			if [ $vlan != 0 ]; then
				uci set vlan$(($i-1)).$name.ports="$vlan_port"
			fi
	    	uci set vlan$(($i-1)).$name.device="switch$((i-1))"
	    }

		_add_child_vlan()
		{
			config_get name "$1" name
			uci set vlan$(($i-1)).$name=switch_child_vlan
			uci set vlan$(($i-1)).$name.name=$name
			config_get vlan "$1" vlan
			uci set vlan$(($i-1)).$name.vlan=$vlan
			config_get parent "$1" parent
			uci set vlan$(($i-1)).$name.parent=$parent
			swconfig dev switch0 set vlan_wan $vlan
			config_get ports "$1" ports
			vlan_port=""
			for k in $ports
			do
				len=${#k}
				tag=${k:$(($len-1)):1}
				if [ "$tag" == "t" ]; then
					k=${k:0:$(($len-1))}
				fi
				cmd="get_phy_index $k"
				phy_port=`$cmd`
				if [ "$tag" == "t" ]; then
					phy_port="$phy_port""t"
				fi
				vlan_port="$vlan_port $phy_port"
			done

			if [ $vlan != 0 ]; then
				uci set vlan$(($i-1)).$name.ports="$vlan_port"
			fi
			uci set vlan$(($i-1)).$name.device="switch$((i-1))"
		}

	    config_foreach _add_vlan switch_vlan
		config_foreach _add_child_vlan switch_child_vlan
	    uci commit vlan$(($i-1))
	done
}

vlan_config_sync_8411c()
{
	echo vlan_config_sync > /dev/console
	switch_num=`uci get switch.global.switch_num`
	for i in `seq 1 $switch_num`
	do
		if [ -f "/etc/config/vlan_new" ]; then
			port_list=`uci get vlan_new.switch0.port_list`
		else
			cmd="uci get vlan0.switch$(($i-1)).port_list"
			port_list=`$cmd`
		fi
		echo $port_list
	    for j in $port_list
	    do
	    	echo "j:$j"
			if [ -f "/etc/config/vlan_new" ]; then
				lport=`uci get vlan_new.port$j.logical`
			else
				cmd="uci get vlan0.port$j.logical"
				lport=`$cmd`
				echo $lport 
				if [ $lport == "none" ]; then
					break
				fi
			fi
	        cmd="uci get lports.port$lport.pvid"
	        pvid=`$cmd`
	        echo $pvid
	        uci set vlan0.port$j.pvid=$pvid
	        cmd="uci get lports.port$lport.pvid"
	        pvid=`$cmd`
	        echo $pvid
	        uci set vlan0.port$j.pvid=$pvid
	    done

	    config_load vlan0

	    _delete_vlan()
	    {
	    	config_get name "$1" name
	    	echo "delete $name"
	    	uci delete vlan0.$name
	    }
	    config_foreach _delete_vlan switch_vlan

	    swconfig dev switch0 set clear_vlan_wan 1

	    config_load lports

	    _add_vlan()
	    {
	    	config_get name "$1" name
	    	echo "$name"
	    	uci set vlan0.$name=switch_vlan
	    	uci set vlan0.$name.name=$name
	    	config_get vlan "$1" vlan
	    	uci set vlan0.$name.vlan=$vlan	
	    	config_get desc "$1" desc
	    	echo "$desc"
	    	uci set vlan0.$name.desc=$desc
	    	config_get users "$1" users
	    	echo "$users"
	    	if [ "$users" == "wan" ]; then
	    		swconfig dev switch0 set vlan_wan $vlan
	    	fi
	    	uci set vlan0.$name.users=$users
	    	config_get ports "$1" ports
	    	echo "ports:$ports"
	    	vlan_port=""
	    	for k in $ports
	    	do
				len=${#k}
	    		tag=${k:$(($len-1)):1}
	    		echo "taqg:$tag" 
	    		if [ "$tag" == "t" ]; then
	    			k=${k:0:$(($len-1))}
	    		fi
	    		cmd="get_phy_index $k"
	    		echo $cmd
	    		phy_port=`$cmd`
	    		if [ "$tag" == "t" ]; then
	    			phy_port="$phy_port""t"
	    		fi
	    		echo "phy_port:$phy_port"    		
	    		vlan_port="$vlan_port $phy_port"
	    	done

	    	echo $vlan_port
			if [ $vlan != 0 ]; then
				uci set vlan0.$name.ports="$vlan_port"
			fi
	    	uci set vlan0.$name.device="switch$((i-1))"
	    }
	    config_foreach _add_vlan switch_vlan
	    uci commit vlan0
	done
}

switch_config_sync()
{
	echo switch_config_sync > /dev/console
	config_load switch
	edit_switch()
	{
		if [ "$1" == "mirror" ]; then
			cmd="uci get lports.$1.mode"
			mode=`$cmd`
			uci set switch.$1.mode=$mode
			
			cmd="uci get lports.$1.mirrorport"
			mirrorport=`$cmd`
			uci set switch.$1.mirrorport=$mirrorport
			
			cmd="uci get lports.$1.state"
			state=`$cmd`
			uci set switch.$1.state=$state
			
			cmd="uci get lports.$1.mirroredports"
			mirroredports=`$cmd`
			uci set switch.$1.mirroredports="$mirroredports"
			return
		fi
		echo $1
		cmd="get_phy_index $1"
		phy_index=`$cmd`
		if [ "$phy_index" == "none" ]; then
			continue
		fi
		cmd="uci get lports.$1.port_state"
		port_state=`$cmd`
		echo $port_state
		uci set switch.$1.port_state=$port_state

		cmd="uci get lports.$1.flowcontrol"
		flowcontrol=`$cmd`
		echo $flowcontrol
		uci set switch.$1.flowcontrol=$flowcontrol

		cmd="uci get lports.$1.negotiation_mode"
		negotiation_mode=`$cmd`
		echo $negotiation_mode
		uci set switch.$1.negotiation_mode=$negotiation_mode

		cmd="uci get lports.$1.ingress_state"
		ingress_state=`$cmd`
		echo $ingress_state
		uci set switch.$1.ingress_state=$ingress_state

		cmd="uci get lports.$1.ingress_mode"
		ingress_mode=`$cmd`
		echo $ingress_mode
		uci set switch.$1.ingress_mode=$ingress_mode

		cmd="uci get lports.$1.ingress_rate"
		ingress_rate=`$cmd`
		echo $ingress_rate
		uci set switch.$1.ingress_rate=$ingress_rate

		cmd="uci get lports.$1.egress_state"
		egress_state=`$cmd`
		echo $egress_state
		uci set switch.$1.egress_state=$egress_state

		cmd="uci get lports.$1.egress_rate"
		egress_rate=`$cmd`
		echo $egress_rate
		uci set switch.$1.egress_rate=$egress_rate

		cmd="uci get lports.$1.isolation"
		isolation=`$cmd`
		echo $isolation
		uci set switch.$1.isolation=$isolation

		cmd="uci get lports.$1.portpri"
		portpri=`$cmd`
		echo $portpri
		uci set switch.$1.portpri=$portpri


	}
	config_foreach edit_switch port 
	uci commit switch
}

