ns-usbloader/src/main/java/nsusbloader/COM/USB/UsbCommunications.java
Dmitry Isaenko 8da07a37ba Refactor save-settings chain on exit (see Controllers package)
Remove concept of 'OldGoldleaf version'. Now all supported version of GoldLeaf listed in Settings tab and one of them has to be always selected.
Add GoldLeaf CLI support
Remove unnecessary tails of 'extras' from CLI Tinfoil/Awoo NET mode.
2020-07-07 02:20:57 +03:00

107 lines
No EOL
3.1 KiB
Java

/*
Copyright 2019-2020 Dmitry Isaenko
This file is part of NS-USBloader.
NS-USBloader is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NS-USBloader is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
*/
package nsusbloader.COM.USB;
import nsusbloader.COM.ICommunications;
import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.ModelControllers.Log;
import nsusbloader.NSLDataTypes.EFileStatus;
import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.NSLDataTypes.EMsgType;
import org.usb4java.*;
import java.io.*;
import java.util.*;
// TODO: add filter option to show only NSP files
public class UsbCommunications implements ICommunications {
private ILogPrinter logPrinter;
private LinkedHashMap<String, File> nspMap;
private String protocol;
private boolean nspFilterForGl;
private volatile boolean cancel;
public UsbCommunications(List<File> nspList, String protocol, boolean filterNspFilesOnlyForGl){
this.protocol = protocol;
this.nspFilterForGl = filterNspFilesOnlyForGl;
this.nspMap = new LinkedHashMap<>();
for (File f: nspList)
nspMap.put(f.getName(), f);
this.logPrinter = Log.getPrinter(EModule.USB_NET_TRANSFERS);
}
@Override
public void run() {
logPrinter.print("\tStart", EMsgType.INFO);
UsbConnect usbConnect = UsbConnect.connectHomebrewMode(logPrinter);
if (! usbConnect.isConnected()){
close(EFileStatus.FAILED);
return;
}
DeviceHandle handler = usbConnect.getNsHandler();
TransferModule module;
switch (protocol) {
case "TinFoil":
module = new TinFoil(handler, nspMap, this, logPrinter);
break;
case "GoldLeafv0.8":
module = new GoldLeaf_08(handler, nspMap, this, logPrinter, nspFilterForGl);
break;
case "GoldLeafv0.7.x":
module = new GoldLeaf_07(handler, nspMap, this, logPrinter, nspFilterForGl);
break;
default:
module = new GoldLeaf_05(handler, nspMap, this, logPrinter);
break;
}
usbConnect.close();
close(module.getStatus());
return;
}
/**
* Report status and close
*/
private void close(EFileStatus status){
logPrinter.update(nspMap, status);
logPrinter.print("\tEnd", EMsgType.INFO);
logPrinter.close();
}
@Override
public boolean isCancelled() {
return cancel;
}
@Override
public void cancel() {
cancel = true;
}
}