Update NXDT-related part

This commit is contained in:
Dmitry Isaenko 2020-10-27 20:53:52 +03:00
parent 98822de559
commit eb07ab1df8

View file

@ -35,7 +35,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
class NxdtUsbAbi1 { class NxdtUsbAbi1 {
private final ILogPrinter logPrinter; private final ILogPrinter logPrinter;
@ -86,7 +85,7 @@ class NxdtUsbAbi1 {
private static final int NXDT_USB_TIMEOUT = 5000; private static final int NXDT_USB_TIMEOUT = 5000;
private HashMap<String, NxdtNspFile> nspFiles; private NxdtNspFile nspFile;
public NxdtUsbAbi1(DeviceHandle handler, public NxdtUsbAbi1(DeviceHandle handler,
ILogPrinter logPrinter, ILogPrinter logPrinter,
@ -96,7 +95,6 @@ class NxdtUsbAbi1 {
this.handlerNS = handler; this.handlerNS = handler;
this.logPrinter = logPrinter; this.logPrinter = logPrinter;
this.parent = parent; this.parent = parent;
this.nspFiles = new HashMap<>();
this.isWindows = System.getProperty("os.name").toLowerCase().contains("windows"); this.isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
if (isWindows) if (isWindows)
@ -213,7 +211,7 @@ class NxdtUsbAbi1 {
String filename = new String(message, 0x20, fileNameLen, StandardCharsets.UTF_8); String filename = new String(message, 0x20, fileNameLen, StandardCharsets.UTF_8);
String absoluteFilePath = getAbsoluteFilePath(filename); String absoluteFilePath = getAbsoluteFilePath(filename);
File fileToDump = new File(absoluteFilePath); File fileToDump = new File(absoluteFilePath);
NxdtNspFile nspFile = nspFiles.get(filename); // it could be null, but it's not a problem.
boolean nspTransferMode = false; boolean nspTransferMode = false;
if (checkSizes(fullSize, headerSize)) if (checkSizes(fullSize, headerSize))
@ -228,7 +226,7 @@ class NxdtUsbAbi1 {
if (headerSize > 0){ // if NSP if (headerSize > 0){ // if NSP
logPrinter.print("Receiving NSP file entry: '"+filename+"' ("+fullSize+" b)", EMsgType.INFO); logPrinter.print("Receiving NSP file entry: '"+filename+"' ("+fullSize+" b)", EMsgType.INFO);
if (nspFiles.containsKey(filename)){ if (filename.equals(nspFile.getName())){
nspTransferMode = true; nspTransferMode = true;
} }
else { else {
@ -265,11 +263,13 @@ class NxdtUsbAbi1 {
private boolean checkSizes(long fileSize, int headerSize) throws Exception{ private boolean checkSizes(long fileSize, int headerSize) throws Exception{
if (fileSize >= headerSize){ if (fileSize >= headerSize){
logPrinter.print("File size should not be equal to header size for NSP files!", EMsgType.FAIL); logPrinter.print("File size should not be equal to header size for NSP files!", EMsgType.FAIL);
resetNsp();
writeUsb(USBSTATUS_MALFORMED_REQUEST); writeUsb(USBSTATUS_MALFORMED_REQUEST);
return true; return true;
} }
if (fileSize < 0){ // It's possible to have files of zero-length, so only less is the problem if (fileSize < 0){ // It's possible to have files of zero-length, so only less is the problem
logPrinter.print("File size should not be less then zero!", EMsgType.FAIL); logPrinter.print("File size should not be less then zero!", EMsgType.FAIL);
resetNsp();
writeUsb(USBSTATUS_MALFORMED_REQUEST); writeUsb(USBSTATUS_MALFORMED_REQUEST);
return true; return true;
} }
@ -294,8 +294,7 @@ class NxdtUsbAbi1 {
} }
private void createNewNsp(String filename, int headerSize, long fileSize, File fileToDump) throws Exception{ private void createNewNsp(String filename, int headerSize, long fileSize, File fileToDump) throws Exception{
try { try {
NxdtNspFile nsp = new NxdtNspFile(filename, headerSize, fileSize, fileToDump); nspFile = new NxdtNspFile(filename, headerSize, fileSize, fileToDump);
nspFiles.putIfAbsent(filename, nsp);
} }
catch (Exception e){ catch (Exception e){
logPrinter.print(e.getMessage(), EMsgType.FAIL); logPrinter.print(e.getMessage(), EMsgType.FAIL);
@ -334,8 +333,6 @@ class NxdtUsbAbi1 {
} }
} }
// @see https://bugs.openjdk.java.net/browse/JDK-8146538 // @see https://bugs.openjdk.java.net/browse/JDK-8146538
private void dumpFile(File file, long size) throws Exception{ private void dumpFile(File file, long size) throws Exception{
FileOutputStream fos = new FileOutputStream(file, true); FileOutputStream fos = new FileOutputStream(file, true);
@ -404,7 +401,8 @@ class NxdtUsbAbi1 {
private void handleSendNspHeader(byte[] message) throws Exception{ private void handleSendNspHeader(byte[] message) throws Exception{
final int headerSize = getLEint(message, 0x8); final int headerSize = getLEint(message, 0x8);
NxdtNspFile nsp = nspFiles.remove( null ); // <---------------------- //TODO: PLEASE PUT FILENAME HERE NxdtNspFile nsp = nspFile;
resetNsp();
if (nsp == null) { if (nsp == null) {
writeUsb(USBSTATUS_MALFORMED_REQUEST); writeUsb(USBSTATUS_MALFORMED_REQUEST);
@ -420,7 +418,7 @@ class NxdtUsbAbi1 {
if (headerSize != nsp.getHeaderSize()) { if (headerSize != nsp.getHeaderSize()) {
writeUsb(USBSTATUS_MALFORMED_REQUEST); writeUsb(USBSTATUS_MALFORMED_REQUEST);
logPrinter.print("Received NSP header size mismatch! "+headerSize+" != "+nsp.getHeaderSize(), EMsgType.FAIL); logPrinter.print("Received NSP header size mismatch! "+headerSize+" != "+ nsp.getHeaderSize(), EMsgType.FAIL);
return; return;
} }
@ -432,6 +430,9 @@ class NxdtUsbAbi1 {
writeUsb(USBSTATUS_SUCCESS); writeUsb(USBSTATUS_SUCCESS);
} }
private void resetNsp(){
this.nspFile = null;
}
/** Sending any byte array to USB device **/ /** Sending any byte array to USB device **/
private void writeUsb(byte[] message) throws Exception{ private void writeUsb(byte[] message) throws Exception{