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.Parcel;
20import android.os.Parcelable;
21
22/**
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.
31 */
32public class UsbEndpoint implements Parcelable {
33
34    private final int mAddress;
35    private final int mAttributes;
36    private final int mMaxPacketSize;
37    private final int mInterval;
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.
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}.
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.
74     * Returns {@link UsbConstants#USB_DIR_OUT}
75     * if the direction is host to device, and
76     * {@link UsbConstants#USB_DIR_IN} if the
77     * direction is device to host.
78     * @see {@link UsbConstants#USB_DIR_IN}
79     * @see {@link UsbConstants#USB_DIR_OUT}
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>
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)
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
130    @Override
131    public String toString() {
132        return "UsbEndpoint[mAddress=" + mAddress + ",mAttributes=" + mAttributes +
133                ",mMaxPacketSize=" + mMaxPacketSize + ",mInterval=" + mInterval +"]";
134    }
135
136    public static final Parcelable.Creator<UsbEndpoint> CREATOR =
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}
162