1/*
2 * Copyright (C) 2012 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.net.wifi.p2p;
18
19import android.os.Parcelable;
20import android.os.Parcel;
21
22/**
23 * A class representing Wifi Display information for a device
24 * @hide
25 */
26public class WifiP2pWfdInfo implements Parcelable {
27
28    private static final String TAG = "WifiP2pWfdInfo";
29
30    private boolean mWfdEnabled;
31
32    private int mDeviceInfo;
33
34    public static final int WFD_SOURCE              = 0;
35    public static final int PRIMARY_SINK            = 1;
36    public static final int SECONDARY_SINK          = 2;
37    public static final int SOURCE_OR_PRIMARY_SINK  = 3;
38
39    /* Device information bitmap */
40    /** One of {@link #WFD_SOURCE}, {@link #PRIMARY_SINK}, {@link #SECONDARY_SINK}
41     * or {@link #SOURCE_OR_PRIMARY_SINK}
42     */
43    private static final int DEVICE_TYPE                            = 0x3;
44    private static final int COUPLED_SINK_SUPPORT_AT_SOURCE         = 0x4;
45    private static final int COUPLED_SINK_SUPPORT_AT_SINK           = 0x8;
46    private static final int SESSION_AVAILABLE                      = 0x30;
47    private static final int SESSION_AVAILABLE_BIT1                 = 0x10;
48    private static final int SESSION_AVAILABLE_BIT2                 = 0x20;
49
50    private int mCtrlPort;
51
52    private int mMaxThroughput;
53
54    public WifiP2pWfdInfo() {
55    }
56
57    public WifiP2pWfdInfo(int devInfo, int ctrlPort, int maxTput) {
58        mWfdEnabled = true;
59        mDeviceInfo = devInfo;
60        mCtrlPort = ctrlPort;
61        mMaxThroughput = maxTput;
62    }
63
64    public boolean isWfdEnabled() {
65        return mWfdEnabled;
66    }
67
68    public void setWfdEnabled(boolean enabled) {
69        mWfdEnabled = enabled;
70    }
71
72    public int getDeviceType() {
73        return (mDeviceInfo & DEVICE_TYPE);
74    }
75
76    public boolean setDeviceType(int deviceType) {
77        if (deviceType >= WFD_SOURCE && deviceType <= SOURCE_OR_PRIMARY_SINK) {
78            mDeviceInfo |= deviceType;
79            return true;
80        }
81        return false;
82    }
83
84    public boolean isCoupledSinkSupportedAtSource() {
85        return (mDeviceInfo & COUPLED_SINK_SUPPORT_AT_SINK) != 0;
86    }
87
88    public void setCoupledSinkSupportAtSource(boolean enabled) {
89        if (enabled ) {
90            mDeviceInfo |= COUPLED_SINK_SUPPORT_AT_SINK;
91        } else {
92            mDeviceInfo &= ~COUPLED_SINK_SUPPORT_AT_SINK;
93        }
94    }
95
96    public boolean isCoupledSinkSupportedAtSink() {
97        return (mDeviceInfo & COUPLED_SINK_SUPPORT_AT_SINK) != 0;
98    }
99
100    public void setCoupledSinkSupportAtSink(boolean enabled) {
101        if (enabled ) {
102            mDeviceInfo |= COUPLED_SINK_SUPPORT_AT_SINK;
103        } else {
104            mDeviceInfo &= ~COUPLED_SINK_SUPPORT_AT_SINK;
105        }
106    }
107
108    public boolean isSessionAvailable() {
109        return (mDeviceInfo & SESSION_AVAILABLE) != 0;
110    }
111
112    public void setSessionAvailable(boolean enabled) {
113        if (enabled) {
114            mDeviceInfo |= SESSION_AVAILABLE_BIT1;
115            mDeviceInfo &= ~SESSION_AVAILABLE_BIT2;
116        } else {
117            mDeviceInfo &= ~SESSION_AVAILABLE;
118        }
119    }
120
121    public int getControlPort() {
122        return mCtrlPort;
123    }
124
125    public void setControlPort(int port) {
126        mCtrlPort = port;
127    }
128
129    public void setMaxThroughput(int maxThroughput) {
130        mMaxThroughput = maxThroughput;
131    }
132
133    public int getMaxThroughput() {
134        return mMaxThroughput;
135    }
136
137    public String getDeviceInfoHex() {
138        return String.format("%04x%04x%04x%04x", 6, mDeviceInfo, mCtrlPort, mMaxThroughput);
139    }
140
141    public String toString() {
142        StringBuffer sbuf = new StringBuffer();
143        sbuf.append("WFD enabled: ").append(mWfdEnabled);
144        sbuf.append("WFD DeviceInfo: ").append(mDeviceInfo);
145        sbuf.append("\n WFD CtrlPort: ").append(mCtrlPort);
146        sbuf.append("\n WFD MaxThroughput: ").append(mMaxThroughput);
147        return sbuf.toString();
148    }
149
150    /** Implement the Parcelable interface */
151    public int describeContents() {
152        return 0;
153    }
154
155    /** copy constructor */
156    public WifiP2pWfdInfo(WifiP2pWfdInfo source) {
157        if (source != null) {
158            mDeviceInfo = source.mDeviceInfo;
159            mCtrlPort = source.mCtrlPort;
160            mMaxThroughput = source.mMaxThroughput;
161        }
162    }
163
164    /** Implement the Parcelable interface */
165    public void writeToParcel(Parcel dest, int flags) {
166        dest.writeInt(mWfdEnabled ? 1 : 0);
167        dest.writeInt(mDeviceInfo);
168        dest.writeInt(mCtrlPort);
169        dest.writeInt(mMaxThroughput);
170    }
171
172    public void readFromParcel(Parcel in) {
173        mWfdEnabled = (in.readInt() == 1);
174        mDeviceInfo = in.readInt();
175        mCtrlPort = in.readInt();
176        mMaxThroughput = in.readInt();
177    }
178
179    /** Implement the Parcelable interface */
180    public static final Creator<WifiP2pWfdInfo> CREATOR =
181        new Creator<WifiP2pWfdInfo>() {
182            public WifiP2pWfdInfo createFromParcel(Parcel in) {
183                WifiP2pWfdInfo device = new WifiP2pWfdInfo();
184                device.readFromParcel(in);
185                return device;
186            }
187
188            public WifiP2pWfdInfo[] newArray(int size) {
189                return new WifiP2pWfdInfo[size];
190            }
191        };
192}
193