refactoring: explicit lock, removal of one dead code line, better magic constant
This commit is contained in:
parent
b8f7efb437
commit
d469c9ff94
|
@ -16,14 +16,18 @@ 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;
|
||||
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;
|
||||
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) {
|
||||
// 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,7 +288,8 @@ public class Elm327Connector implements Closeable, DataListener {
|
|||
}
|
||||
}
|
||||
|
||||
private synchronized byte[] receiveData() {
|
||||
private byte[] receiveData() {
|
||||
synchronized (lock) {
|
||||
try {
|
||||
waitForResponse(TIMEOUT);
|
||||
//log.info("Elm327Connector.receiveData(): size=" + this.completeLines.size());
|
||||
|
@ -296,19 +298,23 @@ public class Elm327Connector implements Closeable, DataListener {
|
|||
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
|
||||
while (true) {
|
||||
int numLines = this.completeLines.size();
|
||||
wait(timeout);
|
||||
int numLines = completeLines.size();
|
||||
lock.wait(timeout);
|
||||
// if nothing changed
|
||||
if (this.completeLines.size() == numLines)
|
||||
if (completeLines.size() == numLines)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void processLine(String line) {
|
||||
private void processLine(String line) {
|
||||
synchronized (lock) {
|
||||
log.info("Elm327Connector.processLine(): {" + line + "}");
|
||||
|
||||
// remove the 'cursor'
|
||||
|
@ -318,12 +324,13 @@ public class Elm327Connector implements Closeable, DataListener {
|
|||
if (isCommandMode) {
|
||||
// store the output as a response to the command (for verification)
|
||||
this.completeLines.add(line);
|
||||
notifyAll();
|
||||
lock.notifyAll();
|
||||
} else {
|
||||
// just send it back to the proxy
|
||||
sendDataBack(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sendDataBack(String line) {
|
||||
byte [] canPacket = HexUtil.asBytes(line);
|
||||
|
@ -348,5 +355,4 @@ public class Elm327Connector implements Closeable, DataListener {
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue