/**
 * @file        hv_drv_Mailbox.c
 * @brief       hv_drv_Mailbox source file.
 * @details     This file provides the following functions: \n
 * @author      HiView SoC Software Team
 * @version     1.0.1
 * @date        2022-09-06
 * @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-09-06   <td>create
 * </table>
*/
#include "hv_drv_Mailbox.h"
#include "hv_cal_Mailbox.h"
#include "hv_comm_Assert.h"
#include "hv_comm_Define.h"
#include <string.h>

static BOOL IsMsgHeaderTypeNull(MailboxMsg *pstMsg)
{
    HV_ASSERT_VALID_PTR_RET(pstMsg, HV_FALSE);

    if (pstMsg->stMsgHeader.u8MsgType == (UCHAR8)E_MAILBOX_CMD_NULL)
    {
        return HV_TRUE;
    }

    return HV_FALSE;
}

Status Hv_Drv_MailboxRecvMsg(MailboxMsg *pstMsg)
{
    HV_ASSERT_VALID_PTR(pstMsg);

    Hv_Cal_Mailbox_RecvMsgHeader(&(pstMsg->stMsgHeader));
    if (IsMsgHeaderTypeNull(pstMsg) == HV_TRUE)
    {
        memset(pstMsg, 0, sizeof(MailboxMsg));
        return HV_CONTINUE;
    }
    else
    {
        Hv_Cal_Mailbox_RecvMsgBody(pstMsg->au8MsgBody);
        Hv_Cal_Mailbox_RecvMsgFinish();
        return HV_SUCCESS;
    }

    return HV_FAILURE;
}

Status Hv_Drv_MailboxSendMsg(MailboxMsg *pstMsg)
{
    UCHAR8 ucRetry = 0;
    HV_ASSERT_VALID_PTR(pstMsg);

    MailboxMsg stMailboxStatusMsg = { 0 };

    Hv_Cal_Mailbox_GetSendStatus(&(stMailboxStatusMsg.stMsgHeader));
    while (IsMsgHeaderTypeNull(&stMailboxStatusMsg) == HV_FALSE)
    {
        if (ucRetry%10 == 0)
        {
            Hv_Cal_Mailbox_EnableInterrupt();
        }
        if (ucRetry++>50)
        {
            HV_LOGI("not send %d for %d.\n", pstMsg->stMsgHeader.u8MsgType, stMailboxStatusMsg.stMsgHeader.u8MsgType);
            return HV_CONTINUE;
        }
        Hv_Vos_Delayus(10*1000);
        Hv_Cal_Mailbox_GetSendStatus(&(stMailboxStatusMsg.stMsgHeader));
    }

    {
        /* firstly, write msg body */
        Hv_Cal_Mailbox_SendMsgBody(pstMsg->au8MsgBody);
        /* secondly, write msg header. do NOT change the order */
        Hv_Cal_Mailbox_SendMsgHeader(&(pstMsg->stMsgHeader));
        /* enable PM51 interrupt */
        Hv_Cal_Mailbox_EnableInterrupt();
        memset(pstMsg, 0, sizeof(MailboxMsg));
        return HV_SUCCESS;
    }
}

Status Hv_Drv_MailboxGetKeyValue(MailboxMsg *pstMsg, UCHAR8 *pucKeyValue)
{
    HV_ASSERT_VALID_PTR(pstMsg);
    HV_ASSERT_VALID_PTR(pucKeyValue);

    *pucKeyValue = pstMsg->au8MsgBody[0];

    return HV_SUCCESS;
}

Status Hv_Drv_MailboxGetIRKeyValue(MailboxMsg *pstMsg, UCHAR8 *pucSysCode, UCHAR8 *pucKeyValue)
{
    HV_ASSERT_VALID_PTR(pstMsg);
    HV_ASSERT_VALID_PTR(pucSysCode);
    HV_ASSERT_VALID_PTR(pucKeyValue);

    *pucSysCode = pstMsg->au8MsgBody[0];
    *pucKeyValue = pstMsg->au8MsgBody[1];

    return HV_SUCCESS;
}

UINT32 Hv_Drv_Mailbox_GetPQToolCmdPort(VOID)
{
    return Hv_Cal_Mailbox_GetPQToolCmdPort();
}

UINT32 Hv_Drv_Mailbox_GetSwIntStatus(VOID)
{
    return Hv_Cal_Mailbox_GetSwIntStatus();
}

VOID Hv_Drv_Mailbox_ClearSwIntStatus(UINT32 uiSwintStatus)
{
    return Hv_Cal_Mailbox_ClearSwIntStatus(uiSwintStatus);
}


