From a15738f42c8024f80f50a9623f07647ad2c9501e Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sun, 8 Mar 2015 13:04:49 -0500 Subject: [PATCH] auto-sync --- firmware/integration/rusefi.xml | 27 ++++- .../io/src/com/rusefi/ConfigurationImage.java | 21 +++- .../rusefi/binaryprotocol/BinaryProtocol.java | 104 ++++++++++++------ java_console/romraider/romraider.iml | 2 + .../com/romraider/swing/ECUEditorToolBar.java | 92 ++++++++-------- .../ui/src/com/rusefi/UploadChanges.java | 42 ++----- .../binaryprotocol/BinaryProtocolCmd.java | 18 +-- 7 files changed, 176 insertions(+), 130 deletions(-) diff --git a/firmware/integration/rusefi.xml b/firmware/integration/rusefi.xml index 99dcdccb35..4b2a5971a5 100644 --- a/firmware/integration/rusefi.xml +++ b/firmware/integration/rusefi.xml @@ -15,6 +15,7 @@ sti04 14008 +
@@ -24,9 +25,31 @@
- + +
+ +
+ +
+ + +
+ + + + +
+ +
+ + +
+ + + + -
+
diff --git a/java_console/io/src/com/rusefi/ConfigurationImage.java b/java_console/io/src/com/rusefi/ConfigurationImage.java index 3aa4e98c8c..bc692f911e 100644 --- a/java_console/io/src/com/rusefi/ConfigurationImage.java +++ b/java_console/io/src/com/rusefi/ConfigurationImage.java @@ -19,22 +19,18 @@ public class ConfigurationImage { this.content = content; } - public int getSize() { return content.length; } public byte[] getFileContent() throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { byte[] bytes = BIN_HEADER.getBytes(); if (bytes.length != BIN_HEADER.length()) throw new IllegalStateException("Encoding issue"); baos.write(bytes); baos.write(content); return baos.toByteArray(); - } finally { - baos.close(); } } @@ -63,7 +59,22 @@ public class ConfigurationImage { return result == image.getContent().length ? image : null; } + public static byte[] extractContent(byte[] rom) { + if (rom.length < BIN_HEADER.length()) + return null; + byte[] result = new byte[rom.length - BIN_HEADER.length()]; + System.arraycopy(rom, BIN_HEADER.length(), result, 0, result.length); + return result; + } + public byte[] getContent() { return content; } + + @SuppressWarnings("CloneDoesntCallSuperClone") + @Override + public ConfigurationImage clone() { + byte[] copy = content.clone(); + return new ConfigurationImage(copy); + } } diff --git a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java index 359099bda2..e628c0bc7d 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -1,7 +1,9 @@ package com.rusefi.binaryprotocol; import com.rusefi.ConfigurationImage; +import com.rusefi.ConfigurationImageDiff; import com.rusefi.Logger; +import com.rusefi.core.Pair; import com.rusefi.io.DataListener; import com.rusefi.io.serial.SerialPortReader; import etch.util.CircularByteBuffer; @@ -25,6 +27,9 @@ public class BinaryProtocol { private final CircularByteBuffer cbb; private boolean isBurnPending; + private final Object lock = new Object(); + private ConfigurationImage controller; + public BinaryProtocol(final Logger logger, SerialPort serialPort) throws SerialPortException { this.logger = logger; this.serialPort = serialPort; @@ -47,12 +52,22 @@ public class BinaryProtocol { serialPort.addEventListener(new SerialPortReader(serialPort, listener)); } - private void waitForBytes(int count) throws InterruptedException { - logger.info("Waiting for " + count + " byte(s)"); - synchronized (cbb) { - while (cbb.length() < count) - cbb.wait(); + public void burnChanges(ConfigurationImage newVersion, Logger logger) throws InterruptedException, EOFException, SerialPortException { + ConfigurationImage current = getController(); + // let's have our own copy which no one would be able to change + newVersion = newVersion.clone(); + int offset = 0; + while (offset < current.getSize()) { + Pair range = ConfigurationImageDiff.findDifferences(current, newVersion, offset); + if (range == null) + break; + logger.info("Need to patch: " + range); + writeData(newVersion.getContent(), range.first, range.second - range.first, logger); + + offset = range.second; } + burn(); + setController(newVersion); } /** @@ -71,29 +86,6 @@ public class BinaryProtocol { return packet; } - private static void putInt(byte[] packet, int offset, int value) { - int index = offset + 3; - for (int i = 0; i < 4; i++) { - packet[index--] = (byte) value; - value >>= 8; - } - } - - private static void putShort(byte[] packet, int offset, int value) { - int index = offset + 1; - for (int i = 0; i < 2; i++) { - packet[index--] = (byte) value; - value >>= 8; - } - } - - private void sendCrcPacket(byte[] command) throws SerialPortException { - byte[] packet = makePacket(command); - - logger.info("Sending " + Arrays.toString(packet)); - serialPort.writeBytes(packet); - } - public static int swap16(int x) { return (((x & 0xFF) << 8) | ((x) >> 8)); } @@ -133,7 +125,9 @@ public class BinaryProtocol { } } - public void readImage(ConfigurationImage image) throws SerialPortException, EOFException, InterruptedException { + public void readImage(int size) throws SerialPortException, EOFException, InterruptedException { + ConfigurationImage image = new ConfigurationImage(size); + int offset = 0; while (offset < image.getSize()) { @@ -159,6 +153,7 @@ public class BinaryProtocol { offset += requestSize; } logger.info("Got image!"); + setController(image); } public byte[] exchange(byte[] packet) throws SerialPortException, InterruptedException, EOFException { @@ -166,10 +161,6 @@ public class BinaryProtocol { return receivePacket(); } - private boolean checkResponseCode(byte[] response, byte code) { - return response != null && response.length > 0 && response[0] == code; - } - public void writeData(byte[] content, Integer offset, int size, Logger logger) throws SerialPortException, EOFException, InterruptedException { if (size > BLOCKING_FACTOR) { writeData(content, offset, BLOCKING_FACTOR, logger); @@ -195,7 +186,6 @@ public class BinaryProtocol { } break; } - } public void burn() throws InterruptedException, EOFException, SerialPortException { @@ -211,4 +201,50 @@ public class BinaryProtocol { } isBurnPending = false; } + + public void setController(ConfigurationImage controller) { + synchronized (lock) { + this.controller = controller.clone(); + } + } + + public ConfigurationImage getController() { + synchronized (lock) { + return controller.clone(); + } + } + + private void waitForBytes(int count) throws InterruptedException { + logger.info("Waiting for " + count + " byte(s)"); + synchronized (cbb) { + while (cbb.length() < count) + cbb.wait(); + } + } + + private boolean checkResponseCode(byte[] response, byte code) { + return response != null && response.length > 0 && response[0] == code; + } + + private static void putInt(byte[] packet, int offset, int value) { + int index = offset + 3; + for (int i = 0; i < 4; i++) { + packet[index--] = (byte) value; + value >>= 8; + } + } + + private static void putShort(byte[] packet, int offset, int value) { + int index = offset + 1; + for (int i = 0; i < 2; i++) { + packet[index--] = (byte) value; + value >>= 8; + } + } + + private void sendCrcPacket(byte[] command) throws SerialPortException { + byte[] packet = makePacket(command); + logger.info("Sending " + Arrays.toString(packet)); + serialPort.writeBytes(packet); + } } diff --git a/java_console/romraider/romraider.iml b/java_console/romraider/romraider.iml index ea38c34862..795d1d2fe5 100644 --- a/java_console/romraider/romraider.iml +++ b/java_console/romraider/romraider.iml @@ -11,5 +11,7 @@ + + \ No newline at end of file diff --git a/java_console/romraider/src/com/romraider/swing/ECUEditorToolBar.java b/java_console/romraider/src/com/romraider/swing/ECUEditorToolBar.java index 634647c296..366212da65 100644 --- a/java_console/romraider/src/com/romraider/swing/ECUEditorToolBar.java +++ b/java_console/romraider/src/com/romraider/swing/ECUEditorToolBar.java @@ -36,14 +36,17 @@ import javax.swing.JToolBar; import com.romraider.Settings; import com.romraider.editor.ecu.ECUEditor; import com.romraider.editor.ecu.ECUEditorManager; +import com.romraider.maps.Rom; import com.romraider.util.SettingsManager; +import com.rusefi.ConfigurationImage; +import com.rusefi.binaryprotocol.BinaryProtocolCmd; -public class ECUEditorToolBar extends JToolBar implements ActionListener { +public class ECUEditorToolBar extends JToolBar { private static final long serialVersionUID = 7778170684606193919L; - private final JButton saveImage = new JButton(); + // private final JButton saveImage = new JButton(); private final JButton refreshImage = new JButton(); - private final JButton closeImage = new JButton(); +// private final JButton closeImage = new JButton(); public ECUEditorToolBar(String name) { super(name); @@ -55,29 +58,40 @@ public class ECUEditorToolBar extends JToolBar implements ActionListener { this.updateIcons(); - this.add(saveImage); - this.add(closeImage); +// this.add(saveImage); +// this.add(closeImage); this.add(refreshImage); - saveImage.setMaximumSize(new Dimension(50, 50)); - saveImage.setBorder(createLineBorder(new Color(150, 150, 150), 0)); - closeImage.setMaximumSize(new Dimension(50, 50)); - closeImage.setBorder(createLineBorder(new Color(150, 150, 150), 0)); + refreshImage.setToolTipText("Burn changes into controller"); + + +// saveImage.setMaximumSize(new Dimension(50, 50)); +// saveImage.setBorder(createLineBorder(new Color(150, 150, 150), 0)); +// closeImage.setMaximumSize(new Dimension(50, 50)); +// closeImage.setBorder(createLineBorder(new Color(150, 150, 150), 0)); refreshImage.setMaximumSize(new Dimension(50, 50)); refreshImage.setBorder(createLineBorder(new Color(150, 150, 150), 0)); this.updateButtons(); - saveImage.addActionListener(this); - closeImage.addActionListener(this); - refreshImage.addActionListener(this); +// saveImage.addActionListener(this); +// closeImage.addActionListener(this); + refreshImage.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Rom lastSelectedRom = ECUEditorManager.getECUEditor().getLastSelectedRom(); + byte[] newVersion = ConfigurationImage.extractContent(lastSelectedRom.saveFile()); + System.out.println(newVersion.length); + BinaryProtocolCmd.scheduleBurn(new ConfigurationImage(newVersion)); + } + }); } public void updateIcons() { int iconScale = getSettings().getEditorIconScale(); - saveImage.setIcon(rescaleImageIcon(new ImageIcon(getClass().getResource("/graphics/icon-save.png")), iconScale)); +// saveImage.setIcon(rescaleImageIcon(new ImageIcon(getClass().getResource("/graphics/icon-save.png")), iconScale)); refreshImage.setIcon(rescaleImageIcon(new ImageIcon(getClass().getResource("/graphics/icon-refresh.png")), iconScale)); - closeImage.setIcon(rescaleImageIcon(new ImageIcon( getClass().getResource("/graphics/icon-close.png")), iconScale)); +// closeImage.setIcon(rescaleImageIcon(new ImageIcon( getClass().getResource("/graphics/icon-close.png")), iconScale)); repaint(); } @@ -85,46 +99,28 @@ public class ECUEditorToolBar extends JToolBar implements ActionListener { int newHeight = (int) (imageIcon.getImage().getHeight(this) * (percentOfOriginal * .01)); int newWidth = (int) (imageIcon.getImage().getWidth(this) * (percentOfOriginal * .01)); - if(newHeight > 0 && newWidth > 0) - { + if (newHeight > 0 && newWidth > 0) { imageIcon.setImage(imageIcon.getImage().getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH)); } return imageIcon; } public void updateButtons() { - String file = getEditor().getLastSelectedRomFileName(); - - saveImage.setToolTipText("Save " + file + " As New Image..."); - refreshImage.setToolTipText("Refresh " + file + " from saved copy"); - closeImage.setToolTipText("Close " + file); - - if ("".equals(file)) { - saveImage.setEnabled(false); - refreshImage.setEnabled(false); - closeImage.setEnabled(false); - } else { - saveImage.setEnabled(true); - refreshImage.setEnabled(true); - closeImage.setEnabled(true); - } - revalidate(); - } - - @Override - public void actionPerformed(ActionEvent e) { - if (e.getSource() == saveImage) { - try { - ((ECUEditorMenuBar) getEditor().getJMenuBar()).saveImage(); - getEditor().refreshUI(); - } catch (Exception ex) { - JOptionPane.showMessageDialog(getEditor(), new DebugPanel(ex, - getSettings().getSupportURL()), "Exception", JOptionPane.ERROR_MESSAGE); - } - } else if (e.getSource() == closeImage) { - getEditor().closeImage(); - } else if (e.getSource() == refreshImage) { - } +// String file = getEditor().getLastSelectedRomFileName(); +// +//// saveImage.setToolTipText("Save " + file + " As New Image..."); +//// closeImage.setToolTipText("Close " + file); +// +// if ("".equals(file)) { +//// saveImage.setEnabled(false); +// refreshImage.setEnabled(false); +// closeImage.setEnabled(false); +// } else { +// saveImage.setEnabled(true); +// refreshImage.setEnabled(true); +// closeImage.setEnabled(true); +// } +// revalidate(); } private Settings getSettings() { diff --git a/java_console/ui/src/com/rusefi/UploadChanges.java b/java_console/ui/src/com/rusefi/UploadChanges.java index 421e27bfe0..031f2477eb 100644 --- a/java_console/ui/src/com/rusefi/UploadChanges.java +++ b/java_console/ui/src/com/rusefi/UploadChanges.java @@ -1,7 +1,6 @@ package com.rusefi; import com.rusefi.binaryprotocol.BinaryProtocol; -import com.rusefi.core.Pair; import com.rusefi.io.serial.PortHolder; import com.rusefi.ui.StatusWindow; import jssc.SerialPort; @@ -19,6 +18,7 @@ import java.util.concurrent.Executors; * 3/7/2015 */ public class UploadChanges { + public static final Logger logger = createUiLogger(); private static final Executor EXEC = Executors.newSingleThreadExecutor(); public static void main(String[] args) throws SerialPortException, InvocationTargetException, InterruptedException { @@ -33,11 +33,7 @@ public class UploadChanges { public void run() { try { showUi(port); - } catch (SerialPortException e) { - throw new IllegalStateException(e); - } catch (IOException e) { - throw new IllegalStateException(e); - } catch (InterruptedException e) { + } catch (SerialPortException | IOException | InterruptedException e) { throw new IllegalStateException(e); } } @@ -45,8 +41,6 @@ public class UploadChanges { } private static void showUi(String port) throws SerialPortException, IOException, InterruptedException { - final Logger logger = createUiLogger(); - SerialPort serialPort; serialPort = new SerialPort(port); @@ -58,29 +52,27 @@ public class UploadChanges { logger.info("Looks good"); final ConfigurationImage ci1 = ConfigurationImage.readFromFile("rus_saved.bin", logger); + final ConfigurationImage ci2 = ConfigurationImage.readFromFile("rusefi_configuration.bin", logger); final BinaryProtocol bp = new BinaryProtocol(logger, serialPort); + bp.setController(ci1); + scheduleBurn(ci2, bp); + } + + public static void scheduleBurn(final ConfigurationImage newVersion, final BinaryProtocol bp) { EXEC.execute(new Runnable() { @Override public void run() { try { - patch(ci1, ci2, bp, logger); - } catch (InterruptedException e) { - logger.error("Error: " + e); - throw new IllegalStateException(e); - } catch (EOFException e) { - logger.error("Error: " + e); - throw new IllegalStateException(e); - } catch (SerialPortException e) { + bp.burnChanges(newVersion, logger); + } catch (InterruptedException | EOFException | SerialPortException e) { logger.error("Error: " + e); throw new IllegalStateException(e); } } }); - - } private static Logger createUiLogger() { @@ -123,18 +115,4 @@ public class UploadChanges { }; } - private static void patch(ConfigurationImage ci1, ConfigurationImage ci2, BinaryProtocol bp, Logger logger) throws InterruptedException, EOFException, SerialPortException { - int offset = 0; - while (offset < ci1.getSize()) { - Pair range = ConfigurationImageDiff.findDifferences(ci1, ci2, offset); - if (range == null) - break; - logger.info("Need to patch: " + range); - bp.writeData(ci2.getContent(), range.first, range.second - range.first, logger); - - offset = range.second; - } - bp.burn(); - - } } diff --git a/java_console/ui/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java b/java_console/ui/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java index 4437d228dc..ab7e6ce734 100644 --- a/java_console/ui/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java +++ b/java_console/ui/src/com/rusefi/binaryprotocol/BinaryProtocolCmd.java @@ -1,22 +1,20 @@ package com.rusefi.binaryprotocol; -import com.romraider.Settings; 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; -import jssc.SerialPortException; - -import java.io.IOException; /** * (c) Andrey Belomutskiy * 3/6/2015 */ public class BinaryProtocolCmd { + static BinaryProtocol bp; public static void main(String[] args) throws Exception { if (args.length != 1) { @@ -26,7 +24,7 @@ public class BinaryProtocolCmd { String port = args[0]; - Logger logger = Logger.STDOUT; + Logger logger = UploadChanges.logger; SerialPort serialPort; serialPort = new SerialPort(port); @@ -34,15 +32,14 @@ public class BinaryProtocolCmd { if (!opened) { logger.error("failed to open " + port); } - BinaryProtocol bp = new BinaryProtocol(logger, serialPort); + bp = new BinaryProtocol(logger, serialPort); PortHolder.setupPort(serialPort, 38400); logger.info("Looks good"); bp.exchange(new byte[]{'S'}); - ConfigurationImage image = new ConfigurationImage(14008); - - bp.readImage(image); + bp.readImage(14008); + ConfigurationImage image = bp.getController(); image.saveToFile("rusefi_configuration.bin"); @@ -53,4 +50,7 @@ public class BinaryProtocolCmd { } + public static void scheduleBurn(ConfigurationImage newVersion) { + UploadChanges.scheduleBurn(newVersion, bp); + } }