/
EDC16FileParser.cs
406 lines (377 loc) · 18.3 KB
/
EDC16FileParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace VAGSuite
{
public class EDC16FileParser : IEDCFileParser
{
public override SymbolCollection parseFile(string filename, out List<CodeBlock> newCodeBlocks, out List<AxisHelper> newAxisHelpers)
{
newCodeBlocks = new List<CodeBlock>();
SymbolCollection newSymbols = new SymbolCollection();
newAxisHelpers = new List<AxisHelper>();
// Bosch EDC16 style mapdetection LL LL AXIS AXIS MAPDATA
byte[] allBytes = File.ReadAllBytes(filename);
for (int i = 0; i < allBytes.Length - 32; i+=2)
{
int len2Skip = CheckMap(i, allBytes, newSymbols, newCodeBlocks);
if ((len2Skip % 2) > 0)
{
if (len2Skip > 2) len2Skip--;
else len2Skip++;
}
i += len2Skip;
}
newSymbols.SortColumn = "Flash_start_address";
newSymbols.SortingOrder = GenericComparer.SortOrder.Ascending;
newSymbols.Sort();
NameKnownMaps(allBytes, newSymbols, newCodeBlocks);
FindSVBL(allBytes, filename, newSymbols, newCodeBlocks);
SymbolTranslator strans = new SymbolTranslator();
foreach (SymbolHelper sh in newSymbols)
{
sh.Description = strans.TranslateSymbolToHelpText(sh.Varname);
}
return newSymbols;
}
private int CheckMap(int t, byte[] allBytes, SymbolCollection newSymbols, List<CodeBlock> newCodeBlocks)
{
int retval = 0;
// read LL LL
int len1 = Convert.ToInt32(allBytes[t]) * 256 + Convert.ToInt32(allBytes[t + 1]);
int len2 = Convert.ToInt32(allBytes[t + 2]) * 256 + Convert.ToInt32(allBytes[t + 3]);
if (len1 < 32 && len2 < 32 && len1 > 0 && len2 > 0)
{
SymbolHelper sh = new SymbolHelper();
sh.X_axis_address = t + 4;
sh.X_axis_length = len1;
sh.Y_axis_address = sh.X_axis_address + sh.X_axis_length * 2;
sh.Y_axis_length = len2;
sh.Flash_start_address = sh.Y_axis_address + sh.Y_axis_length * 2;
sh.Length = sh.X_axis_length * sh.Y_axis_length * 2;
if (sh.X_axis_length > 1 && sh.Y_axis_length > 1)
{
sh.Varname = "3D " + sh.Flash_start_address.ToString("X8");
}
else
{
sh.Varname = "2D " + sh.Flash_start_address.ToString("X8");
}
AddToSymbolCollection(newSymbols, sh, newCodeBlocks);
retval = (len1 + len2) * 2 + sh.Length ;
}
return retval;
}
private bool AddToSymbolCollection(SymbolCollection newSymbols, SymbolHelper newSymbol, List<CodeBlock> newCodeBlocks)
{
if (newSymbol.Length >= 800) return false;
foreach (SymbolHelper sh in newSymbols)
{
if (sh.Flash_start_address == newSymbol.Flash_start_address)
{
// Console.WriteLine("Already in collection: " + sh.Flash_start_address.ToString("X8"));
return false;
}
}
newSymbols.Add(newSymbol);
newSymbol.CodeBlock = DetermineCodeBlockByByAddress(newSymbol.Flash_start_address, newCodeBlocks);
return true;
}
private int DetermineCodeBlockByByAddress(long address, List<CodeBlock> currBlocks)
{
foreach (CodeBlock cb in currBlocks)
{
if (cb.StartAddress <= address && cb.EndAddress >= address)
{
return cb.CodeID;
}
}
return 0;
}
public override string ExtractInfo(byte[] allBytes)
{
// assume info will be @ 0x53452 12 bytes
string retval = string.Empty;
try
{
int partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[5] { 0x45, 0x44, 0x43, 0x20, 0x20 }, new byte[5] { 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress - 8, 12).Trim();
}
}
catch (Exception)
{
}
return retval;
}
public override string ExtractPartnumber(byte[] allBytes)
{
// assume info will be @ 0x53446 12 bytes
string retval = string.Empty;
try
{
int partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[5] { 0x45, 0x44, 0x43, 0x20, 0x20 }, new byte[5] { 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress - 20, 12).Trim();
}
}
catch (Exception)
{
}
return retval;
}
public override string ExtractSoftwareNumber(byte[] allBytes)
{
string retval = string.Empty;
try
{
int partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[5] { 0x45, 0x44, 0x43, 0x20, 0x20 }, new byte[5] { 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress + 5, 8).Trim();
retval = retval.Replace(" ", "");
}
}
catch (Exception)
{
}
return retval;
}
public override string ExtractBoschPartnumber(byte[] allBytes)
{
return Tools.Instance.ExtractBoschPartnumber(allBytes);
string retval = string.Empty;
try
{
int partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[5] { 0x45, 0x44, 0x43, 0x20, 0x20 }, new byte[5] { 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress + 23, 10).Trim();
}
}
catch (Exception)
{
}
return retval;
}
public override void FindSVBL(byte[] allBytes, string filename, SymbolCollection newSymbols, List<CodeBlock> newCodeBlocks)
{
}
private string DetermineNumberByFlashBank(long address, List<CodeBlock> currBlocks)
{
foreach (CodeBlock cb in currBlocks)
{
if (cb.StartAddress <= address && cb.EndAddress >= address)
{
if (cb.CodeID == 1) return "codeblock 1";// - MAN";
if (cb.CodeID == 2) return "codeblock 2";// - AUT (hydr)";
if (cb.CodeID == 3) return "codeblock 3";// - AUT (elek)";
return cb.CodeID.ToString();
}
}
long bankNumber = address / 0x10000;
return "flashbank " + bankNumber.ToString();
}
public override void NameKnownMaps(byte[] allBytes, SymbolCollection newSymbols, List<CodeBlock> newCodeBlocks)
{
SymbolAxesTranslator st = new SymbolAxesTranslator();
foreach (SymbolHelper sh in newSymbols)
{
// sh.X_axis_descr = st.TranslateAxisID(sh.X_axis_ID);
// sh.Y_axis_descr = st.TranslateAxisID(sh.Y_axis_ID);
if (sh.X_axis_length == 4 && sh.Y_axis_length == 20)
{
sh.Category = "Detected maps";
sh.Subcategory = "Limiters";
//int lmCount = GetMapNameCountForCodeBlock("Torque limiter", sh.CodeBlock, newSymbols, false);
sh.Varname = "Torque limiter [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Z_axis_descr = "Maximum IQ (mg)";
//sh.Y_axis_descr = "Atm. pressure (mbar)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.Correction = 0.01;
sh.YaxisUnits = "rpm";
}
if (sh.X_axis_length == 4 && sh.Y_axis_length == 21)
{
sh.Category = "Detected maps";
sh.Subcategory = "Limiters";
//int lmCount = GetMapNameCountForCodeBlock("Torque limiter", sh.CodeBlock, newSymbols, false);
sh.Varname = "Torque limiter [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Z_axis_descr = "Maximum IQ (mg)";
//sh.Y_axis_descr = "Atm. pressure (mbar)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.Correction = 0.01;
sh.YaxisUnits = "rpm";
}
else if (sh.X_axis_length == 3 && sh.Y_axis_length == 21)
{
if (!CollectionContainsMapInSize(newSymbols, 21, 4))
{
sh.Category = "Detected maps";
sh.Subcategory = "Limiters";
//int lmCount = GetMapNameCountForCodeBlock("Torque limiter", sh.CodeBlock, newSymbols, false);
sh.Varname = "Torque limiter [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Z_axis_descr = "Maximum IQ (mg)";
//sh.Y_axis_descr = "Atm. pressure (mbar)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.Correction = 0.01;
sh.YaxisUnits = "rpm";
}
}
else if (sh.X_axis_length == 16 && sh.Y_axis_length == 8)
{
sh.Category = "Detected maps";
sh.Subcategory = "Misc";
int dwCount = GetMapNameCountForCodeBlock("Driver wish ", sh.CodeBlock, newSymbols, false);
sh.Varname = "Driver wish (" + dwCount.ToString() + ") [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Correction = 0.01;
sh.X_axis_correction = 0.01;
sh.X_axis_descr = "Throttle position";
sh.Z_axis_descr = "Requested IQ (mg)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.YaxisUnits = "rpm";
sh.XaxisUnits = "TPS %";
}
else if (sh.X_axis_length == 15 && sh.Y_axis_length == 16)
{
sh.Category = "Detected maps";
sh.Subcategory = "Misc";
sh.Varname = "IQ to Torque conversion [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Correction = 0.01;
sh.X_axis_correction = 0.1;
sh.X_axis_descr = "Torque";
sh.Z_axis_descr = "IQ (mg)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.YaxisUnits = "rpm";
sh.XaxisUnits = "Nm";
}
else if (sh.X_axis_length == 11 && sh.Y_axis_length == 10)
{
sh.Category = "Detected maps";
sh.Subcategory = "Limiters";
sh.Varname = "Boost limit map [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";// " + sh.Flash_start_address.ToString("X8") + " " + sh.X_axis_ID.ToString("X4") + " " + sh.Y_axis_ID.ToString("X4");
// sh.Correction = 0.01;
//sh.X_axis_correction = 0.01;
sh.X_axis_descr = "Atmospheric pressure (mbar)";
sh.Z_axis_descr = "Maximum boost pressure (mbar)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.YaxisUnits = "rpm";
sh.XaxisUnits = "mbar";
}
else if (sh.X_axis_length == 19 && sh.Y_axis_length == 15)
{
sh.Category = "Detected maps";
sh.Subcategory = "Fuel";
int injDurCount = GetMapNameCountForCodeBlock("Injector duration", sh.CodeBlock, newSymbols, false);
sh.Varname = "Injector duration " + injDurCount.ToString("D2") + " [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";// " + sh.Flash_start_address.ToString("X8") + " " + sh.X_axis_ID.ToString("X4") + " " + sh.Y_axis_ID.ToString("X4");
sh.Y_axis_correction = 0.01; // TODO: Check for x or y
sh.Correction = 0.023437;
sh.X_axis_descr = "Engine speed (rpm)";
//sh.Y_axis_descr = "Airflow mg/stroke";
sh.Y_axis_descr = "Requested Quantity mg/stroke";
sh.Z_axis_descr = "Duration (crankshaft degrees)";
sh.XaxisUnits = "rpm";
sh.YaxisUnits = "mg/st";
}
else if (sh.X_axis_length == 16 && sh.Y_axis_length == 12)
{
//int xmax = GetMaxAxisValue(allBytes, sh, MapViewerEx.AxisIdent.X_Axis);
//int ymax = GetMaxAxisValue(allBytes, sh, MapViewerEx.AxisIdent.Y_Axis);
//Console.WriteLine(xmax.ToString() + " " + ymax.ToString() + " " + sh.Flash_start_address.ToString("X8"));
if (GetMaxAxisValue(allBytes, sh, MapViewerEx.AxisIdent.Y_Axis) < 4000)
{
sh.Category = "Detected maps";
sh.Subcategory = "Limiters";
sh.Varname = "Smoke limiter [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Z_axis_descr = "Maximum IQ (mg)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.X_axis_descr = "Manifold pressure (mbar)";
sh.Correction = 0.01;
//sh.X_axis_correction = 0.1; // TODO: Check for x or y
sh.YaxisUnits = "rpm";
sh.XaxisUnits = "mbar";
}
}
else if (sh.X_axis_length == 16 && sh.Y_axis_length == 14)
{
// SOI
sh.Category = "Detected maps";
sh.Subcategory = "Fuel";
int injDurCount = GetMapNameCountForCodeBlock("Start of injection (SOI)", sh.CodeBlock, newSymbols, false);
//Console.WriteLine("Temperature switch for SOI " + injDurCount.ToString() + " " + tempRange.ToString());
//sh.Varname = "Start of injection (SOI) " + injDurCount.ToString("D2") + " [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";// " + sh.Flash_start_address.ToString("X8") + " " + sh.X_axis_ID.ToString("X4") + " " + sh.Y_axis_ID.ToString("X4");
sh.Varname = "Start of injection (SOI) " + injDurCount.ToString() + " [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";// " + sh.Flash_start_address.ToString("X8") + " " + sh.X_axis_ID.ToString("X4") + " " + sh.Y_axis_ID.ToString("X4");
sh.Correction = 0.023437;
//sh.Offset = 78;
sh.Y_axis_descr = "Engine speed (rpm)";
sh.YaxisUnits = "rpm";
sh.X_axis_correction = 0.01; // TODODONE : Check for x or y
sh.XaxisUnits = "mg/st";
sh.X_axis_descr = "IQ (mg/stroke)";
sh.Z_axis_descr = "Start position (degrees BTDC)";
}
}
}
private int GetMaxAxisValue(byte[] allBytes, SymbolHelper sh, MapViewerEx.AxisIdent axisIdent)
{
int retval = 0;
if (axisIdent == MapViewerEx.AxisIdent.X_Axis)
{
//read x axis values
int offset = sh.X_axis_address;
for (int i = 0; i < sh.X_axis_length; i++)
{
int val = Convert.ToInt32(allBytes[offset+1]) + Convert.ToInt32(allBytes[offset]) * 256;
if (val > retval) retval = val;
offset += 2;
}
}
else if (axisIdent == MapViewerEx.AxisIdent.Y_Axis)
{
//read x axis values
int offset = sh.Y_axis_address;
for (int i = 0; i < sh.Y_axis_length; i++)
{
int val = Convert.ToInt32(allBytes[offset+1]) + Convert.ToInt32(allBytes[offset]) * 256;
if (val > retval) retval = val;
offset += 2;
}
}
return retval;
}
private bool CollectionContainsMapInSize(SymbolCollection newSymbols, int ysize, int xsize)
{
foreach (SymbolHelper sh in newSymbols)
{
if (sh.Y_axis_length == ysize && sh.X_axis_length == xsize) return true;
}
return false;
}
private int GetMapNameCountForCodeBlock(string varName, int codeBlock, SymbolCollection newSymbols, bool debug)
{
int count = 0;
if (debug) Console.WriteLine("Check " + varName + " " + codeBlock);
foreach (SymbolHelper sh in newSymbols)
{
if (debug)
{
if (!sh.Varname.StartsWith("2D") && !sh.Varname.StartsWith("3D"))
{
Console.WriteLine(sh.Varname + " " + sh.CodeBlock);
}
}
if (sh.Varname.StartsWith(varName) && sh.CodeBlock == codeBlock)
{
if (debug) Console.WriteLine("Found " + sh.Varname + " " + sh.CodeBlock);
count++;
}
}
count++;
return count;
}
}
}