#!/bin/sh

# mt753x port mirror operations===>>>>
# 1) mtk-switch mirror monitor [port]                                 - enable port mirror and indicate monitor port number
# 2) mtk-switch mirror target  [port]                                 - set port mirror target
#                       [direction| 0:off, 1:rx, 2:tx, 3:all]
# 3) mtk-switch mirror enable [mirror_en:0|1] [mirror_port: 0-6]      - set switch mirror function enable(1) or disabled(0) for port 0~6
# 4) mtk-switch mirror port-based [port] [port_tx_mir:0|1]            - set switch mirror port: target tx/rx/acl/vlan/igmp
#                                [port_rx_mir:0|1] [acl_mir:0|1]
#                                [vlan_mis:0|1] [igmp_mir:0|1]



# clear port mirror
clear_port_mirror()
{
	for i in `seq $port_first $port_last`; do
                local port=`get_port_index $i`
        #       echo "$port" >/dev/console
                if [ "$port" = "none" ]; then
                        continue
                fi
#       echo "mtk-switch mirror enable 0 $port">/dev/console
#       mtk-switch mirror enable 0 $port
        #echo "swconfig dev switch0 port $port set mirrors """ >/dev/console
        swconfig dev switch0 port $port set mirrors ""
        done

}

# enable port mirror
# $1:mirror mode
# $2:mirror port id
# $3:mirrored port list
enable_port_mirror()
{
		local mode=$1
        local mirroredPortlist=$3
        local portid=0
        local mport_id=0
        local mirrorCmd=""
        local setable_ports=""

        if [ $2 -lt $port_first ] || [ $2 -gt $port_last ];then
            echo "[mirror] port is error." > /dev/console
            return
        fi
        for i in $mirroredPortlist; do
            if [ $i -lt $port_first ] || [ $i -gt $port_last ] || [ $i -eq $2 ];then
                echo "[mirror] mirrored port $i is error." > /dev/console
            else
                setable_ports="$setable_ports""$i "
            fi
        done
        if [ "$setable_ports" != "" ]; then
            mirroredPortlist="$setable_ports"
        else
            return
        fi

        clear_port_mirror

        mport_id=`get_port_index $2`
        if [ "$mport_id" = "none" ]; then
                return
        fi
        #set port control register - mirror port
        #echo "$mirroredPortlist">/dev/console
        for i in $mirroredPortlist; do
                portid=`get_port_index $i`
                if [ "$portid" = "none" ]; then
                        continue
                fi
                if [ $mode = "ingress" ]; then
                        mirrorCmd="${mirrorCmd}"" ""${portid}"
                elif [ $mode = "egress" ]; then
                        mirrorCmd="${mirrorCmd}"" ""${portid}""t"
                elif [ $mode = "both" ]; then
                        mirrorCmd="${mirrorCmd}"" ""${portid}"" ""${portid}""t"
                fi
        done
        #echo "swconfig dev switch0 port $mport_id set mirrors "$mirrorCmd"">/dev/console
        swconfig dev switch0 port $mport_id set mirrors "$mirrorCmd"

}

# init tplink switch port mirror
# $1: state
# $2: mirror mode
# $3: mirror port
# $4: mirrored ports
tplink_switch_port_mirror_init()
{
	if [ "$1" = "on" ]; then
		enable_port_mirror $2 $3 "$4"
	elif [ "$1" = "off" ]; then
		clear_port_mirror
	else
		echo "[mirror] state is invalid.(on | off)"
		exit 0
	fi
}

print_mirror_info()
{
	config_get state "mirror" state     
	config_get mode "mirror" mode  
	config_get mirrorport "mirror" mirrorport
	config_get mirroredports "mirror" mirroredports

	echo "Mirror state   :  $state" 
	echo "Mirror mode    :  $mode"
	echo "Mirror port    :  $mirrorport"
	echo "Mirrored ports :  $mirroredports"
	echo ""
}

# check mode mirror port and mirrored portlist
# exit when arg is invalid
# $1: mode
# $2: mirror port
# $3: mirrored ports
mirror_arg_check()
{
	local mode=$1
	local portid=$2
	local ports=$3
	
    if [ "$mode" != "" ] && [ "$mode" != "ingress" ] && [ "$mode" != "egress" ] && [ "$mode" != "both" ]; then
    	echo "[mirror] mode is error. (ingress | egress | both)"
        mirror_usage
        exit 0 
    fi 

    # check input portid
	if [ -z $portid ] || [ `expr match $portid "[0-9]"` = 0 ] || [ $portid -lt $port_first ] || [ $portid -gt $port_last ];then
		echo "[mirror] port is error."
		mirror_usage
		exit 0
	fi

	if [ -z "$ports" ]; then
		echo "[mirror] mirrored port is null."
		mirror_usage
		exit 0
	fi

	for i in $ports; do		
		if [ `expr match $i "[0-9]"` = 0 ] || [ $i -lt $port_first ] || [ $i -gt $port_last ] || [ $i -eq $portid ];then
			echo "[mirror] mirrored port is error."
			mirror_usage
			exit 0
		fi
	done
}

# tplink switch port mirror
# $1:op (get|set)
# $2:state (on | off)
# $3:mirror mode (ingress | egress | both)
# $4:mirror port id (2 | 3 | 4 )
# $5:mirrored port list (2.3.4..)
tplink_switch_port_mirror()
{
	local op=$1
	local state=$2
	local mode=$3
	local portid=$4
	local mirroredPorts=$5	

	#get port mirror info
	if [ "$op" = "get" ]; then
		print_mirror_info
	elif [ "$op" = "set" ]; then
		if [ "$state" = "off" ]; then
			clear_port_mirror
		elif [ "$state" = "on" ]; then
			mirror_arg_check $mode $portid "$mirroredPorts"
			enable_port_mirror $mode $portid "$mirroredPorts"
		else
			echo "[mirror] state is invalid.(on | off)"
			exit 0
		fi

	else
		echo "[mirror] error op. (set | get)"
	fi
}