
var SecuretySeed = {0,0,0,0,};
var SecuretyKey = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
var lockstate = 0;
//发送	
FuncReq27 = function(num,key){
	if(num >= 1 && num <= 0x1C){
		if(key == null){
			var data = {0x02,0x27,num,0,0,0,0,0};
			CANHw.SendMsg(DiagReqID,data);
		}
		else {
			if(#key > 4){
				var data = {0x10,0X12,0x27,num,key[1],key[2],key[3],key[4]};
				CANHw.SendMsg(DiagReqID,data);
				
				FuncPushPackage(DiagReqID,table.slice(key,5));
			}
			else {
				var data = {0x06,0x27,num,key[1],key[2],key[3],key[4],0x00};
				CANHw.SendMsg(DiagReqID,data);
			}
			
		}	
	}
	else {
		FuncDisplay("27长度错误");
	}
	
}

//loadcodex("\user\securety.aardio");
loadcodex("\user\GeelySecurety.aardio");
//接收
FuncDiag27Pro = function(data){
	//console.dumpJson(data);
	select(data[1]) {
		case 1,3,5,7,9,11,0x11 {
			var str = "收到种子"; 
			for(i=1;4;1){
				SecuretySeed[i] = data[i+1];
				str += " " + tostring(SecuretySeed[i],16);
				lockstate = 1;
			}
			FuncDisplay(str)
			SecuretyKey = securetyKeyCalc(SecuretySeed,data[1]);
			if(#SecuretyKey >= 4){
				FuncReq27(data[1]+1,SecuretyKey);
				var str = "计算密钥-"; 
				for(i=1;4;1){
					str += " " + tostring(SecuretyKey[i],16);
				}
				FuncDisplay(str);
			}
			else {
				FuncDisplay("计算失败");
			}
			
		}
		case 2,4,6,8,10,12,0x12 {
			FuncDisplay("解锁成功");
			lockstate = 2;
		}
		else {
			FuncDisplay("27服务未知子服务");
		}
	}
}

FuncDiag27GetState = function(){
	if(lockstate == 2){
		return 0;
	} 
	else {
		return 1; 
	}
	
}
