From 361fb1b74b2698b5e2d5a1335fce5ccf4a7d6618 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sat, 7 Mar 2015 21:04:55 -0600 Subject: [PATCH] auto-sync --- .../rusefi/binaryprotocol/BinaryProtocol.java | 48 ++++++++++--------- .../binaryprotocol/BinaryProtocolCmd.java | 3 +- .../ui/src/com/rusefi/UploadChanges.java | 5 +- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java index 8f308a8c5d..359099bda2 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -18,11 +18,12 @@ import java.util.Arrays; public class BinaryProtocol { private static final int BLOCKING_FACTOR = 256; private static final byte RESPONSE_OK = 0; + private static final byte RESPONSE_BURN_OK = 0x04; private final Logger logger; private final SerialPort serialPort; private static final int BUFFER_SIZE = 10000; - final CircularByteBuffer cbb; - private boolean isBurnPendig; + private final CircularByteBuffer cbb; + private boolean isBurnPending; public BinaryProtocol(final Logger logger, SerialPort serialPort) throws SerialPortException { this.logger = logger; @@ -86,12 +87,7 @@ public class BinaryProtocol { } } - public void sendQueryCommand() throws SerialPortException { - byte[] command = {'S'}; - sendCrcPacket(command); - } - - public void sendCrcPacket(byte[] command) throws SerialPortException { + private void sendCrcPacket(byte[] command) throws SerialPortException { byte[] packet = makePacket(command); logger.info("Sending " + Arrays.toString(packet)); @@ -106,7 +102,7 @@ public class BinaryProtocol { return (((x) >> 24) & 0xff) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) | (((x) << 24) & 0xff000000); } - public byte[] receivePacket() throws InterruptedException, EOFException { + private byte[] receivePacket() throws InterruptedException, EOFException { synchronized (cbb) { waitForBytes(2); @@ -150,9 +146,8 @@ public class BinaryProtocol { putShort(packet, 3, swap16(offset)); putShort(packet, 5, swap16(requestSize)); - sendCrcPacket(packet); + byte[] response = exchange(packet); - byte[] response = receivePacket(); if (!checkResponseCode(response, RESPONSE_OK) || response.length != requestSize + 1) { logger.error("Something is wrong, retrying..."); continue; @@ -166,6 +161,11 @@ public class BinaryProtocol { logger.info("Got image!"); } + public byte[] exchange(byte[] packet) throws SerialPortException, InterruptedException, EOFException { + sendCrcPacket(packet); + return receivePacket(); + } + private boolean checkResponseCode(byte[] response, byte code) { return response != null && response.length > 0 && response[0] == code; } @@ -177,7 +177,7 @@ public class BinaryProtocol { return; } - isBurnPendig = true; + isBurnPending = true; byte packet[] = new byte[7 + size]; packet[0] = 'C'; @@ -188,25 +188,27 @@ public class BinaryProtocol { System.arraycopy(content, offset, packet, 7, size); while (true) { - sendCrcPacket(packet); - - byte[] response = receivePacket(); - - if (!checkResponseCode(response, RESPONSE_OK) - //|| response.length != requestSize + 1 - ) { + byte[] response = exchange(packet); + if (!checkResponseCode(response, RESPONSE_OK) || response.length != 1) { logger.error("Something is wrong, retrying..."); continue; } - break; } } - public void burn() { - if (!isBurnPendig) + public void burn() throws InterruptedException, EOFException, SerialPortException { + if (!isBurnPending) return; - isBurnPendig = false; + + while (true) { + byte[] response = exchange(new byte[]{'B'}); + if (!checkResponseCode(response, RESPONSE_BURN_OK) || response.length != 1) { + continue; + } + break; + } + isBurnPending = false; } } diff --git a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java index f446098352..79b62fd277 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java @@ -35,8 +35,7 @@ public class BinaryProtocolCmd { PortHolder.setupPort(serialPort, 38400); logger.info("Looks good"); - bp.sendQueryCommand(); - bp.receivePacket(); + bp.exchange(new byte[]{'S'}); ConfigurationImage image = new ConfigurationImage(14008); diff --git a/java_console/ui/src/com/rusefi/UploadChanges.java b/java_console/ui/src/com/rusefi/UploadChanges.java index 014d5839a6..b1924d5f03 100644 --- a/java_console/ui/src/com/rusefi/UploadChanges.java +++ b/java_console/ui/src/com/rusefi/UploadChanges.java @@ -114,15 +114,12 @@ public class UploadChanges { while (offset < ci1.getSize()) { Pair range = ConfigurationImageDiff.findDifferences(ci1, ci2, offset); if (range == null) - return; - + break; logger.info("Need to patch: " + range); bp.writeData(ci2.getContent(), range.first, range.second - range.first, logger); - offset = range.second; } bp.burn(); - } }