public void LoadSegmentList(ref PcmFile PCM1) { PCM = PCM1; PCMsegmentdata = new SegmentData(PCM); labelBasefile.Text = Path.GetFileName(PCM.FileName); comboSegments.Items.Clear(); //for (int s=0;s<PCM.segmentinfos.Length;s++) comboSegments.ValueMember = "SegNr"; comboSegments.DisplayMember = "Name"; comboSegments.DataSource = PCM.segmentinfos; setuplistview(); LoadSegments(); }
private void btnSegments_Click(object sender, EventArgs e) { if (frmSL != null && frmSL.Visible) { frmSL.BringToFront(); return; } frmSL = new frmSegmenList(); frmSL.Show(this); PcmFile PCM = new PcmFile(); frmSL.InitMe(); }
private void compareAllTables(List <string> files) { dataGridViewTableList.SelectionChanged -= DataGridViewTableList_SelectionChanged; List <PcmFile> pcmfiles = new List <PcmFile>(); //Load all files: for (int i = 0; i < files.Count; i++) { string fName = files[i]; LoggerBold(fName); PcmFile cmpPCM = new PcmFile(fName, true, ""); loadConfigforPCM(cmpPCM); pcmfiles.Add(cmpPCM); } Logger("Reading tables ", false); for (int i = 0; i < PCM.tableDatas.Count; i++) { td = PCM.tableDatas[i]; int row = dataGridViewTableList.Rows.Add(); dataGridViewTableList.Rows[row].Cells[0].Value = td.TableName; dataGridViewTableList.Rows[row].Cells[1].Value = td.Category; dataGridView1.Rows.Clear(); for (int p = 0; p < pcmfiles.Count; p++) { PcmFile cmpPCM = pcmfiles[p]; int id = findTableDataId(td, cmpPCM.tableDatas); if (id < 0) { continue; } compareTable(cmpPCM); dataGridViewTableList.Rows[row].Cells[2].Value = Convert.ToInt32(dataGridViewTableList.Rows[row].Cells[2].Value) + 1; } DgvRow dgvRow = new DgvRow(); for (int r = 0; r < dataGridView1.Rows.Count; r++) { dgvRow.id = r; dgvRow.gdvrows.Add(dataGridView1.Rows[r]); } tableDgvRows.Add(dgvRow); dataGridViewTableList.Rows[row].Tag = tableDgvRows.Count - 1; if ((i % 10) == 0) { Logger(".", false); } } dataGridViewTableList.SelectionChanged += DataGridViewTableList_SelectionChanged; }
public static uint searchWord(PcmFile PCM, ushort sWord, uint Start, uint End, ushort stopVal = 0) { for (uint addr = Start; addr < End; addr++) { if (stopVal != 0 && BEToUint16(PCM.buf, addr) == stopVal) { return(uint.MaxValue); } if (BEToUint16(PCM.buf, addr) == sWord) { return(addr); } } return(uint.MaxValue); }
private void importTableSeek(PcmFile cmpPCM) { if (cmpPCM.foundTables.Count == 0) { TableSeek TS = new TableSeek(); Logger("Seeking tables...", false); Logger(TS.seekTables(cmpPCM)); } Logger("Importing TableSeek tables... ", false); for (int i = 0; i < cmpPCM.foundTables.Count; i++) { TableData tableData = new TableData(); tableData.importFoundTable(i, cmpPCM); cmpPCM.tableDatas.Add(tableData); } Logger("OK"); }
public void selectCmpFiles() { if (!compareAll) { splitContainer1.Panel1Collapsed = true; splitContainer1.Panel1.Hide(); } frmFileSelection frmF = new frmFileSelection(); frmF.btnOK.Text = "Compare files"; frmF.Text = "Search and Compare: " + td.TableName; frmF.LoadFiles(UniversalPatcher.Properties.Settings.Default.LastBINfolder); if (frmF.ShowDialog(this) == DialogResult.OK) { if (compareAll) { List <string> files = new List <string>(); for (int i = 0; i < frmF.listFiles.CheckedItems.Count; i++) { string FileName = frmF.listFiles.CheckedItems[i].Tag.ToString(); files.Add(FileName); } initCompareAll(); compareAllTables(files); } else { for (int i = 0; i < frmF.listFiles.CheckedItems.Count; i++) { string FileName = frmF.listFiles.CheckedItems[i].Tag.ToString(); PcmFile cmpPcm = new PcmFile(FileName, true, ""); LoggerBold(FileName); loadConfigforPCM(cmpPcm); compareTable(cmpPcm); } } } dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells); Logger("Done"); }
public PidSearch(PcmFile PCM1) { PCM = PCM1; uint step = 8; loadPidList(); if (PCM.configFile.StartsWith("diesel01")) { startAddress = searchBytes(PCM, "00 00 04 * * * * * * * 00 01 04 * * * * * * * 00 02 02", 0, PCM.fsize - 23); step = 10; if (startAddress < uint.MaxValue) { searchPids(step, true); } return; } if (PCM.configFile.StartsWith("p01")) { step = 8; startAddress = searchBytes(PCM, "00 01 02 00 * * * * 00 03 01 00 * * * * 00 04 00 00 * * * * 00 05 00 00", 0, PCM.fsize - 28); if (startAddress < uint.MaxValue) { searchPids(step, false); } return; } if (PCM.configFile.StartsWith("v6")) { startAddress = searchBytes(PCM, "00 00 02 00 * * * * * * 00 01 02 00", 0, PCM.fsize - 14); step = 10; if (startAddress < uint.MaxValue) { searchPids(step, false); } return; } throw new Exception("PID search not implemented for this file type"); }
private void btnTuner_Click(object sender, EventArgs e) { if (countSelections() == 0) { Logger("No files selected"); return; } frmTuner frmT = new frmTuner(null); frmT.Show(); for (int i = 0; i < pcmList.Count; i++) { if (pcmList[i].Selected) { PcmFile newPCM = pcmList[i].pcmFile; frmT.addtoCurrentFileMenu(newPCM); frmT.PCM = newPCM; frmT.loadConfigforPCM(ref newPCM); } } frmT.selectPCM(); }
private void copyTableData(TableData srcTd, TableData dstTd, ref PcmFile dstPCM) { frmTableEditor srcTE = new frmTableEditor(); srcTE.prepareTable(PCM, srcTd, null, "A"); //srcTE.loadTable(); frmTableEditor dstTE = new frmTableEditor(); dstTE.prepareTable(dstPCM, dstTd, null, "A"); //dstTE.loadTable(); for (int cell = 0; cell < srcTE.compareFiles[0].tableInfos[0].tableCells.Count; cell++) { TableCell srcTc = srcTE.compareFiles[0].tableInfos[0].tableCells[cell]; TableCell dstTc = dstTE.compareFiles[0].tableInfos[0].tableCells[cell]; dstTc.saveValue((double)srcTc.lastValue); } dstTE.saveTable(); srcTE.Dispose(); dstTE.Dispose(); }
public SegmentData(PcmFile pcm) { int segmentCount = pcm.segmentinfos.Length; name = new string[segmentCount]; size = new string[segmentCount]; range = new string[segmentCount]; for (int s = 0; s < pcm.segmentinfos.Length; s++) { name[s] = pcm.segmentinfos[s].Name; if (pcm.segmentinfos[s].SwapAddress.Length > 1) { size[s] = pcm.segmentinfos[s].SwapSize; range[s] = pcm.segmentinfos[s].SwapAddress; } else { size[s] = pcm.segmentinfos[s].Size; range[s] = pcm.segmentinfos[s].Address; } } }
public void InitMe(PcmFile PCM1, int SegmentNr) { PCM = PCM1; CurrentSegment = SegmentNr; listCheckwords.Clear(); listCheckwords.View = View.Details; listCheckwords.Columns.Add("Checkword"); listCheckwords.Columns.Add("Location"); listCheckwords.Columns.Add("Data Location"); listCheckwords.Columns.Add("Key"); listCheckwords.Columns[0].Width = 100; listCheckwords.Columns[1].Width = 100; listCheckwords.Columns[2].Width = 130; listCheckwords.Columns[3].Width = 100; listCheckwords.MultiSelect = false; listCheckwords.CheckBoxes = false; listCheckwords.FullRowSelect = true; SegmentConfig S = PCM.Segments[SegmentNr]; if (S.CheckWords == null) { return; } string[] Rows = S.CheckWords.Split(','); foreach (string Row in Rows) { string[] Parts = Row.Split(':'); if (Parts.Length == 4) { var item = new ListViewItem(Parts[0]); item.SubItems.Add(Parts[1]); item.SubItems.Add(Parts[2]); item.SubItems.Add(Parts[3]); listCheckwords.Items.Add(item); } } }
public static uint searchBytes(PcmFile PCM, string searchString, uint Start, uint End, ushort stopVal = 0) { uint addr; string[] searchParts = searchString.Split(' '); for (addr = Start; addr < End; addr++) { bool match = true; if (stopVal != 0 && BEToUint16(PCM.buf, addr) == stopVal) { return(uint.MaxValue); } if ((addr + searchParts.Length) > PCM.fsize) { return(uint.MaxValue); } for (uint part = 0; part < searchParts.Length; part++) { if (searchParts[part] != "*") { byte searchval; HexToByte(searchParts[part], out searchval); if (PCM.buf[addr + part] != searchval) { match = false; break; } } } if (match) { return(addr); } } return(uint.MaxValue); }
private void btnExtract_Click(object sender, EventArgs e) { /*if (comboSegments.Text == "OS") * { * Logger("OS swap disabled"); * return; * }*/ string FileName = SelectFile(); if (FileName.Length == 0) { return; } try { int Seg = comboSegments.SelectedIndex; uint TotalLength = 0; if (PCM.segmentinfos[Seg].SwapAddress.Length > 1) { if (!HexToUint(PCM.segmentinfos[Seg].SwapSize, out TotalLength)) { throw new Exception("Cant't decode HEX: " + PCM.segmentinfos[Seg].Size); } } else { if (!HexToUint(PCM.segmentinfos[Seg].Size, out TotalLength)) { throw new Exception("Cant't decode HEX: " + PCM.segmentinfos[Seg].Size); } } Logger("Reading segment from file: " + FileName, false); uint fsize = (uint)new FileInfo(FileName).Length; if (fsize == TotalLength) { Logger(" (0x" + fsize.ToString("X") + " B)"); SwapBuffer = ReadBin(FileName, 0, fsize); labelSelectedSegment.Text = "Selected: " + Path.GetFileName(FileName); labelSelectedSegment.Tag = FileName; Logger("[OK]"); Logger("Press \"Apply\" to swap"); Applied = false; } else if (fsize == PCM.fsize) { PcmFile tmpPCM = new PcmFile(FileName, true, PCM.configFileFullName); //tmpPCM.GetSegmentAddresses(); //tmpPCM.GetInfo(); /*if (tmpPCM.OS != PCM.OS) * { * throw new Exception(Environment.NewLine + "OS mismatch: " + PCM.OS + " <> " + tmpPCM.OS); * }*/ SwapBuffer = new byte[TotalLength]; labelSelectedSegment.Text = "Selected: " + tmpPCM.segmentinfos[Seg].PN + tmpPCM.segmentinfos[Seg].Ver + " (From file: " + Path.GetFileName(FileName) + ")"; labelSelectedSegment.Tag = FileName; uint Offset = 0; if (PCM.segmentinfos[Seg].SwapAddress.Length == 0) { for (int s = 0; s < PCM.segmentAddressDatas[Seg].SegmentBlocks.Count; s++) { uint Start = PCM.segmentAddressDatas[Seg].SegmentBlocks[s].Start; uint Length = PCM.segmentAddressDatas[Seg].SegmentBlocks[s].End - PCM.segmentAddressDatas[Seg].SegmentBlocks[s].Start + 1; Array.Copy(tmpPCM.buf, Start, SwapBuffer, Offset, Length); Offset += Length; } } else { for (int s = 0; s < PCM.segmentAddressDatas[Seg].SwapBlocks.Count; s++) { uint Start = PCM.segmentAddressDatas[Seg].SwapBlocks[s].Start; uint Length = PCM.segmentAddressDatas[Seg].SwapBlocks[s].End - PCM.segmentAddressDatas[Seg].SwapBlocks[s].Start + 1; Array.Copy(tmpPCM.buf, Start, SwapBuffer, Offset, Length); Offset += Length; } } Logger(" (0x" + TotalLength.ToString("X") + " B)", false); Logger("[OK]"); Logger("Press \"Apply\" to swap"); Applied = false; } else { throw new Exception("Unknown file size"); } } catch (Exception ex) { Logger(ex.Message); } }
public string readTinyDBtoTableData(PcmFile PCM, List <TableData> tdList) { string connetionString = null; OleDbConnection cnn; string dbFile = Path.Combine(Application.StartupPath, "TinyTuner.mdb"); connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile; cnn = new OleDbConnection(connetionString); try { string query = "Select MapNumber from OSIDData where OSID=" + PCM.OS; OleDbCommand selectCommand = new OleDbCommand(query, cnn); cnn.Open(); DataTable table = new DataTable(); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = selectCommand; adapter.Fill(table); string mapNr = ""; foreach (DataRow row in table.Rows) { mapNr = row["MapNumber"].ToString(); } if (mapNr == "") { LoggerBold("OS not found from TinyTuner DB"); return("Not found"); } query = "select * from CategoryList order by Category"; selectCommand = new OleDbCommand(query, cnn); table = new DataTable(); adapter = new OleDbDataAdapter(); adapter.SelectCommand = selectCommand; adapter.Fill(table); if (PCM.tableCategories == null) { PCM.tableCategories = new List <string>(); } foreach (DataRow row in table.Rows) { string cat = row["Category"].ToString(); if (!PCM.tableCategories.Contains(cat)) { PCM.tableCategories.Add(cat); } } query = "select * from TableData where MapNumber=" + mapNr + " order by TableName"; selectCommand = new OleDbCommand(query, cnn); table = new DataTable(); adapter = new OleDbDataAdapter(); adapter.SelectCommand = selectCommand; adapter.Fill(table); if (tableSeeks == null) { tableSeeks = new List <TableSeek>(); } if (PCM.foundTables == null) { PCM.foundTables = new List <FoundTable>(); } foreach (DataRow row in table.Rows) { TableData td = new TableData(); td.OS = PCM.OS; td.Origin = "TinyTuner"; HexToUint(row["StartPosition"].ToString(), out td.addrInt); //td.Address = td.AddrInt.ToString("X8"); td.Columns = Convert.ToUInt16(row["ColumnCount"]); td.TableDescription = row["TableDescription"].ToString(); td.TableName = row["TableName"].ToString(); td.Rows = Convert.ToUInt16(row["RowCount"]); td.Category = row["MainCategory"].ToString(); int elementSize = (byte)(Convert.ToByte(row["ElementSize"])); bool Signed = Convert.ToBoolean(row["AllowNegative"]); td.DataType = convertToDataType(elementSize, Signed, false); string colHeaders = row["ColumnHeaders"].ToString(); td.ColumnHeaders = RemoveDuplicates(colHeaders); //td.Floating = true; td.Decimals = 2; if (row["Factor"].ToString().Length > 0) { td.Math = "X*" + row["Factor"].ToString(); } else { td.Math = "X*1"; } td.RowHeaders = row["RowHeaders"].ToString(); if (td.RowHeaders.Contains(",by,")) { td.RowHeaders = convertByHeader(td.RowHeaders, td.Rows); } if (row["Factor"].ToString().Length > 0) { td.SavingMath = "X/" + row["Factor"].ToString(); } else { td.SavingMath = "X/1"; } td.Category = row["MainCategory"].ToString(); if (!PCM.tableCategories.Contains(td.Category)) { PCM.tableCategories.Add(td.Category); } td.Units = row["Units"].ToString(); td.RowMajor = false; tdList.Add(td); } cnn.Close(); } catch (Exception ex) { var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(st.FrameCount - 1); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); LoggerBold("Error, line " + line + ": " + ex.Message); } return("OK"); }
public string seekTables(PcmFile PCM1) { PCM = PCM1; string retVal = ""; try { PCM.foundTables = new List <FoundTable>(); PCM.tableCategories = new List <string>(); PCM.tableCategories.Add("_All"); if (PCM.segmentinfos == null) { return(""); } for (int c = 0; c < PCM.segmentinfos.Length; c++) { PCM.tableCategories.Add("Seg-" + PCM.segmentinfos[c].Name); } string fileName = Path.Combine(Application.StartupPath, "XML", "TableSeek-" + PCM.configFile + ".xml"); if (fileName != tableSeekFile) { tableSeekFile = fileName; if (File.Exists(fileName)) { Debug.WriteLine("Loading " + fileName); System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(List <TableSeek>)); System.IO.StreamReader file = new System.IO.StreamReader(fileName); tableSeeks = (List <TableSeek>)reader.Deserialize(file); file.Close(); } else { if (PCM.Segments[0].CS1Address.StartsWith("GM-V6")) { if (!PCM.tableCategories.Contains("Fuel")) { PCM.tableCategories.Add("Fuel"); } tableSeeks = new List <TableSeek>(); TableSeek ts = new TableSeek(); FoundTable ft = new FoundTable(PCM); ft.Address = PCM.v6VeTable.address.ToString("X8"); ft.addrInt = PCM.v6VeTable.address; ft.Rows = (byte)PCM.v6VeTable.rows; ft.Columns = 17; ft.configId = 0; ft.Name = "VE"; ft.Category = "Fuel"; ft.Description = "Volumetric Efficiency"; PCM.foundTables.Add(ft); ts.Name = "VE"; ts.Description = "Volumetric Efficiency"; ts.DataType = InDataType.UWORD; //ts.Bits = 16; //ts.Floating = true; ts.OutputType = OutDataType.Float; ts.Decimals = 6; ts.Math = "X*0.0002441406"; ts.Offset = 0; ts.SavingMath = "X/0.0002441406"; //ts.Signed = false; ts.Category = "Fuel"; ts.ColHeaders = "RPM 0,400,800,1200,1600,2000,2400,2800,3200,3600,4000,4400,4800,5200,5600,6000,6400, 6800"; if (ft.Rows == 15) { ts.RowHeaders = "kpa 0,10,20,30,40,50,60,70,80,90,100,110,120,130,140"; } else { ts.RowHeaders = "kpa 20,30,40,50,60,70,80,90,100,110,120,130,140"; } ts.RowMajor = false; tableSeeks.Add(ts); ft = new FoundTable(PCM); HexToUint(PCM.mafAddress, out ft.addrInt); ft.Address = ft.addrInt.ToString("X8"); ft.Rows = 81; ft.Columns = 1; ft.configId = 1; ft.Name = "MAF"; ft.Category = "Fuel"; ft.Description = "Grams Per Second"; PCM.foundTables.Add(ft); ts = new TableSeek(); ts.DataType = InDataType.UWORD; //ts.Bits = 16; ts.Name = "MAF"; ts.Math = "X*0.0078125"; ts.SavingMath = "X/0.0078125"; //ts.Floating = true; ts.OutputType = OutDataType.Float; ts.Decimals = 4; //ts.Signed = false; ts.Category = "Fuel"; ts.Units = "grams/s"; ts.RowHeaders = "1500,"; for (int rh = 1; rh < 82; rh++) { ts.RowHeaders += (1500 + rh * 125).ToString(); if (rh < 81) { ts.RowHeaders += ","; } } ts.ColHeaders = "g/s"; ts.Description = "Grams Per Second"; ts.RowMajor = false; tableSeeks.Add(ts); retVal += "OK"; return(retVal); } else { tableSeeks = new List <TableSeek>(); retVal += "Configuration not found." + Environment.NewLine; return(retVal); } } } for (int s = 0; s < tableSeeks.Count; s++) { if (tableSeeks[s].SearchStr.Length == 0) { continue; //Can't search if string is empty! } if (tableSeeks[s].Category != null && !PCM.tableCategories.Contains(tableSeeks[s].Category)) { PCM.tableCategories.Add(tableSeeks[s].Category); } uint startAddr = 0; uint endAddr = PCM.fsize; List <Block> addrList = new List <Block>(); SearchedAddress sAddr; sAddr.Addr = uint.MaxValue; sAddr.Rows = 0; sAddr.Columns = 0; Block block = new Block(); if (tableSeeks[s].Range != null && tableSeeks[s].Range.Length > 0) { string[] rangeS = tableSeeks[s].Range.Split(','); for (int r = 0; r < rangeS.Length; r++) { string[] range = rangeS[r].Split('-'); if (range.Length != 2) { throw new Exception("Unknown address range:" + rangeS[r]); } if (HexToUint(range[0], out block.Start) == false) { throw new Exception("Unknown HEX code:" + range[0]); } if (HexToUint(range[1], out block.End) == false) { throw new Exception("Unknown HEX code:" + range[1]); } addrList.Add(block); } } else if (tableSeeks[s].Segments != null && tableSeeks[s].Segments.Length > 0) { string[] segStrings = tableSeeks[s].Segments.Split(','); for (int y = 0; y < segStrings.Length; y++) { int segNr = 0; if (int.TryParse(segStrings[y], out segNr) == false) { throw new Exception("Unknown segment: " + segStrings[y]); } for (int b = 0; b < PCM.segmentAddressDatas[segNr].SegmentBlocks.Count; b++) { addrList.Add(PCM.segmentAddressDatas[segNr].SegmentBlocks[b]); } } } else { block.Start = 0; block.End = PCM.fsize; addrList.Add(block); } int hit = 0; ushort wantedHit = 1; List <ushort> wantedHitList = new List <ushort>(); string[] hParts = tableSeeks[s].UseHit.Split(','); for (int h = 0; h < hParts.Length; h++) { if (hParts[h].Contains("-")) { string[] hParts2 = hParts[h].Split('-'); //It's range, loop through all values ushort hStart = 0; ushort hEnd = 1; ushort.TryParse(hParts2[0], out hStart); ushort.TryParse(hParts2[1], out hEnd); for (ushort x = hStart; x <= hEnd; x++) { wantedHitList.Add(x); } } else { //Single value if (ushort.TryParse(hParts[h], out wantedHit)) { wantedHitList.Add(wantedHit); } } } int wHit = 0; for (int b = 0; b < addrList.Count; b++) { startAddr = addrList[b].Start; endAddr = addrList[b].End; while (startAddr < PCM.fsize) { wantedHit = wantedHitList[wHit]; string[] ssParts = tableSeeks[s].SearchStr.Split('+'); //At end of string can be +D4 +1W6 etc, for reading next address from found addr Debug.WriteLine("TableSeek: Searching: " + tableSeeks[s].SearchStr + ", Start: " + startAddr.ToString("X") + ", end: " + endAddr.ToString("X")); sAddr = getAddrbySearchString(PCM, ssParts[0], ref startAddr, endAddr, tableSeeks[s].ConditionalOffset, tableSeeks[s].ValidationSearchStr); for (int jump = 1; jump < ssParts.Length && sAddr.Addr < PCM.fsize; jump++) { //Read table address from address we found by searchstring string numOnly = ssParts[jump].Replace("+", "").Replace("D", "").Replace("W", ""); int offset = Convert.ToInt32(numOnly); //For first jump, use tableseek offset, for other jumps use searchstring offset uint currentAddr = (uint)(sAddr.Addr + offset); Debug.WriteLine("seekTables: Reading new address from:" + currentAddr.ToString("X")); if (ssParts[jump].Contains("D")) { sAddr.Addr = (uint)(BEToUint32(PCM.buf, currentAddr)); } else { sAddr.Addr = (uint)(BEToUint16(PCM.buf, currentAddr)); } Debug.WriteLine("seekTables: New address:" + sAddr.Addr.ToString("X")); } if (sAddr.Addr < PCM.fsize) { hit++; Debug.WriteLine("Found: " + sAddr.Addr.ToString("X") + ", Hit: " + hit.ToString() + " of " + wantedHit); } if (hit == wantedHit && sAddr.Addr < PCM.fsize) { FoundTable ft = new FoundTable(PCM); ft.configId = s; ft.Name = tableSeeks[s].Name.Replace("£", (wHit + 1).ToString()); ft.Description = tableSeeks[s].Description.Replace("£", (wHit + 1).ToString()); ft.addrInt = (uint)(sAddr.Addr + tableSeeks[s].Offset); if (tableSeeks[s].Category != null && tableSeeks[s].Category != "") { ft.Category = tableSeeks[s].Category; } else { ft.Category = "Seg-" + PCM.GetSegmentName(ft.addrInt); } ft.Address = ft.addrInt.ToString("X8"); if (tableSeeks[s].Rows > 0) { ft.Rows = tableSeeks[s].Rows; } else { ft.Rows = sAddr.Rows; } if (tableSeeks[s].Columns > 0) { ft.Columns = tableSeeks[s].Columns; } else { ft.Columns = sAddr.Columns; } PCM.foundTables.Add(ft); wHit++; } if (wHit >= wantedHitList.Count) { break; } } } } } catch (Exception ex) { var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(st.FrameCount - 1); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); return("Table seek, line " + line + ": " + ex.Message); } retVal += "Done"; return(retVal); }
private string peekTableValues(int ind, PcmFile peekPCM) { string retVal = ""; try { frmTableEditor frmT = new frmTableEditor(); frmT.PCM = peekPCM; frmT.disableMultiTable = true; frmT.loadTable(peekPCM.tableDatas[ind]); //txtResult.SelectionFont = new Font(txtResult.Font, FontStyle.Regular); //txtResult.SelectionColor = Color.Blue; if (peekPCM.tableDatas[ind].Rows == 1 && peekPCM.tableDatas[ind].Columns == 1) { double curVal = frmT.getValue((uint)(peekPCM.tableDatas[ind].addrInt + peekPCM.tableDatas[ind].Offset), peekPCM.tableDatas[ind]); UInt64 rawVal = frmT.getRawValue((uint)(peekPCM.tableDatas[ind].addrInt + peekPCM.tableDatas[ind].Offset), peekPCM.tableDatas[ind]); string valTxt = curVal.ToString(); string unitTxt = " " + peekPCM.tableDatas[ind].Units; string maskTxt = ""; if (peekPCM.tableDatas[ind].OutputType == OutDataType.Flag || peekPCM.tableDatas[ind].Units.ToLower().StartsWith("boolean")) { if (peekPCM.tableDatas[ind].BitMask != null && peekPCM.tableDatas[ind].BitMask.Length > 0) { unitTxt = ""; UInt64 maskVal = Convert.ToUInt64(peekPCM.tableDatas[ind].BitMask.Replace("0x", ""), 16); if ((rawVal & maskVal) == maskVal) { valTxt = "Set"; } else { valTxt = "Unset"; } string maskBits = Convert.ToString((Int64)maskVal, 2); int bit = -1; for (int i = 0; 1 <= maskBits.Length; i++) { if (((maskVal & (UInt64)(1 << i)) != 0)) { bit = i + 1; break; } } if (bit > -1) { string rawBinVal = Convert.ToString((Int64)rawVal, 2); rawBinVal = rawBinVal.PadLeft(getBits(peekPCM.tableDatas[ind].DataType), '0'); maskTxt = " [" + rawBinVal + "], bit $" + bit.ToString(); } } else { unitTxt = ", Unset/Set"; if (curVal > 0) { valTxt = "Set, " + valTxt; } else { valTxt = "Unset, " + valTxt; } } } else if (peekPCM.tableDatas[ind].Values.StartsWith("Enum: ")) { Dictionary <double, string> possibleVals = frmT.parseEnumHeaders(peekPCM.tableDatas[ind].Values.Replace("Enum: ", "")); if (possibleVals.ContainsKey(curVal)) { unitTxt = " (" + possibleVals[curVal] + ")"; } else { unitTxt = " (Out of range)"; } } string formatStr = "X" + (getElementSize(peekPCM.tableDatas[ind].DataType) * 2).ToString(); retVal = valTxt + unitTxt + " [" + rawVal.ToString(formatStr) + "]" + maskTxt; //txtResult.AppendText(Environment.NewLine); } else { string tblData = ""; //"Current values: " + Environment.NewLine; uint addr = (uint)(peekPCM.tableDatas[ind].addrInt + peekPCM.tableDatas[ind].Offset); if (peekPCM.tableDatas[ind].RowMajor) { for (int r = 0; r < peekPCM.tableDatas[ind].Rows; r++) { for (int c = 0; c < peekPCM.tableDatas[ind].Columns; c++) { double curVal = frmT.getValue(addr, peekPCM.tableDatas[ind]); addr += (uint)getElementSize(peekPCM.tableDatas[ind].DataType); tblData += "[" + curVal.ToString("#0.0") + "]"; } tblData += Environment.NewLine; } } else { List <string> tblRows = new List <string>(); for (int r = 0; r < peekPCM.tableDatas[ind].Rows; r++) { tblRows.Add(""); } for (int c = 0; c < peekPCM.tableDatas[ind].Columns; c++) { for (int r = 0; r < peekPCM.tableDatas[ind].Rows; r++) { double curVal = frmT.getValue(addr, peekPCM.tableDatas[ind]); addr += (uint)getElementSize(peekPCM.tableDatas[ind].DataType); tblRows[r] += "[" + curVal.ToString("#0.0") + "]"; } } for (int r = 0; r < peekPCM.tableDatas[ind].Rows; r++) { tblData += tblRows[r] + Environment.NewLine; } } retVal = tblData; } } catch (Exception ex) { LoggerBold(ex.Message); } return(retVal); }
public TableInfo(PcmFile _pcm, TableData _td) { pcm = _pcm; td = _td; tableCells = new List <TableCell>(); }
//public string start { get; set; } //public int distanceMin { get; set; } //public int distanceMax { get; set; } //public int location { get; set; } private bool ParseAddress(string Line, PcmFile PCM, out List <Block> Blocks) { Debug.WriteLine("Segment address line: " + Line); Blocks = new List <Block>(); if (Line == null || Line == "") { //It is ok to have empty address (for CS, not for segment) Block B = new Block(); B.End = PCM.fsize; B.Start = 0; Blocks.Add(B); return(true); } string[] Parts = Line.Split(','); int i = 0; foreach (string Part in Parts) { string[] StartEnd = Part.Split('-'); Block B = new Block(); int Offset = 0; if (StartEnd[0].Contains(">")) { string[] SO = StartEnd[0].Split('>'); StartEnd[0] = SO[0]; uint x; if (!HexToUint(SO[1], out x)) { throw new Exception("Can't decode from HEX: " + SO[1] + " (" + Line + ")"); } Offset = (int)x; } if (StartEnd[0].Contains("<")) { string[] SO = StartEnd[0].Split('<'); StartEnd[0] = SO[0]; uint x; if (!HexToUint(SO[1], out x)) { throw new Exception("Can't decode from HEX: " + SO[1] + " (" + Line + ")"); } Offset = ~(int)x; } if (!HexToUint(StartEnd[0].Replace("@", ""), out B.Start)) { throw new Exception("Can't decode from HEX: " + StartEnd[0].Replace("@", "") + " (" + Line + ")"); } if (StartEnd[0].StartsWith("@")) { uint tmpStart = B.Start; B.Start = BEToUint32(PCM.buf, tmpStart); B.End = BEToUint32(PCM.buf, tmpStart + 4); tmpStart += 8; } else { if (!HexToUint(StartEnd[1].Replace("@", ""), out B.End)) { throw new Exception("Can't decode from HEX: " + StartEnd[1].Replace("@", "") + " (" + Line + ")"); } if (B.End >= PCM.buf.Length) //Make 1MB config work with 512kB bin { B.End = (uint)PCM.buf.Length - 1; } } if (StartEnd.Length > 1 && StartEnd[1].StartsWith("@")) { //Read End address from bin at this address B.End = BEToUint32(PCM.buf, B.End); } if (StartEnd.Length > 1 && StartEnd[1].EndsWith("@")) { //Address is relative to end of bin uint end; if (HexToUint(StartEnd[1].Replace("@", ""), out end)) { B.End = (uint)PCM.buf.Length - end - 1; } } B.Start += (uint)Offset; B.End += (uint)Offset; Blocks.Add(B); i++; } foreach (Block B in Blocks) { Debug.WriteLine("Address block: " + B.Start.ToString("X") + " - " + B.End.ToString("X")); } return(true); }
public void loadFlag(PcmFile PCM1, TableData td1) { PCM = PCM1; td = td1; uint addr = (uint)(td.addrInt + td.Offset); if (td.TableDescription.Length > 1) { labelDescription.Text = td.TableDescription; } else { labelDescription.Text = td.TableName; } this.Text = td.TableName; string maskStr = td.BitMask; if (td.DataType == InDataType.UBYTE || td.DataType == InDataType.SBYTE) { Byte mask = Convert.ToByte(maskStr, 16); if ((PCM.buf[addr] & mask) == mask) { chkFlag.Checked = true; } else { chkFlag.Checked = false; } } if (td.DataType == InDataType.UWORD || td.DataType == InDataType.SWORD) { ushort mask = Convert.ToUInt16(maskStr, 16); if ((BEToInt16(PCM.buf, addr) & mask) == mask) { chkFlag.Checked = true; } else { chkFlag.Checked = false; } } if (td.DataType == InDataType.INT32 || td.DataType == InDataType.UINT32) { UInt32 mask = Convert.ToUInt32(maskStr, 16); if ((BEToInt32(PCM.buf, addr) & mask) == mask) { chkFlag.Checked = true; } else { chkFlag.Checked = false; } } if (td.DataType == InDataType.INT64 || td.DataType == InDataType.UINT64) { UInt64 mask = Convert.ToUInt64(maskStr, 16); if ((BEToUint64(PCM.buf, addr) & mask) == mask) { chkFlag.Checked = true; } else { chkFlag.Checked = false; } } }
private TableSearchResult parseMatch(PcmFile PCM, TableSearchConfig.ParsedTableSearchConfig parsedConfig, TableSearchConfig tableSearchConfig, uint addr) { TableSearchResult tsr = new TableSearchResult(); tsr.OS = PCM.OS; tsr.File = PCM.FileName; tsr.Search = parsedConfig.searchString; tsr.Name = tableSearchConfig.Name; tsr.Found = addr.ToString("X8") + ":"; for (uint t = 0; t < parsedConfig.searchParts.Count; t++) { if (t > 0) { tsr.Found += " "; } tsr.Found += PCM.buf[addr + t].ToString("X2"); } if (tableSearchConfig.Items.Length > 0) { string[] items = tableSearchConfig.Items.Split(','); for (int p = 0; p < items.Length; p++) { string[] itemParts = items[p].Split(':'); uint location = (uint)(addr + int.Parse(itemParts[1])); if (itemParts.Length == 3) { SearchVariable SV = new SearchVariable(); SV.Name = itemParts[0]; if (itemParts[2].ToLower() == "e") { location += (uint)parsedConfig.searchParts.Count; } tsr.AddressInt = location; tsr.Data = SV.Name; SV.Data = ((location & 0xFF000000) >> 24).ToString("X2") + " " + ((location & 0xFF0000) >> 16).ToString("X2") + " " + ((location & 0xFF00) >> 8).ToString("X2") + " " + (location & 0xFF).ToString("X2"); searchVariables.Add(SV); } else { if (itemParts.Length != 4) { throw new Exception("item needs 4 parts: name:location:bytes:type, for example: rows:1:2:int"); } if (itemParts[0].StartsWith("@")) { tsr.AddressInt = BEToUint32(PCM.buf, location); } if (tsr.Data != null && tsr.Data.Length > 1) { tsr.Data += "; "; } tsr.Data += itemParts[0] + ":"; //Item name string formatString = ""; if (itemParts[3].ToLower() == "hex") { formatString = "X"; } if (itemParts[2] == "1") { tsr.Data += PCM.buf[location].ToString(formatString); } if (itemParts[2] == "2") { tsr.Data += BEToUint16(PCM.buf, location).ToString(formatString); } if (itemParts[2] == "4") { tsr.Data += BEToUint32(PCM.buf, location).ToString(formatString); } if (itemParts[2] == "8") { tsr.Data += BEToUint16(PCM.buf, location).ToString(formatString); } } for (int seg = 0; seg < Segments.Count; seg++) { for (int b = 0; b < PCM.binfile[seg].SegmentBlocks.Count; b++) { if (tsr.AddressInt >= PCM.binfile[seg].SegmentBlocks[b].Start && tsr.AddressInt <= PCM.binfile[seg].SegmentBlocks[b].End) { tsr.Segment = PCM.segmentinfos[seg].Name; } } } } } if (parsedConfig.searchString.Contains(":")) { int k = 0; while (k < parsedConfig.searchParts.Count) { if (parsedConfig.searchParts[k].Contains(":")) { string[] varParts = parsedConfig.searchParts[k].Split(':'); if (varParts.Length == 2) { //Set variable SearchVariable SV = new SearchVariable(); SV.Name = varParts[0]; tsr.Data += SV.Name; int bytes = int.Parse(varParts[1]); uint location = (uint)(addr + k); tsr.AddressInt = location; for (uint l = 0; l < bytes; l++) { SV.Data += PCM.buf[location + l].ToString("X2") + " "; } SV.Data = SV.Data.Trim(); searchVariables.Add(SV); } else { //Its's item, show it. uint location = (uint)(addr + k); if (varParts[0].ToLower().StartsWith("@")) { tsr.AddressInt = BEToUint32(PCM.buf, location); } if (varParts[0].ToLower().StartsWith("row")) { tsr.Rows = PCM.buf[location]; } if (tsr.Data != null && tsr.Data.Length > 1) { tsr.Data += "; "; } tsr.Data += varParts[0] + ":"; string formatString = ""; if (varParts[2].ToLower() == "hex") { formatString = "X"; } if (varParts[1] == "1") { tsr.Data += PCM.buf[location].ToString(formatString); } if (varParts[1] == "2") { tsr.Data += BEToUint16(PCM.buf, location).ToString(formatString); } if (varParts[1] == "4") { tsr.Data += BEToUint32(PCM.buf, location).ToString(formatString); } if (varParts[1] == "8") { tsr.Data += BEToUint64(PCM.buf, location).ToString(formatString); } } for (int seg = 0; seg < Segments.Count; seg++) { for (int b = 0; b < PCM.binfile[seg].SegmentBlocks.Count; b++) { if (tsr.AddressInt >= PCM.binfile[seg].SegmentBlocks[b].Start && tsr.AddressInt <= PCM.binfile[seg].SegmentBlocks[b].End) { tsr.Segment = PCM.segmentinfos[seg].Name; } } } k += int.Parse(varParts[1]) - 1; } k++; } } for (int o = 0; o < PCM.osaAddressList.Count; o++) { if (PCM.osaAddressList[o].address == tsr.AddressInt) { tsr.Category = PCM.osaAddressList[o].category; tsr.Label = PCM.osaAddressList[o].label; tsr.Size = PCM.osaAddressList[o].size; break; } } TableSearchResult tsrNoFilter = new TableSearchResult(); tsrNoFilter.AddressInt = tsr.AddressInt; tsrNoFilter.Address = tsr.AddressInt.ToString("X8"); tsrNoFilter.Data = tsr.Data; tsrNoFilter.File = tsr.File; tsrNoFilter.Found = tsr.Found; tsrNoFilter.hitCount = 1; tsrNoFilter.Name = tsr.Name; tsrNoFilter.OS = tsr.OS; tsrNoFilter.Search = tsr.Search; tsrNoFilter.Segment = tsr.Segment; tableSearchResultNoFilters.Add(tsrNoFilter); return(tsr); }
public string readTinyDB(PcmFile PCM) { string connetionString = null; OleDbConnection cnn; string dbFile = Path.Combine(Application.StartupPath, "TinyTuner.mdb"); connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile; cnn = new OleDbConnection(connetionString); try { string query = "Select MapNumber from OSIDData where OSID=" + PCM.OS.ToString(); OleDbCommand selectCommand = new OleDbCommand(query, cnn); cnn.Open(); DataTable table = new DataTable(); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = selectCommand; adapter.Fill(table); string mapNr = ""; foreach (DataRow row in table.Rows) { mapNr = row["MapNumber"].ToString(); } if (mapNr == "") { MessageBox.Show("OS not found from TinyTuner DB", "OS not found from TinyTuner DB"); return("Not found"); } query = "select * from CategoryList order by Category"; selectCommand = new OleDbCommand(query, cnn); table = new DataTable(); adapter = new OleDbDataAdapter(); adapter.SelectCommand = selectCommand; adapter.Fill(table); if (PCM.tableCategories == null) { PCM.tableCategories = new List <string>(); } foreach (DataRow row in table.Rows) { string cat = row["Category"].ToString(); if (!PCM.tableCategories.Contains(cat)) { PCM.tableCategories.Add(cat); } } query = "select * from TableData where MapNumber=" + mapNr + " order by TableName"; selectCommand = new OleDbCommand(query, cnn); table = new DataTable(); adapter = new OleDbDataAdapter(); adapter.SelectCommand = selectCommand; adapter.Fill(table); if (tableSeeks == null) { tableSeeks = new List <TableSeek>(); } if (PCM.foundTables == null) { PCM.foundTables = new List <FoundTable>(); } foreach (DataRow row in table.Rows) { TableSeek ts = new TableSeek(); FoundTable ft = new FoundTable(PCM); HexToUint(row["StartPosition"].ToString(), out ft.addrInt); ft.Address = ft.addrInt.ToString("X8"); ft.Columns = Convert.ToUInt16(row["ColumnCount"]); ft.Description = row["TableDescription"].ToString(); ft.Name = row["TableName"].ToString(); ft.Rows = Convert.ToUInt16(row["RowCount"]); ft.Category = row["MainCategory"].ToString(); ft.configId = tableSeeks.Count; PCM.foundTables.Add(ft); ushort elementSize = (ushort)(Convert.ToUInt16(row["ElementSize"])); bool signed = Convert.ToBoolean(row["AllowNegative"]); ts.DataType = convertToDataType(elementSize, signed, true); string colHeaders = row["ColumnHeaders"].ToString(); ts.ColHeaders = RemoveDuplicates(colHeaders); ts.Columns = Convert.ToUInt16(row["ColumnCount"]); ts.Decimals = 2; ts.Description = row["TableDescription"].ToString(); ts.Math = "X*" + row["Factor"].ToString(); ts.Name = row["TableName"].ToString(); ts.Rows = Convert.ToUInt16(row["RowCount"]); ts.RowHeaders = row["RowHeaders"].ToString(); if (ts.RowHeaders.Contains(",by,")) { ts.RowHeaders = convertByHeader(ts.RowHeaders, ts.Rows); } ts.SavingMath = "X/" + row["Factor"].ToString(); ts.Category = row["MainCategory"].ToString(); ts.Units = row["Units"].ToString(); ts.RowMajor = false; tableSeeks.Add(ts); } cnn.Close(); } catch (Exception ex) { var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(st.FrameCount - 1); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); MessageBox.Show("Error, line " + line + ": " + ex.Message, "Error"); } return("OK"); }
public void parseAddresses(PcmFile PCM) { ParseAddress(searchRange, PCM, out searchBlocks); ParseAddress(tableRange, PCM, out tableBlocks); }
public string exportXdf(PcmFile basefile, List <TableData> tdList1) { PCM = basefile; tdList = tdList1; string retVal = ""; string tableRows = ""; String tableText = ""; string templateTxt = ""; int lastCategory = 0; int dtcCategory = 0; int uniqId = 0x100; List <int> uniqIds = new List <int>(); List <string> tableNames = new List <string>(); //Store constant id/name for later use try { string fName = Path.Combine(Application.StartupPath, "Templates", "xdfheader.txt"); string xdfText = ReadTextFile(fName); xdfText = xdfText.Replace("REPLACE-TIMESTAMP", DateTime.Today.ToString("MM/dd/yyyy H:mm")); xdfText = xdfText.Replace("REPLACE-OSID", basefile.OS); xdfText = xdfText.Replace("REPLACE-BINSIZE", basefile.fsize.ToString("X")); for (int s = 1; s < basefile.tableCategories.Count; s++) { tableText += " <CATEGORY index = \"0x" + (s - 1).ToString("X") + "\" name = \"" + basefile.tableCategories[s] + "\" />" + Environment.NewLine; lastCategory = s; } dtcCategory = lastCategory + 1; tableText += " <CATEGORY index = \"0x" + (dtcCategory - 1).ToString("X") + "\" name = \"DTC\" />" + Environment.NewLine; lastCategory = dtcCategory + 1; tableText += " <CATEGORY index = \"0x" + (lastCategory - 1).ToString("X") + "\" name = \"Other\" />"; xdfText = xdfText.Replace("REPLACE-CATEGORYNAME", tableText) + Environment.NewLine; fName = Path.Combine(Application.StartupPath, "Templates", basefile.configFile + "-checksum.txt"); xdfText += ReadTextFile(fName); //Add OS ID: fName = Path.Combine(Application.StartupPath, "Templates", "xdfconstant.txt"); templateTxt = ReadTextFile(fName); tableText = templateTxt.Replace("REPLACE-TABLEID", uniqId.ToString("X")); uniqId++; tableText = tableText.Replace("REPLACE-LINKMATH", ""); tableText = tableText.Replace("REPLACE-MATH", "x"); tableText = tableText.Replace("REPLACE-TABLEDESCRIPTION", "DON'T MODIFY"); tableText = tableText.Replace("REPLACE-TABLETITLE", "OS ID - Don't modify, must match XDF!"); tableText = tableText.Replace("REPLACE-BITS", "32"); tableText = tableText.Replace("REPLACE-MINVALUE", basefile.OS); tableText = tableText.Replace("REPLACE-MAXVALUE", basefile.OS); tableText = tableText.Replace("REPLACE-TABLEADDRESS", basefile.segmentAddressDatas[basefile.OSSegment].PNaddr.Address.ToString("X")); tableText = tableText.Replace("REPLACE-CATEGORY", (basefile.OSSegment + 1).ToString("X")); xdfText += tableText; fName = Path.Combine(Application.StartupPath, "Templates", "xdfconstant.txt"); templateTxt = ReadTextFile(fName); for (int t = 0; t < tdList.Count; t++) { //Add all constants if (tdList[t].Rows < 2 && tdList[t].OutputType != OutDataType.Flag) { string tableName = tdList[t].TableName.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'");; if (tdList[t].TableName == null || tdList[t].TableName.Length == 0) { tableName = tdList[t].Address; } tableText = templateTxt.Replace("REPLACE-TABLETITLE", tableName); int s = basefile.GetSegmentNumber(tdList[t].addrInt); if (s == -1) { s = lastCategory; } tableText = tableText.Replace("REPLACE-CATEGORY", (s + 1).ToString("X")); tableText = tableText.Replace("REPLACE-TABLEID", uniqId.ToString("X")); uniqIds.Add(uniqId); tableNames.Add(tdList[t].TableName.ToLower()); uniqId++; string linkTxt = ""; string mathTxt = tdList[t].Math.ToLower(); if (mathTxt.Contains("raw:")) { mathTxt = linkConversionRaw(mathTxt, out linkTxt); } tableText = tableText.Replace("REPLACE-LINKMATH", linkTxt); tableText = tableText.Replace("REPLACE-MATH", mathTxt); tableText = tableText.Replace("REPLACE-TABLEADDRESS", ((uint)(tdList[t].addrInt + tdList[t].Offset)).ToString("X")); string descr = tdList[t].TableDescription.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'"); tableText = tableText.Replace("REPLACE-TABLEDESCRIPTION", descr); tableText = tableText.Replace("REPLACE-BITS", getBits(tdList[t].DataType).ToString()); tableText = tableText.Replace("REPLACE-MINVALUE", tdList[t].Min.ToString()); tableText = tableText.Replace("REPLACE-MAXVALUE", tdList[t].Max.ToString()); xdfText += tableText; //Add generated table to end of xdfText } } fName = Path.Combine(Application.StartupPath, "Templates", "xdftable.txt"); templateTxt = ReadTextFile(fName); for (int t = 0; t < tdList.Count; t++) { //Add all tables if (tdList[t].Rows > 1) { string tableName = tdList[t].TableName.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'"); if (tdList[t].TableName == null || tdList[t].TableName.Length == 0) { tableName = tdList[t].Address; } tableText = templateTxt.Replace("REPLACE-TABLETITLE", tableName); int s = basefile.GetSegmentNumber(tdList[t].addrInt); if (s == -1) { s = lastCategory; } if (tdList[t].Category != null && tdList[t].Category != "") { for (int c = 1; c < PCM.tableCategories.Count; c++) { if (PCM.tableCategories[c] == tdList[t].Category) { tableText = tableText.Replace("REPLACE-CATEGORY", c.ToString()); break; } } } else { tableText = tableText.Replace("REPLACE-CATEGORY", s.ToString()); } /*if (tdList[t].Values.StartsWith("Enum: ") && !descr.ToLower().Contains("enum")) * { * string[] hParts = tdList[t].Values.Substring(6).Split(','); * for (int x = 0; x < hParts.Length; x++) * { * descr += Environment.NewLine + hParts[x]; * } * }*/ string linkTxt = ""; string mathTxt = tdList[t].Math.ToLower(); if (mathTxt.Contains("table:")) { mathTxt = linkConversionTable(mathTxt, uniqIds, tableNames, out linkTxt); } if (mathTxt.Contains("raw:")) { string tmpTxt = ""; mathTxt = linkConversionRaw(mathTxt, out tmpTxt); linkTxt += tmpTxt; } tableText = tableText.Replace("REPLACE-LINKMATH", linkTxt); tableText = tableText.Replace("REPLACE-MATH", mathTxt); tableText = tableText.Replace("REPLACE-TABLEID", uniqId.ToString("X")); uniqId++; tableText = tableText.Replace("REPLACE-UNITS", tdList[t].Units); tableText = tableText.Replace("REPLACE-ROWCOUNT", tdList[t].Rows.ToString()); tableText = tableText.Replace("REPLACE-COLCOUNT", tdList[t].Columns.ToString()); tableText = tableText.Replace("REPLACE-BITS", getBits(tdList[t].DataType).ToString()); tableText = tableText.Replace("REPLACE-DECIMALS", tdList[t].Decimals.ToString()); tableText = tableText.Replace("REPLACE-OUTPUTTYPE", ((ushort)tdList[t].OutputType).ToString()); tableText = tableText.Replace("REPLACE-TABLEADDRESS", ((uint)(tdList[t].addrInt + tdList[t].Offset)).ToString("X")); string descr = tdList[t].TableDescription.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'"); tableText = tableText.Replace("REPLACE-TABLEDESCRIPTION", descr); tableText = tableText.Replace("REPLACE-MINVALUE", tdList[t].Min.ToString()); tableText = tableText.Replace("REPLACE-MAXVALUE", tdList[t].Max.ToString()); int tableFlags = 0; if (getSigned(tdList[t].DataType)) { tableFlags++; } if (tdList[t].RowMajor == false) { tableFlags += 4; } tableText = tableText.Replace("REPLACE-TYPEFLAGS", tableFlags.ToString("X2")); tableRows = ""; if (tdList[t].RowHeaders == "") { for (int d = 0; d < tdList[t].Rows; d++) { tableRows += " <LABEL index=\"" + d.ToString() + "\" value=\"" + d.ToString() + "\" />"; if (d < tdList[t].Rows - 1) { tableRows += Environment.NewLine; } } } else { string[] hParts = tdList[t].RowHeaders.Split(','); for (int row = 0; row < hParts.Length; row++) { tableRows += " <LABEL index=\"" + row.ToString() + "\" value=\"" + hParts[row] + "\" />"; if (row < hParts.Length - 1) { tableRows += Environment.NewLine; } } } tableText = tableText.Replace("REPLACE-TABLEROWS", tableRows); string tableCols = ""; if (tdList[t].ColumnHeaders == "") { for (int d = 0; d < tdList[t].Columns; d++) { tableCols += " <LABEL index=\"" + d.ToString() + "\" value=\"" + d.ToString() + "\" />"; if (d < tdList[t].Columns - 1) { tableCols += Environment.NewLine; } } } else { string[] hParts = tdList[t].ColumnHeaders.Split(','); for (int col = 0; col < hParts.Length; col++) { tableCols += " <LABEL index=\"" + col.ToString() + "\" value=\"" + hParts[col] + "\" />"; if (col < hParts.Length - 1) { tableCols += Environment.NewLine; } } } tableText = tableText.Replace("REPLACE-TABLECOLS", tableCols); xdfText += tableText; //Add generated table to end of xdfText } } fName = Path.Combine(Application.StartupPath, "Templates", "xdfFlag.txt"); templateTxt = ReadTextFile(fName); for (int t = 0; t < tdList.Count; t++) { //Add all constants if (tdList[t].OutputType == OutDataType.Flag) { string tableName = tdList[t].TableName.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'"); if (tdList[t].TableName == null || tdList[t].TableName.Length == 0) { tableName = tdList[t].Address; } tableText = templateTxt.Replace("REPLACE-TABLETITLE", tableName); int s = basefile.GetSegmentNumber(tdList[t].addrInt); if (s == -1) { s = lastCategory; } tableText = tableText.Replace("REPLACE-CATEGORY", (s + 1).ToString("X")); tableText = tableText.Replace("REPLACE-TABLEID", uniqId.ToString("X")); uniqId++; tableText = tableText.Replace("REPLACE-TABLEADDRESS", ((uint)(tdList[t].addrInt + tdList[t].Offset)).ToString("X")); tableText = tableText.Replace("REPLACE-TABLEDESCRIPTION", ""); tableText = tableText.Replace("REPLACE-BITS", getBits(tdList[t].DataType).ToString()); tableText = tableText.Replace("REPLACE-MINVALUE", tdList[t].Min.ToString()); tableText = tableText.Replace("REPLACE-MAXVALUE", tdList[t].Max.ToString()); tableText = tableText.Replace("REPLACE-MASK", tdList[t].BitMask); xdfText += tableText; //Add generated table to end of xdfText } } xdfText += "</XDFFORMAT>" + Environment.NewLine; string defFname = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Tunerpro Files", "Bin Definitions", basefile.OS + "-generated.xdf"); string fileName = SelectSaveFile("XDF Files(*.xdf)|*.xdf|ALL Files (*.*)|*.*", defFname); if (fileName.Length == 0) { return(""); } retVal += "Writing to file: " + Path.GetFileName(fileName); WriteTextFile(fileName, xdfText); retVal += " [OK]"; } catch (Exception ex) { var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(st.FrameCount - 1); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); retVal += ("Export XDF, line " + line + ": " + ex.Message); } return(retVal); }
public void searchTables(PcmFile PCM, bool crossSearch, string CustomSearch = "", int crossVariation = 3) { try { if (tableSearchResult == null) { tableSearchResult = new List <TableSearchResult>(); } if (tableSearchResultNoFilters == null) { tableSearchResultNoFilters = new List <TableSearchResult>(); } searchVariables = new List <SearchVariable>(); TableSearchResult tsr = new TableSearchResult(); List <TableSearchResult> thisFileTables = new List <TableSearchResult>(); List <TableSearchConfig> tableSearchConfig; if (CustomSearch == "") { string searchXMLFile = Path.Combine(Application.StartupPath, "XML", "SearchTables-" + Path.GetFileName(XMLFile)); if (!File.Exists(searchXMLFile)) { return; } System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(List <TableSearchConfig>)); System.IO.StreamReader file = new System.IO.StreamReader(searchXMLFile); tableSearchConfig = (List <TableSearchConfig>)reader.Deserialize(file); file.Close(); tableSearchFile = searchXMLFile; } else { tableSearchConfig = new List <TableSearchConfig>(); TableSearchConfig tsc = new TableSearchConfig(); tsc.searchData = CustomSearch; tableSearchConfig.Add(tsc); } for (int i = 0; i < tableSearchConfig.Count; i++) { string searchTxt = tableSearchConfig[i].searchData.Replace("[", ""); searchTxt = searchTxt.Replace("]", ""); if (searchTxt.StartsWith("//") || searchTxt.Length < 2) { Debug.WriteLine("Skipping disabled line: " + searchTxt); } else { parsedConfigList = new List <TableSearchConfig.ParsedTableSearchConfig>(); TableSearchConfig.ParsedTableSearchConfig parsedConfig = new TableSearchConfig.ParsedTableSearchConfig(); TableSearchConfig tsc = tableSearchConfig[i]; tsc.parseAddresses(PCM); List <SearchVariable> tmpVariables = tsc.parseVariables(PCM); for (int var = 0; var < tmpVariables.Count; var++) { searchVariables.Add(tmpVariables[var]); } Debug.WriteLine("Original searchstring: " + searchTxt); int commonParts = parseTableSearchString(searchTxt, parsedConfig); Debug.WriteLine("Searchstrings generated: " + parsedConfigList.Count); for (int block = 0; block < tsc.searchBlocks.Count; block++) { uint addr = tsc.searchBlocks[block].Start; while (addr < tsc.searchBlocks[block].End) { int ss = 0; while (ss < parsedConfigList.Count) { bool match = false; int hits = 0; for (int j = 0; j < parsedConfigList[ss].searchParts.Count; j++) { if (parsedConfigList[ss].searchValues[j] == -1) { //Count as hit (wildcard, ignore) hits++; } else { if (PCM.buf[addr + j] == parsedConfigList[ss].searchValues[j]) { hits++; } else { break; } } } if (hits == parsedConfigList[ss].searchValues.Count) { match = true; searchTxt = parsedConfigList[ss].searchString; parsedConfig = parsedConfigList[ss]; } if (hits < parsedConfigList[ss].searchValues.Count && hits < commonParts) { //Optimization: beginning of all parsed searchstrings until "commonParts" are identical. //If this string doesn't match, others in list doesn't match either match = false; ss = parsedConfigList.Count; } if (match) { tsr = parseMatch(PCM, parsedConfigList[ss], tsc, addr); for (int tblock = 0; tblock < tsc.tableBlocks.Count; tblock++) { if (tsr.AddressInt >= tsc.tableBlocks[tblock].Start && tsr.AddressInt <= tsc.tableBlocks[tblock].End) { bool duplicate = false; for (int ts = 0; ts < thisFileTables.Count; ts++) { if (thisFileTables[ts].Data == tsr.Data && thisFileTables[ts].Name == tsr.Name && thisFileTables[ts].Search == tsr.Search) { thisFileTables[ts].hitCount++; duplicate = true; //thisFileTables[ts].Address += ";" + tsr.AddressInt.ToString("X8"); thisFileTables[ts].Found += ";" + tsr.Found; break; } } if (!duplicate) { tsr.Address = tsr.AddressInt.ToString("X8"); thisFileTables.Add(tsr); } } } } ss++; } addr++; } } } } if (crossSearch) { crossSearchTables(thisFileTables, PCM, crossVariation); } else { for (int t = 0; t < thisFileTables.Count; t++) { tableSearchResult.Add(thisFileTables[t]); } } } catch (Exception ex) { // Get stack trace for the exception with source file information var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(0); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); Debug.WriteLine("Tablesearch: " + line + ": " + ex.Message); } }
public void EditSegment(PcmFile PCM1, int SegNr) { PCM = PCM1; labelXML.Text = PCM.configFile + " (v " + PCM.Segments[0].Version + ")"; CurrentSegment = SegNr; SegmentConfig S = PCM.Segments[SegNr]; txtSegmentName.Text = S.Name; txtSegmentAddress.Text = S.Addresses; txtSwapAddr.Text = S.SwapAddress; txtCS1Address.Text = S.CS1Address; txtCS2Address.Text = S.CS2Address; txtCS1Block.Text = S.CS1Blocks; txtCS2Block.Text = S.CS2Blocks; txtPNAddr.Text = S.PNAddr; txtVerAddr.Text = S.VerAddr; txtNrAddr.Text = S.SegNrAddr; txtExtrainfo.Text = S.ExtraInfo; chkEeprom.Checked = S.Eeprom; txtComment.Text = S.Comment; txtCheckWords.Text = S.CheckWords; checkSwapBytes1.Checked = S.CS1SwapBytes; checkSwapBytes2.Checked = S.CS2SwapBytes; if (S.CS1Method == CSMethod_None) { radioCS1None.Checked = true; } if (S.CS1Method == CSMethod_crc16) { radioCS1Crc16.Checked = true; } if (S.CS1Method == CSMethod_crc32) { radioCS1Crc32.Checked = true; } if (S.CS1Method == CSMethod_Bytesum) { radioCS1SUM.Checked = true; } if (S.CS1Method == CSMethod_Wordsum) { radioCS1WordSum.Checked = true; } if (S.CS1Method == CSMethod_Dwordsum) { radioCS1DwordSum.Checked = true; } if (S.CS2Method == CSMethod_None) { radioCS2None.Checked = true; } if (S.CS2Method == CSMethod_crc16) { radioCS2Crc16.Checked = true; } if (S.CS2Method == CSMethod_crc32) { radioCS2Crc32.Checked = true; } if (S.CS2Method == CSMethod_Bytesum) { radioCS2SUM.Checked = true; } if (S.CS2Method == CSMethod_Wordsum) { radioCS2WordSum.Checked = true; } if (S.CS2Method == CSMethod_Dwordsum) { radioCS2DwordSum.Checked = true; } if (S.CS1Complement == 0) { radioCS1Complement0.Checked = true; } if (S.CS1Complement == 1) { radioCS1Complement1.Checked = true; } if (S.CS1Complement == 2) { radioCS1Complement2.Checked = true; } if (S.CS2Complement == 0) { radioCS2Complement0.Checked = true; } if (S.CS2Complement == 1) { radioCS2Complement1.Checked = true; } if (S.CS2Complement == 2) { radioCS2Complement2.Checked = true; } if (S.CVN == 0) { comboCVN.Text = "None"; } if (S.CVN == 1) { comboCVN.Text = "Checksum 1"; } if (S.CVN == 2) { comboCVN.Text = "Checksum 2"; } }
private void crossSearchTables(List <TableSearchResult> thisFileTables, PcmFile PCM, int crossVariation) { try { bool[] usedRow = new bool[thisFileTables.Count]; for (int u = 0; u < usedRow.Length; u++) { usedRow[u] = false; } int t = 0; int lastHit = 0; int missCount = 0; int t3 = 0; for (int pos = 0; pos < tableSearchResult.Count; pos++) { for (int pos2 = pos + 1; pos2 < tableSearchResult.Count; pos2++) { if (tableSearchResult[pos].AddressInt > tableSearchResult[pos2].AddressInt) { //Oraganize by Address: TableSearchResult tmpTsr = tableSearchResult[pos]; tableSearchResult[pos] = tableSearchResult[pos2]; tableSearchResult[pos2] = tmpTsr; } } } for (int pos = 0; pos < thisFileTables.Count; pos++) { for (int pos2 = pos + 1; pos2 < thisFileTables.Count; pos2++) { if (thisFileTables[pos].AddressInt > thisFileTables[pos2].AddressInt) { //Oraganize by Address: TableSearchResult tmpTsr = thisFileTables[pos]; thisFileTables[pos] = thisFileTables[pos2]; thisFileTables[pos2] = tmpTsr; } } } while (t < tableSearchResult.Count) { bool hit = false; int start = t3 - missCount - crossVariation; if (start < 0) { start = 0; } for (int maxOffset = 0; maxOffset < 100000 && !hit; maxOffset++) { for (int t2 = start; t2 < (t3 + crossVariation) && t2 < thisFileTables.Count; t2++) { if (!usedRow[t2]) { uint offset = (uint)Math.Abs(tableSearchResult[t].AddressInt - thisFileTables[t2].AddressInt); if (tableSearchResult[t].Name == thisFileTables[t2].Name && tableSearchResult[t].Segment == thisFileTables[t2].Segment && tableSearchResult[t].Search == thisFileTables[t2].Search && tableSearchResult[t].Rows == thisFileTables[t2].Rows && offset <= maxOffset ) { tableSearchResult.Insert(t + 1, thisFileTables[t2]); usedRow[t2] = true; lastHit = t2; hit = true; Debug.WriteLine("BinA: " + t3 + ", BinB: " + t2 + ", Addr offset: " + offset.ToString()); break; } } } } if (!hit) { TableSearchResult crossTSR = new TableSearchResult(); crossTSR.OS = PCM.OS; tableSearchResult.Insert(t + 1, crossTSR); missCount++; Debug.WriteLine("BinA: " + t3 + ", BinB: missing"); } t += 2; t3++; } } catch (Exception ex) { // Get stack trace for the exception with source file information var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(0); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); Debug.WriteLine("crossSearchTables: " + line + ": " + ex.Message); } }
public string searchDtc(PcmFile PCM) { try { loadOBD2Codes(); //Search DTC codes: uint codeAddr = uint.MaxValue; string searchStr; int configIndex = 0; uint startAddr = 0; PCM.dtcCombined = false; bool condOffset = false; uint statusAddr = uint.MaxValue; uint milAddr = uint.MaxValue; for (configIndex = 0; configIndex < dtcSearchConfigs.Count; configIndex++) { if (PCM.configFile == dtcSearchConfigs[configIndex].XMLFile.ToLower()) { searchStr = dtcSearchConfigs[configIndex].CodeSearch; startAddr = 0; condOffset = false; if (dtcSearchConfigs[configIndex].ConditionalOffset.Contains("code")) { condOffset = true; } codeAddr = getAddrbySearchString(PCM, searchStr, ref startAddr, PCM.fsize, condOffset).Addr; //Check if we found status table, too: startAddr = 0; condOffset = false; if (dtcSearchConfigs[configIndex].ConditionalOffset.Contains("status")) { condOffset = true; } statusAddr = getAddrbySearchString(PCM, dtcSearchConfigs[configIndex].StatusSearch, ref startAddr, PCM.fsize, condOffset).Addr; if (codeAddr < PCM.fsize && statusAddr < PCM.fsize) { Debug.WriteLine("Code search string: " + searchStr); Debug.WriteLine("DTC code table address: " + codeAddr.ToString("X")); codeAddr = (uint)(codeAddr + dtcSearchConfigs[configIndex].CodeOffset); if (PCM.buf[codeAddr] > 0x10 && dtcSearchConfigs[configIndex].ConditionalOffset.Contains("code")) { codeAddr += 0x10000; //HACK: Don't know when should use -0x10000 offset, check if first PID is < 0x10 } statusAddr = (uint)(statusAddr + dtcSearchConfigs[configIndex].StatusOffset); Debug.WriteLine("DTC status table address: " + statusAddr.ToString("X")); break; } else { codeAddr = 0; } } } if (codeAddr == uint.MaxValue) { if (PCM.configFile == "e38" || PCM.configFile == "e67") { PCM.dtcCombined = true; string retval = SearchDtcE38(PCM); return(retval); } return("DTC search: can't find DTC code table"); } //Read codes: bool dCodes = false; for (uint addr = codeAddr; addr < PCM.fsize; addr += (uint)dtcSearchConfigs[configIndex].CodeSteps) { dtcCode dtc = new dtcCode(); dtc.codeAddrInt = addr; dtc.CodeAddr = addr.ToString("X8"); dtc.codeInt = BEToUint16(PCM.buf, addr); string codeTmp = dtc.codeInt.ToString("X"); if (dCodes && !codeTmp.StartsWith("D") || (dtc.codeInt < 10 && PCM.dtcCodes.Count > 10)) { break; } codeTmp = dtc.codeInt.ToString("X4"); dtc.Code = decodeDTC(codeTmp); if (codeTmp.StartsWith("D")) { dCodes = true; } //Find description for code: dtc.Description = getDtcDescription(dtc.Code); PCM.dtcCodes.Add(dtc); } List <uint> milAddrList = new List <uint>(); if (dtcSearchConfigs[configIndex].MilTable == "afterstatus") { milAddr = (uint)(statusAddr + PCM.dtcCodes.Count + (uint)dtcSearchConfigs[configIndex].MilOffset); if (PCM.configFile == "p01-p59" && PCM.buf[milAddr - 1] == 0xFF) { milAddr++; //P59 hack: If there is FF before first byte, skip first byte } milAddrList.Add(milAddr); } else if (dtcSearchConfigs[configIndex].MilTable == "combined") { //Do nothing for now milAddr = 0; milAddrList.Add(milAddr); PCM.dtcCombined = true; } else { //Search MIL table startAddr = 0; for (int i = 0; i < 30; i++) { condOffset = false; if (dtcSearchConfigs[configIndex].ConditionalOffset.Contains("mil")) { condOffset = true; } milAddr = getAddrbySearchString(PCM, dtcSearchConfigs[configIndex].MilSearch, ref startAddr, PCM.fsize, condOffset).Addr; if (milAddr < uint.MaxValue) { milAddr += (uint)dtcSearchConfigs[configIndex].MilOffset; if (milAddr < PCM.fsize) //Hit { milAddrList.Add(milAddr); //break; } } else { //Not found break; } } } if (milAddrList.Count > 1) { //IF have multiple hits for search, use table which starts after FF, ends before FF for (int m = 0; m < milAddrList.Count; m++) { Debug.WriteLine("MIL Start: " + (milAddrList[m] - 1).ToString("X")); Debug.WriteLine("MIL End: " + (milAddrList[m] + PCM.dtcCodes.Count).ToString("X")); if (PCM.buf[milAddrList[m] - 2] == 0xFF && PCM.buf[milAddrList[m] + PCM.dtcCodes.Count] == 0xFF) { milAddr = milAddrList[m]; break; } } if (milAddr == uint.MaxValue) { //We didn't found it, use first from list milAddr = milAddrList[0]; } } else { milAddr = milAddrList[0]; } Debug.WriteLine("MIL: " + milAddr.ToString("X")); if (milAddr >= PCM.fsize) { return("DTC search: MIL table address out of address range:" + milAddr.ToString("X8")); } //Read DTC status bytes: int dtcNr = 0; uint addr3 = milAddr; for (uint addr2 = statusAddr; dtcNr < PCM.dtcCodes.Count; addr2 += (uint)dtcSearchConfigs[configIndex].StatusSteps, addr3 += (uint)dtcSearchConfigs[configIndex].MilSteps) { if (PCM.buf[addr2] > 7) { break; } if (!PCM.dtcCombined && PCM.buf[addr2] > 3) //DTC = 0-3 { break; } dtcCode dtc = PCM.dtcCodes[dtcNr]; dtc.statusAddrInt = addr2; //dtc.StatusAddr = addr2.ToString("X8"); byte statusByte = PCM.buf[addr2]; dtc.Status = statusByte; if (PCM.dtcCombined) { dtc.StatusTxt = dtcStatusCombined[dtc.Status]; if (statusByte > 4) { dtc.MilStatus = 1; } else { dtc.MilStatus = 0; } } else { dtc.StatusTxt = dtcStatus[dtc.Status]; //Read MIL bytes: dtc.milAddrInt = addr3; dtc.MilAddr = addr3.ToString("X8"); dtc.MilStatus = PCM.buf[addr3]; } PCM.dtcCodes.RemoveAt(dtcNr); PCM.dtcCodes.Insert(dtcNr, dtc); dtcNr++; } } catch (Exception ex) { var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(st.FrameCount - 1); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); return("DTC search, line " + line + ": " + ex.Message); } return(""); }
//Search GM e38/e67 DTC codes public string SearchDtcE38(PcmFile PCM) { try { if (PCM.OSSegment == -1) { return("DTC search: No OS segment??"); } if (PCM.diagSegment == -1) { return("DTC search: No Diagnostic segment??"); } //Get codes from OS segment: string searchStr = "3D 80 * * 39 8C * * 7D 7B DA 14 7C 8C 5A 2E 80 BD"; uint opCodeAddr = uint.MaxValue; uint tableStart = 0; for (int b = 0; b < PCM.segmentAddressDatas[PCM.OSSegment].SegmentBlocks.Count; b++) { opCodeAddr = searchBytes(PCM, searchStr, PCM.segmentAddressDatas[PCM.OSSegment].SegmentBlocks[b].Start, PCM.segmentAddressDatas[PCM.OSSegment].SegmentBlocks[b].End); if (opCodeAddr < uint.MaxValue) { ushort highBytes = BEToUint16(PCM.buf, (uint)(opCodeAddr + 2)); ushort lowBytes = BEToUint16(PCM.buf, (uint)(opCodeAddr + 6)); tableStart = (uint)(highBytes << 16 | lowBytes); ushort tmp = (ushort)(opCodeAddr & 0xffff); if (tmp > 0x5000) { tableStart -= 0x10000; //Some kind of address offset } bool dCodes = false; for (uint addr = tableStart; addr < PCM.fsize; addr += 2) { dtcCode dtc = new dtcCode(); dtc.codeAddrInt = addr; dtc.CodeAddr = addr.ToString("X8"); dtc.codeInt = BEToUint16(PCM.buf, addr); string codeTmp = dtc.codeInt.ToString("X4"); if (dCodes && !codeTmp.StartsWith("D")) { break; } if (codeTmp.StartsWith("D")) { dCodes = true; } dtc.Code = decodeDTC(codeTmp); dtc.Description = getDtcDescription(dtc.Code); PCM.dtcCodes.Add(dtc); } break; } } int dtcCount = PCM.dtcCodes.Count; //Search by opcode: opCodeAddr = searchBytes(PCM, "3C A0 * * 38 A5 * * 7D 85 50", 0, PCM.fsize); if (opCodeAddr < uint.MaxValue) { ushort highBytes = BEToUint16(PCM.buf, (uint)(opCodeAddr + 2)); ushort lowBytes = BEToUint16(PCM.buf, (uint)(opCodeAddr + 6)); tableStart = (uint)(highBytes << 16 | lowBytes); } if (tableStart > 0) { int dtcNr = 0; for (uint addr2 = tableStart; addr2 < tableStart + dtcCount; addr2++) { if (PCM.buf[addr2] == 0xFF) { return(""); } dtcCode dtc = PCM.dtcCodes[dtcNr]; dtc.statusAddrInt = addr2; //dtc.StatusAddr = addr2.ToString("X8"); //dtc.Status = PCM.buf[addr2]; byte statusByte = PCM.buf[addr2]; if (statusByte > 3) { dtc.MilStatus = 1; } else { dtc.MilStatus = 0; } dtc.Status = statusByte; dtc.StatusTxt = dtcStatusCombined[dtc.Status]; PCM.dtcCodes.RemoveAt(dtcNr); PCM.dtcCodes.Insert(dtcNr, dtc); dtcNr++; } } } catch (Exception ex) { var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(st.FrameCount - 1); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); return("DTC search, line " + line + ": " + ex.Message); } return(""); }
public void importDTC(PcmFile PCM, ref List <TableData> tdList) { if (PCM.dtcCodes.Count == 0) { DtcSearch DS = new DtcSearch(); Logger(DS.searchDtc(PCM)); } if (PCM.dtcCodes.Count == 0) { return; } TableData dtcTd = new TableData(); dtcCode dtc = PCM.dtcCodes[0]; dtcTd.Origin = "seek"; dtcTd.addrInt = dtc.statusAddrInt; dtcTd.Category = "DTC"; dtcTd.Columns = 1; //td.Floating = false; dtcTd.OutputType = OutDataType.Int; dtcTd.Decimals = 0; dtcTd.DataType = InDataType.UBYTE; dtcTd.Math = "X"; dtcTd.OS = PCM.OS; for (int i = 0; i < PCM.dtcCodes.Count; i++) { dtcTd.RowHeaders += PCM.dtcCodes[i].Code + ","; } dtcTd.RowHeaders = dtcTd.RowHeaders.Trim(','); dtcTd.Rows = (ushort)PCM.dtcCodes.Count; //dtcTd.SavingMath = "X"; if (PCM.dtcCombined) { //td.TableDescription = "00 MIL and reporting off, 01 type A/no mil, 02 type B/no mil, 03 type C/no mil, 04 not reported/mil, 05 type A/mil, 06 type B/mil, 07 type c/mil"; dtcTd.Values = "Enum: 00:MIL and reporting off,01:type A/no mil,02:type B/no mil,03:type C/no mil,04:not reported/mil,05:type A/mil,06:type B/mil,07:type c/mil"; dtcTd.TableName = "DTC"; } else { //td.TableDescription = "0 = 1 Trip, Emissions Related (MIL will illuminate IMMEDIATELY), 1 = 2 Trips, Emissions Related (MIL will illuminate if the DTC is active for two consecutive drive cycles), 2 = Non Emssions (MIL will NOT be illuminated, but the PCM will store the DTC), 3 = Not Reported (the DTC test/algorithm is NOT functional, i.e. the DTC is Disabled)"; dtcTd.Values = "Enum: 0:1 Trip (MIL IMMEDIATELY),1:2 Trips (MIL if DTC active two drive cycles),2:(No MIL store DTC),3:Not Reported (DTC Disabled)"; dtcTd.TableName = "DTC.Codes"; } tdList.Insert(0, dtcTd); if (!PCM.dtcCombined) { dtcTd = new TableData(); dtcTd.TableName = "DTC.MIL_Enable"; dtcTd.addrInt = dtc.milAddrInt; dtcTd.Category = "DTC"; dtcTd.Origin = "seek"; //td.ColumnHeaders = "MIL"; dtcTd.Columns = 1; dtcTd.OutputType = OutDataType.Flag; dtcTd.Decimals = 0; dtcTd.DataType = InDataType.UBYTE; dtcTd.Math = "X"; dtcTd.OS = PCM.OS; for (int i = 0; i < PCM.dtcCodes.Count; i++) { dtcTd.RowHeaders += PCM.dtcCodes[i].Code + ","; } dtcTd.RowHeaders = dtcTd.RowHeaders.Trim(','); dtcTd.Rows = (ushort)PCM.dtcCodes.Count; //dtcTd.SavingMath = "X"; //td.Signed = false; dtcTd.TableDescription = "0 = No MIL (Lamp always off) 1 = MIL (Lamp may be commanded on by PCM)"; //td.Values = "Enum: 0:No MIL (Lamp always off),1:MIL (Lamp may be commanded on by PCM)"; tdList.Insert(1, dtcTd); } if (!PCM.tableCategories.Contains("DTC")) { PCM.tableCategories.Add("DTC"); } }
private bool CheckRule(DetectRule DR, PcmFile PCM) { try { UInt64 Data = 0; uint Addr = 0; if (DR.address == "filesize") { Data = (UInt64) new FileInfo(PCM.FileName).Length; } else { string[] Parts = DR.address.Split(':'); HexToUint(Parts[0].Replace("@", ""), out Addr); if (DR.address.StartsWith("@")) { Addr = BEToUint32(PCM.buf, Addr); } if (Parts[0].EndsWith("@")) { Addr = (uint)PCM.buf.Length - Addr; } if (Parts.Length == 1) { Data = BEToUint16(PCM.buf, Addr); } else { if (Parts[1] == "1") { Data = (uint)PCM.buf[Addr]; } if (Parts[1] == "2") { Data = (uint)BEToUint16(PCM.buf, Addr); } if (Parts[1] == "4") { Data = BEToUint32(PCM.buf, Addr); } if (Parts[1] == "8") { Data = BEToUint64(PCM.buf, Addr); } } } //Logger(DR.xml + ": " + DR.address + ": " + DR.data.ToString("X") + DR.compare + "(" + DR.grouplogic + ") " + " [" + Addr.ToString("X") + ": " + Data.ToString("X") + "]"); if (DR.compare == "==") { if (Data == DR.data) { return(true); } } if (DR.compare == "<") { if (Data < DR.data) { return(true); } } if (DR.compare == ">") { if (Data > DR.data) { return(true); } } if (DR.compare == "!=") { if (Data != DR.data) { return(true); } } //Logger("Not match"); return(false); } catch (Exception ex) { //Something wrong, just skip this part and continue Debug.WriteLine(ex.Message); return(false); } }