Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit acc29cc9 authored by Mike Lockwood's avatar Mike Lockwood
Browse files

UsbDevice: Move IO related methods to new UsbDeviceConnection class



UsbDevice is now just an immutable parcelable object like UsbInterface and
UsbEndpoint.
All IO related functionality is now contained in UsbDeviceConnection
and UsbRequest.

Bug: 4067029

Change-Id: Ia84da0b512a697acc940eee0c3566711c62e1a68
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 364903ba
Loading
Loading
Loading
Loading
+100 −122
Original line number Diff line number Diff line
@@ -94880,13 +94880,13 @@
 extends="java.lang.Object"
 abstract="false"
 static="false"
 final="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<implements name="android.os.Parcelable">
</implements>
<method name="bulkTransfer"
<method name="describeContents"
 return="int"
 abstract="false"
 native="false"
@@ -94896,17 +94896,9 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="endpoint" type="android.hardware.usb.UsbEndpoint">
</parameter>
<parameter name="buffer" type="byte[]">
</parameter>
<parameter name="length" type="int">
</parameter>
<parameter name="timeout" type="int">
</parameter>
</method>
<method name="claimInterface"
 return="boolean"
<method name="getDeviceClass"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
@@ -94915,13 +94907,9 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="intf" type="android.hardware.usb.UsbInterface">
</parameter>
<parameter name="force" type="boolean">
</parameter>
</method>
<method name="close"
 return="void"
<method name="getDeviceId"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
@@ -94931,33 +94919,21 @@
 visibility="public"
>
</method>
<method name="controlTransfer"
<method name="getDeviceId"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="requestType" type="int">
</parameter>
<parameter name="request" type="int">
</parameter>
<parameter name="value" type="int">
</parameter>
<parameter name="index" type="int">
</parameter>
<parameter name="buffer" type="byte[]">
</parameter>
<parameter name="length" type="int">
</parameter>
<parameter name="timeout" type="int">
<parameter name="name" type="java.lang.String">
</parameter>
</method>
<method name="describeContents"
 return="int"
<method name="getDeviceName"
 return="java.lang.String"
 abstract="false"
 native="false"
 synchronized="false"
@@ -94967,18 +94943,20 @@
 visibility="public"
>
</method>
<method name="getDeviceClass"
 return="int"
<method name="getDeviceName"
 return="java.lang.String"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="id" type="int">
</parameter>
</method>
<method name="getDeviceId"
<method name="getDeviceProtocol"
 return="int"
 abstract="false"
 native="false"
@@ -94989,21 +94967,19 @@
 visibility="public"
>
</method>
<method name="getDeviceId"
<method name="getDeviceSubclass"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="true"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="name" type="java.lang.String">
</parameter>
</method>
<method name="getDeviceName"
 return="java.lang.String"
<method name="getInterface"
 return="android.hardware.usb.UsbInterface"
 abstract="false"
 native="false"
 synchronized="false"
@@ -95012,21 +94988,21 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="index" type="int">
</parameter>
</method>
<method name="getDeviceName"
 return="java.lang.String"
<method name="getInterfaceCount"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="true"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="id" type="int">
</parameter>
</method>
<method name="getDeviceProtocol"
<method name="getProductId"
 return="int"
 abstract="false"
 native="false"
@@ -95037,7 +95013,7 @@
 visibility="public"
>
</method>
<method name="getDeviceSubclass"
<method name="getVendorId"
 return="int"
 abstract="false"
 native="false"
@@ -95048,8 +95024,8 @@
 visibility="public"
>
</method>
<method name="getFileDescriptor"
 return="int"
<method name="writeToParcel"
 return="void"
 abstract="false"
 native="false"
 synchronized="false"
@@ -95058,21 +95034,31 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="parcel" type="android.os.Parcel">
</parameter>
<parameter name="flags" type="int">
</parameter>
</method>
<method name="getInterface"
 return="android.hardware.usb.UsbInterface"
<field name="CREATOR"
 type="android.os.Parcelable.Creator"
 transient="false"
 volatile="false"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
</class>
<class name="UsbDeviceConnection"
 extends="java.lang.Object"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="index" type="int">
</parameter>
</method>
<method name="getInterfaceCount"
<method name="bulkTransfer"
 return="int"
 abstract="false"
 native="false"
@@ -95082,9 +95068,17 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="endpoint" type="android.hardware.usb.UsbEndpoint">
</parameter>
<parameter name="buffer" type="byte[]">
</parameter>
<parameter name="length" type="int">
</parameter>
<parameter name="timeout" type="int">
</parameter>
</method>
<method name="getProductId"
 return="int"
<method name="claimInterface"
 return="boolean"
 abstract="false"
 native="false"
 synchronized="false"
@@ -95093,9 +95087,13 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="intf" type="android.hardware.usb.UsbInterface">
</parameter>
<parameter name="force" type="boolean">
</parameter>
</method>
<method name="getSerial"
 return="java.lang.String"
<method name="close"
 return="void"
 abstract="false"
 native="false"
 synchronized="false"
@@ -95105,7 +95103,7 @@
 visibility="public"
>
</method>
<method name="getVendorId"
<method name="controlTransfer"
 return="int"
 abstract="false"
 native="false"
@@ -95115,9 +95113,23 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="requestType" type="int">
</parameter>
<parameter name="request" type="int">
</parameter>
<parameter name="value" type="int">
</parameter>
<parameter name="index" type="int">
</parameter>
<parameter name="buffer" type="byte[]">
</parameter>
<parameter name="length" type="int">
</parameter>
<parameter name="timeout" type="int">
</parameter>
</method>
<method name="releaseInterface"
 return="boolean"
<method name="getFileDescriptor"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
@@ -95126,11 +95138,9 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="intf" type="android.hardware.usb.UsbInterface">
</parameter>
</method>
<method name="requestWait"
 return="android.hardware.usb.UsbRequest"
<method name="getSerial"
 return="java.lang.String"
 abstract="false"
 native="false"
 synchronized="false"
@@ -95140,8 +95150,8 @@
 visibility="public"
>
</method>
<method name="writeToParcel"
 return="void"
<method name="releaseInterface"
 return="boolean"
 abstract="false"
 native="false"
 synchronized="false"
@@ -95150,27 +95160,26 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="parcel" type="android.os.Parcel">
</parameter>
<parameter name="flags" type="int">
<parameter name="intf" type="android.hardware.usb.UsbInterface">
</parameter>
</method>
<field name="CREATOR"
 type="android.os.Parcelable.Creator"
 transient="false"
 volatile="false"
 static="true"
 final="true"
<method name="requestWait"
 return="android.hardware.usb.UsbRequest"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</field>
</method>
</class>
<class name="UsbEndpoint"
 extends="java.lang.Object"
 abstract="false"
 static="false"
 final="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
@@ -95209,17 +95218,6 @@
 visibility="public"
>
</method>
<method name="getDevice"
 return="android.hardware.usb.UsbDevice"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getDirection"
 return="int"
 abstract="false"
@@ -95242,17 +95240,6 @@
 visibility="public"
>
</method>
<method name="getInterface"
 return="android.hardware.usb.UsbInterface"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getInterval"
 return="int"
 abstract="false"
@@ -95333,17 +95320,6 @@
 visibility="public"
>
</method>
<method name="getDevice"
 return="android.hardware.usb.UsbDevice"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getEndpoint"
 return="android.hardware.usb.UsbEndpoint"
 abstract="false"
@@ -95534,7 +95510,7 @@
</parameter>
</method>
<method name="openDevice"
 return="boolean"
 return="android.hardware.usb.UsbDeviceConnection"
 abstract="false"
 native="false"
 synchronized="false"
@@ -95834,6 +95810,8 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="connection" type="android.hardware.usb.UsbDeviceConnection">
</parameter>
<parameter name="endpoint" type="android.hardware.usb.UsbEndpoint">
</parameter>
</method>
@@ -113765,7 +113743,7 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="manager" type="android.hardware.usb.UsbManager">
<parameter name="connection" type="android.hardware.usb.UsbDeviceConnection">
</parameter>
</method>
</class>
@@ -267438,7 +267416,7 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="arg0" type="T">
<parameter name="t" type="T">
</parameter>
</method>
</interface>
+9 −141
Original line number Diff line number Diff line
@@ -19,33 +19,24 @@ package android.hardware.usb;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.FileDescriptor;


/**
 * A class representing a USB device.
 */
public final class UsbDevice implements Parcelable {
public class UsbDevice implements Parcelable {

    private static final String TAG = "UsbDevice";

    private String mName;
    private int mVendorId;
    private int mProductId;
    private int mClass;
    private int mSubclass;
    private int mProtocol;
    private Parcelable[] mInterfaces;

    // used by the JNI code
    private int mNativeContext;

    private UsbDevice() {
    }

    private final String mName;
    private final int mVendorId;
    private final int mProductId;
    private final int mClass;
    private final int mSubclass;
    private final int mProtocol;
    private final Parcelable[] mInterfaces;

    /**
     * UsbDevice should only be instantiated by UsbService implementation
@@ -150,114 +141,6 @@ public final class UsbDevice implements Parcelable {
        return (UsbInterface)mInterfaces[index];
    }

    /* package */ boolean open(ParcelFileDescriptor pfd) {
        return native_open(mName, pfd.getFileDescriptor());
    }

    /**
     * Releases all system resources related to the device.
     */
    public void close() {
        native_close();
    }

    /**
     * Returns an integer file descriptor for the device, or
     * -1 if the device is not opened.
     * This is intended for passing to native code to access the device
     */
    public int getFileDescriptor() {
        return native_get_fd();
    }

    /**
     * Claims exclusive access to a {@link android.hardware.usb.UsbInterface}.
     * This must be done before sending or receiving data on any
     * {@link android.hardware.usb.UsbEndpoint}s belonging to the interface
     * @param intf the interface to claim
     * @param force true to disconnect kernel driver if necessary
     * @return true if the interface was successfully claimed
     */
    public boolean claimInterface(UsbInterface intf, boolean force) {
        return native_claim_interface(intf.getId(), force);
    }

    /**
     * Releases exclusive access to a {@link android.hardware.usb.UsbInterface}.
     *
     * @return true if the interface was successfully released
     */
    public boolean releaseInterface(UsbInterface intf) {
        return native_release_interface(intf.getId());
    }

    /**
     * Performs a control transaction on endpoint zero for this device.
     * The direction of the transfer is determined by the request type.
     * If requestType & {@link UsbConstants#USB_ENDPOINT_DIR_MASK} is
     * {@link UsbConstants#USB_DIR_OUT}, then the transfer is a write,
     * and if it is {@link UsbConstants#USB_DIR_IN}, then the transfer
     * is a read.
     *
     * @param requestType request type for this transaction
     * @param request request ID for this transaction
     * @param value value field for this transaction
     * @param index index field for this transaction
     * @param buffer buffer for data portion of transaction,
     * or null if no data needs to be sent or received
     * @param length the length of the data to send or receive
     * @param timeout in milliseconds
     * @return length of data transferred (or zero) for success,
     * or negative value for failure
     */
    public int controlTransfer(int requestType, int request, int value,
            int index, byte[] buffer, int length, int timeout) {
        return native_control_request(requestType, request, value, index, buffer, length, timeout);
    }

    /**
     * Performs a bulk transaction on the given endpoint.
     * The direction of the transfer is determined by the direction of the endpoint
     *
     * @param endpoint the endpoint for this transaction
     * @param buffer buffer for data to send or receive,
     * @param length the length of the data to send or receive
     * @param timeout in milliseconds
     * @return length of data transferred (or zero) for success,
     * or negative value for failure
     */
    public int bulkTransfer(UsbEndpoint endpoint, byte[] buffer, int length, int timeout) {
        return native_bulk_request(endpoint.getAddress(), buffer, length, timeout);
    }

    /**
     * Waits for the result of a {@link android.hardware.usb.UsbRequest#queue} operation
     * Note that this may return requests queued on multiple 
     * {@link android.hardware.usb.UsbEndpoint}s.
     * When multiple endpoints are in use, {@link android.hardware.usb.UsbRequest#getEndpoint} and
     * {@link android.hardware.usb.UsbRequest#getClientData} can be useful in determining
     * how to process the result of this function.
     *
     * @return a completed USB request, or null if an error occurred
     */
    public UsbRequest requestWait() {
        UsbRequest request = native_request_wait();
        if (request != null) {
            request.dequeue();
        }
        return request;
    }

    /**
     * Returns the serial number for the device.
     * This will return null if the device has not been opened.
     *
     * @return the device serial number
     */
    public String getSerial() {
        return native_get_serial();
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof UsbDevice) {
@@ -292,11 +175,7 @@ public final class UsbDevice implements Parcelable {
            int subClass = in.readInt();
            int protocol = in.readInt();
            Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClassLoader());
            UsbDevice result = new UsbDevice(name, vendorId, productId, clasz, subClass, protocol, interfaces);
            for (int i = 0; i < interfaces.length; i++) {
                ((UsbInterface)interfaces[i]).setDevice(result);
            }
            return result;
            return new UsbDevice(name, vendorId, productId, clasz, subClass, protocol, interfaces);
        }

        public UsbDevice[] newArray(int size) {
@@ -326,17 +205,6 @@ public final class UsbDevice implements Parcelable {
        return native_get_device_name(id);
    }

    private native boolean native_open(String deviceName, FileDescriptor pfd);
    private native void native_close();
    private native int native_get_fd();
    private native boolean native_claim_interface(int interfaceID, boolean force);
    private native boolean native_release_interface(int interfaceID);
    private native int native_control_request(int requestType, int request, int value,
            int index, byte[] buffer, int length, int timeout);
    private native int native_bulk_request(int endpoint, byte[] buffer, int length, int timeout);
    private native UsbRequest native_request_wait();
    private native String native_get_serial();

    private static native int native_get_device_id(String name);
    private static native String native_get_device_name(int id);
}
+163 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.usb;

import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.FileDescriptor;


/**
 * A class representing a USB device.
 */
public class UsbDeviceConnection {

    private static final String TAG = "UsbDeviceConnection";

    private final UsbDevice mDevice;

    // used by the JNI code
    private int mNativeContext;

    /**
     * UsbDevice should only be instantiated by UsbService implementation
     * @hide
     */
    public UsbDeviceConnection(UsbDevice device) {
        mDevice = device;
    }

    /* package */ boolean open(String name, ParcelFileDescriptor pfd) {
        return native_open(name, pfd.getFileDescriptor());
    }

    /**
     * Releases all system resources related to the device.
     */
    public void close() {
        native_close();
    }

    /**
     * Returns an integer file descriptor for the device, or
     * -1 if the device is not opened.
     * This is intended for passing to native code to access the device
     */
    public int getFileDescriptor() {
        return native_get_fd();
    }

    /**
     * Claims exclusive access to a {@link android.hardware.usb.UsbInterface}.
     * This must be done before sending or receiving data on any
     * {@link android.hardware.usb.UsbEndpoint}s belonging to the interface
     * @param intf the interface to claim
     * @param force true to disconnect kernel driver if necessary
     * @return true if the interface was successfully claimed
     */
    public boolean claimInterface(UsbInterface intf, boolean force) {
        return native_claim_interface(intf.getId(), force);
    }

    /**
     * Releases exclusive access to a {@link android.hardware.usb.UsbInterface}.
     *
     * @return true if the interface was successfully released
     */
    public boolean releaseInterface(UsbInterface intf) {
        return native_release_interface(intf.getId());
    }

    /**
     * Performs a control transaction on endpoint zero for this device.
     * The direction of the transfer is determined by the request type.
     * If requestType & {@link UsbConstants#USB_ENDPOINT_DIR_MASK} is
     * {@link UsbConstants#USB_DIR_OUT}, then the transfer is a write,
     * and if it is {@link UsbConstants#USB_DIR_IN}, then the transfer
     * is a read.
     *
     * @param requestType request type for this transaction
     * @param request request ID for this transaction
     * @param value value field for this transaction
     * @param index index field for this transaction
     * @param buffer buffer for data portion of transaction,
     * or null if no data needs to be sent or received
     * @param length the length of the data to send or receive
     * @param timeout in milliseconds
     * @return length of data transferred (or zero) for success,
     * or negative value for failure
     */
    public int controlTransfer(int requestType, int request, int value,
            int index, byte[] buffer, int length, int timeout) {
        return native_control_request(requestType, request, value, index, buffer, length, timeout);
    }

    /**
     * Performs a bulk transaction on the given endpoint.
     * The direction of the transfer is determined by the direction of the endpoint
     *
     * @param endpoint the endpoint for this transaction
     * @param buffer buffer for data to send or receive,
     * @param length the length of the data to send or receive
     * @param timeout in milliseconds
     * @return length of data transferred (or zero) for success,
     * or negative value for failure
     */
    public int bulkTransfer(UsbEndpoint endpoint, byte[] buffer, int length, int timeout) {
        return native_bulk_request(endpoint.getAddress(), buffer, length, timeout);
    }

    /**
     * Waits for the result of a {@link android.hardware.usb.UsbRequest#queue} operation
     * Note that this may return requests queued on multiple 
     * {@link android.hardware.usb.UsbEndpoint}s.
     * When multiple endpoints are in use, {@link android.hardware.usb.UsbRequest#getEndpoint} and
     * {@link android.hardware.usb.UsbRequest#getClientData} can be useful in determining
     * how to process the result of this function.
     *
     * @return a completed USB request, or null if an error occurred
     */
    public UsbRequest requestWait() {
        UsbRequest request = native_request_wait();
        if (request != null) {
            request.dequeue();
        }
        return request;
    }

    /**
     * Returns the serial number for the device.
     * This will return null if the device has not been opened.
     *
     * @return the device serial number
     */
    public String getSerial() {
        return native_get_serial();
    }

    private native boolean native_open(String deviceName, FileDescriptor pfd);
    private native void native_close();
    private native int native_get_fd();
    private native boolean native_claim_interface(int interfaceID, boolean force);
    private native boolean native_release_interface(int interfaceID);
    private native int native_control_request(int requestType, int request, int value,
            int index, byte[] buffer, int length, int timeout);
    private native int native_bulk_request(int endpoint, byte[] buffer, int length, int timeout);
    private native UsbRequest native_request_wait();
    private native String native_get_serial();
}
+5 −32
Original line number Diff line number Diff line
@@ -23,16 +23,12 @@ import android.os.Parcelable;
/**
 * A class representing an endpoint on a {@link android.hardware.usb.UsbInterface}.
 */
public final class UsbEndpoint implements Parcelable {
public class UsbEndpoint implements Parcelable {

    private int mAddress;
    private int mAttributes;
    private int mMaxPacketSize;
    private int mInterval;
    private UsbInterface mInterface;

    private UsbEndpoint() {
    }
    private final int mAddress;
    private final int mAttributes;
    private final int mMaxPacketSize;
    private final int mInterval;

    /**
     * UsbEndpoint should only be instantiated by UsbService implementation
@@ -119,29 +115,6 @@ public final class UsbEndpoint implements Parcelable {
        return mInterval;
    }

    /**
     * Returns the {@link android.hardware.usb.UsbInterface} this endpoint belongs to.
     *
     * @return the endpoint's interface
     */
    public UsbInterface getInterface() {
        return mInterface;
    }

    /**
     * Returns the {@link android.hardware.usb.UsbDevice} this endpoint belongs to.
     *
     * @return the endpoint's device
     */
    public UsbDevice getDevice() {
        return mInterface.getDevice();
    }

    // only used for parcelling
    /* package */ void setInterface(UsbInterface intf) {
        mInterface = intf;
    }

    @Override
    public String toString() {
        return "UsbEndpoint[mAddress=" + mAddress + ",mAttributes=" + mAttributes +
+6 −28

File changed.

Preview size limit exceeded, changes collapsed.

Loading