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.Pattern;
import static com.rusefi.Timeouts.SECOND;
public class Elm327Connector implements Closeable, DataListener {
private final static Logging log = Logging.getLogging(Elm327Connector.class);
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes();
private final static int ELM327_DEFAULT_BAUDRATE = 38400;
private final static int BIG_TIMEOUT = 2000;
private final static int TIMEOUT = 70;
public final static int ELM327_DEFAULT_BAUDRATE = 38400;
private final static int BIG_TIMEOUT = 2 * SECOND;
private final static int TIMEOUT = 70;
private final Object lock = new Object();
// these should match the defines in the firmware
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) {
// 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.setInputListener(this);
@ -287,42 +288,48 @@ public class Elm327Connector implements Closeable, DataListener {
}
}
private synchronized byte[] receiveData() {
try {
waitForResponse(TIMEOUT);
//log.info("Elm327Connector.receiveData(): size=" + this.completeLines.size());
return null;
} catch (InterruptedException ignore) {
return null;
}
private byte[] receiveData() {
synchronized (lock) {
try {
waitForResponse(TIMEOUT);
//log.info("Elm327Connector.receiveData(): size=" + this.completeLines.size());
return null;
} catch (InterruptedException ignore) {
return null;
}
}
}
private synchronized void waitForResponse(int timeout) throws InterruptedException {
// multiple lines can be sent, we need to wait for them all
while (true) {
int numLines = this.completeLines.size();
wait(timeout);
// if nothing changed
if (this.completeLines.size() == numLines)
break;
}
private void waitForResponse(int timeout) throws InterruptedException {
synchronized (lock) {
// multiple lines can be sent, we need to wait for them all
while (true) {
int numLines = completeLines.size();
lock.wait(timeout);
// if nothing changed
if (completeLines.size() == numLines)
break;
}
}
}
private synchronized void processLine(String line) {
log.info("Elm327Connector.processLine(): {" + line + "}");
private void processLine(String line) {
synchronized (lock) {
log.info("Elm327Connector.processLine(): {" + line + "}");
// remove the 'cursor'
if (line.charAt(0) == '>')
line = line.substring(1);
// remove the 'cursor'
if (line.charAt(0) == '>')
line = line.substring(1);
if (isCommandMode) {
// store the output as a response to the command (for verification)
this.completeLines.add(line);
notifyAll();
} else {
// just send it back to the proxy
sendDataBack(line);
}
if (isCommandMode) {
// store the output as a response to the command (for verification)
this.completeLines.add(line);
lock.notifyAll();
} else {
// just send it back to the proxy
sendDataBack(line);
}
}
}
private void sendDataBack(String line) {
@ -348,5 +355,4 @@ public class Elm327Connector implements Closeable, DataListener {
return true;
}
}