• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.hardware.usb;
18 
19 import android.os.Bundle;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import android.util.Log;
23 
24 import java.io.FileDescriptor;
25 
26 /**
27  * This class represents a USB device attached to the android device with the android device
28  * acting as the USB host.
29  * Each device contains one or more {@link UsbInterface}s, each of which contains a number of
30  * {@link UsbEndpoint}s (the channels via which data is transmitted over USB).
31  *
32  * <p> This class contains information (along with {@link UsbInterface} and {@link UsbEndpoint})
33  * that describes the capabilities of the USB device.
34  * To communicate with the device, you open a {@link UsbDeviceConnection} for the device
35  * and use {@link UsbRequest} to send and receive data on an endpoint.
36  * {@link UsbDeviceConnection#controlTransfer} is used for control requests on endpoint zero.
37  *
38  * <div class="special reference">
39  * <h3>Developer Guides</h3>
40  * <p>For more information about communicating with USB hardware, read the
41  * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p>
42  * </div>
43  */
44 public class UsbDevice implements Parcelable {
45 
46     private static final String TAG = "UsbDevice";
47 
48     private final String mName;
49     private final int mVendorId;
50     private final int mProductId;
51     private final int mClass;
52     private final int mSubclass;
53     private final int mProtocol;
54     private final Parcelable[] mInterfaces;
55 
56     /**
57      * UsbDevice should only be instantiated by UsbService implementation
58      * @hide
59      */
UsbDevice(String name, int vendorId, int productId, int Class, int subClass, int protocol, Parcelable[] interfaces)60     public UsbDevice(String name, int vendorId, int productId,
61             int Class, int subClass, int protocol, Parcelable[] interfaces) {
62         mName = name;
63         mVendorId = vendorId;
64         mProductId = productId;
65         mClass = Class;
66         mSubclass = subClass;
67         mProtocol = protocol;
68         mInterfaces = interfaces;
69     }
70 
71     /**
72      * Returns the name of the device.
73      * In the standard implementation, this is the path of the device file
74      * for the device in the usbfs file system.
75      *
76      * @return the device name
77      */
getDeviceName()78     public String getDeviceName() {
79         return mName;
80     }
81 
82     /**
83      * Returns a unique integer ID for the device.
84      * This is a convenience for clients that want to use an integer to represent
85      * the device, rather than the device name.
86      * IDs are not persistent across USB disconnects.
87      *
88      * @return the device ID
89      */
getDeviceId()90     public int getDeviceId() {
91         return getDeviceId(mName);
92     }
93 
94     /**
95      * Returns a vendor ID for the device.
96      *
97      * @return the device vendor ID
98      */
getVendorId()99     public int getVendorId() {
100         return mVendorId;
101     }
102 
103     /**
104      * Returns a product ID for the device.
105      *
106      * @return the device product ID
107      */
getProductId()108     public int getProductId() {
109         return mProductId;
110     }
111 
112     /**
113      * Returns the devices's class field.
114      * Some useful constants for USB device classes can be found in {@link UsbConstants}.
115      *
116      * @return the devices's class
117      */
getDeviceClass()118     public int getDeviceClass() {
119         return mClass;
120     }
121 
122     /**
123      * Returns the device's subclass field.
124      *
125      * @return the device's subclass
126      */
getDeviceSubclass()127     public int getDeviceSubclass() {
128         return mSubclass;
129     }
130 
131     /**
132      * Returns the device's protocol field.
133      *
134      * @return the device's protocol
135      */
getDeviceProtocol()136     public int getDeviceProtocol() {
137         return mProtocol;
138     }
139 
140     /**
141      * Returns the number of {@link UsbInterface}s this device contains.
142      *
143      * @return the number of interfaces
144      */
getInterfaceCount()145     public int getInterfaceCount() {
146         return mInterfaces.length;
147     }
148 
149     /**
150      * Returns the {@link UsbInterface} at the given index.
151      *
152      * @return the interface
153      */
getInterface(int index)154     public UsbInterface getInterface(int index) {
155         return (UsbInterface)mInterfaces[index];
156     }
157 
158     @Override
equals(Object o)159     public boolean equals(Object o) {
160         if (o instanceof UsbDevice) {
161             return ((UsbDevice)o).mName.equals(mName);
162         } else if (o instanceof String) {
163             return ((String)o).equals(mName);
164         } else {
165             return false;
166         }
167     }
168 
169     @Override
hashCode()170     public int hashCode() {
171         return mName.hashCode();
172     }
173 
174     @Override
toString()175     public String toString() {
176         return "UsbDevice[mName=" + mName + ",mVendorId=" + mVendorId +
177                 ",mProductId=" + mProductId + ",mClass=" + mClass +
178                 ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol +
179                 ",mInterfaces=" + mInterfaces + "]";
180     }
181 
182     public static final Parcelable.Creator<UsbDevice> CREATOR =
183         new Parcelable.Creator<UsbDevice>() {
184         public UsbDevice createFromParcel(Parcel in) {
185             String name = in.readString();
186             int vendorId = in.readInt();
187             int productId = in.readInt();
188             int clasz = in.readInt();
189             int subClass = in.readInt();
190             int protocol = in.readInt();
191             Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClassLoader());
192             return new UsbDevice(name, vendorId, productId, clasz, subClass, protocol, interfaces);
193         }
194 
195         public UsbDevice[] newArray(int size) {
196             return new UsbDevice[size];
197         }
198     };
199 
describeContents()200     public int describeContents() {
201         return 0;
202     }
203 
writeToParcel(Parcel parcel, int flags)204     public void writeToParcel(Parcel parcel, int flags) {
205         parcel.writeString(mName);
206         parcel.writeInt(mVendorId);
207         parcel.writeInt(mProductId);
208         parcel.writeInt(mClass);
209         parcel.writeInt(mSubclass);
210         parcel.writeInt(mProtocol);
211         parcel.writeParcelableArray(mInterfaces, 0);
212    }
213 
getDeviceId(String name)214     public static int getDeviceId(String name) {
215         return native_get_device_id(name);
216     }
217 
getDeviceName(int id)218     public static String getDeviceName(int id) {
219         return native_get_device_name(id);
220     }
221 
native_get_device_id(String name)222     private static native int native_get_device_id(String name);
native_get_device_name(int id)223     private static native String native_get_device_name(int id);
224 }
225