/*
 *  Created on: 2023年1月6日
 *  Author: hc
 *  车端处理无线通讯中的协议处理
 */
#include "stdio.h"
#include "AppTaskUart1.h"

/*
 *  Author: hc
 *  ******  车端协议解码函数 *******
 *
 *  uint8* dataOutBuffer 待解码数据
 *  *** 注意:解码后的数据仍存放到该数组中 ***
 *   uint8 buffSize 该数组长度
 *   uint8 cmd 解码指令
 *
 *   return 解码后长度
 */
uint16 DecodeSlaveBuffer(uint8* dataOutBuffer, uint8 buffSize, uint8 cmd)
{
	uint8  start_field[3] = {0X68,0X23,0X24};
	uint8  car_vin[17] = "12345678901234567";
	uint16 tx_len = 0;
	uint8  time_now[8] = {0};

	memcpy(dataOutBuffer + tx_len, start_field, 3);
	tx_len += 3;
	tx_len += 2; /* 长度 +2 */
	tx_len ++;   /* 指令 +1*/
	memcpy(dataOutBuffer + tx_len, car_vin, 17);
	tx_len += 17;

	switch(cmd)
	{
		case HEARTBEAT_S:
		{
			uint8 lock_sta = 0; 		/* 锁止状态 */
			uint8 key_sta = 0;			/* 钥匙状态 */
			uint8 ele_sta = 0; 			/* 电磁阀驱动状态 */
			if(1 == KeySignal)
			{
				key_sta = 2;
			}
			else if(0 == KeySignal)
			{
				key_sta = 1;
			}
			else{}

			if(1 == LockCheck)
			{
				lock_sta = 2;
			}
			else
				lock_sta = 1;

			ele_sta = SolValActuSt;

			dataOutBuffer[5] = HEARTBEAT_S;
			memcpy(dataOutBuffer + tx_len, time_now, 8);
			tx_len += 8;
			dataOutBuffer[tx_len] = lock_sta;
			tx_len += 1;
			dataOutBuffer[tx_len] = key_sta;
			tx_len += 1;
			dataOutBuffer[tx_len] = ele_sta;
			tx_len += 1;
			break;
		}
		case LOCK_S:
		{
			uint8 answer = 0;
			if(0 == SolValActuSt)
			{
				answer = 0;
			}
			else if(1 == SolValActuSt)
			{
				answer = 1;
			}

			dataOutBuffer[5] = LOCK_S;
			memcpy(dataOutBuffer + tx_len, time_now, 8);
			tx_len += 8;
			dataOutBuffer[tx_len] = answer;
			tx_len += 1;
			lock_sta = 2;
			break;
		}
		case UNLOCK_S:
		{
			uint8 answer = 0X0;
			if(0 == SolValActuSt)
			{
				answer = 1;
			}
			else if(1 == SolValActuSt)
			{
				answer = 0;
			}
			dataOutBuffer[5] = UNLOCK_S;
			memcpy(dataOutBuffer + tx_len, time_now, 8);
			tx_len += 8;
			dataOutBuffer[tx_len] = answer;
			tx_len += 1;
			lock_sta = 1;
			break;
		}
		case GETINFO_S:
		{
			memcpy(dataOutBuffer + tx_len, time_now, 8);
			tx_len += 8;

			SLAVE_INFO slav_info_temp = {0};
			memcpy(slav_info_temp.sn, BatSN, 26);
			slav_info_temp.com_sta = BatComState;
			slav_info_temp.BMS_sta = BatBMS_State;
			slav_info_temp.touch_sta = MContactorsState;
			slav_info_temp.bms_v = BatValtage;
			slav_info_temp.bms_a = BatteryCurrent;
			slav_info_temp.soc = SOC;
			slav_info_temp.soh = SOH;
			slav_info_temp.str_sta = VehicleState;
			slav_info_temp.stop_sta = BrakeSignal;
			slav_info_temp.odo = AccruedMileage;
			slav_info_temp.inputA = AccruedChargeAh;
			slav_info_temp.outputA = AccruedDischargeAh;
			slav_info_temp.inputKwh = AccruedChargeKwh;
			slav_info_temp.outputKwh = AccruedDischargeKwh;
			slav_info_temp.enger = BatRateRnergy;
			dataOutBuffer[5] = GETINFO_S;
			memcpy(dataOutBuffer + tx_len, &slav_info_temp, sizeof(SLAVE_INFO));
			tx_len += sizeof(SLAVE_INFO);
			break;
		}
		default:
			tx_len = 0;
			break;
	}
	dataOutBuffer[tx_len] = 0X68;
	tx_len += 1;
	dataOutBuffer[3] = tx_len & 0XFF;
	dataOutBuffer[4] = (tx_len >> 8) & 0XFF;

	return tx_len;
}
