Skip to content

Commit

Permalink
NXDT-related updates
Browse files Browse the repository at this point in the history
  • Loading branch information
developersu committed Aug 27, 2020
1 parent ab1b5b1 commit ba4afa0
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/main/java/nsusbloader/COM/USB/UsbConnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package nsusbloader.COM.USB;

import nsusbloader.COM.USB.common.DeviceInformation;
import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.NSLDataTypes.EMsgType;
import org.usb4java.*;
Expand Down Expand Up @@ -88,6 +89,7 @@ public static UsbConnect connectHomebrewMode(ILogPrinter logPrinter){
usbConnect.connected = true;
}
catch (Exception e){
e.printStackTrace();
logPrinter.print(e.getMessage(), EMsgType.FAIL);
usbConnect.close();
}
Expand Down Expand Up @@ -193,12 +195,10 @@ private void setConfiguration(int configuration) throws Exception{
throw new Exception("Unable to set active configuration on device: "+UsbErrorCodes.getErrCode(returningValue));
}
private void claimInterface() throws Exception{
// Claim interface
returningValue = LibUsb.claimInterface(handlerNS, DEFAULT_INTERFACE);
if (returningValue != LibUsb.SUCCESS)
throw new Exception("Claim interface failure: "+UsbErrorCodes.getErrCode(returningValue));
}

/**
* Get USB status
* @return status of connection
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/nsusbloader/COM/USB/common/DeviceInformation.java
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");
}
}
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;
}
}
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 src/main/java/nsusbloader/COM/USB/common/NsUsbInterface.java
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]);
}
}
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;
}
}
7 changes: 6 additions & 1 deletion src/main/java/nsusbloader/Utilities/nxdumptool/NxdtTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ public void run() {

DeviceHandle handler = usbConnect.getNsHandler();

new NxdtUsbAbi1(handler, logPrinter, saveToLocation, this);
try {
new NxdtUsbAbi1(handler, logPrinter, saveToLocation, this);
}
catch (Exception e){
logPrinter.print(e.getMessage(), EMsgType.FAIL);
}

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

Expand Down
Loading

0 comments on commit ba4afa0

Please sign in to comment.