refactoring: explicit lock, removal of one dead code line, better magic constant

This commit is contained in:
rusefillc 2021-12-04 14:06:34 -05:00
parent b8f7efb437
commit d469c9ff94
1 changed files with 56 additions and 50 deletions

View File

@ -16,14 +16,18 @@ import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static com.rusefi.Timeouts.SECOND;
public class Elm327Connector implements Closeable, DataListener { public class Elm327Connector implements Closeable, DataListener {
private final static Logging log = Logging.getLogging(Elm327Connector.class); private final static Logging log = Logging.getLogging(Elm327Connector.class);
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(); private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes();
private final static int ELM327_DEFAULT_BAUDRATE = 38400; public final static int ELM327_DEFAULT_BAUDRATE = 38400;
private final static int BIG_TIMEOUT = 2000; private final static int BIG_TIMEOUT = 2 * SECOND;
private final static int TIMEOUT = 70; private final static int TIMEOUT = 70;
private final Object lock = new Object();
// these should match the defines in the firmware // these should match the defines in the firmware
private final static int CAN_SERIAL_TX_ID = 0x100; private final static int CAN_SERIAL_TX_ID = 0x100;
private final static int CAN_SERIAL_RX_ID = 0x102; private final static int CAN_SERIAL_RX_ID = 0x102;
@ -205,9 +209,6 @@ public class Elm327Connector implements Closeable, DataListener {
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
private boolean initConnection(String msg, IoStream stream) { private boolean initConnection(String msg, IoStream stream) {
// todo: this seems like a hack-ish way? Shouldn't be openPort(port, baudrate)?
BaudRateHolder.INSTANCE.baudRate = ELM327_DEFAULT_BAUDRATE;
this.stream = stream; this.stream = stream;
this.stream.setInputListener(this); this.stream.setInputListener(this);
@ -287,7 +288,8 @@ public class Elm327Connector implements Closeable, DataListener {
} }
} }
private synchronized byte[] receiveData() { private byte[] receiveData() {
synchronized (lock) {
try { try {
waitForResponse(TIMEOUT); waitForResponse(TIMEOUT);
//log.info("Elm327Connector.receiveData(): size=" + this.completeLines.size()); //log.info("Elm327Connector.receiveData(): size=" + this.completeLines.size());
@ -296,19 +298,23 @@ public class Elm327Connector implements Closeable, DataListener {
return null; return null;
} }
} }
}
private synchronized void waitForResponse(int timeout) throws InterruptedException { private void waitForResponse(int timeout) throws InterruptedException {
synchronized (lock) {
// multiple lines can be sent, we need to wait for them all // multiple lines can be sent, we need to wait for them all
while (true) { while (true) {
int numLines = this.completeLines.size(); int numLines = completeLines.size();
wait(timeout); lock.wait(timeout);
// if nothing changed // if nothing changed
if (this.completeLines.size() == numLines) if (completeLines.size() == numLines)
break; break;
} }
} }
}
private synchronized void processLine(String line) { private void processLine(String line) {
synchronized (lock) {
log.info("Elm327Connector.processLine(): {" + line + "}"); log.info("Elm327Connector.processLine(): {" + line + "}");
// remove the 'cursor' // remove the 'cursor'
@ -318,12 +324,13 @@ public class Elm327Connector implements Closeable, DataListener {
if (isCommandMode) { if (isCommandMode) {
// store the output as a response to the command (for verification) // store the output as a response to the command (for verification)
this.completeLines.add(line); this.completeLines.add(line);
notifyAll(); lock.notifyAll();
} else { } else {
// just send it back to the proxy // just send it back to the proxy
sendDataBack(line); sendDataBack(line);
} }
} }
}
private void sendDataBack(String line) { private void sendDataBack(String line) {
byte [] canPacket = HexUtil.asBytes(line); byte [] canPacket = HexUtil.asBytes(line);
@ -348,5 +355,4 @@ public class Elm327Connector implements Closeable, DataListener {
return true; return true;
} }
} }