

var bootstate = 0;
var bootcount = 0;
var sendstate = 0;
FuncWait = function(sid){
	if(RespState == (sid + 0x40)){//正响应
		//bootcount = 0;
		return 0;
	}
	errsid,errnrc = FuncGetNrc();
	FuncClrNrc();
	if(errsid == sid){
		if(errnrc == 0x78){//等待
			bootcount = 0;
			return 0xff; 
		}
		else {
			FuncDisplay("刷写错误 步骤" + bootstate);
			boottimer.disable();
			thread.command.$SendEnd(false);
			return 1; //负响应
		}
		
	}
	if(stopflag == 1){
		FuncDisplay("停止");
		boottimer.disable();
		thread.command.$SendEnd(false);
		return 2;//停止命令
	}
	bootcount += 1;
	/*
	if(bootcount > 20 && sid == 0x36){
		bootcount = 0;
		return 0; 
	}
	*/
	if(bootcount >= 400){
		FuncDisplay("步骤" + bootstate + "超时 " + tostring(sid,16));
		thread.command.$SendEnd(false);
		boottimer.disable();
		return 3;//超时 
	}
}	
	
FuncStopBoot = function(){
	stopflag = 1;
}
FuncClearState = function(){
	RespState = 0;
	bootcount = 0;
	sendstate = 1;
}
FuncInitBootState = function(){
	bootstate = 0;
	RespState = 0;
	bootcount = 0;
	sendstate = 0;
}




BCD_Conv = function(num){
	if(num > 99){
		return 0;
	}
	var ret = 0;
	ret += math.floor(num/10) * 16;
	ret += num%10;
	return ret;
}

var sendblockstate = 0;
var alldata = {};
var blocknum = 1;
var alldata_blocknum = 0;
var block_sended = 0;
var sendFlashDriverState = 0;
var FuncSendBlockApp_sendflag = 0;
var nowid=0;
FuncSendBlockInit = function(id){
	var len = 0;
	alldata = FuncGetAllData(id);
	blocknum = 1;
	if(alldata == null){
		FuncDisplay("错误：空数据");
		alldata_blocknum = 0;
		sendblockstate = 0xff;
		return 1; 
	}
	for(i=1;#alldata;1){
		len += math.ceil(#alldata[i]["data"] / FuncGetSengLen())
	}
	alldata_blocknum = len;
	block_sended = 0;
	sendblockstate = 0;
	FuncSendBlockApp_sendflag = 0;
	nowid = id;
	//console.log("发送数据初始化")
	return 0; 
}
nextstate = function(val){
	if(val == 0){
		bootstate += 1;
		sendstate = 0;
		block_sended += 1;
	}
}

FuncSendBlockApp = function(){
	select(sendblockstate) {
		case 0{
			if(FuncSendBlockApp_sendflag == 0){
				FuncReq34(alldata[blocknum]["address"],#alldata[blocknum]["data"]);
				FuncSendBlockApp_sendflag = 1;
				RespState = 0;
				bootcount = 0;
			}
			else {
				if(FuncWait(0x34) == 0){
					FuncSendBlockApp_sendflag = 0;
					sendblockstate += 1;
				}
			}
			
		}
		case 1 {
			FuncStartSendBlock(alldata[blocknum]["data"]);//初始化
			FuncSendBlockApp_sendflag = 0;
			sendblockstate += 1;
		}
		case 2 {
			if(FuncSendBlockApp_sendflag == 0){
				FuncSendNextBlock();
				FuncSendBlockApp_sendflag = 1;
				block_sended += 1;
				RespState = 0;
				bootcount = 0;
			}
			else {
				if(FuncWait(0x36) == 0){
					FuncSendBlockApp_sendflag = 0;
					if(FuncGetSendStatus() == true){//所有块发送完成
						sendblockstate += 1;
						if(nowid == 1){
							sendblockstate += 1;
						}
					}
				}
			}
		}
		case 3 {
			if(FuncSendBlockApp_sendflag == 0){
				FuncReq37();
				FuncSendBlockApp_sendflag = 1;
				RespState = 0;
				bootcount = 0;
			}
			else {
				if(FuncWait(0x37) == 0){
					FuncSendBlockApp_sendflag = 0;
					sendblockstate += 1;
				}
			}
		}
		else {
			if(blocknum < #alldata){
				blocknum += 1;
				sendblockstate = 0;
			}
			else {
				//所有数据发送完成
				return 0; 
			}
			
		}
	}
	return 1; 
}
FuncSendBlockFlashDriver = function(){
	select(sendFlashDriverState) {
		case 0 {
		}
		case 1 {
		}
		case 2 {
		}
		case 3 {
		}
		else {
		}
	}
}

var delaycount = 0; 
//boot总流程
var boottime = 0;
FuncBootSeq = function(){
	boottime++;
	select(bootstate) {
		case 0 {
			boottime = 1;
			if(sendstate == 0){
				//FuncSendBlockInit(0);
				FuncReadDID(0xF186);
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x22);
				nextstate(0);
			}
		}
		case 1 {
			if(sendstate == 0){
				FuncReadDID(0xF195);//读取软件版本
				FuncClearState();
			}
			else {
				//var ret = FuncWait(0x22);
				nextstate(0);
			}
		}
		case 2 {
			if(sendstate == 0){
				FuncReadDID(0xF193);//读取硬件版本
				FuncClearState();
			}
			else {
				//var ret = FuncWait(0x22);
				nextstate(0);
			}
		}
		case 3 {
			if(sendstate == 0){
				FuncReq10(true,0x03);//进入扩展会话
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x10);
				nextstate(ret);
			}
		}
		case 4 {
			if(sendstate == 0){
				FuncReq85(0x02);//关闭DTC
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x85);
				nextstate(ret);
			}
		}
		case 5 {
			if(sendstate == 0){
				FuncReq28(0x01,0x01);//禁止发送
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x28);
				nextstate(0);
			}
		}
		case 6 {
			if(sendstate == 0){
				FuncReq10(false,0x02);//进入编程会话
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x10);
				nextstate(ret);
			}
		}
		case 7 {
			if(sendstate == 0){//等待复位完成
				delaycount = 0;
				FuncClearState();	
			}
			else {
				delaycount += 1;
				if(delaycount > 20){
					nextstate(0);//延时1S
				}

			}
		}
		case 8 {
			if(sendstate == 0){
				FuncReq27(0x09);//解密
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x27);
				nextstate(ret);
			}
		}
		case 9 {
			if(sendstate == 0){
				//FuncReq27(02);//发送key
				FuncClearState();
				FuncDisplay("等待解锁");
			}
			else {
				//var ret = FuncWait(0x27);
				var ret = FuncDiag27GetState();
				nextstate(ret);
			}
		}
		case 10 {
			if(sendstate == 0){
				FuncReq31EraseFlash(0x1FFFF000,0x1000);
				FuncClearState();
				
			}
			else {
				var ret = FuncWait(0x31);
				nextstate(ret);
			}
		}
		
		case 11 {
			if(sendstate == 0){//写入installation_date
				FuncReq3103(0xFD01);
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x31);
				nextstate(ret);
			}
		}
		case 12 {
			if(sendstate == 0){//发送数据
				FuncSendBlockInit(1);
				FuncDisplay("开始发送FlashDrv数据");
				FuncClearState();
			}
			else {
				var ret = FuncSendBlockApp();
				nextstate(ret);
			}
		}
		case 13 {
			if(sendstate == 0){//CheckLogicBlock
				//var crc = FuncGetFlashCRC(0); 
				//FuncReq31CheckLogicBlock(crc);
				FuncReq3101FD02();
				FuncClearState();	
			}
			else {
				var ret = FuncWait(0x31);
				nextstate(ret);
			}
		}
		case 14 {
			if(sendstate == 0){//
				FuncReq3103(0xFD02);
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x31);
				nextstate(ret);
			}
		}
		case 15 {
			if(sendstate == 0){//CheckLogicBlock
				FuncReq31EraseFlash(0x00010000,0x70000);
				FuncClearState();	
			}
			else {
				var ret = FuncWait(0x31);
				nextstate(ret);
			}
		}
		
		case 16 {
			if(sendstate == 0){//发送数据
				FuncSendBlockInit(2);
				FuncDisplay("开始发送App数据");
				FuncClearState();
			}
			else {
				var ret = FuncSendBlockApp();
				nextstate(ret);
			}
		}
		case 17 {
			if(sendstate == 0){//CheckLogicBlock
				FuncReq3101FD03();
				FuncClearState();	
			}
			else {
				var ret = FuncWait(0x31);
				nextstate(ret);
			}
		}
		case 18 {
			if(sendstate == 0){////Programming Dependencies
				FuncReq3103(0xFD03);
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x31);
				nextstate(ret);
			}
		}
		case 19 {
			if(sendstate == 0){//复位
				FuncClearState();
				FuncReq11(0x03);
			}
			else {
				var ret = FuncWait(0x11);
				nextstate(ret);
			}
		}
		
		case 20 {
			if(sendstate == 0){//等待复位完成
				delaycount = 0;
				FuncClearState();	
			}
			else {
				delaycount += 1;
				if(delaycount > 100){
					nextstate(0);//延时1S
				}

			}
		}
		
		case 21 {
			if(sendstate == 0){
				FuncReq10(true,0x03);//进入扩展会话
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x10);
				nextstate(ret);
			}
		}
		case 22 {
			if(sendstate == 0){
				FuncReq28(0x00,0x01);//开启发送
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x28);
				nextstate(ret);
			}
		}
		case 23 {
			if(sendstate == 0){
				FuncReq85(0x01);//开启DTC
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x85);
				nextstate(ret);
			}
		}
		case 24 {
			if(sendstate == 0){
				FuncReq10(true,0x01);//进入默认会话
				FuncClearState();
			}
			else {
				var ret = FuncWait(0x10);
				nextstate(ret);
			}
		}
		else {
			thread.command.$SendEnd(true);
			//FuncDisplay("刷写完成,用时 " + boottime/100 + " S")
			boottimer.disable();
		}
	}
	if(alldata_blocknum != 0){
		thread.command.$SetProgress(block_sended/alldata_blocknum * 100);
	}
	
}