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,13 +16,17 @@ 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;
@ -202,12 +206,9 @@ 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,42 +288,48 @@ public class Elm327Connector implements Closeable, DataListener {
} }
} }
private synchronized byte[] receiveData() { private byte[] receiveData() {
try { synchronized (lock) {
waitForResponse(TIMEOUT); try {
//log.info("Elm327Connector.receiveData(): size=" + this.completeLines.size()); waitForResponse(TIMEOUT);
return null; //log.info("Elm327Connector.receiveData(): size=" + this.completeLines.size());
} catch (InterruptedException ignore) { return null;
return null; } catch (InterruptedException ignore) {
} return null;
}
}
} }
private synchronized void waitForResponse(int timeout) throws InterruptedException { private void waitForResponse(int timeout) throws InterruptedException {
// multiple lines can be sent, we need to wait for them all synchronized (lock) {
while (true) { // multiple lines can be sent, we need to wait for them all
int numLines = this.completeLines.size(); while (true) {
wait(timeout); int numLines = completeLines.size();
// if nothing changed lock.wait(timeout);
if (this.completeLines.size() == numLines) // if nothing changed
break; if (completeLines.size() == numLines)
} break;
}
}
} }
private synchronized void processLine(String line) { private void processLine(String line) {
log.info("Elm327Connector.processLine(): {" + line + "}"); synchronized (lock) {
log.info("Elm327Connector.processLine(): {" + line + "}");
// remove the 'cursor' // remove the 'cursor'
if (line.charAt(0) == '>') if (line.charAt(0) == '>')
line = line.substring(1); line = line.substring(1);
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) {
@ -348,5 +355,4 @@ public class Elm327Connector implements Closeable, DataListener {
return true; return true;
} }
} }