/**
 * @file        hv_comm_shell_DP_debug.c
 * @brief       shell source code.
 * @details     This file provides the following functions: \n
 *              (1) shell init and handle \n
 * @author      HiView SoC Software Team
 * @version     1.0.1
 * @date        2022-09-05
 * @copyright   Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par         History:
 * <table>
 * <tr><th>Author                        <th>Date         <th>Change Description
 * <tr><td>HiView SoC Software Team      <td>2022-08-25   <td>create
 * </table>
*/

#ifdef HV_SCALER_DEBUG_VERSION
#include <string.h>
#include "hv_comm_Log.h"
#include "hv_comm_shell_hvconfig.h"
#include "hv_comm_DataType.h"
#include "hv_comm_Assert.h"
#include "hv_comm_Define.h"
#include "hv_vos_Comm.h"
#include "hv_mw_DisplayPort.h"
#include "hv_mw_SystemManager.h"
#include "hv_drv_Physical.h"

VOID Hv_Shell_DpRx_DebugUsage(VOID)
{
    HV_LOG_INFO(HDMI,
        "    dp [OPTION]\n"
        "        -p [port_index], which DP port is operated, 0 for DP0 & 1 for DP1 ...\n"
        "        -e [edidcfg], update DP EDID with given edid select\n"
        "        -t [hpdtoggle], toggle hpd low-->high, 0:Event HPD 1:IRQ HPD\n"
        "        -f [framerate], get current framerate\n"
        "        -i [info], Dump the details of the DP port status and configrations.\n"
        "        -h print the command dp help info.\n"
        "           For example to dump all the information of dp0 : \n"
        "           dp -p 0 -i 0");

    return;
}

VOID Hv_Shell_DpRx_Dump(DisplayPortCtrlData *pstDpCtrl)
{
    HV_ASSERT_VALID_PTR_VOID(pstDpCtrl);

    HV_LOG_INFO(DP, "Current DP Port: %d", pstDpCtrl->ucCurPort);
    HV_LOG_INFO(DP, "DP Cable PlugIn: %d", pstDpCtrl->bCableConnected);
    HV_LOG_INFO(DP, "DP Link is Sync: %d", pstDpCtrl->bLinkSync);
    HV_LOG_INFO(DP, "DP HPD Level is: %d", pstDpCtrl->ucHpdLevel);
    HV_LOG_INFO(DP, "DP StateMachine: %d", pstDpCtrl->enCurrentPortState);
    HV_LOG_INFO(DP, "DP Current Edid: %d", pstDpCtrl->enCurEdidType);
    HV_LOG_INFO(DP, "DP VrrVender is: %d", pstDpCtrl->stCurVrr.eVendor);
    HV_LOG_INFO(DP, "DP VrrEnable is: %d", pstDpCtrl->stCurVrr.bVRREnable);

    Hv_Mw_DpRx_PrintTimingInfo(&pstDpCtrl->stCurTiming);
    Hv_Mw_DpRx_PrintColorInfo(&pstDpCtrl->stCurColor);
    Hv_Mw_DpRx_PrintHdrInfo(&pstDpCtrl->stCurHdr);
    if (pstDpCtrl->stCurVrr.eVendor == VendorType_AMD)
    {
        Hv_Mw_DpRx_PrintAMDInfo(&pstDpCtrl->stCurVrr);
    }
    else if (pstDpCtrl->stCurVrr.eVendor == VendorType_NVIDIA)
    {
        Hv_Mw_DpRx_PrintNvidiaInfo(&pstDpCtrl->stCurVrr);
    }
    else
    {
    }

    return;
}

INT32 Hv_Shell_DpRx_Debug(INT32 iArgc, CHAR8 *cArgv[])
{
    INT32 uiOpt = -1;
    UINT32 uiPara = 0;
    UINT32 uiPortId = 0;
    InputPortManager* pstInputPortManager = Hv_Mw_InputPort_GetManagerPtr();
    DisplayPortCtrlData *pstDpCtrl = &pstInputPortManager->stDisplayPortManager.astDpCtrl[uiPortId];

    while ((uiOpt = Hv_getOpt(iArgc, cArgv, "h::e:i::p:t::f::g::")) != -1)
    {
        switch (uiOpt)
        {
            case 'h':
            {
                Hv_Shell_DpRx_DebugUsage();
                break;
            }
            case 'p':
            {
                uiPortId = atoi(hv_optarg);
                if (uiPortId >= HV_BOARD_CONFIG_MAX_DP_PORT_NUM)
                {
                    HV_LOG_INFO(DP, "DP port %d is invalid, total is %d", uiPortId, HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
                    uiPortId = HV_BOARD_CONFIG_MAX_DP_PORT_NUM;
                }
                else
                {
                    pstDpCtrl = &pstInputPortManager->stDisplayPortManager.astDpCtrl[uiPortId];
                }
                break;
            }
            case 'e':
            {
                uiPara = atoi(hv_optarg);
                HV_LOG_INFO(DP, "Change to EDID %d", uiPara);
                if (uiPortId != HV_BOARD_CONFIG_MAX_DP_PORT_NUM)
                {
                    pstInputPortManager->pfUpdateEdid(pstDpCtrl->enPortIndex, uiPara);
                }
                break;
            }
            case 'i':
            {
                if (uiPortId != HV_BOARD_CONFIG_MAX_DP_PORT_NUM)
                {
                    Hv_Shell_DpRx_Dump(pstDpCtrl);
                }
                break;
            }
            case 't':
            {
                uiPara = atoi(hv_optarg);
                if (uiPortId != HV_BOARD_CONFIG_MAX_DP_PORT_NUM)
                {
                    if (uiPara == 0)
                    {
                        pstInputPortManager->stDisplayPortManager.pfHpdToggle(pstDpCtrl->enPortIndex);
                    }
                    else if (uiPara == 1)
                    {
                        Hv_Mw_DpRx_IrqHpd(pstDpCtrl->enPortIndex);
                    }
                    else if (uiPara == 2)
                    {
                        pstInputPortManager->stDisplayPortManager.pfHPDAction(pstDpCtrl->enPortIndex, DP_HPD_LOW);
                    }
                    else
                    {
                        pstInputPortManager->stDisplayPortManager.pfHPDAction(pstDpCtrl->enPortIndex, DP_HPD_HIGH);
                    }
                }
                break;
            }
            case 'f':
            {
                if (uiPortId != HV_BOARD_CONFIG_MAX_DP_PORT_NUM)
                {
                    HV_LOG_INFO(DP, "FrameRate: %dHz", pstInputPortManager->stDisplayPortManager.pfGetCurrentFrameRate(pstDpCtrl->enPortIndex));
                }
                break;
            }
            case 'g':
            {
                uiPara = atoi(hv_optarg);
                if (uiPortId != HV_BOARD_CONFIG_MAX_DP_PORT_NUM)
                {
                    if (uiPara == 1)
                    {
                        Hv_Mw_DpRx_SetDpVer(pstDpCtrl->enPortIndex, HV_DP_VER11);
                    }
                    else if (uiPara == 2)
                    {
                        Hv_Mw_DpRx_SetDpVer(pstDpCtrl->enPortIndex, HV_DP_VER12);
                    }
                    else if (uiPara == 3)
                    {
                        Hv_Mw_DpRx_SetDpVer(pstDpCtrl->enPortIndex, HV_DP_VER14);
                    }
                    else
                    {
                        Hv_Mw_DpRx_SetAllDpVer(HV_DP_VER14);
                    }
                }
                break;
            }
            default:
            {
                Hv_Shell_DpRx_DebugUsage();
                break;
            }
        }
    }

    hv_optind = 1;
    hv_optarg = NULL;

    return 0;
}

#endif

