/*
 * PCTools.c
 *
 *  Created on: Oct 16, 2020
 *      Author: yang_p1
 */

#include "Config/Include/APP_Config.h"
#include "Abstraction/Include/OS_Interface.h"
#include "App/CAN/CAN_Sender.h"
#include "PCTools.h"
#include <string.h>
#include "App/Power/Power_Os.h"
#include "Middleware/Sensor/Sensor.h"

static OS_TaskParam PCToolsMgrParam;


/*********************************************************************************
 *                      Typedef/Struct                                           *
**********************************************************************************/
typedef struct{
	uint8_t  ubSID;
	uint8_t	ubSubID;
	uint32_t  uwDLC;
	void* vdDataBuff_p;
}JTools_Cmd_Info;

typedef struct
{
	uint8_t ubSID;
	uint32_t ubEvID;
	uint8_t (* Func_p)(uint16_t auhEventCode, void * avdDate_p, uint32_t auwSize);
}JTools_Send_REQ;

enum RecMsgStatus
{
	WaitJToolsCmd,
	WaitJToolsCmdCF,
	WaitTestRes,
	WaitTestReq
};



/*********************************************************************************
*                      Private variables                                         *
**********************************************************************************/

int32_t nswJToolsRxTmout;

void* JToolsCmd = NULL;
void* JToolsResp = NULL;
uint8_t ubWaitRecMsgStatus=WaitJToolsCmd;
uint32_t uwJToolsCmdRemainLen = 0;
int32_t uwJToolsRespRemainLen = 0;
uint8_t ubJToolsIsResponeing = 0;
uint8_t uwJToolsRepIndex =0;


uint8_t ubJToolsCmdStatus = JTOOLS_CMD_OK;
uint32_t uwJToolsCmdDLC = 0;
uint32_t uwJToolsRecCmdLength = 0;
uint8_t uwJToolsRecCmdSID = 0;
uint8_t ubCA_Subcmd;
uint8_t ubBeacon_Subcmd;
uint8_t uwJToolsCmdSN =0;
uint8_t ubJToolsTimerCnt =0 ;

uint32_t uwCFCnt=0;

uint8_t ubJToolsFuncIsON = JTOOLS_FUNC_OFF;//Recieve ON/OFF command from PC Tools
uint8_t ubJToolsDiagSeq = 0;

uint8_t JToolsStandBySts=0;

uint8_t* ubamutli_Res_MPU=NULL;
uint32_t uwJToolsMultiResRecLen_MPU=0; //recieved data len when multiple message
uint16_t auwMultiAllMsgLen=0;

uint8_t TestProcessSts=WaitTestReq;

uint8_t aubData_p[200];

/*********************************************************************************
*                      func declared                                        *
**********************************************************************************/

uint8_t nubJTools_SendMessage(uint16_t auhEventCode, void * avdDate_p, uint32_t auwSize);
void nvdJToolsSendResp(uint16_t auhEventCode, void * avdDate_p, uint32_t auwSize);
void nvdJToolsReqProc(uint8_t* reqdata,uint32_t  uwdatasize);
uint8_t nvdJTools_SendReq(uint8_t* reqdata,uint32_t  uwdatasize);
void nvJToolsModuleInit();
void nvJToolsTimerHook(void);
void nvdStartJToolsTimer();
uint8_t nubJTools_To_MCU(uint16_t auhEventCode, void * avdDate_p, uint32_t auwSize);
void nvdJtools_multiresp_MPU_para_init();
void nvdSocAdapterProc( uint16_t auhEventCode,uint8_t* reqdata,uint32_t  uwdatasize);
uint8_t nubJToolsSendReqToSOC(uint16_t auhEventCode, void * avdDate_p, uint32_t auwSize);

const JTools_Send_REQ wstJToolsCmdTbl[] = {
		/*SID*/		  			/*Event ID*/              							/*send func*/
/****MPU: Read Data      ****/
	{	ICCIDReadOut, 						EV_JTOOLS_TO_MPU_READ_ICCID,						nubJToolsSendReqToSOC},
	{	IMEIReadOut, 						EV_JTOOLS_TO_MPU_READ_IMEI,							nubJToolsSendReqToSOC},
/****MPU: Function Detect	 ****/
	{	Ecall_Press, 						EV_JTOOLS_TO_MPU_IO_ECALL_PRESS,					nubJToolsSendReqToSOC},
	{	Ecall_Hangup, 						EV_JTOOLS_TO_MPU_IO_ECALL_HANGUP,					nubJToolsSendReqToSOC},
	{	V2XDetect, 							EV_JTOOLS_TO_MPU_ROUTINE_V2X_FUNC_TEST,				nubJToolsSendReqToSOC},
	{	ServerCommDetect,					EV_JTOOLS_TO_MPU_ROUTINE_SERVER_TEST,             	nubJToolsSendReqToSOC},
	{	GPSDetect,                          EV_JTOOLS_TO_MPU_ROUTINE_GPS_TEST,					nubJToolsSendReqToSOC},
	{	EthernetTestStart,					EV_JTOOLS_TO_MPU_ROUTINE_ETHERNET_TEST_START,       nubJToolsSendReqToSOC},
	{	EthernetTestStop,					EV_JTOOLS_TO_MPU_ROUTINE_ETHERNET_TEST_STOP,        nubJToolsSendReqToSOC},
	{	GSensorDetectStart,					EV_JTOOLS_TO_MPU_ROUTINE_G_SENSOR_TEST_START,		nubJToolsSendReqToSOC},
	{	GSensorDetectStop,					EV_JTOOLS_TO_MPU_ROUTINE_G_SENSOR_TEST_STOP,		nubJToolsSendReqToSOC},
	{	EMMCFuncDetect,						EV_JTOOLS_TO_MPU_ROUTINE_EMMC_TEST,					nubJToolsSendReqToSOC},
	{	CommSigIntensityTest,               EV_JTOOLS_TO_MPU_READ_SIM_SIGNAL_STRENGTH,			nubJToolsSendReqToSOC},
	{	GPSLocationDetect,                  EV_JTOOLS_TO_MPU_GPS_Location_REQUEST,				nubJToolsSendReqToSOC},
	{	V2XCommDetect,                      EV_JTOOLS_TO_MPU_V2X_COMM_REQUEST,				    nubJToolsSendReqToSOC},
	{	WIFITest,                      		EV_JTOOLS_TO_MPU_ROUTINE_WIFI_TEST,				    nubJToolsSendReqToSOC},
	{	BTTest,                      		EV_JTOOLS_TO_MPU_ROUTINE_BLE_TEST,				    nubJToolsSendReqToSOC},
	{	BTMACReadOut,                      	EV_JTOOLS_TO_MPU_ROUTINE_BLE_TEST,				    nubJToolsSendReqToSOC},


/****MCU: ElectricInspection Switch ****/
	{	ElectricInspectionON, 				ElectricInspectionON,								nubJTools_To_MCU},
	{	ElectricInspectionOFF, 				ElectricInspectionOFF,								nubJTools_To_MCU},
/****MCU: Read Data ****/
	{	HWVerReadOut, 						HWVerReadOut,										nubJTools_To_MCU},
	{	SWVerReadOut, 						SWVerReadOut,										nubJTools_To_MCU},
	{	SNReadOut, 							SNReadOut,											nubJTools_To_MCU},
/****MCU: Write Data ****/
	{	SNWriteIn, 							SNWriteIn,											nubJTools_To_MCU},

/****MCU: DTC ****/
	{	RestoreFactorySetting_ReadDTC,		RestoreFactorySetting_ReadDTC,						nubJTools_To_MCU},
	{	RestoreFactorySetting_ClearDTC,		RestoreFactorySetting_ClearDTC,						nubJTools_To_MCU},
/****MCU: Function Detect ****/
	{	VolDetect,							VolDetect,											nubJTools_To_MCU},
	{	StandbyBatteryLifeTimeDetect,		StandbyBatteryLifeTimeDetect,						nubJTools_To_MCU},
	{	StandbyBatteryStsDetect,			StandbyBatteryStsDetect,							nubJTools_To_MCU},
	{	MicDetect,							MicDetect,											nubJTools_To_MCU},
	{	SpeakerDetect,						SpeakerDetect,										nubJTools_To_MCU},
	{	SOSKeyDetect,						SOSKeyDetect,										nubJTools_To_MCU},
	{	CrashDetect,						CrashDetect,										nubJTools_To_MCU},
	{	GPSPlugDetect,						GPSPlugDetect,										nubJTools_To_MCU},
	{	BeaconDetect,						BeaconDetect,										nubJTools_To_MCU},

/****MCU: Setting ****/
	{	StandbyBatteryDisable,				StandbyBatteryDisable,								nubJTools_To_MCU},


};




static void PCToolsMgr_task_init()
{
	nvJToolsModuleInit();
}



void PCToolsMgr_MainProcess(PCToolsMsg_st PCToolsMgrStdMsg)
{

	switch(PCToolsMgrStdMsg.event_code)
	{
		case EV_STANDBYIN:

			break;
		case EV_STANDBYOUT:

			break;

		case EV_REQ_FROM_JTOOLS:
			if(!ubJToolsIsResponeing)
			{
				TestProcessSts=WaitTestRes;
				nvdJToolsReqProc(PCToolsMgrStdMsg.data_buff,PCToolsMgrStdMsg.data_size);
			}
			break;
		default :
			nvdJToolsSendResp(PCToolsMgrStdMsg.event_code,PCToolsMgrStdMsg.data_buff,PCToolsMgrStdMsg.data_size);
			break;
	}



}

static void PCToolsMgr_recv_msg(uint16_t event_code, void* data, uint32_t data_size)
{
		PCToolsMsg_st PCToolsMgrStdMsg;

		PCToolsMgrStdMsg.event_code = event_code;
		PCToolsMgrStdMsg.data_buff = data;
		PCToolsMgrStdMsg.data_size = data_size;

		PCToolsMgr_MainProcess(PCToolsMgrStdMsg);
}

static void PCToolsMgr_recv_time_out()
{
	;
}

static void PCToolsMgr_loop()
{
	;
}

void* PCToolsMgr_getTaskParam()
{
	PCToolsMgrParam.queue_id = MSG_ID_PCTOOLS;
	PCToolsMgrParam.wait_time = OS_WAIT_INFINITE;
	PCToolsMgrParam.task_init = PCToolsMgr_task_init;
	PCToolsMgrParam.recv_msg = PCToolsMgr_recv_msg;
	PCToolsMgrParam.time_out = PCToolsMgr_recv_time_out;
	PCToolsMgrParam.loop = PCToolsMgr_loop;
    return &PCToolsMgrParam;
}

/*********************************************************************************
 * Function:        nvdJToolsReqProc
 * Description:     Process the request from PCTools
 * Input:            uint8_t* reqdata,uint32_t  uwdatasize
 * Output:          -
 * Return:          void
 *********************************************************************************
 * History:         No.     Date        		Author          Modification
 *--------------------------------------------------------------------------------
 *                  0001    2019-09-10  		pf.Yang		     New
 *
 **********************************************************************************/

void nvdJToolsReqProc(uint8_t* reqdata,uint32_t  uwdatasize)
{
	uint32_t uwdlc;

	switch(ubWaitRecMsgStatus)
	{
		case WaitJToolsCmd:
			uwJToolsRecCmdSID = *(reqdata+JTOOLS_CMD_SID_POS);
			uwdlc = (*(reqdata+JTOOLS_CMD_DLC_POS) << 8 ) | *(reqdata+JTOOLS_CMD_DLC_POS+1);
			if((JTOOLS_CMD_SYNC_H==*(reqdata+JTOOLS_CMD_SYNC_POS)) && (JTOOLS_CMD_SYNC_L==*(reqdata+JTOOLS_CMD_SYNC_POS+1)))
			{
				if(uwdlc <= (JTOOLS_CMD_ALL_LEN-JTOOLS_CMD_SYNC_LEN-JTOOLS_CMD_DLC_LEN-JTOOLS_CMD_SID_LEN-JTOOLS_CMD_TYPE_LEN))
				{
					if(JTOOLS_CMD_TYPE_COMMAND==*(reqdata+JTOOLS_CMD_TYPE_POS))
					{
						ubJToolsCmdStatus=nvdJTools_SendReq(reqdata,uwdatasize);
					}else
					{
						ubJToolsCmdStatus = JTOOLS_CMD_TYPE_ERR;
					}
				}
				else
				{
					ubWaitRecMsgStatus = WaitJToolsCmdCF;
					uwJToolsRecCmdLength = uwdlc+JTOOLS_CMD_SYNC_LEN+JTOOLS_CMD_DLC_LEN+JTOOLS_CMD_SID_LEN+JTOOLS_CMD_TYPE_LEN;
					JToolsCmd = OS_malloc(uwJToolsRecCmdLength);
					if(JToolsCmd!=NULL)
					{
						memcpy(JToolsCmd,reqdata,JTOOLS_CMD_ALL_LEN);
						uwCFCnt++;
						uwJToolsCmdRemainLen = uwdlc - (JTOOLS_CMD_ALL_LEN-JTOOLS_CMD_SYNC_LEN-JTOOLS_CMD_DLC_LEN-JTOOLS_CMD_SID_LEN-JTOOLS_CMD_TYPE_LEN);
						nvdStartJToolsTimer();
					}else
					{
						ubWaitRecMsgStatus = WaitJToolsCmd;
						uwJToolsCmdSN = 0;
						uwJToolsRecCmdLength = 0;
						uwCFCnt=0;
					}
				}
			}else
			{
				ubJToolsCmdStatus = JTOOLS_CMD_SYNC_ERR;
			}
			break;
		case WaitJToolsCmdCF:
			ubJToolsTimerCnt=0; //reset timer cnt
			if(uwJToolsCmdRemainLen <= JTOOLS_CMD_ALL_LEN)
			{
				ubWaitRecMsgStatus = WaitJToolsCmd;
				memcpy(JToolsCmd+(uwCFCnt*JTOOLS_CMD_ALL_LEN),reqdata,uwJToolsCmdRemainLen);
				ubJToolsCmdStatus=nvdJTools_SendReq(JToolsCmd,uwJToolsRecCmdLength);
				uwCFCnt=0;
				uwJToolsCmdRemainLen=0;
				OS_free(JToolsCmd);
				OS_stopTimer(TIMER_ID_JTOOLS);
			}else
			{
				memcpy(JToolsCmd+(uwCFCnt*JTOOLS_CMD_ALL_LEN),reqdata,JTOOLS_CMD_ALL_LEN);
				uwCFCnt++;
				uwJToolsCmdRemainLen -= JTOOLS_CMD_ALL_LEN;
			}
			break;
		default:
			break;
	}
}

/*********************************************************************************
 * Function:        nvdJTools_SendReq
 * Description:     Send request(which be sent by PC Tools) to other module
 * Input:            uint8_t* reqdata,uint32_t  uwdatasize
 * Output:          -
 * Return:          return error if servie not supported
 *********************************************************************************
 * History:         No.     Date        		Author          Modification
 *--------------------------------------------------------------------------------
 *                  0001    2019-09-10  		pf.Yang		     New
 *
 **********************************************************************************/

uint8_t nvdJTools_SendReq(uint8_t* reqdata,uint32_t  uwdatasize)
{
	uint32_t uwdatadlc=0;
	uint16_t uwindex=0;
	uint8_t spiStatus;

	uwdatadlc = (*(reqdata+JTOOLS_CMD_DLC_POS) << 8 ) | *(reqdata+JTOOLS_CMD_DLC_POS+1);

	for(uwindex=0;uwindex<sizeof(wstJToolsCmdTbl)/sizeof(wstJToolsCmdTbl[0]);uwindex++)
	{
		if( (wstJToolsCmdTbl[uwindex].ubSID) == *(reqdata+JTOOLS_CMD_SID_POS) )
		{
#ifdef JTOOLS_DEBUG
			JPCC_DEBUG(JPCC_LOG_LEVEL_INFO,"[JTOOLS] [Send REQUEST  : [%x] ]\n",wstJToolsCmdTbl[uwindex].ubSID);
#endif

			if((wstJToolsCmdTbl[uwindex].ubSID != ElectricInspectionON) && (wstJToolsCmdTbl[uwindex].ubSID != ElectricInspectionOFF))
			{
				if(JTOOLS_FUNC_ON==ubJToolsFuncIsON)
				{
					if(uwdatadlc!=0)
					{

						spiStatus=wstJToolsCmdTbl[uwindex].Func_p(wstJToolsCmdTbl[uwindex].ubEvID,reqdata+JTOOLS_CMD_DATA_POS,uwdatadlc);
						#ifdef JTOOLS_DEBUG
						JPCC_DEBUG(JPCC_LOG_LEVEL_INFO, "[JTOOLS] [SEND CMD TO MPU by SPI [%d]]\n",spiStatus);
						#endif
					}else
					{

						spiStatus=wstJToolsCmdTbl[uwindex].Func_p(wstJToolsCmdTbl[uwindex].ubEvID,NULL,0);
						#ifdef JTOOLS_DEBUG
						JPCC_DEBUG(JPCC_LOG_LEVEL_INFO, "[JTOOLS] [SEND CMD TO MPU by SPI [%d]]\n",spiStatus);
						#endif
					}
				}
			}else
			{
				spiStatus=wstJToolsCmdTbl[uwindex].Func_p(wstJToolsCmdTbl[uwindex].ubEvID,NULL,0);
			}
			return spiStatus;
		}
	}
	return JTOOLS_CMD_SID_ERR;


}

/*********************************************************************************
 * Function:        nubJTools_To_MCU
 * Description:     Send request to moudle of MCU
 * Input:            uint16_t auhEventCode, void * avdDate_p, uint32_t auwSize
 * Output:          -
 * Return:          0
 *********************************************************************************
 * History:         No.     Date        		Author          Modification
 *--------------------------------------------------------------------------------
 *                  0001    2019-09-10  		pf.Yang		     New
 *                  0002    2020-05-14          pf.yang          change LIN detect to beacon detect
 *                                                               add BLE connect status test
 **********************************************************************************/

uint8_t nubJTools_To_MCU(uint16_t auhEventCode, void * avdDate_p, uint32_t auwSize)
{
	//rtc_timedate_t stRTC;
    uint8_t aubRespStatus=JTOOLS_RESP_NG;
    uint16_t aubDataLen;
	uint8_t nubret;
	uint8_t eeprdata=JTOOLS_EEPROM_TEST_DATA;
	uint8_t swversion[] = MCU_VERSION_SW;

	switch(auhEventCode)
	{
	//--------------------------------------
	// ElectricInspection Switch
	//--------------------------------------
		case ElectricInspectionON:
			ubJToolsFuncIsON = JTOOLS_FUNC_ON;
			aubRespStatus = JTOOLS_RESP_OK;
			nubJTools_SendMessage(EV_RESP_FROM_MCU,&aubRespStatus,1);
			break;
		case ElectricInspectionOFF:
			ubJToolsFuncIsON = JTOOLS_FUNC_OFF;
			aubRespStatus = JTOOLS_RESP_OK;
			nubJTools_SendMessage(EV_RESP_FROM_MCU,&aubRespStatus,1);
			break;
	//--------------------------------------
	// Read Data
	//--------------------------------------
		case HWVerReadOut:
			//aubRespStatus=Ee24c64ReadData(EE24C64_ECU_HARDWARE_VERSION,(uint32_t)&aubData_p[1],HARDWARE_VER_LEN);
			aubData_p[0]=JTOOLS_RESP_OK;
			aubData_p[1]=GetHardWareVersion();
			nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,2);
			break;
		case SWVerReadOut:
			aubData_p[0]=JTOOLS_RESP_OK;
			memcpy(&aubData_p[1],&swversion[0],sizeof(swversion)-1);
			nubJTools_SendMessage(EV_RESP_FROM_MCU,&aubData_p[0],sizeof(swversion));
			break;
		case SNReadOut:
			//aubRespStatus=Ee24c64ReadData(EE24C64_TRACE_CODE,(uint32_t)&aubData_p[1],TRACE_CODE_LEN);
			//aubData_p[0]=aubRespStatus;
			//if(JTOOLS_RESP_OK==aubRespStatus)
			//{
			//	nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,(TRACE_CODE_LEN+1));
			//}else
			//{
			//	nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,1);
			//}
			break;

		case RestoreFactorySetting_ReadDTC:
#if 0
			aubDataLen=nubDtcList(aubData_p,JTOOLS_DTC_ABAIL_MASK);
			aubData_p[0] = JTOOLS_RESP_OK;
			aubData_p[1] = (uint8_t)(((aubDataLen-1)/JTOOLS_DTC_ONE_SIZE)>>8);//update DTC mask to dtc count
			aubData_p[2] = (uint8_t)((aubDataLen-1)/JTOOLS_DTC_ONE_SIZE);//update DTC mask to dtc count
			nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,aubDataLen+3);
#endif
			break;
		//--------------------------------------
		// Write Data
		//--------------------------------------
		case SNWriteIn:
#if 0
			aubRespStatus=Ee24c64WriteData(EE24C64_TRACE_CODE,(uint32_t)avdDate_p,TRACE_CODE_LEN);
			nubJTools_SendMessage(EV_RESP_FROM_MCU,&aubRespStatus,1);
			break;
#endif

		//--------------------------------------
		// DTC
		//--------------------------------------
		case RestoreFactorySetting_ClearDTC:
			//for ( ubindex = 0; ubindex < DTC_MAX_NUM; ubindex++ )
			//{
			//	nvdDTCStackClear(0);
			//	OS_Sleep(5);
			//}
			aubData_p[0]= JTOOLS_RESP_OK;
			nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,1);
			break;
		//--------------------------------------
		// Fun detect
		//--------------------------------------
		case VolDetect:
			//aubData_p[0] = JTOOLS_RESP_OK;
			//aubData_p[1] = GetMBattVoltPCtool();//*10
			nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,2);
			break;
		case StandbyBatteryLifeTimeDetect:
			//aubData_p[0] = GetBatteryHealth();
			nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,1);
			break;
		case StandbyBatteryStsDetect:
			//aubData_p[0] = GetBatteryTempStatus();
			nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,1);
			break;
		case MicDetect:
			//aubData_p[0] =  GetMicStatusPCtool();
			nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,1);
			break;
		case SpeakerDetect:
			aubData_p[0] = get_AmpFaultRegSts(&aubData_p[1]);
			//aubData_p[0] =  GetAmpStatusPCtool();
			nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,2);
			break;
		case SOSKeyDetect:
			//nubSensor_SendMessage(EVENT_SENSOR_PCTOOL_SOS_MODE,0,0);
			break;
		case CrashDetect:
			//nubSensor_SendMessage(EVENT_SENSOR_PCTOOL_SRS_MODE,0,0);
			break;
		case GPSPlugDetect:
			//aubData_p[0] =  GetGnssStatusPCtool();
			nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,1);
			break;
		//--------------------------------------
		// Setting
		//--------------------------------------
		case StandbyBatteryDisable:
			//aubData_p[0]=DisableBatteryBoost();
			//nubJTools_SendMessage(EV_RESP_FROM_MCU,aubData_p,1);
			break;
		default :
			break;
	}
	return 0;
}

/*********************************************************************************
 * Function:        nvdSocAdapterProc
 * Description:     Process the request from soc adapter test
 * Input:           uint16_t auhEventCode, uint8_t* reqdata,uint32_t  uwdatasize
 * Output:          -
 * Return:          void
 *********************************************************************************
 * History:         No.     Date        		Author          Modification
 *--------------------------------------------------------------------------------
 *                  0001    2020-10-29  		pf.Yang		     New
 *
 **********************************************************************************/
void nvdSocAdapterProc( uint16_t auhEventCode,uint8_t* reqdata,uint32_t  uwdatasize)
{
#if 0
	uint8_t ret = 0;
	uint16_t respID = 0;

	JPCC_DEBUG(JPCC_LOG_LEVEL_INFO, "[JTOOLS] [Receive soc adapter request [%d],len=[%d]]\n",auhEventCode,uwdatasize);
	switch(auhEventCode)
	{
		case EV_MPU_TO_JTOOLS_ROUTINE_WRITE_SN_REQUEST:
			respID = EV_JTOOLS_TO_MPU_ROUTINE_WRITE_SN_RESULT;
			if((reqdata == NULL) || uwdatasize<TRACE_CODE_LEN)
			{
				ret = JTOOLS_RESP_NG;
			}else
			{
				ret = Ee24c64WriteData(EE24C64_TRACE_CODE,(uint32_t)reqdata,TRACE_CODE_LEN);
			}
			break;
		case EV_MPU_TO_JTOOLS_ROUTINE_WRTIE_HSM_KEY_REQUEST:
			respID = EV_JTOOLS_TO_MPU_ROUTINE_WRITE_HSM_KEY_RESULT;
			ret = nvdJTools_HSM_Key_Set(HSM_Const_Data);
			break;
		case EV_MPU_TO_JTOOLS_ROUTINE_WRTIE_TBOX_SK_REQUEST:
			respID = EV_JTOOLS_TO_MPU_ROUTINE_WRITE_TBOX_SK_RESULT;
			if((reqdata == NULL) || uwdatasize<TBOX_SK)
			{
				ret = JTOOLS_RESP_NG;
			}else
			{
				ret = Ee24c64WriteData(EE24C64_TBOX_SK,(uint32_t)reqdata,TBOX_SK);
			}
			break;
		case EV_MPU_TO_JTOOLS_ROUTINE_WRTIE_VIN_REQUEST:
			respID = EV_JTOOLS_TO_MPU_ROUTINE_WRITE_VIN_RESULT;
			if((reqdata == NULL) || uwdatasize<VIN_LEN)
			{
				ret = JTOOLS_RESP_NG;
			}else
			{
				ret = Ee24c64WriteData(EE24C64_VIN,(uint32_t)reqdata,VIN_LEN);
			}
			break;
		default:
			break;
	}

	/*send response to soc adapter*/
	ret = nubJToolsSendReqToSOC(respID,&ret,1);
	JPCC_DEBUG(JPCC_LOG_LEVEL_INFO, "[JTOOLS] [send response to soc adapter, ret = [%d] ]\n",ret);
#endif
}

/*********************************************************************************
 * Function:        nvdStartJToolsTimer
 * Description:     Start JTools base timer(10ms)
 * Input:            void
 * Output:          -
 * Return:          void
 *********************************************************************************
 * History:         No.     Date        		Author          Modification
 *--------------------------------------------------------------------------------
 *                  0001    2019-09-10  		pf.Yang		     New
 *
 **********************************************************************************/

void nvdStartJToolsTimer()
{
	OS_startTimer(TIMER_ID_JTOOLS);
	ubJToolsTimerCnt = 0;
}

/*********************************************************************************
 * Function:        nvdJToolsSendResp
 * Description:     Send response to PCTools by CAN IL stack
 * Input:            OS_StdMsg_st * MsgRx
 * Output:          -
 * Return:          void
 *********************************************************************************
 * History:         No.     Date        		Author          Modification
 *--------------------------------------------------------------------------------
 *                  0001    2019-09-10  		pf.Yang		     New
 *
 **********************************************************************************/

void nvdJToolsSendResp(uint16_t auhEventCode, void * avdDate_p, uint32_t auwSize)
{
	uint8_t* respdata;
	uint32_t  uwlen;
	uint8_t  ubdataPosi;
	uint8_t sendata[JTOOLS_CMD_ALL_LEN];
/*request from soc adapter test*/
	if( (auhEventCode == EV_MPU_TO_JTOOLS_ROUTINE_WRITE_SN_REQUEST) || (auhEventCode == EV_MPU_TO_JTOOLS_ROUTINE_WRTIE_HSM_KEY_REQUEST)
		||(auhEventCode == EV_MPU_TO_JTOOLS_ROUTINE_WRTIE_TBOX_SK_REQUEST) ||(auhEventCode == EV_MPU_TO_JTOOLS_ROUTINE_WRTIE_VIN_REQUEST))
	{
		nvdSocAdapterProc(auhEventCode,avdDate_p,auwSize);
	}

	if(TestProcessSts==WaitTestRes)
	{
		respdata = OS_malloc(auwSize+JTOOLS_CMD_HEADER_LEN+1);

		if(respdata==NULL)
		{
			//JPCC_DEBUG(JPCC_LOG_LEVEL_DLT,"[JTOOLS] [Send response, malloc null]");
			return;
		}

		memset(respdata,0x00,auwSize+JTOOLS_CMD_HEADER_LEN+1);
		memset(sendata,0x00,JTOOLS_CMD_ALL_LEN);

		//SYNC
		*(respdata+JTOOLS_CMD_SYNC_POS)=JTOOLS_CMD_SYNC_H;
		*(respdata+JTOOLS_CMD_SYNC_POS+1)=JTOOLS_CMD_SYNC_L;
		//DLC
		switch(auhEventCode)
		{
			//Get data from AG35/MPU, but no OK/NG result
			//So add OK/NG result into response data
			case  EV_MPU_TO_JTOOLS_ICCID_READ_DATA:case EV_MPU_TO_JTOOLS_IMEI_READ_DATA:case EV_MPU_TO_JTOOLS_READ_SIMCARD_STATUS:case EV_MPU_TO_JTOOLS_READ_SIM_SIGNAL_STRENGTH:
			case EV_MPU_TO_JTOOLS_READ_SIM_CARD_MODE:case EV_MPU_TO_JTOOLS_READ_SIM_CARD_NETWORK_STATUS:case EV_MPU_TO_JTOOLS_ROUTINE_GPS_TEST:case EV_MPU_TO_JTOOLS_ROUTINE_GPS_Location_TEST:
			case EV_MPU_TO_JTOOLS_ROUTINE_BLE_TEST_RESULT:
				{
					uwlen = auwSize + 1;
					ubdataPosi = JTOOLS_CMD_DATA_POS + 1;
					respdata[JTOOLS_CMD_DATA_POS] = JTOOLS_RESP_OK;
				}
				break;
			//Get test result from AG35/MPU, but only event id not OK/NG result
			//So add OK/NG result into response data
			case EV_MPU_TO_JTOOLS_ROUTINE_G_SENSOR_TEST:case EV_MPU_TO_JTOOLS_ROUTINE_ETHERNET_TEST:case EV_JTOOLS_TO_MPU_V2X_COMM_REQUEST:
				{
					uwlen = 1;
					ubdataPosi = JTOOLS_CMD_DATA_POS + 1;
					respdata[JTOOLS_CMD_DATA_POS] = JTOOLS_RESP_OK;
				}
				break;
			//Get test result from AG35/MPU, but OK/NG have 4 bytes.
			//So change 4 bytes to 1 byte
			case EV_MPU_TO_JTOOLS_ROUTINE_4G5G_ANTENNA_TEST:case EV_MPU_TO_JTOOLS_ROUTINE_SERVER_TEST:case EV_MPU_TO_JTOOLS_ROUTINE_EMMC_TEST:
			case EV_MPU_TO_JTOOLS_ROUTINE_V2X_FUNC_TEST:case EV_MPU_TO_JTOOLS_ROUTINE_WIFI_TEST_RESULT:
				{
					uwlen = 1;
					ubdataPosi = JTOOLS_CMD_DATA_POS;

					if(auwSize>=4)
					{
						auwSize = 1;
					}
				}
				break;
			default:
				{
					uwlen = (auwSize) ;
					ubdataPosi = JTOOLS_CMD_DATA_POS;
				}
				break;

		}
		*(respdata+JTOOLS_CMD_DLC_POS) = (uwlen>>8);
		*(respdata+JTOOLS_CMD_DLC_POS+1) = uwlen;
		//SID
		*(respdata+JTOOLS_CMD_SID_POS) = uwJToolsRecCmdSID;
		//TYPE
		*(respdata+JTOOLS_CMD_TYPE_POS) = JTOOLS_CMD_TYPE_RESULT;
		//DATA
		memcpy(respdata+ubdataPosi,avdDate_p,auwSize);
		uwlen += JTOOLS_CMD_HEADER_LEN;
		uwJToolsRespRemainLen = uwlen;
		while(uwJToolsRespRemainLen>0)
		{
			if(uwJToolsRespRemainLen<JTOOLS_CMD_ALL_LEN)
			{
				memcpy(sendata,respdata+(uwlen-uwJToolsRespRemainLen),uwJToolsRespRemainLen);
				uwJToolsRespRemainLen = 0;
			}else
			{
				memcpy(sendata,respdata+(uwlen-uwJToolsRespRemainLen),JTOOLS_CMD_ALL_LEN);
				uwJToolsRespRemainLen-=(JTOOLS_CMD_ALL_LEN);
			}
			CanTx_PCTOOL_RES(sendata,JTOOLS_CMD_ALL_LEN);
			TestProcessSts=WaitTestReq;
			memset(sendata,0x00,JTOOLS_CMD_ALL_LEN);
			OS_sleep(30);
		}
		OS_free(respdata);
	}
}

/*********************************************************************************
 * Function:        nvJToolsTimerHook
 * Description:     Timer counter : Recieve multiple frames timeout and Send multiple frames internal time
 * Input:           void
 * Output:          -
 * Return:          void
 *********************************************************************************
 * History:         No.     Date        		Author          Modification
 *--------------------------------------------------------------------------------
 *                  0001    2019-09-10  		pf.Yang		     New
 *
 **********************************************************************************/


void nvJToolsTimerHook(void)
{
	ubJToolsTimerCnt ++;
	if(WaitJToolsCmdCF==ubWaitRecMsgStatus)//wait continue frames from PCTools
	{
		if(ubJToolsTimerCnt > JTOOLS_TIMER_WAITCF_TIMEOUT)//do not recieved CF frames within timeout
		{
			ubWaitRecMsgStatus = WaitJToolsCmd;
			TestProcessSts=WaitTestReq;
			uwJToolsCmdSN = 0;
			uwJToolsRecCmdLength = 0;
			uwCFCnt=0;
			OS_free(JToolsCmd);
			OS_stopTimer(TIMER_ID_JTOOLS);
		}
	}else
	{

	}
}

/*********************************************************************************
 * Function:        nvJToolsModuleInit
 * Description:     initial JTools module
 * Input:           void
 * Output:          -
 * Return:          void
 *********************************************************************************
 * History:         No.     Date        		Author          Modification
 *--------------------------------------------------------------------------------
 *                  0001    2019-09-10  		pf.Yang		     New
 *
 **********************************************************************************/

void nvJToolsModuleInit()
{
	OS_createTimer(TIMER_ID_JTOOLS,JTOOLS_TIMER_BASE,1,nvJToolsTimerHook);
}

void nvdJtools_multiresp_MPU_para_init()
{
	ubamutli_Res_MPU = NULL;
	uwJToolsMultiResRecLen_MPU = 0;

}

uint8_t nubJToolsSendReqToSOC(uint16_t auhEventCode, void * avdDate_p, uint32_t auwSize)
{
	//send reques to soc by spi
	OS_sndMsg(MSG_ID_SPIMANAGER,auhEventCode,avdDate_p,auwSize,OS_WAIT_INFINITE);
}

uint8_t nubJTools_SendMessage(uint16_t auhEventCode, void * avdDate_p, uint32_t auwSize)
{
	OS_Error ret;
	ret = OS_sndMsg(MSG_ID_PCTOOLS,auhEventCode,avdDate_p,auwSize,OS_WAIT_INFINITE);
}



