/**
 * @file hv_drv_HdmiHdcpRx.c
 * @brief HDMI 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"

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

    return Hv_Cal_HdmiHdcp1x_SetBcaps(ucBcaps, ucPortIndex);
}

VOID Hv_Drv_Hdcp2x_HdmiClrRxStatSize(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp2x_HdmiClrRxStatSize(ucPortIndex);
}

VOID Hv_Drv_Hdcp2x_HdmiSetRxStatusReAuth(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp2x_HdmiSetRxStatusReAuth(ucPortIndex);
}

UCHAR8 Hv_Drv_Hdcp_HdmiGetReAuthStatus(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_RET(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM, 0);

    return Hv_Cal_Hdcp_HdmiGetReAuthStatus(ucPortIndex);
}

HdcpRxEvtId Hv_Drv_Hdcp_HdmiGetIrqEvent(UINT32 uiIrqMask, UCHAR8 ucPortIndex, HdcpKmStoreState enKmState, Hdcp2xRxMsgState enMsgState)
{
    HdcpRxEvtId enHdcpRxEvtId = HdcpRxEvt_Invalid;
    HV_R32_DECL_VAR();

    if(0 == uiIrqMask)
    {
        return enHdcpRxEvtId;
    }

    if(Hv_Cal_Hdcp_CheckHdmiRxRdMsgIrq(uiIrqMask, ucPortIndex))
    {
        //Hv_Drv_Hdcp2x_HdmiClrRxStatSize(ucPortIndex);
        if(HDCP2X_AKE_SEND_HPRIME_DONE == enMsgState)
        {
            if(HV_HDCP_NO_STORED_KM == enKmState)
            {
                enHdcpRxEvtId = Hdcp2xRxEvt_RdHPrime;
            }
        }
    }

    if(Hv_Cal_Hdcp_CheckHdmiRxWrAksvIrq(uiIrqMask, ucPortIndex))
    {
        UCHAR8 aucAKsv[HV_HDCP_1X_SIZE_AKSV] = {0,};
        Hv_Drv_Hdcp1x_HdmiGetRxAksvFromReg(aucAKsv, ucPortIndex);
        if(Hv_Drv_Hdcp_CheckKidValid(aucAKsv))
        {
            enHdcpRxEvtId = Hdcp1xRxEvt_WrAksv;
        }
        else
        {
            HV_LOG_INFO(HDMI, "HDMI%d HDCP1X AKSV Invalid.", ucPortIndex);
        }
    }
    else if(Hv_Cal_Hdcp_CheckHdmiRxWrAKEInitIrq(uiIrqMask, ucPortIndex))
    {
        //Hv_Cal_Hdcp2x_HdmiClrRxStatSize(ucPortIndex);
        enHdcpRxEvtId = Hdcp2xRxEvt_WrAKEInit;
    }
    else if(Hv_Cal_Hdcp_CheckHdmiRxWrAKENoStoredKmIrq(uiIrqMask, ucPortIndex))
    {
        //Hv_Cal_Hdcp2x_HdmiClrRxStatSize(ucPortIndex);
        enHdcpRxEvtId = Hdcp2xRxEvt_WrAKENoStoredKm;
    }
    else if(Hv_Cal_Hdcp_CheckHdmiRxWrAKEStoredKmIrq(uiIrqMask, ucPortIndex))
    {
        //Hv_Cal_Hdcp2x_HdmiClrRxStatSize(ucPortIndex);
        enHdcpRxEvtId = Hdcp2xRxEvt_WrAKEStoredKm;
    }
    else if(Hv_Cal_Hdcp_CheckHdmiRxWrLCInitIrq(uiIrqMask, ucPortIndex))
    {
        //Hv_Cal_Hdcp2x_HdmiClrRxStatSize(ucPortIndex);
        enHdcpRxEvtId = Hdcp2xRxEvt_WrLcInit;
    }
    else if(Hv_Cal_Hdcp_CheckHdmiRxWrSKESendEksIrq(uiIrqMask, ucPortIndex))
    {
        //Hv_Cal_Hdcp2x_HdmiClrRxStatSize(ucPortIndex);
        enHdcpRxEvtId = Hdcp2xRxEvt_WrAKESendEks;
    }
    return enHdcpRxEvtId;
}

VOID Hv_Drv_Hdcp_HdmiSaveBksvtoReg(const UCHAR8 *pucBKsv, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    HV_ASSERT_VALID_PTR_VOID(pucBKsv);
    return Hv_Cal_Hdcp_HdmiSaveBksvtoReg(pucBKsv, ucPortIndex);
}


VOID Hv_Drv_Hdcp1x_HdmiSetHdcpPath(UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex < HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);

    Hv_Cal_Hdcp1x_HdmiSetHdcpPath(ucHdcpPortIndex);

    return;
}

VOID Hv_Drv_Hdcp2x_HdmiSetHdcpPath(UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex < HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);

    Hv_Cal_Hdcp2x_HdmiSetHdcpPath(ucHdcpPortIndex);

    return;
}

VOID Hv_Drv_Hdcp_HdmiCommInit(VOID)
{
    return Hv_Cal_Hdcp_HdmiCommInit();
}

VOID Hv_Drv_Hdcp_SetHdmiHdcpVersion(UCHAR8 ucHdcpVersion)
{
    return Hv_Cal_Hdcp_SetHdmiHdcpVersion(ucHdcpVersion);
}

VOID Hv_Drv_Hdcp_RealseHdmiPath(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp_RealseHdmiPath(ucPortIndex);
}

VOID Hv_Drv_Hdcp_AttachHdmiPath(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp_AttachHdmiPath(ucPortIndex);
}

VOID Hv_Drv_Hdcp2x_HdmiStartRxAuthCallBack(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp2x_HdmiStartRxAuthCallBack(ucPortIndex);
}

/* HDCP 1X Interface */
VOID Hv_Drv_Hdcp1x_HdmiGetRxAksvFromReg(UCHAR8 *pucAKsv, UCHAR8 ucPortIndex)
{
    HV_ASSERT_VALID_PTR_VOID(pucAKsv);
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp1x_HdmiGetRxAksvFromReg(pucAKsv, ucPortIndex);
}
VOID Hv_Drv_Hdcp1x_HdmiGetRxAnFromReg(UCHAR8 *pucAn, UCHAR8 ucPortIndex)
{
    HV_ASSERT_VALID_PTR_VOID(pucAn);
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp1x_HdmiGetRxAnFromReg(pucAn, ucPortIndex);
}

VOID Hv_Drv_Hdcp1x_HdmiRxSetRI(USHORT16 usRI, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp1x_HdmiRxSetRI(usRI, ucPortIndex);
}

VOID Hv_Drv_Hdcp1x_HdmiRxSetRiSelToHw(UCHAR8 ucPortIndex, UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex <= HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);
    HV_ASSERT_TRUE_VOID(ucPortIndex < HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp1x_HdmiRxSetRiSelToHw(ucPortIndex, ucHdcpPortIndex);
}

VOID Hv_Drv_Hdcp1x_HdmiRxAuthStart(UCHAR8 ucPortIndex, UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex <= HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);
    HV_ASSERT_TRUE_VOID(ucPortIndex < HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp1x_HdmiRxAuthStart(ucPortIndex, ucHdcpPortIndex);
}

VOID Hv_Drv_Hdcp1x_HdmiRxSetEessQstc(UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucHdcpPortIndex <= HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM);

    return Hv_Cal_Hdcp1x_HdmiRxSetEessQstc(ucHdcpPortIndex);
}

VOID Hv_Drv_Hdcp_HdmiRxClearMsgAfterAutherDone(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp_HdmiRxClearMsgAfterAutherDone(ucPortIndex);
}

/* HDCP 2X Interface */
VOID Hv_Drv_Hdcp2x_HdmiGetRxMsgFromReg(UCHAR8 *pucData, USHORT16 usMsgSize, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp2x_HdmiGetRxMsgFromReg(pucData,usMsgSize,ucPortIndex);
}

VOID Hv_Drv_Hdcp2x_HdmiSetRxMsgToReg(UCHAR8 *pucData, USHORT16 usMsgSize, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp2x_HdmiSetRxMsgToReg(pucData, usMsgSize, ucPortIndex);
}

HV_ATTR_ISR_SECTION VOID Hv_Drv_Hdcp2x_HdmiSetRxStatSize(USHORT16 usMsgSize, UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp2x_HdmiSetRxStatSize(usMsgSize, ucPortIndex);
}

VOID Hv_Drv_Hdcp2x_HdmiResetLinkIntegrityFailure(UCHAR8 ucPortIndex)
{
    HV_ASSERT_TRUE_VOID(ucPortIndex <= HV_BOARD_CONFIG_MAX_HDMI_PORT_NUM);

    return Hv_Cal_Hdcp2x_HdmiResetLinkIntegrityFailure(ucPortIndex);
}

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


    return Hv_Cal_Hdcp_HdmiMuxSel(ucPortIndex, ucHdcpPortIndex);
}

BOOL Hv_Drv_Hdcp2x_IsHdmiInEncryed(UCHAR8 ucHdcpPortIndex)
{
    HV_ASSERT_TRUE_RET(ucHdcpPortIndex < HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM, HV_FALSE);

    return Hv_Cal_Hdcp2x_IsHdmiInEncryed(ucHdcpPortIndex);
}

VOID Hv_Drv_Hdcp1x_HdcpClrAnAksv(UCHAR8 ucPortIndex)
{
    Hv_Cal_Hdcp1x_HdcpClrAnAksv(ucPortIndex);
    return;
}


