Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8690e5d9 authored by Etan Cohen's avatar Etan Cohen
Browse files

[AWARE] Add builder pattern for Wi-Fi Aware NetworkSpecifier

The NetworkSpecifier is required when requesting a Wi-Fi Aware
data-path link. Previously the NetworkSpecifier was created using
3 separate methods - such a mechanism is not scalable as we need
to add more parameters (i.e. more methods).

Add Builder pattern for creating the Aware NetworkSpecifier and
deprecate the original methods.

This CL does not add any new parameter - it should be functionally
the same as the original codebase.

Bug: 117605977
Test: atest android.net.wifi.aware
Test: ACTS ThroughputTest:test_iperf_single_ndp_aware_only_ib
Change-Id: Ifd4a1791a5fa0c351912733012e543bd90939c7f
parent 557b77fb
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -29557,8 +29557,8 @@ package android.net.wifi.aware {
  public class DiscoverySession implements java.lang.AutoCloseable {
    method public void close();
    method public android.net.NetworkSpecifier createNetworkSpecifierOpen(android.net.wifi.aware.PeerHandle);
    method public android.net.NetworkSpecifier createNetworkSpecifierPassphrase(android.net.wifi.aware.PeerHandle, java.lang.String);
    method public deprecated android.net.NetworkSpecifier createNetworkSpecifierOpen(android.net.wifi.aware.PeerHandle);
    method public deprecated android.net.NetworkSpecifier createNetworkSpecifierPassphrase(android.net.wifi.aware.PeerHandle, java.lang.String);
    method public void sendMessage(android.net.wifi.aware.PeerHandle, int, byte[]);
  }
@@ -29643,6 +29643,14 @@ package android.net.wifi.aware {
    field public static final int WIFI_AWARE_DATA_PATH_ROLE_RESPONDER = 1; // 0x1
  }
  public static class WifiAwareManager.NetworkSpecifierBuilder {
    ctor public WifiAwareManager.NetworkSpecifierBuilder();
    method public android.net.NetworkSpecifier build();
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setDiscoverySession(android.net.wifi.aware.DiscoverySession);
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPeerHandle(android.net.wifi.aware.PeerHandle);
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPskPassphrase(java.lang.String);
  }
  public final class WifiAwareNetworkInfo implements android.os.Parcelable android.net.TransportInfo {
    method public int describeContents();
    method public java.net.Inet6Address getPeerIpv6Addr();
+5 −1
Original line number Diff line number Diff line
@@ -3938,7 +3938,11 @@ package android.net.wifi {
package android.net.wifi.aware {

  public class DiscoverySession implements java.lang.AutoCloseable {
    method public android.net.NetworkSpecifier createNetworkSpecifierPmk(android.net.wifi.aware.PeerHandle, byte[]);
    method public deprecated android.net.NetworkSpecifier createNetworkSpecifierPmk(android.net.wifi.aware.PeerHandle, byte[]);
  }

  public static class WifiAwareManager.NetworkSpecifierBuilder {
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPmk(byte[]);
  }

  public class WifiAwareSession implements java.lang.AutoCloseable {
+10 −4
Original line number Diff line number Diff line
@@ -35,10 +35,10 @@ import java.lang.ref.WeakReference;
 * class provides functionality common to both publish and subscribe discovery sessions:
 * <ul>
 *      <li>Sending messages: {@link #sendMessage(PeerHandle, int, byte[])} method.
 *     <li>Creating a network-specifier when requesting a Aware connection:
 *     {@link #createNetworkSpecifierOpen(PeerHandle)} or
 *     {@link #createNetworkSpecifierPassphrase(PeerHandle, String)}.
 *      <li>Creating a network-specifier when requesting a Aware connection using
 *      {@link WifiAwareManager.NetworkSpecifierBuilder}.
 * </ul>
 * <p>
 * The {@link #close()} method must be called to destroy discovery sessions once they are
 * no longer needed.
 */
@@ -270,6 +270,7 @@ public class DiscoverySession implements AutoCloseable {
     * <p>
     * To set up an encrypted link use the
     * {@link #createNetworkSpecifierPassphrase(PeerHandle, String)} API.
     * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}.
     *
     * @param peerHandle The peer's handle obtained through
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, byte[], java.util.List)}
@@ -284,6 +285,7 @@ public class DiscoverySession implements AutoCloseable {
     * android.net.ConnectivityManager.NetworkCallback)}
     * [or other varieties of that API].
     */
    @Deprecated
    public NetworkSpecifier createNetworkSpecifierOpen(@NonNull PeerHandle peerHandle) {
        if (mTerminated) {
            Log.w(TAG, "createNetworkSpecifierOpen: called on terminated session");
@@ -318,6 +320,7 @@ public class DiscoverySession implements AutoCloseable {
     * <p>
     * Note: per the Wi-Fi Aware specification the roles are fixed - a Subscriber is an INITIATOR
     * and a Publisher is a RESPONDER.
     * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}.
     *
     * @param peerHandle The peer's handle obtained through
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle,
@@ -336,6 +339,7 @@ public class DiscoverySession implements AutoCloseable {
     * android.net.ConnectivityManager.NetworkCallback)}
     * [or other varieties of that API].
     */
    @Deprecated
    public NetworkSpecifier createNetworkSpecifierPassphrase(
            @NonNull PeerHandle peerHandle, @NonNull String passphrase) {
        if (!WifiAwareUtils.validatePassphrase(passphrase)) {
@@ -376,6 +380,7 @@ public class DiscoverySession implements AutoCloseable {
     * <p>
     * Note: per the Wi-Fi Aware specification the roles are fixed - a Subscriber is an INITIATOR
     * and a Publisher is a RESPONDER.
     * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}.
     *
     * @param peerHandle The peer's handle obtained through
     * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle,
@@ -397,6 +402,7 @@ public class DiscoverySession implements AutoCloseable {
     *
     * @hide
     */
    @Deprecated
    @SystemApi
    public NetworkSpecifier createNetworkSpecifierPmk(@NonNull PeerHandle peerHandle,
            @NonNull byte[] pmk) {
+139 −13
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.content.Context;
import android.net.ConnectivityManager;
@@ -57,11 +58,7 @@ import java.util.List;
 * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback, Handler)}.
 * <li>Create a Aware network specifier to be used with
 * {@link ConnectivityManager#requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)}
 * to set-up a Aware connection with a peer. Refer to
 * {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)},
 * {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)},
 * {@link WifiAwareSession#createNetworkSpecifierOpen(int, byte[])}, and
 * {@link WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)}.
 * to set-up a Aware connection with a peer. Refer to {@link NetworkSpecifierBuilder}.
 * </ul>
 * <p>
 *     Aware may not be usable when Wi-Fi is disabled (and other conditions). To validate that
@@ -110,10 +107,7 @@ import java.util.List;
 *        <li>{@link NetworkRequest.Builder#addTransportType(int)} of
 *        {@link android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE}.
 *        <li>{@link NetworkRequest.Builder#setNetworkSpecifier(String)} using
 *        {@link WifiAwareSession#createNetworkSpecifierOpen(int, byte[])},
 *        {@link WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)},
 *        {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)}, or
 *        {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}.
 *        {@link NetworkSpecifierBuilder}.
 *    </ul>
 */
@SystemService(Context.WIFI_AWARE_SERVICE)
@@ -145,8 +139,6 @@ public class WifiAwareManager {
     * Connection creation role is that of INITIATOR. Used to create a network specifier string
     * when requesting a Aware network.
     *
     * @see DiscoverySession#createNetworkSpecifierOpen(PeerHandle)
     * @see DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)
     * @see WifiAwareSession#createNetworkSpecifierOpen(int, byte[])
     * @see WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)
     */
@@ -156,8 +148,6 @@ public class WifiAwareManager {
     * Connection creation role is that of RESPONDER. Used to create a network specifier string
     * when requesting a Aware network.
     *
     * @see DiscoverySession#createNetworkSpecifierOpen(PeerHandle)
     * @see DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)
     * @see WifiAwareSession#createNetworkSpecifierOpen(int, byte[])
     * @see WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)
     */
@@ -415,6 +405,11 @@ public class WifiAwareManager {
                    + ", passphrase=" + ((passphrase == null) ? "null" : "non-null"));
        }

        if (!WifiAwareUtils.isLegacyVersion(mContext, Build.VERSION_CODES.Q)) {
            throw new UnsupportedOperationException(
                    "API not deprecated - use WifiAwareManager.NetworkSpecifierBuilder");
        }

        if (role != WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
                && role != WIFI_AWARE_DATA_PATH_ROLE_RESPONDER) {
            throw new IllegalArgumentException(
@@ -813,4 +808,135 @@ public class WifiAwareManager {
            mOriginalCallback.onSessionTerminated();
        }
    }

    /**
     * A builder class for a Wi-Fi Aware network specifier to set up an Aware connection with a
     * peer.
     * <p>
     * Note that all Wi-Fi Aware connection specifier objects must call the
     * {@link NetworkSpecifierBuilder#setDiscoverySession(DiscoverySession)} to specify the context
     * within which the connection is created, and
     * {@link NetworkSpecifierBuilder#setPeerHandle(PeerHandle)} to specify the peer to which the
     * connection is created.
     */
    public static class NetworkSpecifierBuilder {
        private DiscoverySession mDiscoverySession;
        private PeerHandle mPeerHandle;
        private String mPskPassphrase;
        private byte[] mPmk;

        /**
         * Configure the {@link PublishDiscoverySession} or {@link SubscribeDiscoverySession}
         * discovery session in whose context the connection is created.
         * <p>
         * Note: this method must be called for any connection request!
         *
         * @param discoverySession A Wi-Fi Aware discovery session.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         */
        public @NonNull NetworkSpecifierBuilder setDiscoverySession(
                @NonNull DiscoverySession discoverySession) {
            if (discoverySession == null) {
                throw new IllegalArgumentException("Non-null discoverySession required");
            }
            mDiscoverySession = discoverySession;
            return this;
        }

        /**
         * Configure the {@link PeerHandle} of the peer to which the Wi-Fi Aware connection is
         * requested. The peer is discovered through Wi-Fi Aware discovery,
         * <p>
         * Note: this method must be called for any connection request!
         *
         * @param peerHandle The peer's handle obtained through
         * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, byte[], java.util.List)}
         *                   or
         *                   {@link DiscoverySessionCallback#onMessageReceived(PeerHandle, byte[])}.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         */
        public @NonNull NetworkSpecifierBuilder setPeerHandle(@NonNull PeerHandle peerHandle) {
            if (peerHandle == null) {
                throw new IllegalArgumentException("Non-null peerHandle required");
            }
            mPeerHandle = peerHandle;
            return this;
        }

        /**
         * Configure the PSK Passphrase for the Wi-Fi Aware connection being requested. This method
         * is optional - if not called, then an Open (unencrypted) connection will be created.
         *
         * @param pskPassphrase The (optional) passphrase to be used to encrypt the link.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         */
        public @NonNull NetworkSpecifierBuilder setPskPassphrase(@NonNull String pskPassphrase) {
            if (!WifiAwareUtils.validatePassphrase(pskPassphrase)) {
                throw new IllegalArgumentException("Passphrase must meet length requirements");
            }
            mPskPassphrase = pskPassphrase;
            return this;
        }

        /**
         * Configure the PMK for the Wi-Fi Aware connection being requested. This method
         * is optional - if not called, then an Open (unencrypted) connection will be created.
         *
         * @param pmk A PMK (pairwise master key, see IEEE 802.11i) specifying the key to use for
         *            encrypting the data-path. Use the {@link #setPskPassphrase(String)} to
         *            specify a Passphrase.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         * @hide
         */
        @SystemApi
        public @NonNull NetworkSpecifierBuilder setPmk(@NonNull byte[] pmk) {
            if (!WifiAwareUtils.validatePmk(pmk)) {
                throw new IllegalArgumentException("PMK must 32 bytes");
            }
            mPmk = pmk;
            return this;
        }

        /**
         * Create a {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier)}
         * for a WiFi Aware connection (link) to the specified peer. The
         * {@link android.net.NetworkRequest.Builder#addTransportType(int)} should be set to
         * {@link android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE}.
         * <p> The default builder constructor will initialize a NetworkSpecifier which requests an
         * open (non-encrypted) link. To request an encrypted link use the
         * {@link #setPskPassphrase(String)} builder method.
         *
         * @return A {@link NetworkSpecifier} to be used to construct
         * {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier)} to pass
         * to {@link android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest,
         * android.net.ConnectivityManager.NetworkCallback)}
         * [or other varieties of that API].
         */
        public @NonNull NetworkSpecifier build() {
            if (mDiscoverySession == null) {
                throw new IllegalStateException("Null discovery session!?");
            }
            if (mPskPassphrase != null & mPmk != null) {
                throw new IllegalStateException(
                        "Can only specify a Passphrase or a PMK - not both!");
            }

            int role = mDiscoverySession instanceof SubscribeDiscoverySession
                    ? WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
                    : WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER;

            if (role == WIFI_AWARE_DATA_PATH_ROLE_INITIATOR && mPeerHandle == null) {
                throw new IllegalStateException("Null peerHandle!?");
            }

            return new WifiAwareNetworkSpecifier(
                    WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_IB, role,
                    mDiscoverySession.mClientId, mDiscoverySession.mSessionId, mPeerHandle.peerId,
                    null, mPmk, mPskPassphrase, Process.myUid());
        }
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -213,7 +213,7 @@ public class WifiAwareSession implements AutoCloseable {
     *     This API is targeted for applications which can obtain the peer MAC address using OOB
     *     (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
     *     when using Aware discovery use the alternative network specifier method -
     *     {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)}.
     *     {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}.
     * <p>
     * To set up an encrypted link use the
     * {@link #createNetworkSpecifierPassphrase(int, byte[], String)} API.
@@ -254,7 +254,7 @@ public class WifiAwareSession implements AutoCloseable {
     *     This API is targeted for applications which can obtain the peer MAC address using OOB
     *     (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
     *     when using Aware discovery use the alternative network specifier method -
     *     {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}.
     *     {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}.
     *
     * @param role  The role of this device:
     *              {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR} or
@@ -300,7 +300,7 @@ public class WifiAwareSession implements AutoCloseable {
     *     This API is targeted for applications which can obtain the peer MAC address using OOB
     *     (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
     *     when using Aware discovery use the alternative network specifier method -
     *     {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}.
     *     {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}.
     *
     * @param role  The role of this device:
     *              {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR} or
Loading