From 1a7154647e44112f655c750ac87bff2f8ecdeff7 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sun, 21 Jun 2020 00:31:01 -0400 Subject: [PATCH] REO progress --- .../rusefi/binaryprotocol/BinaryProtocol.java | 6 ++ .../com/rusefi/binaryprotocol/IoHelper.java | 2 +- .../rusefi/io/tcp/BinaryProtocolServer.java | 69 +++++++++++-------- 3 files changed, 48 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 8295085f72..33b3816ad8 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -600,4 +600,10 @@ public class BinaryProtocol implements BinaryProtocolCommands { throw new UnsupportedOperationException("type " + sensor.getType()); } } + + public void setRange(byte[] src, int scrPos, int offset, int count) { + synchronized (imageLock) { + System.arraycopy(src, scrPos, controller.getContent(), offset, count); + } + } } diff --git a/java_console/io/src/com/rusefi/binaryprotocol/IoHelper.java b/java_console/io/src/com/rusefi/binaryprotocol/IoHelper.java index cf1366a9d1..76b92d6e25 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/IoHelper.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/IoHelper.java @@ -13,7 +13,7 @@ public class IoHelper { return getCrc32(packet, 0, packet.length); } - public static int getCrc32(byte[] packet, int offset, int length) { + private static int getCrc32(byte[] packet, int offset, int length) { CRC32 c = new CRC32(); c.update(packet, offset, length); return (int) c.getValue(); diff --git a/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java b/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java index 663bdd6a4d..e6df4f2d32 100644 --- a/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java +++ b/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java @@ -110,43 +110,19 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { } else if (command == Fields.TS_GET_FIRMWARE_VERSION) { stream.sendPacket((TS_OK + "rusEFI proxy").getBytes(), FileLog.LOGGER); } else if (command == COMMAND_CRC_CHECK_COMMAND) { - System.out.println("CRC check"); - BinaryProtocol bp = BinaryProtocolHolder.getInstance().getCurrentStreamState(); - byte[] content = bp.getControllerConfiguration().getContent(); - int result = IoHelper.getCrc32(content, 0, content.length); - ByteArrayOutputStream response = new ByteArrayOutputStream(); - response.write(TS_OK.charAt(0)); - new DataOutputStream(response).write(result); - stream.sendPacket(response.toByteArray(), FileLog.LOGGER); + handleCrc(stream); } else if (command == COMMAND_PAGE) { stream.sendPacket(TS_OK.getBytes(), FileLog.LOGGER); } else if (command == COMMAND_READ) { - short page = dis.readShort(); - int offset = swap16(dis.readShort()); - int count = swap16(dis.readShort()); - if (count <= 0) { - FileLog.MAIN.logLine("Error: negative read request " + offset + "/" + count); - } else { - System.out.println("read " + page + "/" + offset + "/" + count); - BinaryProtocol bp = BinaryProtocolHolder.getInstance().getCurrentStreamState(); - byte[] response = new byte[1 + count]; - response[0] = (byte) TS_OK.charAt(0); - System.arraycopy(bp.getControllerConfiguration().getContent(), offset, response, 1, count); - stream.sendPacket(response, FileLog.LOGGER); - } + handleRead(dis, stream); } else if (command == Fields.TS_CHUNK_WRITE_COMMAND) { - dis.readShort(); // page - int offset = swap16(dis.readShort()); - int count = swap16(dis.readShort()); - FileLog.MAIN.logLine("TS_CHUNK_WRITE_COMMAND: offset=" + offset + " count=" + count); - BinaryProtocol bp = BinaryProtocolHolder.getInstance().getCurrentStreamState(); - System.arraycopy(packet, 7, bp.getControllerConfiguration().getContent(), offset, count); - stream.sendPacket(TS_OK.getBytes(), FileLog.LOGGER); + handleWrite(packet, dis, stream); } else if (command == Fields.TS_BURN_COMMAND) { stream.sendPacket(new byte[]{TS_RESPONSE_BURN_OK}, FileLog.LOGGER); } else if (command == Fields.TS_OUTPUT_COMMAND) { int offset = swap16(dis.readShort()); int count = swap16(dis.readShort()); + System.out.println("TS_OUTPUT_COMMAND offset=" + offset + "/count=" + count); byte[] response = new byte[1 + count]; response[0] = (byte) TS_OK.charAt(0); @@ -161,4 +137,41 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { } } } + + private static void handleWrite(byte[] packet, DataInputStream dis, TcpIoStream stream) throws IOException { + dis.readShort(); // page + int offset = swap16(dis.readShort()); + int count = swap16(dis.readShort()); + FileLog.MAIN.logLine("TS_CHUNK_WRITE_COMMAND: offset=" + offset + " count=" + count); + BinaryProtocol bp = BinaryProtocolHolder.getInstance().getCurrentStreamState(); + bp.setRange(packet, 7, offset, count); + stream.sendPacket(TS_OK.getBytes(), FileLog.LOGGER); + } + + private static void handleRead(DataInputStream dis, TcpIoStream stream) throws IOException { + short page = dis.readShort(); + int offset = swap16(dis.readShort()); + int count = swap16(dis.readShort()); + if (count <= 0) { + FileLog.MAIN.logLine("Error: negative read request " + offset + "/" + count); + } else { + System.out.println("read " + page + "/" + offset + "/" + count); + BinaryProtocol bp = BinaryProtocolHolder.getInstance().getCurrentStreamState(); + byte[] response = new byte[1 + count]; + response[0] = (byte) TS_OK.charAt(0); + System.arraycopy(bp.getControllerConfiguration().getContent(), offset, response, 1, count); + stream.sendPacket(response, FileLog.LOGGER); + } + } + + private static void handleCrc(TcpIoStream stream) throws IOException { + System.out.println("CRC check"); + BinaryProtocol bp = BinaryProtocolHolder.getInstance().getCurrentStreamState(); + byte[] content = bp.getControllerConfiguration().getContent(); + int result = IoHelper.getCrc32(content); + ByteArrayOutputStream response = new ByteArrayOutputStream(); + response.write(TS_OK.charAt(0)); + new DataOutputStream(response).writeInt(result); + stream.sendPacket(response.toByteArray(), FileLog.LOGGER); + } } \ No newline at end of file