Android Open Source - USBIPServerForAndroid Xfer Utils






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;
//  ww  w.  jav a  2 s .  co m
import org.cgutman.usbip.errno.Errno;

import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;

public class XferUtils {

  public static int doInterruptTransfer(UsbDeviceConnection devConn, UsbEndpoint endpoint, byte[] buff, int timeout) {
    // Interrupt transfers are implemented as one-shot bulk transfers
    int res = devConn.bulkTransfer(endpoint, buff,
        buff.length, timeout);
    if (res < 0) {
      res = -Errno.getErrno();
      if (res != -110) { 
        // Don't print for ETIMEDOUT
        System.err.println("Interrupt Xfer failed: "+res);
      }
    }
    
    return res;
  }
  
  public static int doBulkTransfer(UsbDeviceConnection devConn, UsbEndpoint endpoint, byte[] buff, int timeout) {
    int bytesTransferred = 0;
    while (bytesTransferred < buff.length) {
      byte[] remainingBuffer = new byte[buff.length - bytesTransferred];
      
      if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
        // Copy input data into the new buffer
        System.arraycopy(buff, bytesTransferred, remainingBuffer, 0, remainingBuffer.length);
      }
      
      int res = devConn.bulkTransfer(endpoint, remainingBuffer,
          remainingBuffer.length, timeout);
      if (res < 0) {
        // Failed transfer terminates the bulk transfer
        res = -Errno.getErrno();
        if (res != -110) { 
          // Don't print for ETIMEDOUT
          System.err.println("Bulk Xfer failed: "+res);
        }
        return res;
      }
      
      if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
        // Copy output data into the original buffer
        System.arraycopy(remainingBuffer, 0, buff, bytesTransferred, res);
      }
      
      bytesTransferred += res;
      
      if (res < endpoint.getMaxPacketSize()) {
        // A packet less than the maximum size for this endpoint
        // indicates the transfer has ended
        break;
      }
    }
    
    return bytesTransferred;
  }

  public static int doControlTransfer(UsbDeviceConnection devConn, int requestType,
      int request, int value, int index, byte[] buff, int length, int interval) {
    
    // Mask out possible sign expansions
    requestType &= 0xFF;
    request &= 0xFF;
    value &= 0xFFFF;
    index &= 0xFFFF;
    length &= 0xFFFF;
    
    System.out.printf("SETUP: %x %x %x %x %x\n",
        requestType, request, value, index, length);
    
    int res = devConn.controlTransfer(requestType, request, value,
        index, buff, length, interval);
    if (res < 0) {
      res = -Errno.getErrno();
      if (res != -110) { 
        // Don't print for ETIMEDOUT
        System.err.println("Control Xfer failed: "+res);
      }
    }
    
    return res;
  }
}




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