#ifndef _G_VARIABLE_H
#define _G_VARIABLE_H

#include <linux/can.h>
#include <linux/can/raw.h>
#include <pthread.h>
#include "sys.h"

/* CAN协议参数值 */
/* 1.1 帧ID: 来自工控机(ECU) */
#define ECU_EPS_CMD 0x1801B0A0
#define ECU_SPEED_CMD 0x1803B0A0
#define ECU_CONTROL_CMD 0x1805B0A0

/* 1.2 帧ID: 来自整车控制器（VCU） */
#define DRIVING_STATE 0x1804A0B0
#define VCU_VEHICLE_STATE1 0x1806A0B0
#define VCU_VEHICLE_STATE4 0x1812A0B0
#define VCU_VEHICLE_STATE8 0x1818A0B0
#define EPS_STATE 0x1802A0B0

/* 2.1 EPS模式 */
#define EPS_MODE_STOP (0x00)
#define EPS_MODE_MANUAL (0x10) // 助力模式
#define EPS_MODE_AUTO (0x20)   // 自动控制方式（角度控制方式）

/* 2.2 档位 */
#define GEAR_N (0b0000) // 空挡
#define GEAR_D (0b0001) // 前进挡
#define GEAR_R (0b0010) // 倒车档

/* 3.0 CAN报文发送周期 */
#define EPS_TIMER (20000) // 20ms
#define SPEED_TIMER (20000)
#define CONTROL_TIMER (50000)

#pragma pack(push)
#pragma pack(1)

/* 智能控制器EPS命令 ECU_EPS_CMD（协议3.1） */
struct ecu_eps_st
{
	// 1
	unsigned char eps_state;
	// 2
	unsigned char heartbeat;
	// 3
	unsigned char ang_velocity_max; // 方向盘角速度命令
	// 4~5
	unsigned short steering_wheel_angle; // 方向盘角度指令
	// 6~7
	unsigned short reserved;
	// 8
	unsigned char xor_verify;
};

/* 智能控制器车速命令 ECU_SPEED_CMD（协议3.3） */
struct ecu_speed_st
{

	unsigned char acceleration; /*加速度指令 */

	unsigned char reserved1 : 2;
	unsigned char epb_state : 2;
	unsigned char gear_state : 4; /*挡位指令 */

	unsigned char heartbeat;

	unsigned char emergency_brake : 1;
	unsigned int reserved2 : 31;

	unsigned char xor_verify;
};

/* 车辆控制命令 ECU_CONTROL_CMD（协议3.5） */
struct ecu_control_st
{
	unsigned char autopilot : 2;
	unsigned char door : 2;
	unsigned char reserved1 : 2;
	unsigned char daytime_driving_light : 2;

	unsigned char left_blinker : 2;
	unsigned char right_blinker : 2;
	unsigned char warning_light : 2;
	unsigned char clearance_light : 2;

	unsigned char headlamp : 2;
	unsigned char rear_foglight : 2;
	unsigned char left_foglight : 2;
	unsigned char right_foglight : 2;

	unsigned char interior_light : 2;
	unsigned char reserved2 : 2;
	unsigned char reserved3 : 4;

	unsigned char system_status : 2;		// 无人驾驶系统启动状态
	unsigned char system_reliability : 2;	// 无人驾驶系统可靠性
	unsigned char system_failure_level : 2; // 无人驾驶系统故障级别
	unsigned char reserved4 : 2;

	unsigned char ac_mode : 3;
	unsigned char ac_temperature : 5;

	unsigned char reserved5;

	unsigned char heartbeat;
};

/* 控制命令2 GATE_CONTROL_CMD2 */
struct gate_control_st2
{
	unsigned char AVAS;
	unsigned char ask_for_rc : 4;
	unsigned char reserved1;
	unsigned char reserved2;
	unsigned char reserved3;
	unsigned char reserved4;
	unsigned char reserved5;
	unsigned char reserved6 : 4;
	unsigned char heartbeat;
};

/* VCU_VEHICLE_STATE1 */
struct vcu_vstate1_st
{
	unsigned char autopilot : 2;
	unsigned char door : 1;
	unsigned char daytime_driving_light : 1;
	unsigned char left_blinker : 1;
	unsigned char right_blinker : 1;
	unsigned char warning_light : 1;
	unsigned char clearance_light : 1;

	unsigned char headlamp : 2;
	unsigned char rear_foglight : 1;
	unsigned char left_foglight : 1;
	unsigned char right_foglight : 1;
	unsigned char interior_light : 1;
	unsigned char ambient_light : 1;
	unsigned char door_button : 1;

	unsigned char speed; // 车速

	unsigned char SOC;

	unsigned char system_power : 2;				 // 无人驾驶系统状态
	unsigned char charge_status : 2;			 // 充电状态
	unsigned char charger_connection_status : 2; // 充电连接指示
	unsigned char failure_level : 2;			 // 整车故障级别

	unsigned short ODO; // 总里程

	unsigned char heartbeat;
};

/* VCU_VEHICLE_STATE4 */
struct vcu_vstate4_st
{
	unsigned char out_temperature;
	unsigned char in_temperature;

	unsigned char aircondition : 4;
	unsigned char power : 2;
	unsigned char hill_park_assist : 1;
	unsigned char regenerative_brake : 1;
	unsigned short remaining_mileage : 12;
	unsigned char artificial_take : 1;
	unsigned char reserved1 : 2;
	unsigned char allow_rc : 1;
	unsigned char temperature : 5;
	unsigned char reserved2 : 3;
	unsigned short battery_power;
};

/* VCU_VEHICLE_STATE8 */
struct vcu_vstate8_st
{
	unsigned char DCDC_voltage;
	unsigned char DCDC_current;
	unsigned char battery_voltage;
	unsigned char reserved1;
	unsigned char reserved2;
	unsigned char reserved3;

	unsigned char reserved4 : 1;
	unsigned char reserved5 : 1;
	unsigned char reserved6 : 1;
	unsigned char conditions : 1;
	unsigned char horn : 1; // 喇叭状态
	unsigned char BodyCtrStatus : 1;
	unsigned char reserved7 : 2;

	unsigned char reserved8;
};

/* 行车状态 VCU_DRIVE_STATE（协议3.4） */
// struct vcu_dstate_st
// {
// 	// 1
// 	unsigned char epb_state : 2;
// 	unsigned char gear_state : 4;
// 	unsigned char reserved1 : 2;
// 	// 2~3
// 	unsigned short motor_speed;
// 	// 4~5
// 	unsigned short motor_torque;
// 	// 6
// 	unsigned char reserved2;
// 	// 7
// 	unsigned char acceleration;
// 	// 8
// 	unsigned char xor_verify;
// };

/* 车辆EPS状态 Vehicle_EPS */
struct eps_state
{
	unsigned char eps_ctrl_st;
	unsigned char sw_moment;  // 方向盘力矩
	unsigned char eps_moment; // EPS 输出力矩
	unsigned short sw_angle;  // 方向盘角度
	unsigned char ctrl_tm : 6;
	unsigned char calibration_status : 1;  // 角传感器标定状态
	unsigned char intervention_status : 1; // 驾驶员干预状态
	unsigned char eps_fail : 2;
	unsigned char reserved2 : 6;
	unsigned char heart;
};

/* 行车状态 VCU_DRIVE_STATE */
struct driving_state
{
	unsigned char ebp_st : 2;  // 电子手刹状态
	unsigned char gear_st : 4; // 档位状态
	unsigned char emergency_braking : 1;
	unsigned char reserved1 : 1;
	unsigned short motor_speed;
	unsigned short motor_torque;
	unsigned char ratio;
	unsigned short acceleration;
	unsigned char xor_verify;
};

#pragma pack(pop)

extern struct vcu_vstate1_st g_vcu_vstate1;
extern struct vcu_vstate4_st g_vcu_vstate4;
extern struct vcu_vstate8_st g_vcu_vstate8;
extern struct eps_state g_eps_state;
extern struct driving_state g_driving_state;

extern struct ecu_eps_st g_ecu_eps_cmd;
extern struct ecu_speed_st g_ecu_speed_cmd;
extern struct ecu_control_st g_ecu_control_cmd;
extern struct gate_control_st2 g_ecu_control2_cmd;

/* 定时器 */
extern SYSTIMER t1;
extern SYSTIMER t2;
extern SYSTIMER t3;

/* 定义Linux的信号量与锁 */
extern pthread_mutex_t Mutex_CAN_S1_DATA;
extern pthread_mutex_t Mutex_CAN_S4_DATA;
extern pthread_mutex_t Mutex_CAN_S8_DATA;
extern pthread_mutex_t Mutex_CAN_DRV_DATA;
extern pthread_mutex_t Mutex_CAN_EPS_DATA;

extern pthread_mutex_t Mutex_EPS_DATA;
extern pthread_mutex_t Mutex_SPEED_DATA;
extern pthread_mutex_t Mutex_CONTROL_DATA;

/* CAN接收数组 */
extern struct can_frame read_frame;
extern struct can_frame write_frame;

extern int Output;
extern int OutputSpeed;

/* 打印输出 */
extern int print_st_1;
extern int print_st_4;
extern int print_st_8;
extern int print_st_eps;
extern int print_st_drive;
#endif // !_G_VARIABLE_H
