auto-sync

This commit is contained in:
rusEfi 2015-03-09 19:08:27 -05:00
parent 06512e0cf4
commit 782d396edf
3 changed files with 68 additions and 18 deletions

View File

@ -116,6 +116,8 @@ static int ts_serial_ready(void) {
#endif
}
static uint16_t BINARY_RESPONSE = (uint16_t)SWAP_UINT16(BINARY_SWITCH_TAG);
// this thread wants a bit extra stack
static THD_WORKING_AREA(tsThreadStack, UTILITY_THREAD_STACK_SIZE + 200);
@ -401,8 +403,9 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel) {
uint32_t incomingPacketSize = firstByte * 256 + secondByte;
if(incomingPacketSize==BINARY_SWITCH_TAG) {
if (incomingPacketSize == BINARY_SWITCH_TAG) {
// we are here if we get a binary switch request while already in binary mode. We will just ignore it.
tunerStudioWriteData(tsChannel, (const uint8_t *)&BINARY_RESPONSE, 2);
continue;
}

View File

@ -22,6 +22,10 @@ public class BinaryProtocol {
private static final byte RESPONSE_OK = 0;
private static final byte RESPONSE_BURN_OK = 0x04;
private static final byte RESPONSE_COMMAND_OK = 0x07;
private static final int SWITCH_TO_BINARY_RESPONSE = 0xA7E;
private static final int TIMEOUT = 30 * 1000;
private final Logger logger;
private final SerialPort serialPort;
private static final int BUFFER_SIZE = 10000;
@ -54,8 +58,32 @@ public class BinaryProtocol {
}
void switchToBinaryProtocol() throws SerialPortException, EOFException, InterruptedException {
// while (true)
serialPort.writeBytes("~\n".getBytes());
long start = System.currentTimeMillis();
while (true) {
dropPending();
serialPort.writeBytes("~\n".getBytes());
synchronized (cbb) {
waitForBytes(2, start);
int response = cbb.getShort();
if (response != SWITCH_TO_BINARY_RESPONSE) {
logger.error("Unexpected response, re-trying");
continue;
}
}
break;
}
}
private void dropPending() {
synchronized (cbb) {
int pending = cbb.length();
if (pending > 0) {
logger.error("Unexpected pending data: " + pending);
cbb.get(new byte[pending]);
}
}
}
public void burnChanges(ConfigurationImage newVersion, Logger logger) throws InterruptedException, EOFException, SerialPortException {
@ -101,8 +129,9 @@ public class BinaryProtocol {
}
private byte[] receivePacket() throws InterruptedException, EOFException {
long start = System.currentTimeMillis();
synchronized (cbb) {
waitForBytes(2);
waitForBytes(2, start);
int packetSize = BinaryProtocol.swap16(cbb.getShort());
logger.trace("Got packet size " + packetSize);
@ -112,7 +141,7 @@ public class BinaryProtocol {
// invalid packet size
return null;
}
waitForBytes(packetSize + 4);
waitForBytes(packetSize + 4, start);
byte[] packet = new byte[packetSize];
int packetCrc;
@ -165,6 +194,7 @@ public class BinaryProtocol {
}
public byte[] exchange(byte[] packet) throws SerialPortException, InterruptedException, EOFException {
dropPending();
sendCrcPacket(packet);
return receivePacket();
}
@ -222,12 +252,21 @@ public class BinaryProtocol {
}
}
private void waitForBytes(int count) throws InterruptedException {
/**
* @return true in case of timeout, false if everything is fine
*/
private boolean waitForBytes(int count, long start) throws InterruptedException {
logger.info("Waiting for " + count + " byte(s)");
synchronized (cbb) {
while (cbb.length() < count)
cbb.wait();
while (cbb.length() < count) {
int timeout = (int) (start + TIMEOUT - System.currentTimeMillis());
if (timeout < 0) {
return true; // timeout. Sad face.
}
cbb.wait(timeout);
}
}
return false; // looks good!
}
private boolean checkResponseCode(byte[] response, byte code) {

View File

@ -1,10 +1,7 @@
package com.rusefi.binaryprotocol;
import com.romraider.editor.ecu.ECUEditor;
import com.romraider.util.SettingsManager;
import com.rusefi.ConfigurationImage;
import com.rusefi.Logger;
import com.rusefi.RomRaiderWrapper;
import com.rusefi.UploadChanges;
import com.rusefi.io.serial.PortHolder;
import jssc.SerialPort;
@ -36,17 +33,28 @@ public class BinaryProtocolCmd {
PortHolder.setupPort(serialPort, 38400);
logger.info("Looks good");
bp.exchange(new byte[]{'S'});
bp.switchToBinaryProtocol();
bp.readImage(14008);
ConfigurationImage image = bp.getController();
image.saveToFile("rusefi_configuration.bin");
// bp.exchange(new byte[]{'S'});
RomRaiderWrapper.startRomRaider();
ECUEditor.openImage(image.getFileContent(), SettingsManager.getSettings().getEcuDefinitionFiles().elementAt(0),
"rusEfi");
// bp.sendTextCommand("hello");
// bp.sendTextCommand("echo howareyou");
while (true) {
bp.requestText();
}
// bp.readImage(14008);
// ConfigurationImage image = bp.getController();
//
// image.saveToFile("rusefi_configuration.bin");
//
// RomRaiderWrapper.startRomRaider();
//
// ECUEditor.openImage(image.getFileContent(), SettingsManager.getSettings().getEcuDefinitionFiles().elementAt(0),
// "rusEfi");
}