/**
 * @file hv_drv_DpHdcpRx.c
 * @brief DP HDCP RX 模块提供的驱动层
 * @details 该模块接口提供给Hiview-Madia-Ware及其他上层应用使用。
 * @author HiView SoC Software team
 * @version 0.0.1
 * @date 20230522
 * @copyright Copyright(c),2023-5, Hiview Software. All rights reserved.
 * @par History: 
 * <table>
 * <tr><th>作者                        <th>日期              <th>描述
 * <tr><td>HiView SoC Software team  <td>20220822        <td>Create and first Version.
 * </table>
**/
#include "hv_drv_HdcpRx.h"
#include "hv_cal_Hdcp.h"
#include "hv_hdcp_BigDigits.h"
#include "hv_hdcp2x_Common.h"
#include "hv_hdcp_engine_Aes256.h"

Status Hv_Drv_Hdcp1x_DpRxGetMsg(HdcpRx* pstHdcpRxIns, UCHAR8 ucPortIndex)
{
    /*检查入参*/
    HV_ASSERT_TRUE(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    HV_ASSERT_VALID_PTR(pstHdcpRxIns);

    Hv_Cal_Hdcp1x_DpGetRxAksvFromReg(pstHdcpRxIns->stHdcp1xRx.stParams.AKsv, HV_HDCP_1X_SIZE_AKSV, ucPortIndex);
    Hv_Cal_Hdcp1x_DpGetRxAnFromReg(pstHdcpRxIns->stHdcp1xRx.stParams.An, HV_HDCP_1X_SIZE_AN, ucPortIndex);

    return HV_SUCCESS;
}

VOID Hv_Drv_Hdcp1x_NotifyTxIrq(Hdcp1xRxStatusMask enStatusType, UCHAR8 ucPortIndex, UCHAR8 ucHdcpIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    return Hv_Cal_Hdcp1x_NotifyTxIrq(enStatusType, ucPortIndex, ucHdcpIndex);

}

VOID Hv_Drv_Hdcp1x_DpRxAuthStart(UCHAR8 ucPortIndex, UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex < HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);

    return Hv_Cal_Hdcp1x_DpRxAuthStart(ucPortIndex, ucHdcpPortIndex);
}

VOID Hv_Drv_Hdcp1x_DpRxSetRI(USHORT16 usRI, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    return Hv_Cal_Hdcp1x_DpRxSetRI(usRI, ucPortIndex);
}

VOID Hv_Drv_Hdcp1x_DpSetBcaps(UCHAR8 ucBcaps, UCHAR8 ucPortIndex)
{
    /*检查入参*/
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);

    Hv_Cal_Hdcp1x_SetBcaps(ucBcaps, ucPortIndex);

    return;
}

HdcpRxEvtId Hv_Drv_Hdcp_DpGetIrqEvent(const UINT32* puiIrqMask, UCHAR8 ucPortIndex, HdcpKmStoreState enStoreState)
{
    HdcpRxEvtId enHdcpRxEvtId = HdcpRxEvt_Invalid;
    HV_ASSERT_TRUE_RET(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM, enHdcpRxEvtId);

    if(Hv_Cal_Hdcp_CheckDpRxRdHIrq(puiIrqMask, ucPortIndex))
    {
        Hv_Cal_Hdcp2x_DpResetLinkIntegrityFailure(ucPortIndex);
        if(HV_HDCP_NO_STORED_KM == enStoreState)
        {
            enHdcpRxEvtId = Hdcp2xRxEvt_RdHPrime;
        }
        else
        {
            /* Do Noting */
        }
    }
    else if(Hv_Cal_Hdcp_CheckDpRxRdPairingIrq(puiIrqMask, ucPortIndex))
    {
        Hv_Cal_Hdcp2x_DpResetLinkIntegrityFailure(ucPortIndex);
    }
    else if(Hv_Cal_Hdcp_CheckDpRxWrAKEInitIrq(puiIrqMask, ucPortIndex))
    {
        enHdcpRxEvtId = Hdcp2xRxEvt_WrAKEInit;
    }
    else if(Hv_Cal_Hdcp_CheckDpRxWrNoStoredKmIrq(puiIrqMask, ucPortIndex))
    {
        enHdcpRxEvtId = Hdcp2xRxEvt_WrAKENoStoredKm;
    }
    else if(Hv_Cal_Hdcp_CheckDpRxWrStoredKmIrq(puiIrqMask, ucPortIndex))
    {
        enHdcpRxEvtId = Hdcp2xRxEvt_WrAKEStoredKm;
    }
    else if(Hv_Cal_Hdcp_CheckDpRxWrLCInitIrq(puiIrqMask, ucPortIndex))
    {
        enHdcpRxEvtId = Hdcp2xRxEvt_WrLcInit;
    }
    else if(Hv_Cal_Hdcp_CheckDpRxWrSKESendEksIrq(puiIrqMask, ucPortIndex))
    {
        enHdcpRxEvtId = Hdcp2xRxEvt_WrAKESendEks;
    }
    else if(Hv_Cal_Hdcp_CheckDpRxStreamTypeIrq(puiIrqMask, ucPortIndex))
    {
        enHdcpRxEvtId = Hdcp2xRxEvt_UpdateStreamType;
    }
    else if(Hv_Cal_Hdcp_CheckDpRxWrAksvIrq(puiIrqMask, ucPortIndex))
    {
        enHdcpRxEvtId = Hdcp1xRxEvt_WrAksv;
    }
    else
    {
        return enHdcpRxEvtId;
    }

    return enHdcpRxEvtId;
}

VOID Hv_Drv_Hdcp1x_DpResetLinkIntegrityFailure(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);

    Hv_Cal_Hdcp1x_DpResetLinkIntegrityFailure(ucPortIndex);

    return;
}

VOID Hv_Drv_Hdcp2x_DpResetLinkIntegrityFailure(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);

    Hv_Cal_Hdcp2x_DpResetLinkIntegrityFailure(ucPortIndex);

    return;
}

Status Hv_Drv_Hdcp2x_DpRxGetMsg(HdcpRx* pstHdcpRxIns, HV_HDCP_2X_RX_MSG_ID enMsgId, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    HV_ASSERT_VALID_PTR(pstHdcpRxIns);

    switch(enMsgId)
    {
        case HV_HDCP_2X_RX_MSG_ID_AKEINIT:
            {
                /* Hv_Drv_Hdcp2x_RxResetParams(&(pstHdcpRxIns->stHdcp2xRx)); */
                Hv_Cal_Hdcp2x_DpRxGetTxCaps(pstHdcpRxIns->stHdcp2xRx.stParams.TxCaps, sizeof(pstHdcpRxIns->stHdcp2xRx.stParams.TxCaps), ucPortIndex);
                Hv_Cal_Hdcp2x_DpRxGetRtx(pstHdcpRxIns->stHdcp2xRx.stParams.Rtx, sizeof(pstHdcpRxIns->stHdcp2xRx.stParams.Rtx), ucPortIndex);
                break;
            }

        case HV_HDCP_2X_RX_MSG_ID_AKENOSTOREDKM:
            {
                Hv_Cal_Hdcp2x_DpRxGetEkpubKm(pstHdcpRxIns->stHdcp2xRx.stParams.EKpubKm, sizeof(pstHdcpRxIns->stHdcp2xRx.stParams.EKpubKm), ucPortIndex);
                break;
            }

        case HV_HDCP_2X_RX_MSG_ID_AKESTOREDKM:
            {
                Hv_Cal_Hdcp2x_DpRxGetEkhKm(pstHdcpRxIns->stHdcp2xRx.stParams.EKh, sizeof(pstHdcpRxIns->stHdcp2xRx.stParams.EKh), ucPortIndex);
                Hv_Cal_Hdcp2x_DpRxGetM(pstHdcpRxIns->stHdcp2xRx.stParams.M, sizeof(pstHdcpRxIns->stHdcp2xRx.stParams.M), ucPortIndex);
                break;
            }

        case HV_HDCP_2X_RX_MSG_ID_LCINIT:
            {
                Hv_Cal_Hdcp2x_DpRxGetRn(pstHdcpRxIns->stHdcp2xRx.stParams.Rn, sizeof(pstHdcpRxIns->stHdcp2xRx.stParams.Rn), ucPortIndex);
                break;
            }
        case HV_HDCP_2X_RX_MSG_ID_SKESENDEKS:
            {
                Hv_Cal_Hdcp2x_DpRxGetRiv(pstHdcpRxIns->stHdcp2xRx.stParams.Riv, sizeof(pstHdcpRxIns->stHdcp2xRx.stParams.Riv), ucPortIndex);
                Hv_Cal_Hdcp2x_DpRxGetKs(pstHdcpRxIns->stHdcp2xRx.stParams.EDkeyKs, sizeof(pstHdcpRxIns->stHdcp2xRx.stParams.EDkeyKs), ucPortIndex);
                break;
            }
        default:
            {
                HV_LOG_INFO(HDCP, "No match msg id!");

                return HV_FAILURE;
            }
    }

    return HV_SUCCESS;
}

VOID Hv_Drv_Hdcp2x_DpRxSaveRrxToReg(UCHAR8* pucRrx, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    return Hv_Cal_Hdcp2x_DpRxSaveRrxToReg(pucRrx, ucPortIndex);
}

VOID Hv_Drv_Hdcp2x_NotifyTxIrq(Hdcp2xDpRxStatusMask enStatusType, UCHAR8 ucPortIndex, UCHAR8 ucHdcpIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    return Hv_Cal_Hdcp2x_NotifyTxIrq(enStatusType, ucPortIndex, ucHdcpIndex);
}

VOID Hv_Drv_Hdcp2x_DpSaveHPrimeToReg(UCHAR8* pucHPrime, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    return Hv_Cal_Hdcp2x_DpSaveHPrimeToReg(pucHPrime, ucPortIndex);
}

VOID Hv_Drv_Hdcp2x_DpSavePairingToReg(UCHAR8* pucEkhKm, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    return Hv_Cal_Hdcp2x_DpSavePairingToReg(pucEkhKm, ucPortIndex);
}

VOID Hv_Drv_Hdcp2x_DpSaveLPrimeToReg(UCHAR8* pucLPrime, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    return Hv_Cal_Hdcp2x_DpSaveLPrimeToReg(pucLPrime, ucPortIndex);
}

VOID Hv_Drv_Hdcp2x_DpUpdateStreamType(UCHAR8 ucPortIndex, UCHAR8 ucHdcpPortIndex)
{
    UCHAR8 ucStreamType = 0;
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex < HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);

    ucStreamType = Hv_Cal_Hdcp2x_DpRxGetStreamType(ucPortIndex);

    return Hv_Cal_Hdcp2x_DpRxSetStreamType(ucStreamType, ucHdcpPortIndex);
}
VOID Hv_Drv_Hdcp2x_DpRxAuthStart(UCHAR8 ucPortIndex, UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex < HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);
    return Hv_Cal_Hdcp2x_DpRxAuthStart(ucPortIndex, ucHdcpPortIndex);
}

VOID Hv_Drv_Hdcp_DpLoadPublicCert(const UCHAR8 *pucPubCert, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    return Hv_Cal_Hdcp_DpSaveCertToReg(pucPubCert, ucPortIndex);
}

VOID Hv_Drv_Hdcp_DpSaveBksvtoReg(const UCHAR8 *pucBKsv, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    return Hv_Cal_Hdcp_DpSaveBksvtoReg(pucBKsv, ucPortIndex);
}

VOID Hv_Drv_Hdcp_RealseDpPath(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);

    return Hv_Cal_Hdcp_RealseDpPath(ucPortIndex);
}

VOID Hv_Drv_Hdcp_AttachDpPath(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);

    return Hv_Cal_Hdcp_AttachDpPath(ucPortIndex);
}

VOID Hv_Drv_Hdcp1x_DpSetHdcpPath(UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex < HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);
    return Hv_Cal_Hdcp1x_DpSetHdcpPath(ucHdcpPortIndex);
}

VOID Hv_Drv_Hdcp2x_DpSetHdcpPath(UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex < HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);
    return Hv_Cal_Hdcp2x_DpSetHdcpPath(ucHdcpPortIndex);
}

VOID Hv_Drv_Hdcp_DpCommInit()
{
    return Hv_Cal_Hdcp_DpCommInit();
}

VOID Hv_Drv_Hdcp2x_SetDpHdcpVersion(UCHAR8 ucHdcpVersion)
{
    return Hv_Cal_Hdcp2x_SetDpHdcpVersion(ucHdcpVersion);
}

VOID Hv_Drv_Hdcp_DpMixClr(UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex < HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);
    Hv_Cal_Hdcp_DpMixClr(ucHdcpPortIndex);

    return;
}

VOID Hv_Drv_Hdcp_DpMuxSel(UCHAR8 ucPortIndex, UCHAR8 ucHdcpPortIndex)
{
    /*检查入参*/
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_DP_PORT_NUM);
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex < HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);

    return Hv_Cal_Hdcp_DpMuxSel(ucPortIndex, ucHdcpPortIndex);
}

