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