Elm327Connector - exceptions are for exceptional situations only

This commit is contained in:
rusefillc 2021-12-05 01:45:18 -05:00
parent ee3030b92b
commit 157ee72944
3 changed files with 32 additions and 15 deletions

View File

@ -47,6 +47,17 @@ public class Elm327Connector implements Closeable {
tsStream = new Elm327IoStream(this, "elm327Stream");
}
/**
* TODO: HUH? what's that about?!
*/
public static void whyDoWeNeedToSleepBetweenCommands() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
public Elm327IoStream getTsStream() {
return tsStream;
}
@ -134,7 +145,7 @@ public class Elm327Connector implements Closeable {
log.info("-------sendBytesToSerial " + bytes.length + " byte(s):");
for (int i = 0; i < bytes.length; i++) {
log.info("[" + i + "] " + ((int) bytes[i] & 0xff));
log.info("[index=" + i + "] " + ((int) bytes[i] & 0xff));
}
// 1 frame
@ -165,10 +176,10 @@ public class Elm327Connector implements Closeable {
private boolean initConnection(String msg) {
if (sendCommand(HELLO, "ELM327 v[0-9]+\\.[0-9]+", BIG_TIMEOUT) != null) {
log.info("ELM DETECTED on " + msg + "!");
log.info("ELM DETECTED on " + msg + "! " + ELM327_DEFAULT_BAUDRATE);
return true;
}
log.info("ELM NOT FOUND on " + msg + "!");
log.info("ELM NOT FOUND on " + msg + "!" + ELM327_DEFAULT_BAUDRATE);
return false;
}
@ -287,11 +298,7 @@ public class Elm327Connector implements Closeable {
private void sendDataBack(String line) {
byte [] canPacket = HexUtil.asBytes(line);
try {
tsStream.processCanPacket(canPacket);
} catch (Exception e) {
System.out.println("ELM327: Error processing " + line);
}
tsStream.processCanPacket(canPacket);
}
public static boolean checkConnection(String serialPort, IoStream stream) {

View File

@ -129,9 +129,10 @@ public class Elm327IoStream extends AbstractIoStream {
dataListener.onDataArrived(packet);
}
public void processCanPacket(byte [] data) throws Exception {
public void processCanPacket(byte [] data) {
byte [] rawData = canDecoder.decodePacket(data);
sendDataToClient(rawData);
if (rawData.length != 0)
sendDataToClient(rawData);
}
public interface DisconnectListener {

View File

@ -18,10 +18,12 @@ class IsoTpCanDecoder {
final static int ISO_TP_FRAME_FIRST = 1;
final static int ISO_TP_FRAME_CONSECUTIVE = 2;
private final static int FC_ContinueToSend = 0;
public int waitingForNumBytes = 0;
public int waitingForFrameIndex = 0;
public byte[] decodePacket(byte[] data) throws Exception {
public byte[] decodePacket(byte[] data) {
int frameType = (data[0] >> 4) & 0xf;
int numBytesAvailable;
int frameIdx;
@ -31,6 +33,8 @@ class IsoTpCanDecoder {
numBytesAvailable = data[0] & 0xf;
dataOffset = 1;
this.waitingForNumBytes = 0;
if (log.debugEnabled())
log.debug("ISO_TP_FRAME_SINGLE " + numBytesAvailable);
break;
case ISO_TP_FRAME_FIRST:
this.waitingForNumBytes = ((data[0] & 0xf) << 8) | data[1];
@ -44,7 +48,7 @@ class IsoTpCanDecoder {
case ISO_TP_FRAME_CONSECUTIVE:
frameIdx = data[0] & 0xf;
if (this.waitingForNumBytes < 0 || this.waitingForFrameIndex != frameIdx) {
throw new Exception("ISO_TP_FRAME_CONSECUTIVE: That's an abnormal situation, and we probably should react?");
throw new IllegalStateException("ISO_TP_FRAME_CONSECUTIVE: That's an abnormal situation, and we probably should react? " + waitingForNumBytes + " " + waitingForFrameIndex + " " + frameIdx);
}
this.waitingForFrameIndex = (this.waitingForFrameIndex + 1) & 0xf;
numBytesAvailable = Math.min(this.waitingForNumBytes, 7);
@ -54,13 +58,18 @@ class IsoTpCanDecoder {
log.debug("ISO_TP_FRAME_CONSECUTIVE Got " + numBytesAvailable + ", still expecting: " + waitingForNumBytes);
break;
case ISO_TP_FRAME_FLOW_CONTROL:
throw new Exception("ISO_TP_FRAME_FLOW_CONTROL: should we just ignore the FC frame?");
int flowStatus = data[0] & 0xf;
int blockSize = data[1];
int separationTime = data[2];
if (flowStatus == FC_ContinueToSend && blockSize == 0 && separationTime == 0)
return new byte[0];
throw new IllegalStateException("ISO_TP_FRAME_FLOW_CONTROL: should we just ignore the FC frame? " + flowStatus + " " + blockSize + " " + separationTime);
default:
throw new Exception("Unknown frame type");
throw new IllegalStateException("Unknown frame type");
}
byte[] bytes = Arrays.copyOfRange(data, dataOffset, dataOffset + numBytesAvailable);
if (log.debugEnabled())
log.debug(numBytesAvailable + " bytes(s) arrived in this packet:" + IoStream.printHexBinary(bytes));
log.debug(numBytesAvailable + " bytes(s) arrived in this packet: " + IoStream.printHexBinary(bytes));
return bytes;
}
}