Home | History | Annotate | Download | only in p2p
      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 /**
     23  * A class representing Wifi Display information for a device
     24  * @hide
     25  */
     26 public 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             mWfdEnabled = source.mWfdEnabled;
    159             mDeviceInfo = source.mDeviceInfo;
    160             mCtrlPort = source.mCtrlPort;
    161             mMaxThroughput = source.mMaxThroughput;
    162         }
    163     }
    164 
    165     /** Implement the Parcelable interface */
    166     public void writeToParcel(Parcel dest, int flags) {
    167         dest.writeInt(mWfdEnabled ? 1 : 0);
    168         dest.writeInt(mDeviceInfo);
    169         dest.writeInt(mCtrlPort);
    170         dest.writeInt(mMaxThroughput);
    171     }
    172 
    173     public void readFromParcel(Parcel in) {
    174         mWfdEnabled = (in.readInt() == 1);
    175         mDeviceInfo = in.readInt();
    176         mCtrlPort = in.readInt();
    177         mMaxThroughput = in.readInt();
    178     }
    179 
    180     /** Implement the Parcelable interface */
    181     public static final Creator<WifiP2pWfdInfo> CREATOR =
    182         new Creator<WifiP2pWfdInfo>() {
    183             public WifiP2pWfdInfo createFromParcel(Parcel in) {
    184                 WifiP2pWfdInfo device = new WifiP2pWfdInfo();
    185                 device.readFromParcel(in);
    186                 return device;
    187             }
    188 
    189             public WifiP2pWfdInfo[] newArray(int size) {
    190                 return new WifiP2pWfdInfo[size];
    191             }
    192         };
    193 }
    194