UsbDevice.java revision 11dd5ae97b1cd5889bb66862fd12718da62a9c75
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 17package android.hardware.usb; 18 19import android.os.Bundle; 20import android.os.Parcel; 21import android.os.Parcelable; 22import android.util.Log; 23 24import 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 */ 38public class UsbDevice implements Parcelable { 39 40 private static final String TAG = "UsbDevice"; 41 42 private final String mName; 43 private final int mVendorId; 44 private final int mProductId; 45 private final int mClass; 46 private final int mSubclass; 47 private final int mProtocol; 48 private final Parcelable[] mInterfaces; 49 50 /** 51 * UsbDevice should only be instantiated by UsbService implementation 52 * @hide 53 */ 54 public UsbDevice(String name, int vendorId, int productId, 55 int Class, int subClass, int protocol, Parcelable[] interfaces) { 56 mName = name; 57 mVendorId = vendorId; 58 mProductId = productId; 59 mClass = Class; 60 mSubclass = subClass; 61 mProtocol = protocol; 62 mInterfaces = interfaces; 63 } 64 65 /** 66 * Returns the name of the device. 67 * In the standard implementation, this is the path of the device file 68 * for the device in the usbfs file system. 69 * 70 * @return the device name 71 */ 72 public String getDeviceName() { 73 return mName; 74 } 75 76 /** 77 * Returns a unique integer ID for the device. 78 * This is a convenience for clients that want to use an integer to represent 79 * the device, rather than the device name. 80 * IDs are not persistent across USB disconnects. 81 * 82 * @return the device ID 83 */ 84 public int getDeviceId() { 85 return getDeviceId(mName); 86 } 87 88 /** 89 * Returns a vendor ID for the device. 90 * 91 * @return the device vendor ID 92 */ 93 public int getVendorId() { 94 return mVendorId; 95 } 96 97 /** 98 * Returns a product ID for the device. 99 * 100 * @return the device product ID 101 */ 102 public int getProductId() { 103 return mProductId; 104 } 105 106 /** 107 * Returns the devices's class field. 108 * Some useful constants for USB device classes can be found in {@link UsbConstants}. 109 * 110 * @return the devices's class 111 */ 112 public int getDeviceClass() { 113 return mClass; 114 } 115 116 /** 117 * Returns the device's subclass field. 118 * 119 * @return the device's subclass 120 */ 121 public int getDeviceSubclass() { 122 return mSubclass; 123 } 124 125 /** 126 * Returns the device's protocol field. 127 * 128 * @return the device's protocol 129 */ 130 public int getDeviceProtocol() { 131 return mProtocol; 132 } 133 134 /** 135 * Returns the number of {@link UsbInterface}s this device contains. 136 * 137 * @return the number of interfaces 138 */ 139 public int getInterfaceCount() { 140 return mInterfaces.length; 141 } 142 143 /** 144 * Returns the {@link UsbInterface} at the given index. 145 * 146 * @return the interface 147 */ 148 public UsbInterface getInterface(int index) { 149 return (UsbInterface)mInterfaces[index]; 150 } 151 152 @Override 153 public boolean equals(Object o) { 154 if (o instanceof UsbDevice) { 155 return ((UsbDevice)o).mName.equals(mName); 156 } else if (o instanceof String) { 157 return ((String)o).equals(mName); 158 } else { 159 return false; 160 } 161 } 162 163 @Override 164 public int hashCode() { 165 return mName.hashCode(); 166 } 167 168 @Override 169 public String toString() { 170 return "UsbDevice[mName=" + mName + ",mVendorId=" + mVendorId + 171 ",mProductId=" + mProductId + ",mClass=" + mClass + 172 ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol + 173 ",mInterfaces=" + mInterfaces + "]"; 174 } 175 176 public static final Parcelable.Creator<UsbDevice> CREATOR = 177 new Parcelable.Creator<UsbDevice>() { 178 public UsbDevice createFromParcel(Parcel in) { 179 String name = in.readString(); 180 int vendorId = in.readInt(); 181 int productId = in.readInt(); 182 int clasz = in.readInt(); 183 int subClass = in.readInt(); 184 int protocol = in.readInt(); 185 Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClassLoader()); 186 return new UsbDevice(name, vendorId, productId, clasz, subClass, protocol, interfaces); 187 } 188 189 public UsbDevice[] newArray(int size) { 190 return new UsbDevice[size]; 191 } 192 }; 193 194 public int describeContents() { 195 return 0; 196 } 197 198 public void writeToParcel(Parcel parcel, int flags) { 199 parcel.writeString(mName); 200 parcel.writeInt(mVendorId); 201 parcel.writeInt(mProductId); 202 parcel.writeInt(mClass); 203 parcel.writeInt(mSubclass); 204 parcel.writeInt(mProtocol); 205 parcel.writeParcelableArray(mInterfaces, 0); 206 } 207 208 public static int getDeviceId(String name) { 209 return native_get_device_id(name); 210 } 211 212 public static String getDeviceName(int id) { 213 return native_get_device_name(id); 214 } 215 216 private static native int native_get_device_id(String name); 217 private static native String native_get_device_name(int id); 218} 219