using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
namespace DiCEdotNET.Flash.ECU
{
public class ECUProgrammer : ModuleProgrammer
{
private bool flash512;
public ECUProgrammer(DiCEComm dice, byte[] sbl, byte[] binFile, bool flash512, bool p80)
: base(p80)
{
this.dice = dice;
this.sbl = sbl;
this.binFile = binFile;
this.flash512 = flash512;
}
public byte[] readECU(bool readRAM)
{
byte[] numArray = (byte[]) null;
try
{
int num1 = 0;
if (readRAM)
{
numArray = new byte[65536];
num1 = 3145728;
}
else
numArray = !this.flash512 ? new byte[1048576] : new byte[524288];
int num2 = 1;
if (!readRAM)
{
CANPacket canPacket1 = new CANPacket(new byte[8]
{
(byte) 122,
(byte) 188,
(byte) 0,
(byte) 15,
byte.MaxValue,
(byte) 0,
(byte) 0,
(byte) 0
});
byte[] arr1 = new byte[6];
byte[] arr2 = new byte[6]
{
(byte) 79,
(byte) 79,
(byte) 82,
(byte) 69,
(byte) 65,
(byte) 68
};
List<CANPacket> canPacketList;
do
{
uint maxNumMsgs = 1;
uint timeout = 1000;
canPacketList = this.dice.sendMsgReadResponse(canPacket1, CANChannel.HS, ref maxNumMsgs, timeout);
++num2;
if (num2 == 5)
throw new MessageNotReceivedException(canPacket1);
}
while (canPacketList == null);
int num3 = 0;
foreach (CANPacket canPacket2 in canPacketList)
{
for (int index = 6; index < canPacket2.data.Length; ++index)
{
if (num3 < numArray.Length)
arr1[num3++] = canPacket2.data[index];
}
}
if (ECUProgrammer.checkArrayEq(arr1, arr2))
return new byte[4]
{
(byte) 68,
(byte) 69,
(byte) 78,
(byte) 89
};
}
try
{
int num4 = num1;
while (num4 < numArray.Length + num1)
{
CANPacket canPacket3 = new CANPacket(new byte[8]
{
(byte) 122,
(byte) 188,
(byte) 0,
(byte) (num4 >> 16),
(byte) (num4 >>
,
(byte) num4,
(byte) 0,
(byte) 0
});
int num5 = 1;
List<CANPacket> canPacketList;
do
{
uint maxNumMsgs = 1;
uint timeout = 1000;
canPacketList = this.dice.sendMsgReadResponse(canPacket3, CANChannel.HS, ref maxNumMsgs, timeout);
++num5;
if (num5 == 5)
throw new MessageNotReceivedException(canPacket3);
}
while (canPacketList == null);
foreach (CANPacket canPacket4 in canPacketList)
{
for (int index = 6; index < canPacket4.data.Length; ++index)
{
if (num4 - num1 < numArray.Length)
numArray[num4++ - num1] = canPacket4.data[index];
}
}
}
}
catch (MessageNotReceivedException ex)
{
this.sendReset();
Console.WriteLine(ex.ToString());
return (byte[]) null;
}
catch (Exception ex)
{
this.sendReset();
Console.WriteLine(ex.ToString());
return (byte[]) null;
}
}
catch (Exception ex)
{
this.sendReset();
Console.WriteLine(ex.ToString());
}
finally
{
if (readRAM)
{
lock (new object())
this.doneFlashing = true;
}
}
return numArray;
}
public static bool checkArrayEq(byte[] arr1, byte[] arr2)
{
int num = arr1.Length > arr2.Length ? arr2.Length : arr1.Length;
bool flag = true;
for (int index = 0; index < num; ++index)
flag &= (int) arr1[index] == (int) arr2[index];
return flag;
}
public void recodeECU(int unlockPin, int pinCode, long securityKey)
{
uint msgid = 0;
this.dice.startPeriodicMsg(ModuleProgrammer.msgCANTesterPresent, ref msgid, 1000U, CANChannel.HS);
Thread.Sleep(2000);
this.writeEEPROMPin(unlockPin, pinCode, securityKey);
this.dice.stopPeriodicMsg(msgid, CANChannel.HS);
}
public void recodeECU(int unlockPin, long securityKey)
{
int immoPin = 0;
for (int index = 0; index < 24; index += 4)
immoPin += (int) (byte) ((ulong) (securityKey >> index + 16) & 15UL) << 20 - index;
uint msgid = 0;
this.dice.startPeriodicMsg(ModuleProgrammer.msgCANTesterPresent, ref msgid, 1000U, CANChannel.HS);
Thread.Sleep(2000);
this.writeEEPROMPin(unlockPin, immoPin, securityKey);
this.dice.stopPeriodicMsg(msgid, CANChannel.HS);
}
public override bool clearFaultCodes()
{
uint msgid1 = 0;
this.dice.startPeriodicMsg(ModuleProgrammer.msgCANTesterPresent, ref msgid1, 1000U, CANChannel.HS);
uint msgid2 = 0;
if (!this.p80)
this.dice.startPeriodicMsg(ModuleProgrammer.msgCANTesterPresent, ref msgid2, 1000U, CANChannel.MS);
Thread.Sleep(2000);
bool flag = true;
foreach (byte hsModuleAddress in VolvoECUCommands.hsModuleAddresses)
{
CANPacket CANMsg1 = new CANPacket(ModuleProgrammer.msgCANReadFaultCodes);
CANMsg1.setDiagModuleAddress(hsModuleAddress);
this.dice.sendMsg(CANMsg1, CANChannel.HS);
Thread.Sleep(250);
CANPacket CANMsg2 = new CANPacket(ModuleProgrammer.msgCANClearFaultCodes);
flag &= this.dice.sendMsgCheckDiagResponse(CANMsg2, CANChannel.HS, (byte) 239);
}
if (!this.p80)
{
foreach (byte msModuleAddress in VolvoECUCommands.msModuleAddresses)
{
CANPacket CANMsg3 = new CANPacket(ModuleProgrammer.msgCANReadFaultCodes);
CANMsg3.setDiagModuleAddress(msModuleAddress);
this.dice.sendMsg(CANMsg3, CANChannel.MS);
Thread.Sleep(300);
CANPacket CANMsg4 = new CANPacket(ModuleProgrammer.msgCANClearFaultCodes);
flag &= this.dice.sendMsgCheckDiagResponse(CANMsg4, CANChannel.MS, (byte) 239);
}
}
if (!this.p80)
this.dice.stopPeriodicMsg(msgid2, CANChannel.MS);
this.dice.stopPeriodicMsg(msgid1, CANChannel.HS);
return flag;
}
public bool canFlash()
{
uint msgid = 0;
bool flag = false;
this.dice.startPeriodicMsg(ModuleProgrammer.msgCANTesterPresent, ref msgid, 1000U, CANChannel.HS);
Thread.Sleep(2000);
if (this.dice.sendMsgReadResponse(VolvoECUCommands.msgCEMEngineState, CANChannel.HS).data[9] == (byte) 102)
flag = true;
this.dice.stopPeriodicMsg(msgid, CANChannel.HS);
return flag;
}
public int readCANConfigNumber()
{
uint msgid = 0;
uint numMsgs = 4;
this.dice.startPeriodicMsg(ModuleProgrammer.msgCANTesterPresent, ref msgid, 1000U, CANChannel.HS);
Thread.Sleep(2000);
List<CANPacket> canPacketList = this.dice.sendMsgReadResponse(VolvoECUCommands.msgCANReadECMConfig, CANChannel.HS, ref numMsgs);
this.dice.stopPeriodicMsg(msgid, CANChannel.HS);
byte[] numArray1 = new byte[28];
for (int index1 = 0; index1 < canPacketList.Count; ++index1)
{
for (int index2 = 5; index2 < 12; ++index2)
numArray1[index2 - 5 + index1 * 7] = canPacketList[index1].data[index2];
}
byte[] numArray2 = new byte[4];
for (int index = 4; index < 8; ++index)
numArray2[3 - (index - 4)] = numArray1[index];
return BitConverter.ToInt32(numArray2, 0);
}