#include "common.h"
#include "drv_can.h"
#include "app_rs485_1.h"
#include "app_rs485_2.h"
#include "app_can.h"
#include "app.h"
#include "measure_temprature.h"
#include "measure_vol.h"
#include "app_adas.h"
#include "app_end_ctr.h"
#include "app_bms_1.h"
#include "app_bms_2.h"

//can
static uint8_t app_can_buf[CAN_TX_BUF_MAX];
CAN_FRAME app_can_frame;

static uint8_t app_can_bms_1_buf[CAN_BMS_TX_BUF_MAX];
CAN_FRAME app_can_bms_1_frame;

static uint8_t app_can_bms_2_buf[CAN_BMS_TX_BUF_MAX];
CAN_FRAME app_can_bms_2_frame;

static uint8_t app_can_ctr_buf[CAN_CTR_TX_BUF_MAX];
CAN_FRAME app_can_ctr_frame;

static uint8_t app_can_adas_buf[CAN_ADAS_TX_BUF_MAX];
CAN_FRAME app_can_adas_frame;

uint8_t mode = 0;
uint8_t adjust_step = 0;
	
int8_t Resend_can_bms_1_frame(void)
{
	return Send_Data_Can(&app_can_bms_1_frame,BMS_1_RESEND);
}

int8_t Resend_can_bms_2_frame(void)
{
	return Send_Data_Can(&app_can_bms_2_frame,BMS_2_RESEND);
}

int8_t Resend_can_ctr_frame(void)
{
	return Send_Data_Can(&app_can_ctr_frame,CTR_RESEND);
}

int8_t Resend_can_adas_frame(void)
{
	return Send_Data_Can(&app_can_adas_frame,ADAS_RESEND);
}


int8_t Timeout_Resend_can_bms_1_frame(void)
{
	return Send_Data_Can(&app_can_bms_1_frame,TIMEOUT_BMS_1_RESEND);
}

int8_t Timeout_Resend_can_bms_2_frame(void)
{
	return Send_Data_Can(&app_can_bms_2_frame,TIMEOUT_BMS_2_RESEND);
}

int8_t Timeout_Resend_can_ctr_frame(void)
{
	return Send_Data_Can(&app_can_ctr_frame,TIMEOUT_CTR_RESEND);
}

int8_t Timeout_Resend_can_adas_frame(void)
{
	return Send_Data_Can(&app_can_adas_frame,TIMEOUT_ADAS_RESEND);
}

void ADAS_Self_Send_Up(void)
{
	Can_ADAS_Self_Send(&app_can_adas_frame);
}

void Check_Can_Self_Send_Status(void)
{
	Can_Bms_1_Self_Send_Check(&app_can_bms_1_frame);
	Can_Bms_2_Self_Send_Check(&app_can_bms_2_frame);
	Can_End_Ctr_Self_Send_Check(&app_can_ctr_frame);
	
}

static int8_t Handle_Can_CMD(CAN_FRAME*can_frame)
{
	uint8_t key = can_frame->data[1];

	switch(key)
	{
		case 0x31:
			memcpy(&app_can_bms_1_frame.head,&can_frame->head,sizeof(can_frame->head));
			app_can_bms_1_frame.len = can_frame->len;
			memcpy(app_can_bms_1_frame.data,can_frame->data,app_can_bms_1_frame.len);
			
			if(!Handle_Can_Bms_1_CMD(&app_can_bms_1_frame))
				return 0;
			if(!Rsp_Can_Bms_1_CMD(&app_can_bms_1_frame))
				return 0;
			break;
		case 0x32:
			memcpy(&app_can_bms_2_frame.head,&can_frame->head,sizeof(can_frame->head));
			app_can_bms_2_frame.len = can_frame->len;
			memcpy(app_can_bms_2_frame.data,can_frame->data,app_can_bms_2_frame.len);
			
			if(!Handle_Can_Bms_2_CMD(&app_can_bms_2_frame))
				return 0;
			if(!Rsp_Can_Bms_2_CMD(&app_can_bms_2_frame))
				return 0;
			break;
		case 0x48:
		case 0x42:
		case 0x43:
			memcpy(&app_can_ctr_frame.head,&can_frame->head,sizeof(can_frame->head));
			app_can_ctr_frame.len = can_frame->len;
			memcpy(app_can_ctr_frame.data,can_frame->data,app_can_ctr_frame.len);
			
			if(!Handle_Can_Ctr_CMD(&app_can_ctr_frame))
				return 0;
			if(!Rsp_Can_Ctr_CMD(&app_can_ctr_frame))
				return 0;
			break;
		case 0x52:
			memcpy(&app_can_adas_frame.head,&can_frame->head,sizeof(can_frame->head));
			app_can_adas_frame.len = can_frame->len;
			memcpy(app_can_adas_frame.data,can_frame->data,app_can_adas_frame.len);
#if 0
			if(!Handle_Can_Adas_CMD(&app_can_adas_frame))
				return 0;
			if(!Rsp_Can_Adas_CMD(&app_can_adas_frame))
				return 0;
#endif
			break;
		default:
			break;
	}
	
	return 1;
	
}

int8_t Handle_Can_Data(void)
{
	uint8_t len = Get_Data_Can(&app_can_frame);

	if(len == 0)
		return 0;

	
	if((mode == 1)&&(app_can_buf[0] == 0xFF) &&(app_can_buf[1] == 0xFF))
	{
		len = 2;
		adjust_step = app_can_buf[len++];
		
		switch(adjust_step)
		{	
			/*case 0:
				break;
			case OPERATE_UN_INPUT:
				memcpy(&Un,&app_can_buf[len],sizeof(Un));
				break;
			case OPERATE_UNIBA_INPUT:
				memcpy(&Iba,&app_can_buf[len],sizeof(Iba));
				break;
			case OPERATE_UNIBB_INPUT:
				memcpy(&Ibb,&app_can_buf[len],sizeof(Ibb));
				break;
			case OPERATE_POWERA_INPUT:
				memcpy(&tPowerA,&app_can_buf[len],sizeof(tPowerA));
				break;
			case OPERATE_POWERB_INPUT:
				memcpy(&tPowerB,&app_can_buf[len],sizeof(tPowerB));
				break;*/
			case OPERATE_READ_DATA:
				printf("return:%d\r\n",Battery_CB_Switch(app_can_buf[len]));
				break;
			case OPERATE_POWER_DATA:
				printf("sub_bms_1_lt_state:%d\r\n",sub_bms_1_lt_state);
				printf("rs485_connect_1:%d\r\n",sub_bms_info_1.rs485_connect);
				printf("rs485_time_out_1:%d\r\n",sub_bms_info_1.rs485_time_out);
				printf("yjkxslc_1:%d\r\n",sub_bms_info_1.packet_common.yjkxslc);
				printf("m_percent_1:%d\r\n",sub_bms_info_1.packet_common.m_percent);
				printf("yjcdwcsj_1:%d\r\n",sub_bms_info_1.packet_common.yjcdwcsj);
				printf("charge_flag_1:%d\r\n",sub_bms_info_1.packet_common.charge_flag);
				printf("m_current_1:%dma\r\n",sub_bms_info_1.packet_common.m_current);
				printf("m_total_vol_1:%umv\r\n",sub_bms_info_1.packet_common.m_total_vol);
				printf("bms_temp_1:%d\r\n",sub_bms_info_1.packet_common.bms_temp);
				printf("work_status_1:%x\r\n",sub_bms_info_1.packet_common.work_status);
				printf("banlance_cell_1:%x\r\n",sub_bms_info_1.packet_common.banlance_cell);
				printf("bms_status_1:%x\r\n",sub_bms_info_1.packet_common.bms_status);

				memcpy(app_can_buf,sub_bms_info_1.bat_dev_info.sn,SERIAL_NUM_SIZE);
				app_can_buf[SERIAL_NUM_SIZE] = 0;
				printf("sn_1:%s\r\n",app_can_buf);

				memcpy(app_can_buf,sub_bms_info_1.bat_dev_info.soft_ver,SOFTWARE_SIZE);
				app_can_buf[SOFTWARE_SIZE] = 0;
				printf("soft_ver_1:%s\r\n",app_can_buf);

				printf("dcsmxs_1:%d\r\n",sub_bms_info_1.bat_times.dcsmxs);
				printf("Charge_Num_1:%lu\r\n",sub_bms_info_1.bat_times.charge_times);
				printf("OvCh_vol_Num_1:%lu\r\n",sub_bms_info_1.bat_times.charge_over_vol_times);
				printf("OvDis_vol_Num_1:%lu\r\n",sub_bms_info_1.bat_times.discharge_under_vol_times);
				printf("OvCharge_cur_Num_1:%lu\r\n",sub_bms_info_1.bat_times.charge_over_cur_times);
				printf("OvDischarge_cur_Num_1:%lu\r\n",sub_bms_info_1.bat_times.discharge_under_cur_times);
				printf("Short_Num_1:%lu\r\n",sub_bms_info_1.bat_times.short_times);
				printf("UdTempeCh_Num_1:%lu\r\n",sub_bms_info_1.bat_times.charge_under_temp_times);
				printf("UdTempeDis_Num_1:%lu\r\n",sub_bms_info_1.bat_times.discharge_under_temp_times);
				printf("OvTempeCh_Num_1:%lu\r\n",sub_bms_info_1.bat_times.charge_over_temp_times);
				printf("OvTempeDis_Num_1:%lu\r\n",sub_bms_info_1.bat_times.discharge_over_temp_times);


				
				break;
				
			case OPERATE_ENERGY_DATA: //0x0C
				printf("sub_bms_2_lt_state:%d\r\n",sub_bms_2_lt_state);
				
				printf("rs485_connect_2:%d\r\n",sub_bms_info_2.rs485_connect);
				printf("rs485_time_out_2:%d\r\n",sub_bms_info_2.rs485_time_out);
				printf("yjkxslc_2:%d\r\n",sub_bms_info_2.packet_common.yjkxslc);
				printf("m_percent_2:%d\r\n",sub_bms_info_2.packet_common.m_percent);
				printf("yjcdwcsj_2:%d\r\n",sub_bms_info_2.packet_common.yjcdwcsj);
				printf("charge_flag_2:%d\r\n",sub_bms_info_2.packet_common.charge_flag);
				printf("m_current_2:%dma\r\n",sub_bms_info_2.packet_common.m_current);
				printf("m_total_vol_2:%umv\r\n",sub_bms_info_2.packet_common.m_total_vol);
				printf("bms_temp_2:%d\r\n",sub_bms_info_2.packet_common.bms_temp);
				printf("work_status_2:%x\r\n",sub_bms_info_2.packet_common.work_status);
				printf("banlance_cell_2:%x\r\n",sub_bms_info_2.packet_common.banlance_cell);
				printf("bms_status_2:%x\r\n",sub_bms_info_2.packet_common.bms_status);

				memcpy(app_can_buf,sub_bms_info_2.bat_dev_info.sn,SERIAL_NUM_SIZE);
				app_can_buf[SERIAL_NUM_SIZE] = 0;
				printf("sn_2:%s\r\n",app_can_buf);

				memcpy(app_can_buf,sub_bms_info_2.bat_dev_info.soft_ver,SOFTWARE_SIZE);
				app_can_buf[SOFTWARE_SIZE] = 0;
				printf("soft_ver_2:%s\r\n",app_can_buf);

				printf("dcsmxs_2:%d\r\n",sub_bms_info_2.bat_times.dcsmxs);
				printf("Charge_Num_2:%lu\r\n",sub_bms_info_2.bat_times.charge_times);
				printf("OvCh_vol_Num_2:%lu\r\n",sub_bms_info_2.bat_times.charge_over_vol_times);
				printf("OvDis_vol_Num_2:%lu\r\n",sub_bms_info_2.bat_times.discharge_under_vol_times);
				printf("OvCharge_cur_Num_2:%lu\r\n",sub_bms_info_2.bat_times.charge_over_cur_times);
				printf("OvDischarge_cur_Num_2:%lu\r\n",sub_bms_info_2.bat_times.discharge_under_cur_times);
				printf("Short_Num_2:%lu\r\n",sub_bms_info_2.bat_times.short_times);
				printf("UdTempeCh_Num_2:%lu\r\n",sub_bms_info_2.bat_times.charge_under_temp_times);
				printf("UdTempeDis_Num_2:%lu\r\n",sub_bms_info_2.bat_times.discharge_under_temp_times);
				printf("OvTempeCh_Num_2:%lu\r\n",sub_bms_info_2.bat_times.charge_over_temp_times);
				printf("OvTempeDis_Num_2:%lu\r\n",sub_bms_info_2.bat_times.discharge_over_temp_times);
				break;
				
			case OPERATE_DEEP_DATA://0x0D
				printf("ctr_temperature[0]:%d\r\n",ctr_temperature[0]);
				printf("ctr_temperature[1]:%d\r\n",ctr_temperature[1]);
				printf("Batter_Vol:%lu\r\n",Measure_Vol());
				
				printf("cb_val_last:%d\r\n",*(uint8_t*)(&cb_val_last));
				printf("cb_operate_state:%d\r\n",cb_operate_state);
				break;
				
			case OPERATE_ERASE_RECORD_TIMES://0x0E
				if(app_can_buf[len])
				{
					sub_bms_info_1.sub_bms_cmd.operate = OP_OPEN_FET;
					sub_bms_info_1.sub_bms_cmd.param = 0x03;
				}
				else
				{
					sub_bms_info_1.sub_bms_cmd.operate = OP_OPEN_FET;
					sub_bms_info_1.sub_bms_cmd.param = 0x00;
				}
				printf("BMS_1:%d\r\n",sub_bms_info_1.sub_bms_cmd.param);
				break;
				
			case OPERATE_RECORD_DATA://0x0F
				if(app_can_buf[len])
				{
					sub_bms_info_2.sub_bms_cmd.operate = OP_OPEN_FET;
					sub_bms_info_2.sub_bms_cmd.param = 0x03;
				}
				else
				{
					sub_bms_info_2.sub_bms_cmd.operate = OP_OPEN_FET;
					sub_bms_info_2.sub_bms_cmd.param = 0x00;
				}
				printf("BMS_2:%d\r\n",sub_bms_info_2.sub_bms_cmd.param);
				break;
				
			case OPERATE_LOW_SHAKE_DATA:
				

				
				break;

			case OPERATE_MEASURE_LOW_VOL:
				
				break;
			case OPERATE_TIMES_DATA:
				
				
				break;
			case 0x13:
				
				break;
			case 0x14:
				printf("mode:%d\r\n",mode);
				
				break;	
			case 0x30: // FF 0X30 LEN ADDR0 ADDR1 DATA
				
				break;
			case 0x31: // FF 0X31 LEN ADDR0 ADDR1 DATA
				
				break;
			default: break;
		}
		return 1;
	}
	else if(memcmp(app_can_buf,"mode",4) == 0)
	{
		if(memcmp(&app_can_buf[4],"test",4) == 0)
		{
			mode = 1;
			printf("test success!");
		}
		else if(memcmp(&app_can_buf[4],"jiaozhun",8) == 0)
		{
			
			mode = 4;
			printf("jiaozhun success!");
		}
		else if(memcmp(&app_can_buf[4],"peizhi",6) == 0)
		{
			mode = 2;
			printf("peizhi success!");
		}
		else if(memcmp(&app_can_buf[4],"ceshi",5) == 0)
		{
			mode = 3;
			printf("ceshi success!");
		}
		else if(memcmp(&app_can_buf[4],"tuichu",6) == 0)
		{
			mode = 0;
			printf("tuichu success!");
		}
		else if(memcmp(&app_can_buf[4],"cachu",5) == 0)
		{
			printf("cachu success!");
		}
		else
			return 0;	

		return 1;
	}
	else if(mode == 2&&app_can_buf[0] == 0xFC && app_can_buf[1] == 0xFC)
	{
	}
	else if(mode == 3&&app_can_buf[0] == 0xFE && app_can_buf[1] == 0xFE)
	{
	}
	else if(mode == 4&&app_can_buf[0] == 0xFD && app_can_buf[1] == 0xFD)
	{

	}
	else
	{
		mode = 0;
		Handle_Can_CMD(&app_can_frame);
		return 1;
	}
	/**/

	if(!Send_Data_Can(&app_can_frame,OTHER_SEND))
		return 0;

	return 1;
	
}


void Can_Com_Initial(void)
{
	CAN_Config();

	memset(app_can_buf,0x00,sizeof(app_can_buf));
	memset(&app_can_frame,0x00,sizeof(app_can_frame));
	app_can_frame.data = app_can_buf;

	memset(app_can_bms_1_buf,0x00,sizeof(app_can_bms_1_buf));
	memset(&app_can_bms_1_frame,0x00,sizeof(app_can_bms_1_frame));
	app_can_bms_1_frame.data = app_can_bms_1_buf;

	memset(app_can_bms_2_buf,0x00,sizeof(app_can_bms_2_buf));
	memset(&app_can_bms_2_frame,0x00,sizeof(app_can_bms_2_frame));
	app_can_bms_2_frame.data = app_can_bms_2_buf;
	
	memset(app_can_ctr_buf,0x00,sizeof(app_can_ctr_buf));
	memset(&app_can_ctr_frame,0x00,sizeof(app_can_ctr_frame));
	app_can_ctr_frame.data = app_can_ctr_buf;
	
	memset(app_can_adas_buf,0x00,sizeof(app_can_adas_buf));
	memset(&app_can_adas_frame,0x00,sizeof(app_can_adas_frame));
	app_can_adas_frame.data = app_can_adas_buf;

	memset(&bms_1_self_ss,0x00,sizeof(bms_1_self_ss));
	bms_1_self_ss.key = KEY_BMS_1_SELF_UP;
	memset(&bms_1_cur_vol_ss,0x00,sizeof(bms_1_cur_vol_ss));
	bms_1_cur_vol_ss.key = KEY_BMS_1_CUR_VOL_SELF_UP;

	memset(&bms_2_self_ss,0x00,sizeof(bms_2_self_ss));
	bms_2_self_ss.key = KEY_BMS_2_SELF_UP;
	memset(&bms_2_cur_vol_ss,0x00,sizeof(bms_2_cur_vol_ss));
	bms_2_cur_vol_ss.key = KEY_BMS_2_CUR_VOL_SELF_UP;
	
	memset(&end_ctr_self_ss,0x00,sizeof(end_ctr_self_ss));
	end_ctr_self_ss.key = KEY_END_CTR_SELF_UP;
	memset(&end_ctr_self_ss_new,0x00,sizeof(end_ctr_self_ss_new));
	end_ctr_self_ss_new.key = KEY_END_CTR_SELF_UP;
	memset(&end_ctr_rsp,0x00,sizeof(end_ctr_rsp));
	end_ctr_rsp.key = KEY_END_CTR_COMMON;
	
}

void Can_Test(void)
{
	app_can_frame.head.rsp = FRAME_PT_NEED_RSP;
	app_can_frame.head.dest = CTR_ID;
	app_can_frame.head.sour = SELF_ID;
	app_can_frame.head.index = 1;
	app_can_frame.head.total = 2;
	app_can_frame.head.pro   =FRAME_PRO_D;
	//
	app_can_frame.len = 15;
	memset(app_can_frame.data,0xCC,15);
	Send_Data_Can(&app_can_frame,CAN_TEST);
}

