From c76487617c1f7692cf5ab01245953b60ede74725 Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Tue, 12 May 2020 15:48:40 +0300 Subject: [PATCH] rewrite timeouts --- .../nsusbloader/Utilities/NxdtUsbAbi1.java | 72 ++++++++++--------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java b/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java index 936f383..c8f9105 100644 --- a/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java +++ b/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java @@ -22,7 +22,6 @@ import javafx.concurrent.Task; import nsusbloader.COM.USB.UsbErrorCodes; import nsusbloader.ModelControllers.LogPrinter; import nsusbloader.NSLDataTypes.EMsgType; -import nsusbloader.RainbowHexDump; import org.usb4java.DeviceHandle; import org.usb4java.LibUsb; @@ -42,7 +41,7 @@ class NxdtUsbAbi1 { private boolean isWindows; private boolean isWindows10; - private static final int NXDT_MAX_COMMAND_SIZE = 0x1000; + private static final int NXDT_MAX_DIRECTIVE_SIZE = 0x1000; private static final int NXDT_FILE_CHUNK_SIZE = 0x800000; private static final int NXDT_FILE_PROPERTIES_MAX_NAME_LENGTH = 0x300; @@ -53,6 +52,8 @@ class NxdtUsbAbi1 { private static final int CMD_SEND_FILE_PROPERTIES = 1; private static final int CMD_ENDSESSION = 3; + private boolean nxdtCanDie; + // Standard set of possible replies private static final byte[] USBSTATUS_SUCCESS = { 0x4e, 0x58, 0x44, 0x54, 0x00, 0x00, 0x00, 0x00, @@ -119,6 +120,7 @@ class NxdtUsbAbi1 { switch (command){ case CMD_HANDSHAKE: performHandshake(directive); + nxdtCanDie = true; break; case CMD_SEND_FILE_PROPERTIES: handleSendFileProperties(directive); @@ -190,7 +192,6 @@ class NxdtUsbAbi1 { return; } - logPrinter.print("Receiving: '"+filename+"' ("+fileSize+" b)", EMsgType.INFO); // If RomFs related if (isRomFs(filename)) { if (isWindows) @@ -200,8 +201,10 @@ class NxdtUsbAbi1 { createPath(filename); } - else - filename = saveToPath + filename; + else { + logPrinter.print("Receiving: '"+filename+"' ("+fileSize+" b)", EMsgType.INFO); + filename = saveToPath + filename; + } File fileToDump = new File(filename); // Check if enough space @@ -264,20 +267,13 @@ class NxdtUsbAbi1 { long received = 0; int bufferSize; - System.out.println("|"+file.getAbsolutePath()+"|"); - System.out.println(String.format("R: %10d / S: %10d", received, size)); - while (received < size){ readBuffer = readUsbFile(); - System.out.println("Size: "+readBuffer.length); bos.write(readBuffer); - System.out.println("Write OK"); bufferSize = readBuffer.length; received += bufferSize; - System.out.println("calculations done"); logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0); } - System.out.println("Done "+file.getName()); logPrinter.updateProgress(1.0); bos.close(); } @@ -317,27 +313,25 @@ class NxdtUsbAbi1 { ByteBuffer writeBuffer = ByteBuffer.allocateDirect(message.length); writeBuffer.put(message); IntBuffer writeBufTransferred = IntBuffer.allocate(1); - int result; - while (! task.isCancelled()) { - result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 5050); + if ( task.isCancelled()) + throw new InterruptedException("Execution interrupted"); - switch (result){ - case LibUsb.SUCCESS: - if (writeBufTransferred.get() == message.length) - return; - throw new Exception("Data transfer issue [write]" + - "\n Requested: "+message.length+ - "\n Transferred: "+writeBufTransferred.get()); - case LibUsb.ERROR_TIMEOUT: - continue; - default: - throw new Exception("Data transfer issue [write]" + - "\n Returned: "+ UsbErrorCodes.getErrCode(result) + - "\n (execution stopped)"); - } + int result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 5050); + + switch (result){ + case LibUsb.SUCCESS: + if (writeBufTransferred.get() == message.length) + return; + throw new Exception("Data transfer issue [write]" + + "\n Requested: "+message.length+ + "\n Transferred: "+writeBufTransferred.get()); + default: + throw new Exception("Data transfer issue [write]" + + "\n Returned: "+ UsbErrorCodes.getErrCode(result) + + "\n (execution stopped)"); } - throw new InterruptedException("Execution interrupted"); + } /** * Reading what USB device responded (command). @@ -345,10 +339,11 @@ class NxdtUsbAbi1 { * 'null' if read failed * */ private byte[] readUsbDirective() throws Exception{ - ByteBuffer readBuffer = ByteBuffer.allocateDirect(NXDT_MAX_COMMAND_SIZE); + ByteBuffer readBuffer = ByteBuffer.allocateDirect(NXDT_MAX_DIRECTIVE_SIZE); // We can limit it to 32 bytes, but there is a non-zero chance to got OVERFLOW from libusb. IntBuffer readBufTransferred = IntBuffer.allocate(1); int result; + int countDown = 0; while (! task.isCancelled()) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81 @@ -359,7 +354,14 @@ class NxdtUsbAbi1 { readBuffer.get(receivedBytes); return receivedBytes; case LibUsb.ERROR_TIMEOUT: - continue; + if (nxdtCanDie){ + if (countDown < 5) { + countDown++; + break; + } + } + else + break; default: throw new Exception("Data transfer issue [read command]" + "\n Returned: " + UsbErrorCodes.getErrCode(result)+ @@ -377,7 +379,8 @@ class NxdtUsbAbi1 { ByteBuffer readBuffer = ByteBuffer.allocateDirect(NXDT_FILE_CHUNK_SIZE); IntBuffer readBufTransferred = IntBuffer.allocate(1); int result; - while (! task.isCancelled()) { + int countDown = 0; + while (! task.isCancelled() && countDown < 5) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); switch (result) { @@ -387,7 +390,8 @@ class NxdtUsbAbi1 { readBuffer.get(receivedBytes); return receivedBytes; case LibUsb.ERROR_TIMEOUT: - continue; + countDown++; + break; default: throw new Exception("Data transfer issue [read file]" + "\n Returned: " + UsbErrorCodes.getErrCode(result)+