// +FHDR------------------------------------------------------------
//                 Copyright (c) 2020 PicocomTech.
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : ecs_rfm_dm_mgt.c
// Author        : xianfeng.du
// Created On    : 2022-06-27
// Last Modified :
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------

#include "ucp_printf.h"
#include "mem_sections.h"
#include "ecs_rfm_dm_mgt.h"

EcsRfmDmLocalMgt_t gEcsRfmDmLocalMgt;
EcsRfmDmLocalMgt_t* get_ecs_rfm_dm_local_mgt(void)
{
    return (EcsRfmDmLocalMgt_t *)&gEcsRfmDmLocalMgt;
}

int32_t ecs_rfm_dm_alloc(void)
{
    EcsRfmDmLocalMgt_t* pEcsDmLocalMgt = get_ecs_rfm_dm_local_mgt();
	if (NULL == pEcsDmLocalMgt)
	{
		return -1;
	}
    MEM_SECTION_INFO* pMemSection = GetRfmDm1Section();
	if (NULL == pMemSection)
	{
		return -1;
	}

    pEcsDmLocalMgt->pCpriPara = (stCpriPara*)memSectionAlloc(pMemSection, sizeof(stCpriPara), MEM_ALIGNED_4BYTES, "pCpriPara");
	if (NULL == pEcsDmLocalMgt->pCpriPara)
	{
		return -1;
	}
	
	pEcsDmLocalMgt->pCpriDelay = (stCpriDelayMeasure*)memSectionAlloc(pMemSection, sizeof(stCpriDelayMeasure), MEM_ALIGNED_4BYTES, "pCpriDelay");
	if (NULL == pEcsDmLocalMgt->pCpriDelay)
	{
		return -1;
	}
	
	//pEcsDmLocalMgt->pGpioInfo = (stGpioOnBoard*)0x0A4F4000;
	pEcsDmLocalMgt->pGpioInfo = (stGpioOnBoard*)memSectionAlloc(pMemSection, sizeof(stGpioOnBoard), MEM_ALIGNED_4BYTES, "pGpioInfo"); // (stGpioOnBoard*)0x0A4D7400;
	if (NULL == pEcsDmLocalMgt->pGpioInfo)
	{
		return -1;
	}

    pEcsDmLocalMgt->pAlarmStatus = (stFhAlarmStat*)memSectionAlloc(pMemSection, sizeof(stFhAlarmStat), MEM_ALIGNED_4BYTES, "pAlarmStatus");
	if (NULL == pEcsDmLocalMgt->pAlarmStatus)
	{
		return -1;
	}
	
    pEcsDmLocalMgt->pOamMsgPtr  = (stOamMsgTransferHeader*)memSectionAlloc(pMemSection, sizeof(stOamMsgTransferHeader), MEM_ALIGNED_4BYTES, "pOamMsg");
	if (NULL == pEcsDmLocalMgt->pOamMsgPtr)
	{
		return -1;
	}
	
    pEcsDmLocalMgt->pOamBaseDelaySetRspPtr  =  (stCpriSetLinkDelay*)memSectionAlloc(pMemSection, sizeof(stCpriSetLinkDelay), MEM_ALIGNED_4BYTES, "pOamBaseDelaySetRsp");
	if (NULL == pEcsDmLocalMgt->pOamBaseDelaySetRspPtr)
	{
		return -1;
	}
	
    pEcsDmLocalMgt->pOamBaseDelayQryRspPtr  = (stCpriGetLinkDelay*)memSectionAlloc(pMemSection, sizeof(stCpriGetLinkDelay), MEM_ALIGNED_4BYTES, "pOamBaseDelayQryRsp");
	if (NULL == pEcsDmLocalMgt->pOamBaseDelayQryRspPtr)
	{
		return -1;
	}
	
    pEcsDmLocalMgt->pOamFiberDelayQryRspPtr = (stCpriGetRndDelay*)memSectionAlloc(pMemSection, sizeof(stCpriGetRndDelay), MEM_ALIGNED_4BYTES, "OamFiberDelayQryRsp");
	if (NULL == pEcsDmLocalMgt->pOamFiberDelayQryRspPtr)
	{
		return -1;
	}

    pEcsDmLocalMgt->pOamFrameHeadOffsetRspPtr = (stCpriFrameHeadOffsetRsp*)memSectionAlloc(pMemSection, sizeof(stCpriFrameHeadOffsetRsp), MEM_ALIGNED_4BYTES, "OamFrameHeadOffsetRsp");
	if (NULL == pEcsDmLocalMgt->pOamFrameHeadOffsetRspPtr)
	{
		return -1;
	}
	
    pEcsDmLocalMgt->pOamFrameHeadOffsetQryRspPtr  = (stCpriGetFrameHeadOffset*)memSectionAlloc(pMemSection, sizeof(stCpriGetFrameHeadOffset), MEM_ALIGNED_4BYTES, "pOamFrameHeadOffsetQryRspPtr");
	if (NULL == pEcsDmLocalMgt->pOamFrameHeadOffsetQryRspPtr)
	{
		return -1;
	}
	
	for (int i = 0; i < MTIMER_INTEGRATED_MAX_NUM; i++)
	{
		pEcsDmLocalMgt->pMtimerPara[i] = (stMtimerPara*)memSectionAlloc(pMemSection, sizeof(stMtimerPara), MEM_ALIGNED_4BYTES, "pMtimerPara[i]");
		if (NULL == pEcsDmLocalMgt->pMtimerPara[i])
		{
			return -1;
		}
	}
	
	pEcsDmLocalMgt->tx_cmd_fifo_ptr = (stCpriCsuCmdFifoInfo*)memSectionAlloc(pMemSection, sizeof(stCpriCsuCmdFifoInfo), MEM_ALIGNED_4BYTES, "tx_cmd_fifo");
	if (NULL == pEcsDmLocalMgt->tx_cmd_fifo_ptr)
	{
		return -1;
	}

	pEcsDmLocalMgt->rx_cmd_fifo_ptr = (stCpriCsuCmdFifoInfo*)memSectionAlloc(pMemSection, sizeof(stCpriCsuCmdFifoInfo), MEM_ALIGNED_4BYTES, "rx_cmd_fifo");
	if (NULL == pEcsDmLocalMgt->rx_cmd_fifo_ptr)
	{
		return -1;
	}
	
	pEcsDmLocalMgt->jesd_orx_para_ptr = (JesdOrxPara_t*)memSectionAlloc(pMemSection, sizeof(JesdOrxPara_t), MEM_ALIGNED_4BYTES, "jesd_orx_para");
	if (NULL == pEcsDmLocalMgt->jesd_orx_para_ptr)
	{
		return -1;
	}

	pEcsDmLocalMgt->jesd_delay_ptr = (JesdDelay_t*)memSectionAlloc(pMemSection, sizeof(JesdDelay_t), MEM_ALIGNED_4BYTES, "jesd_delay");
	if (NULL == pEcsDmLocalMgt->jesd_delay_ptr)
	{
		return -1;
	}

    return 0;
}

int32_t ecs_rfm1_dm_init(void)
{
    EcsRfmDmLocalMgt_t* pEcsDmLocalMgt = get_ecs_rfm_dm_local_mgt();
	if (NULL == pEcsDmLocalMgt)
	{
		return -1;
	}
    
	if (NULL == pEcsDmLocalMgt->pCpriPara)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->pCpriPara, 0, sizeof(stCpriPara));
	
	if (NULL == pEcsDmLocalMgt->pCpriDelay)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->pCpriDelay, 0, sizeof(stCpriDelayMeasure));
	
	if (NULL == pEcsDmLocalMgt->pGpioInfo)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->pGpioInfo, 0, sizeof(stGpioOnBoard));

	if (NULL == pEcsDmLocalMgt->pAlarmStatus)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->pAlarmStatus, 0, sizeof(stFhAlarmStat));
	
	if (NULL == pEcsDmLocalMgt->pOamMsgPtr)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->pOamMsgPtr, 0, sizeof(stOamMsgTransferHeader));
	
	if (NULL == pEcsDmLocalMgt->pOamBaseDelaySetRspPtr)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->pOamBaseDelaySetRspPtr, 0, sizeof(stCpriSetLinkDelay));
	
	if (NULL == pEcsDmLocalMgt->pOamBaseDelayQryRspPtr)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->pOamBaseDelayQryRspPtr, 0, sizeof(stCpriGetLinkDelay));
	
	if (NULL == pEcsDmLocalMgt->pOamFiberDelayQryRspPtr)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->pOamFiberDelayQryRspPtr, 0, sizeof(stCpriGetRndDelay));

	if (NULL == pEcsDmLocalMgt->pOamFrameHeadOffsetRspPtr)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->pOamFrameHeadOffsetRspPtr, 0, sizeof(stCpriFrameHeadOffsetRsp));

	for (int i = 0; i < MTIMER_INTEGRATED_MAX_NUM; i++)
	{
		if (NULL == pEcsDmLocalMgt->pMtimerPara[i])
		{
			return -1;
		}
		memset_ucp(pEcsDmLocalMgt->pMtimerPara[i], 0, sizeof(stMtimerPara));
	}

	if (NULL == pEcsDmLocalMgt->pOamFrameHeadOffsetQryRspPtr)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->pOamFrameHeadOffsetQryRspPtr, 0, sizeof(stCpriGetFrameHeadOffset));

	if (NULL == pEcsDmLocalMgt->tx_cmd_fifo_ptr)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->tx_cmd_fifo_ptr, 0, sizeof(stCpriCsuCmdFifoInfo));

	if (NULL == pEcsDmLocalMgt->rx_cmd_fifo_ptr)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->rx_cmd_fifo_ptr, 0, sizeof(stCpriCsuCmdFifoInfo));

	if (NULL == pEcsDmLocalMgt->jesd_orx_para_ptr)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->jesd_orx_para_ptr, 0, sizeof(JesdOrxPara_t));

	if (NULL == pEcsDmLocalMgt->jesd_delay_ptr)
	{
		return -1;
	}
	memset_ucp(pEcsDmLocalMgt->jesd_delay_ptr, 0, sizeof(JesdDelay_t));

	return 0;
}

