/*****************************************************************************
 函 数 名  : ftm_mailbox_msgproc
 功能描述  : 邮箱数据读取
 输入参数  : enMbxType 邮箱数据类型
 输出参数  : 无
 返 回 值  : 0无数据,大于0为邮箱数据长度
*****************************************************************************/
VOS_UINT32 ftm_mailbox_msgproc(MAILBOX_SERVICE_TYPE_E enMbxType)
{
    VOS_UINT32 ulRet      = 0;
    VOS_UINT32 ulMsgSize  = 0;
    VOS_VOID* pMailBoxMsg = NULL;
    MsgBlock* pMsgBlock   = NULL;
    VOS_UINT32 ret = ERR_MSP_UNKNOWN;

    /* 读取消息长度 */
    HAL_SDMLOG("[%s] BEIGN TO READ DATA.\n", __FUNCTION__);
    ulMsgSize = BSP_MailBox_ComMsgSize(enMbxType);
    HAL_SDMLOG("[%s] BEIGN TO READ DATA.LEN = %d\n", __FUNCTION__,ulMsgSize);
    if(ulMsgSize == 0 )
    {
        return ulMsgSize;
    }

    pMailBoxMsg = VOS_MemAlloc(MSP_SYS_FTM_PID, ((DYNAMIC_MEM_PT)), ulMsgSize);
    if(NULL == pMailBoxMsg)
    {
        HAL_SDMLOG("[%s] VOS_MemAlloc failed.\n", __FUNCTION__);
        return 0;
    }

    /* 读取邮箱数据 */
    ulRet = BSP_MailBox_ComMsgRead(enMbxType, pMailBoxMsg, ulMsgSize, EN_MAILBOX_SLEEP_WAKEUP);
    if(ulRet != BSP_OK)
    {
        VOS_MemFree(MSP_SYS_FTM_PID, pMailBoxMsg);
        return 0;
    }

    /* 发送消息到MSP_SYS_FTM_PID任务 */
    pMsgBlock = (MsgBlock*)VOS_AllocMsg(MSP_SYS_FTM_PID, sizeof(OS_MSG_STRU));

    if (pMsgBlock)
    {
        OS_MSG_STRU* pMsg = (OS_MSG_STRU*)pMsgBlock->aucValue;

        pMsgBlock->ulReceiverPid = MSP_SYS_FTM_PID;
        pMsgBlock->ulSenderPid   = MSP_SYS_FTM_PID;
        pMsg->ulMsgId  = ID_MSG_L1A_CT_IND;
        pMsg->ulParam1 = pMailBoxMsg;
        pMsg->ulParam2 = ulMsgSize;

        ret = VOS_SendMsg(MSP_SYS_FTM_PID, pMsgBlock);
        if (ret != VOS_OK)
        {
            HAL_SDMLOG("[%s] send msg to MSP_SYS_FTM_PID FAILED \n",  __FUNCTION__);
        }
    }

    return ulMsgSize;
}
VOS_VOID diag_MailboxSelfTask(VOS_VOID)
{
    VOS_UINT32 len, ret;
    DIAG_MSG_DSP_CNF_TO_AGENT_STRU * pData;

    if(ERR_MSP_SUCCESS != VOS_SmBCreate("diagmbx", 0, VOS_SEMA4_FIFO, &g_diagMbxSem))
    {
        diag_printf("[%s]:agent sem init err!\n",__FUNCTION__);
        return ;
    }

    /* 向邮箱注册回调函数 */
    if(ERR_MSP_SUCCESS != BSP_MailBox_ComNotifyReg(EN_MAILBOX_SERVICE_RTT_SYS_CTRL, diag_MailboxCb))
    {
        diag_printf("[%s]:BSP_MailBox_ComNotifyReg err!\n",__FUNCTION__);
        return ;
    }
    /*lint -save -e716*/
    while(1)
    /*lint -restore*/
    {
        if(VOS_OK != VOS_SmP(g_diagMbxSem, 0))
        {
            diag_printf("[%s] VOS_SmP failed.\n", __FUNCTION__);

            continue;
        }

        DIAG_DEBUG_SDM_FUN(EN_DIAG_AGENT_LDSP_MB_MSG, 0, 0, 0);

        len = BSP_MailBox_ComMsgSize(EN_MAILBOX_SERVICE_RTT_SYS_CTRL);
        if(0 == len)
        {
            DIAG_DEBUG_SDM_FUN(EN_DIAG_AGENT_LDSP_MB_MSG, 1, 0, 0);
            continue ;
        }

        /* VOS消息体在事先分配好的内存中查找空闲,没有阻塞操作 */
        pData = (DIAG_MSG_DSP_CNF_TO_AGENT_STRU *)VOS_AllocMsg(MSP_PID_DIAG_AGENT, len);
        if(VOS_NULL == pData)
        {
            DIAG_DEBUG_SDM_FUN(EN_DIAG_AGENT_LDSP_MB_MSG, 2, 0, 0);
            continue ;
        }

        /* (pData+20)表示把邮箱中的内容直接读取到消息中 */
        ret = BSP_MailBox_ComMsgRead(EN_MAILBOX_SERVICE_RTT_SYS_CTRL, ((VOS_UINT8 *)pData+VOS_MSG_HEAD_LENGTH), len, EN_MAILBOX_SLEEP_WAKEUP);
        if(ret)
        {
            DIAG_DEBUG_SDM_FUN(EN_DIAG_AGENT_LDSP_MB_MSG, 3, 0, 0);
            continue;
        }

        pData->ulReceiverPid = MSP_PID_DIAG_AGENT;
        pData->ulSenderPid   = MSP_PID_DIAG_AGENT;
        pData->ulLength      = len;

        /* 注意,只能给自己发消息,跨核发消息中会有阻塞接口,不能在中断中调用 */
        ret = VOS_SendMsg(MSP_PID_DIAG_AGENT, pData);
        if (ret != VOS_OK)
        {
            DIAG_DEBUG_SDM_FUN(EN_DIAG_AGENT_LDSP_MB_MSG, 4, 0, 0);
        }
    }
}