Skip to content

Commit c3e64d4

Browse files
committed
Fix KW2C3PE for Scanmatik, Add security automatic login
Also fixes cross-thread stringbuilder access when logging
1 parent 061bcfd commit c3e64d4

13 files changed

Lines changed: 230 additions & 45 deletions

File tree

Caesar/Caesar/DiagService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ public enum ServiceType
106106
public List<List<DiagPreparation>> OutputPreparations = new List<List<DiagPreparation>>();
107107
public List<ComParameter> DiagComParameters = new List<ComParameter>();
108108

109+
public ECU ParentECU;
110+
109111
public DiagService(BinaryReader reader, CTFLanguage language, long baseAddress, int poolIndex, ECU parentEcu)
110112
{
113+
ParentECU = parentEcu;
111114
PoolIndex = poolIndex;
112115
BaseAddress = baseAddress;
113116
reader.BaseStream.Seek(baseAddress, SeekOrigin.Begin);

Caesar/Diogenes/DiagnosticProtocol/KW2C3PE.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ private static bool GetVariantID(ECUConnection connection, out int variantId)
6767
variantId = idFor1A87;
6868
return true;
6969
}
70-
71-
Console.WriteLine($"Failed to identify variant.");
7270
variantId = 0;
7371
return false;
7472
}
@@ -83,9 +81,11 @@ public override void ConnectionEstablishedHandler(ECUConnection connection)
8381
{
8482
connection.VariantIsAvailable = true;
8583
connection.ECUVariantID = variantId;
84+
Console.WriteLine($"Variant has been successfully configured as {(variantId & 0xFFFF):X4}");
8685
}
8786
else
8887
{
88+
Console.WriteLine("KW2C3PE: Could not identify variant (1A86, 1A87)");
8989
return;
9090
}
9191
}

Caesar/Diogenes/DiagnosticProtocol/UDS.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public override void ConnectionEstablishedHandler(ECUConnection connection)
197197
{
198198
connection.VariantIsAvailable = true;
199199
connection.ECUVariantID = variantId;
200+
Console.WriteLine($"Variant has been successfully configured as {(variantId & 0xFFFF):X4}");
200201
}
201202
else
202203
{

Caesar/Diogenes/Diogenes.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<Compile Include="Forms\SecurityLevelForm.Designer.cs">
122122
<DependentUpon>SecurityLevelForm.cs</DependentUpon>
123123
</Compile>
124+
<Compile Include="SecurityAccess\SecurityAutoLogin.cs" />
124125
<Compile Include="TextboxWriter.cs" />
125126
<Compile Include="Forms\TraceForm.cs">
126127
<SubType>Form</SubType>

Caesar/Diogenes/ECUConnection.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class ECUConnection
6161
public API ConnectionAPI;
6262
public Device ConnectionDevice;
6363
public Channel ConnectionChannel;
64+
public string DriverPath = "";
6465

6566
public delegate void ConnectionStateChanged(string newStateDescription);
6667
public ConnectionStateChanged ConnectionStateChangeEvent;
@@ -115,9 +116,11 @@ public ECUConnection()
115116

116117
public ECUConnection(string fileName, string friendlyName)
117118
{
119+
DriverPath = fileName;
120+
118121
// apparently AVDI embeds their hardware identifier in the device's name and path, which might be regarded as sensitive when sharing
119122
// this redacts it (somewhat) to help save some time for testers
120-
if (friendlyName.Contains("AVDI-PT"))
123+
if (DriverIsAVDI())
121124
{
122125
FriendlyName = "AVDI-PT";
123126
Console.WriteLine($"Initializing new connection to {friendlyName}");
@@ -222,26 +225,46 @@ public ConnectResponse Connect(ECUInterfaceSubtype profile, ECU ecuContext)
222225
Console.WriteLine($"Target voltage : {ConnectionChannel.MeasureBatteryVoltage()} mV");
223226
ConnectionChannel.DefaultTxFlag = TxFlag.ISO15765_FRAME_PAD;
224227

225-
// this chunk is repeated twice, seems to be required for some j2534 devices, and doesn't harm preexisting, working devices
226228
J2534SetFilters(profile);
227229
J2534SetConfig(profile);
228230
J2534FlushBuffers();
229231

230-
J2534SetFilters(profile);
231-
J2534SetConfig(profile);
232-
J2534FlushBuffers();
233232

234-
State = ConnectionState.ChannelConnectedPendingEcuContact;
235233
}
236234
catch (Exception e)
237235
{
238-
Console.WriteLine($"{e.Message}");
236+
Console.WriteLine($"Connection failed with exception : {e.Message}");
239237
return ConnectResponse.FailedWithException;
240238
}
239+
240+
// this chunk is repeated for AVDI devices; OpenPort2 does not care, Scanmatik refuses to continue if reconfigured without clearing prior filters
241+
// wrap the second attempt in a separate try block, so that we can suppress any potential filter errors
242+
if (DriverIsAVDI())
243+
{
244+
try
245+
{
246+
ConnectionChannel.ClearMsgFilters();
247+
J2534SetFilters(profile);
248+
J2534SetConfig(profile);
249+
J2534FlushBuffers();
250+
}
251+
catch (Exception ex)
252+
{
253+
Console.WriteLine($"AVDI Second config exception suppressed: {ex.Message}");
254+
}
255+
}
256+
257+
State = ConnectionState.ChannelConnectedPendingEcuContact;
258+
241259
ConnectionUpdateState();
242260
return ConnectResponse.OK;
243261
}
244262

263+
public bool DriverIsAVDI()
264+
{
265+
return DriverPath.ToUpper().EndsWith("ABRPT32.DLL");
266+
}
267+
245268
public void J2534SetFilters(ECUInterfaceSubtype profile)
246269
{
247270
// setup ecu filter (mimicking vediamo's behavior)
@@ -256,7 +279,6 @@ public void J2534SetFilters(ECUInterfaceSubtype profile)
256279
MessageFilter filter = new MessageFilter();
257280

258281
// Apparently in the EIS series, the RX identifier is !! NOT !! CanIdentifier+8 per ISO15765, so the automatic config in J2534-Sharp will fail
259-
//filter.StandardISO15765(CanIdentifier);
260282

261283
// manually configure a ISO15765 filter
262284
filter.FilterType = Filter.FLOW_CONTROL_FILTER;
@@ -368,6 +390,7 @@ public byte[] SendMessage(IEnumerable<byte> message, bool quiet = false)
368390
}
369391

370392
GetMessageResults readResult = ConnectionChannel.GetMessage();
393+
371394
if (readResult.Result == ResultCode.STATUS_NOERROR)
372395
{
373396
foreach (Message row in readResult.Messages)

Caesar/Diogenes/Forms/MainForm.Designer.cs

Lines changed: 18 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Caesar/Diogenes/Forms/MainForm.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Diogenes.Properties;
1313
using System.Runtime.InteropServices;
1414
using SAE.J2534;
15+
using Diogenes.SecurityAccess;
1516

1617
namespace Diogenes
1718
{
@@ -167,10 +168,7 @@ private void AddDiagServicesToNode(TreeNode parentNode, ECUVariant variant)
167168
}
168169
}
169170

170-
if (Connection != null)
171-
{
172-
parentNode.Nodes.Add(diagUnlockingOptions);
173-
}
171+
parentNode.Nodes.Add(diagUnlockingOptions);
174172
parentNode.Nodes.Add(diagStoredData);
175173
parentNode.Nodes.Add(diagData);
176174
parentNode.Nodes.Add(diagFunction);
@@ -440,12 +438,14 @@ private void TreeViewDoubleClickCheckIfVariantDiag(TreeNode node)
440438
{
441439
DiagService ds = foundVariant.DiagServices[int.Parse(node.Tag.ToString())];
442440

441+
bool connectionSupportsUnlocking = Connection?.ConnectionProtocol.SupportsUnlocking() ?? false;
442+
443443
// can we help to skip the modal if the ds doesn't require additional user input? common for data, stored data
444444
if ((ds.DataClass_ServiceType == (int)DiagService.ServiceType.StoredData) || (ds.DataClass_ServiceType == (int)DiagService.ServiceType.Data))
445445
{
446446
ExecUserDiagJob(ds.RequestBytes, ds);
447447
}
448-
else if ((Connection.ConnectionProtocol.SupportsUnlocking()) && (ds.RequestBytes.Length == 2) && (ds.RequestBytes[0] == 0x27))
448+
else if (connectionSupportsUnlocking && (ds.RequestBytes.Length == 2) && (ds.RequestBytes[0] == 0x27))
449449
{
450450
// request seed, no need to prompt
451451
ExecUserDiagJob(ds.RequestBytes, ds);
@@ -484,19 +484,7 @@ private void ExecUserDiagJob(byte[] request, DiagService diagService)
484484
// check if the response was an ECU seed
485485
if (Connection.ConnectionProtocol.SupportsUnlocking() && (response.Length >= 2) && (response[0] == 0x67))
486486
{
487-
if (response.Length == 2)
488-
{
489-
Console.WriteLine($"Security level has been successfully changed to 0x{(response[1] - 1):X}");
490-
}
491-
else
492-
{
493-
byte[] seedValue = response.Skip(2).ToArray();
494-
string seedValueAsString = BitUtility.BytesToHex(seedValue, true);
495-
if (MessageBox.Show($"Received a seed value of {seedValueAsString}. \r\nCopy to clipboard?", "Security Access", MessageBoxButtons.YesNo) == DialogResult.Yes)
496-
{
497-
Clipboard.SetText(seedValueAsString);
498-
}
499-
}
487+
SecurityAutoLogin.ReceiveSecurityResponse(response, diagService.ParentECU, Connection);
500488
}
501489
}
502490

@@ -703,6 +691,7 @@ private void tvMain_DoubleClick(object sender, EventArgs e)
703691
else
704692
{
705693
// uhoh
694+
Console.WriteLine($"ECU connection was unsuccessful : {response}");
706695
}
707696
break;
708697
}
@@ -1010,5 +999,10 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
1010999
{
10111000
SetDisconnectedState();
10121001
}
1002+
1003+
private void copyConsoleToolStripMenuItem_Click(object sender, EventArgs e)
1004+
{
1005+
Clipboard.SetText(txtLog.Text);
1006+
}
10131007
}
10141008
}

Caesar/Diogenes/Forms/SecurityLevelForm.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading.Tasks;
99
using System.Windows.Forms;
1010
using Caesar;
11+
using Diogenes.SecurityAccess;
1112

1213
namespace Diogenes
1314
{

Caesar/Diogenes/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.4.4.0")]
36-
[assembly: AssemblyFileVersion("1.4.4.0")]
35+
[assembly: AssemblyVersion("1.4.5.0")]
36+
[assembly: AssemblyFileVersion("1.4.5.0")]

Caesar/Diogenes/SecurityAccess/DllContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Threading.Tasks;
1010
using Caesar;
1111

12-
namespace Diogenes
12+
namespace Diogenes.SecurityAccess
1313
{
1414
public class DllContext
1515
{

0 commit comments

Comments
 (0)