#!/bin/bash 

#------------------------------------------------------------------------
# September, 2014, Balaji Krishnamoorthy
# Copyright (c) 2014-2015, 2019 by Cisco Systems, Inc.
# All Rights Reserved
#------------------------------------------------------------------------

if [[ $# -ne 3 ]]; then
    echo ""
    echo "  USAGE : $0 link param value"
    echo "    receive-link is provided as R/S/A/L where"
    echo "          R : Rack range LCC:0 to 15, FCC:F0 to F3"
    echo "          S : LC slot range: 0 to 7, FC slot range LCC:FC0 to FC5, FCC:FC0 to FC11"
    echo "          A : SFE Asic range L-FC1:0 to 1, L-FC-M:0 to 3, FFC:0 to 2"
    echo "          L : FIA link number range 0 to 35, SFE link number range 0 to 127."
    echo ""
    echo "    param is one of the following : "
    echo ""
    echo "      Receiver (RX):"
    echo "       Peak Filter, use the control:"
    echo "        BCM_PORT_PHY_CONTROL_RX_PEAK_FILTER "
    echo "       VGA, use the control:"
    echo "        BCM_PORT_PHY_CONTROL_RX_VGA"
    echo "       DFE-Taps, use the control:"
    echo "        BCM_PORT_PHY_CONTROL_RX_TAP1"
    echo "        BCM_PORT_PHY_CONTROL_RX_TAP2"
    echo "        BCM_PORT_PHY_CONTROL_RX_TAP3"
    echo "        BCM_PORT_PHY_CONTROL_RX_TAP4"
    echo "        BCM_PORT_PHY_CONTROL_RX_TAP5"
    echo "       Slicer control (Typically should not be overridden), use the control:"
    echo "        BCM_PORT_PHY_CONTROL_RX_PLUS1_SLICER"
    echo "        BCM_PORT_PHY_CONTROL_RX_MINUS1_SLICER"
    echo "        BCM_PORT_PHY_CONTROL_RX_D_SLICER"
    echo ""
    echo "      Transmitter (TX):"
    echo "       Preemphasis, use the control:"
    echo "        BCM_PORT_PHY_CONTROL_PREEMPHASIS"
    echo "       Amplitude (Driver Current), use the control:"
    echo "        BCM_PORT_PHY_CONTROL_DRIVER_CURRENT"
    echo "        BCM_PORT_PHY_CONTROL_PRE_DRIVER_CURRENT"
    echo "        BCM_PORT_PHY_CONTROL_DRIVER_POST2_CURRENT"
    echo ""
    echo "    value is the desired value of param."
    exit 1
fi

MYADDR=$(ifconfig eth-vf1.3073 | grep 'inet addr:' | awk '{print $2}' | awk -F: '{print $2}')

CONFDADDR=$(ds_test_client -l confd6 | tail -1 | awk -F, '{print $2}' | awk -F= '{print $2}')

if [[ "$MYADDR" != "$CONFDADDR" ]]; then
    echo -n "Confd not active on this node: Run this command from VRF 0 of " >& 2
    H=$(ssh -q $CONFDADDR hostname)
    echo "$H" >& 2
    exit 1
fi

thislink=$1
param=$2
value=$3

create_coeff_cint() {
    [ -f /tmp/cint_coeff.c ] && rm -f /tmp/cint_coeff.c

cat > /tmp/cint_coeff.c <<-EOF
    cint_reset();
    int     rc;
    int     status;
    int     unit    = $1;
    int     port    = $2;
    int     parm    = $3;
    int     val     = $4;
    char    cmd[256];
  
    sprintf(cmd, "log file=/tmp/coeff_$1_$2_$3_$4 on", unit);
    bshell(unit, cmd);
    
    rc = bcm_port_phy_control_set(unit, port, $3, $4);
    sprintf(cmd, "\n echo Unit:%d, Port:%d, $3, $4, rc %x", unit, port, rc);
    bshell(unit, cmd);

    sprintf(cmd, "log file=/tmp/coeff_$1_$2_$3_$4 off", unit);
    bshell(unit, cmd);
    
EOF

}

feid=$(parselink -e $thislink)
unit=$(parselink -u $thislink)
asic=$(parselink -a $thislink)
rack=$(parselink -r $thislink)
slot=$(parselink -s $thislink)
sfi=$(parselink -p $thislink)
driver_ip=$(parselink -i $thislink)

create_coeff_cint $unit $sfi $param $value
scp /tmp/cint_coeff.c root@${driver_ip}:/tmp

if [[ $feid -lt 768 ]]; then
    ssh $driver_ip /pkg/bin/fia_test diag_shell -u $unit -C \"cd /tmp/\" >& /dev/null
    ssh $driver_ip /pkg/bin/fia_test diag_shell -u $unit -C \"cint /tmp/cint_coeff.c\" >& /dev/null
else
    show_cmd "show controller sfe diagshell $asic \"cd /tmp/\" location $rack/$slot" >& /dev/null
    show_cmd "show controller sfe diagshell $asic \"cint /tmp/cint_coeff.c\" location $rack/$slot" >& /dev/null
fi
