From 3271e24856052db3de13cf33b3bc793d631d69f2 Mon Sep 17 00:00:00 2001 From: rusefi Date: Wed, 5 Aug 2020 19:13:41 -0400 Subject: [PATCH] TS SD integration #1653 --- .../rusefi/io/tcp/BinaryProtocolServer.java | 36 ++++---- .../com/rusefi/ts_plugin/ConnectPanel.java | 83 +++++++++++++++---- .../rusefi/ts_plugin/ConnectPanelTest.java | 15 ++++ 3 files changed, 103 insertions(+), 31 deletions(-) create mode 100644 java_tools/ts_plugin/src/test/java/com/rusefi/ts_plugin/ConnectPanelTest.java diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java index 2738dfaf28..5dd4a7daf7 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java @@ -223,7 +223,9 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { log.info("TS_SD: read directory command " + payload[payload.length - 1]); sendOkResponse(stream); } else if (payload[6] == TS_SD_PROTOCOL_REMOVE_FILE) { - log.info("TS_SD: remove file command " + Arrays.toString(packet.packet)); + String pattern = new String(payload, 7, 4); + log.info("TS_SD: remove file command " + Arrays.toString(packet.packet) + " " + pattern); + sendOkResponse(stream); } else if (payload[6] == TS_SD_PROTOCOL_FETCH_COMPRESSED) { log.info("TS_SD: read compressed file command " + Arrays.toString(packet.packet)); @@ -275,22 +277,9 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { // SD read directory command // - System.arraycopy("hello123mlq".getBytes(), 0, response, 1, 11); - response[1 + 11] = 1; // file - - response[1 + 23] = 3; // sector number - - response[1 + 24] = (byte) 0; // size - response[1 + 25] = (byte) 0; // size - - File f = new File(TEST_FILE); - int size = (int) f.length(); - - IoHelper.putInt(response, 29, IoHelper.swap32(size)); - -// response[1 + 29] = (byte) 128; -// response[1 + 30] = (byte) 1; -// response[1 + 31] = (byte) 0; // size + setFileEntry(response, 0, "hello123mlq", (int) new File(TEST_FILE).length()); + setFileEntry(response, 1, "he mlq", 1024); + setFileEntry(response, 2, "_333o123mlq", 1000000); } else { log.info("TS_SD: Got unexpected r fetch " + IoStream.printHexBinary(packet.packet)); @@ -331,6 +320,19 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { } } + private static void setFileEntry(byte[] response, int index, String fileName, int fileSize) { + int offset = 1 + 32 * index; + System.arraycopy(fileName.getBytes(), 0, response, offset, 11); + response[offset + 11] = 1; // file + + response[offset + 23] = (byte) index; // sector number + + response[offset + 24] = (byte) 0; // size + response[offset + 25] = (byte) 0; // size + + IoHelper.putInt(response, offset + 28, IoHelper.swap32(fileSize)); + } + private static void sendOkResponse(TcpIoStream stream) throws IOException { byte[] response = new byte[1]; response[0] = TS_RESPONSE_OK; diff --git a/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/ConnectPanel.java b/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/ConnectPanel.java index fe14c55e89..5107a3c756 100644 --- a/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/ConnectPanel.java +++ b/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/ConnectPanel.java @@ -4,6 +4,7 @@ import com.rusefi.config.generated.Fields; import com.rusefi.io.ConnectionStateListener; import com.rusefi.io.IoStream; import com.rusefi.io.LinkManager; +import org.putgemin.VerticalFlowLayout; import javax.swing.*; import java.awt.*; @@ -17,6 +18,7 @@ import static com.rusefi.config.generated.Fields.TS_SD_PROTOCOL_FETCH_INFO; public class ConnectPanel { private final JPanel content = new JPanel(new BorderLayout()); + private final JPanel fileList = new JPanel(new VerticalFlowLayout()); private LinkManager controllerConnector; @@ -54,7 +56,8 @@ public class ConnectPanel { stream.sendPacket(packet); response = stream.getDataBuffer().getPacket("RTC status"); System.out.println("RTC response " + IoStream.printHexBinary(response)); - + if (response == null) + throw new IOException("RTC No packet"); packet = new byte[17]; packet[0] = Fields.TS_SD_W_COMMAND; @@ -62,9 +65,10 @@ public class ConnectPanel { packet[6] = Fields.TS_SD_PROTOCOL_READ_DIR; stream.sendPacket(packet); response = stream.getDataBuffer().getPacket("read dir command"); + if (response == null) + throw new IOException("Read Dir No packet"); System.out.println("read dir command " + IoStream.printHexBinary(response)); - packet = new byte[8]; packet[0] = Fields.TS_SD_R_COMMAND; packet[1] = 0; @@ -73,36 +77,87 @@ public class ConnectPanel { packet[6] = 0x02; stream.sendPacket(packet); response = stream.getDataBuffer().getPacket("read command", true); + if (response == null) + throw new IOException("No packet"); System.out.println("read command " + IoStream.printHexBinary(response)); - int fileIndex = 0; + fileList.removeAll(); - int offset = 32 * fileIndex; - String fileNamePart = new String(response, 1 + offset, 8).trim(); - String fileExt = new String(response, 1 + offset + 8, 3).trim(); - String fileName = fileNamePart + "." + fileExt; + for (int fileIndex = 0; fileIndex < 512 / 32; fileIndex++) { + int offset = 32 * fileIndex; + String fileNamePart = new String(response, 1 + offset, 8).trim(); + String fileExt = new String(response, 1 + offset + 8, 3).trim(); + String fileName = fileNamePart + "." + fileExt; - ByteBuffer bb = ByteBuffer.wrap(response, 1 + offset + 28, 4); - bb.order(ByteOrder.LITTLE_ENDIAN); - int size = bb.getInt(); + ByteBuffer bb = ByteBuffer.wrap(response, 1 + offset + 28, 4); + bb.order(ByteOrder.LITTLE_ENDIAN); + int size = bb.getInt(); - System.out.println("Filename " + fileName + " size " + size); + JPanel filePanel = new JPanel(new FlowLayout()); + filePanel.add(new JLabel(fileName + " " + size + " byte(0)")); + JButton download = new JButton("Download"); + download.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + } + }); + + filePanel.add(download); + JButton delete = new JButton("Delete"); + delete.addActionListener(e1 -> { + int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to remove " + fileName, + "rusEfi", JOptionPane.YES_NO_OPTION); + if (result == JOptionPane.YES_OPTION) { + deleteFile(fileName); + + } + }); + filePanel.add(delete); + + System.out.println("Filename " + fileName + " size " + size); + } } catch (IOException ioException) { ioException.printStackTrace(); } - - } }); flow.add(connect); flow.add(poke); - content.add(flow); + content.add(flow, BorderLayout.NORTH); + content.add(fileList, BorderLayout.CENTER); + } + + public static String getLastFour(String fileName) { + int dotIndex = fileName.indexOf("."); + fileName = fileName.substring(0, dotIndex); + if (fileName.length() < 5) + return fileName; + return fileName.substring(fileName.length() - 4); + } + + private void deleteFile(String fileName) { + fileName = ConnectPanel.getLastFour(fileName); + + byte[] packet = new byte[17]; + packet[0] = Fields.TS_SD_W_COMMAND; + packet[2] = TS_SD_PROTOCOL_FETCH_INFO; + packet[6] = Fields.TS_SD_PROTOCOL_READ_DIR; + IoStream stream = controllerConnector.getConnector().getBinaryProtocol().getStream(); + + try { + stream.sendPacket(packet); + byte[] response = stream.getDataBuffer().getPacket("delete file"); + System.out.println("delete file " + IoStream.printHexBinary(response)); + } catch (IOException e) { + throw new IllegalStateException(e); + } } public JComponent getContent() { diff --git a/java_tools/ts_plugin/src/test/java/com/rusefi/ts_plugin/ConnectPanelTest.java b/java_tools/ts_plugin/src/test/java/com/rusefi/ts_plugin/ConnectPanelTest.java new file mode 100644 index 0000000000..99c66bb13a --- /dev/null +++ b/java_tools/ts_plugin/src/test/java/com/rusefi/ts_plugin/ConnectPanelTest.java @@ -0,0 +1,15 @@ +package com.rusefi.ts_plugin; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ConnectPanelTest { + @Test + public void testFileName() { + assertEquals("1234", ConnectPanel.getLastFour("aaa1234.a")); + assertEquals("1234", ConnectPanel.getLastFour("a1234.a")); + assertEquals("123", ConnectPanel.getLastFour("123.a")); + assertEquals("1", ConnectPanel.getLastFour("1.a")); + } +}