auto-sync

This commit is contained in:
rusEfi 2015-03-11 20:05:10 -05:00
parent 1cb86be932
commit 012be3d8a1
7 changed files with 43 additions and 15 deletions

View File

@ -5,7 +5,7 @@
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/lib/jssc-2.6.0-src.jar!/src" />
<root url="jar://$PROJECT_DIR$/lib/jssc-src.jar!/src" />
</SOURCES>
</library>
</component>

View File

@ -38,6 +38,7 @@ public class BinaryProtocol {
// todo: fix this, this is HORRIBLE!
@Deprecated
public static BinaryProtocol instance;
public boolean isClosed;
public BinaryProtocol(final Logger logger, SerialPort serialPort) {
this.logger = logger;
@ -67,6 +68,10 @@ public class BinaryProtocol {
}
}
public Logger getLogger() {
return logger;
}
public void switchToBinaryProtocol() {
long start = System.currentTimeMillis();
@ -189,6 +194,9 @@ public class BinaryProtocol {
int offset = 0;
while (offset < image.getSize()) {
if (isClosed)
return;
int remainingSize = image.getSize() - offset;
int requestSize = Math.min(remainingSize, BLOCKING_FACTOR);
@ -201,7 +209,7 @@ public class BinaryProtocol {
byte[] response = exchange(packet);
if (!checkResponseCode(response, RESPONSE_OK) || response.length != requestSize + 1) {
logger.error("Something is wrong, retrying...");
logger.error("readImage: Something is wrong, retrying...");
continue;
}
@ -214,13 +222,22 @@ public class BinaryProtocol {
setController(image);
}
/**
* Blocking sending binary packet and waiting for a response
*
* @return null in case of IO issues
*/
public byte[] exchange(byte[] packet) {
dropPending();
try {
sendCrcPacket(packet);
return receivePacket();
} catch (SerialPortException | InterruptedException | EOFException e) {
} catch (InterruptedException e) {
throw new IllegalStateException(e);
} catch (SerialPortException | EOFException e) {
logger.error("exchange failed: " + e);
isClosed = true;
return null;
}
}
@ -244,7 +261,7 @@ public class BinaryProtocol {
while (true) {
byte[] response = exchange(packet);
if (!checkResponseCode(response, RESPONSE_OK) || response.length != 1) {
logger.error("Something is wrong, retrying...");
logger.error("writeData: Something is wrong, retrying...");
continue;
}
break;
@ -257,6 +274,8 @@ public class BinaryProtocol {
logger.info("Need to burn");
while (true) {
if (isClosed)
return;
byte[] response = exchange(new byte[]{'B'});
if (!checkResponseCode(response, RESPONSE_BURN_OK) || response.length != 1) {
continue;
@ -275,6 +294,8 @@ public class BinaryProtocol {
public ConfigurationImage getController() {
synchronized (lock) {
if (controller == null)
return null;
return controller.clone();
}
}
@ -287,7 +308,7 @@ public class BinaryProtocol {
synchronized (cbb) {
while (cbb.length() < count) {
int timeout = (int) (start + TIMEOUT - System.currentTimeMillis());
if (timeout < 0) {
if (timeout <= 0) {
return true; // timeout. Sad face.
}
cbb.wait(timeout);
@ -343,6 +364,8 @@ public class BinaryProtocol {
}
public String requestText() {
if (isClosed)
return null;
try {
byte[] response = new byte[0];
response = exchange(new byte[]{'G'});

View File

@ -26,6 +26,7 @@ public class PortHolder {
private static final int BAUD_RATE = 115200;
private static final int SECOND = 1000;
private static final int MINUTE = 60 * SECOND;
private static final int COMMAND_TIMEOUT_SEC = 10; // seconds
private static PortHolder instance = new PortHolder();
private final Object portLock = new Object();
@ -81,17 +82,20 @@ public class PortHolder {
bp.switchToBinaryProtocol();
bp.readImage(BinaryProtocol.IMAGE_SIZE);
if (bp.isClosed)
return false;
Runnable textPull = new Runnable() {
@Override
public void run() {
while (true) {
while (!bp.isClosed) {
if (LinkManager.COMMUNICATION_QUEUE.isEmpty()) {
LinkManager.COMMUNICATION_EXECUTOR.submit(new Runnable() {
@Override
public void run() {
String text = bp.requestText();
listener.onDataArrived((text + "\r\n").getBytes());
if (text != null)
listener.onDataArrived((text + "\r\n").getBytes());
}
});
}
@ -162,9 +166,12 @@ public class PortHolder {
});
try {
f.get(30, TimeUnit.SECONDS);
} catch (ExecutionException | TimeoutException e) {
f.get(COMMAND_TIMEOUT_SEC, TimeUnit.SECONDS);
} catch (ExecutionException e) {
throw new IllegalStateException(e);
} catch (TimeoutException e) {
bp.getLogger().error("timeout, giving up: " + e);
return;
}
/**
* this here to make CommandQueue happy

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -30,17 +30,15 @@ public class TableEditor extends JPanel {
add(editor.getContent());
ConfigurationImage image = BinaryProtocol.instance.getController();
BinaryProtocol instance = BinaryProtocol.instance;
if (instance == null)
throw new NullPointerException("instance");
ConfigurationImage image = instance.getController();
ECUEditor.openImage(image.getFileContent(), SettingsManager.getSettings().getEcuDefinitionFiles().elementAt(0),
"rusEfi");
} catch (Exception e) {
throw new IllegalStateException(e);
}
UiUtils.trueLayout(this);
}
}