if(writeUsb(nspListNames)){// list of the names goes...
logPrinter.print(" [send list itself]",EMsgType.FAIL);
returnfalse;
}
logPrinter.print(" [send list itself]",EMsgType.PASS);
returntrue;
}
/**
*AfterwesentcommandstoNS,thischainstarts
**/
privatebooleanproceedCommands(){
logPrinter.print("TF Awaiting for NS commands.",EMsgType.INFO);
/*byte[]magic=newbyte[4];
ByteBufferbb=StandardCharsets.UTF_8.encode("TUC0").rewind().get(magic);// Let's rephrase this 'string'
*/
finalbyte[]magic=newbyte[]{(byte)0x54,(byte)0x55,(byte)0x43,(byte)0x30};// eq. 'TUC0' @ UTF-8 (actually ASCII lol, u know what I mean)
byte[]receivedArray;
while(true){// Check if user interrupted process.
receivedArray=readUsb();
if(receivedArray==null)// catches error
returnfalse;
if(!Arrays.equals(Arrays.copyOfRange(receivedArray,0,4),magic))// Bytes from 0 to 3 should contain 'magic' TUC0, so must be verified like this
continue;
// 8th to 12th(explicits) bytes in returned data stands for command ID as unsigned integer (Little-endian). Actually, we have to compare arrays here, but in real world it can't be greater then 0/1/2, thus:
// BTW also protocol specifies 4th byte to be 0x00 kinda indicating that that this command is valid. But, as you may see, never happens other situation when it's not = 0.
if(receivedArray[8]==0x00){//0x00 - exit
logPrinter.print("TF Received 'EXIT' command. Terminating.",EMsgType.PASS);
returntrue;// All interaction with USB device should be ended (expected);
}
elseif((receivedArray[8]==0x01)||(receivedArray[8]==0x02)){//0x01 - file range; 0x02 unknown bug on backend side (dirty hack).
logPrinter.print("TF Received 'FILE RANGE' command. Proceeding: [0x0"+receivedArray[8]+"]",EMsgType.PASS);
/*// We can get in this pocket a length of file name (+32). Why +32? I dunno man.. Do we need this? Definitely not. This app can live without it.
logsArea.appendText("[V] Received FILE_RANGE command. Size: "+Long.toUnsignedString(receivedSize)+"\n");// this shit returns string that will be chosen next '+32'. And, BTW, can't be greater then 512
*/
if(!fileRangeCmd())
returnfalse;// catches exception
}
}
}
/**
*Thisiswhatreturnsrequestedfile(files)
*Executesmultipletimes
*@return'true'ifeverythingisok
*'false'iserror/exceptionoccurs
**/
privatebooleanfileRangeCmd(){
byte[]receivedArray;
// Here we take information of what other side wants
receivedArray=readUsb();
if(receivedArray==null)
returnfalse;
// range_offset of the requested file. In the begining it will be 0x10.
longreceivedRangeSize=ByteBuffer.wrap(Arrays.copyOfRange(receivedArray,0,8)).order(ByteOrder.LITTLE_ENDIAN).getLong();// Note - it could be unsigned long. Unfortunately, this app won't support files greater then 8796093022208 Gb
byte[]receivedRangeSizeRAW=Arrays.copyOfRange(receivedArray,0,8);// used (only) when we use sendResponse(). It's just simply.
longreceivedRangeOffset=ByteBuffer.wrap(Arrays.copyOfRange(receivedArray,8,16)).order(ByteOrder.LITTLE_ENDIAN).getLong();// Note - it could be unsigned long. Unfortunately, this app won't support files greater then 8796093022208 Gb
logPrinter.print("TF NullPointerException (in some moment application didn't find something. Something important.):\n "+npe.getMessage(),EMsgType.FAIL);
result=LibUsb.bulkTransfer(handlerNS,(byte)0x01,writeBuffer,writeBufTransferred,1000);// last one is TIMEOUT. 0 stands for unlimited. Endpoint OUT = 0x01
switch(result){
caseLibUsb.SUCCESS:
if(writeBufTransferred.get()==message.length)
returnfalse;
else{
logPrinter.print("TF Data transfer issue [write]\n Requested: "+message.length+"\n Transferred: "+writeBufTransferred.get(),EMsgType.FAIL);
returntrue;
}
caseLibUsb.ERROR_TIMEOUT:
continue;
default:
logPrinter.print("TF Data transfer issue [write]\n Returned: "+UsbErrorCodes.getErrCode(result),EMsgType.FAIL);