Skip to content

Commit ba4afa0

Browse files
committed
NXDT-related updates
1 parent ab1b5b1 commit ba4afa0

File tree

8 files changed

+400
-45
lines changed

8 files changed

+400
-45
lines changed

src/main/java/nsusbloader/COM/USB/UsbConnect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package nsusbloader.COM.USB;
2020

21+
import nsusbloader.COM.USB.common.DeviceInformation;
2122
import nsusbloader.ModelControllers.ILogPrinter;
2223
import nsusbloader.NSLDataTypes.EMsgType;
2324
import org.usb4java.*;
@@ -88,6 +89,7 @@ public static UsbConnect connectHomebrewMode(ILogPrinter logPrinter){
8889
usbConnect.connected = true;
8990
}
9091
catch (Exception e){
92+
e.printStackTrace();
9193
logPrinter.print(e.getMessage(), EMsgType.FAIL);
9294
usbConnect.close();
9395
}
@@ -193,12 +195,10 @@ private void setConfiguration(int configuration) throws Exception{
193195
throw new Exception("Unable to set active configuration on device: "+UsbErrorCodes.getErrCode(returningValue));
194196
}
195197
private void claimInterface() throws Exception{
196-
// Claim interface
197198
returningValue = LibUsb.claimInterface(handlerNS, DEFAULT_INTERFACE);
198199
if (returningValue != LibUsb.SUCCESS)
199200
throw new Exception("Claim interface failure: "+UsbErrorCodes.getErrCode(returningValue));
200201
}
201-
202202
/**
203203
* Get USB status
204204
* @return status of connection
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Copyright 2019-2020 Dmitry Isaenko
3+
4+
This file is part of NS-USBloader.
5+
6+
NS-USBloader is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
NS-USBloader is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package nsusbloader.COM.USB.common;
20+
21+
import nsusbloader.COM.USB.UsbErrorCodes;
22+
import org.usb4java.*;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
public class DeviceInformation {
28+
private static final byte DEFAULT_IN_EP_ADDRESS = -127; // 0x81
29+
private static final byte DEFAULT_OUT_EP_ADDRESS = 1;
30+
31+
private Device device;
32+
private ConfigDescriptor configDescriptor;
33+
private List<NsUsbInterface> interfacesInformation = new ArrayList<>();
34+
35+
private DeviceInformation(){}
36+
37+
public static DeviceInformation build(DeviceHandle handler) throws Exception{
38+
Device device = LibUsb.getDevice(handler);
39+
return DeviceInformation.build(device);
40+
}
41+
public static DeviceInformation build(Device device) throws Exception{
42+
DeviceInformation deviceInformation = new DeviceInformation();
43+
deviceInformation.device = device;
44+
deviceInformation.claimConfigurationDescriptor();
45+
deviceInformation.collectInterfaces();
46+
deviceInformation.freeConfigurationDescriptor();
47+
return deviceInformation;
48+
}
49+
50+
private void claimConfigurationDescriptor() throws Exception{
51+
configDescriptor = new ConfigDescriptor();
52+
int returningValue = LibUsb.getActiveConfigDescriptor(device, configDescriptor);
53+
54+
if (returningValue != LibUsb.SUCCESS)
55+
throw new Exception("Get Active config descriptor failed: "+ UsbErrorCodes.getErrCode(returningValue));
56+
}
57+
58+
private void collectInterfaces(){
59+
for (Interface intrface : configDescriptor.iface())
60+
interfacesInformation.add(new NsUsbInterface(intrface));
61+
}
62+
63+
private void freeConfigurationDescriptor(){
64+
LibUsb.freeConfigDescriptor(configDescriptor);
65+
}
66+
67+
/** Bulk transfer endpoint IN */
68+
public NsUsbEndpointDescriptor getSimplifiedDefaultEndpointDescriptorIn() throws Exception{
69+
return getSimplifiedDefaultEndpointDescriptor(true);
70+
}
71+
/** Bulk transfer endpoint OUT */
72+
public NsUsbEndpointDescriptor getSimplifiedDefaultEndpointDescriptorOut() throws Exception{
73+
return getSimplifiedDefaultEndpointDescriptor(false);
74+
}
75+
76+
private NsUsbEndpointDescriptor getSimplifiedDefaultEndpointDescriptor(boolean isDescriptorIN) throws Exception{
77+
byte endpointAddress;
78+
79+
if (isDescriptorIN)
80+
endpointAddress = DEFAULT_IN_EP_ADDRESS;
81+
else
82+
endpointAddress = DEFAULT_OUT_EP_ADDRESS;
83+
84+
NsUsbInterface nsUsbInterface = interfacesInformation.get(0);
85+
86+
NsUsbInterfaceDescriptor firstInterfaceDescriptor = nsUsbInterface.getInterfaceDescriptors()[0];
87+
NsUsbEndpointDescriptor[] endpointDescriptors = firstInterfaceDescriptor.getEndpointDescriptors();
88+
89+
for (NsUsbEndpointDescriptor epDescriptor : endpointDescriptors){
90+
if (epDescriptor.getbEndpointAddress() == endpointAddress)
91+
return epDescriptor;
92+
}
93+
throw new Exception("No "+(isDescriptorIN?"IN":"OUT")+" endpoint descriptors found on default interface");
94+
}
95+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2019-2020 Dmitry Isaenko
3+
4+
This file is part of NS-USBloader.
5+
6+
NS-USBloader is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
NS-USBloader is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package nsusbloader.COM.USB.common;
20+
21+
import org.usb4java.EndpointDescriptor;
22+
23+
public class NsUsbEndpointDescriptor {
24+
private final byte bLength;
25+
private final byte bDescriptorType;
26+
private final byte bEndpointAddress;
27+
private final byte bmAttributes;
28+
//Ignoring: Transfer Type, Synch Type, Usage Type
29+
private final short wMaxPacketSize;
30+
private final byte bInterval;
31+
32+
NsUsbEndpointDescriptor(EndpointDescriptor endpointDescriptor){
33+
this.bLength = endpointDescriptor.bLength();
34+
this.bDescriptorType = endpointDescriptor.bDescriptorType();
35+
this.bEndpointAddress = endpointDescriptor.bEndpointAddress();
36+
this.bmAttributes = endpointDescriptor.bmAttributes();
37+
this.wMaxPacketSize = endpointDescriptor.wMaxPacketSize();
38+
this.bInterval = endpointDescriptor.bInterval();
39+
}
40+
41+
public byte getbLength() {
42+
return bLength;
43+
}
44+
45+
public byte getbDescriptorType() {
46+
return bDescriptorType;
47+
}
48+
49+
public byte getbEndpointAddress() {
50+
return bEndpointAddress;
51+
}
52+
53+
public byte getBmAttributes() {
54+
return bmAttributes;
55+
}
56+
57+
public short getwMaxPacketSize() {
58+
return wMaxPacketSize;
59+
}
60+
61+
public byte getbInterval() {
62+
return bInterval;
63+
}
64+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2019-2020 Dmitry Isaenko
3+
4+
This file is part of NS-USBloader.
5+
6+
NS-USBloader is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
NS-USBloader is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package nsusbloader.COM.USB.common;
20+
21+
import org.usb4java.EndpointDescriptor;
22+
23+
public class NsUsbEndpointDescriptorUtils {
24+
static NsUsbEndpointDescriptor[] convertFromNatives(EndpointDescriptor[] nativeEpDescriptors){
25+
int descriptorsCount = nativeEpDescriptors.length;
26+
NsUsbEndpointDescriptor[] nsUsbEpDescriptors = new NsUsbEndpointDescriptor[descriptorsCount];
27+
for (int i = 0; i < descriptorsCount; i++) {
28+
nsUsbEpDescriptors[i] = new NsUsbEndpointDescriptor(nativeEpDescriptors[i]);
29+
}
30+
return nsUsbEpDescriptors;
31+
}
32+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2019-2020 Dmitry Isaenko
3+
4+
This file is part of NS-USBloader.
5+
6+
NS-USBloader is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
NS-USBloader is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package nsusbloader.COM.USB.common;
20+
21+
import org.usb4java.Interface;
22+
import org.usb4java.InterfaceDescriptor;
23+
24+
import java.util.LinkedList;
25+
26+
/**
27+
* Adapter for easier access to USB devices which has only one interface with one interface descriptor (BULK)
28+
*
29+
* After few JVM failed to core few 'holders' were added: such as NsUsbEndpoint descriptor and NsUsbInterfaceDescriptor
30+
* */
31+
32+
public class NsUsbInterface {
33+
private final Interface iface;
34+
private final LinkedList<NsUsbInterfaceDescriptor> interfaceDescriptors;
35+
36+
public NsUsbInterface(Interface iface){
37+
this.iface = iface;
38+
this.interfaceDescriptors = new LinkedList<>();
39+
collectDescriptors();
40+
}
41+
42+
private void collectDescriptors(){
43+
for (InterfaceDescriptor ifaceDescriptor : iface.altsetting()){
44+
interfaceDescriptors.add(new NsUsbInterfaceDescriptor(ifaceDescriptor));
45+
}
46+
}
47+
public NsUsbInterfaceDescriptor[] getInterfaceDescriptors(){
48+
return interfaceDescriptors.toArray(new NsUsbInterfaceDescriptor[0]);
49+
}
50+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Copyright 2019-2020 Dmitry Isaenko
3+
4+
This file is part of NS-USBloader.
5+
6+
NS-USBloader is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
NS-USBloader is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package nsusbloader.COM.USB.common;
20+
21+
import org.usb4java.EndpointDescriptor;
22+
import org.usb4java.InterfaceDescriptor;
23+
24+
import java.nio.ByteBuffer;
25+
26+
public class NsUsbInterfaceDescriptor {
27+
private final byte bLength;
28+
private final byte bDescriptorType;
29+
private final byte bInterfaceNumber;
30+
private final byte bAlternateSetting;
31+
private final byte bNumEndpoints;
32+
private final byte bInterfaceClass;
33+
private final byte bInterfaceSubClass;
34+
private final byte bInterfaceProtocol;
35+
private final byte iInterface;
36+
//private final int extralen;
37+
//private final ByteBuffer extra;
38+
private final NsUsbEndpointDescriptor[] endpointDescriptors;
39+
40+
NsUsbInterfaceDescriptor(InterfaceDescriptor interfaceDescriptor){
41+
this.bLength = interfaceDescriptor.bLength();
42+
this.bDescriptorType = interfaceDescriptor.bDescriptorType();
43+
this.bInterfaceNumber = interfaceDescriptor.bInterfaceNumber();
44+
this.bAlternateSetting = interfaceDescriptor.bAlternateSetting();
45+
this.bNumEndpoints = interfaceDescriptor.bNumEndpoints();
46+
this.bInterfaceClass = interfaceDescriptor.bInterfaceClass();
47+
this.bInterfaceSubClass = interfaceDescriptor.bInterfaceSubClass();
48+
this.bInterfaceProtocol = interfaceDescriptor.bInterfaceProtocol();
49+
this.iInterface = interfaceDescriptor.iInterface();
50+
51+
this.endpointDescriptors = NsUsbEndpointDescriptorUtils.convertFromNatives(interfaceDescriptor.endpoint());
52+
}
53+
54+
public byte getbLength() {
55+
return bLength;
56+
}
57+
58+
public byte getbDescriptorType() {
59+
return bDescriptorType;
60+
}
61+
62+
public byte getbInterfaceNumber() {
63+
return bInterfaceNumber;
64+
}
65+
66+
public byte getbAlternateSetting() {
67+
return bAlternateSetting;
68+
}
69+
70+
public byte getbNumEndpoints() {
71+
return bNumEndpoints;
72+
}
73+
74+
public byte getbInterfaceClass() {
75+
return bInterfaceClass;
76+
}
77+
78+
public byte getbInterfaceSubClass() {
79+
return bInterfaceSubClass;
80+
}
81+
82+
public byte getbInterfaceProtocol() {
83+
return bInterfaceProtocol;
84+
}
85+
86+
public byte getiInterface() {
87+
return iInterface;
88+
}
89+
90+
public NsUsbEndpointDescriptor[] getEndpointDescriptors() {
91+
return endpointDescriptors;
92+
}
93+
}

src/main/java/nsusbloader/Utilities/nxdumptool/NxdtTask.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ public void run() {
5050

5151
DeviceHandle handler = usbConnect.getNsHandler();
5252

53-
new NxdtUsbAbi1(handler, logPrinter, saveToLocation, this);
53+
try {
54+
new NxdtUsbAbi1(handler, logPrinter, saveToLocation, this);
55+
}
56+
catch (Exception e){
57+
logPrinter.print(e.getMessage(), EMsgType.FAIL);
58+
}
5459

5560
logPrinter.print(".:: Complete ::.", EMsgType.PASS);
5661

0 commit comments

Comments
 (0)