17531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood/*
27531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * Copyright (C) 2014 The Android Open Source Project
37531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood *
47531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * Licensed under the Apache License, Version 2.0 (the "License");
57531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * you may not use this file except in compliance with the License.
67531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * You may obtain a copy of the License at
77531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood *
87531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood *      http://www.apache.org/licenses/LICENSE-2.0
97531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood *
107531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * Unless required by applicable law or agreed to in writing, software
117531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * distributed under the License is distributed on an "AS IS" BASIS,
127531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * See the License for the specific language governing permissions and
147531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * limitations under the License.
157531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood */
167531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
177531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwoodpackage android.hardware.usb;
187531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
197531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwoodimport android.os.Parcel;
207531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwoodimport android.os.Parcelable;
217531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
227531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood/**
237531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * A class representing a configuration on a {@link UsbDevice}.
247531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * A USB configuration can have one or more interfaces, each one providing a different
257531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * piece of functionality, separate from the other interfaces.
267531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * An interface will have one or more {@link UsbEndpoint}s, which are the
277531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * channels by which the host transfers data with the device.
287531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood *
297531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * <div class="special reference">
307531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * <h3>Developer Guides</h3>
317531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * <p>For more information about communicating with USB hardware, read the
327531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p>
337531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * </div>
347531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood */
357531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwoodpublic class UsbConfiguration implements Parcelable {
367531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
377531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    private final int mId;
387531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    private final String mName;
397531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    private final int mAttributes;
407531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    private final int mMaxPower;
417531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    private Parcelable[] mInterfaces;
427531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
437531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
447531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Mask for "self-powered" bit in the configuration's attributes.
457531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @see #getAttributes
467531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
47fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    private static final int ATTR_SELF_POWERED = 1 << 6;
487531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
497531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
507531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Mask for "remote wakeup" bit in the configuration's attributes.
517531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @see #getAttributes
527531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
53fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    private static final int ATTR_REMOTE_WAKEUP = 1 << 5;
547531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
557531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
567531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * UsbConfiguration should only be instantiated by UsbService implementation
577531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @hide
587531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
597531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public UsbConfiguration(int id, String name, int attributes, int maxPower) {
607531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        mId = id;
617531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        mName = name;
627531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        mAttributes = attributes;
637531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        mMaxPower = maxPower;
647531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
657531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
667531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
677531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Returns the configuration's ID field.
687531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * This is an integer that uniquely identifies the configuration on the device.
697531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
707531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @return the configuration's ID
717531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
727531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public int getId() {
737531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return mId;
747531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
757531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
767531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
777531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Returns the configuration's name.
787531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
797531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @return the configuration's name
807531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
817531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public String getName() {
827531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return mName;
837531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
847531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
857531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
86fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * Returns the self-powered attribute value configuration's attributes field.
87fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * This attribute indicates that the device has a power source other than the USB connection.
887531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
89fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * @return the configuration's self-powered attribute
907531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
91fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    public boolean isSelfPowered() {
92fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood        return (mAttributes & ATTR_SELF_POWERED) != 0;
93fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    }
94fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood
95fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    /**
96fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * Returns the remote-wakeup attribute value configuration's attributes field.
97fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * This attributes that the device may signal the host to wake from suspend.
98fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     *
99fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * @return the configuration's remote-wakeup attribute
100fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     */
101fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    public boolean isRemoteWakeup() {
102fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood        return (mAttributes & ATTR_REMOTE_WAKEUP) != 0;
1037531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1047531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1057531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
1067531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Returns the configuration's max power consumption, in milliamps.
1077531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
1087531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @return the configuration's max power
1097531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
1107531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public int getMaxPower() {
1117531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return mMaxPower * 2;
1127531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1137531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1147531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
1157531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Returns the number of {@link UsbInterface}s this configuration contains.
1167531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
1177531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @return the number of endpoints
1187531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
1197531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public int getInterfaceCount() {
1207531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return mInterfaces.length;
1217531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1227531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1237531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
1247531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Returns the {@link UsbInterface} at the given index.
1257531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
1267531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @return the interface
1277531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
1287531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public UsbInterface getInterface(int index) {
1297531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return (UsbInterface)mInterfaces[index];
1307531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1317531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1327531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
1337531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Only used by UsbService implementation
1347531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @hide
1357531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
1367531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public void setInterfaces(Parcelable[] interfaces) {
1377531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        mInterfaces = interfaces;
1387531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1397531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1407531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    @Override
1417531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public String toString() {
1427531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        StringBuilder builder = new StringBuilder("UsbConfiguration[mId=" + mId +
1437531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood                ",mName=" + mName + ",mAttributes=" + mAttributes +
1447531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood                ",mMaxPower=" + mMaxPower + ",mInterfaces=[");
1457531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        for (int i = 0; i < mInterfaces.length; i++) {
1467531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            builder.append("\n");
1477531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            builder.append(mInterfaces[i].toString());
1487531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        }
1497531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        builder.append("]");
1507531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return builder.toString();
1517531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1527531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1537531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public static final Parcelable.Creator<UsbConfiguration> CREATOR =
1547531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        new Parcelable.Creator<UsbConfiguration>() {
1557531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        public UsbConfiguration createFromParcel(Parcel in) {
1567531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            int id = in.readInt();
1577531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            String name = in.readString();
1587531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            int attributes = in.readInt();
1597531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            int maxPower = in.readInt();
1607531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClassLoader());
1617531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            UsbConfiguration configuration = new UsbConfiguration(id, name, attributes, maxPower);
1627531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            configuration.setInterfaces(interfaces);
1637531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            return configuration;
1647531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        }
1657531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1667531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        public UsbConfiguration[] newArray(int size) {
1677531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            return new UsbConfiguration[size];
1687531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        }
1697531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    };
1707531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1717531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public int describeContents() {
1727531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return 0;
1737531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1747531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1757531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public void writeToParcel(Parcel parcel, int flags) {
1767531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        parcel.writeInt(mId);
1777531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        parcel.writeString(mName);
1787531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        parcel.writeInt(mAttributes);
1797531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        parcel.writeInt(mMaxPower);
1807531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        parcel.writeParcelableArray(mInterfaces, 0);
1817531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood   }
1827531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood}
183