From 782d396edfa4ab31ac078eaf56da465f70496f43 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Mon, 9 Mar 2015 19:08:27 -0500 Subject: [PATCH] auto-sync --- firmware/console/binary/tunerstudio.cpp | 5 +- .../rusefi/binaryprotocol/BinaryProtocol.java | 53 ++++++++++++++++--- .../binaryprotocol/BinaryProtocolCmd.java | 28 ++++++---- 3 files changed, 68 insertions(+), 18 deletions(-) diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index 2da8aed0a9..7b4628a3a2 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -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; } diff --git a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java index dd580e77d2..80798bf41d 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -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) { diff --git a/java_console/ui/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java b/java_console/ui/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java index ab7e6ce734..03b398311a 100644 --- a/java_console/ui/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java +++ b/java_console/ui/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java @@ -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"); }