#include "bsp.h"
#include "bsp_custom.h"
#include "osasys.h"
#include "ostask.h"
#include "queue.h"
#include "ps_event_callback.h"
#include "cmisim.h"
#include "cmimm.h"
#include "cmips.h"
#include "sockets.h"
#include "psifevent.h"
#include "ps_lib_api.h"
#include "lwip/netdb.h"
#include <cis_def.h>
#include "debug_log.h"
#include "slpman_ec616.h"
#include "plat_config.h"
#include "ec_tcpip_api.h"
#include "hal_module_adapter.h"
#include "UartTask.h"
#include "MainTask.h"
#include "app.h"
#include "CANTask.h"
#include "UDSService.h"

extern osMutexId_t UartMutex;
extern UINT32 Timer_count;
extern volatile bool Sleep_flag;
extern UartReadMsgType UartReadMsg;
CAN_Msg_Type CanRxMsg[2];
/*线程声明*/
#define 			PROC_CAN_RX_TASK_STACK_SIZE			(1024)
static StaticTask_t gProcess_Can_Rx_Task_t;
static UINT8 		gProcess_Can_Rx_TaskStack[PROC_CAN_RX_TASK_STACK_SIZE];


static process_CAN	gProcess_CAN_Task = PROCESS_CAN_STATE_IDLE;
#define 			PROC_CAN_STATE_SWITCH(a)  (gProcess_CAN_Task = a)
volatile bool 		CAN_Sleep_State = false;
UINT8 				CanSendSlpHandler	= 0xfd;



void CANTaskInit(void* arg)
{
    osThreadAttr_t task_rx_attr,task_tx_attr;
    Can_InitType param;
    param.baudrate = CAN_250Kbps;
	param.mode = REQOP_NORMAL;	
	param.packType = STD_PACK;
	
    memset(&task_rx_attr,0,sizeof(task_rx_attr));
    memset(gProcess_Can_Rx_TaskStack, 0, PROC_CAN_RX_TASK_STACK_SIZE);
    task_rx_attr.name = "Can_Task";
    task_rx_attr.stack_mem = gProcess_Can_Rx_TaskStack;
    task_rx_attr.stack_size = PROC_CAN_RX_TASK_STACK_SIZE;
    task_rx_attr.priority = osPriorityBelowNormal7;
    task_rx_attr.cb_mem = &gProcess_Can_Rx_Task_t;
    task_rx_attr.cb_size = sizeof(StaticTask_t);
	
    HAL_Can_Init(param);
    osDelay(1000);
    osThreadNew(Can_Receive, NULL, &task_rx_attr);
}


static void Can_Receive()
{    
	slpManSlpState_t State;
    uint8_t cnt,temp,udsFlag=0;
	UINT8 i = 0;
	CAN_Msg_Type CANSendBuffer ={0};
    while(true)
    {
		memset(CanRxMsg, 0, sizeof(CanRxMsg));    		
		switch(gProcess_CAN_Task)
		{
			case PROCESS_CAN_STATE_IDLE:
				CAN_Sleep_State = false;
				
				PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_WORK);
				
				break;				
			
			case PROCESS_CAN_STATE_WORK:
				{
					if(Sleep_flag)
					{
						PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP);
					}
					HAL_Can_Receive(CanRxMsg);
					for(i=0; i<2; i++)
					{								
						if(CanRxMsg[i].Id == 0x7A0)
						{	
							#ifdef USING_PRINTF1
								printf("ID = 0x%x\n",CanRxMsg[i].Id);
								for(UINT8 j = 0; j<8;j++)
								{
									printf("%x ",CanRxMsg[i].Data[j]);
								}
								printf("\n");
							#endif
							udsFlag = 1;
							UDSService[i] =  CanRxMsg[i].Data[0];
							UDSSubService[i] = CanRxMsg[i].Data[1];
							UDSSubServiceActionCode[i] = CanRxMsg[i].Data[2];
							if(UDSService[i] == 0x10)
							{
								if(UDSSubService[i] == 0x02)
								{
									UDSSwitch = 1;
								}								
							}
						}
					}	
					if(udsFlag==1)
					{	
						UDS_Service();
						udsFlag = 0;							
					}		
					break;
				}
	        case PROCESS_CAN_STATE_SLEEP:
	        {	            
	            HAL_Can_Sleep();	 
	            
	            CAN_Sleep_State = true;
	            while(true)
	            {
	                osDelay(60000/portTICK_PERIOD_MS);
	            }	            
	            break;
	       }
	   }
	   osDelay(50);
	}      
}