mirror of https://github.com/rusefi/rusefi-1.git
TS SD integration #1653
This commit is contained in:
parent
f6dcd2cbcb
commit
3271e24856
|
@ -223,7 +223,9 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
|
||||||
log.info("TS_SD: read directory command " + payload[payload.length - 1]);
|
log.info("TS_SD: read directory command " + payload[payload.length - 1]);
|
||||||
sendOkResponse(stream);
|
sendOkResponse(stream);
|
||||||
} else if (payload[6] == TS_SD_PROTOCOL_REMOVE_FILE) {
|
} 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);
|
sendOkResponse(stream);
|
||||||
} else if (payload[6] == TS_SD_PROTOCOL_FETCH_COMPRESSED) {
|
} else if (payload[6] == TS_SD_PROTOCOL_FETCH_COMPRESSED) {
|
||||||
log.info("TS_SD: read compressed file command " + Arrays.toString(packet.packet));
|
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
|
// SD read directory command
|
||||||
//
|
//
|
||||||
|
|
||||||
System.arraycopy("hello123mlq".getBytes(), 0, response, 1, 11);
|
setFileEntry(response, 0, "hello123mlq", (int) new File(TEST_FILE).length());
|
||||||
response[1 + 11] = 1; // file
|
setFileEntry(response, 1, "he mlq", 1024);
|
||||||
|
setFileEntry(response, 2, "_333o123mlq", 1000000);
|
||||||
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
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.info("TS_SD: Got unexpected r fetch " + IoStream.printHexBinary(packet.packet));
|
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 {
|
private static void sendOkResponse(TcpIoStream stream) throws IOException {
|
||||||
byte[] response = new byte[1];
|
byte[] response = new byte[1];
|
||||||
response[0] = TS_RESPONSE_OK;
|
response[0] = TS_RESPONSE_OK;
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.rusefi.config.generated.Fields;
|
||||||
import com.rusefi.io.ConnectionStateListener;
|
import com.rusefi.io.ConnectionStateListener;
|
||||||
import com.rusefi.io.IoStream;
|
import com.rusefi.io.IoStream;
|
||||||
import com.rusefi.io.LinkManager;
|
import com.rusefi.io.LinkManager;
|
||||||
|
import org.putgemin.VerticalFlowLayout;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
@ -17,6 +18,7 @@ import static com.rusefi.config.generated.Fields.TS_SD_PROTOCOL_FETCH_INFO;
|
||||||
|
|
||||||
public class ConnectPanel {
|
public class ConnectPanel {
|
||||||
private final JPanel content = new JPanel(new BorderLayout());
|
private final JPanel content = new JPanel(new BorderLayout());
|
||||||
|
private final JPanel fileList = new JPanel(new VerticalFlowLayout());
|
||||||
|
|
||||||
private LinkManager controllerConnector;
|
private LinkManager controllerConnector;
|
||||||
|
|
||||||
|
@ -54,7 +56,8 @@ public class ConnectPanel {
|
||||||
stream.sendPacket(packet);
|
stream.sendPacket(packet);
|
||||||
response = stream.getDataBuffer().getPacket("RTC status");
|
response = stream.getDataBuffer().getPacket("RTC status");
|
||||||
System.out.println("RTC response " + IoStream.printHexBinary(response));
|
System.out.println("RTC response " + IoStream.printHexBinary(response));
|
||||||
|
if (response == null)
|
||||||
|
throw new IOException("RTC No packet");
|
||||||
|
|
||||||
packet = new byte[17];
|
packet = new byte[17];
|
||||||
packet[0] = Fields.TS_SD_W_COMMAND;
|
packet[0] = Fields.TS_SD_W_COMMAND;
|
||||||
|
@ -62,9 +65,10 @@ public class ConnectPanel {
|
||||||
packet[6] = Fields.TS_SD_PROTOCOL_READ_DIR;
|
packet[6] = Fields.TS_SD_PROTOCOL_READ_DIR;
|
||||||
stream.sendPacket(packet);
|
stream.sendPacket(packet);
|
||||||
response = stream.getDataBuffer().getPacket("read dir command");
|
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));
|
System.out.println("read dir command " + IoStream.printHexBinary(response));
|
||||||
|
|
||||||
|
|
||||||
packet = new byte[8];
|
packet = new byte[8];
|
||||||
packet[0] = Fields.TS_SD_R_COMMAND;
|
packet[0] = Fields.TS_SD_R_COMMAND;
|
||||||
packet[1] = 0;
|
packet[1] = 0;
|
||||||
|
@ -73,10 +77,13 @@ public class ConnectPanel {
|
||||||
packet[6] = 0x02;
|
packet[6] = 0x02;
|
||||||
stream.sendPacket(packet);
|
stream.sendPacket(packet);
|
||||||
response = stream.getDataBuffer().getPacket("read command", true);
|
response = stream.getDataBuffer().getPacket("read command", true);
|
||||||
|
if (response == null)
|
||||||
|
throw new IOException("No packet");
|
||||||
System.out.println("read command " + IoStream.printHexBinary(response));
|
System.out.println("read command " + IoStream.printHexBinary(response));
|
||||||
|
|
||||||
int fileIndex = 0;
|
fileList.removeAll();
|
||||||
|
|
||||||
|
for (int fileIndex = 0; fileIndex < 512 / 32; fileIndex++) {
|
||||||
int offset = 32 * fileIndex;
|
int offset = 32 * fileIndex;
|
||||||
String fileNamePart = new String(response, 1 + offset, 8).trim();
|
String fileNamePart = new String(response, 1 + offset, 8).trim();
|
||||||
String fileExt = new String(response, 1 + offset + 8, 3).trim();
|
String fileExt = new String(response, 1 + offset + 8, 3).trim();
|
||||||
|
@ -86,23 +93,71 @@ public class ConnectPanel {
|
||||||
bb.order(ByteOrder.LITTLE_ENDIAN);
|
bb.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
int size = bb.getInt();
|
int size = bb.getInt();
|
||||||
|
|
||||||
|
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);
|
System.out.println("Filename " + fileName + " size " + size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} catch (IOException ioException) {
|
} catch (IOException ioException) {
|
||||||
ioException.printStackTrace();
|
ioException.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
flow.add(connect);
|
flow.add(connect);
|
||||||
flow.add(poke);
|
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() {
|
public JComponent getContent() {
|
||||||
|
|
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue