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