-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab1b5b1
commit ba4afa0
Showing
8 changed files
with
400 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/nsusbloader/COM/USB/common/DeviceInformation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
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.common; | ||
|
||
import nsusbloader.COM.USB.UsbErrorCodes; | ||
import org.usb4java.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DeviceInformation { | ||
private static final byte DEFAULT_IN_EP_ADDRESS = -127; // 0x81 | ||
private static final byte DEFAULT_OUT_EP_ADDRESS = 1; | ||
|
||
private Device device; | ||
private ConfigDescriptor configDescriptor; | ||
private List<NsUsbInterface> interfacesInformation = new ArrayList<>(); | ||
|
||
private DeviceInformation(){} | ||
|
||
public static DeviceInformation build(DeviceHandle handler) throws Exception{ | ||
Device device = LibUsb.getDevice(handler); | ||
return DeviceInformation.build(device); | ||
} | ||
public static DeviceInformation build(Device device) throws Exception{ | ||
DeviceInformation deviceInformation = new DeviceInformation(); | ||
deviceInformation.device = device; | ||
deviceInformation.claimConfigurationDescriptor(); | ||
deviceInformation.collectInterfaces(); | ||
deviceInformation.freeConfigurationDescriptor(); | ||
return deviceInformation; | ||
} | ||
|
||
private void claimConfigurationDescriptor() throws Exception{ | ||
configDescriptor = new ConfigDescriptor(); | ||
int returningValue = LibUsb.getActiveConfigDescriptor(device, configDescriptor); | ||
|
||
if (returningValue != LibUsb.SUCCESS) | ||
throw new Exception("Get Active config descriptor failed: "+ UsbErrorCodes.getErrCode(returningValue)); | ||
} | ||
|
||
private void collectInterfaces(){ | ||
for (Interface intrface : configDescriptor.iface()) | ||
interfacesInformation.add(new NsUsbInterface(intrface)); | ||
} | ||
|
||
private void freeConfigurationDescriptor(){ | ||
LibUsb.freeConfigDescriptor(configDescriptor); | ||
} | ||
|
||
/** Bulk transfer endpoint IN */ | ||
public NsUsbEndpointDescriptor getSimplifiedDefaultEndpointDescriptorIn() throws Exception{ | ||
return getSimplifiedDefaultEndpointDescriptor(true); | ||
} | ||
/** Bulk transfer endpoint OUT */ | ||
public NsUsbEndpointDescriptor getSimplifiedDefaultEndpointDescriptorOut() throws Exception{ | ||
return getSimplifiedDefaultEndpointDescriptor(false); | ||
} | ||
|
||
private NsUsbEndpointDescriptor getSimplifiedDefaultEndpointDescriptor(boolean isDescriptorIN) throws Exception{ | ||
byte endpointAddress; | ||
|
||
if (isDescriptorIN) | ||
endpointAddress = DEFAULT_IN_EP_ADDRESS; | ||
else | ||
endpointAddress = DEFAULT_OUT_EP_ADDRESS; | ||
|
||
NsUsbInterface nsUsbInterface = interfacesInformation.get(0); | ||
|
||
NsUsbInterfaceDescriptor firstInterfaceDescriptor = nsUsbInterface.getInterfaceDescriptors()[0]; | ||
NsUsbEndpointDescriptor[] endpointDescriptors = firstInterfaceDescriptor.getEndpointDescriptors(); | ||
|
||
for (NsUsbEndpointDescriptor epDescriptor : endpointDescriptors){ | ||
if (epDescriptor.getbEndpointAddress() == endpointAddress) | ||
return epDescriptor; | ||
} | ||
throw new Exception("No "+(isDescriptorIN?"IN":"OUT")+" endpoint descriptors found on default interface"); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/nsusbloader/COM/USB/common/NsUsbEndpointDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
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.common; | ||
|
||
import org.usb4java.EndpointDescriptor; | ||
|
||
public class NsUsbEndpointDescriptor { | ||
private final byte bLength; | ||
private final byte bDescriptorType; | ||
private final byte bEndpointAddress; | ||
private final byte bmAttributes; | ||
//Ignoring: Transfer Type, Synch Type, Usage Type | ||
private final short wMaxPacketSize; | ||
private final byte bInterval; | ||
|
||
NsUsbEndpointDescriptor(EndpointDescriptor endpointDescriptor){ | ||
this.bLength = endpointDescriptor.bLength(); | ||
this.bDescriptorType = endpointDescriptor.bDescriptorType(); | ||
this.bEndpointAddress = endpointDescriptor.bEndpointAddress(); | ||
this.bmAttributes = endpointDescriptor.bmAttributes(); | ||
this.wMaxPacketSize = endpointDescriptor.wMaxPacketSize(); | ||
this.bInterval = endpointDescriptor.bInterval(); | ||
} | ||
|
||
public byte getbLength() { | ||
return bLength; | ||
} | ||
|
||
public byte getbDescriptorType() { | ||
return bDescriptorType; | ||
} | ||
|
||
public byte getbEndpointAddress() { | ||
return bEndpointAddress; | ||
} | ||
|
||
public byte getBmAttributes() { | ||
return bmAttributes; | ||
} | ||
|
||
public short getwMaxPacketSize() { | ||
return wMaxPacketSize; | ||
} | ||
|
||
public byte getbInterval() { | ||
return bInterval; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/nsusbloader/COM/USB/common/NsUsbEndpointDescriptorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
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.common; | ||
|
||
import org.usb4java.EndpointDescriptor; | ||
|
||
public class NsUsbEndpointDescriptorUtils { | ||
static NsUsbEndpointDescriptor[] convertFromNatives(EndpointDescriptor[] nativeEpDescriptors){ | ||
int descriptorsCount = nativeEpDescriptors.length; | ||
NsUsbEndpointDescriptor[] nsUsbEpDescriptors = new NsUsbEndpointDescriptor[descriptorsCount]; | ||
for (int i = 0; i < descriptorsCount; i++) { | ||
nsUsbEpDescriptors[i] = new NsUsbEndpointDescriptor(nativeEpDescriptors[i]); | ||
} | ||
return nsUsbEpDescriptors; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/nsusbloader/COM/USB/common/NsUsbInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
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.common; | ||
|
||
import org.usb4java.Interface; | ||
import org.usb4java.InterfaceDescriptor; | ||
|
||
import java.util.LinkedList; | ||
|
||
/** | ||
* Adapter for easier access to USB devices which has only one interface with one interface descriptor (BULK) | ||
* | ||
* After few JVM failed to core few 'holders' were added: such as NsUsbEndpoint descriptor and NsUsbInterfaceDescriptor | ||
* */ | ||
|
||
public class NsUsbInterface { | ||
private final Interface iface; | ||
private final LinkedList<NsUsbInterfaceDescriptor> interfaceDescriptors; | ||
|
||
public NsUsbInterface(Interface iface){ | ||
this.iface = iface; | ||
this.interfaceDescriptors = new LinkedList<>(); | ||
collectDescriptors(); | ||
} | ||
|
||
private void collectDescriptors(){ | ||
for (InterfaceDescriptor ifaceDescriptor : iface.altsetting()){ | ||
interfaceDescriptors.add(new NsUsbInterfaceDescriptor(ifaceDescriptor)); | ||
} | ||
} | ||
public NsUsbInterfaceDescriptor[] getInterfaceDescriptors(){ | ||
return interfaceDescriptors.toArray(new NsUsbInterfaceDescriptor[0]); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/nsusbloader/COM/USB/common/NsUsbInterfaceDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
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.common; | ||
|
||
import org.usb4java.EndpointDescriptor; | ||
import org.usb4java.InterfaceDescriptor; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class NsUsbInterfaceDescriptor { | ||
private final byte bLength; | ||
private final byte bDescriptorType; | ||
private final byte bInterfaceNumber; | ||
private final byte bAlternateSetting; | ||
private final byte bNumEndpoints; | ||
private final byte bInterfaceClass; | ||
private final byte bInterfaceSubClass; | ||
private final byte bInterfaceProtocol; | ||
private final byte iInterface; | ||
//private final int extralen; | ||
//private final ByteBuffer extra; | ||
private final NsUsbEndpointDescriptor[] endpointDescriptors; | ||
|
||
NsUsbInterfaceDescriptor(InterfaceDescriptor interfaceDescriptor){ | ||
this.bLength = interfaceDescriptor.bLength(); | ||
this.bDescriptorType = interfaceDescriptor.bDescriptorType(); | ||
this.bInterfaceNumber = interfaceDescriptor.bInterfaceNumber(); | ||
this.bAlternateSetting = interfaceDescriptor.bAlternateSetting(); | ||
this.bNumEndpoints = interfaceDescriptor.bNumEndpoints(); | ||
this.bInterfaceClass = interfaceDescriptor.bInterfaceClass(); | ||
this.bInterfaceSubClass = interfaceDescriptor.bInterfaceSubClass(); | ||
this.bInterfaceProtocol = interfaceDescriptor.bInterfaceProtocol(); | ||
this.iInterface = interfaceDescriptor.iInterface(); | ||
|
||
this.endpointDescriptors = NsUsbEndpointDescriptorUtils.convertFromNatives(interfaceDescriptor.endpoint()); | ||
} | ||
|
||
public byte getbLength() { | ||
return bLength; | ||
} | ||
|
||
public byte getbDescriptorType() { | ||
return bDescriptorType; | ||
} | ||
|
||
public byte getbInterfaceNumber() { | ||
return bInterfaceNumber; | ||
} | ||
|
||
public byte getbAlternateSetting() { | ||
return bAlternateSetting; | ||
} | ||
|
||
public byte getbNumEndpoints() { | ||
return bNumEndpoints; | ||
} | ||
|
||
public byte getbInterfaceClass() { | ||
return bInterfaceClass; | ||
} | ||
|
||
public byte getbInterfaceSubClass() { | ||
return bInterfaceSubClass; | ||
} | ||
|
||
public byte getbInterfaceProtocol() { | ||
return bInterfaceProtocol; | ||
} | ||
|
||
public byte getiInterface() { | ||
return iInterface; | ||
} | ||
|
||
public NsUsbEndpointDescriptor[] getEndpointDescriptors() { | ||
return endpointDescriptors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.