
#include "CAN_recv.h"
#include "zxwl_log.hpp"
using namespace std;

/*接收CAN数据 */
void can_recv(int fd)
{
	// ZXWL_DEBUG << "can_recv" << ZXWL_REND;
	int nbytes;
	nbytes = read(fd, &read_frame, sizeof(struct can_frame));
	unsigned int id;
	id = read_frame.can_id & CAN_EFF_MASK;
	// /*CAN消息打印*/
	// for (int i = 0; i < nbytes; i++)
	// {
	// 	printf("CAN1: ");
	// 	printf("%03X: ", id);
	// 	for (int j = 0; j < read_frame.can_dlc; j++)
	// 	{
	// 		printf("%02X ", read_frame.data[j]);
	// 	}
	// 	printf("\n");
	// }

	if (id == VCU_VEHICLE_STATE1)
	{
		pthread_mutex_lock(&Mutex_CAN_S1_DATA);
		memcpy(&g_vcu_vstate1, read_frame.data, read_frame.can_dlc * sizeof(char));
		if (print_st_1)
		{
			ZXWL_DEBUG << "VCU_VEHICLE_STATE1收到了---------------%" << nbytes << ZXWL_REND;
		}
		// pthread_cond_signal(&Cond_CAN_DATA);
		pthread_mutex_unlock(&Mutex_CAN_S1_DATA);
	}
	else if (id == VCU_VEHICLE_STATE4)
	{
		pthread_mutex_lock(&Mutex_CAN_S4_DATA);
		memcpy(&g_vcu_vstate4, read_frame.data, read_frame.can_dlc * sizeof(char));
		if (print_st_4)
		{
			ZXWL_DEBUG << "VCU_DRIVE_STATE4收到了---------------%" << nbytes << ZXWL_REND;
		}
		pthread_mutex_unlock(&Mutex_CAN_S4_DATA);
	}
	else if (id == VCU_VEHICLE_STATE8)
	{
		pthread_mutex_lock(&Mutex_CAN_S8_DATA);
		memcpy(&g_vcu_vstate8, read_frame.data, read_frame.can_dlc * sizeof(char));
		if (print_st_8)
		{
			ZXWL_DEBUG << "VCU_DRIVE_STATE8收到了---------------%" << nbytes << ZXWL_REND;
		}
		pthread_mutex_unlock(&Mutex_CAN_S8_DATA);
	}
	else if (id == DRIVING_STATE)
	{
		pthread_mutex_lock(&Mutex_CAN_DRV_DATA);
		// memcpy(&g_driving_state, read_frame.data, read_frame.can_dlc * sizeof(char));

		g_driving_state.ebp_st = read_frame.data[0] & 0b00000011;
		g_driving_state.gear_st = (read_frame.data[0] & 0b00111100) >> 2;
		g_driving_state.emergency_braking = (read_frame.data[0] & 0b01000000) >> 6;
		g_driving_state.motor_speed = (read_frame.data[2] * 256) + read_frame.data[1] - 15000;
		g_driving_state.motor_torque = (read_frame.data[4] * 256) + read_frame.data[3] - 5000;
		g_driving_state.ratio = read_frame.data[5] + 30;
		g_driving_state.acceleration = read_frame.data[6];
		g_driving_state.xor_verify = read_frame.data[7];
		if (print_st_drive)
		{
			ZXWL_DEBUG << "VCU_DRIVING_STATE收到了---------------%" << nbytes << ZXWL_REND;
			printf("can id: %X\n", read_frame.can_id | CAN_EFF_FLAG);
			for (int j = 0; j < 2; j++)
			{
				printf("%02x ", read_frame.data[j]);
			}
			printf("\n");
			if (g_driving_state.ebp_st == 0)
				cout << "电子手刹状态：故障" << endl;
			else if (g_driving_state.ebp_st == 1)
				cout << "电子手刹状态：拉起手刹（驻车）" << endl;
			else if (g_driving_state.ebp_st == 2)
				cout << "电子手刹状态：释放手刹" << endl;
			else if (g_driving_state.ebp_st == 3)
				cout << "电子手刹状态：行进间制动" << endl;

			if (g_driving_state.gear_st == 0)
				cout << "档位状态：N档" << endl;
			else if (g_driving_state.gear_st == 1)
				cout << "档位状态：D档" << endl;
			else if (g_driving_state.gear_st == 2)
				cout << "档位状态：R档" << endl;

			if (g_driving_state.emergency_braking == 0)
				cout << "紧急制动状态：未制动" << endl;
			else if (g_driving_state.emergency_braking == 1)
				cout << "紧急制动状态：制动" << endl;

			cout << "电机转速：" << g_driving_state.motor_speed << endl;
			cout << "电机扭矩：" << g_driving_state.motor_torque << endl;
			cout << "电机转速与车速比值：" << static_cast<int>(g_driving_state.ratio) << endl;
			cout << "当前加速度：" << (float)(g_driving_state.acceleration) * 0.1 - 12.8 << endl;
			cout << "异或检验：" << static_cast<int>(g_driving_state.xor_verify) << endl;
		}
		pthread_mutex_unlock(&Mutex_CAN_DRV_DATA);
	}
	else if (id == EPS_STATE)
	{
		pthread_mutex_lock(&Mutex_CAN_EPS_DATA);
		// memcpy(&g_eps_state, read_frame.data, read_frame.can_dlc * sizeof(char));
		g_eps_state.eps_ctrl_st = read_frame.data[0];
		g_eps_state.sw_moment = read_frame.data[1];
		g_eps_state.eps_moment = read_frame.data[2];
		g_eps_state.sw_angle = (read_frame.data[4] * 256) + read_frame.data[3];
		if (print_st_eps)
		{
			ZXWL_DEBUG << "VCU_EPS_STATE收到了---------------%" << nbytes << ZXWL_REND;
			printf("can id: %X\n", read_frame.can_id | CAN_EFF_FLAG);
			for (int i = 0; i < 8; i++)
			{
				printf("%02X ", read_frame.data[i]);
			}
			printf("\n");
			if (g_eps_state.eps_ctrl_st == 0x00)
				cout << "EPS驾驶状态：停止控制" << endl;
			else if (g_eps_state.eps_ctrl_st == 0x10)
				cout << "EPS驾驶状态：手动控制方式（助力模式）" << endl;
			else if (g_eps_state.eps_ctrl_st == 0x20)
				cout << "EPS驾驶状态：自动控制方式（角度控制方式） " << endl;
			cout << "方向盘力矩：" << (float)(g_eps_state.sw_moment) * 0.1 - 12.8 << endl;
			cout << "EPS 输出力矩：" << float(g_eps_state.eps_moment) * 0.3 - 38.4 << endl;
			cout << "方向盘角度：" << float(g_eps_state.sw_angle) * 0.1 - 1080 << endl;
			printf("\n");
		}
		pthread_mutex_unlock(&Mutex_CAN_EPS_DATA);
	}
	else
	{
		/*暂不解析 */
	}
	// std::this_thread::sleep_for(std::chrono::milliseconds(100)); // ms
}