diff options
author | Mike Lockwood <lockwood@android.com> | 2011-03-01 08:04:54 -0800 |
---|---|---|
committer | Mike Lockwood <lockwood@android.com> | 2011-03-01 08:07:20 -0800 |
commit | c4308f01c965571dc2354107c3574df113e397ee (patch) | |
tree | fa0c8c934147ab99f2ff6aec87edfb7266ba7890 /core/java/android/hardware/usb | |
parent | 43cdf9b4191e18b5a4d646d23b06438c0e10b8dd (diff) | |
download | frameworks_base-c4308f01c965571dc2354107c3574df113e397ee.zip frameworks_base-c4308f01c965571dc2354107c3574df113e397ee.tar.gz frameworks_base-c4308f01c965571dc2354107c3574df113e397ee.tar.bz2 |
Move USB framework support from android.hardware to android.hardware.usb package
Change-Id: I00fd4f0caaa4aebe48f71c576bb211b5f38bf88d
Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'core/java/android/hardware/usb')
-rw-r--r-- | core/java/android/hardware/usb/IUsbManager.aidl | 64 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbAccessory.aidl | 19 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbAccessory.java | 144 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbConstants.java | 66 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbDevice.aidl | 19 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbDevice.java | 337 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbEndpoint.aidl | 19 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbEndpoint.java | 176 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbInterface.aidl | 19 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbInterface.java | 159 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbManager.java | 315 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbRequest.java | 177 |
12 files changed, 1514 insertions, 0 deletions
diff --git a/core/java/android/hardware/usb/IUsbManager.aidl b/core/java/android/hardware/usb/IUsbManager.aidl new file mode 100644 index 0000000..be65bdb --- /dev/null +++ b/core/java/android/hardware/usb/IUsbManager.aidl @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2010 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.hardware.usb.UsbAccessory; +import android.hardware.usb.UsbDevice; +import android.os.Bundle; +import android.os.ParcelFileDescriptor; + +/** @hide */ +interface IUsbManager +{ + /* Returns a list of all currently attached USB devices */ + void getDeviceList(out Bundle devices); + + /* Returns a file descriptor for communicating with the USB device. + * The native fd can be passed to usb_device_new() in libusbhost. + */ + ParcelFileDescriptor openDevice(String deviceName); + + /* Returns the currently attached USB accessory */ + UsbAccessory getCurrentAccessory(); + + /* Returns a file descriptor for communicating with the USB accessory. + * This file descriptor can be used with standard Java file operations. + */ + ParcelFileDescriptor openAccessory(in UsbAccessory accessory); + + /* Sets the default package for a USB device + * (or clears it if the package name is null) + */ + oneway void setDevicePackage(in UsbDevice device, String packageName); + + /* Sets the default package for a USB device + * (or clears it if the package name is null) + */ + void setAccessoryPackage(in UsbAccessory accessory, String packageName); + + /* Grants permission for the given UID to access the device */ + void grantDevicePermission(in UsbDevice device, int uid); + + /* Grants permission for the given UID to access the accessory */ + void grantAccessoryPermission(in UsbAccessory accessory, int uid); + + /* Returns true if the USB manager has default preferences or permissions for the package */ + boolean hasDefaults(String packageName, int uid); + + /* Clears default preferences and permissions for the package */ + oneway void clearDefaults(String packageName, int uid); +} diff --git a/core/java/android/hardware/usb/UsbAccessory.aidl b/core/java/android/hardware/usb/UsbAccessory.aidl new file mode 100644 index 0000000..1c15f1c --- /dev/null +++ b/core/java/android/hardware/usb/UsbAccessory.aidl @@ -0,0 +1,19 @@ +/* + * 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; + +parcelable UsbAccessory; diff --git a/core/java/android/hardware/usb/UsbAccessory.java b/core/java/android/hardware/usb/UsbAccessory.java new file mode 100644 index 0000000..6cd9178 --- /dev/null +++ b/core/java/android/hardware/usb/UsbAccessory.java @@ -0,0 +1,144 @@ +/* + * 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.Bundle; +import android.os.Parcel; +import android.os.Parcelable; +import android.util.Log; + +/** + * A class representing a USB accessory. + */ +public class UsbAccessory implements Parcelable { + + private static final String TAG = "UsbAccessory"; + + private final String mManufacturer; + private final String mModel; + private final String mType; + private final String mVersion; + + /** + * UsbAccessory should only be instantiated by UsbService implementation + * @hide + */ + public UsbAccessory(String manufacturer, String model, String type, String version) { + mManufacturer = manufacturer; + mModel = model; + mType = type; + mVersion = version; + } + + /** + * UsbAccessory should only be instantiated by UsbService implementation + * @hide + */ + public UsbAccessory(String[] strings) { + mManufacturer = strings[0]; + mModel = strings[1]; + mType = strings[2]; + mVersion = strings[3]; + } + + /** + * Returns the manufacturer of the accessory. + * + * @return the accessory manufacturer + */ + public String getManufacturer() { + return mManufacturer; + } + + /** + * Returns the model name of the accessory. + * + * @return the accessory model + */ + public String getModel() { + return mModel; + } + + /** + * Returns the type of the accessory. + * + * @return the accessory type + */ + public String getType() { + return mType; + } + + /** + * Returns the version of the accessory. + * + * @return the accessory version + */ + public String getVersion() { + return mVersion; + } + + private static boolean compare(String s1, String s2) { + if (s1 == null) return (s2 == null); + return s1.equals(s2); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof UsbAccessory) { + UsbAccessory accessory = (UsbAccessory)obj; + return (compare(mManufacturer, accessory.getManufacturer()) && + compare(mModel, accessory.getModel()) && + compare(mType, accessory.getType()) && + compare(mVersion, accessory.getVersion())); + } + return false; + } + + @Override + public String toString() { + return "UsbAccessory[mManufacturer=" + mManufacturer + + ", mModel=" + mModel + + ", mType=" + mType + + ", mVersion=" + mVersion + "]"; + } + + public static final Parcelable.Creator<UsbAccessory> CREATOR = + new Parcelable.Creator<UsbAccessory>() { + public UsbAccessory createFromParcel(Parcel in) { + String manufacturer = in.readString(); + String model = in.readString(); + String type = in.readString(); + String version = in.readString(); + return new UsbAccessory(manufacturer, model, type, version); + } + + public UsbAccessory[] newArray(int size) { + return new UsbAccessory[size]; + } + }; + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel parcel, int flags) { + parcel.writeString(mManufacturer); + parcel.writeString(mModel); + parcel.writeString(mType); + parcel.writeString(mVersion); + } +} diff --git a/core/java/android/hardware/usb/UsbConstants.java b/core/java/android/hardware/usb/UsbConstants.java new file mode 100644 index 0000000..6626c9f --- /dev/null +++ b/core/java/android/hardware/usb/UsbConstants.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2010 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; + +/** + * Contains constants for the USB protocol. + * These constants correspond to definitions in linux/usb/ch9.h in the linux kernel. + */ +public final class UsbConstants { + + public static final int USB_ENDPOINT_DIR_MASK = 0x80; + public static final int USB_DIR_OUT = 0; + public static final int USB_DIR_IN = 0x80; + + public static final int USB_TYPE_MASK = (0x03 << 5); + public static final int USB_TYPE_STANDARD = (0x00 << 5); + public static final int USB_TYPE_CLASS = (0x01 << 5); + public static final int USB_TYPE_VENDOR = (0x02 << 5); + public static final int USB_TYPE_RESERVED = (0x03 << 5); + + public static final int USB_ENDPOINT_NUMBER_MASK = 0x0f; + + // flags for endpoint attributes + public static final int USB_ENDPOINT_XFERTYPE_MASK = 0x03; + public static final int USB_ENDPOINT_XFER_CONTROL = 0; + public static final int USB_ENDPOINT_XFER_ISOC = 1; + public static final int USB_ENDPOINT_XFER_BULK = 2; + public static final int USB_ENDPOINT_XFER_INT = 3; + + // USB classes + public static final int USB_CLASS_PER_INTERFACE = 0; + public static final int USB_CLASS_AUDIO = 1; + public static final int USB_CLASS_COMM = 2; + public static final int USB_CLASS_HID = 3; + public static final int USB_CLASS_PHYSICA = 5; + public static final int USB_CLASS_STILL_IMAGE = 6; + public static final int USB_CLASS_PRINTER = 7; + public static final int USB_CLASS_MASS_STORAGE = 8; + public static final int USB_CLASS_HUB = 9; + public static final int USB_CLASS_CDC_DATA = 0x0a; + public static final int USB_CLASS_CSCID = 0x0b; + public static final int USB_CLASS_CONTENT_SEC = 0x0d; + public static final int USB_CLASS_VIDEO = 0x0e; + public static final int USB_CLASS_WIRELESS_CONTROLLER = 0xe0; + public static final int USB_CLASS_MISC = 0xef; + public static final int USB_CLASS_APP_SPEC = 0xfe; + public static final int USB_CLASS_VENDOR_SPEC = 0xff; + + // USB subclasses + public static final int USB_INTERFACE_SUBCLASS_BOOT = 1; // for HID class + public static final int USB_SUBCLASS_VENDOR_SPEC = 0xff; +}
\ No newline at end of file diff --git a/core/java/android/hardware/usb/UsbDevice.aidl b/core/java/android/hardware/usb/UsbDevice.aidl new file mode 100644 index 0000000..6030ad1 --- /dev/null +++ b/core/java/android/hardware/usb/UsbDevice.aidl @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2010, 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; + +parcelable UsbDevice; diff --git a/core/java/android/hardware/usb/UsbDevice.java b/core/java/android/hardware/usb/UsbDevice.java new file mode 100644 index 0000000..37bd82b --- /dev/null +++ b/core/java/android/hardware/usb/UsbDevice.java @@ -0,0 +1,337 @@ +/* + * Copyright (C) 2010 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.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 { + + 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() { + } + + + /** + * UsbDevice should only be instantiated by UsbService implementation + * @hide + */ + public UsbDevice(String name, int vendorId, int productId, + int Class, int subClass, int protocol, Parcelable[] interfaces) { + mName = name; + mVendorId = vendorId; + mProductId = productId; + mClass = Class; + mSubclass = subClass; + mProtocol = protocol; + mInterfaces = interfaces; + } + + /** + * Returns the name of the device. + * In the standard implementation, this is the path of the device file + * for the device in the usbfs file system. + * + * @return the device name + */ + public String getDeviceName() { + return mName; + } + + /** + * Returns a unique integer ID for the device. + * This is a convenience for clients that want to use an integer to represent + * the device, rather than the device name. + * IDs are not persistent across USB disconnects. + * + * @return the device ID + */ + public int getDeviceId() { + return getDeviceId(mName); + } + + /** + * Returns a vendor ID for the device. + * + * @return the device vendor ID + */ + public int getVendorId() { + return mVendorId; + } + + /** + * Returns a product ID for the device. + * + * @return the device product ID + */ + public int getProductId() { + return mProductId; + } + + /** + * Returns the devices's class field. + * Some useful constants for USB device classes can be found in + * {@link android.hardware.usb.UsbConstants} + * + * @return the devices's class + */ + public int getDeviceClass() { + return mClass; + } + + /** + * Returns the device's subclass field. + * + * @return the device's subclass + */ + public int getDeviceSubclass() { + return mSubclass; + } + + /** + * Returns the device's subclass field. + * + * @return the device's protocol + */ + public int getDeviceProtocol() { + return mProtocol; + } + + /** + * Returns the number of {@link android.hardware.usb.UsbInterface}s this device contains. + * + * @return the number of interfaces + */ + public int getInterfaceCount() { + return mInterfaces.length; + } + + /** + * Returns the {@link android.hardware.usb.UsbInterface} at the given index. + * + * @return the interface + */ + public UsbInterface getInterface(int index) { + 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) { + return ((UsbDevice)o).mName.equals(mName); + } else if (o instanceof String) { + return ((String)o).equals(mName); + } else { + return false; + } + } + + @Override + public String toString() { + return "UsbDevice[mName=" + mName + ",mVendorId=" + mVendorId + + ",mProductId=" + mProductId + ",mClass=" + mClass + + ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol + + ",mInterfaces=" + mInterfaces + "]"; + } + + public static final Parcelable.Creator<UsbDevice> CREATOR = + new Parcelable.Creator<UsbDevice>() { + public UsbDevice createFromParcel(Parcel in) { + String name = in.readString(); + int vendorId = in.readInt(); + int productId = in.readInt(); + int clasz = in.readInt(); + 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; + } + + public UsbDevice[] newArray(int size) { + return new UsbDevice[size]; + } + }; + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel parcel, int flags) { + parcel.writeString(mName); + parcel.writeInt(mVendorId); + parcel.writeInt(mProductId); + parcel.writeInt(mClass); + parcel.writeInt(mSubclass); + parcel.writeInt(mProtocol); + parcel.writeParcelableArray(mInterfaces, 0); + } + + public static int getDeviceId(String name) { + return native_get_device_id(name); + } + + public static String getDeviceName(int id) { + 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); +} diff --git a/core/java/android/hardware/usb/UsbEndpoint.aidl b/core/java/android/hardware/usb/UsbEndpoint.aidl new file mode 100644 index 0000000..a75cd7e --- /dev/null +++ b/core/java/android/hardware/usb/UsbEndpoint.aidl @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2010 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; + +parcelable UsbEndpoint; diff --git a/core/java/android/hardware/usb/UsbEndpoint.java b/core/java/android/hardware/usb/UsbEndpoint.java new file mode 100644 index 0000000..a48d88f --- /dev/null +++ b/core/java/android/hardware/usb/UsbEndpoint.java @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2010 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.Bundle; +import android.os.Parcel; +import android.os.Parcelable; + +/** + * A class representing an endpoint on a {@link android.hardware.usb.UsbInterface}. + */ +public final class UsbEndpoint implements Parcelable { + + private int mAddress; + private int mAttributes; + private int mMaxPacketSize; + private int mInterval; + private UsbInterface mInterface; + + private UsbEndpoint() { + } + + /** + * UsbEndpoint should only be instantiated by UsbService implementation + * @hide + */ + public UsbEndpoint(int address, int attributes, int maxPacketSize, int interval) { + mAddress = address; + mAttributes = attributes; + mMaxPacketSize = maxPacketSize; + mInterval = interval; + } + + /** + * Returns the endpoint's address field. + * + * @return the endpoint's address + */ + public int getAddress() { + return mAddress; + } + + /** + * Extracts the endpoint's endpoint number from its address + * + * @return the endpoint's endpoint number + */ + public int getEndpointNumber() { + return mAddress & UsbConstants.USB_ENDPOINT_NUMBER_MASK; + } + + /** + * Returns the endpoint's direction. + * Returns {@link android.hardware.usb.UsbConstants#USB_DIR_OUT} + * if the direction is host to device, and + * {@link android.hardware.usb.UsbConstants#USB_DIR_IN} if the + * direction is device to host. + * + * @return the endpoint's direction + */ + public int getDirection() { + return mAddress & UsbConstants.USB_ENDPOINT_DIR_MASK; + } + + /** + * Returns the endpoint's attributes field. + * + * @return the endpoint's attributes + */ + public int getAttributes() { + return mAttributes; + } + + /** + * Returns the endpoint's type. + * Possible results are: + * <ul> + * <li>{@link android.hardware.usb.UsbConstants#USB_ENDPOINT_XFER_CONTROL} (endpoint zero) + * <li>{@link android.hardware.usb.UsbConstants#USB_ENDPOINT_XFER_ISOC} (isochronous endpoint) + * <li>{@link android.hardware.usb.UsbConstants#USB_ENDPOINT_XFER_BULK} (bulk endpoint) + * <li>{@link android.hardware.usb.UsbConstants#USB_ENDPOINT_XFER_INT} (interrupt endpoint) + * </ul> + * + * @return the endpoint's type + */ + public int getType() { + return mAttributes & UsbConstants.USB_ENDPOINT_XFERTYPE_MASK; + } + + /** + * Returns the endpoint's maximum packet size. + * + * @return the endpoint's maximum packet size + */ + public int getMaxPacketSize() { + return mMaxPacketSize; + } + + /** + * Returns the endpoint's interval field. + * + * @return the endpoint's interval + */ + public int getInterval() { + 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 + + ",mMaxPacketSize=" + mMaxPacketSize + ",mInterval=" + mInterval +"]"; + } + + public static final Parcelable.Creator<UsbEndpoint> CREATOR = + new Parcelable.Creator<UsbEndpoint>() { + public UsbEndpoint createFromParcel(Parcel in) { + int address = in.readInt(); + int attributes = in.readInt(); + int maxPacketSize = in.readInt(); + int interval = in.readInt(); + return new UsbEndpoint(address, attributes, maxPacketSize, interval); + } + + public UsbEndpoint[] newArray(int size) { + return new UsbEndpoint[size]; + } + }; + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel parcel, int flags) { + parcel.writeInt(mAddress); + parcel.writeInt(mAttributes); + parcel.writeInt(mMaxPacketSize); + parcel.writeInt(mInterval); + } +} diff --git a/core/java/android/hardware/usb/UsbInterface.aidl b/core/java/android/hardware/usb/UsbInterface.aidl new file mode 100644 index 0000000..32b8c64 --- /dev/null +++ b/core/java/android/hardware/usb/UsbInterface.aidl @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2010 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; + +parcelable UsbInterface; diff --git a/core/java/android/hardware/usb/UsbInterface.java b/core/java/android/hardware/usb/UsbInterface.java new file mode 100644 index 0000000..b3b0e81 --- /dev/null +++ b/core/java/android/hardware/usb/UsbInterface.java @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2010 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.Bundle; +import android.os.Parcel; +import android.os.Parcelable; + +/** + * A class representing an interface on a {@link android.hardware.usb.UsbDevice}. + */ +public class UsbInterface implements Parcelable { + + private int mId; + private int mClass; + private int mSubclass; + private int mProtocol; + private UsbDevice mDevice; + private Parcelable[] mEndpoints; + + private UsbInterface() { + } + + /** + * UsbInterface should only be instantiated by UsbService implementation + * @hide + */ + public UsbInterface(int id, int Class, int subClass, int protocol, + Parcelable[] endpoints) { + mId = id; + mClass = Class; + mSubclass = subClass; + mProtocol = protocol; + mEndpoints = endpoints; + } + + /** + * Returns the interface's ID field. + * + * @return the interface's ID + */ + public int getId() { + return mId; + } + + /** + * Returns the interface's class field. + * Some useful constants for USB classes can be found in + * {@link android.hardware.usb.UsbConstants} + * + * @return the interface's class + */ + public int getInterfaceClass() { + return mClass; + } + + /** + * Returns the interface's subclass field. + * + * @return the interface's subclass + */ + public int getInterfaceSubclass() { + return mSubclass; + } + + /** + * Returns the interface's protocol field. + * + * @return the interface's protocol + */ + public int getInterfaceProtocol() { + return mProtocol; + } + + /** + * Returns the number of {@link android.hardware.usb.UsbEndpoint}s this interface contains. + * + * @return the number of endpoints + */ + public int getEndpointCount() { + return mEndpoints.length; + } + + /** + * Returns the {@link android.hardware.usb.UsbEndpoint} at the given index. + * + * @return the endpoint + */ + public UsbEndpoint getEndpoint(int index) { + return (UsbEndpoint)mEndpoints[index]; + } + + /** + * Returns the {@link android.hardware.usb.UsbDevice} this interface belongs to. + * + * @return the interface's device + */ + public UsbDevice getDevice() { + return mDevice; + } + + // only used for parcelling + /* package */ void setDevice(UsbDevice device) { + mDevice = device; + } + + @Override + public String toString() { + return "UsbInterface[mId=" + mId + ",mClass=" + mClass + + ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol + + ",mEndpoints=" + mEndpoints + "]"; + } + + public static final Parcelable.Creator<UsbInterface> CREATOR = + new Parcelable.Creator<UsbInterface>() { + public UsbInterface createFromParcel(Parcel in) { + int id = in.readInt(); + int Class = in.readInt(); + int subClass = in.readInt(); + int protocol = in.readInt(); + Parcelable[] endpoints = in.readParcelableArray(UsbEndpoint.class.getClassLoader()); + UsbInterface result = new UsbInterface(id, Class, subClass, protocol, endpoints); + for (int i = 0; i < endpoints.length; i++) { + ((UsbEndpoint)endpoints[i]).setInterface(result); + } + return result; + } + + public UsbInterface[] newArray(int size) { + return new UsbInterface[size]; + } + }; + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel parcel, int flags) { + parcel.writeInt(mId); + parcel.writeInt(mClass); + parcel.writeInt(mSubclass); + parcel.writeInt(mProtocol); + parcel.writeParcelableArray(mEndpoints, 0); + } +} diff --git a/core/java/android/hardware/usb/UsbManager.java b/core/java/android/hardware/usb/UsbManager.java new file mode 100644 index 0000000..6683179 --- /dev/null +++ b/core/java/android/hardware/usb/UsbManager.java @@ -0,0 +1,315 @@ +/* + * Copyright (C) 2010 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.Bundle; +import android.os.ParcelFileDescriptor; +import android.os.RemoteException; +import android.util.Log; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.HashMap; + +/** + * This class allows you to access the state of USB, both in host and device mode. + * + * <p>You can obtain an instance of this class by calling + * {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}. + * + * {@samplecode + * UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); + * } + */ +public class UsbManager { + private static final String TAG = "UsbManager"; + + /** + * Broadcast Action: A sticky broadcast for USB state change events when in device mode. + * + * This is a sticky broadcast for clients that includes USB connected/disconnected state, + * <ul> + * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected. + * <li> {@link #USB_CONFIGURATION} a Bundle containing name/value pairs where the name + * is the name of a USB function and the value is either {@link #USB_FUNCTION_ENABLED} + * or {@link #USB_FUNCTION_DISABLED}. The possible function names include + * {@link #USB_FUNCTION_MASS_STORAGE}, {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS}, + * {@link #USB_FUNCTION_MTP} and {@link #USB_FUNCTION_ACCESSORY}. + * </ul> + */ + public static final String ACTION_USB_STATE = + "android.hardware.usb.action.USB_STATE"; + + /** + * Broadcast Action: A broadcast for USB device attached event. + * + * This intent is sent when a USB device is attached to the USB bus when in host mode. + * <ul> + * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice} + * for the attached device + * </ul> + */ + public static final String ACTION_USB_DEVICE_ATTACHED = + "android.hardware.usb.action.USB_DEVICE_ATTACHED"; + + /** + * Broadcast Action: A broadcast for USB device detached event. + * + * This intent is sent when a USB device is detached from the USB bus when in host mode. + * <ul> + * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice} + * for the detached device + * </ul> + */ + public static final String ACTION_USB_DEVICE_DETACHED = + "android.hardware.usb.action.USB_DEVICE_DETACHED"; + + /** + * Broadcast Action: A broadcast for USB accessory attached event. + * + * This intent is sent when a USB accessory is attached. + * <ul> + * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory} + * for the attached accessory + * </ul> + */ + public static final String ACTION_USB_ACCESSORY_ATTACHED = + "android.hardware.usb.action.USB_ACCESSORY_ATTACHED"; + + /** + * Broadcast Action: A broadcast for USB accessory detached event. + * + * This intent is sent when a USB accessory is detached. + * <ul> + * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory} + * for the attached accessory that was detached + * </ul> + */ + public static final String ACTION_USB_ACCESSORY_DETACHED = + "android.hardware.usb.action.USB_ACCESSORY_DETACHED"; + + /** + * Boolean extra indicating whether USB is connected or disconnected. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast. + */ + public static final String USB_CONNECTED = "connected"; + + /** + * Integer extra containing currently set USB configuration. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast. + */ + public static final String USB_CONFIGURATION = "configuration"; + + /** + * Name of the USB mass storage USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_MASS_STORAGE = "mass_storage"; + + /** + * Name of the adb USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_ADB = "adb"; + + /** + * Name of the RNDIS ethernet USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_RNDIS = "rndis"; + + /** + * Name of the MTP USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_MTP = "mtp"; + + /** + * Name of the Accessory USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_ACCESSORY = "accessory"; + + /** + * Value indicating that a USB function is enabled. + * Used in {@link #USB_CONFIGURATION} extras bundle for the + * {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_ENABLED = "enabled"; + + /** + * Value indicating that a USB function is disabled. + * Used in {@link #USB_CONFIGURATION} extras bundle for the + * {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_DISABLED = "disabled"; + + /** + * Name of extra for {@link #ACTION_USB_DEVICE_ATTACHED} and + * {@link #ACTION_USB_DEVICE_DETACHED} broadcasts + * containing the UsbDevice object for the device. + */ + + public static final String EXTRA_DEVICE = "device"; + + /** + * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} and + * {@link #ACTION_USB_ACCESSORY_DETACHED} broadcasts + * containing the UsbAccessory object for the accessory. + */ + public static final String EXTRA_ACCESSORY = "accessory"; + + private IUsbManager mService; + + /** + * {@hide} + */ + public UsbManager(IUsbManager service) { + mService = service; + } + + /** + * Returns a HashMap containing all USB devices currently attached. + * USB device name is the key for the returned HashMap. + * The result will be empty if no devices are attached, or if + * USB host mode is inactive or unsupported. + * + * @return HashMap containing all connected USB devices. + */ + public HashMap<String,UsbDevice> getDeviceList() { + Bundle bundle = new Bundle(); + try { + mService.getDeviceList(bundle); + HashMap<String,UsbDevice> result = new HashMap<String,UsbDevice>(); + for (String name : bundle.keySet()) { + result.put(name, (UsbDevice)bundle.get(name)); + } + return result; + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in getDeviceList", e); + return null; + } + } + + /** + * Opens the device so it can be used to send and receive + * data using {@link android.hardware.usb.UsbRequest}. + * + * @param device the device to open + * @return true if we successfully opened the device + */ + public boolean openDevice(UsbDevice device) { + try { + ParcelFileDescriptor pfd = mService.openDevice(device.getDeviceName()); + if (pfd == null) { + return false; + } + boolean result = device.open(pfd); + pfd.close(); + return result; + } catch (Exception e) { + Log.e(TAG, "exception in UsbManager.openDevice", e); + return false; + } + } + + /** + * Returns a list of currently attached USB accessories. + * (in the current implementation there can be at most one) + * + * @return list of USB accessories, or null if none are attached. + */ + public UsbAccessory[] getAccessoryList() { + try { + UsbAccessory accessory = mService.getCurrentAccessory(); + if (accessory == null) { + return null; + } else { + return new UsbAccessory[] { accessory }; + } + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in getAccessoryList" , e); + return null; + } + } + + /** + * Opens a file descriptor for reading and writing data to the USB accessory. + * + * @param accessory the USB accessory to open + * @return file descriptor, or null if the accessor could not be opened. + */ + public ParcelFileDescriptor openAccessory(UsbAccessory accessory) { + try { + return mService.openAccessory(accessory); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in openAccessory" , e); + return null; + } + } + + private static File getFunctionEnableFile(String function) { + return new File("/sys/class/usb_composite/" + function + "/enable"); + } + + /** + * Returns true if the specified USB function is supported by the kernel. + * Note that a USB function maybe supported but disabled. + * + * @param function name of the USB function + * @return true if the USB function is supported. + */ + public static boolean isFunctionSupported(String function) { + return getFunctionEnableFile(function).exists(); + } + + /** + * Returns true if the specified USB function is currently enabled. + * + * @param function name of the USB function + * @return true if the USB function is enabled. + */ + public static boolean isFunctionEnabled(String function) { + try { + FileInputStream stream = new FileInputStream(getFunctionEnableFile(function)); + boolean enabled = (stream.read() == '1'); + stream.close(); + return enabled; + } catch (IOException e) { + return false; + } + } + + /** + * Enables or disables a USB function. + * + * @hide + */ + public static boolean setFunctionEnabled(String function, boolean enable) { + try { + FileOutputStream stream = new FileOutputStream(getFunctionEnableFile(function)); + stream.write(enable ? '1' : '0'); + stream.close(); + return true; + } catch (IOException e) { + return false; + } + } +} diff --git a/core/java/android/hardware/usb/UsbRequest.java b/core/java/android/hardware/usb/UsbRequest.java new file mode 100644 index 0000000..80085c1 --- /dev/null +++ b/core/java/android/hardware/usb/UsbRequest.java @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2010 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.util.Log; + +import java.nio.ByteBuffer; + +/** + * A class representing USB request packet. + * This can be used for both reading and writing data to or from a + * {@link android.hardware.usb.UsbDevice}. + * UsbRequests are sent asynchronously via {@link #queue} and the results + * are read by {@link android.hardware.usb.UsbDevice#requestWait}. + */ +public class UsbRequest { + + private static final String TAG = "UsbRequest"; + + // used by the JNI code + private int mNativeContext; + + private UsbEndpoint mEndpoint; + + // for temporarily saving current buffer across queue and dequeue + private ByteBuffer mBuffer; + private int mLength; + + // for client use + private Object mClientData; + + public UsbRequest() { + } + + /** + * Initializes the request so it can read or write data on the given endpoint. + * Whether the request allows reading or writing depends on the direction of the endpoint. + * + * @param endpoint the endpoint to be used for this request. + * @return true if the request was successfully opened. + */ + public boolean initialize(UsbEndpoint endpoint) { + mEndpoint = endpoint; + return native_init(endpoint.getDevice(), + endpoint.getAddress(), endpoint.getAttributes(), + endpoint.getMaxPacketSize(), endpoint.getInterval()); + } + + /** + * Releases all resources related to this request. + */ + public void close() { + mEndpoint = null; + native_close(); + } + + @Override + protected void finalize() throws Throwable { + try { + if (mEndpoint != null) { + Log.v(TAG, "endpoint still open in finalize(): " + this); + close(); + } + } finally { + super.finalize(); + } + } + + /** + * Returns the endpoint for the request, or null if the request is not opened. + * + * @return the request's endpoint + */ + public UsbEndpoint getEndpoint() { + return mEndpoint; + } + + /** + * Returns the client data for the request. + * This can be used in conjunction with {@link #setClientData} + * to associate another object with this request, which can be useful for + * maintaining state between calls to {@link #queue} and + * {@link android.hardware.usb.UsbDevice#requestWait} + * + * @return the client data for the request + */ + public Object getClientData() { + return mClientData; + } + + /** + * Sets the client data for the request. + * This can be used in conjunction with {@link #getClientData} + * to associate another object with this request, which can be useful for + * maintaining state between calls to {@link #queue} and + * {@link android.hardware.usb.UsbDevice#requestWait} + * + * @param data the client data for the request + */ + public void setClientData(Object data) { + mClientData = data; + } + + /** + * Queues the request to send or receive data on its endpoint. + * For OUT endpoints, the given buffer data will be sent on the endpoint. + * For IN endpoints, the endpoint will attempt to read the given number of bytes + * into the specified buffer. + * If the queueing operation is successful, we return true and the result will be + * returned via {@link android.hardware.usb.UsbDevice#requestWait} + * + * @param buffer the buffer containing the bytes to write, or location to store + * the results of a read + * @param length number of bytes to read or write + * @return true if the queueing operation succeeded + */ + public boolean queue(ByteBuffer buffer, int length) { + boolean out = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT); + boolean result; + if (buffer.isDirect()) { + result = native_queue_direct(buffer, length, out); + } else if (buffer.hasArray()) { + result = native_queue_array(buffer.array(), length, out); + } else { + throw new IllegalArgumentException("buffer is not direct and has no array"); + } + if (result) { + // save our buffer for when the request has completed + mBuffer = buffer; + mLength = length; + } + return result; + } + + /* package */ void dequeue() { + boolean out = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT); + if (mBuffer.isDirect()) { + native_dequeue_direct(); + } else { + native_dequeue_array(mBuffer.array(), mLength, out); + } + mBuffer = null; + mLength = 0; + } + + /** + * Cancels a pending queue operation. + * + * @return true if cancelling succeeded + */ + public boolean cancel() { + return native_cancel(); + } + + private native boolean native_init(UsbDevice device, int ep_address, int ep_attributes, + int ep_max_packet_size, int ep_interval); + private native void native_close(); + private native boolean native_queue_array(byte[] buffer, int length, boolean out); + private native void native_dequeue_array(byte[] buffer, int length, boolean out); + private native boolean native_queue_direct(ByteBuffer buffer, int length, boolean out); + private native void native_dequeue_direct(); + private native boolean native_cancel(); +} |