/******************************************************************************
 Prototype      : CpuView_GetTaskName
 Description    : 获取任务ID和NAME的对应关系
 Input          :
 Output         :
 Return Value   :
 Calls          :
 Called By      :

 History        : ---
  1.Date        : 2007-1-24
    Author      : g45205
    Modification: Created function
******************************************************************************/
VOS_UINT32 CpuView_GetTaskName(VOS_VOID)
{
    VOS_UINT32                  ulloop;
    CPU_TASK_NAME_STRU         *pTaskName;
    int                         lTaskNumber;
    int                         alVxWorks_TID_list[CPU_VIEW_MAX_TASK_NUMBER];
    char                       *pcTaskName;

    lTaskNumber = OS_GetTaskIdList( alVxWorks_TID_list, CPU_VIEW_MAX_TASK_NUMBER );
    if (( 0 >= lTaskNumber ) || (CPU_VIEW_MAX_TASK_NUMBER < lTaskNumber))
    {
        CPU_VIEW_LOG_PRINT1("CpuView_GetTaskName fail.TaskNumber outof range:%d\r\n", lTaskNumber);
        return VOS_ERR;
    }

    memset((void *)g_astCpuTaskName, 0, sizeof(CPU_TASK_NAME_STRU) * CPU_VIEW_MAX_TASK_NUMBER);

    for ( ulloop = 0; ulloop < (VOS_UINT32)lTaskNumber; ulloop++ )
    {
        pTaskName       = &g_astCpuTaskName[ulloop];
        pTaskName->pTcb = (CPUVIEW_TCB *)alVxWorks_TID_list[ulloop];
        pcTaskName      = OS_GetTaskName((VOS_UINT32)alVxWorks_TID_list[ulloop]);

        if (VOS_NULL_PTR != pcTaskName)
        {
            strncpy(pTaskName->aucTaskName, pcTaskName, CPU_VIEW_TASK_NAME_LEN);
        }
    }

    g_ulTaskNumber  = (VOS_UINT32)lTaskNumber;

    return VOS_OK;
} /* CpuView_GetTaskName */
VOS_VOID CpuView_StopTimer(VOS_VOID)
{
    VOS_INT32         lValue;

    /* disenable CPU_VIEW timer,时钟频率为19.2MHz */
    lValue = DRV_TIMER_STOP(CPU_VIEW_CLK_ID);
    if (0 > lValue)
    {
        CPU_VIEW_LOG_PRINT1("CpuView_StopTimer Stop Timer Fail:%d\r\n", lValue);
    }

    return;
}
VOS_VOID CpuView_StartTimer(VOS_VOID)
{
    VOS_INT32         lValue;

    /* enable CPU_VIEW timer,时钟频率为19.2MHz */
    lValue = DRV_TIMER_START(CPU_VIEW_CLK_ID, VOS_NULL, 0, TIMER_MAX_VALUE, TIMER_PERIOD_COUNT, TIMER_UNIT_NONE);
    if (0 > lValue)
    {
        CPU_VIEW_LOG_PRINT1("CpuView_StartTimer Start Timer Fail:%d\r\n", lValue);
    }

    return;
}
/******************************************************************************
 Prototype      : CpuView_BaseAddrPrint
 Description    : 打印CPU_VIEW所使用的地址
 Input          :
 Output         :
 Return Value   :
 Calls          :
 Called By      :

 History        : ---
  1.Date        : 2007-1-24
    Author      : g45205
    Modification: Created function
******************************************************************************/
VOS_VOID CpuView_InfoPrint(VOS_VOID)
{
    if ((CPU_VIEW_NO == g_ulCpuViewInitFlag) || (VOS_NULL == g_pstCpuViewCtrl))
    {
        CPU_VIEW_LOG_PRINT("CpuView is not initialized!\r\n");
        return;
    }

    CPU_VIEW_LOG_PRINT1("CpuViewCtrl Base Addr:0x%x\r\n", (VOS_INT)g_pstCpuViewCtrl);
    CPU_VIEW_LOG_PRINT1("CpuView Base Addr:0x%x\r\n", (VOS_INT)g_pstCpuViewCtrl->pstCpuView);

    CPU_VIEW_LOG_PRINT1("CpuView Mode:%d\r\n", (VOS_INT)g_pstCpuViewCtrl->ulCpuViewMode);
    CPU_VIEW_LOG_PRINT1("CpuView TraceCnt:%d\r\n", (VOS_INT)g_pstCpuViewCtrl->ulCpuViewTraceCnt);
    CPU_VIEW_LOG_PRINT1("CpuView Cnt:%d\r\n", (VOS_INT)g_pstCpuViewCtrl->ulCpuViewCnt);
    CPU_VIEW_LOG_PRINT1("CpuView OC:%d\r\n", (VOS_INT)g_pstCpuViewCtrl->ulCpuViewOC);
    CPU_VIEW_LOG_PRINT1("CpuView TaskNumber:%d\r\n", (VOS_INT)g_pstCpuViewCtrl->ulTaskNumber);

    return;
}
VOS_VOID CpuView_StopTimer(VOS_VOID)
{
    VOS_INT32         lValue;

    if (CPU_VIEW_NO == g_ulCpuViewTimerFlag)
    {
        CPU_VIEW_LOG_PRINT("CpuView_StartTimer Timer is already Stopped!\r\n");
        return;
    }

    /* disenable CPU_VIEW timer,时钟频率为19.2MHz */
    lValue = mdrv_timer_stop(CPU_VIEW_CLK_ID);
    if (0 > lValue)
    {
        CPU_VIEW_LOG_PRINT1("CpuView_StopTimer Stop Timer Fail:%d\r\n", lValue);
        return;
    }

    g_ulCpuViewTimerFlag = CPU_VIEW_NO;

    return;
}
VOS_VOID CpuView_StartTimer(VOS_VOID)
{
    VOS_INT32         lValue;

    if (CPU_VIEW_YES == g_ulCpuViewTimerFlag)
    {
        CPU_VIEW_LOG_PRINT("CpuView_StartTimer Timer is already Started!\r\n");
        return;
    }

    /* enable CPU_VIEW timer,时钟频率为19.2MHz */
    lValue = mdrv_timer_start(CPU_VIEW_CLK_ID, VOS_NULL, 0, TIMER_MAX_VALUE, TIMER_PERIOD_COUNT, TIMER_UNIT_NONE);
    if (0 > lValue)
    {
        CPU_VIEW_LOG_PRINT1("CpuView_StartTimer Start Timer Fail:%d\r\n", lValue);
        return;
    }

    g_ulCpuViewTimerFlag = CPU_VIEW_YES;

    return;
}
/******************************************************************************
 Prototype      : CpuView_Dump
 Description    : 输出结果到主机文件上
 Input          :
 Output         :
 Return Value   :
 Calls          :
 Called By      :

 History        : ---
  1.Date        : 2007-1-24
    Author      : g45205
    Modification: Created function
******************************************************************************/
VOS_VOID CpuView_Dump(VOS_UINT32 ulDumpMode)
{
    FILE                               *fp;
    VOS_CHAR                            acDumpFileName[500];
    static VOS_UINT8                    s_ucDumpFileNO = 0;
    VOS_INT                             iRet;
    VOS_INT32                           lLockValue;

    /* cpu_View未初始化,则按照默认值进行初始化 */
    if (CPU_VIEW_NO == g_ulCpuViewInitFlag)
    {
        /* 按照默认值初始化 */
        CpuView_Init(CPU_VIEW_MEMORY_MODE_DYNAMIC, 0);

        /* 非循环模式 */
        CpuView_Start(CPU_VIEW_RECORD_MODE_CYCLE_NO);

        /* 等待10s */
        CPU_VIEW_TASK_DELAY(1000);
    }

    /* cpu_View未开始,则按照默认值开始 */
    if (CPU_VIEW_NO == g_ulCpuViewStartFlag)
    {
        /* 非循环模式 */
        CpuView_Start(CPU_VIEW_RECORD_MODE_CYCLE_NO);

        /* 等待10s */
        CPU_VIEW_TASK_DELAY(1000);
    }

    if ( (VOS_NULL == g_pstCpuView)
        || ((0 == g_ulCpuViewCnt) && (CPU_VIEW_RECORD_OVERTURN_NO == g_ulCpuViewOC)) )
    {
        CPU_VIEW_LOG_PRINT("CpuView_DumpTrace, No Trace need dump!\r\n");
        return;
    }

    CpuView_End();

    CPU_VIEW_LOG_PRINT("CpuView Dump Begin ............\r\n");

    lLockValue = VOS_SplIMP();

    if (CPU_VIEW_DUMP_GEN_MODE_FLASH == ulDumpMode)    /* 输出到FLASH */
    {
#if (FEATURE_ON == FEATURE_MULTI_FS_PARTITION) /* SFT board*/
        sprintf(acDumpFileName, "/data/hisi_logs/cp_log/coredump/CpuView%d.dump", s_ucDumpFileNO);
#else
        sprintf(acDumpFileName, "/yaffs0/CpuView%d.dump", s_ucDumpFileNO);
#endif

        fp  = mdrv_file_open(acDumpFileName, "wb");
    }
    /* 暂不支持输出到网卡 */
#if 0
    else if (CPU_VIEW_DUMP_GEN_MODE_NETCARD == ulDumpMode)   /* 输出到网卡 */
    {
        CPU_VIEW_LOG_PRINT("Dump to Host!\r\n");
        fp  = DRV_FILE_OPEN("host:CpuView.dump", "wb");
    }
#endif
    else
    {
        VOS_Splx(lLockValue);
        CPU_VIEW_LOG_PRINT("Not support Dump file mode,Please check again!\r\n");

        return;
    }

    if (VOS_NULL == fp)
    {
        VOS_Splx(lLockValue);
        CPU_VIEW_LOG_PRINT("Open File Fail!\r\n");

        return;
    }

    iRet = mdrv_file_write(g_pstCpuViewCtrl, sizeof(CPU_VIEW_CTRL_STRU), 1, fp);
    if (-1 ==  iRet)
    {
        mdrv_file_close(fp);
        VOS_Splx(lLockValue);
        CPU_VIEW_LOG_PRINT("Write Control Info Fail!\r\n");

        return;
    }

    iRet = mdrv_file_write(g_pstCpuView, sizeof(CPU_VIEW_STRU), g_ulCpuViewTraceCnt, fp);
    if (-1 ==  iRet)
    {
        mdrv_file_close(fp);
        VOS_Splx(lLockValue);
        CPU_VIEW_LOG_PRINT("Write Cpu View Fail!\r\n");

        return;
    }

    mdrv_file_close(fp);

    VOS_Splx(lLockValue);

    CPU_VIEW_LOG_PRINT("CpuView Dump End ............\r\n");
    CPU_VIEW_LOG_PRINT1("Dump File Name:%s......\r\n", (VOS_INT)acDumpFileName);

    s_ucDumpFileNO++;

    return;
} /* CpuView_Dump */