Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 17 | package android.hardware.usb; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 18 | |
| 19 | import android.os.Bundle; |
| 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | |
| 23 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 24 | * A class representing an endpoint on a {@link UsbInterface}. |
| 25 | * Endpoints are the channels for sending and receiving data over USB. |
| 26 | * Typically bulk endpoints are used for sending non-trivial amounts of data. |
| 27 | * Interrupt endpoints are used for sending small amounts of data, typically events, |
| 28 | * separately from the main data streams. |
| 29 | * The endpoint zero is a special endpoint for control messages sent from the host |
| 30 | * to device. |
| 31 | * Isochronous endpoints are currently unsupported. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 32 | */ |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 33 | public class UsbEndpoint implements Parcelable { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 34 | |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 35 | private final int mAddress; |
| 36 | private final int mAttributes; |
| 37 | private final int mMaxPacketSize; |
| 38 | private final int mInterval; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * UsbEndpoint should only be instantiated by UsbService implementation |
| 42 | * @hide |
| 43 | */ |
| 44 | public UsbEndpoint(int address, int attributes, int maxPacketSize, int interval) { |
| 45 | mAddress = address; |
| 46 | mAttributes = attributes; |
| 47 | mMaxPacketSize = maxPacketSize; |
| 48 | mInterval = interval; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns the endpoint's address field. |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 53 | * The address is a bitfield containing both the endpoint number |
| 54 | * as well as the data direction of the endpoint. |
| 55 | * the endpoint number and direction can also be accessed via |
| 56 | * {@link #getEndpointNumber} and {@link #getDirection}. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 57 | * |
| 58 | * @return the endpoint's address |
| 59 | */ |
| 60 | public int getAddress() { |
| 61 | return mAddress; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Extracts the endpoint's endpoint number from its address |
| 66 | * |
| 67 | * @return the endpoint's endpoint number |
| 68 | */ |
| 69 | public int getEndpointNumber() { |
| 70 | return mAddress & UsbConstants.USB_ENDPOINT_NUMBER_MASK; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the endpoint's direction. |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 75 | * Returns {@link UsbConstants#USB_DIR_OUT} |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 76 | * if the direction is host to device, and |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 77 | * {@link UsbConstants#USB_DIR_IN} if the |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 78 | * direction is device to host. |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 79 | * @see {@link UsbConstants#USB_DIR_IN} |
| 80 | * @see {@link UsbConstants#USB_DIR_OUT} |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 81 | * |
| 82 | * @return the endpoint's direction |
| 83 | */ |
| 84 | public int getDirection() { |
| 85 | return mAddress & UsbConstants.USB_ENDPOINT_DIR_MASK; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the endpoint's attributes field. |
| 90 | * |
| 91 | * @return the endpoint's attributes |
| 92 | */ |
| 93 | public int getAttributes() { |
| 94 | return mAttributes; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the endpoint's type. |
| 99 | * Possible results are: |
| 100 | * <ul> |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 101 | * <li>{@link UsbConstants#USB_ENDPOINT_XFER_CONTROL} (endpoint zero) |
| 102 | * <li>{@link UsbConstants#USB_ENDPOINT_XFER_ISOC} (isochronous endpoint) |
| 103 | * <li>{@link UsbConstants#USB_ENDPOINT_XFER_BULK} (bulk endpoint) |
| 104 | * <li>{@link UsbConstants#USB_ENDPOINT_XFER_INT} (interrupt endpoint) |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 105 | * </ul> |
| 106 | * |
| 107 | * @return the endpoint's type |
| 108 | */ |
| 109 | public int getType() { |
| 110 | return mAttributes & UsbConstants.USB_ENDPOINT_XFERTYPE_MASK; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the endpoint's maximum packet size. |
| 115 | * |
| 116 | * @return the endpoint's maximum packet size |
| 117 | */ |
| 118 | public int getMaxPacketSize() { |
| 119 | return mMaxPacketSize; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns the endpoint's interval field. |
| 124 | * |
| 125 | * @return the endpoint's interval |
| 126 | */ |
| 127 | public int getInterval() { |
| 128 | return mInterval; |
| 129 | } |
| 130 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 131 | @Override |
| 132 | public String toString() { |
| 133 | return "UsbEndpoint[mAddress=" + mAddress + ",mAttributes=" + mAttributes + |
| 134 | ",mMaxPacketSize=" + mMaxPacketSize + ",mInterval=" + mInterval +"]"; |
| 135 | } |
| 136 | |
| 137 | public static final Parcelable.Creator<UsbEndpoint> CREATOR = |
| 138 | new Parcelable.Creator<UsbEndpoint>() { |
| 139 | public UsbEndpoint createFromParcel(Parcel in) { |
| 140 | int address = in.readInt(); |
| 141 | int attributes = in.readInt(); |
| 142 | int maxPacketSize = in.readInt(); |
| 143 | int interval = in.readInt(); |
| 144 | return new UsbEndpoint(address, attributes, maxPacketSize, interval); |
| 145 | } |
| 146 | |
| 147 | public UsbEndpoint[] newArray(int size) { |
| 148 | return new UsbEndpoint[size]; |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | public int describeContents() { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | public void writeToParcel(Parcel parcel, int flags) { |
| 157 | parcel.writeInt(mAddress); |
| 158 | parcel.writeInt(mAttributes); |
| 159 | parcel.writeInt(mMaxPacketSize); |
| 160 | parcel.writeInt(mInterval); |
| 161 | } |
| 162 | } |