#include "iap.h"
#include "CommandsDefine.h"
#include "ProjectConfig.h"
#include "usart.h"
#include "string.h"
#include "Helper.h"
/* 
	第一次验证：
	1、总字节数要相等
	2、总内容的Crc要相等，总内容已经烧写在FLASH_ADDR_UPDATE_TEMP_AREA
*/

void CMD0x0B_Request_UpdateVerifyStage1_process(u8 *pData,u16 dataLen,u8 bIsContentBitInverted_Recv,u8 bIsContentBitInverted_SendBack)
{
#ifdef RUNNING_WHERE_AT_BOOT
		if(Updating==0)
		{
			Command_SendbackErrorMessage(Error0x06_CMD_UpdateNotStart,1);
			return ;
		}
		//TotalBytes
		u32 totalBytesStartIndex=0;//format totalBytes[4bytes]+crcCode[4bytes]+data...+
		u32 totalBytes=pData[totalBytesStartIndex] + (pData[totalBytesStartIndex+1]<<8) + (pData[totalBytesStartIndex+2]<<16)+ (pData[totalBytesStartIndex+3]<<24);;
		//Crc
		u32 crcStartIndex=totalBytesStartIndex+sizeof(u32);

		u32 getCRC32 = pData[crcStartIndex] + (pData[crcStartIndex+1]<<8) + (pData[crcStartIndex+2]<<16)+ (pData[crcStartIndex+3]<<24);
		u32 calcCRC32=0;
		if (UpdateRecvedContentBytes==totalBytes)
		{
			calcCRC32 = Helper_CRC_CalcFlashBlockCRC8(FLASH_ADDR_UPDATE_TEMP_AREA,totalBytes);
			if(getCRC32==calcCRC32)
			{
				UpdateVerifyStage1OK=1;
			}
		}
		//response!!!
		u8 dataBytes=sizeof(u32)+sizeof(u32);//totalLength,crcCode
		
		memcpy(USART_WR_BUF,&UpdateRecvedContentBytes,sizeof(u32));
		memcpy((&USART_WR_BUF[0]+sizeof(u32)),&calcCRC32,sizeof(u32));	

		USART6_Send(CMD0x0B_Request_UpdateVerifyStage1,dataBytes,bIsContentBitInverted_SendBack);			
#else
		Command_SendbackErrorMessage(Error0x01_CannotRunAtApp,1);
#endif
}



