rewrite timeouts

This commit is contained in:
Dmitry Isaenko 2020-05-12 15:48:40 +03:00
parent 4236ccdb06
commit c76487617c

View file

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