Skip to content

Commit

Permalink
- add invalid message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
olegel committed May 23, 2019
1 parent 435ca54 commit 7eeaf9d
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 180 deletions.
125 changes: 70 additions & 55 deletions CanLogger/VolvoCanLogger/Can232Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void ProcessTextMessage(string line)
var messageList = new ReceivedMessageList();
var receivedMessage = messageList.Add(++_rawId, line);

CanMessage canMessage = ProcessMessageData(line);
CanMessage canMessage = ProcessMessageData(line, receivedMessage);
if (canMessage != null)
{
receivedMessage.CanMessage = canMessage;
Expand Down Expand Up @@ -104,74 +104,89 @@ int ParseMessages(string data, ReceivedMessageList messageList)

var receivedMessage = messageList.Add(++_rawId, message);

CanMessage canMessage = ProcessMessageData(message);

CanMessage canMessage = ProcessMessageData(message, receivedMessage);
if (canMessage != null)
{
receivedMessage.CanMessage = canMessage;
}
}
return -1;
}

CanMessage ProcessMessageData(string data)
CanMessage ProcessMessageData(string data, ReceivedMessage receivedMessage)
{
CanMessage canMessage = null;

int length = data.Length;
int pos = 0;
int c = data[pos];
int idLen = 0;
switch (c)
Exception processingException = null;
try
{
case 'T':
idLen = 8;
break;
case 't':
idLen = 3;
int length = data.Length;
int pos = 0;
int c = data[pos];
int idLen = 0;
switch (c)
{
case 'T':
idLen = 8;
break;
case 't':
idLen = 3;
break;
}

while (true)
{
if (idLen <= 0)
break;

pos++;

if (length < idLen + pos)
break;

string idTxt = data.Substring(pos, idLen);
pos += idLen;

if (length < pos + 1)
break;

int dataByteCount = int.Parse(data.Substring(pos, 1));
if (dataByteCount < 0 || dataByteCount > 8)
break;
pos++;
if (length < pos + dataByteCount * 2)
break;

string dataTxt = data.Substring(pos, dataByteCount * 2);
pos += dataByteCount * 2;

string timeTxt = "00";
if (pos < length)
timeTxt = data.Substring(pos);

canMessage = new CanMessage();
canMessage.Id = Convert.ToInt32(idTxt, 16);
canMessage.Time = Convert.ToInt32(timeTxt, 16);
canMessage.DataCount = dataByteCount;
for (int i = 0; i < dataByteCount; i++)
{
string s = dataTxt.Substring(i * 2, 2);
byte b = (byte)Convert.ToInt32(s, 16);
canMessage.Data = Helpers.SetDataByte(canMessage.Data, i, b);
}
break;
}
}

while (true)
catch (Exception ex)
{
if (idLen <= 0)
break;

pos++;

if (length < idLen + pos)
break;

string idTxt = data.Substring(pos, idLen);
pos += idLen;

if (length < pos + 1)
break;

int dataByteCount = int.Parse(data.Substring(pos, 1));
if (dataByteCount < 0 || dataByteCount > 8)
break;
pos++;
if (length < pos + dataByteCount * 2)
break;

string dataTxt = data.Substring(pos, dataByteCount * 2);
pos += dataByteCount * 2;

string timeTxt = "00";
if (pos < length)
timeTxt = data.Substring(pos);
processingException = ex;
canMessage = null;
}

canMessage = new CanMessage();
canMessage.Id = Convert.ToInt32(idTxt, 16);
canMessage.Time = Convert.ToInt32(timeTxt, 16);
canMessage.DataCount = dataByteCount;
for (int i = 0; i < dataByteCount; i++)
{
string s = dataTxt.Substring(i * 2, 2);
byte b = (byte)Convert.ToInt32(s, 16);
canMessage.Data = Helpers.SetDataByte(canMessage.Data, i, b);
}
break;
if (processingException != null)
{
receivedMessage.RawText = "Invalid data: " + data;
string errorMsg = $"\n{DateTime.Now} Exception: {processingException.Message}\nProcessing '{data}'\nStack Trace:\n {processingException.StackTrace}";
System.IO.File.AppendAllText("error.log", errorMsg);
}
return canMessage;
}
Expand Down
Loading

0 comments on commit 7eeaf9d

Please sign in to comment.