Home | History | Annotate | Download | only in aware
      1 /*
      2  * Copyright (C) 2017 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.aware;
     18 
     19 import android.net.NetworkSpecifier;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 
     23 import java.util.Arrays;
     24 import java.util.Objects;
     25 
     26 /**
     27  * Network specifier object used to request a Wi-Fi Aware network. Apps do not create these objects
     28  * directly but obtain them using
     29  * {@link WifiAwareSession#createNetworkSpecifierOpen(int, byte[])} or
     30  * {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)} or their secure (Passphrase)
     31  * versions.
     32  *
     33  * @hide
     34  */
     35 public final class WifiAwareNetworkSpecifier extends NetworkSpecifier implements Parcelable {
     36     /**
     37      * TYPE: in band, specific peer: role, client_id, session_id, peer_id, pmk/passphrase optional
     38      * @hide
     39      */
     40     public static final int NETWORK_SPECIFIER_TYPE_IB = 0;
     41 
     42     /**
     43      * TYPE: in band, any peer: role, client_id, session_id, pmk/passphrase optional
     44      * [only permitted for RESPONDER]
     45      * @hide
     46      */
     47     public static final int NETWORK_SPECIFIER_TYPE_IB_ANY_PEER = 1;
     48 
     49     /**
     50      * TYPE: out-of-band: role, client_id, peer_mac, pmk/passphrase optional
     51      * @hide
     52      */
     53     public static final int NETWORK_SPECIFIER_TYPE_OOB = 2;
     54 
     55     /**
     56      * TYPE: out-of-band, any peer: role, client_id, pmk/passphrase optional
     57      * [only permitted for RESPONDER]
     58      * @hide
     59      */
     60     public static final int NETWORK_SPECIFIER_TYPE_OOB_ANY_PEER = 3;
     61 
     62     /** @hide */
     63     public static final int NETWORK_SPECIFIER_TYPE_MAX_VALID = NETWORK_SPECIFIER_TYPE_OOB_ANY_PEER;
     64 
     65     /**
     66      * One of the NETWORK_SPECIFIER_TYPE_* constants. The type of the network specifier object.
     67      * @hide
     68      */
     69     public final int type;
     70 
     71     /**
     72      * The role of the device: WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR or
     73      * WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER.
     74      * @hide
     75      */
     76     public final int role;
     77 
     78     /**
     79      * The client ID of the device.
     80      * @hide
     81      */
     82     public final int clientId;
     83 
     84     /**
     85      * The session ID in which context to request a data-path. Only relevant for IB requests.
     86      * @hide
     87      */
     88     public final int sessionId;
     89 
     90     /**
     91      * The peer ID of the device which the data-path should be connected to. Only relevant for
     92      * IB requests (i.e. not IB_ANY_PEER or OOB*).
     93      * @hide
     94      */
     95     public final int peerId;
     96 
     97     /**
     98      * The peer MAC address of the device which the data-path should be connected to. Only relevant
     99      * for OB requests (i.e. not OOB_ANY_PEER or IB*).
    100      * @hide
    101      */
    102     public final byte[] peerMac;
    103 
    104     /**
    105      * The PMK of the requested data-path. Can be null. Only one or none of pmk or passphrase should
    106      * be specified.
    107      * @hide
    108      */
    109     public final byte[] pmk;
    110 
    111     /**
    112      * The Passphrase of the requested data-path. Can be null. Only one or none of the pmk or
    113      * passphrase should be specified.
    114      * @hide
    115      */
    116     public final String passphrase;
    117 
    118     /** @hide */
    119     public WifiAwareNetworkSpecifier(int type, int role, int clientId, int sessionId, int peerId,
    120             byte[] peerMac, byte[] pmk, String passphrase) {
    121         this.type = type;
    122         this.role = role;
    123         this.clientId = clientId;
    124         this.sessionId = sessionId;
    125         this.peerId = peerId;
    126         this.peerMac = peerMac;
    127         this.pmk = pmk;
    128         this.passphrase = passphrase;
    129     }
    130 
    131     public static final Creator<WifiAwareNetworkSpecifier> CREATOR =
    132             new Creator<WifiAwareNetworkSpecifier>() {
    133                 @Override
    134                 public WifiAwareNetworkSpecifier createFromParcel(Parcel in) {
    135                     return new WifiAwareNetworkSpecifier(
    136                         in.readInt(), // type
    137                         in.readInt(), // role
    138                         in.readInt(), // clientId
    139                         in.readInt(), // sessionId
    140                         in.readInt(), // peerId
    141                         in.createByteArray(), // peerMac
    142                         in.createByteArray(), // pmk
    143                         in.readString()); // passphrase
    144                 }
    145 
    146                 @Override
    147                 public WifiAwareNetworkSpecifier[] newArray(int size) {
    148                     return new WifiAwareNetworkSpecifier[size];
    149                 }
    150             };
    151 
    152     @Override
    153     public int describeContents() {
    154         return 0;
    155     }
    156 
    157     @Override
    158     public void writeToParcel(Parcel dest, int flags) {
    159         dest.writeInt(type);
    160         dest.writeInt(role);
    161         dest.writeInt(clientId);
    162         dest.writeInt(sessionId);
    163         dest.writeInt(peerId);
    164         dest.writeByteArray(peerMac);
    165         dest.writeByteArray(pmk);
    166         dest.writeString(passphrase);
    167     }
    168 
    169     /** @hide */
    170     @Override
    171     public boolean satisfiedBy(NetworkSpecifier other) {
    172         // MatchAllNetworkSpecifier is taken care in NetworkCapabilities#satisfiedBySpecifier.
    173         return equals(other);
    174     }
    175 
    176     /** @hide */
    177     @Override
    178     public int hashCode() {
    179         int result = 17;
    180 
    181         result = 31 * result + type;
    182         result = 31 * result + role;
    183         result = 31 * result + clientId;
    184         result = 31 * result + sessionId;
    185         result = 31 * result + peerId;
    186         result = 31 * result + Arrays.hashCode(peerMac);
    187         result = 31 * result + Arrays.hashCode(pmk);
    188         result = 31 * result + Objects.hashCode(passphrase);
    189 
    190         return result;
    191     }
    192 
    193     /** @hide */
    194     @Override
    195     public boolean equals(Object obj) {
    196         if (this == obj) {
    197             return true;
    198         }
    199 
    200         if (!(obj instanceof WifiAwareNetworkSpecifier)) {
    201             return false;
    202         }
    203 
    204         WifiAwareNetworkSpecifier lhs = (WifiAwareNetworkSpecifier) obj;
    205 
    206         return type == lhs.type
    207                 && role == lhs.role
    208                 && clientId == lhs.clientId
    209                 && sessionId == lhs.sessionId
    210                 && peerId == lhs.peerId
    211                 && Arrays.equals(peerMac, lhs.peerMac)
    212                 && Arrays.equals(pmk, lhs.pmk)
    213                 && Objects.equals(passphrase, lhs.passphrase);
    214     }
    215 
    216     /** @hide */
    217     @Override
    218     public String toString() {
    219         StringBuilder sb = new StringBuilder("WifiAwareNetworkSpecifier [");
    220         sb.append("type=").append(type)
    221                 .append(", role=").append(role)
    222                 .append(", clientId=").append(clientId)
    223                 .append(", sessionId=").append(sessionId)
    224                 .append(", peerId=").append(peerId)
    225                 // masking potential PII (although low impact information)
    226                 .append(", peerMac=").append((peerMac == null) ? "<null>" : "<non-null>")
    227                 // masking PII
    228                 .append(", pmk=").append((pmk == null) ? "<null>" : "<non-null>")
    229                 // masking PII
    230                 .append(", passphrase=").append((passphrase == null) ? "<null>" : "<non-null>")
    231                 .append("]");
    232         return sb.toString();
    233     }
    234 }
    235