TS SD integration #1653
This commit is contained in:
parent
6eecba28b1
commit
2051551617
|
@ -20,6 +20,7 @@
|
||||||
|
|
||||||
#if EFI_FILE_LOGGING
|
#if EFI_FILE_LOGGING
|
||||||
#include "ff.h"
|
#include "ff.h"
|
||||||
|
#define ROOT_DIR "/"
|
||||||
#endif // EFI_FILE_LOGGING
|
#endif // EFI_FILE_LOGGING
|
||||||
|
|
||||||
#define DIR_RESPONSE_SIZE 512
|
#define DIR_RESPONSE_SIZE 512
|
||||||
|
@ -35,7 +36,8 @@
|
||||||
#include "efilib.h"
|
#include "efilib.h"
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
|
|
||||||
EXTERN_ENGINE;
|
EXTERN_ENGINE
|
||||||
|
;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* for funny reasons file name has to be at least 4 symbols before the dot
|
* for funny reasons file name has to be at least 4 symbols before the dot
|
||||||
|
@ -69,13 +71,14 @@ extern LoggingWithStorage sharedLogger;
|
||||||
|
|
||||||
static uint8_t buffer[TRANSFER_SIZE + 2];
|
static uint8_t buffer[TRANSFER_SIZE + 2];
|
||||||
|
|
||||||
static void setFileEntry(uint8_t *buffer, int index, const char *fileName, int fileSize) {
|
static void setFileEntry(uint8_t *buffer, int index, const char *fileName,
|
||||||
|
int fileSize) {
|
||||||
int offset = 32 * index;
|
int offset = 32 * index;
|
||||||
|
|
||||||
int dotIndex = indexOf(fileName, DOT);
|
int dotIndex = indexOf(fileName, DOT);
|
||||||
// assert dotIndex != -1
|
// assert dotIndex != -1
|
||||||
memcpy(buffer + offset, fileName, dotIndex);
|
memcpy(buffer + offset, fileName, dotIndex);
|
||||||
for (int i = dotIndex; i < 8 ; i++) {
|
for (int i = dotIndex; i < 8; i++) {
|
||||||
buffer[offset + i] = 0;
|
buffer[offset + i] = 0;
|
||||||
}
|
}
|
||||||
memcpy(buffer + offset + 8, &fileName[dotIndex + 1], 3);
|
memcpy(buffer + offset + 8, &fileName[dotIndex + 1], 3);
|
||||||
|
@ -86,7 +89,7 @@ static void setFileEntry(uint8_t *buffer, int index, const char *fileName, int f
|
||||||
buffer[offset + 18 + i] = fileName[dotIndex + i - 4];
|
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) {
|
||||||
|
@ -94,7 +97,7 @@ void handleTsR(ts_channel_s *tsChannel, char *input) {
|
||||||
printf("TS_SD r %d\n", input[1]);
|
printf("TS_SD r %d\n", input[1]);
|
||||||
#endif // EFI_SIMULATOR
|
#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) {
|
||||||
scheduleMsg(&sharedLogger, "TS_SD: RTC read command");
|
scheduleMsg(&sharedLogger, "TS_SD: RTC read command");
|
||||||
|
@ -103,8 +106,8 @@ void handleTsR(ts_channel_s *tsChannel, char *input) {
|
||||||
|
|
||||||
} else if (input[0] == 0 && input[1] == TS_SD_PROTOCOL_FETCH_INFO) {
|
} else if (input[0] == 0 && input[1] == TS_SD_PROTOCOL_FETCH_INFO) {
|
||||||
uint16_t length = SWAP_UINT16(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);
|
||||||
|
|
||||||
if (length == 16) {
|
if (length == 16) {
|
||||||
buffer[0] = 1 + 4; // Card present + Ready
|
buffer[0] = 1 + 4; // Card present + Ready
|
||||||
|
@ -126,7 +129,6 @@ void handleTsR(ts_channel_s *tsChannel, char *input) {
|
||||||
// SD read directory command
|
// SD read directory command
|
||||||
memset(buffer, 0, DIR_RESPONSE_BUFFER_SIZE);
|
memset(buffer, 0, DIR_RESPONSE_BUFFER_SIZE);
|
||||||
|
|
||||||
|
|
||||||
#if EFI_SIMULATOR
|
#if EFI_SIMULATOR
|
||||||
DIR *dr = opendir(".");
|
DIR *dr = opendir(".");
|
||||||
if (dr == NULL) {
|
if (dr == NULL) {
|
||||||
|
@ -160,11 +162,10 @@ void handleTsR(ts_channel_s *tsChannel, char *input) {
|
||||||
|
|
||||||
#endif // EFI_SIMULATOR
|
#endif // EFI_SIMULATOR
|
||||||
|
|
||||||
|
|
||||||
#if EFI_FILE_LOGGING
|
#if EFI_FILE_LOGGING
|
||||||
LOCK_SD_SPI;
|
LOCK_SD_SPI;
|
||||||
DIR dir;
|
DIR dir;
|
||||||
FRESULT res = f_opendir(&dir, "/");
|
FRESULT res = f_opendir(&dir, ROOT_DIR);
|
||||||
if (res != FR_OK) {
|
if (res != FR_OK) {
|
||||||
scheduleMsg(&sharedLogger, "Error opening directory");
|
scheduleMsg(&sharedLogger, "Error opening directory");
|
||||||
UNLOCK_SD_SPI;
|
UNLOCK_SD_SPI;
|
||||||
|
@ -191,22 +192,28 @@ void handleTsR(ts_channel_s *tsChannel, char *input) {
|
||||||
// fileSize = statBuffer.st_size;
|
// fileSize = statBuffer.st_size;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
setFileEntry(buffer, index, fileName, 1);
|
FILINFO fileInfo;
|
||||||
|
// todo: handle return value?
|
||||||
|
f_stat(fileName, &fileInfo);
|
||||||
|
|
||||||
|
setFileEntry(buffer, index, fileName,
|
||||||
|
(int) fileInfo.fsize);
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
UNLOCK_SD_SPI;
|
UNLOCK_SD_SPI;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // EFI_FILE_LOGGING
|
#endif // EFI_FILE_LOGGING
|
||||||
|
|
||||||
sr5SendResponse(tsChannel, TS_CRC, buffer, DIR_RESPONSE_BUFFER_SIZE);
|
sr5SendResponse(tsChannel, TS_CRC, buffer,
|
||||||
|
DIR_RESPONSE_BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
} else if (input[0] == 0 && input[1] == TS_SD_PROTOCOL_FETCH_DATA) {
|
} else if (input[0] == 0 && input[1] == TS_SD_PROTOCOL_FETCH_DATA) {
|
||||||
uint16_t blockNumber = SWAP_UINT16(data16[1]);
|
uint16_t blockNumber = SWAP_UINT16(data16[1]);
|
||||||
scheduleMsg(&sharedLogger, "TS_SD: fetch data command blockNumber=%d", blockNumber);
|
scheduleMsg(&sharedLogger, "TS_SD: fetch data command blockNumber=%d",
|
||||||
|
blockNumber);
|
||||||
|
|
||||||
// int offset = blockNumber * TRANSFER_SIZE;
|
// int offset = blockNumber * TRANSFER_SIZE;
|
||||||
|
|
||||||
|
@ -221,7 +228,7 @@ void handleTsR(ts_channel_s *tsChannel, char *input) {
|
||||||
#if EFI_FILE_LOGGING
|
#if EFI_FILE_LOGGING
|
||||||
LOCK_SD_SPI;
|
LOCK_SD_SPI;
|
||||||
got = 0;
|
got = 0;
|
||||||
f_read(&uploading, (void*)&buffer[2], TRANSFER_SIZE, (UINT*)&got);
|
f_read(&uploading, (void*) &buffer[2], TRANSFER_SIZE, (UINT*) &got);
|
||||||
UNLOCK_SD_SPI;
|
UNLOCK_SD_SPI;
|
||||||
#endif // EFI_FILE_LOGGING
|
#endif // EFI_FILE_LOGGING
|
||||||
|
|
||||||
|
@ -232,7 +239,7 @@ 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
|
#if EFI_SIMULATOR
|
||||||
printf("TS_SD w %d\n", input[1]);
|
printf("TS_SD w %d\n", input[1]);
|
||||||
|
@ -242,7 +249,6 @@ void handleTsW(ts_channel_s *tsChannel, char *input) {
|
||||||
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");
|
scheduleMsg(&sharedLogger, "TS_SD_PROTOCOL_DO");
|
||||||
sendOkResponse(tsChannel, TS_CRC);
|
sendOkResponse(tsChannel, TS_CRC);
|
||||||
|
@ -252,13 +258,11 @@ void handleTsW(ts_channel_s *tsChannel, char *input) {
|
||||||
} else if (input[5] == TS_SD_PROTOCOL_REMOVE_FILE) {
|
} else if (input[5] == TS_SD_PROTOCOL_REMOVE_FILE) {
|
||||||
#if EFI_SIMULATOR
|
#if EFI_SIMULATOR
|
||||||
DIR *dr = opendir(".");
|
DIR *dr = opendir(".");
|
||||||
|
|
||||||
if (dr == NULL) {
|
if (dr == NULL) {
|
||||||
// opendir returns NULL if couldn't open directory
|
// opendir returns NULL if couldn't open directory
|
||||||
printf("Could not open current directory" );
|
printf("Could not open current directory" );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct dirent *de; // Pointer for directory entry
|
struct dirent *de; // Pointer for directory entry
|
||||||
while ((de = readdir(dr)) != NULL) {
|
while ((de = readdir(dr)) != NULL) {
|
||||||
const char * fileName = de->d_name;
|
const char * fileName = de->d_name;
|
||||||
|
@ -273,8 +277,35 @@ void handleTsW(ts_channel_s *tsChannel, char *input) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
closedir(dr);
|
closedir(dr);
|
||||||
|
|
||||||
#endif // EFI_SIMULATOR
|
#endif // EFI_SIMULATOR
|
||||||
|
|
||||||
|
#if EFI_FILE_LOGGING
|
||||||
|
LOCK_SD_SPI;
|
||||||
|
DIR dir;
|
||||||
|
FRESULT res = f_opendir(&dir, ROOT_DIR);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
scheduleMsg(&sharedLogger, "Error opening directory");
|
||||||
|
} else {
|
||||||
|
while (true) {
|
||||||
|
FILINFO fno;
|
||||||
|
res = f_readdir(&dir, &fno);
|
||||||
|
char *fileName = fno.fname;
|
||||||
|
if (res != FR_OK || fileName[0] == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (isLogFile(fileName)) {
|
||||||
|
int dotIndex = indexOf(fileName, DOT);
|
||||||
|
if (0 == strncmp(input + 6, &fileName[dotIndex - 4], 4)) {
|
||||||
|
scheduleMsg(&sharedLogger, "Removing %s", fileName);
|
||||||
|
f_unlink(fileName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UNLOCK_SD_SPI;
|
||||||
|
#endif // EFI_FILE_LOGGING
|
||||||
|
|
||||||
sendOkResponse(tsChannel, TS_CRC);
|
sendOkResponse(tsChannel, TS_CRC);
|
||||||
|
|
||||||
} else if (input[5] == TS_SD_PROTOCOL_FETCH_COMPRESSED) {
|
} else if (input[5] == TS_SD_PROTOCOL_FETCH_COMPRESSED) {
|
||||||
|
@ -311,7 +342,7 @@ void handleTsW(ts_channel_s *tsChannel, char *input) {
|
||||||
LOCK_SD_SPI;
|
LOCK_SD_SPI;
|
||||||
|
|
||||||
DIR dir;
|
DIR dir;
|
||||||
FRESULT res = f_opendir(&dir, "/");
|
FRESULT res = f_opendir(&dir, ROOT_DIR);
|
||||||
|
|
||||||
if (res != FR_OK) {
|
if (res != FR_OK) {
|
||||||
scheduleMsg(&sharedLogger, "Error opening directory");
|
scheduleMsg(&sharedLogger, "Error opening directory");
|
||||||
|
@ -328,7 +359,7 @@ void handleTsW(ts_channel_s *tsChannel, char *input) {
|
||||||
if (isLogFile(fileName)) {
|
if (isLogFile(fileName)) {
|
||||||
int dotIndex = indexOf(fileName, DOT);
|
int dotIndex = indexOf(fileName, DOT);
|
||||||
if (0 == strncmp(input + 6, &fileName[dotIndex - 4], 4)) {
|
if (0 == strncmp(input + 6, &fileName[dotIndex - 4], 4)) {
|
||||||
FRESULT err = f_open(&uploading, fileName, FA_READ); // This file has the index for next log file name
|
FRESULT err = f_open(&uploading, fileName, FA_READ);// This file has the index for next log file name
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -336,10 +367,8 @@ void handleTsW(ts_channel_s *tsChannel, char *input) {
|
||||||
UNLOCK_SD_SPI;
|
UNLOCK_SD_SPI;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif // EFI_FILE_LOGGING
|
#endif // EFI_FILE_LOGGING
|
||||||
|
|
||||||
|
|
||||||
sendOkResponse(tsChannel, TS_CRC);
|
sendOkResponse(tsChannel, TS_CRC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,5 +377,4 @@ void handleTsW(ts_channel_s *tsChannel, char *input) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif // EFI_FILE_LOGGING
|
#endif // EFI_FILE_LOGGING
|
||||||
|
|
|
@ -165,6 +165,8 @@ public class SdCardReader {
|
||||||
|
|
||||||
int totalSize = 0;
|
int totalSize = 0;
|
||||||
|
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
packet = new byte[17];
|
packet = new byte[17];
|
||||||
packet[0] = Fields.TS_SD_R_COMMAND;
|
packet[0] = Fields.TS_SD_R_COMMAND;
|
||||||
|
@ -180,17 +182,17 @@ public class SdCardReader {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Got content package size " + response.length);
|
|
||||||
|
|
||||||
|
|
||||||
int dataBytes = response.length - TRANSFER_HEADER_SIZE;
|
int dataBytes = response.length - TRANSFER_HEADER_SIZE;
|
||||||
fos.write(response, TRANSFER_HEADER_SIZE, dataBytes);
|
|
||||||
|
|
||||||
totalSize += dataBytes;
|
totalSize += dataBytes;
|
||||||
|
|
||||||
|
System.out.println("Got content package size " + response.length + "/total=" + totalSize);
|
||||||
|
|
||||||
|
fos.write(response, TRANSFER_HEADER_SIZE, dataBytes);
|
||||||
|
|
||||||
if (dataBytes != 2048) {
|
if (dataBytes != 2048) {
|
||||||
System.out.println(response.length + " must be the last packet");
|
System.out.println(response.length + " must be the last packet");
|
||||||
status.setText(fileName + " downloaded " + totalSize + " byte(s)");
|
long duration = System.currentTimeMillis() - start;
|
||||||
|
status.setText(fileName + " downloaded " + totalSize + " byte(s) in " + duration);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
chunk++;
|
chunk++;
|
||||||
|
|
Loading…
Reference in New Issue