// +FHDR------------------------------------------------------------
//                 Copyright (c) 2022 SmartLogic.
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : main.c
// Author        : xianfeng.du
// Created On    : 2022-06-25
// Last Modified :
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------
#include "typedef.h"
#include "osp_task.h"
#include "osp_timer.h"
#include "ucp_printf.h"
#include "spu_log.h"

#include "ucp_drv_common.h"
//#include "pet_rfm_spu0_top.h"
//#include "msg_transfer_host.h"
#include "msg_transfer_layer.h"
//#include "ucp_pcie_traffic.h"
#include "ucp_printf.h"
#include "msg_transfer_mem.h"
#include "msg_transfer_mbuffer.h"

uint32_t pcie_rx_callback_data(const char* buf,uint32_t payloadSize)
{
	char tmplogbuf[1024] = {0};
	if(payloadSize < 1024)
	{
		memcpy_ucp(tmplogbuf, buf, payloadSize);
	}
	else
	{
		memcpy_ucp(tmplogbuf, buf, 1024);
	}
	UCP_PRINT_LOG("received buf : %s\n", tmplogbuf);

    return payloadSize;
}

uint32_t pcie_rx_callback_ctrl(const char* buf,uint32_t payloadSize)
{
	char tmplogbuf[1024] = {0};
	if(payloadSize < 1024)
	{
		memcpy_ucp(tmplogbuf, buf, payloadSize);
	}
	else
	{
		memcpy_ucp(tmplogbuf, buf, 1024);
	}
	UCP_PRINT_LOG("received buf : %s\n", tmplogbuf);

    return payloadSize;
}

uint32_t pcie_rx_callback_oam(const char* buf,uint32_t payloadSize)
{
	char tmplogbuf[1024] = {0};
	if(payloadSize < 1024)
	{
		memcpy_ucp(tmplogbuf, buf, payloadSize);
	}
	else
	{
		memcpy_ucp(tmplogbuf, buf, 1024);
	}
	UCP_PRINT_LOG("received buf : %s\n", tmplogbuf);

    return payloadSize;
}

void test_case_cfgpar_pcie()
{
	int i = 0;

	for(i = 0; i < MAX_INSTANCE_NUM; i++)
	{
		msg_transfer_callback_register(CU_SPLIT, i, C_PLANE, pcie_rx_callback_ctrl);
		msg_transfer_callback_register(CU_SPLIT, i, U_PLANE, pcie_rx_callback_data);
		msg_transfer_callback_register(OAM, i, C_PLANE, pcie_rx_callback_oam);
	}
}

int8_t get_id(uint16_t type_id, uint16_t cu_flag)
{
	int8_t que_id = -1;

	switch (type_id) {
		case CU_SPLIT:
			if (cu_flag == U_PLANE) {
				que_id = UCP4008_TRAFFIC_NR_eMBB_DATA;
			} else {
				que_id = UCP4008_TRAFFIC_NR_eMBB_CTRL;
			}
			break;
		case OAM:
			que_id = UCP4008_TRAFFIC_OAM;
			break;
		default:
			UCP_PRINT_LOG("get_queue_id doesn't support transfer_type[%d] .",type_id);
			break;
	}

	return que_id;
}

//static uint32_t count = 0;

void test_case_sendmsg_pcie()
{
    uint32_t size = 512-16;
    char* buf = NULL;
    uint32_t availableSize;

	uint16_t cu_flag = C_PLANE;
	HandleId_t handler;
	handler.port_id = 0;
	handler.inst_id = 0;
	handler.type_id = CU_SPLIT;
	uint32_t slot_ind_flag = 1;
	int ret = 0;
	
	uint32_t offset = 0;
	uint32_t i = 0;
	char templogbuf[256] = {0};
	
	int8_t que_id = 0;
	MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
	MsgQueueLocalInfo_t* ch;	
	//uint32_t core_id = get_core_id();
	//uint32_t bufsize =  ch->bufSize - MSG_MBUF_HEAD_SIZE;
	/****************************test code************************************************/
	for(i = 0; i < MAX_INSTANCE_NUM; i++)
	{
		//debug_write(DBG_DDR_COMMON_IDX(core_id, 2), ++count);
		handler.port_id = 0;
		handler.inst_id = i;
		handler.type_id = CU_SPLIT; 	
		if(1 == slot_ind_flag)
		{
		   // slot_ind_flag = 0;
			ret = msg_transfer_send_start(handler.value);
			/************C_PLANE***************/
			cu_flag = C_PLANE;
			que_id = get_id(handler.type_id, cu_flag);
			ch = (MsgQueueLocalInfo_t *)&pMsgQueueLocalMgt->localUlQueue[i][que_id];
			size =  ch->bufSize-MSG_MBUF_HEAD_SIZE;				
			ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset);
			//com_debug_log("alloc ctrl msg ulque[%d][%d] ch=0x%llx ch->bufSize=0x%x\n", i, que_id, (uint64_t)ch, ch->bufSize);			
			if ( SUCCESS != ret) {
				com_debug_log("alloc ctrl msg failed! ulque[%d][%d] ch=0x%llx ch->bufSize=0x%x\n", i, que_id, (uint64_t)ch, ch->bufSize);
				continue;
			}
			//size = availableSize;
			//buf = *bufAddr;
			UCP_PRINT_LOG("msg transfer alloced ul msg C_PLANE buf = 0x%x!\n", buf);
			memset(templogbuf, 0x00, sizeof(templogbuf));				
			sprintf(templogbuf, "%s", "this is test for ul CU_SPLIT ctrl transfer!\n");
			memcpy_ucp(buf, templogbuf, strlen(templogbuf)+1);
			/*
			*(uint32_t*)(ptr + 0) = 0x12345678;
			*(uint32_t*)(ptr + 4) = 0x5a5a5a5a;
			*(uint32_t*)(ptr + 8) = 0x12345678;
			*(uint32_t*)(ptr + 12) = 0xa5a5a5a5;
			*/
			//PCIE_DEBUG_LOG("start enter msg_transfer_send_msg\n");
			ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size);
			if(ret != SUCCESS)
			{
				com_debug_log("ul msg_transfer_send_msg CU_SPLIT ctrl  failed! ret = %d inst_id=%d\n", ret, handler.inst_id);
			}	
	
			/************U_PLANE***************/
			cu_flag = U_PLANE;
			que_id = get_id(handler.type_id, cu_flag);
			ch = (MsgQueueLocalInfo_t *)&pMsgQueueLocalMgt->localUlQueue[i][que_id];
			size =  ch->bufSize-MSG_MBUF_HEAD_SIZE;							
			ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset);
			//com_debug_log("alloc data msg ulque[%d][%d] ch=0x%llx ch->bufSize=0x%x\n", i, que_id, (uint64_t)ch, ch->bufSize);			
			
			if ( SUCCESS != ret) {
				com_debug_log("alloc data msg failed! ulque[%d][%d]  ch=0x%llx  ch->bufSize=0x%x\n", i, que_id, ch, ch->bufSize);
				continue;
			}
			//size = availableSize;
			//buf = *bufAddr;
			UCP_PRINT_LOG("msg transfer alloced ul U_PLANE msg buf = 0x%x!\n", buf);
			memset(templogbuf, 0x00, sizeof(templogbuf));
			sprintf(templogbuf, "%s", "this is test for ul CU_SPLIT data transfer!\n");
			memcpy_ucp(buf, templogbuf, strlen(templogbuf)+1);
			
			/*
			ptr = (uint8_t *)buf;
			*(uint32_t*)(ptr + 0) = 0x12345678;
			*(uint32_t*)(ptr + 4) = 0x5a5a5a5a;
			*(uint32_t*)(ptr + 8) = 0x12345678;
			*(uint32_t*)(ptr + 12) = 0xa5a5a5a5;
			*/
			ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size);
			if(ret != SUCCESS)
			{
				com_debug_log("ul msg_transfer_send_msg CU_SPLIT data failed! ret = %d inst_id = %d\n", ret, handler.inst_id);
			}
	
			ret = msg_transfer_send_end(handler.value);
			if(ret != SUCCESS)
			{
				com_debug_log("ul msg_transfer_send_end CU_SPLIT data failed! ret = %d inst_id = %d\n", ret, handler.inst_id);
			}
		}
	
		handler.type_id = OAM;	
		if(1 == slot_ind_flag)
		{
		   // slot_ind_flag = 0;
		
			ret = msg_transfer_send_start(handler.value);
		
			/************C_PLANE***************/
			cu_flag = C_PLANE;
			que_id = get_id(handler.type_id, cu_flag);
			ch = (MsgQueueLocalInfo_t *)&pMsgQueueLocalMgt->localUlQueue[i][que_id];
			size =  ch->bufSize-MSG_MBUF_HEAD_SIZE;				
			ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset);
			//com_debug_log("alloc oam msg ulque[%d][%d] ch=0x%llx ch->bufSize=0x%x\n", i, que_id, (uint64_t)ch, ch->bufSize);			
			
			if ( SUCCESS != ret) {
				com_debug_log("oam alloc msg failed! ulque[%d][%d] ch=0x%llx ch->bufSize = 0x%x\n", i, que_id, ch, ch->bufSize);
				continue;
			}
			//size = availableSize;
			//buf = *bufAddr;
			UCP_PRINT_LOG("msg transfer alloced msg OAM buf = 0x%x!\n", buf);
			memset(templogbuf, 0x00, sizeof(templogbuf));				
			sprintf(templogbuf, "%s", "this is test for ul OAM ctrl transfer!\n");
			memcpy_ucp(buf, templogbuf, strlen(templogbuf)+1);
			
			/*
			*(uint32_t*)(ptr + 0) = 0x12345678;
			*(uint32_t*)(ptr + 4) = 0x5a5a5a5a;
			*(uint32_t*)(ptr + 8) = 0x12345678;
			*(uint32_t*)(ptr + 12) = 0xa5a5a5a5;
			*/
			ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size);
			if(ret != SUCCESS)
			{
				com_debug_log("ul msg_transfer_send_msg failed! CU_SPLIT oam !	ret = %d inst_id = %d\n", ret, handler.inst_id);
				continue;
			}
			
			UCP_PRINT_LOG("msg_transfer_send_msg end!\n");
			ret = msg_transfer_send_end(handler.value);
			if(ret != SUCCESS)
			{
				com_debug_log("ul msg_transfer_send_end CU_SPLIT oam failed! ret = %d inst_id = %d\n", ret, handler.inst_id);
			}			
		}			
	}
	/*********************************************************************************/

}

void test_case_recv_msg_pcie()
{
	uint16_t cu_flag = C_PLANE;
	HandleId_t handler;
	handler.port_id = 0;
	handler.inst_id = 0;
	handler.type_id = CU_SPLIT;
	
	uint8_t port_id = 0;
	uint32_t offset = 0;
	uint32_t len = 0;
	uint8_t* msg_ptr;	
	uint32_t i = 0;	
	
	/****************************test code************************************************/
	for ( i = 0; i < MAX_INSTANCE_NUM; i++) {
		handler.port_id = port_id;
		handler.inst_id = i;
		handler.type_id = CU_SPLIT;
		cu_flag = C_PLANE;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);
	
		cu_flag = U_PLANE;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);
	
		handler.type_id = OAM;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);		
	}
	/*********************************************************************************/ 	
}

#if 0

void test_speed_sendmsg_pcie()
{
    uint32_t size = 0;
    char* buf = NULL;
    uint32_t availableSize;

	uint16_t cu_flag = C_PLANE;
	HandleId_t handler;
	handler.port_id = 0;
	handler.inst_id = 0;
	handler.type_id = CU_SPLIT;
	uint32_t slot_ind_flag = 1;
	int ret = 0;
	
	uint32_t offset = 0;
	uint32_t i = 0;
	char templogbuf[1024] = {0};
	
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
	MsgQueueLocalInfo_t* ch = (MsgQueueLocalInfo_t*)&pMsgQueueLocalMgt->localUlQueue[0][1];	
	size = ch->bufSize - MSG_MBUF_HEAD_SIZE;
	/****************************test code************************************************/
	for(i = 0; i < 2; i++)
	{
		handler.port_id = 0;
		handler.inst_id = i;
		handler.type_id = CU_SPLIT; 	
		if(1 == slot_ind_flag)
		{
		   // slot_ind_flag = 0;
			ret = msg_transfer_send_start(handler.value);
	
			/************C_PLANE***************/
			cu_flag = C_PLANE;
			ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset);
			if ( SUCCESS != ret) {
				UCP_PRINT_LOG("alloc msg failed!\n");
				continue;
			}
			//size = availableSize;
			//buf = *bufAddr;
			UCP_PRINT_LOG("msg transfer alloced ul msg C_PLANE buf = 0x%x!\n", buf);
			memset(templogbuf, 0x00, sizeof(templogbuf));				
			sprintf(templogbuf, "%s", "this is test for ul CU_SPLIT ctrl transfer!\n");
			memcpy_ucp(buf, templogbuf, strlen(templogbuf)+1);
			/*
			*(uint32_t*)(ptr + 0) = 0x12345678;
			*(uint32_t*)(ptr + 4) = 0x5a5a5a5a;
			*(uint32_t*)(ptr + 8) = 0x12345678;
			*(uint32_t*)(ptr + 12) = 0xa5a5a5a5;
			*/
			//PCIE_DEBUG_LOG("start enter msg_transfer_send_msg\n");
			ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size);
			if(ret != SUCCESS)
			{
				UCP_PRINT_LOG("ul msg_transfer_send_msg CU_SPLIT ctrl  failed! ret = %d\n", ret);
			}
			
			ret = msg_transfer_send_end(handler.value);
		}

	}
	/*********************************************************************************/

}
#endif

void test_speed_recv_msg_pcie()
{
	uint16_t cu_flag = C_PLANE;
	HandleId_t handler;
	handler.port_id = 0;
	handler.inst_id = 0;
	handler.type_id = CU_SPLIT;
	
	uint8_t port_id = 0;
	uint32_t offset = 0;
	uint32_t len = 0;
	uint8_t* msg_ptr;	
	uint32_t i = 0;	
	
	/****************************test code************************************************/
	for ( i = 0; i < 2; i++) {
		handler.port_id = port_id;
		handler.inst_id = i;
		handler.type_id = CU_SPLIT;
		cu_flag = C_PLANE;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);
		
		cu_flag = U_PLANE;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);
	
		handler.type_id = OAM;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);		
	}
	/*********************************************************************************/ 	
}


void pcie_test_task_func(void)
{
    //uint32_t clockTick   = 0;
   // uint32_t clockOffset = 0;	
    test_case_sendmsg_pcie();
    //test_speed_recv_msg_pcie();
    //delay_us(50);

    return;
}

void pcie_test_task_func4(void)
{
    //uint32_t clockTick   = 0;
   // uint32_t clockOffset = 0;	
    test_case_sendmsg_pcie();
    //test_speed_recv_msg_pcie();
    //delay_us(50);

    return;
}


void ape0_test_task_reg(void)
{
	
	//uint32_t core_id = get_core_id();	
   // test_case_cfgpar_pcie();
    osp_task_info_ex pcie_timer_task_stc  = {50, (int8_t*)"pcie_test_taskc", 50, 4096, OSP_NORMAL_TYPE, 1, 0, 0, NULL, (OSP_TASKENTRY_FUNC)pcie_test_task_func};
    osp_task_create(&pcie_timer_task_stc);
    //osp_timer_sync(1);

    return;
}

void ape1_test_task_reg(void)
{

    return;
}

void ape2_test_task_reg(void)
{
    return ;
}

void ape3_test_task_reg(void)
{
    return ;
}

void ape4_test_task_reg(void)
{
    return ;
}

void ape5_test_task_reg(void)
{
    return ;
}

void ape6_test_task_reg(void)
{
    return ;
}

void ape7_test_task_reg(void)
{
    return ;
}


