// Path: se.bitcraze.crazyflie.lib/src/main/java/se/bitcraze/crazyflie/lib/Utilities.java
// public class Utilities {
// 
//     private Utilities() {
//         throw new IllegalStateException("Utility class");
//     }
// 
//     /**
//      * Returns byte array as comma separated string
//      * (for debugging purposes)
//      *
//      * @param data
//      * @return
//      */
//     public static String getByteString(byte[] data) {
//         StringBuilder sb = new StringBuilder();
//         for (byte b : data) {
//             sb.append(b);
//             sb.append(",");
//         }
//         return sb.toString();
//     }
// 
//     public static String getHexString(byte... array) {
//         StringBuilder sb = new StringBuilder();
//         for (byte b : array) {
//             sb.append(String.format("%02X", b));
//             sb.append(" ");
//         }
//         return sb.toString();
//     }
// 
//     /**
//      * Strip bytes of the beginning of an array
//      *
//      * @param array
//      * @param offset
//      * @return
//      */
//     public static byte[] strip(byte[] array, int offset) {
//         byte[] strippedArray = new byte[array.length-offset];
//         System.arraycopy(array, offset, strippedArray, 0, strippedArray.length);
//         return strippedArray;
//     }
// 
// }


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.usb.UsbConfiguration;
import javax.usb.UsbConst;
import javax.usb.UsbControlIrp;
import javax.usb.UsbDevice;
import javax.usb.UsbDeviceDescriptor;
import javax.usb.UsbDisconnectedException;
import javax.usb.UsbEndpoint;
import javax.usb.UsbException;
import javax.usb.UsbHostManager;
import javax.usb.UsbHub;
import javax.usb.UsbInterface;
import javax.usb.UsbIrp;
import javax.usb.UsbPipe;
import javax.usb.UsbServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.bitcraze.crazyflie.lib.Utilities;
        return mIntf != null && mIntf.isClaimed();
    }

    /* (non-Javadoc)
     * @see CrazyUsbInterface#sendControlTransfer(int, int, int, int, byte[])
     */
    @Override
    public int sendControlTransfer(int requestType, int request, int value, int index, byte[] data) {
        int returnCode = -1;
        if(mUsbDevice != null){
            try {
                UsbControlIrp usbControlIrp = mUsbDevice.createUsbControlIrp((byte) requestType, (byte) request, (short) value, (short) index);
                if(data == null){
                    data = new byte[0];
                }
                usbControlIrp.setData(data);
                usbControlIrp.setLength(data.length);
                debugControlTransfer((byte) requestType, (byte) request, (byte) value, (byte) index, data);
                if(sendUsbControlIrp(mUsbDevice, usbControlIrp)){
                    returnCode = usbControlIrp.getActualLength();
                }
            } catch (IllegalArgumentException | UsbDisconnectedException e) {
                e.printStackTrace();
            }
        }
        return returnCode;
    }

    private void debugControlTransfer(byte requestType, byte request, byte value, byte index, byte[] data) {
        if (!FILTER_OUT_SEND_CONTROL_TRANSFER) {
// Path: se.bitcraze.crazyflie.lib/src/main/java/se/bitcraze/crazyflie/lib/Utilities.java
// public class Utilities {
// 
//     private Utilities() {
//         throw new IllegalStateException("Utility class");
//     }
// 
//     /**
//      * Returns byte array as comma separated string
//      * (for debugging purposes)
//      *
//      * @param data
//      * @return
//      */
//     public static String getByteString(byte[] data) {
//         StringBuilder sb = new StringBuilder();
//         for (byte b : data) {
//             sb.append(b);
//             sb.append(",");
//         }
//         return sb.toString();
//     }
// 
//     public static String getHexString(byte... array) {
//         StringBuilder sb = new StringBuilder();
//         for (byte b : array) {
//             sb.append(String.format("%02X", b));
//             sb.append(" ");
//         }
//         return sb.toString();
//     }
// 
//     /**
//      * Strip bytes of the beginning of an array
//      *
//      * @param array
//      * @param offset
//      * @return
//      */
//     public static byte[] strip(byte[] array, int offset) {
//         byte[] strippedArray = new byte[array.length-offset];
//         System.arraycopy(array, offset, strippedArray, 0, strippedArray.length);
//         return strippedArray;
//     }
// 
// }


// Path: se.bitcraze.crazyflie.lib/src/main/java/se/bitcraze/crazyflie/lib/usb/UsbLinkJava.java
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.usb.UsbConfiguration;
import javax.usb.UsbConst;
import javax.usb.UsbControlIrp;
import javax.usb.UsbDevice;
import javax.usb.UsbDeviceDescriptor;
import javax.usb.UsbDisconnectedException;
import javax.usb.UsbEndpoint;
import javax.usb.UsbException;
import javax.usb.UsbHostManager;
import javax.usb.UsbHub;
import javax.usb.UsbInterface;
import javax.usb.UsbIrp;
import javax.usb.UsbPipe;
import javax.usb.UsbServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.bitcraze.crazyflie.lib.Utilities;

        return mIntf != null && mIntf.isClaimed();
    }

    /* (non-Javadoc)
     * @see CrazyUsbInterface#sendControlTransfer(int, int, int, int, byte[])
     */
    @Override
    public int sendControlTransfer(int requestType, int request, int value, int index, byte[] data) {
        int returnCode = -1;
        if(mUsbDevice != null){
            try {
                UsbControlIrp usbControlIrp = mUsbDevice.createUsbControlIrp((byte) requestType, (byte) request, (short) value, (short) index);
                if(data == null){
                    data = new byte[0];
                }
                usbControlIrp.setData(data);
                usbControlIrp.setLength(data.length);
                debugControlTransfer((byte) requestType, (byte) request, (byte) value, (byte) index, data);
                if(sendUsbControlIrp(mUsbDevice, usbControlIrp)){
                    returnCode = usbControlIrp.getActualLength();
                }
            } catch (IllegalArgumentException | UsbDisconnectedException e) {
                e.printStackTrace();
            }
        }
        return returnCode;
    }

    private void debugControlTransfer(byte requestType, byte request, byte value, byte index, byte[] data) {
        if (!FILTER_OUT_SEND_CONTROL_TRANSFER) {
            mLogger.debug("sendControlTransfer, requestType: {}, request: {}, value: {}, index: {}, data: {}", Utilities.getHexString(requestType), Utilities.getHexString(request), Utilities.getHexString(value), Utilities.getHexString(index), Utilities.getHexString(data));