
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include "osp.h"



uint64_t *g_DiagCntBase = 0;
#define OSP_DIAGCNT_LEN (10*1024)

static OSP_TASKCB *TaskCBBase;


/**************************************************************************


**************************************************************************/

int32_t osp_diag_cnt_init(void)
{
    g_DiagCntBase = (uint64_t *)osp_get_init_mem(OSP_MAX_TASK * sizeof(uint64_t) * CNT_NUM_PER_TASK);
    memset(g_DiagCntBase, 0, OSP_MAX_TASK * sizeof(uint64_t) * CNT_NUM_PER_TASK);
    TaskCBBase = osp_get_taskcb_base();
    return OSP_OK;
}

OSP_STATUS osp_diag_cnt_add(uint32_t TaskId, uint32_t CntId)
{
    TASK_CNT_ADD(TaskId, CntId);
    return OSP_OK;
}

OSP_STATUS osp_diag_cnt_set(uint32_t TaskId, uint32_t CntId, uint64_t Val)
{
    TASK_CNT_SET(TaskId, CntId,Val);
    return OSP_OK;
    
}
uint64_t osp_diag_cnt_get(uint32_t TaskId, uint32_t CntId)
{
    return TASK_CNT_GET(TaskId, CntId);
}

OSP_STATUS osp_show_tsk_diag_cnt(uint32_t TaskId)
{
    char *pstr = NULL;
    uint32_t len = OSP_DIAGCNT_LEN;
    uint32_t tmplen = 0;
//    OSP_STATUS ret;

    if (!TaskCBBase[TaskId].Active)
    {
        osp_debug_out(CMD_DEBUG_LEVEL, "Task:%d is not active\n", TaskId);
        return OSP_ERROR;
    }
    
    pstr = malloc(len);
    osp_assert(NULL != pstr);
    
    tmplen = (uint32_t)sprintf(pstr, "%-16s%-16s%-16s\r\n", "OspTaskId", "OspCntIdx", "OspCntValue");
    tmplen += osp_diag_cnt_str(TaskId, pstr + tmplen, len - tmplen);

    
    pstr[tmplen] = '\0';
    osp_debug_out(CMD_DEBUG_LEVEL, "%s", pstr);
    free(pstr);

    return OSP_OK;
}
OSP_STATUS osp_show_all_diag_cnt(void)
{
    char *pstr = NULL;
    uint32_t len = OSP_DIAGCNT_LEN;
    uint32_t tmplen = 0;
//    U32 outlen = 0;
//    OSP_STATUS ret;
    uint32_t TaskId;


    
    pstr = malloc(len);
    osp_assert(NULL != pstr);
    
    tmplen = (uint32_t)sprintf(pstr, "%-16s%-16s%-16s\r\n", "OspTaskId", "OspCntIdx", "OspCntValue");
    for (TaskId = 0; TaskId < OSP_MAX_TASK; TaskId++)
    {
        if (!TaskCBBase[TaskId].Active)
        {
            continue;
        }
        tmplen += osp_diag_cnt_str(TaskId, pstr + tmplen, len - tmplen);
    }

    
    pstr[tmplen] = '\0';
    osp_debug_out(CMD_DEBUG_LEVEL, "%s", pstr);
    free(pstr);
    return OSP_OK;
}


uint32_t osp_diag_cnt_str(uint32_t TaskId, char *pstr, uint32_t inlen)
{
    uint32_t CntId = 0;
    uint32_t len = 0;
    uint64_t CntVal;
    
    if ((!TaskCBBase[TaskId].Active) || (NULL == pstr) || (0 == inlen))
    {
        osp_assert(0);
    }
    
    for (CntId = 0; CntId < CNT_NUM_PER_TASK; CntId++)
    {
        CntVal = TASK_CNT_GET(TaskId,CntId);
        if (0 == CntVal)
        {
            continue;
        }
        if (inlen-len < 64)
        {
            osp_assert(0);
        }        
        len += sprintf(pstr + len,"%-16d%-16d%-16lu\r\n", TaskId, CntId, CntVal);

    }
    return len;
}


/**************************************************************************


**************************************************************************/

int32_t osp_diag_init(void)
{
    int32_t ret ;
    ret = osp_diag_cnt_init();
    if (OSP_OK != ret)
    {
        return ret;
    }
  
    return OSP_OK;
}
/**************************************************************************

**************************************************************************/

int32_t  osp_show_que_status(void)
{
    return OSP_OK;
}






