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

Commit 557b77fb authored by Etan Cohen's avatar Etan Cohen
Browse files

[AWARE] Add Wi-Fi Aware-specific Network Capabilities

Add Wi-Fi Aware-specific Network Capabilities which are
used to send IPv6 as part of Wi-Fi Aware data-path setup.

Bug: 117605977
Test: (unit tests) atest android.net.wifi.aware
Test: (ACTS) ThroughputTest:test_iperf_single_ndp_aware_only_ib
Change-Id: If806f82b8b3d729c3e5c5ded4883deb7e49d14f5
parent 609b6695
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -29643,6 +29643,13 @@ package android.net.wifi.aware {
    field public static final int WIFI_AWARE_DATA_PATH_ROLE_RESPONDER = 1; // 0x1
  }
  public final class WifiAwareNetworkInfo implements android.os.Parcelable android.net.TransportInfo {
    method public int describeContents();
    method public java.net.Inet6Address getPeerIpv6Addr();
    method public void writeToParcel(android.os.Parcel, int);
    field public static final android.os.Parcelable.Creator<android.net.wifi.aware.WifiAwareNetworkInfo> CREATOR;
  }
  public class WifiAwareSession implements java.lang.AutoCloseable {
    method public void close();
    method public android.net.NetworkSpecifier createNetworkSpecifierOpen(int, byte[]);
+135 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.wifi.aware;

import android.annotation.Nullable;
import android.net.NetworkCapabilities;
import android.net.TransportInfo;
import android.os.Parcel;
import android.os.Parcelable;

import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Objects;

/**
 * Wi-Fi Aware-specific network information. The information can be extracted from the
 * {@link android.net.NetworkCapabilities} of the network using
 * {@link NetworkCapabilities#getTransportInfo()}.
 * The {@link NetworkCapabilities} is provided by the connectivity service to apps, e.g. received
 * through the
 * {@link android.net.ConnectivityManager.NetworkCallback#onCapabilitiesChanged(android.net.Network,
 * android.net.NetworkCapabilities)} callback.
 * <p>
 * The Wi-Fi Aware-specific network information include the peer's scoped link-local IPv6 address
 * for the Wi-Fi Aware link. The scoped link-local IPv6 can then be used to create a
 * {@link java.net.Socket} connection to the peer.
 */
public final class WifiAwareNetworkInfo implements TransportInfo, Parcelable {
    private Inet6Address mIpv6Addr;

    /** @hide */
    public WifiAwareNetworkInfo(Inet6Address ipv6Addr) {
        mIpv6Addr = ipv6Addr;
    }

    /**
     * Get the scoped link-local IPv6 address of the Wi-Fi Aware peer (not of the local device!).
     *
     * @return An IPv6 address.
     */
    @Nullable
    public Inet6Address getPeerIpv6Addr() {
        return mIpv6Addr;
    }

    // parcelable methods

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByteArray(mIpv6Addr.getAddress());
        NetworkInterface ni = mIpv6Addr.getScopedInterface();
        dest.writeString(ni == null ? null : ni.getName());
    }

    public static final Creator<WifiAwareNetworkInfo> CREATOR =
            new Creator<WifiAwareNetworkInfo>() {
                @Override
                public WifiAwareNetworkInfo createFromParcel(Parcel in) {
                    Inet6Address ipv6Addr;
                    try {
                        byte[] addr = in.createByteArray();
                        String interfaceName = in.readString();
                        NetworkInterface ni = null;
                        if (interfaceName != null) {
                            try {
                                ni = NetworkInterface.getByName(interfaceName);
                            } catch (SocketException e) {
                                e.printStackTrace();
                            }
                        }
                        ipv6Addr = Inet6Address.getByAddress(null, addr, ni);
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                        return null;
                    }

                    return new WifiAwareNetworkInfo(ipv6Addr);
                }

                @Override
                public WifiAwareNetworkInfo[] newArray(int size) {
                    return new WifiAwareNetworkInfo[size];
                }
            };


    // object methods

    @Override
    public String toString() {
        return new StringBuilder("AwareNetworkInfo: IPv6=").append(mIpv6Addr).toString();
    }

    /** @hide */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof WifiAwareNetworkInfo)) {
            return false;
        }

        WifiAwareNetworkInfo lhs = (WifiAwareNetworkInfo) obj;
        return Objects.equals(mIpv6Addr, lhs.mIpv6Addr);
    }

    /** @hide */
    @Override
    public int hashCode() {
        return Objects.hash(mIpv6Addr);
    }
}
+30 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.MacAddress;
import android.net.wifi.RttManager;
import android.os.Build;
import android.os.Handler;
@@ -50,6 +51,8 @@ import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.net.Inet6Address;
import java.net.UnknownHostException;
import java.util.List;

/**
@@ -1245,4 +1248,31 @@ public class WifiAwareManagerTest {
            sessionCaptor.getValue().createNetworkSpecifierPassphrase(role, someMac, passphrase);
        }
    }

    // WifiAwareNetworkInfo tests

    @Test
    public void testWifiAwareNetworkCapabilitiesParcel() throws UnknownHostException {
        final Inet6Address inet6 = MacAddress.fromString(
                "11:22:33:44:55:66").getLinkLocalIpv6FromEui48Mac();
        // note: dummy scope = 5
        final Inet6Address inet6Scoped = Inet6Address.getByAddress(null, inet6.getAddress(), 5);

        assertEquals(inet6Scoped.toString(), "/fe80::1322:33ff:fe44:5566%5");
        WifiAwareNetworkInfo cap = new WifiAwareNetworkInfo(inet6Scoped);

        Parcel parcelW = Parcel.obtain();
        cap.writeToParcel(parcelW, 0);
        byte[] bytes = parcelW.marshall();
        parcelW.recycle();

        Parcel parcelR = Parcel.obtain();
        parcelR.unmarshall(bytes, 0, bytes.length);
        parcelR.setDataPosition(0);
        WifiAwareNetworkInfo rereadCap =
                WifiAwareNetworkInfo.CREATOR.createFromParcel(parcelR);

        assertEquals(cap.getPeerIpv6Addr().toString(), "/fe80::1322:33ff:fe44:5566%5");
        assertEquals(cap.hashCode(), rereadCap.hashCode());
    }
}