// +FHDR------------------------------------------------------------
//                 Copyright (c) 2022 SmartLogic.
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : test.c
// Author        : xianfeng.du
// Created On    : 2022-11-25
// Last Modified :
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------

#include <stddef.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>

#include "typedef.h"
#include "msg_transfer_layer.h"
#include "ucp_handshake.h"
#include "typedef.h"
#include "ucp_printf.h"
#include "msg_transfer_mem.h"
#include "ecs_sm_mgt.h"
#include "ucp_handshake.h"
#include "ospShell.h"
#include "ospLog.h"
#include "ospDump.h"

static inline void get_msg_transfer_info(uint16_t port_index, uint16_t inst_id, uint16_t transfer_type, transfer_type_info_s* transfer_type_info_ptr)
{
    queue_info_s c_plane,u_plane;

    switch (transfer_type) {
        case CU_SPLIT:
            c_plane.rx_block_size = 0x25800;
            c_plane.rx_block_num = 32;
            c_plane.rx_callback = NULL;
            c_plane.tx_block_size = 0x8000;
            c_plane.tx_block_num = 8;
            c_plane.tx_callback = NULL;

            u_plane.rx_block_size = 0x28000;
            u_plane.rx_block_num = 8;
            //u_plane.rx_callback = rx_callback_data;
            if (0 == inst_id)
            {
                u_plane.rx_callback = NULL;
            }
            else
            {
                u_plane.rx_callback = NULL;
            }
            u_plane.tx_block_size = 0x28000;
            u_plane.tx_block_num = 8;
            u_plane.tx_callback = NULL;

            transfer_type_info_ptr->queue_cplane_info = c_plane;
            transfer_type_info_ptr->queue_uplane_info = u_plane;
            break;
        case OAM:
            c_plane.rx_block_size = 0x100000;
            c_plane.rx_block_num = 32;
            c_plane.rx_callback = rx_callback_oam;
            c_plane.tx_block_size = 0x8000;
            c_plane.tx_block_num = 8;
            c_plane.tx_callback = NULL;

            transfer_type_info_ptr->queue_cplane_info = c_plane;
            break;
        default:
            UCP_PRINT_ERROR("get_msg_queue_cfg doesn't support transfer_type[%d] .",transfer_type);
            break;
    }

    return;
}

static inline void msg_transfer_cfg(void)
{
    uint8_t port_id = 0;
    uint16_t inst_id = 0;
    uint16_t transfer_type = 0;
    int32_t handle_id = 0;
    transfer_type_info_s transfer_type_info;

    for (inst_id=0; inst_id<MAX_INSTANCE_NUM; inst_id++) {
        /*********************************transfer_type = CU_SPLIT**********************************/
        transfer_type = CU_SPLIT;
        get_msg_transfer_info(port_id,inst_id,transfer_type, &transfer_type_info);
        handle_id = msg_transfer_init(port_id, transfer_type, inst_id, &transfer_type_info);
        if (handle_id < 0) {
            UCP_PRINT_ERROR("phy_cfg_init transfer_type:CU_SPLIT, handle_id[0x%08x] error.",handle_id);
        }

        /*********************************transfer_type = OAM**********************************/
        transfer_type = OAM;
        get_msg_transfer_info(port_id,inst_id,transfer_type, &transfer_type_info);
        handle_id = msg_transfer_init(port_id, transfer_type, inst_id, &transfer_type_info);
        if (handle_id < 0) {
            UCP_PRINT_ERROR("phy_cfg_init transfer_type:OAM, handle_id[0x%08x] error.",handle_id);
        }
    }

    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    pPetSmLocalMgt->pSyncInfo->queueCfgFlag = ++pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag;

    return;
}

static inline void msg_transfer_queue_polling(void)
{
    uint8_t port_id = 0;
    HandleId_t handler;
    uint16_t cu_flag;
    uint32_t offset = 0;
    uint32_t len = 0;
    uint8_t* msg_ptr;

    for (uint32_t 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);
    }

    return;
}

OSP_FUNCPTR arm_log_test()
{
    char  abuf1[128] = "test1234test1234test1234test1234test1234test1234test1234test1234test1234test1234test1234test1234test1234test1234test1234test567";
    char  abuf2[10]   = "abcdefg\n";    

    osp_write_diaglog(abuf1, strlen(abuf1), 13, 0x61);    
    osp_write_diaglog(abuf2, strlen(abuf2), 13, 0x60);          

    return OSP_OK;
        
}

int32_t test_case(uint32_t argc, int32_t* argvp)
{
	UCP_PRINT_DEBUG("start running testcase 3.");

    msg_transfer_mem_init();
    msg_transfer_cfg();
    ucp_handshake();
	
	UCP_PRINT_DEBUG("start transfering message.");

    while(1) {
        usleep(500);
        msg_transfer_queue_polling();
    }
    
    return 0;
}

