#!/bin/sh

set_lan_mac_static_atl(){
	lanmac=$(uci get network.@interface[1].macaddr)	
	touch /tmp/lanmac
	oldlanmac=$(cat /tmp/lanmac)
	
    lanmac_inchar=${lanmac//:/}
    hight4byte=${lanmac_inchar:0:8}
    low2byte=${lanmac_inchar:8:4}
	
	ralink-switch clear
	
	local atlfun0=0x40071
	local atlfun1=0x0000${low2byte}
	local atlfun2=0x${hight4byte}
	
    swconfig dev switch0 reg set 0x3C ${atlfun2}
	swconfig dev switch0 reg set 0x38 ${atlfun1}
    swconfig dev switch0 reg set 0x34 ${atlfun0}
	
    echo ${lanmac} >  /tmp/lanmac
	
}

set_led_by_wan_number(){
	local wan_num=$1

	. /lib/switch/qca9533_register
	. /lib/switch/switch_functions

	#disable the CPU port MAC learning	
	swconfig_reg_write $PORT0_CONTROL 0x2204

	if [ ${wan_num} -eq 1 ] ;then
		echo 0 > /sys/class/gpio/gpio57/value
		echo 1 > /sys/class/gpio/gpio58/value
		echo 0 > /sys/class/gpio/gpio59/value
		echo 1 > /sys/class/gpio/gpio60/value
		echo 0 > /sys/class/gpio/gpio61/value
		echo 1 > /sys/class/gpio/gpio62/value

		#disable the WAN port MAC learning and enable LAN port MAC learning
		swconfig_reg_write $PORT1_CONTROL 0x2144
		swconfig_reg_write $PORT2_CONTROL 0x6104
		swconfig_reg_write $PORT3_CONTROL 0x6104
		swconfig_reg_write $PORT4_CONTROL 0x6104	
	elif [ ${wan_num} -eq 2 ] ;then
		echo 1 > /sys/class/gpio/gpio57/value
		echo 0 > /sys/class/gpio/gpio58/value
		echo 0 > /sys/class/gpio/gpio59/value
		echo 1 > /sys/class/gpio/gpio60/value
		echo 0 > /sys/class/gpio/gpio61/value
		echo 1 > /sys/class/gpio/gpio62/value

		swconfig_reg_write $PORT1_CONTROL 0x2144
		swconfig_reg_write $PORT2_CONTROL 0x2144
		swconfig_reg_write $PORT3_CONTROL 0x6104
		swconfig_reg_write $PORT4_CONTROL 0x6104	
	elif [ ${wan_num} -eq 3 ] ;then
		echo 1 > /sys/class/gpio/gpio57/value
		echo 0 > /sys/class/gpio/gpio58/value
		echo 1 > /sys/class/gpio/gpio59/value
		echo 0 > /sys/class/gpio/gpio60/value
		echo 0 > /sys/class/gpio/gpio61/value
		echo 1 > /sys/class/gpio/gpio62/value

		swconfig_reg_write $PORT1_CONTROL 0x2144
		swconfig_reg_write $PORT2_CONTROL 0x2144
		swconfig_reg_write $PORT3_CONTROL 0x2144
		swconfig_reg_write $PORT4_CONTROL 0x6104
	elif [ ${wan_num} -eq 4 ] ;then
		echo 1 > /sys/class/gpio/gpio57/value
		echo 0 > /sys/class/gpio/gpio58/value
		echo 1 > /sys/class/gpio/gpio59/value
		echo 0 > /sys/class/gpio/gpio60/value
		echo 1 > /sys/class/gpio/gpio61/value
		echo 0 > /sys/class/gpio/gpio62/value

		swconfig_reg_write $PORT1_CONTROL 0x2144
		swconfig_reg_write $PORT2_CONTROL 0x2144
		swconfig_reg_write $PORT3_CONTROL 0x2144
		swconfig_reg_write $PORT4_CONTROL 0x2144
	fi	
	
	set_lan_mac_static_atl
}


mt7628_set_led_by_wan_number()
{
	local wan_num=$1
	local val=0
	local reg=0	

	. /lib/switch/mt7628_register
	. /lib/switch/switch_functions
	
	#QAC9561: set port-vlan, isolate the Port1 and Port2
	#swconfig_reg_write $PORT1_BASED_VLAN 0x10010000
	#swconfig_reg_write $PORT2_BASED_VLAN 0x10020000
	#swconfig_reg_write $PORT1_BASED_VLAN2 0xc0030000
	#swconfig_reg_write $PORT2_BASED_VLAN2 0xc0050000

	#QAC9561: disable the CPU port MAC learning	
	#swconfig_reg_write $PORT0_CONTROL 0x2204

	#QAC9561: enable the Port1 and Port2 MAC learning, which connect to RTL8309N
	#swconfig_reg_write $PORT1_CONTROL 0x6204
	#swconfig_reg_write $PORT2_CONTROL 0x6204

	if [ ${wan_num} -eq 1 ] ;then
		#disable the WAN port MAC learning and enable LAN port MAC learning	
		#swconfig_reg_write $PORT5_CONTROL 0x2144		
		#swconfig_reg_write $PORT4_CONTROL 0x6104
		#swconfig_reg_write $PORT3_CONTROL 0x6104
		echo "wan num 1"
	elif [ ${wan_num} -eq 2 ] ;then		
		#swconfig_reg_write $PORT5_CONTROL 0x2144
		#swconfig_reg_write $PORT4_CONTROL 0x2144
		#swconfig_reg_write $PORT3_CONTROL 0x6104	
		echo "wan num 2"
	elif [ ${wan_num} -eq 3 ] ;then
		#swconfig_reg_write $PORT5_CONTROL 0x2144
		#swconfig_reg_write $PORT4_CONTROL 0x2144
		#swconfig_reg_write $PORT3_CONTROL 0x2144
		echo "wan num 3"
	elif [ ${wan_num} -eq 4 ] ;then
		#swconfig_reg_write $PORT5_CONTROL 0x2144
		#swconfig_reg_write $PORT4_CONTROL 0x2144
		#swconfig_reg_write $PORT3_CONTROL 0x2144
		echo "wan num 4"
	fi
}


mt7628_rtl8309n_set_led_by_wan_number()
{
	local wan_num=$1
	local val=0
	local reg=0	

	. /lib/switch/mt7628_rtl8309n_register
	. /lib/switch/switch_functions
	
	#QAC9561: set port-vlan, isolate the Port1 and Port2
	#swconfig_reg_write $PORT1_BASED_VLAN 0x10010000
	#swconfig_reg_write $PORT2_BASED_VLAN 0x10020000
	#swconfig_reg_write $PORT1_BASED_VLAN2 0xc0030000
	#swconfig_reg_write $PORT2_BASED_VLAN2 0xc0050000

	#QAC9561: disable the CPU port MAC learning	
	#swconfig_reg_write $PORT0_CONTROL 0x2204

	#mt7628: enable the Port3 and Port4
	swconfig dev switch0 phy_reg set 3 0-0x2100
	swconfig dev switch0 phy_reg set 4 0-0x2100

	if [ ${wan_num} -eq 1 ] ;then
		swconfig dev switch0 port 3 set pvid 3
		swconfig dev switch0 port 4 set pvid 2
		swconfig dev switch0 set apply
		swconfig dev switch1 port 0 set pvid 2
		swconfig dev switch1 port 1 set pvid 3
		swconfig dev switch1 set apply
		
		echo "wan num 1"
	elif [ ${wan_num} -eq 2 ] ;then		
		swconfig dev switch0 port 3 set pvid 3
		swconfig dev switch0 port 4 set pvid 2
		swconfig dev switch0 set apply
		swconfig dev switch1 port 0 set pvid 2
		swconfig dev switch1 port 1 set pvid 3
		swconfig dev switch1 set apply
		echo "wan num 2"
	elif [ ${wan_num} -eq 3 ] ;then
		swconfig dev switch0 port 3 set pvid 3
		swconfig dev switch0 port 4 set pvid 2
		swconfig dev switch0 set apply
		swconfig dev switch1 port 0 set pvid 2
		swconfig dev switch1 port 1 set pvid 3
		swconfig dev switch1 set apply
		echo "wan num 3"
	elif [ ${wan_num} -eq 4 ] ;then
		#swconfig_reg_write $PORT5_CONTROL 0x2144
		#swconfig_reg_write $PORT4_CONTROL 0x2144
		#swconfig_reg_write $PORT3_CONTROL 0x2144
		echo "wan num 4"
	fi
	
	#RTL8309N open port0 & port1
	swconfig dev switch1 phy_reg set 0 0-0x2100
	swconfig dev switch1 phy_reg set 1 0-0x2100
	
	#enable RTL8309N port MAC learning
	#arg: reg_address-value-page_address
	switch_dev="switch1"
	val=`swconfig_phy_reg_read_switch $switch_dev 2 0x10-0x5`	
	val=`echo $(($val | 0x1)) | awk '{printf("%x"), $0}'`
	swconfig_phy_reg_write_switch $switch_dev 2 0x10-$val-0x5

	val=`swconfig_phy_reg_read_switch $switch_dev 3 0x10-0x5`
	val=`echo $(($val | 0x1)) | awk '{printf("%x"), $0}'`
	swconfig_phy_reg_write_switch $switch_dev 3 0x10-$val-0x5

	val=`swconfig_phy_reg_read_switch $switch_dev 4 0x10-0x5`
	val=`echo $(($val | 0x1)) | awk '{printf("%x"), $0}'`
	swconfig_phy_reg_write_switch $switch_dev 4 0x10-$val-0x5

	val=`swconfig_phy_reg_read_switch $switch_dev 5 0x10-0x5`
	val=`echo $(($val | 0x1)) | awk '{printf("%x"), $0}'`
	swconfig_phy_reg_write_switch $switch_dev 5 0x10-$val-0x5

	val=`swconfig_phy_reg_read_switch $switch_dev 6 0x10-0x5`
	val=`echo $(($val | 0x1)) | awk '{printf("%x"), $0}'`
	swconfig_phy_reg_write_switch $switch_dev 6 0x10-$val-0x5
}

