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