
#define	_IN_TILETRANS_C
#include "filetrans.h"

#include "transdata.h"
#include "exbfiletrans.h"

//------------------------------------------------------

//------------------------------------------------------

//------------------------------------------------------

FileTransCtrl g_fileTransCtrl;

//------------------------------------------------------

//------------------------------------------------------

//------------------------------------------------------

#if (TRANS_DATA_FILE != 0)

int ResetFileList(OperPacket * pPkt)
{
	if (pPkt == NULL)
	{
		return -1;
	}

	g_datFile.fileIdxMap = pPkt->setFilesList.fileIdxMap;
	if (g_datFile.fileIdx == -1)
	{
		// 如果当前没有选择文件,则文件失效
		InvalidDatFile(0);
	}
	else
	{
		// 如果当前文件的位图无效,则文件失效
		if ((g_datFile.fileIdxMap & (0x01<<g_datFile.fileIdx)) == 0)
		{
			InvalidDatFile(0);
			g_datFile.fileIdx = -1;
		}
	}

	printf("g_datFile.fileIdxMap =  0x%x \r\n",g_datFile.fileIdxMap);
	return 0;
}
#endif

//------------------------------------------------------
// 功能: 启动文件传输, 接收上位机传输的数据
// 参数: pPkt 接收到的数据包
// 结果:
// = 0, 成功
// = 1, 失败

int StartReceiveFile(OperPacket * pPkt)
{
	int save;

	if (pPkt == NULL)
	{
		return -1;
	}
	if (1	&&
#if (TRANS_DATA_FILE != 0)
		pPkt->packet.vldp.exlen != sizeof(DataFileHead) &&
#endif
		pPkt->packet.vldp.exlen != sizeof(AppFileHead)	&&
		pPkt->packet.vldp.exlen != sizeof(BoardFileHead)&&
		1	)
	{
		return -1;
	}

	printf("get cmd start receive file\r\n");
	InitFileTransCtrl(&g_fileTransCtrl);

	// 根据发送的文件类型确定保存方式及保存地址
#if (TRANS_DATA_FILE != 0)
	if (pPkt->startTrans.fileType == FILE_TYPE_DAT)			// 数据文件
	{
#if (FILE_NETWORK == 0)
		DataFileHead * pHead;
		pHead = (DataFileHead *)(pPkt->packet.vldp.exData);

		InitNorSectorManage();	// 初始化norflash扇区管理
		// 根据大小，存放在sram或norflash中
		if (pHead->fileHead.dataSize <= DATA_FILE_SIZE)
		{
			save = STORAGE_EXSRAM;
		}
		else
		{
			save = STORAGE_SRAMANDNOR;		// 混合存放
			//InitNorSectorManage();
			if (pHead->fileHead.dataSize > DATA_FILE_SIZE)
			{
				NorAllocAndSetUsingFlags(pHead->fileHead.dataSize - DATA_FILE_SIZE);
			}
		}
#else
		save = STORAGE_NETWORK;
#endif
	}
	else
#endif
	if (pPkt->startTrans.fileType == FILE_TYPE_PGM)	// 升级文件
	{
#if (APP_SAVE_TO == APP_SAVE_NORFLASH)
		save = STORAGE_NORFLASH;
#elif (APP_SAVE_TO == APP_SAVE_EXRAM)	// 20230523 完善使用外部RAM进行升级
		save = STORAGE_EXSRAM;
#elif (APP_SAVE_TO == APP_SAVE_INFLASH)
		save = STORAGE_INFLASH;
#endif
		InitSaveApp();		// APP升级初始化
	}
	else if (pPkt->startTrans.fileType == FILE_TYPE_BOARD)	// 外围板文件
	{
		save = STORAGE_TRANS;		// 数据转发,不进行存储
	}
	else if (pPkt->startTrans.fileType == FILE_TYPE_FRAME)	// 边框刺绣文件
	{
		save = STORAGE_SPAREARAM;
	}
	else
	{
		printf("not support file\r\n");
		return -1;
	}

	if (save == STORAGE_EXSRAM)
	{
#if (TRANS_DATA_FILE != 0)
		if (pPkt->startTrans.fileType == FILE_TYPE_DAT)		// 数据文件
		{
			g_datFile.storage = STORAGE_EXSRAM;
			g_datFile.fileIdx = g_fileTransCtrl.fileIdx;
			g_fileTransCtrl.transActive = TRANS_NONE;
			g_fileTransCtrl.transflag = TRANS_NOT_BEG;
			g_fileTransCtrl.fileType = pPkt->startTrans.fileType;
			g_fileTransCtrl.fileIdx = pPkt->startTrans.fileIdx;
			g_fileTransCtrl.fileId = pPkt->startTrans.fileId;

			g_fileTransCtrl.pDatFile = &g_datFile;
			g_fileTransCtrl.pAppFile = NULL;

			g_datFile.pFileHead = &g_fileHead1;
			memcpy((u8*)g_fileTransCtrl.pDatFile->pFileHead, pPkt->packet.vldp.exData, pPkt->packet.vldp.exlen);	// 拷贝数据到结构

			g_fileTransCtrl.packetNum = ((g_fileTransCtrl.pDatFile->pFileHead->fileHead.dataSize+g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk-1)/g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk);
			g_fileTransCtrl.lenBmp = (g_fileTransCtrl.packetNum+7)/8;
//			printf("rsv packet num=%d, bitmap len=%d\r\n", g_fileTransCtrl.packetNum, g_fileTransCtrl.lenBmp);
			memset(g_fileTransCtrl.rsvBitmap, 0, LEN_RSV_BMP);	// 花样接收进度标志位图

			g_fileTransCtrl.transflag = TRANS_READY;
			g_fileTransCtrl.transActive = TRANS_ACTIVE;
			InvalidDatFile(0);
			SetNewDatFlag(0, FALSE);
		}
		else
#endif
		// 20230523 完善使用外部RAM进行升级
		if (pPkt->startTrans.fileType == FILE_TYPE_PGM)		// 升级文件
		{
			g_fileTransCtrl.transActive = TRANS_NONE;
			g_fileTransCtrl.transflag = TRANS_NOT_BEG;
			g_fileTransCtrl.fileType = pPkt->startTrans.fileType;
			g_fileTransCtrl.fileIdx = pPkt->startTrans.fileIdx;
			g_fileTransCtrl.fileId = pPkt->startTrans.fileId;

			g_fileTransCtrl.pDatFile = NULL;
			g_fileTransCtrl.pAppFile = &g_appFile;

			memcpy(&(g_fileTransCtrl.pAppFile->fileHead), pPkt->packet.vldp.exData, pPkt->packet.vldp.exlen);	// 拷贝数据到结构

			g_fileTransCtrl.packetNum = ((g_fileTransCtrl.pAppFile->fileHead.dataSize+1024-1)/1024);		// 只支持1024字节的包
			g_fileTransCtrl.lenBmp = (g_fileTransCtrl.packetNum+7)/8;
			memset(g_fileTransCtrl.rsvBitmap, 0, g_fileTransCtrl.lenBmp);	// 文件接收进度标志位图

			g_fileTransCtrl.transflag = TRANS_READY;
			g_fileTransCtrl.transActive = TRANS_ACTIVE;

			InvalidDatFile(0);
			SetNewDatFlag(0, FALSE);
		}
	}
	else if (save == STORAGE_SPAREARAM)
	{
		if (pPkt->startTrans.fileType == FILE_TYPE_FRAME)	// 边框刺绣文件
		{
			g_datFile.storage = STORAGE_SPAREARAM;
			g_datFile.fileIdx = g_fileTransCtrl.fileIdx;
			g_fileTransCtrl.transActive = TRANS_NONE;
			g_fileTransCtrl.transflag = TRANS_NOT_BEG;
			g_fileTransCtrl.fileType = pPkt->startTrans.fileType;
			g_fileTransCtrl.fileIdx = pPkt->startTrans.fileIdx;
			g_fileTransCtrl.fileId = pPkt->startTrans.fileId;

			g_fileTransCtrl.pDatFile = &g_datFile;
			g_fileTransCtrl.pAppFile = NULL;

			g_datFile.pFileHead = &g_fileHead2;
			memcpy(&(g_fileTransCtrl.pDatFile->pFileHead), pPkt->packet.vldp.exData, pPkt->packet.vldp.exlen);	// 拷贝数据到结构

			g_fileTransCtrl.packetNum = ((g_fileTransCtrl.pDatFile->pFileHead->fileHead.dataSize+g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk-1)/g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk);
			g_fileTransCtrl.lenBmp = (g_fileTransCtrl.packetNum+7)/8;
			printf("rsv packet num=%d, bitmap len=%d\r\n", g_fileTransCtrl.packetNum, g_fileTransCtrl.lenBmp);
			memset(g_fileTransCtrl.rsvBitmap, 0, LEN_RSV_BMP);	// 花样接收进度标志位图

			g_fileTransCtrl.transflag = TRANS_READY;
			g_fileTransCtrl.transActive = TRANS_ACTIVE;
			InvalidDatFile(0);
			SetNewDatFlag(0, FALSE);
		}
	}
	else if (save == STORAGE_NORFLASH || save == STORAGE_SRAMANDNOR)
	{
#if (TRANS_DATA_FILE != 0)
		if (pPkt->startTrans.fileType == FILE_TYPE_DAT)				// 数据文件
		{
			g_datFile.storage = save;
			g_datFile.fileIdx = g_fileTransCtrl.fileIdx;
			g_fileTransCtrl.transActive = TRANS_NONE;
			g_fileTransCtrl.transflag = TRANS_NOT_BEG;
			g_fileTransCtrl.fileType = pPkt->startTrans.fileType;
			g_fileTransCtrl.fileIdx = pPkt->startTrans.fileIdx;
			g_fileTransCtrl.fileId = pPkt->startTrans.fileId;

			g_fileTransCtrl.pDatFile = &g_datFile;
			g_fileTransCtrl.pAppFile = NULL;

			g_datFile.pFileHead = &g_fileHead1;
			memcpy((u8*)g_fileTransCtrl.pDatFile->pFileHead, pPkt->packet.vldp.exData, pPkt->packet.vldp.exlen);	// 拷贝数据到结构

			g_fileTransCtrl.packetNum = ((g_fileTransCtrl.pDatFile->pFileHead->fileHead.dataSize+g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk-1)/g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk);
			g_fileTransCtrl.lenBmp = (g_fileTransCtrl.packetNum+7)/8;
			memset(g_fileTransCtrl.rsvBitmap, 0, g_fileTransCtrl.lenBmp);	// 花样接收进度标志位图

			printf("rsv packet num=%d, bitmap len=%d\r\n", g_fileTransCtrl.packetNum, g_fileTransCtrl.lenBmp);

			if (1)			// 未使用位写1
			{
				int idx = g_fileTransCtrl.packetNum;
				while((idx % 8) != 0)
				{
					WriteRsvBitmap(idx, 1, &g_fileTransCtrl);
					idx++;
				}
				if (g_fileTransCtrl.lenBmp < LEN_RSV_BMP)
				{
					memset(&(g_fileTransCtrl.rsvBitmap[g_fileTransCtrl.lenBmp]), 0xff, LEN_RSV_BMP - g_fileTransCtrl.lenBmp);	// 花样接收进度标志位图
				}
			}

			g_fileTransCtrl.transflag = TRANS_READY;
			g_fileTransCtrl.transActive = TRANS_ACTIVE;

			InvalidDatFile(0);
			SetNewDatFlag(0, FALSE);
		}
		else
#endif
		if (pPkt->startTrans.fileType == FILE_TYPE_PGM)		// 升级文件
		{
			g_fileTransCtrl.transActive = TRANS_NONE;
			g_fileTransCtrl.transflag = TRANS_NOT_BEG;
			g_fileTransCtrl.fileType = pPkt->startTrans.fileType;
			g_fileTransCtrl.fileIdx = pPkt->startTrans.fileIdx;
			g_fileTransCtrl.fileId = pPkt->startTrans.fileId;

			g_fileTransCtrl.pDatFile = NULL;
			g_fileTransCtrl.pAppFile = &g_appFile;

			memcpy(&(g_fileTransCtrl.pAppFile->fileHead), pPkt->packet.vldp.exData, pPkt->packet.vldp.exlen);	// 拷贝数据到结构

			g_fileTransCtrl.packetNum = ((g_fileTransCtrl.pAppFile->fileHead.dataSize+1024-1)/1024);		// 只支持1024字节的包
			g_fileTransCtrl.lenBmp = (g_fileTransCtrl.packetNum+7)/8;
			memset(g_fileTransCtrl.rsvBitmap, 0, g_fileTransCtrl.lenBmp);	// 文件接收进度标志位图

			g_fileTransCtrl.transflag = TRANS_READY;
			g_fileTransCtrl.transActive = TRANS_ACTIVE;
		}
	}
	else if (save == STORAGE_NETWORK)
	{
#if (TRANS_DATA_FILE != 0)
		if (pPkt->startTrans.fileType == FILE_TYPE_DAT)		// 数据文件
		{
			g_datFile.storage = STORAGE_NETWORK;
			g_datFile.fileIdx = g_fileTransCtrl.fileIdx;
			g_fileTransCtrl.transActive = TRANS_NONE;
			g_fileTransCtrl.transflag = TRANS_REQUEST;
			g_fileTransCtrl.fileType = pPkt->startTrans.fileType;
			g_fileTransCtrl.fileIdx = pPkt->startTrans.fileIdx;
			g_fileTransCtrl.fileId = pPkt->startTrans.fileId;

			g_fileTransCtrl.pDatFile = &g_datFile;
			g_fileTransCtrl.pAppFile = NULL;

			g_datFile.pFileHead = &g_fileHead1;
			memcpy((u8*)g_fileTransCtrl.pDatFile->pFileHead, pPkt->packet.vldp.exData, pPkt->packet.vldp.exlen);	// 拷贝数据到结构

			g_fileTransCtrl.packetNum = ((g_fileTransCtrl.pDatFile->pFileHead->fileHead.dataSize+g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk-1)/g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk);
			g_fileTransCtrl.lenBmp = (g_fileTransCtrl.packetNum+7)/8;
			memset(g_fileTransCtrl.rsvBitmap, 0xff, LEN_RSV_BMP);	// 花样接收进度标志位图，设置为完成状态

			printf("rsv packet num=%d, bitmap len=%d\r\n", g_fileTransCtrl.packetNum, g_fileTransCtrl.lenBmp);

			memset(&(g_datFile.pDatExCtrl->exbufFlag[0]), 0, sizeof(int)*FILE_EXBUF_NUM);	// 数据交换区域清空
			g_fileTransCtrl.transflag = TRANS_REQUEST;
			g_fileTransCtrl.transActive = TRANS_ACTIVE;

			AvalidDatFile(0);			// 文件有效
			SetNewDatFlag(0, TRUE);		// 设置新文件标志
			printf("get a new file, saved at network\r\n");
		}
#endif
	}
	else if (save == STORAGE_TRANS)		// 数据转发,不做存储
	{
		if (pPkt->startTrans.fileType == FILE_TYPE_BOARD)		// 外围板文件
		{
			g_fileTransCtrl.transActive = TRANS_NONE;
			g_fileTransCtrl.transflag = TRANS_NOT_BEG;
			g_fileTransCtrl.fileType = pPkt->startTrans.fileType;		// 文件类型,外围板文件
			g_fileTransCtrl.fileIdx = pPkt->startTrans.fileIdx;			// 文件编号
			g_fileTransCtrl.fileId = pPkt->startTrans.fileId;			// 文件ID

			g_fileTransCtrl.pDatFile = NULL;
			g_fileTransCtrl.pAppFile = NULL;
			g_fileTransCtrl.pBoardFile = &g_boardFile;

			memcpy(&(g_fileTransCtrl.pBoardFile->fileHead), pPkt->packet.vldp.exData, pPkt->packet.vldp.exlen);	// 拷贝数据到结构

			g_fileTransCtrl.packetNum = ((g_fileTransCtrl.pBoardFile->fileHead.dataSize+1024-1)/1024);		// 只支持1024字节的包
			g_fileTransCtrl.lenBmp = (g_fileTransCtrl.packetNum+7)/8;
			memset(g_fileTransCtrl.rsvBitmap, 0, g_fileTransCtrl.lenBmp);	// 文件接收进度标志位图

			g_fileTransCtrl.transflag = TRANS_READY;
			g_fileTransCtrl.transActive = TRANS_ACTIVE;

			printf("start exboard update\r\n");

			// 升级文件
			FileHeadFromOperToCan(FILE_TYPE_PGM, g_fileTransCtrl.pBoardFile);
		}
	}
	return 0;
}

// 请求一个数据块
void RequestADataBlock(int datblockIdx)
{
	if (g_fileTransCtrl.pDatFile->storage == STORAGE_NETWORK)
	{
		if (g_fileTransCtrl.transActive == TRANS_ACTIVE &&
				g_fileTransCtrl.transflag == TRANS_REQUEST &&
				g_fileTransCtrl.pDatFile->enflag == DATA_VALID &&
				1	)
		{
			if ( (datblockIdx >= 0) &&
					((datblockIdx * g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk) < g_fileTransCtrl.pDatFile->pFileHead->fileHead.dataSize)	)
			{
				printf("send request cmd, dbidx=%d\r\n", datblockIdx);
				RequestFileData(g_fileTransCtrl.fileType, g_fileTransCtrl.fileIdx, g_fileTransCtrl.fileId, datblockIdx);
			}
			else
			{
				printf("datblockIdx=%d, out of range. bytesPerBlk=%d, dataSize=%d\r\n", datblockIdx, g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk, g_fileTransCtrl.pDatFile->pFileHead->fileHead.dataSize);
			}
		}
		else
		{
			printf("request failed, transActive=0x%x, transflag=%d, enflag=0x%x\r\n", g_fileTransCtrl.transActive, g_fileTransCtrl.transflag, g_fileTransCtrl.pDatFile->enflag);
		}
	}
}

int CreateFileInfo(OperPacket * pPkt)
{
	int rslt;
	int idx;
	int type;

	rslt = 0;
	if (pPkt == NULL)
	{
		return -1;
	}

	idx = pPkt->getFileInfo.fileIdx;
	type = pPkt->getFileInfo.fileType;
	
	memset(pPkt, 0, LEN_NORMAL_PACKET);

	pPkt->sendFileInfo.cmdCode = DCMD_SEND_FILE_INFO;
	pPkt->sendFileInfo.fileType = type;
	pPkt->sendFileInfo.fileIdx = idx;

#if (TRANS_DATA_FILE != 0)
	if (type == FILE_TYPE_DAT)
	{
		rslt = PacketAVLDP(&(pPkt->packet), (u8*)(&g_datFile.pFileHead), sizeof(DataFileHead));
	}
	else
#endif
	if (type == FILE_TYPE_PGM)
	{
		rslt = PacketAVLDP(&(pPkt->packet), (u8*)(&(g_appFile.fileHead)), sizeof(AppFileHead));		
	}

	if (rslt != 0)
	{
		printf("error at call PacketAVLDP in CreateFileInfo\r\n");
	}

	return rslt;
}

int CreateTransResult(OperPacket * pPkt)
{
	int rslt;
	if (pPkt == NULL)
	{
		return -1;
	}

	memset(pPkt, 0, LEN_NORMAL_PACKET);

	if (g_fileTransCtrl.transActive != TRANS_ACTIVE)
	{
		g_fileTransCtrl.lenBmp = 0;
		g_fileTransCtrl.transflag = 0;
	}

	pPkt->sendTransResult.cmdCode = DCMD_SEND_TRANS_RESULT;
	pPkt->sendTransResult.fileId = g_fileTransCtrl.fileId;
	pPkt->sendTransResult.active = g_fileTransCtrl.transActive;
	pPkt->sendTransResult.result = g_fileTransCtrl.transflag;

	rslt = PacketAVLDP(&(pPkt->packet), (u8*)(g_fileTransCtrl.rsvBitmap), g_fileTransCtrl.lenBmp);
	if (rslt != 0)
	{
		printf("error at call PacketAVLDP in CreateTransResult\r\n");
	}

	return rslt;
}

int SavePreProc(void)
{
#if (1)
	{
		return ReleaseSpace(0);
	}
#endif
	return -1;
}

int ReceiveFileData(OperPacket * pPkt)
{
	if (pPkt == NULL)
	{
		return -1;
	}

	if (g_fileTransCtrl.transActive != TRANS_ACTIVE)
	{
		printf("Trans file not active\r\n");
		return -2;
	}

	if (g_fileTransCtrl.transflag != TRANS_READY && 
		g_fileTransCtrl.transflag != TRANS_DOING &&
		g_fileTransCtrl.transflag != TRANS_REQUEST &&
		1	)
	{
		printf("Trans file not ready\r\n");
		return -2;
	}

	if (g_fileTransCtrl.transflag == TRANS_READY)
	{
		g_fileTransCtrl.transflag = TRANS_DOING;
	}

	if (pPkt->transFileData.fileId != g_fileTransCtrl.fileId)
	{
		printf("fileID is not equal in ReceiveFileData, thisid=%d, saveid=%d\r\n", pPkt->transFileData.fileId, g_fileTransCtrl.fileId);
		return -3;
	}

	//printf("get trans data, idx=%d\r\n", pPkt->transFileData.pktIdx);

#if (TRANS_DATA_FILE != 0)
	if (g_fileTransCtrl.fileType == FILE_TYPE_DAT)		// 数据文件
	{
		// 保存数据
		WriteFileData(g_fileTransCtrl.fileIdx, (pPkt->transFileData.pktIdx)*g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk, pPkt->transFileData.exData, pPkt->transFileData.exLen);
		WriteRsvBitmap(pPkt->transFileData.pktIdx, 1, &g_fileTransCtrl);
	}
	else
#endif
		if (g_fileTransCtrl.fileType == FILE_TYPE_PGM)		// 升级文件
	{
		// 保存数据
		int rst;
		rst = SaveAppData(pPkt->transFileData.pktIdx, pPkt->transFileData.exData, pPkt->transFileData.exLen, 0);
		if (rst >= 0)
		{
			WriteRsvBitmap(pPkt->transFileData.pktIdx, 1, &g_fileTransCtrl);
		}
		else
		{
			printf("WriteFileApp error	rst= %d \r\n", rst);
		}
	}
	else if (g_fileTransCtrl.fileType == FILE_TYPE_FRAME)		// 边框刺绣文件
	{
		// 保存数据
		WriteFileData(g_fileTransCtrl.fileIdx, (pPkt->transFileData.pktIdx)*g_fileTransCtrl.pDatFile->pFileHead->fileHead.bytesPerBlk, pPkt->transFileData.exData, pPkt->transFileData.exLen);
		WriteRsvBitmap(pPkt->transFileData.pktIdx, 1, &g_fileTransCtrl);
	}
	else if (g_fileTransCtrl.fileType == FILE_TYPE_BOARD)		// 外围板文件
	{
		// 数据转发,并写入接收位图
		FileDataFromOperToCan(pPkt, g_fileTransCtrl.pBoardFile);
		WriteRsvBitmap(pPkt->transFileData.pktIdx, 1, &g_fileTransCtrl);
	}

	if (g_fileTransCtrl.transflag == TRANS_REQUEST)
	{
		//printf("Receive request block =%d\r\n", pPkt->transFileData.pktIdx);
	}
	else
	{
		// 查看接收位图，是否接收完成
		if (IsResvFinish(&g_fileTransCtrl) == TRUE)
		{
#if (TRANS_DATA_FILE != 0)
			if (g_fileTransCtrl.fileType == FILE_TYPE_DAT)
			{
				OutFileInfo();
				AvalidDatFile(0);
			#if (0)		// 重新计算XY最大最小值
				if (g_datFile.storage == STORAGE_EXSRAM)
				{
					s32 minx, miny, maxx, maxy;

					printf("recalc min max\r\n");
					GetMinMaxFromFileDat(&minx, &maxx, &miny, &maxy);

					g_fileTransCtrl.pDatFile->fileHead.fileHead.minX = minx;
					g_fileTransCtrl.pDatFile->fileHead.fileHead.maxX = maxx;
					g_fileTransCtrl.pDatFile->fileHead.fileHead.minY = miny;
					g_fileTransCtrl.pDatFile->fileHead.fileHead.maxY = maxy;

					OutFileInfo();
				}
				else if (g_datFile.storage == STORAGE_NORFLASH)
				{
				}
				else if (g_datFile.storage == STORAGE_SRAMANDNOR)
				{
				
				}
			#endif
				SetNewDatFlag(0, TRUE);
			}
			else
#endif
				if (g_fileTransCtrl.fileType == FILE_TYPE_PGM)
			{
			
			}
			else if (g_fileTransCtrl.fileType == FILE_TYPE_BOARD)
			{// CAN总线具备ACK应答机制
			}
			else if (g_fileTransCtrl.fileType == FILE_TYPE_FRAME)
			{
				OutFileInfo();
				AvalidDatFile(0);
				SetNewDatFlag(0, TRUE);
			}

			g_fileTransCtrl.transflag = TRANS_DONE;		// 接收完成
			printf("Receive file finish\r\n");
		}
	}

	return 0;
}

// 接收文件参数
int ReceiveFileParas(OperPacket * pPkt)
{
	int rslt;
	if (pPkt == NULL)
	{
		return -1;
	}

	if (pPkt->setFileParas.exLen == sizeof(DataFileHead))
	{
		ReSetFilePara((DataFileHead*)(pPkt->setParas.exData));
		rslt = 0;
	}
	else if (pPkt->setFileParas.exLen == sizeof(DataFilePara))
	{
		ReSetFileParaOld((DataFilePara*)(pPkt->setParas.exData));
		rslt = 0;
	}
	else
	{
		printf("receive len not equ, receivelen=%d, DataFilePara = %d", pPkt->setFileParas.exLen, sizeof(DataFileHead));
		rslt = -2;
	}

	
	return rslt;
}

/*
 * return: 0,正常
 * -1,参数错误
 * -2,数据大小错误
 * -3,扩展数据类型错误
 */
int ReceiveFileExData(OperPacket * pPkt)
{
	int rslt = 0;

	if (pPkt == NULL)
	{
		return -1;
	}

	if (pPkt->setFileExData.exLen != 1024)
	{
		return -2;
	}

	rslt = SetTempData(pPkt->setFileExData.exDataType, pPkt->setFileExData.exDataIdx, &pPkt->setFileExData.exData[0]);

	return rslt;
}

// 外围板数据传输
// 用CAN数据帧的格式发送数据包
void ExboardDataTrans(u32 exid)
{
	SendPacketWithCanData(exid, g_fileTransCtrl.pBoardFile);
}



