// Path: javax.usb/src/main/java/javax/usb/UsbHostManager.java
// public final class UsbHostManager {
// 
//     public static final String JAVAX_USB_USBSERVICES_PROPERTY = "javax.usb.services";
// 
//     public static final String JAVAX_USB_PROPERTIES_FILE = "javax.usb.properties";
// 
//     private static UsbServices usbServices;
// 
//     private final static Object lock = new Object();
// 
//     private UsbHostManager() {
//     }
// 
//     public static UsbServices getUsbServices() throws UsbException, SecurityException {
//         synchronized (lock) {
//             if (usbServices == null) {
//                 usbServices = initialize();
//             }
//             return usbServices;
//         }
//     }
// 
//     private static UsbServices initialize() throws UsbException, SecurityException {
//         Properties properties = getProperties();
// 
//         String services = properties.getProperty(JAVAX_USB_USBSERVICES_PROPERTY);
// 
//         if (services == null) {
//             throw new UsbException("Missing required property '" + JAVAX_USB_USBSERVICES_PROPERTY + "' from configuration file.");
//         }
// 
//         // TODO: Use the thread's current context class loader?
// 
//         Class<?> usbServicesClass;
// 
//         try {
//             usbServicesClass = UsbHostManager.class.getClassLoader().loadClass(services);
// 
//             return (UsbServices) usbServicesClass.newInstance();
//         } catch (ClassNotFoundException e) {
//             throw new UsbPlatformException("Unable to load UsbServices class '" + services + "'.", e);
//         } catch (InstantiationException e) {
//             throw new UsbPlatformException("Unable to instantiate class '" + services + "'.", e);
//         } catch (IllegalAccessException e) {
//             throw new UsbPlatformException("Unable to instantiate class '" + services + "'.", e);
//         } catch (ClassCastException e) {
//             throw new UsbPlatformException("Class " + services + " is not an instance of javax.usb.UsbServices.");
//         }
//     }
// 
//     public static Properties getProperties() throws UsbException, SecurityException {
//         InputStream stream = UsbHostManager.class.getClassLoader().
//             getResourceAsStream(JAVAX_USB_PROPERTIES_FILE);
// 
//         if (stream == null) {
//             throw new UsbException("Unable to load configuration file '" + JAVAX_USB_PROPERTIES_FILE + "'.");
//         }
// 
//         Properties properties;
//         try {
//             properties = new Properties();
//             properties.load(stream);
//         } catch (IOException e) {
//             throw new UsbPlatformException("Error while reading configuration file.", e);
//         } finally {
//             try {
//                 stream.close();
//             } catch (IOException e) {
//                 // ignore
//             }
//         }
//         return properties;
//     }
// }
// 
// Path: usbtools/src/main/java/io/trygvis/usb/tools/UsbCliUtil.java
// public static UsbDevice findDevice(UsbHub hub, Short idVendor, Short idProduct, String id) {
//     // TODO: Parse out --id=VVVV.PPPP[.N] and --device=[device path]
// 
//     if (id != null) {
//         int i = id.indexOf(':');
//         if (i == 4) {
//             // this is an id on the form VVVV.PPPP
//             idVendor = (short) parseInt(id.substring(0, 4), 16);
//             idProduct = (short) parseInt(id.substring(5, 9), 16);
// 
//             id = null;
//         }
//     }
// 
//     if (id != null) {
//         // As this library is implemented with libusb which returns
//         // everything in a single, flat list for now just do this simple search.
// 
//         int index;
//         try {
//             index = parseInt(id);
//         } catch (NumberFormatException e) {
//             System.err.println("Invalid 'id' parameter, has to be an integer.");
//             return null;
//         }
// 
//         List<UsbDevice> devices = hub.getAttachedUsbDevices();
// 
//         if (index >= devices.size() || index < 0) {
//             System.err.println("'id' parameter is out of range.");
//             return null;
//         }
// 
//         return devices.get(index);
//     } else if (idProduct != null && idVendor != null) {
//         UsbDevice usbDevice = findUsbDevice(hub, idVendor, idProduct);
// 
//         if (usbDevice == null) {
//             System.err.println("Could not find device with id " + deviceIdToString(idVendor, idProduct) + ".");
//         }
// 
//         return usbDevice;
//     }
//     return null;
// }


import static java.lang.Byte.parseByte;
import static java.lang.Integer.parseInt;
import static javax.usb.UsbHostManager.*;
import static io.trygvis.usb.tools.UsbCliUtil.findDevice;
import javax.usb.*;
package io.trygvis.usb.tools;



/**
 * Stream an endpoint from stdin.
 */
public class UsbWrite {
    public static final short idVendor = 0x0547;
    public static final short idProduct = (short) 0xff01;

    public static void main(String[] args) throws Exception {
        String id = null;
        Byte endpointId = null;

        System.setProperty("javax.usb.libusb.trace", "true");

        for (String arg : args) {
            if(arg.startsWith("--id=")) {
                id = arg.substring(5);
            }
            else if(arg.startsWith("--ep=")) {
                endpointId = parseByte(arg.substring(5));
            }
        }

        if(id == null || endpointId == null) {
            System.err.println("Usage: --ep=<ep> --id=VVVV:PPPP");
            return;
        }

// Path: javax.usb/src/main/java/javax/usb/UsbHostManager.java
// public final class UsbHostManager {
// 
//     public static final String JAVAX_USB_USBSERVICES_PROPERTY = "javax.usb.services";
// 
//     public static final String JAVAX_USB_PROPERTIES_FILE = "javax.usb.properties";
// 
//     private static UsbServices usbServices;
// 
//     private final static Object lock = new Object();
// 
//     private UsbHostManager() {
//     }
// 
//     public static UsbServices getUsbServices() throws UsbException, SecurityException {
//         synchronized (lock) {
//             if (usbServices == null) {
//                 usbServices = initialize();
//             }
//             return usbServices;
//         }
//     }
// 
//     private static UsbServices initialize() throws UsbException, SecurityException {
//         Properties properties = getProperties();
// 
//         String services = properties.getProperty(JAVAX_USB_USBSERVICES_PROPERTY);
// 
//         if (services == null) {
//             throw new UsbException("Missing required property '" + JAVAX_USB_USBSERVICES_PROPERTY + "' from configuration file.");
//         }
// 
//         // TODO: Use the thread's current context class loader?
// 
//         Class<?> usbServicesClass;
// 
//         try {
//             usbServicesClass = UsbHostManager.class.getClassLoader().loadClass(services);
// 
//             return (UsbServices) usbServicesClass.newInstance();
//         } catch (ClassNotFoundException e) {
//             throw new UsbPlatformException("Unable to load UsbServices class '" + services + "'.", e);
//         } catch (InstantiationException e) {
//             throw new UsbPlatformException("Unable to instantiate class '" + services + "'.", e);
//         } catch (IllegalAccessException e) {
//             throw new UsbPlatformException("Unable to instantiate class '" + services + "'.", e);
//         } catch (ClassCastException e) {
//             throw new UsbPlatformException("Class " + services + " is not an instance of javax.usb.UsbServices.");
//         }
//     }
// 
//     public static Properties getProperties() throws UsbException, SecurityException {
//         InputStream stream = UsbHostManager.class.getClassLoader().
//             getResourceAsStream(JAVAX_USB_PROPERTIES_FILE);
// 
//         if (stream == null) {
//             throw new UsbException("Unable to load configuration file '" + JAVAX_USB_PROPERTIES_FILE + "'.");
//         }
// 
//         Properties properties;
//         try {
//             properties = new Properties();
//             properties.load(stream);
//         } catch (IOException e) {
//             throw new UsbPlatformException("Error while reading configuration file.", e);
//         } finally {
//             try {
//                 stream.close();
//             } catch (IOException e) {
//                 // ignore
//             }
//         }
//         return properties;
//     }
// }
// 
// Path: usbtools/src/main/java/io/trygvis/usb/tools/UsbCliUtil.java
// public static UsbDevice findDevice(UsbHub hub, Short idVendor, Short idProduct, String id) {
//     // TODO: Parse out --id=VVVV.PPPP[.N] and --device=[device path]
// 
//     if (id != null) {
//         int i = id.indexOf(':');
//         if (i == 4) {
//             // this is an id on the form VVVV.PPPP
//             idVendor = (short) parseInt(id.substring(0, 4), 16);
//             idProduct = (short) parseInt(id.substring(5, 9), 16);
// 
//             id = null;
//         }
//     }
// 
//     if (id != null) {
//         // As this library is implemented with libusb which returns
//         // everything in a single, flat list for now just do this simple search.
// 
//         int index;
//         try {
//             index = parseInt(id);
//         } catch (NumberFormatException e) {
//             System.err.println("Invalid 'id' parameter, has to be an integer.");
//             return null;
//         }
// 
//         List<UsbDevice> devices = hub.getAttachedUsbDevices();
// 
//         if (index >= devices.size() || index < 0) {
//             System.err.println("'id' parameter is out of range.");
//             return null;
//         }
// 
//         return devices.get(index);
//     } else if (idProduct != null && idVendor != null) {
//         UsbDevice usbDevice = findUsbDevice(hub, idVendor, idProduct);
// 
//         if (usbDevice == null) {
//             System.err.println("Could not find device with id " + deviceIdToString(idVendor, idProduct) + ".");
//         }
// 
//         return usbDevice;
//     }
//     return null;
// }


// Path: usbtools/src/main/java/io/trygvis/usb/tools/UsbWrite.java
import static java.lang.Byte.parseByte;
import static java.lang.Integer.parseInt;
import static javax.usb.UsbHostManager.*;
import static io.trygvis.usb.tools.UsbCliUtil.findDevice;
import javax.usb.*;

package io.trygvis.usb.tools;



/**
 * Stream an endpoint from stdin.
 */
public class UsbWrite {
    public static final short idVendor = 0x0547;
    public static final short idProduct = (short) 0xff01;

    public static void main(String[] args) throws Exception {
        String id = null;
        Byte endpointId = null;

        System.setProperty("javax.usb.libusb.trace", "true");

        for (String arg : args) {
            if(arg.startsWith("--id=")) {
                id = arg.substring(5);
            }
            else if(arg.startsWith("--ep=")) {
                endpointId = parseByte(arg.substring(5));
            }
        }

        if(id == null || endpointId == null) {
            System.err.println("Usage: --ep=<ep> --id=VVVV:PPPP");
            return;
        }

        UsbDevice device = findDevice(getUsbServices().getRootUsbHub(), idVendor, idProduct, id);