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;
22
23/**
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.
32 */
33public class UsbEndpoint implements Parcelable {
34
35    private final int mAddress;
36    private final int mAttributes;
37    private final int mMaxPacketSize;
38    private final int mInterval;
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.
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}.
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.
75     * Returns {@link UsbConstants#USB_DIR_OUT}
76     * if the direction is host to device, and
77     * {@link UsbConstants#USB_DIR_IN} if the
78     * direction is device to host.
79     * @see {@link UsbConstants#USB_DIR_IN}
80     * @see {@link UsbConstants#USB_DIR_OUT}
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>
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)
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
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}
163