/******************************************************************
*  Copyright (C) 2023 - 2023 OneChip, Inc. All rights reserved.
* File name: van_bsp_debug_shell.c
* Author: fanyidong 	 Date:2020.11.21
* Description:		单片机仿shell函数
* Others:
* History:
        1. Date:
                 Author:
                 Modification:
*****************************************************************/


#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <van_bsp_debug_uart.h>

#include "onechip_include_header.h"



#define FYD_DEBUG_PRINT  Van_Device_Printf
	
#define VAN_PRINT_HIGH 0

typedef unsigned int (*fundef)(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *);

#define COMPARE_ALIGN  0
#define DISBUF_LEN 512


typedef struct
{
	char * functionname;
	fundef function;
} FUNCALLTYPE;

typedef struct
{
	char *	  variablename;
	unsigned int *variableaddr;
} VAREDITTYPE;

#define DEBUG_FUNFLAG (unsigned int)0x3a6e7566 // fun:
#define DEBUG_VERFLAG (unsigned int)0x3a726576 // ver:
#define DEBUG_CANFLAG (unsigned int)0x3a6e6163 // can:




unsigned int  gloublePrintLevel = VAN_LOG_NOTICE; //
unsigned int  gloublePrintSwitch = 0; //
unsigned char debugcmdbuf[132]	 = {0};
unsigned int  debugcmdbuflast	 = 0;
unsigned int  debugcmdbufheader	 = 0;

unsigned int debugtest1(int hexint1);
unsigned int debugtest2(int hexint1, int hexint2);


#define DEBUGFUNCTION(Function)     \
	{                               \
#Function, (fundef)Function \
	}

#define DEBUGVARIABLE(VARIABLE)          \
	{                                    \
#VARIABLE, (unsigned int *)&VARIABLE \
	}

extern bool set_io(unsigned int ioState);
unsigned int debugver;

FUNCALLTYPE debugFunctionList[] = {DEBUGFUNCTION(debugtest1),
//	DEBUGFUNCTION(Van_UART_Sys_Ctr_SendTest),

	 DEBUGFUNCTION(com_pwm_test),
	 DEBUGFUNCTION(show_com_pwm),
	// DEBUGFUNCTION(showPid),
	// DEBUGFUNCTION(Van_UART_Sys_Get_Debuging),
	{NULL, 0}};



extern int	printDrvStatusFlag;
extern unsigned int  onechip_log_level;
extern unsigned int  onechip_testflag;
extern unsigned int p_rec_flag;
extern unsigned int p_rec_data_flag;
extern unsigned int p_DecodeNew;
extern unsigned int p_recSystemMsg;
extern unsigned int p_Rep_flag;
extern unsigned int p_Rep_data_flag;
extern unsigned int p_Ans_flag;
extern unsigned int p_Ans_data_flag;
extern unsigned int p_Ans_flag_1;
extern unsigned int p_Ans_data_flag_1;
extern unsigned int print_chip_pwm;
extern unsigned int comAnsOutTimeDebug;
extern unsigned int dealOutTimeDebug;


extern uint32_t TMC5160SPIFlag;


extern uint32_t resetSysUart;


VAREDITTYPE debugVariableList[]
	= {DEBUGVARIABLE(debugver),
	// DEBUGVARIABLE(p_rec_flag),
	// DEBUGVARIABLE(p_rec_data_flag),
	// DEBUGVARIABLE(p_DecodeNew),
	// DEBUGVARIABLE(p_recSystemMsg),
	// DEBUGVARIABLE(p_Rep_flag),
	// DEBUGVARIABLE(p_Rep_data_flag),
	// DEBUGVARIABLE(p_Ans_flag),
	// DEBUGVARIABLE(p_Ans_data_flag),
	// DEBUGVARIABLE(p_Ans_flag_1),
	// DEBUGVARIABLE(p_Ans_data_flag_1),
	// DEBUGVARIABLE(comAnsOutTimeDebug),
	// DEBUGVARIABLE(resetSysUart),
	
	  {NULL, 0}};


unsigned int AsiicTorHexChar(const char *cmd, unsigned char *hexchar)
{
	unsigned char temp = 0;
	if (cmd == 0)
	{
		return 0;
	}
	if (('0' <= *cmd) && (*cmd <= '9'))
	{
		temp += *cmd - '0';
	}
	else if (('A' <= *cmd) && (*cmd <= 'F'))
	{
		temp += *cmd - '7';
	}
	else if (('a' <= *cmd) && (*cmd <= 'f'))
	{
		temp += *cmd - 'W';
	}
	else
	{
		return 1;
	}
	temp = temp * 0x10;
	cmd++;
	if (('0' <= *cmd) && (*cmd <= '9'))
	{
		temp += *cmd - '0';
	}
	else if (('A' <= *cmd) && (*cmd <= 'F'))
	{
		temp += *cmd - '7';
	}
	else if (('a' <= *cmd) && (*cmd <= 'f'))
	{
		temp += *cmd - 'W';
	}
	else
	{
		return 1;
	}
	*hexchar = temp;
	return 0;
}


unsigned int AsiicTorHexShort(const char *cmd, unsigned short *hexshort)
{
	unsigned short temp = 0;
	unsigned int   i;
	if (cmd == 0)
	{
		return 0;
	}
	for (i = 0; i < 4; i++)
	{
		temp = temp * 0x10;
		if (('0' <= *cmd) && (*cmd <= '9'))
		{
			temp += *cmd - '0';
		}
		else if (('A' <= *cmd) && (*cmd <= 'F'))
		{
			temp += *cmd - '7';
		}
		else if (('a' <= *cmd) && (*cmd <= 'f'))
		{
			temp += *cmd - 'W';
		}
		else
		{
			return 1;
		}
		cmd++;
	}

	*hexshort = temp;
	return 0;
}


unsigned int AsiicTorHexInt(const char *cmd, unsigned int *hexint)
{
	unsigned int temp = 0;
	if (cmd == 0)
	{
		return 0;
	}
	while (*cmd)
	{
		temp = temp * 0x10;
		if (('0' <= *cmd) && (*cmd <= '9'))
		{
			temp += *cmd - '0';
		}
		else if (('A' <= *cmd) && (*cmd <= 'F'))
		{
			temp += *cmd - '7';
		}
		else if (('a' <= *cmd) && (*cmd <= 'f'))
		{
			temp += *cmd - 'W';
		}
		else
		{
			return 1;
		}
		cmd++;
	}

	*hexint = temp;
	return 0;
}

unsigned int INTAsiicTorHexInt(const char *cmd, unsigned int *hexint)
{
	unsigned int temp	 = 0;
	unsigned int		 isMinus = 0;
	if (cmd == 0)
	{
		return 0;
	}
	if (*cmd == '-')
	{
		isMinus = 1;
		cmd++;
	}
	while (*cmd)
	{
		temp = temp * 10;
		if (('0' <= *cmd) && (*cmd <= '9'))
		{
			temp += *cmd - '0';
		}
		else
		{
			return 1;
		}
		cmd++;
	}
	if (isMinus)
	{
		*hexint = -temp;
	}
	else
	{
		*hexint = temp;
	}
	return 0;
}

unsigned int HexCharTorAsiic(unsigned char *asiccbuf, unsigned char hexchar)
{
	unsigned short temp = 0;
	unsigned int   i;
	if (asiccbuf == 0)
	{
		return 1;
	}
	for (i = 0; i < 2; i++)
	{
		temp = (hexchar & 0xf0) >> 4;
		if (temp < 10)
		{
			*asiccbuf = '0' + temp;
		}
		else
		{
			*asiccbuf = '7' + temp;
		}
		asiccbuf++;
		hexchar = hexchar << 4;
	}
	return 0;
}


unsigned int HexShortToAsiicr(char *asiccbuf, unsigned short hexshort)
{
	unsigned short temp = 0;
	unsigned int   i;
	if (asiccbuf == 0)
	{
		return 1;
	}
	for (i = 0; i < 4; i++)
	{
		temp = (hexshort & 0xf000) >> 12;
		if (temp < 10)
		{
			*asiccbuf = '0' + temp;
		}
		else
		{
			*asiccbuf = '7' + temp;
		}
		asiccbuf++;
		hexshort = hexshort << 4;
	}
	return 0;
}
unsigned int HexIntAsiicToAsiicr(char *asiccbuf, unsigned int hexint)
{
	unsigned int temp = 0;
	unsigned int i;
	if (asiccbuf == 0)
	{
		return 1;
	}
	for (i = 0; i < 8; i++)
	{
		temp = (hexint & 0xf0000000) >> 28;
		if (temp < 10)
		{
			*asiccbuf = '0' + temp;
		}
		else
		{
			*asiccbuf = '7' + temp;
		}
		asiccbuf++;
		hexint = hexint << 4;
	}
	return 0;
}

unsigned int IntAsiicToAsiicr(char *asiccbuf, int hexint)
{
	unsigned int temp = 0;
	unsigned int i	  = 0;
	if (asiccbuf == 0)
	{
		return 1;
	}
	if (hexint < 0)
	{
		*asiccbuf = '-';
		asiccbuf++;
		hexint = -hexint;
		i++;
	}
	temp = hexint / 1000000000;
	if (temp)
	{
		*asiccbuf = '0' + temp;
		hexint	  = hexint % 1000000000;
		i++;
	}
	temp = hexint / 100000000;
	if (temp)
	{
		*asiccbuf = '0' + temp;
		hexint	  = hexint % 100000000;
		i++;
	}
	temp = hexint / 10000000;
	if (temp)
	{
		*asiccbuf = '0' + temp;
		hexint	  = hexint % 10000000;
		i++;
	}
	temp = hexint / 1000000;
	if (temp)
	{
		*asiccbuf = '0' + temp;
		hexint	  = hexint % 1000000;
		i++;
	}
	temp = hexint / 100000;
	if (temp)
	{
		*asiccbuf = '0' + temp;
		hexint	  = hexint % 100000;
		i++;
	}
	temp = hexint / 10000;
	if (temp)
	{
		*asiccbuf = '0' + temp;
		hexint	  = hexint % 10000;
		i++;
	}
	temp = hexint / 1000;
	if (temp)
	{
		*asiccbuf = '0' + temp;
		hexint	  = hexint % 1000;
		i++;
	}
	temp = hexint / 100;
	if (temp)
	{
		*asiccbuf = '0' + temp;
		hexint	  = hexint % 100;
		i++;
	}
	temp = hexint / 10;
	if (temp)
	{
		*asiccbuf = '0' + temp;
		hexint	  = hexint % 10;
		i++;
	}
	*asiccbuf = '0' + temp;
	i++;

	return i;
}
unsigned int debugtest1(int hexint1)
{
	FYD_DEBUG_PRINT(VAN_PRINT_HIGH,"%s:pra1[0x%x]:\n\r", __func__,
	 hexint1);
	return (hexint1);
}
unsigned int debugtest2(int hexint1, int hexint2)
{
	FYD_DEBUG_PRINT(VAN_PRINT_HIGH,"%s:pra1[0x%x]:pra2[0x%x]:\n\r",
	__func__, hexint1, hexint2);
	return (hexint1 + hexint2);
}

int funnamecmp(unsigned char *callfun, unsigned char *funlist)
{
	while (*callfun && *funlist)
	{
		if (*callfun != *funlist)
		{
			return 0;
		}
		else
		{
			callfun++;
			funlist++;
		}
	}
	if (*callfun == 0 && *funlist == 0 )
		return 1;
	else
		return 0;
}
void debugcall(unsigned char *cmdbuf)
{
	unsigned char		 functionname[32] = {0};
	unsigned char		 parabuf[32]	  = {0};
	unsigned int	 i				  = 0;
	unsigned int	 j				  = 0;
	unsigned int	 k				  = 0;
	unsigned int	 para[10]		  = {0};
	unsigned int	 result			  = 0;
	unsigned int	 paraDecimal; //
	FUNCALLTYPE *findfun = debugFunctionList;
	while (*(cmdbuf + i) != '(' && *(cmdbuf + i))
	{
		functionname[i] = *(cmdbuf + i);
		i++;
	}
	if (*(cmdbuf + i) != '(')
	{
		return;
	}
	i++; //find '('
	for (j = 0; j < 10; j++)
	{
		paraDecimal = 1;
		k			= 0;
		if ((*(cmdbuf + i) == '0')
			&& (*(cmdbuf + i + 1) == 'x' || *(cmdbuf + i + 1) == 'X'))
		{
			i			= i + 2;
			paraDecimal = 0;
		}
		while (*(cmdbuf + i) != ',' && *(cmdbuf + i) != ')' && *(cmdbuf + i))
		{
			parabuf[k++] = *(cmdbuf + i);
			i++;
		}
		parabuf[k++] = '\0';
		if (paraDecimal)
		{
			INTAsiicTorHexInt((const char *)parabuf, &para[j]);
		}
		else
		{
			AsiicTorHexInt((const char *)parabuf, &para[j]);
		}
		if (*(cmdbuf + i) == ')')
		{
			break;
		}
		i++; //skip space
	}

	while (findfun->functionname)
	{
		if (funnamecmp(functionname, (unsigned char *)findfun->functionname))
		{
			break;
		}
		findfun++;
	}
	if (findfun->function)
	{
		result = (unsigned int)findfun->function((void *)para[0], (void *)para[1],
											 (void *)para[2], (void *)para[3],
											 (void *)para[4], (void *)para[5], 
											 (void *)para[6], (void *)para[7], 
											 (void *)para[8], (void *)para[9]);
		FYD_DEBUG_PRINT(VAN_PRINT_HIGH, "%s = 0x%x \r\n", functionname,
						result);
	}
	else
	{
		FYD_DEBUG_PRINT(VAN_PRINT_HIGH, "Pleas add %s to function list\n",
						functionname);
	}
}

void debugvarb(unsigned char *varbbuf)
{
	unsigned char		 variablename[32] = {0};
	unsigned char		 parabuf[32]	  = {0};
	unsigned int	 i				  = 0;
	unsigned int	 j				  = 0;
	unsigned int	 k				  = 0;
	unsigned int	 para[5]		  = {0, 0, 0, 0, 0};
	unsigned int	 paraDecimal;		//decmail flag
	unsigned int	 readwriteFlag = 0; //
	VAREDITTYPE *findvarb	   = debugVariableList;
	/*get fun name*/
	while (*(varbbuf + i) != '(')
	{
		variablename[i] = *(varbbuf + i);
		i++;
	}
	i++; //find (
	for (j = 0; j < 5; j++)
	{
		paraDecimal = 1;
		k			= 0;
		if ((*(varbbuf + i) == '0')
			&& (*(varbbuf + i + 1) == 'x' || *(varbbuf + i + 1) == 'X'))
		{
			i			= i + 2;
			paraDecimal = 0;
		}
		while (*(varbbuf + i) != ',' && *(varbbuf + i) != ')')
		{
			parabuf[k++] = *(varbbuf + i);
			i++;
			readwriteFlag = 1;
		}
		parabuf[k++] = '\0';
		if (paraDecimal)
		{
			INTAsiicTorHexInt((const char *)parabuf, &para[j]);
		}
		else
		{
			AsiicTorHexInt((const char *)parabuf, &para[j]);
		}
		if (*(varbbuf + i) == ')')
		{
			break;
		}
		i++; //find space
	}

	while (findvarb->variablename)
	{
		if (funnamecmp(variablename, (unsigned char *)findvarb->variablename))
		{
			break;
		}
		findvarb++;
	}
	if (findvarb->variableaddr)
	{
		if (readwriteFlag)
		{
			*(findvarb->variableaddr) = para[0];
		}
		FYD_DEBUG_PRINT(VAN_PRINT_HIGH, "%s = 0x%x\r\n", variablename,
						*(findvarb->variableaddr));
	}
	else
	{
		FYD_DEBUG_PRINT(VAN_PRINT_HIGH, "Pleas add %s to variable list\r\n",
						variablename);
	}
}

void debugcan(unsigned char *buf)
{
	unsigned char	 canbuf[32] = {0};
	unsigned char	 caninf[8]	= {0};
	unsigned int i			= 1; 
	unsigned int j			= 0;
	while (*(buf + i) != ')')
	{
		canbuf[j++] = *(buf + i);
		i++;
	}
	for (i = 0; i < 8; i++)
	{
		AsiicTorHexChar((const char *)&canbuf[2 * i], &caninf[i]);
	}
}
int cmppare_hear(unsigned char *buf, unsigned char *header ){
	if(buf[0]==header[0]&&buf[1]==header[1]&&buf[2]==header[2]&&buf[3]==header[3]){
		return 1;
	}
	return 0;

}
void decodedebugfunction(void)
{
	unsigned int  seekstart;
	unsigned char cmdbuf[64] = {0};
	unsigned int  i, j;
	unsigned int  cmdflag	= 0;
	unsigned int  buflen	= 0;
	unsigned int  buflencon = 0;
	unsigned int   dataleft = (debugcmdbuflast + 128 -debugcmdbufheader)%128;
	if(dataleft > 32){
		Van_Device_Printf(VAN_LOG_NOTICE, "debug buffer have:%d\n\r***", dataleft);
	}	
	if ((debugcmdbufheader + 6) < debugcmdbuflast)
	{
		seekstart = debugcmdbufheader;
#ifdef COMPARE_ALIGN
		while (0==cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"fun:"))
		{
			if((seekstart + 6) < debugcmdbuflast){
				seekstart++;
			}else{
				break;
			}
		}
#else
		while (*(unsigned int *)&debugcmdbuf[seekstart] != DEBUG_FUNFLAG
			   && (seekstart + 6) < debugcmdbuflast)
		{
			seekstart++;
		}
#endif	

#ifdef COMPARE_ALIGN
		if (cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"fun:"))
#else
		if (*(unsigned int *)&debugcmdbuf[seekstart] == DEBUG_FUNFLAG)
#endif				
		{
			i		  = seekstart + 4;
			j		  = 0;
			buflen	  = debugcmdbuflast - i;
			buflencon = 0;
			while (debugcmdbuf[i] != ')' && buflencon < buflen)
			{
				cmdbuf[j++] = debugcmdbuf[i++];
				buflencon++;
			}
			if (debugcmdbuf[i] == ')')
			{
				cmdflag		= 1;
				cmdbuf[j++] = ')';
				debugcmdbufheader = i;
			}
			i++;
		}
	}
	else if (((debugcmdbufheader + 6) < (debugcmdbuflast + 128))
			 && (debugcmdbufheader > debugcmdbuflast))
	{
		seekstart = debugcmdbufheader;
		for (; ((seekstart + 6) < (debugcmdbuflast + 128)) && seekstart < 128;)
		{
#ifdef COMPARE_ALIGN
			if (0==cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"fun:"))
#else
			if (*(unsigned int *)&debugcmdbuf[seekstart] != DEBUG_FUNFLAG)
#endif							
			{
				seekstart++;
			}
			else
			{
				break;
			}
		}
#ifdef COMPARE_ALIGN
		if (cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"fun:"))
#else
		if (*(unsigned int *)&debugcmdbuf[seekstart] == DEBUG_FUNFLAG)
#endif		

		{
			i = (seekstart + 4) % 128;
			j = 0;
			if (i < debugcmdbuflast)
			{
				buflen = debugcmdbuflast - i;
			}
			else
			{
				buflen = debugcmdbuflast + 128 - i;
			}
			buflencon = 0;
			while (debugcmdbuf[i % 128] != ')' && buflencon < buflen)
			{
				cmdbuf[j++] = debugcmdbuf[i % 128];
				buflencon++;
				i++;
			}
			i = i % 128;
			if (debugcmdbuf[i] == ')')
			{
				cmdflag		= 1;
				cmdbuf[j++] = ')';
				debugcmdbufheader = i;
			}
			i++;
		}
		else
		{
			seekstart = 0;
			for (; (seekstart + 6) < (debugcmdbuflast);)
			{
#ifdef COMPARE_ALIGN
				if (0==cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"fun:"))
#else
				if (*(unsigned int *)&debugcmdbuf[seekstart] != DEBUG_FUNFLAG)
#endif			
				{
					seekstart++;
				}
#ifdef COMPARE_ALIGN
				if (cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"fun:"))
#else
				if (*(unsigned int *)&debugcmdbuf[seekstart] == DEBUG_FUNFLAG)
#endif			
				{
					i = (seekstart + 4);
					j = 0;
					if (i < debugcmdbuflast)
					{
						buflen = debugcmdbuflast - i;
					}
					else
					{
						buflen = debugcmdbuflast + 128 - i;
					}
					buflencon = 0;

					while (debugcmdbuf[i] != ')' && buflencon < buflen)
					{
						cmdbuf[j++] = debugcmdbuf[i];
						i++;
						buflencon++;
					}
					if (debugcmdbuf[i] == ')')
					{
						cmdflag		= 1;
						cmdbuf[j++] = ')';
						debugcmdbufheader = i;
					}
					i++;
					break;
				}
			}
		}
	}
	if (cmdflag)
	{
		debugcall(cmdbuf);
	}
}

void decodedebugvariable(void)
{
	unsigned int  seekstart;
	unsigned char varbuf[64];
	unsigned int  i, j;
	unsigned int  cmdflag = 0;
	unsigned int  buflencon = 0; 
	unsigned int  buflen = 0;
	if ((debugcmdbufheader + 6) < debugcmdbuflast)
	{
		seekstart = debugcmdbufheader;
#ifdef COMPARE_ALIGN
		while (0==cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"ver:"))
		{
			if((seekstart + 6) < debugcmdbuflast){
				seekstart++;
			}else{
				break;
			}
		}
#else
		while (*(unsigned int *)&debugcmdbuf[seekstart] != DEBUG_VERFLAG
			   && (seekstart + 6) < debugcmdbuflast)
		{
			seekstart++;
		}
#endif		
		
#ifdef COMPARE_ALIGN
				if (cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"ver:"))
#else
				if (*(unsigned int *)&debugcmdbuf[seekstart] == DEBUG_VERFLAG)
#endif			
		{
			i		= seekstart + 4;
			j		= 0;
			while (debugcmdbuf[i] != ')' && i < debugcmdbuflast) { varbuf[j++] = debugcmdbuf[i++]; }
			if(debugcmdbuf[i] == ')'){
				cmdflag = 1;				
				varbuf[j++] = ')';
				debugcmdbufheader = i;

			}
			i++;
		}
	}
	else if (((debugcmdbufheader + 6) < (debugcmdbuflast + 128))
			 && (debugcmdbufheader > debugcmdbuflast))
	{
		seekstart = debugcmdbufheader;
		for (; ((seekstart + 6) < (debugcmdbuflast + 128)) && seekstart < 128;)
		{
#ifdef COMPARE_ALIGN
		    if (0 == cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"ver:"))
#else
			if (*(unsigned int *)&debugcmdbuf[seekstart] != DEBUG_VERFLAG)
#endif	
			{
				seekstart++;
			}
			else
			{
				break;
			}
		}
#ifdef COMPARE_ALIGN
		if (cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"ver:"))
#else
		if (*(unsigned int *)&debugcmdbuf[seekstart] == DEBUG_VERFLAG)
#endif			
		
		{
			i		= (seekstart + 4) % 128;
			j		= 0;
			if (i < debugcmdbuflast)
			{
				buflen = debugcmdbuflast - i;
			}
			else
			{
				buflen = debugcmdbuflast + 128 - i;
			}
			buflencon = 0; 
			while (debugcmdbuf[i % 128] != ')' && buflencon < buflen)
			{
				varbuf[j++] = debugcmdbuf[i % 128];
				i++;
			}
			if(debugcmdbuf[i % 128] == ')'){				
				cmdflag = 1;
				varbuf[j++] = ')';
				debugcmdbufheader = i % 128;
			}
			i++;
		}
		else
		{
			seekstart = 0;
			for (; (seekstart + 6) < (debugcmdbuflast);)
			{
#ifdef COMPARE_ALIGN
			if (0 == cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"ver:"))
#else
			if (*(unsigned int *)&debugcmdbuf[seekstart] != DEBUG_VERFLAG)
#endif			
				
				{
					seekstart++;
				}
#ifdef COMPARE_ALIGN
			if (cmppare_hear(&debugcmdbuf[seekstart],(unsigned char *)"ver:"))
#else
			if (*(unsigned int *)&debugcmdbuf[seekstart] == DEBUG_VERFLAG)
#endif		
			{
					i		= (seekstart + 4);
					j		= 0;
					if (i < debugcmdbuflast)
					{
						buflen = debugcmdbuflast - i;
					}
					else
					{
						buflen = debugcmdbuflast + 128 - i;
					}
					buflencon = 0; 
					while (debugcmdbuf[i] != ')'&& buflencon < buflen)
					{
						varbuf[j++] = debugcmdbuf[i];
						i++;
					}
					if(debugcmdbuf[i % 128] == ')'){				
						cmdflag = 1;
						varbuf[j++] = ')';
						debugcmdbufheader = i % 128;
					}
					i++;
					break;
				}
			}
		}
	}
	if (cmdflag)
	{
		debugvarb(varbuf);
	}
}

void decodedebugCAN(unsigned char *caninf)
{
	unsigned int  seekstart;
	unsigned char canbuf[64];
	unsigned int  i, j;
	unsigned int  cmdflag = 0;
	if ((debugcmdbufheader + 6) < debugcmdbuflast)
	{
		seekstart = debugcmdbufheader;
		while (*(unsigned int *)&debugcmdbuf[seekstart] != DEBUG_CANFLAG
			   && (seekstart + 6) < debugcmdbuflast)
		{
			seekstart++;
		}
		if (*(unsigned int *)&debugcmdbuf[seekstart] == DEBUG_CANFLAG)
		{
			i		= seekstart + 4;
			j		= 0;
			cmdflag = 1;
			while (debugcmdbuf[i] != ')') { canbuf[j++] = debugcmdbuf[i++]; }
			canbuf[j++] = ')';
			i++;
			debugcmdbufheader = i;
		}
	}
	else if (((debugcmdbufheader + 6) < (debugcmdbuflast + 128))
			 && (debugcmdbufheader > debugcmdbuflast))
	{
		seekstart = debugcmdbufheader;
		for (; ((seekstart + 6) < (debugcmdbuflast + 128)) && seekstart < 128;)
		{
			if (*(unsigned int *)&debugcmdbuf[seekstart] != DEBUG_CANFLAG)
			{
				seekstart++;
			}
			else
			{
				break;
			}
		}
		if (*(unsigned int *)&debugcmdbuf[seekstart] == DEBUG_CANFLAG)
		{
			i		= (seekstart + 4) % 128;
			j		= 0;
			cmdflag = 1;
			while (debugcmdbuf[i % 128] != ')')
			{
				canbuf[j++] = debugcmdbuf[i % 128];
				i++;
			}
			canbuf[j++] = ')';
			i++;
			debugcmdbufheader = i % 128;
		}
		else
		{
			seekstart = 0;
			for (; (seekstart + 6) < (debugcmdbuflast);)
			{
				if (*(unsigned int *)&debugcmdbuf[seekstart] != DEBUG_CANFLAG)
				{
					seekstart++;
				}
				if (*(unsigned int *)&debugcmdbuf[seekstart] == DEBUG_CANFLAG)
				{
					i		= (seekstart + 4);
					j		= 0;
					cmdflag = 1;
					while (debugcmdbuf[i] != ')')
					{
						canbuf[j++] = debugcmdbuf[i];
						i++;
					}
					canbuf[j++] = ')';
					i++;
					debugcmdbufheader = i;
					break;
				}
			}
		}
	}
	if (cmdflag)
	{
		debugcan(canbuf);
	}
}
void addinputtocmdbuf(unsigned char *cmdbuf, unsigned int len)
{
	unsigned int i;
	for (i = 0; i < len; i++)
	{
		debugcmdbuf[debugcmdbuflast] = *(cmdbuf + i);
		if (debugcmdbuflast < 4)
			*(unsigned int *)&debugcmdbuf[128]
				= *(unsigned int *)&debugcmdbuf[0];
		debugcmdbuflast++;
		if (debugcmdbuflast == 128)
			debugcmdbuflast = 0;
	}
}
void addChartoCmdBuf(unsigned char inputchar)
{
	debugcmdbuf[debugcmdbuflast] = inputchar;
	if (debugcmdbuflast < 4)
		*(unsigned int *)&debugcmdbuf[128] = *(unsigned int *)&debugcmdbuf[0];
	debugcmdbuflast++;
	if (debugcmdbuflast == 128)
		debugcmdbuflast = 0;
}

/****************************************************************
* 函数名:    Van_Device_Printf
* 创建时间:  2021/04/22
* 创建人:    范义东
* 函数说明：调试打印接口函数
* 输入参数:   dev  传 NULL 采用本地定义
						devNum   数码
* 输出参数:   *name
* 返回值 :   void
****************************************************************/
void Van_Device_Printf(unsigned int printLevel, const char *cmd, ...)
{
	char buf[DISBUF_LEN] = {0};
	int	 buflen			 = 0;
	  __disable_irq(); 
	if (gloublePrintLevel >= printLevel)
	{
		va_list args; //定义一个va_list类型的变量，用来储存单个参数
		va_start(args, cmd);	  //使args指向可变参数的第一个参数
		vsprintf(buf, cmd, args); //必须用vprintf等带V的
		va_end(args);			  //结束可变参数的获取
		buflen = strlen((const char *)buf);
		Van_Debug_UART_Send_DMA((unsigned char *)buf, buflen); //将收到的信息发送出
		
	}
	__enable_irq();
	return;
}


/****************************************************************
* 函数名:    find_stm32_Peripherals
* 创建时间:  2021/04/22
* 创建人:    范义东
* 函数说明：调试打印接口函数
* 输入参数:   peripheralsAdd 地址指针
			
* 输出参数:   *name
* 返回值 :   void
****************************************************************/
void find_stm32_Peripherals(void *peripheralsAdd, char *name)
{
   
}

