Home | History | Annotate | Download | only in nan
      1 /*
      2  * Copyright (C) 2016 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.nan;
     18 
     19 import android.os.Parcel;
     20 import android.os.Parcelable;
     21 
     22 /**
     23  * Defines a request object to configure a Wi-Fi NAN network. Built using
     24  * {@link ConfigRequest.Builder}. Configuration is requested using
     25  * {@link WifiNanManager#requestConfig(ConfigRequest)}. Note that the actual
     26  * achieved configuration may be different from the requested configuration -
     27  * since multiple applications may request different configurations.
     28  *
     29  * @hide PROPOSED_NAN_API
     30  */
     31 public class ConfigRequest implements Parcelable {
     32     /**
     33      * Lower range of possible cluster ID.
     34      *
     35      * @hide
     36      */
     37     public static final int CLUSTER_ID_MIN = 0;
     38 
     39     /**
     40      * Upper range of possible cluster ID.
     41      *
     42      * @hide
     43      */
     44     public static final int CLUSTER_ID_MAX = 0xFFFF;
     45 
     46     /**
     47      * Indicates whether 5G band support is requested.
     48      *
     49      * @hide
     50      */
     51     public final boolean mSupport5gBand;
     52 
     53     /**
     54      * Specifies the desired master preference.
     55      *
     56      * @hide
     57      */
     58     public final int mMasterPreference;
     59 
     60     /**
     61      * Specifies the desired lower range of the cluster ID. Must be lower then
     62      * {@link ConfigRequest#mClusterHigh}.
     63      *
     64      * @hide
     65      */
     66     public final int mClusterLow;
     67 
     68     /**
     69      * Specifies the desired higher range of the cluster ID. Must be higher then
     70      * {@link ConfigRequest#mClusterLow}.
     71      *
     72      * @hide
     73      */
     74     public final int mClusterHigh;
     75 
     76     private ConfigRequest(boolean support5gBand, int masterPreference, int clusterLow,
     77             int clusterHigh) {
     78         mSupport5gBand = support5gBand;
     79         mMasterPreference = masterPreference;
     80         mClusterLow = clusterLow;
     81         mClusterHigh = clusterHigh;
     82     }
     83 
     84     @Override
     85     public String toString() {
     86         return "ConfigRequest [mSupport5gBand=" + mSupport5gBand + ", mMasterPreference="
     87                 + mMasterPreference + ", mClusterLow=" + mClusterLow + ", mClusterHigh="
     88                 + mClusterHigh + "]";
     89     }
     90 
     91     @Override
     92     public int describeContents() {
     93         return 0;
     94     }
     95 
     96     @Override
     97     public void writeToParcel(Parcel dest, int flags) {
     98         dest.writeInt(mSupport5gBand ? 1 : 0);
     99         dest.writeInt(mMasterPreference);
    100         dest.writeInt(mClusterLow);
    101         dest.writeInt(mClusterHigh);
    102     }
    103 
    104     public static final Creator<ConfigRequest> CREATOR = new Creator<ConfigRequest>() {
    105         @Override
    106         public ConfigRequest[] newArray(int size) {
    107             return new ConfigRequest[size];
    108         }
    109 
    110         @Override
    111         public ConfigRequest createFromParcel(Parcel in) {
    112             boolean support5gBand = in.readInt() != 0;
    113             int masterPreference = in.readInt();
    114             int clusterLow = in.readInt();
    115             int clusterHigh = in.readInt();
    116             return new ConfigRequest(support5gBand, masterPreference, clusterLow, clusterHigh);
    117         }
    118     };
    119 
    120     @Override
    121     public boolean equals(Object o) {
    122         if (this == o) {
    123             return true;
    124         }
    125 
    126         if (!(o instanceof ConfigRequest)) {
    127             return false;
    128         }
    129 
    130         ConfigRequest lhs = (ConfigRequest) o;
    131 
    132         return mSupport5gBand == lhs.mSupport5gBand && mMasterPreference == lhs.mMasterPreference
    133                 && mClusterLow == lhs.mClusterLow && mClusterHigh == lhs.mClusterHigh;
    134     }
    135 
    136     @Override
    137     public int hashCode() {
    138         int result = 17;
    139 
    140         result = 31 * result + (mSupport5gBand ? 1 : 0);
    141         result = 31 * result + mMasterPreference;
    142         result = 31 * result + mClusterLow;
    143         result = 31 * result + mClusterHigh;
    144 
    145         return result;
    146     }
    147 
    148     /**
    149      * Builder used to build {@link ConfigRequest} objects.
    150      */
    151     public static final class Builder {
    152         private boolean mSupport5gBand;
    153         private int mMasterPreference;
    154         private int mClusterLow;
    155         private int mClusterHigh;
    156 
    157         /**
    158          * Default constructor for the Builder.
    159          */
    160         public Builder() {
    161             mSupport5gBand = false;
    162             mMasterPreference = 0;
    163             mClusterLow = 0;
    164             mClusterHigh = CLUSTER_ID_MAX;
    165         }
    166 
    167         /**
    168          * Specify whether 5G band support is required in this request.
    169          *
    170          * @param support5gBand Support for 5G band is required.
    171          * @return The builder to facilitate chaining
    172          *         {@code builder.setXXX(..).setXXX(..)}.
    173          */
    174         public Builder setSupport5gBand(boolean support5gBand) {
    175             mSupport5gBand = support5gBand;
    176             return this;
    177         }
    178 
    179         /**
    180          * Specify the Master Preference requested. The permitted range is 0 to
    181          * 255 with 1 and 255 excluded (reserved).
    182          *
    183          * @param masterPreference The requested master preference
    184          * @return The builder to facilitate chaining
    185          *         {@code builder.setXXX(..).setXXX(..)}.
    186          */
    187         public Builder setMasterPreference(int masterPreference) {
    188             if (masterPreference < 0) {
    189                 throw new IllegalArgumentException(
    190                         "Master Preference specification must be non-negative");
    191             }
    192             if (masterPreference == 1 || masterPreference == 255 || masterPreference > 255) {
    193                 throw new IllegalArgumentException("Master Preference specification must not "
    194                         + "exceed 255 or use 1 or 255 (reserved values)");
    195             }
    196 
    197             mMasterPreference = masterPreference;
    198             return this;
    199         }
    200 
    201         /**
    202          * The Cluster ID is generated randomly for new NAN networks. Specify
    203          * the lower range of the cluster ID. The upper range is specified using
    204          * the {@link ConfigRequest.Builder#setClusterHigh(int)}. The permitted
    205          * range is 0 to the value specified by
    206          * {@link ConfigRequest.Builder#setClusterHigh(int)}. Equality is
    207          * permitted which restricts the Cluster ID to the specified value.
    208          *
    209          * @param clusterLow The lower range of the generated cluster ID.
    210          * @return The builder to facilitate chaining
    211          *         {@code builder.setClusterLow(..).setClusterHigh(..)}.
    212          */
    213         public Builder setClusterLow(int clusterLow) {
    214             if (clusterLow < CLUSTER_ID_MIN) {
    215                 throw new IllegalArgumentException("Cluster specification must be non-negative");
    216             }
    217             if (clusterLow > CLUSTER_ID_MAX) {
    218                 throw new IllegalArgumentException("Cluster specification must not exceed 0xFFFF");
    219             }
    220 
    221             mClusterLow = clusterLow;
    222             return this;
    223         }
    224 
    225         /**
    226          * The Cluster ID is generated randomly for new NAN networks. Specify
    227          * the lower upper of the cluster ID. The lower range is specified using
    228          * the {@link ConfigRequest.Builder#setClusterLow(int)}. The permitted
    229          * range is the value specified by
    230          * {@link ConfigRequest.Builder#setClusterLow(int)} to 0xFFFF. Equality
    231          * is permitted which restricts the Cluster ID to the specified value.
    232          *
    233          * @param clusterHigh The upper range of the generated cluster ID.
    234          * @return The builder to facilitate chaining
    235          *         {@code builder.setClusterLow(..).setClusterHigh(..)}.
    236          */
    237         public Builder setClusterHigh(int clusterHigh) {
    238             if (clusterHigh < CLUSTER_ID_MIN) {
    239                 throw new IllegalArgumentException("Cluster specification must be non-negative");
    240             }
    241             if (clusterHigh > CLUSTER_ID_MAX) {
    242                 throw new IllegalArgumentException("Cluster specification must not exceed 0xFFFF");
    243             }
    244 
    245             mClusterHigh = clusterHigh;
    246             return this;
    247         }
    248 
    249         /**
    250          * Build {@link ConfigRequest} given the current requests made on the
    251          * builder.
    252          */
    253         public ConfigRequest build() {
    254             if (mClusterLow > mClusterHigh) {
    255                 throw new IllegalArgumentException(
    256                         "Invalid argument combination - must have Cluster Low <= Cluster High");
    257             }
    258 
    259             return new ConfigRequest(mSupport5gBand, mMasterPreference, mClusterLow, mClusterHigh);
    260         }
    261     }
    262 }
    263