forked from gmenounos/kw1281test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
453 lines (395 loc) · 14.9 KB
/
Copy pathProgram.cs
File metadata and controls
453 lines (395 loc) · 14.9 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
global using static BitFab.KW1281Test.Program;
using BitFab.KW1281Test.Interface;
using BitFab.KW1281Test.Logging;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
namespace BitFab.KW1281Test
{
class Program
{
public static ILog Log { get; private set; } = new ConsoleLog();
static void Main(string[] args)
{
try
{
Log = new FileLog("KW1281Test.log");
var tester = new Program();
tester.Run(args);
}
catch (Exception ex)
{
Log.WriteLine($"Caught: {ex.GetType()} {ex.Message}");
Log.WriteLine($"Unhandled exception: {ex}");
}
finally
{
Log.Close();
}
}
void Run(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("KW1281Test: Yesterday's diagnostics...");
Thread.Sleep(2000);
Console.WriteLine("Today.");
Thread.Sleep(2000);
Console.ResetColor();
Console.WriteLine();
var version = GetType().GetTypeInfo().Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!
.InformationalVersion;
Log.WriteLine($"Version {version} (https://github.com/gmenounos/kw1281test/releases)");
Log.WriteLine($"Args: {string.Join(' ', args)}");
Log.WriteLine($"OSVersion: {Environment.OSVersion}");
Log.WriteLine($".NET Version: {Environment.Version}");
Log.WriteLine($"Culture: {CultureInfo.InstalledUICulture}");
if (args.Length < 4)
{
ShowUsage();
return;
}
try
{
// This seems to increase the accuracy of our timing loops
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
}
catch(Win32Exception)
{
// Ignore if we don't have permission to increase our priority
}
string portName = args[0];
var baudRate = int.Parse(args[1]);
int controllerAddress = int.Parse(args[2], NumberStyles.HexNumber);
var command = args[3];
uint address = 0;
uint length = 0;
byte value = 0;
int softwareCoding = 0;
int workshopCode = 0;
byte channel = 0;
ushort channelValue = 0;
ushort? login = null;
byte groupNumber = 0;
if (string.Compare(command, "ReadEeprom", ignoreCase: true) == 0)
{
if (args.Length < 5)
{
ShowUsage();
return;
}
address = Utils.ParseUint(args[4]);
}
else if (string.Compare(command, "DumpMarelliMem", ignoreCase: true) == 0 ||
string.Compare(command, "DumpEeprom", ignoreCase: true) == 0 ||
string.Compare(command, "DumpMem", ignoreCase: true) == 0 ||
string.Compare(command, "DumpRB8Eeprom", ignoreCase: true) == 0)
{
if (args.Length < 6)
{
ShowUsage();
return;
}
address = Utils.ParseUint(args[4]);
length = Utils.ParseUint(args[5]);
if (args.Length > 6)
{
_filename = args[6];
}
}
else if (string.Compare(command, "WriteEeprom", ignoreCase: true) == 0)
{
if (args.Length < 6)
{
ShowUsage();
return;
}
address = Utils.ParseUint(args[4]);
value = (byte)Utils.ParseUint(args[5]);
}
else if (string.Compare(command, "LoadEeprom", ignoreCase: true) == 0)
{
if (args.Length < 6)
{
ShowUsage();
return;
}
address = Utils.ParseUint(args[4]);
_filename = args[5];
}
else if (string.Compare(command, "SetSoftwareCoding", ignoreCase: true) == 0)
{
if (args.Length < 6)
{
ShowUsage();
return;
}
softwareCoding = (int)Utils.ParseUint(args[4]);
if (softwareCoding > 32767)
{
Log.WriteLine("SoftwareCoding cannot be greater than 32767.");
return;
}
workshopCode = (int)Utils.ParseUint(args[5]);
if (workshopCode > 99999)
{
Log.WriteLine("WorkshopCode cannot be greater than 99999.");
return;
}
}
else if (string.Compare(command, "DumpEdc15Eeprom", ignoreCase: true) == 0)
{
if (args.Length < 4)
{
ShowUsage();
return;
}
if (args.Length > 4)
{
_filename = args[4];
}
}
else if (string.Compare(command, "AdaptationRead", ignoreCase: true) == 0)
{
if (args.Length < 5)
{
ShowUsage();
return;
}
channel = byte.Parse(args[4]);
if (args.Length > 5)
{
login = ushort.Parse(args[5]);
}
}
else if (
string.Compare(command, "AdaptationSave", ignoreCase: true) == 0 ||
string.Compare(command, "AdaptationTest", ignoreCase: true) == 0)
{
if (args.Length < 6)
{
ShowUsage();
return;
}
channel = byte.Parse(args[4]);
channelValue = ushort.Parse(args[5]);
if (args.Length > 6)
{
login = ushort.Parse(args[6]);
}
}
else if (
string.Compare(command, "BasicSetting", ignoreCase: true) == 0 ||
string.Compare(command, "GroupRead", ignoreCase: true) == 0)
{
if (args.Length < 5)
{
ShowUsage();
return;
}
groupNumber = byte.Parse(args[4]);
}
else if (
string.Compare(command, "FindLogins", ignoreCase: true) == 0)
{
if (args.Length < 5)
{
ShowUsage();
return;
}
login = ushort.Parse(args[4]);
}
using var @interface = OpenPort(portName, baudRate);
var tester = new Tester(@interface, controllerAddress);
switch (command.ToLower())
{
case "dumprb8eeprom":
tester.DumpRB8Eeprom(address, length, _filename);
tester.EndCommunication();
return;
case "getskc":
tester.GetSkc();
tester.EndCommunication();
return;
default:
break;
}
ControllerInfo ecuInfo = tester.Kwp1281Wakeup();
switch (command.ToLower())
{
case "actuatortest":
tester.ActuatorTest();
break;
case "adaptationread":
tester.AdaptationRead(channel, login, ecuInfo.WorkshopCode);
break;
case "adaptationsave":
tester.AdaptationSave(channel, channelValue, login, ecuInfo.WorkshopCode);
break;
case "adaptationtest":
tester.AdaptationTest(channel, channelValue, login, ecuInfo.WorkshopCode);
break;
case "basicsetting":
tester.BasicSettingRead(groupNumber);
break;
case "clarionvwpremium4safecode":
tester.ClarionVWPremium4SafeCode();
break;
case "clearfaultcodes":
tester.ClearFaultCodes();
break;
case "delcovwpremium5safecode":
tester.DelcoVWPremium5SafeCode();
break;
case "dumpccmrom":
tester.DumpCcmRom(_filename);
break;
case "dumpclusternecrom":
tester.DumpClusterNecRom(_filename);
break;
case "dumpedc15eeprom":
tester.DumpEdc15Eeprom(_filename);
break;
case "dumpeeprom":
tester.DumpEeprom(address, length, _filename);
break;
case "dumpmarellimem":
tester.DumpMarelliMem(address, length, ecuInfo, _filename);
return;
case "dumpmem":
tester.DumpMem(address, length, _filename);
break;
case "findlogins":
tester.FindLogins(login!.Value, ecuInfo.WorkshopCode);
break;
case "groupread":
tester.GroupRead(groupNumber);
break;
case "loadeeprom":
tester.LoadEeprom(address, _filename!);
break;
case "mapeeprom":
tester.MapEeprom(_filename);
break;
case "readeeprom":
tester.ReadEeprom(address);
break;
case "readfaultcodes":
tester.ReadFaultCodes();
break;
case "readident":
tester.ReadIdent();
break;
case "readsoftwareversion":
tester.ReadSoftwareVersion();
break;
case "reset":
tester.Reset();
break;
case "setsoftwarecoding":
tester.SetSoftwareCoding(softwareCoding, workshopCode);
break;
case "writeeeprom":
tester.WriteEeprom(address, value);
break;
default:
ShowUsage();
break;
}
tester.EndCommunication();
}
/// <summary>
/// Opens the serial port.
/// </summary>
/// <param name="portName">
/// Either the device name of a serial port (e.g. COM1, /dev/tty23)
/// or an FTDI USB->Serial device serial number (2 letters followed by 6 letters/numbers).
/// </param>
/// <param name="baudRate"></param>
/// <returns></returns>
private static IInterface OpenPort(string portName, int baudRate)
{
if (Regex.IsMatch(portName.ToUpper(), @"\A[A-Z0-9]{8}\Z"))
{
Log.WriteLine($"Opening FTDI serial port {portName}");
return new FtdiInterface(portName, baudRate);
}
else
{
Log.WriteLine($"Opening serial port {portName}");
return new GenericInterface(portName, baudRate);
}
}
private static void ShowUsage()
{
Log.WriteLine(@"Usage: KW1281Test PORT BAUD ADDRESS COMMAND [args]
PORT = COM1|COM2|etc.
BAUD = 10400|9600|etc.
ADDRESS = The controller address, e.g. 1 (ECU), 17 (cluster), 46 (CCM), 56 (radio)
COMMAND =
ActuatorTest
AdaptationRead CHANNEL [LOGIN]
CHANNEL = Channel number (0-99)
LOGIN = Optional login (0-65535)
AdaptationSave CHANNEL VALUE [LOGIN]
CHANNEL = Channel number (0-99)
VALUE = Channel value (0-65535)
LOGIN = Optional login (0-65535)
AdaptationTest CHANNEL VALUE [LOGIN]
CHANNEL = Channel number (0-99)
VALUE = Channel value (0-65535)
LOGIN = Optional login (0-65535)
BasicSetting GROUP
GROUP = Group number (0-255)
(Group 0: Raw controller data)
ClarionVWPremium4SafeCode
ClearFaultCodes
DelcoVWPremium5SafeCode
DumpEdc15Eeprom [FILENAME]
FILENAME = Optional filename
DumpEeprom START LENGTH [FILENAME]
START = Start address in decimal (e.g. 0) or hex (e.g. $0)
LENGTH = Number of bytes in decimal (e.g. 2048) or hex (e.g. $800)
FILENAME = Optional filename
DumpMarelliMem START LENGTH [FILENAME]
START = Start address in decimal (e.g. 3072) or hex (e.g. $C00)
LENGTH = Number of bytes in decimal (e.g. 1024) or hex (e.g. $400)
FILENAME = Optional filename
DumpMem START LENGTH [FILENAME]
START = Start address in decimal (e.g. 8192) or hex (e.g. $2000)
LENGTH = Number of bytes in decimal (e.g. 65536) or hex (e.g. $10000)
FILENAME = Optional filename
DumpRB8Eeprom START LENGTH [FILENAME]
START = Start address in decimal (e.g. 66560) or hex (e.g. $10400)
LENGTH = Number of bytes in decimal (e.g. 1024) or hex (e.g. $400)
FILENAME = Optional filename
FindLogins LOGIN
LOGIN = Known good login (0-65535)
GetSKC
GroupRead GROUP
GROUP = Group number (0-255)
(Group 0: Raw controller data)
LoadEeprom START FILENAME
START = Start address in decimal (e.g. 0) or hex (e.g. $0)
FILENAME = Name of file containing binary data to load into EEPROM
MapEeprom
ReadFaultCodes
ReadIdent
ReadEeprom ADDRESS
ADDRESS = Address in decimal (e.g. 4361) or hex (e.g. $1109)
ReadSoftwareVersion
Reset
SetSoftwareCoding CODING WORKSHOP
CODING = Software coding in decimal (e.g. 4361) or hex (e.g. $1109)
WORKSHOP = Workshop code in decimal (e.g. 4361) or hex (e.g. $1109)
WriteEeprom ADDRESS VALUE
ADDRESS = Address in decimal (e.g. 4361) or hex (e.g. $1109)
VALUE = Value in decimal (e.g. 138) or hex (e.g. $8A)");
}
private string? _filename = null;
}
}