/**
@file log_debug_cmd.c
@brief Getting debugging Commands\n
 

## changelog ##
______
Data        | Vesion   | Change Description
------------|----------|-------------------------------------
20221104    | 1.0      | First release
 
*/


/********************************************************************************************
 Debug Switch Definition Section                          
********************************************************************************************/


/********************************************************************************************
Include File Section                                    
********************************************************************************************/
#include "log_creat.h"
#include "main.h"
#include "log.h"
#include <string.h>

/********************************************************************************************
Macro Definition Section 
********************************************************************************************/
#define POWER_DEBUG(...)   	LOG_OUTPUT_DEBUG(power,__VA_ARGS__);
/********************************************************************************************
Prototype Declaration Section                                   
********************************************************************************************/
void vWDTCmdParseProcess(void);
void vGWCmdParseProcess(void);
void vCycleGetDebugCmd(void);
//void vRPMSGCmdParseProcess(void);
//void vSreDesCmdParseProcess(void);
//extern void vGatewayCmdParseProcess(void);
extern void vParsingStringProcess(void *g_aucGetDebugCmd,uint8_t pcmdStr[][MAXSINGELSTRINGLENGTH],uint8_t rowsub,uint8_t colsub);

/********************************************************************************************
Const Global Variable Definition Section                        
********************************************************************************************/
typedef enum 
{
    IpclTxData = 0,
    IpclRxData = 1,
    IpclInviadDir = 0xFF
}IpclDebugDirType;

typedef enum 
{
    IpclDriverSend = 0,
    IpclE2E = 1,
    IpclDomain = 2,
    IpclPayload = 3,
    IpclInviadDebug = 0xFF
}IpclDebugType;

typedef enum 
{
    MCU_TO_CP_TIME = 0,
    CP_TO_MCU_TIME = 1,
    all_open       = 2,
    InviadTIME = 0xFF
}IpclDebugTIMEType;


/********************************************************************************************
Global Variable Definition Section                              
********************************************************************************************/

uint8_t g_aucGetDebugCmd[SHELL_BUFFER_LENGTH] = {0};
uint8_t g_aucArgv[MAXDEGPARAMNUM][MAXSINGELSTRINGLENGTH] = {0};
uint32_t g_RpmsgDebugMask = 0;
uint8_t SerDesDebugCmd =0;
char SerDesRegbuff[MAXDEGPARAMNUM][MAXSINGELSTRINGLENGTH];
/********************************************************************************************
Static Variable Definition Section                              
********************************************************************************************/


/********************************************************************************************
Structure Type Definition Section                               
********************************************************************************************/


/********************************************************************************************
Function Definition Section                                     
********************************************************************************************/

//dd debug command handler functions for each function acquisition command
// The cmd processing of SafetyOS cannot be modified.

/********************Cmd processing used by SafetyOS start********************/
/** 
@brief Parsing WDT debugging commands.
@return void 
*/
 
void vWDTCmdParseProcess(void)
{
	
	memset(g_aucGetDebugCmd, 0, sizeof(g_aucGetDebugCmd));	
	memset(g_aucArgv, 0, sizeof(g_aucArgv));
	LOG_GETINPUTSTRING_PROCESS(wdt,g_aucGetDebugCmd,sizeof(g_aucGetDebugCmd));
	
	
	#if 0
	if(strlen((const char *)g_aucGetDebugCmd)!=0)
	{
		Queue_logSendToTask1(TASK1_EVT1,argv,MAXDEGPARAMNUM);
	}
	#endif
	
}

/** 
@brief Parsing Power debugging commands.
@return void 
*/
void vPwrCmdParseProcess(void)
{
	
	memset(g_aucGetDebugCmd, 0, sizeof(g_aucGetDebugCmd));	
	memset(g_aucArgv, 0, sizeof(g_aucArgv));
	LOG_GETINPUTSTRING_PROCESS(wdt,g_aucGetDebugCmd,sizeof(g_aucGetDebugCmd));
	
	
	#if 0
	if(strlen((const char *)g_aucGetDebugCmd)!=0)
	{
		Queue_logSendToTask1(TASK1_EVT1,argv,MAXDEGPARAMNUM);
	}
	#endif
	
}

/**
@brief Parsing GW debugging commands. 
@return void
*/
 
void vGWCmdParseProcess(void)
{
	memset(g_aucGetDebugCmd, 0, sizeof(g_aucGetDebugCmd));	
	memset(g_aucArgv, 0, sizeof(g_aucArgv));
	LOG_GETINPUTSTRING_PROCESS(power,g_aucGetDebugCmd,sizeof(g_aucGetDebugCmd));
	
    vParsingStringProcess(g_aucGetDebugCmd, g_aucArgv, MAXDEGPARAMNUM, MAXSINGELSTRINGLENGTH);
    if(strlen((const char *)g_aucGetDebugCmd)!=0)
    {
        if(strcmp(((char*)g_aucArgv[0]) , "update") == 0) /**< Driver Layer Debug */
        {
             if(strcmp(((char*)g_aucArgv[1]) , "1") == 0)
            {
                POWER_DEBUG("[PwrCMD]Start APU update, stop poweroff/forces5/reset/hot");
            }
            else if(strcmp(((char*)g_aucArgv[1]) , "0") == 0)
            {
                POWER_DEBUG("[PwrCMD]End APU update, recover poweroff/forces5/reset/hot");
            }
			else
			{
			;// do nothing
			}
        }
    }
	return;
}

/** IpclDebug variable*/
 IpclDebugDirType ucIpclDebugFlag = IpclInviadDir;
 IpclDebugType ucIpclDebugType = IpclInviadDebug;
 IpclDebugTIMEType ucIpclDebugTIMEType = InviadTIME;
 char ucDebugBuf[3][MAXSINGELSTRINGLENGTH];

/*
@brief Ipcl Master debugging commands. 
@return void
*/
void vIPCLCmdParseProcess(void)
{
	memset(g_aucGetDebugCmd, 0, sizeof(g_aucGetDebugCmd));	
	memset(g_aucArgv, 0, sizeof(g_aucArgv));
	LOG_GETINPUTSTRING_PROCESS(ipcl,g_aucGetDebugCmd,sizeof(g_aucGetDebugCmd));

    vParsingStringProcess(g_aucGetDebugCmd, g_aucArgv, MAXDEGPARAMNUM, MAXSINGELSTRINGLENGTH);
    if(strlen((const char *)g_aucGetDebugCmd)!=0)
    {
        if(strcmp(((char*)g_aucArgv[0]) , "dv") == 0) /**< Driver Layer Debug */
        {
            ucIpclDebugType =0;// IpclDriverSend;
             if(strcmp(((char*)g_aucArgv[1]) , "") != 0)
            {
                memcpy(ucDebugBuf[0] , g_aucArgv[1] , MAXSINGELSTRINGLENGTH);
            }
            if(strcmp(((char*)g_aucArgv[2]) , "") != 0)
            {
                memcpy(ucDebugBuf[1] , g_aucArgv[2] , MAXSINGELSTRINGLENGTH);
            }
            if(strcmp(((char*)g_aucArgv[3]) , "") != 0)
            {
                memcpy(ucDebugBuf[2] , g_aucArgv[3] , MAXSINGELSTRINGLENGTH);
            }
        }
        else if(strcmp(((char*)g_aucArgv[0]) , "e2e") == 0) /**< E2E TX Domain Layer Debug */
        {
            ucIpclDebugType = 1;//IpclE2E;
            if(strcmp(((char*)g_aucArgv[1]) , "") != 0)
            {
                memcpy(ucDebugBuf[0] , g_aucArgv[1] , MAXSINGELSTRINGLENGTH);
            }
            if(strcmp(((char*)g_aucArgv[2]) , "") != 0)
            {
                memcpy(ucDebugBuf[1] , g_aucArgv[2] , MAXSINGELSTRINGLENGTH);
            }
        }
        else if(strcmp(((char*)g_aucArgv[0]) , "dm") == 0) /**< Domain Layer Debug */
        {
            ucIpclDebugType = 2;//IpclDomain;
            if(strcmp(((char*)g_aucArgv[1]) , "") != 0)
            {
                if(strcmp(((char*)g_aucArgv[1]) , "tx") == 0)
                {
                    ucIpclDebugFlag = 0;//IpclTxData;
                }
                else if(strcmp(((char*)g_aucArgv[1]) , "rx") == 0)
                {
                    ucIpclDebugFlag = 1;//IpclRxData;
                }
				else
				{
				;// do nothing
				}
            }
            if(strcmp(((char*)g_aucArgv[2]) , "") != 0)
            {
                memcpy(ucDebugBuf[0] , g_aucArgv[2] , MAXSINGELSTRINGLENGTH);
            }

            if(strcmp(((char*)g_aucArgv[3]) , "") != 0)
            {
                memcpy(ucDebugBuf[1] , g_aucArgv[3] , MAXSINGELSTRINGLENGTH);
            }
        }
        else if(strcmp(((char*)g_aucArgv[0]) , "pl") == 0) /**< Paylaod Layer Debug */
        {
            ucIpclDebugType = IpclPayload;
            if(strcmp(((char*)g_aucArgv[1]) , "") != 0)
            {
                if(strcmp(((char*)g_aucArgv[1]) , "tx") == 0)
                {
                    ucIpclDebugFlag = 0;//IpclTxData;
                }
                else if(strcmp(((char*)g_aucArgv[1]) , "rx") == 0)
                {
                    ucIpclDebugFlag = 1;//IpclRxData;
                }
				else
				{
				;// do nothing
				}
            }
            if(strcmp(((char*)g_aucArgv[2]) , "") != 0)
            {
                memcpy(ucDebugBuf[0] , g_aucArgv[2] , MAXSINGELSTRINGLENGTH);
            }

            if(strcmp(((char*)g_aucArgv[3]) , "") != 0)
            {
                memcpy(ucDebugBuf[1] , g_aucArgv[3] , MAXSINGELSTRINGLENGTH);
            }
        }
        else if (strcmp(((char*)g_aucArgv[0]) , "spd2cp") == 0)
		{
			ucIpclDebugTIMEType =0; //MCU_TO_CP_TIME;
		}
		else if (strcmp(((char*)g_aucArgv[0]) , "spd2mcu") == 0)
		{
			ucIpclDebugTIMEType =1; //CP_TO_MCU_TIME;
		}
		else if (strcmp(((char*)g_aucArgv[0]) , "spd") == 0)
		{
			ucIpclDebugTIMEType = 2;//all_open;
		}
		else
        {
            /* do nothing */
        }
    }
}

/**
@brief Parsing debugging commands. 
@return void
@note The cmd processing of SafetyOS cannot be modified.
*/

void vCycleGetDebugCmd(void)
{
	vWDTCmdParseProcess(); //wdt
	vGWCmdParseProcess(); //wdt
    vIPCLCmdParseProcess();//ipcl
    vPwrCmdParseProcess();
	//vRPMSGCmdParseProcess(); //rpmsg
	//vSreDesCmdParseProcess();
	//vGatewayCmdParseProcess()
}





