/**
 * @file hv_drv_TypecDrpApp.c
 * @brief  This source file contains user application specific functions and interfaces
 * @details This file provides the following functions: \n
 *          (1) \n
 *          (2) \n
 *          (3) \n
 *
 * 
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-08-25
 * @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>init
 * </table>
*/
/* ************************************************************************** */
/* ************************************************************************** */
/* Section: Included Files                                                    */
/* ************************************************************************** */
/* ************************************************************************** */
#include "hv_comm_Assert.h"
#include "hv_drv_TypecStdInc.h"

/* ************************************************************************** */
/* ************************************************************************** */
/* Section: File Scope or Global Data                                         */
/* ************************************************************************** */
/* ************************************************************************** */

#if (HV_TYPEC_CONFIG_TYPEC_MODE == TYPEC_MODE_DRP)

/* ************************************************************************** */
/* ************************************************************************** */
// Section: Local Functions                                                   */
/* ************************************************************************** */
/* ************************************************************************** */

/* ************************************************************************** */
/* ************************************************************************** */
// Section: Interface Functions                                               */
/* ************************************************************************** */
/* ************************************************************************** */

UINT8 App_HandlePSFEvents(UINT8 u8PortNum, eHV_PSF_NOTIFICATION ePDEvent)
{
    UINT8 u8RetVal = HV_FALSE;
    TypecPlatformCB *pstTypecPlatformCB = Hv_Drv_Typec_GetFlatformCbPointer();
    HV_ASSERT_VALID_PTR(pstTypecPlatformCB);
    
    switch(ePDEvent)
    {
        case eHV_PSF_UPDS_IN_IDLE:
        {
            break;
        }  
         
        case eHV_PSF_VCONN_PWR_FAULT:
        {
            /*Returning HV_TRUE to enable PSF to handle the power fault*/
            u8RetVal = HV_FALSE;
            break;
        }
        
        case eHV_PSF_VBUS_PWR_FAULT:
        {
            /*Returning HV_TRUE to enable PSF to handle the power fault*/
            u8RetVal = HV_FALSE;
            break;
        }
        
        case eHV_PSF_PORT_POWERED_OFF:
        {
            break;
        }
        
        case eHV_PSF_TYPEC_ERROR_RECOVERY: 
        {
            /*Returning HV_TRUE to enable PSF to handle Error Recovery*/
            u8RetVal = HV_TRUE;
            break; 
        }
        
        case eHV_PSF_TYPEC_DETACH_EVENT:
        {
            /* after detach, reset u8SVDMConfigFlag flat to HV_FALSE */
            gasCfgStatusData.sVDMPerPortData[u8PortNum].u8SVDMConfigFlag = HV_FALSE;
            pstTypecPlatformCB->DetachHandler(u8PortNum);
            break;
        }
        
        case eHV_PSF_TYPEC_CC1_ATTACH:
        {
            pstTypecPlatformCB->AttachHandler(u8PortNum);
            break;
        }
        
        case eHV_PSF_TYPEC_CC2_ATTACH:
        {
            pstTypecPlatformCB->AttachHandler(u8PortNum);
            break;
        }
        
        case eHV_PSF_CAPS_MISMATCH:
        {
            break;
        }
        
        case eHV_PSF_NEW_SRC_CAPS_RCVD:
        {
            break;
        }
        
        case eHV_PSF_PE_SRC_DISABLED:
        {
            break; 
        }
        
        case eHV_PSF_PD_CONTRACT_NEGOTIATED: 
        {
#if (HV_TRUE == INCLUDE_PD_ALT_MODE)
            if(DPM_IS_MODAL_OPERATION_ACTIVE(u8PortNum))
            {
                if(PD_ROLE_UFP != DPM_GET_CURRENT_DATA_ROLE(u8PortNum))
                {
                    DPM_CLR_MODAL_OPR_ACTIVE_STATUS(u8PortNum);
                    /* solution for Samsung mobile phone's waterdrop issue */
                    Hv_Drv_Typec_DpAuxPowerOff();
                    Hv_Drv_Typec_SimulateUnplugPlug();
                }
            }
            else
#endif
            {
                /**
                 * For certain notebook devices, such as the Victus by HP Gaming Laptop 15-fa1143TX with an RTX4060 Laptop GPU,
                 * the Type-C port only has a source function. When the laptop is shut down and the motherboard enters standby mode,
                 * it is observed that the Type-C port still works, with the Type-C hardware state being 3 and VBUS at 5V.
                 * This situation can cause the PM (Power Management) to be directly awakened after entering standby.
                 * Therefore, an additional condition is added here: if it is such a device, it will enter a fake standby mode.
                 */
                if (PD_ROLE_SOURCE == DPM_GET_CURRENT_POWER_ROLE (u8PortNum) ||
                    (PD_ROLE_SINK == DPM_GET_CURRENT_POWER_ROLE (u8PortNum) &&
                    DPM_GET_PDO_DUAL_POWER(gasCfgStatusData.sPerPortData[u8PortNum].u32aPartnerSourcePDO[INDEX_0]) == HV_FALSE))
                {
                    pstTypecPlatformCB->PdDoneHandler(u8PortNum);
                }
            }
            break; 
        }
        
        case eHV_PSF_HARD_RESET_COMPLETE:
        {
            break; 
        }
        
        case eHV_PSF_SINK_CAPS_NOT_RCVD: 
        {
            break; 
        }  
        
        case eHV_PSF_SINK_CAPS_RCVD:
        {
            /* Sink Capabilities received from partner is available in 
               gasCfgStatusData.sPerPortData[u8PortNum].u32aPartnerSinkPDO array */            
            break;            
        }
        
        case eHV_PSF_SINK_ALERT_RCVD: 
        {
            /* Alert information received from Sink is available in 
             gasCfgStatusData.sPPSPerPortData[u8PortNum].u32PartnerAlert */
            break; 
        }
        
        case eHV_PSF_SINK_STATUS_NOT_RCVD:
        {
            break; 
        }

        case eHV_PSF_SINK_STATUS_RCVD:
        {
            /* Status information received from Sink is available in 
            gasCfgStatusData.sPPSPerPortData[u8PortNum].u8aPartnerStatus array */
            break; 
        }
        
        case eHV_PSF_VCONN_SWAP_COMPLETE:
        {
            break; 
        }
        
        case eHV_PSF_VCONN_SWAP_RCVD:
        {
            break; 
        }
        
        case eHV_PSF_VCONN_SWAP_NO_RESPONSE_RCVD:
        {
            break; 
        }        
                
        case eHV_PSF_PR_SWAP_COMPLETE:
        {
            break; 
        }
        
        case eHV_PSF_PR_SWAP_RCVD:
        {
            break; 
        }
        
        case eHV_PSF_PR_SWAP_NO_RESPONSE_RCVD:
        {
            break; 
        }
        
        case eHV_PSF_DR_SWAP_COMPLETE:
        {
            break; 
        }
        
        case eHV_PSF_DR_SWAP_RCVD:
        {
            break; 
        }
        
        case eHV_PSF_DR_SWAP_NO_RESPONSE_RCVD:
        {
            break; 
        }
        
        case eHV_PSF_CABLE_IDENTITY_DISCOVERED:
        {
            /* Cable identity received from cable is available in 
            gasCfgStatusData.sPerPortData[u8PortNum].u32aCableIdentity array */                                   
            break; 
        }
        
        case eHV_PSF_CABLE_IDENTITY_NAKED:
        {
            break;
        }
        
        case eHV_PSF_VDM_RESPONSE_RCVD:
        {
            break;
        }
        
        case eHV_PSF_VDM_RESPONSE_NOT_RCVD:
        {
            break; 
        }
        
        case eHV_PSF_VDM_REQUEST_RCVD:
        {
            u8RetVal = HV_TRUE;
            break; 
        }
        
        case eHV_PSF_VDM_AMS_COMPLETE:
        {            
            break; 
        }

        case eHV_PSF_HPD_ENABLED:
        {
            break;
        }
        
        case eHV_PSF_HPD_EVENT_HIGH:
        {
            break;
        }

        case eHV_PSF_HPD_EVENT_LOW:
        {
            break;
        }

        case eHV_PSF_HPD_EVENT_IRQ_HPD:
        {
            break;
        }
        
        case eHV_PSF_HPD_DISABLED:
        {
            break;
        }
        
        case eHV_PSF_ALT_MODE_ENTRY_FAILED:
        {
            pstTypecPlatformCB->VdmFailedHandler(u8PortNum);
            break; 
        }
        
        case eHV_PSF_PORT_DISABLED:
        {
            break;
        }
        
        case eHV_PSF_PORT_ENABLED:
        {
            break;
        }
        case eHV_PSF_VDM_AMS_DONE:
        {
            pstTypecPlatformCB->VdmDoneHandler(u8PortNum);
            break;
        }
        case eHV_PSF_VDM_AMS_EXIT:
        {
            pstTypecPlatformCB->VdmExitHandler(u8PortNum);
            break;
        }
        case eHV_PSF_VDM_AMS_CONFIG:
        {
            UCHAR8 ucTmpPinAssign = (UCHAR8)DPM_GET_DO_DP_CONFIG_PIN_ASSIGNMENT(gasCfgStatusData.sAltModePerPortData[u8PortNum].u32aPartnerVDO[gasCfgStatusData.sAltModePerPortData[u8PortNum].u8PartnerVDOCnt - 1]);
            if(DP_PIN_ASSIGNMENT_E == ucTmpPinAssign)
            {
                ucTmpPinAssign = 2;
            }
            else
            {
                ucTmpPinAssign = 0;
            }
            if((gasCfgStatusData.sPerPortData[u8PortNum].u32PortConnectStatus & DPM_PORT_ATTACHED_STATUS)
                && !(gasCfgStatusData.sPerPortData[u8PortNum].u32PortConnectStatus & DPM_PORT_ORIENTATION_FLIPPED_STATUS))
            {
                pstTypecPlatformCB->VdmConfigHandler(u8PortNum, 0, ucTmpPinAssign);
            }
            else if((gasCfgStatusData.sPerPortData[u8PortNum].u32PortConnectStatus & DPM_PORT_ATTACHED_STATUS)
                && (gasCfgStatusData.sPerPortData[u8PortNum].u32PortConnectStatus & DPM_PORT_ORIENTATION_FLIPPED_STATUS))
            {
                pstTypecPlatformCB->VdmConfigHandler(u8PortNum, 1, ucTmpPinAssign);
            }
            else
            {
                /* do nothing */
            }
            break;
        }        
        
        default:
        {
            break;
        }
    }

    return u8RetVal;
}
/**************************************************************************************/

void App_GPIOControl_Init(UINT8 u8PortNum, eHV_PSF_GPIO_FUNCTIONALITY eGPIOFunc)
{
    return;
}

void App_GPIOControl_Drive(UINT8 u8PortNum, eHV_PSF_GPIO_FUNCTIONALITY eGPIOFunc,
                                    eHV_PSF_GPIO_DRIVE_VAL eGPIODrive)
{
    return;
}

UINT8 App_PortPowerInit(UINT8 u8PortNum)
{   
    return HV_TRUE; 
}

#if (HV_TRUE == INCLUDE_PD_SINK)
void App_DriveDAC_I(UINT8 u8PortNum, UINT16 u16DACData)
{
    return;
}
#endif


void App_AMESetDpHpd(UINT8 u8PortNum, BOOL bHpdEn)
{
#if (HV_TRUE == INCLUDE_PD_ALT_MODE)
    if(DPM_IS_MODAL_OPERATION_ACTIVE(u8PortNum))
    {
        if(bHpdEn)
        {
            gasCfgStatusData.sVDMPerPortData[u8PortNum].u8SVDMHpdHighFlag = HV_TRUE;
        }
        else
        {
            gasCfgStatusData.sVDMPerPortData[u8PortNum].u8SVDMHpdLowFlag = HV_TRUE;
        }
    }
#endif
}

void App_AMESetDpIrqHpd(UINT8 u8PortNum)
{
#if (HV_TRUE == INCLUDE_PD_ALT_MODE)
    if(DPM_IS_MODAL_OPERATION_ACTIVE(u8PortNum))
    {
        gasCfgStatusData.sVDMPerPortData[u8PortNum].u8SVDMIrqHpdFlag = HV_TRUE;
    }
#endif    
}

BOOL App_AMEGetStatus(UINT8 u8PortNum)
{
    return DPM_IS_MODAL_OPERATION_ACTIVE(u8PortNum);
}

void App_GetSourceCapabilities(UINT8 u8PortNum, UINT8 *u8pSrcPDOCnt, UINT32 *pu32DataObj)
{
    DPM_GetSourceCapabilities(u8PortNum, u8pSrcPDOCnt, pu32DataObj);
}


/* *****************************************************************************
 End of File
 */
#endif
