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
19f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmannimport android.annotation.NonNull;
20f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmannimport android.annotation.Nullable;
217531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwoodimport android.os.Parcel;
227531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwoodimport android.os.Parcelable;
23371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann
24f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmannimport com.android.internal.util.Preconditions;
257531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
267531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood/**
277531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * A class representing a configuration on a {@link UsbDevice}.
287531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * A USB configuration can have one or more interfaces, each one providing a different
297531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * piece of functionality, separate from the other interfaces.
307531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * An interface will have one or more {@link UsbEndpoint}s, which are the
317531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * channels by which the host transfers data with the device.
327531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood *
337531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * <div class="special reference">
347531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * <h3>Developer Guides</h3>
357531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * <p>For more information about communicating with USB hardware, read the
367531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p>
377531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood * </div>
387531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood */
397531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwoodpublic class UsbConfiguration implements Parcelable {
407531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
417531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    private final int mId;
42f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmann    private final @Nullable String mName;
437531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    private final int mAttributes;
447531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    private final int mMaxPower;
45f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmann
46f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmann    /** All interfaces for this config, only null during creation */
47f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmann    private @Nullable Parcelable[] mInterfaces;
487531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
497531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
507531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Mask for "self-powered" bit in the configuration's attributes.
517531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
52fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    private static final int ATTR_SELF_POWERED = 1 << 6;
537531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
547531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
557531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Mask for "remote wakeup" bit in the configuration's attributes.
567531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
57fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    private static final int ATTR_REMOTE_WAKEUP = 1 << 5;
587531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
597531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
607531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * UsbConfiguration should only be instantiated by UsbService implementation
617531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @hide
627531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
63f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmann    public UsbConfiguration(int id, @Nullable String name, int attributes, int maxPower) {
647531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        mId = id;
657531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        mName = name;
667531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        mAttributes = attributes;
677531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        mMaxPower = maxPower;
687531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
697531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
707531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
717531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Returns the configuration's ID field.
727531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * This is an integer that uniquely identifies the configuration on the device.
737531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
747531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @return the configuration's ID
757531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
767531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public int getId() {
777531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return mId;
787531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
797531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
807531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
817531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Returns the configuration's name.
827531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
83f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmann     * @return the configuration's name, or {@code null} if the property could not be read
847531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
85f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmann    public @Nullable String getName() {
867531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return mName;
877531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
887531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
897531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
90fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * Returns the self-powered attribute value configuration's attributes field.
91fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * This attribute indicates that the device has a power source other than the USB connection.
927531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
93fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * @return the configuration's self-powered attribute
947531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
95fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    public boolean isSelfPowered() {
96fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood        return (mAttributes & ATTR_SELF_POWERED) != 0;
97fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    }
98fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood
99fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    /**
100fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * Returns the remote-wakeup attribute value configuration's attributes field.
101fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * This attributes that the device may signal the host to wake from suspend.
102fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     *
103fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     * @return the configuration's remote-wakeup attribute
104fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood     */
105fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood    public boolean isRemoteWakeup() {
106fa2b3fc6cd15a3b6bbfef87288b97354edb42307Mike Lockwood        return (mAttributes & ATTR_REMOTE_WAKEUP) != 0;
1077531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1087531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1097531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
110371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann     * Returns the attributes of this configuration
111371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann     *
112371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann     * @return the configuration's attributes
113371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann     *
114371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann     * @hide
115371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann     */
116371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann    public int getAttributes() {
117371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann        return mAttributes;
118371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann    }
119371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann
120371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann    /**
1217531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Returns the configuration's max power consumption, in milliamps.
1227531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
1237531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @return the configuration's max power
1247531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
1257531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public int getMaxPower() {
1267531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return mMaxPower * 2;
1277531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1287531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1297531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
1307531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Returns the number of {@link UsbInterface}s this configuration contains.
1317531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
1327531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @return the number of endpoints
1337531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
1347531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public int getInterfaceCount() {
1357531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return mInterfaces.length;
1367531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1377531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1387531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
1397531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Returns the {@link UsbInterface} at the given index.
1407531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     *
1417531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @return the interface
1427531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
143f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmann    public @NonNull UsbInterface getInterface(int index) {
1447531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return (UsbInterface)mInterfaces[index];
1457531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1467531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1477531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    /**
1487531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * Only used by UsbService implementation
1497531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     * @hide
1507531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood     */
151f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmann    public void setInterfaces(@NonNull Parcelable[] interfaces) {
152f2d83edc2604983b03bd0c7dd92c2c5d1c1378dcPhilip P. Moltmann        mInterfaces = Preconditions.checkArrayElementsNotNull(interfaces, "interfaces");
1537531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1547531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1557531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    @Override
1567531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public String toString() {
1577531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        StringBuilder builder = new StringBuilder("UsbConfiguration[mId=" + mId +
1587531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood                ",mName=" + mName + ",mAttributes=" + mAttributes +
1597531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood                ",mMaxPower=" + mMaxPower + ",mInterfaces=[");
1607531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        for (int i = 0; i < mInterfaces.length; i++) {
1617531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            builder.append("\n");
1627531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            builder.append(mInterfaces[i].toString());
1637531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        }
1647531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        builder.append("]");
1657531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return builder.toString();
1667531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1677531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1687531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public static final Parcelable.Creator<UsbConfiguration> CREATOR =
1697531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        new Parcelable.Creator<UsbConfiguration>() {
1707531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        public UsbConfiguration createFromParcel(Parcel in) {
1717531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            int id = in.readInt();
1727531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            String name = in.readString();
1737531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            int attributes = in.readInt();
1747531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            int maxPower = in.readInt();
1757531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClassLoader());
1767531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            UsbConfiguration configuration = new UsbConfiguration(id, name, attributes, maxPower);
1777531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            configuration.setInterfaces(interfaces);
1787531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            return configuration;
1797531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        }
1807531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1817531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        public UsbConfiguration[] newArray(int size) {
1827531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood            return new UsbConfiguration[size];
1837531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        }
1847531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    };
1857531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1867531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public int describeContents() {
1877531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        return 0;
1887531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    }
1897531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood
1907531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood    public void writeToParcel(Parcel parcel, int flags) {
1917531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        parcel.writeInt(mId);
1927531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        parcel.writeString(mName);
1937531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        parcel.writeInt(mAttributes);
1947531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        parcel.writeInt(mMaxPower);
1957531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood        parcel.writeParcelableArray(mInterfaces, 0);
1967531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood   }
1977531aa22355cf03f51def61ba67f1636bf85f408Mike Lockwood}
198