Skip to content

Commit

Permalink
DTC, Metadata, ID, debug simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
jglim committed Jan 26, 2021
1 parent 3857c4a commit 87dc8a1
Show file tree
Hide file tree
Showing 41 changed files with 3,157 additions and 279 deletions.
1 change: 1 addition & 0 deletions Caesar/Caesar/Caesar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Compile Include="BitUtility.cs" />
<Compile Include="CaesarContainer.cs" />
<Compile Include="DiagPresentation.cs" />
<Compile Include="DTC.cs" />
<Compile Include="Flash\CaesarFlashContainer.cs" />
<Compile Include="CaesarReader.cs" />
<Compile Include="CaesarStructure.cs" />
Expand Down
9 changes: 2 additions & 7 deletions Caesar/Caesar/CaesarContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class CaesarContainer

public uint FileChecksum;

// fixup serialization/deserialization:
// language strings should be properties; resolve to actual string only when called
public CaesarContainer(byte[] fileBytes)
{
FileBytes = fileBytes;
Expand All @@ -36,7 +38,6 @@ public CaesarContainer(byte[] fileBytes)
ReadCTF(reader);
ReadECU(reader);
}

}

public static bool VerifyChecksum(byte[] fileBytes, out uint checksum)
Expand Down Expand Up @@ -64,7 +65,6 @@ public static uint ReadFileChecksum(byte[] fileBytes)
return BitConverter.ToUInt32(fileBytes, fileBytes.Length - 4);
}


public ECUVariant GetECUVariantByName(string name)
{
foreach (ECU ecu in CaesarECUs)
Expand Down Expand Up @@ -133,7 +133,6 @@ void ReadECU(BinaryReader fileReader)
int offsetToActualEcuEntry = fileReader.ReadInt32();
CaesarECUs.Add(new ECU(fileReader, GetLanguage(), CaesarCFFHeader, ecuTableOffset + offsetToActualEcuEntry, this));
}

}

void ReadCTF(BinaryReader fileReader)
Expand All @@ -144,16 +143,13 @@ void ReadCTF(BinaryReader fileReader)
{
throw new NotImplementedException("No idea how to handle nonexistent ctf header");
}
// Console.WriteLine($"ctf header relative to definitions: {nameof(CaesarCFFHeader.nCtfHeaderRpos)} : 0x{CaesarCFFHeader.nCtfHeaderRpos:X}");

long ctfOffset = CaesarCFFHeader.BaseAddress + CaesarCFFHeader.CtfOffset;
CaesarCTFHeader = new CTFHeader(fileReader, ctfOffset, CaesarCFFHeader.CffHeaderSize);
}


void ReadCFFDefinition(BinaryReader fileReader)
{

CaesarCFFHeader = new CFFHeader(fileReader);
// CaesarCFFHeader.PrintDebug();

Expand All @@ -178,7 +174,6 @@ void ReadCFFDefinition(BinaryReader fileReader)
Console.WriteLine($"{nameof(formOffsetTableSize)} : 0x{formOffsetTableSize:X}\n\n");
}
*/

}

public string GetFileSize()
Expand Down
2 changes: 2 additions & 0 deletions Caesar/Caesar/CaesarStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public static void FillCaesarTypes()
CaesarTypes.Add(new byte[] { 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 0, 0, 0 }); // UNK36
}

// this is an artifact from implementing the reverse-engineered code as-is; since Caesar loads all objects in a greedy strategy (for serialization),
// use of ReadCBFWithOffset should be discontinued
public static int ReadCBFWithOffset(int memberIndex, StructureName structureName, byte[] input)
{
int byteOffset = CaesarStructure.GetCBFOffset(memberIndex, structureName, input);
Expand Down
84 changes: 84 additions & 0 deletions Caesar/Caesar/DTC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Caesar
{
public class DTC
{
public enum DTCStatusByte : uint
{
TestFailedAtRequestTime = 0x01,
TestFailedAtCurrentCycle = 0x02,
PendingDTC = 0x04,
ConfirmedDTC = 0x08,
TestIncompleteSinceLastClear = 0x10,
TestFailedSinceLastClear = 0x20,
TestIncompleteAtCurrentCycle = 0x40,
WarningIndicatorActive = 0x80,
}

// see : const char *__cdecl DIGetComfortErrorCode(DI_ECUINFO *ecuh, unsigned int dtcIndex)
public string Qualifier;

public int Description_CTF;
public int Reference_CTF;

public int XrefStart = -1;
public int XrefCount = -1;

public long BaseAddress;
public int PoolIndex;

public ECU ParentECU;
CTFLanguage Language;

public string Description { get { return Language.GetString(Description_CTF); } }

public DTC(BinaryReader reader, CTFLanguage language, long baseAddress, int poolIndex, ECU parentEcu)
{
ParentECU = parentEcu;
PoolIndex = poolIndex;
BaseAddress = baseAddress;
Language = language;
reader.BaseStream.Seek(baseAddress, SeekOrigin.Begin);

ulong bitflags = reader.ReadUInt16();

Qualifier = CaesarReader.ReadBitflagStringWithReader(ref bitflags, reader, baseAddress);

Description_CTF = CaesarReader.ReadBitflagInt32(ref bitflags, reader, -1);
Reference_CTF = CaesarReader.ReadBitflagInt32(ref bitflags, reader, -1);
#if DEBUG
if (bitflags > 0)
{
Console.WriteLine($"DTC {Qualifier} has additional unparsed fields : 0x{bitflags:X}");
}
#endif
}
/*
public string GetDescription()
{
return Language.GetString(Description_CTF);
}
*/
public static DTC FindDTCById(string id, ECUVariant variant)
{
foreach (DTC dtc in variant.DTCs)
{
if (dtc.Qualifier.EndsWith(id))
{
return dtc;
}
}
return null;
}
public void PrintDebug()
{
Console.WriteLine($"DTC: {Qualifier}: {Language.GetString(Description_CTF)} : {Language.GetString(Reference_CTF)}");
}
}
}
48 changes: 16 additions & 32 deletions Caesar/Caesar/DiagPreparation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ public int GetSizeInBits(BinaryReader reader, bool verbose = true)
byte[] poolBytes = ParentECU.ReadECUInfoPool(reader);
using (BinaryReader poolReader = new BinaryReader(new MemoryStream(poolBytes)))
{
DiagPresentation pres = ParentECU.GlobalInternalPresentations[InfoPoolIndex];
/*
// depreciate use of ReadCBFWithOffset
poolReader.BaseStream.Seek(ParentECU.Info_EntrySize * InfoPoolIndex, SeekOrigin.Begin);
int presentationStructOffset = poolReader.ReadInt32();
Expand All @@ -163,8 +166,11 @@ public int GetSizeInBits(BinaryReader reader, bool verbose = true)
{
resultBitSize = CaesarStructure.ReadCBFWithOffset(0x21, CaesarStructure.StructureName.PRESENTATION_STRUCTURE, presentationStruct); // ???
}
*/
resultBitSize = pres.TypeLength_1a > 0 ? pres.TypeLength_1a : pres.TypeLengthBytesMaybe_21;

// if value was specified in bytes, convert to bits
if (presentationMode == 0)
if (pres.Type_1c == 0)
{
resultBitSize *= 8;
}
Expand All @@ -177,15 +183,19 @@ public int GetSizeInBits(BinaryReader reader, bool verbose = true)

using (BinaryReader poolReader = new BinaryReader(new MemoryStream(presPool)))
{
DiagPresentation pres = ParentECU.GlobalPresentations[PresPoolIndex];
/*
// depreciate use of ReadCBFWithOffset
poolReader.BaseStream.Seek(ParentECU.Presentations_EntrySize * PresPoolIndex, SeekOrigin.Begin);
int presentationStructOffset = poolReader.ReadInt32();
int presentationStructSize = poolReader.ReadInt32();
reader.BaseStream.Seek(presentationStructOffset + ParentECU.Presentations_BlockOffset, SeekOrigin.Begin);
byte[] presentationStruct = reader.ReadBytes(presentationStructSize);

int presentationMode = CaesarStructure.ReadCBFWithOffset(0x1C, CaesarStructure.StructureName.PRESENTATION_STRUCTURE, presentationStruct); // PRESS_Type
int presentationLength = CaesarStructure.ReadCBFWithOffset(0x1A, CaesarStructure.StructureName.PRESENTATION_STRUCTURE, presentationStruct); // PRESS_TypeLength
if (presentationLength > 0)
{
resultBitSize = presentationLength;
Expand All @@ -194,41 +204,15 @@ public int GetSizeInBits(BinaryReader reader, bool verbose = true)
{
resultBitSize = CaesarStructure.ReadCBFWithOffset(0x21, CaesarStructure.StructureName.PRESENTATION_STRUCTURE, presentationStruct); // ???
}
// if value was specified in bytes, convert to bits
if (presentationMode == 0)
{
resultBitSize *= 8;
}

/*
// no idea how negative requests are registered; their params seem empty?
// usually 7F XX XX
if (qualifier == "PRES_NR_Request1")
{
Console.WriteLine($"NR: {qualifier}");
for (int i = 1; i < 39; i++)
{
int readout = CaesarStructure.ReadCBFWithOffset(i, CaesarStructure.StructureName.PRESENTATION_STRUCTURE, presentationStruct);
Console.Write($"{i:X}: {readout:X}, ");
}
Console.WriteLine("\n------------");
}
*/

resultBitSize = pres.TypeLength_1a > 0 ? pres.TypeLength_1a : pres.TypeLengthBytesMaybe_21;

/*
// dbg: identifying struct members
if (qualifier.ToLower().Contains("byte"))
// if value was specified in bytes, convert to bits
if (pres.Type_1c == 0)
{
Console.WriteLine($"{qualifier}");
for (int i = 1; i < 0x1D; i++)
{
int readout = CaesarStructure.ReadCBFWithOffset(i, CaesarStructure.StructureName.PRESENTATION_STRUCTURE, presentationStruct);
Console.Write($"{i:X}: {readout}, ");
}
Console.WriteLine("\n------------");
resultBitSize *= 8;
}
*/
}
}
else
Expand Down
Loading

2 comments on commit 87dc8a1

@Feezex
Copy link

@Feezex Feezex commented on 87dc8a1 Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NRC aus UDS DC_10747.pdf
may be helpful for negative responses

@Feezex
Copy link

@Feezex Feezex commented on 87dc8a1 Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.