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