Android Open Source - USBIPServerForAndroid Usb Control Helper






From Project

Back to project page USBIPServerForAndroid.

License

The source code is released under:

GNU General Public License

If you think the Android project USBIPServerForAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.cgutman.usbip.usb;
//from  w  w w  .j av  a2  s.  c  om
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;

public class UsbControlHelper {
  
  private static final int GET_DESCRIPTOR_REQUEST_TYPE = 0x80;
  private static final int GET_DESCRIPTOR_REQUEST = 0x06;
  
  private static final int GET_STATUS_REQUEST_TYPE = 0x82;
  private static final int GET_STATUS_REQUEST = 0x00;
  
  private static final int CLEAR_FEATURE_REQUEST_TYPE = 0x02;
  private static final int CLEAR_FEATURE_REQUEST = 0x01;
  
  private static final int FEATURE_VALUE_HALT = 0x00;
  
  private static final int DEVICE_DESCRIPTOR_TYPE = 1;

  public static UsbDeviceDescriptor readDeviceDescriptor(UsbDeviceConnection devConn) {
    byte[] descriptorBuffer = new byte[UsbDeviceDescriptor.DESCRIPTOR_SIZE];
    
    
    int res = XferUtils.doControlTransfer(devConn, GET_DESCRIPTOR_REQUEST_TYPE,
        GET_DESCRIPTOR_REQUEST,
        (DEVICE_DESCRIPTOR_TYPE << 8) | 0x00, // Devices only have 1 descriptor
        0, descriptorBuffer, descriptorBuffer.length, 0);
    if (res != UsbDeviceDescriptor.DESCRIPTOR_SIZE) {
      return null;
    }
    
    return new UsbDeviceDescriptor(descriptorBuffer);
  }
  
  public static boolean isEndpointHalted(UsbDeviceConnection devConn, UsbEndpoint endpoint) {
    byte[] statusBuffer = new byte[2];
    
    int res = XferUtils.doControlTransfer(devConn, GET_STATUS_REQUEST_TYPE,
        GET_STATUS_REQUEST,
        0,
        endpoint != null ? endpoint.getAddress() : 0,
        statusBuffer, statusBuffer.length, 0);
    if (res != statusBuffer.length) {
      return false;
    }
    
    return (statusBuffer[0] & 1) != 0;
  }
  
  public static boolean clearHaltCondition(UsbDeviceConnection devConn, UsbEndpoint endpoint) {
    int res = XferUtils.doControlTransfer(devConn, CLEAR_FEATURE_REQUEST_TYPE,
        CLEAR_FEATURE_REQUEST,
        FEATURE_VALUE_HALT,
        endpoint.getAddress(),
        null, 0, 0);
    if (res < 0) {
      return false;
    }
    
    return true;
  }
}




Java Source Code List

org.cgutman.usbip.config.UsbIpConfig.java
org.cgutman.usbip.errno.Errno.java
org.cgutman.usbip.server.UsbDeviceInfo.java
org.cgutman.usbip.server.UsbIpServer.java
org.cgutman.usbip.server.UsbRequestHandler.java
org.cgutman.usbip.server.protocol.ProtoDefs.java
org.cgutman.usbip.server.protocol.UsbIpDevice.java
org.cgutman.usbip.server.protocol.UsbIpInterface.java
org.cgutman.usbip.server.protocol.cli.CommonPacket.java
org.cgutman.usbip.server.protocol.cli.DevListReply.java
org.cgutman.usbip.server.protocol.cli.DevListRequest.java
org.cgutman.usbip.server.protocol.cli.ImportDeviceReply.java
org.cgutman.usbip.server.protocol.cli.ImportDeviceRequest.java
org.cgutman.usbip.server.protocol.dev.UsbIpDevicePacket.java
org.cgutman.usbip.server.protocol.dev.UsbIpSubmitUrbReply.java
org.cgutman.usbip.server.protocol.dev.UsbIpSubmitUrb.java
org.cgutman.usbip.server.protocol.dev.UsbIpUnlinkUrbReply.java
org.cgutman.usbip.server.protocol.dev.UsbIpUnlinkUrb.java
org.cgutman.usbip.service.UsbIpService.java
org.cgutman.usbip.usb.UsbControlHelper.java
org.cgutman.usbip.usb.UsbDeviceDescriptor.java
org.cgutman.usbip.usb.XferUtils.java
org.cgutman.usbip.utils.StreamUtils.java