TS SD integration #1653

This commit is contained in:
rusefi 2020-08-07 00:59:00 -04:00
parent af34d22c11
commit 8d0f47b940
5 changed files with 196 additions and 102 deletions

View File

@ -8,6 +8,9 @@
#if EFI_SIMULATOR #if EFI_SIMULATOR
#include <stdio.h> #include <stdio.h>
#include <dirent.h> #include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif #endif
#define DIR_RESPONSE_SIZE 512 #define DIR_RESPONSE_SIZE 512
@ -37,7 +40,6 @@ bool isLogFile(const char *fileName) {
#endif #endif
#if EFI_FILE_LOGGING || EFI_SIMULATOR #if EFI_FILE_LOGGING || EFI_SIMULATOR
#include "mmc_card.h" #include "mmc_card.h"
@ -60,10 +62,18 @@ static void setFileEntry(uint8_t *buffer, int index, const char *fileName, int f
buffer[offset + 11] = 1; buffer[offset + 11] = 1;
for (int i = 0; i < 4; i++) {
buffer[offset + 18 + i] = fileName[dotIndex + i - 4];
}
*(uint32_t *) (&buffer[offset + 28]) = fileSize; *(uint32_t *) (&buffer[offset + 28]) = fileSize;
} }
void handleTsR(ts_channel_s *tsChannel, char *input) { void handleTsR(ts_channel_s *tsChannel, char *input) {
#if EFI_SIMULATOR
printf("TS_SD r %d\n", input[1]);
#endif // EFI_SIMULATOR
const uint16_t* data16 = reinterpret_cast<uint16_t*>(input); const uint16_t* data16 = reinterpret_cast<uint16_t*>(input);
if (input[0] == 0 && input[1] == TS_SD_PROTOCOL_RTC) { if (input[0] == 0 && input[1] == TS_SD_PROTOCOL_RTC) {
@ -72,7 +82,7 @@ void handleTsR(ts_channel_s *tsChannel, char *input) {
sr5SendResponse(tsChannel, TS_CRC, buffer, 9); sr5SendResponse(tsChannel, TS_CRC, buffer, 9);
} else if (input[0] == 0 && input[1] == TS_SD_PROTOCOL_FETCH_INFO) { } else if (input[0] == 0 && input[1] == TS_SD_PROTOCOL_FETCH_INFO) {
int length = data16[2]; uint16_t length = SWAP_UINT16(data16[2]);
scheduleMsg(&sharedLogger, "TS_SD: fetch buffer command, length=%d", length); scheduleMsg(&sharedLogger, "TS_SD: fetch buffer command, length=%d", length);
@ -109,21 +119,28 @@ void handleTsR(ts_channel_s *tsChannel, char *input) {
int index = 0; int index = 0;
struct dirent *de; // Pointer for directory entry struct dirent *de; // Pointer for directory entry
while ((de = readdir(dr)) != NULL) { while ((de = readdir(dr)) != NULL) {
printf("%s\n", de->d_name); const char * fileName = de->d_name;
printf("%s\n", fileName);
if (index >= DIR_RESPONSE_SIZE / 32) { if (index >= DIR_RESPONSE_SIZE / 32) {
break; break;
} }
if (isLogFile(de->d_name)) { if (isLogFile(fileName)) {
setFileEntry(buffer, index, de->d_name, 239); struct stat statBuffer;
int status;
int fileSize = 0;
status = stat(fileName, &statBuffer);
if (status == 0) {
fileSize = statBuffer.st_size;
}
setFileEntry(buffer, index, fileName, fileSize);
index++; index++;
} }
} }
closedir(dr); closedir(dr);
// setFileEntry(buffer, 0, "hello123.msq", 123123);
// setFileEntry(buffer, 1, "hello222.msq", 123123);
#endif // EFI_SIMULATOR #endif // EFI_SIMULATOR
sr5SendResponse(tsChannel, TS_CRC, buffer, 0x202); sr5SendResponse(tsChannel, TS_CRC, buffer, 0x202);
} }
@ -141,20 +158,74 @@ void handleTsR(ts_channel_s *tsChannel, char *input) {
void handleTsW(ts_channel_s *tsChannel, char *input) { void handleTsW(ts_channel_s *tsChannel, char *input) {
const uint16_t* data16 = reinterpret_cast<uint16_t*>(input); const uint16_t* data16 = reinterpret_cast<uint16_t*>(input);
#if EFI_SIMULATOR
printf("TS_SD w %d\n", input[1]);
#endif // EFI_SIMULATOR
if (input[0] == 0 && input[1] == TS_SD_PROTOCOL_FETCH_INFO) { if (input[0] == 0 && input[1] == TS_SD_PROTOCOL_FETCH_INFO) {
int code = data16[2]; int code = data16[2];
scheduleMsg(&sharedLogger, "TS_SD: w, code=%d", code); scheduleMsg(&sharedLogger, "TS_SD: w, code=%d", code);
if (input[5] == TS_SD_PROTOCOL_DO) { if (input[5] == TS_SD_PROTOCOL_DO) {
scheduleMsg(&sharedLogger, "TS_SD_PROTOCOL_DO");
sendOkResponse(tsChannel, TS_CRC); sendOkResponse(tsChannel, TS_CRC);
} else if (input[5] == TS_SD_PROTOCOL_READ_DIR) { } else if (input[5] == TS_SD_PROTOCOL_READ_DIR) {
scheduleMsg(&sharedLogger, "TS_SD_PROTOCOL_READ_DIR");
sendOkResponse(tsChannel, TS_CRC); sendOkResponse(tsChannel, TS_CRC);
} else if (input[5] == TS_SD_PROTOCOL_REMOVE_FILE) { } else if (input[5] == TS_SD_PROTOCOL_REMOVE_FILE) {
// todo #if EFI_SIMULATOR
DIR *dr = opendir(".");
if (dr == NULL) {
// opendir returns NULL if couldn't open directory
printf("Could not open current directory" );
return;
}
struct dirent *de; // Pointer for directory entry
while ((de = readdir(dr)) != NULL) {
const char * fileName = de->d_name;
printf("%s\n", fileName);
if (isLogFile(fileName)) {
int dotIndex = indexOf(fileName, DOT);
if (0 == strncmp(input + 6, &fileName[dotIndex - 4], 4)) {
printf("Removing %s\n", fileName);
remove(fileName);
break;
}
}
}
closedir(dr);
#endif // EFI_SIMULATOR
sendOkResponse(tsChannel, TS_CRC);
} else if (input[5] == TS_SD_PROTOCOL_FETCH_COMPRESSED) { } else if (input[5] == TS_SD_PROTOCOL_FETCH_COMPRESSED) {
} #if EFI_SIMULATOR
DIR *dr = opendir(".");
if (dr == NULL) {
// opendir returns NULL if couldn't open directory
printf("Could not open current directory" );
return;
}
struct dirent *de; // Pointer for directory entry
while ((de = readdir(dr)) != NULL) {
const char * fileName = de->d_name;
printf("%s\n", fileName);
if (isLogFile(fileName)) {
int dotIndex = indexOf(fileName, DOT);
if (0 == strncmp(input + 6, &fileName[dotIndex - 4], 4)) {
printf("Will be uploading %s\n", fileName);
break;
}
}
}
closedir(dr);
#endif // EFI_SIMULATOR
}
} else { } else {

View File

@ -12,7 +12,7 @@
#include "global.h" #include "global.h"
#include "efilib.h" #include "efilib.h"
#if EFI_UNIT_TEST #if EFI_UNIT_TEST || EFI_SIMULATOR
extern bool verboseMode; extern bool verboseMode;
#endif /* EFI_UNIT_TEST */ #endif /* EFI_UNIT_TEST */
@ -148,7 +148,7 @@ char * swapOutputBuffers(int *actualOutputBufferSize) {
* this is really 'global lock + printf + scheduleLogging + unlock' a bit more clear * this is really 'global lock + printf + scheduleLogging + unlock' a bit more clear
*/ */
void scheduleMsg(Logging *logging, const char *format, ...) { void scheduleMsg(Logging *logging, const char *format, ...) {
#if EFI_UNIT_TEST #if EFI_UNIT_TEST || EFI_SIMULATOR
if (verboseMode) { if (verboseMode) {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);

View File

@ -20,6 +20,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 final JPanel fileList = new JPanel(new VerticalFlowLayout());
private final JLabel status = new JLabel();
private LinkManager controllerConnector; private LinkManager controllerConnector;
@ -31,117 +32,129 @@ public class ConnectPanel {
connect.addActionListener(new ActionListener() { connect.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
connect.setEnabled(false);
controllerConnector = new LinkManager() controllerConnector = new LinkManager()
.setCompositeLogicEnabled(false) .setCompositeLogicEnabled(false)
.setNeedPullData(false); .setNeedPullData(false);
//controllerConnector.startAndConnect(":2390", ConnectionStateListener.VOID); //controllerConnector.startAndConnect(":2390", ConnectionStateListener.VOID);
controllerConnector.startAndConnect(":29001", ConnectionStateListener.VOID); controllerConnector.startAndConnect(":29001", new ConnectionStateListener() {
public void onConnectionEstablished() {
status.setText("Connected to rusEFI");
requestFileList();
}
public void onConnectionFailed() {
}
});
} }
}); });
JButton poke = new JButton("poke"); JButton poke = new JButton("poke");
poke.addActionListener(new ActionListener() { poke.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { requestFileList();
byte[] packet;
byte[] response;
IoStream stream = controllerConnector.getConnector().getBinaryProtocol().getStream();
packet = new byte[3];
packet[0] = Fields.TS_SD_R_COMMAND;
packet[2] = Fields.TS_SD_PROTOCOL_RTC;
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;
packet[2] = TS_SD_PROTOCOL_FETCH_INFO;
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;
packet[2] = TS_SD_PROTOCOL_FETCH_INFO;
packet[5] = 0x02;
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));
fileList.removeAll();
for (int fileIndex = 0; fileIndex < 512 / 32; fileIndex++) {
int offset = 32 * fileIndex;
String fileNamePart = new String(response, 1 + offset, 8).trim();
if (fileNamePart.trim().isEmpty())
break;
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();
JPanel filePanel = new JPanel(new FlowLayout());
filePanel.add(new JLabel(fileName + " " + size + " byte(s)"));
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);
fileList.add(filePanel);
System.out.println("Filename " + fileName + " size " + size);
AutoupdateUtil.trueLayout(content.getParent());
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
} }
}); });
flow.add(connect); flow.add(connect);
flow.add(poke); flow.add(poke);
content.add(flow, BorderLayout.NORTH); content.add(flow, BorderLayout.NORTH);
content.add(fileList, BorderLayout.CENTER); content.add(fileList, BorderLayout.CENTER);
content.add(status, BorderLayout.SOUTH);
}
private void requestFileList() {
try {
byte[] packet;
byte[] response;
IoStream stream = controllerConnector.getConnector().getBinaryProtocol().getStream();
packet = new byte[3];
packet[0] = Fields.TS_SD_R_COMMAND;
packet[2] = Fields.TS_SD_PROTOCOL_RTC;
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;
packet[2] = TS_SD_PROTOCOL_FETCH_INFO;
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;
packet[2] = TS_SD_PROTOCOL_FETCH_INFO;
packet[5] = 0x02;
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));
fileList.removeAll();
for (int fileIndex = 0; fileIndex < 512 / 32; fileIndex++) {
int offset = 32 * fileIndex;
String fileNamePart = new String(response, 1 + offset, 8).trim();
if (fileNamePart.trim().isEmpty())
break;
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();
JPanel filePanel = new JPanel(new FlowLayout());
filePanel.add(new JLabel(fileName + " " + size + " byte(s)"));
JButton download = new JButton("Download");
download.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
downloadFile(fileName);
}
});
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);
requestFileList();
}
});
filePanel.add(delete);
fileList.add(filePanel);
System.out.println("Filename " + fileName + " size " + size);
AutoupdateUtil.trueLayout(content.getParent());
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
} }
public static String getLastFour(String fileName) { public static String getLastFour(String fileName) {
@ -152,13 +165,21 @@ public class ConnectPanel {
return fileName.substring(fileName.length() - 4); return fileName.substring(fileName.length() - 4);
} }
private void downloadFile(String fileName) {
fileName = ConnectPanel.getLastFour(fileName);
}
private void deleteFile(String fileName) { private void deleteFile(String fileName) {
fileName = ConnectPanel.getLastFour(fileName); fileName = ConnectPanel.getLastFour(fileName);
byte[] packet = new byte[17]; byte[] packet = new byte[17];
packet[0] = Fields.TS_SD_W_COMMAND; packet[0] = Fields.TS_SD_W_COMMAND;
packet[2] = TS_SD_PROTOCOL_FETCH_INFO; packet[2] = TS_SD_PROTOCOL_FETCH_INFO;
packet[6] = Fields.TS_SD_PROTOCOL_READ_DIR; packet[6] = Fields.TS_SD_PROTOCOL_REMOVE_FILE;
for (int i = 0; i < 4; i++)
packet[7 + i] = (byte) fileName.charAt(i);
IoStream stream = controllerConnector.getConnector().getBinaryProtocol().getStream(); IoStream stream = controllerConnector.getConnector().getBinaryProtocol().getStream();
try { try {

Binary file not shown.

View File

@ -130,6 +130,8 @@ static void sd2_handler(eventid_t id) {
static evhandler_t fhandlers[] = { termination_handler, sd1_handler, sd2_handler }; static evhandler_t fhandlers[] = { termination_handler, sd1_handler, sd2_handler };
bool verboseMode = true;
/*------------------------------------------------------------------------* /*------------------------------------------------------------------------*
* Simulator main. * * Simulator main. *
*------------------------------------------------------------------------*/ *------------------------------------------------------------------------*/