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

Commit 48856ee3 authored by Etan Cohen's avatar Etan Cohen
Browse files

[RTT2] Convert APIs to use MacAddress

MacAddress class is being added as a public API. Convert ad-hoc
byte[] represetations to new class.

Bug: 65108607
Test: unit tests and integration tests
Change-Id: I4bf3b7c47f37d4ef4dd5af0dcdeb7d57f2f94368
parent f10f83d5
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.net.wifi.rtt;

import android.annotation.NonNull;
import android.net.MacAddress;
import android.net.wifi.ScanResult;
import android.net.wifi.aware.AttachCallback;
import android.net.wifi.aware.DiscoverySessionCallback;
@@ -168,7 +169,7 @@ public final class RangingRequest implements Parcelable {
         * @param peerMacAddress The MAC address of the Wi-Fi Aware peer.
         * @return The builder, to facilitate chaining {@code builder.setXXX(..).setXXX(..)}.
         */
        public Builder addWifiAwarePeer(@NonNull byte[] peerMacAddress) {
        public Builder addWifiAwarePeer(@NonNull MacAddress peerMacAddress) {
            if (peerMacAddress == null) {
                throw new IllegalArgumentException("Null peer MAC address");
            }
+22 −14
Original line number Diff line number Diff line
@@ -17,16 +17,15 @@
package android.net.wifi.rtt;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.net.MacAddress;
import android.net.wifi.aware.PeerHandle;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;

import libcore.util.HexEncoding;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@@ -62,7 +61,7 @@ public final class RangingResult implements Parcelable {
    public static final int STATUS_FAIL = 1;

    private final int mStatus;
    private final byte[] mMac;
    private final MacAddress mMac;
    private final PeerHandle mPeerHandle;
    private final int mDistanceMm;
    private final int mDistanceStdDevMm;
@@ -70,7 +69,7 @@ public final class RangingResult implements Parcelable {
    private final long mTimestamp;

    /** @hide */
    public RangingResult(@RangeResultStatus int status, byte[] mac, int distanceMm,
    public RangingResult(@RangeResultStatus int status, @NonNull MacAddress mac, int distanceMm,
            int distanceStdDevMm, int rssi, long timestamp) {
        mStatus = status;
        mMac = mac;
@@ -109,7 +108,7 @@ public final class RangingResult implements Parcelable {
     * Will return a {@code null} for results corresponding to requests issued using a {@code
     * PeerHandle}, i.e. using the {@link RangingRequest.Builder#addWifiAwarePeer(PeerHandle)} API.
     */
    public byte[] getMacAddress() {
    public MacAddress getMacAddress() {
        return mMac;
    }

@@ -193,7 +192,12 @@ public final class RangingResult implements Parcelable {
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mStatus);
        dest.writeByteArray(mMac);
        if (mMac == null) {
            dest.writeBoolean(false);
        } else {
            dest.writeBoolean(true);
            mMac.writeToParcel(dest, flags);
        }
        if (mPeerHandle == null) {
            dest.writeBoolean(false);
        } else {
@@ -216,7 +220,11 @@ public final class RangingResult implements Parcelable {
        @Override
        public RangingResult createFromParcel(Parcel in) {
            int status = in.readInt();
            byte[] mac = in.createByteArray();
            boolean macAddressPresent = in.readBoolean();
            MacAddress mac = null;
            if (macAddressPresent) {
                mac = MacAddress.CREATOR.createFromParcel(in);
            }
            boolean peerHandlePresent = in.readBoolean();
            PeerHandle peerHandle = null;
            if (peerHandlePresent) {
@@ -240,11 +248,11 @@ public final class RangingResult implements Parcelable {
    @Override
    public String toString() {
        return new StringBuilder("RangingResult: [status=").append(mStatus).append(", mac=").append(
                mMac == null ? "<null>" : new String(HexEncoding.encodeToString(mMac))).append(
                ", peerHandle=").append(mPeerHandle == null ? "<null>" : mPeerHandle.peerId).append(
                ", distanceMm=").append(mDistanceMm).append(", distanceStdDevMm=").append(
                mDistanceStdDevMm).append(", rssi=").append(mRssi).append(", timestamp=").append(
                mTimestamp).append("]").toString();
                mMac).append(", peerHandle=").append(
                mPeerHandle == null ? "<null>" : mPeerHandle.peerId).append(", distanceMm=").append(
                mDistanceMm).append(", distanceStdDevMm=").append(mDistanceStdDevMm).append(
                ", rssi=").append(mRssi).append(", timestamp=").append(mTimestamp).append(
                "]").toString();
    }

    @Override
@@ -259,7 +267,7 @@ public final class RangingResult implements Parcelable {

        RangingResult lhs = (RangingResult) o;

        return mStatus == lhs.mStatus && Arrays.equals(mMac, lhs.mMac) && Objects.equals(
        return mStatus == lhs.mStatus && Objects.equals(mMac, lhs.mMac) && Objects.equals(
                mPeerHandle, lhs.mPeerHandle) && mDistanceMm == lhs.mDistanceMm
                && mDistanceStdDevMm == lhs.mDistanceStdDevMm && mRssi == lhs.mRssi
                && mTimestamp == lhs.mTimestamp;
+26 −20
Original line number Diff line number Diff line
@@ -24,11 +24,8 @@ import android.net.wifi.aware.PeerHandle;
import android.os.Parcel;
import android.os.Parcelable;

import libcore.util.HexEncoding;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.Objects;

/**
@@ -127,13 +124,13 @@ public class ResponderConfig implements Parcelable {
     * peerHandle field) ise used to identify the Responder.
     * TODO: convert to MacAddress
     */
    public byte[] macAddress;
    public MacAddress macAddress;

    /**
     * The peer identifier of a Wi-Fi Aware Responder. Will be null if a MAC Address (the macAddress
     * field) is used to identify the Responder.
     */
    public final PeerHandle peerHandle;
    public PeerHandle peerHandle;

    /**
     * The device type of the Responder.
@@ -194,9 +191,13 @@ public class ResponderConfig implements Parcelable {
     * @param preamble        The preamble used by the Responder, specified using
     *                        {@link PreambleType}.
     */
    public ResponderConfig(@NonNull byte[] macAddress, @ResponderType int responderType,
    public ResponderConfig(@NonNull MacAddress macAddress, @ResponderType int responderType,
            boolean supports80211mc, @ChannelWidth int channelWidth, int frequency, int centerFreq0,
            int centerFreq1, @PreambleType int preamble) {
        if (macAddress == null) {
            throw new IllegalArgumentException(
                    "Invalid ResponderConfig - must specify a MAC address");
        }
        this.macAddress = macAddress;
        this.peerHandle = null;
        this.responderType = responderType;
@@ -248,10 +249,7 @@ public class ResponderConfig implements Parcelable {
     * Point (AP), which can be obtained from {@link android.net.wifi.WifiManager#getScanResults()}.
     */
    public static ResponderConfig fromScanResult(ScanResult scanResult) {
        byte[] macAddress = null;
        if (scanResult.BSSID != null) {
            macAddress = MacAddress.byteAddrFromStringAddr(scanResult.BSSID);
        }
        MacAddress macAddress = MacAddress.fromString(scanResult.BSSID);
        int responderType = RESPONDER_AP;
        boolean supports80211mc = scanResult.is80211mcResponder();
        int channelWidth = translcateScanResultChannelWidth(scanResult.channelWidth);
@@ -275,7 +273,7 @@ public class ResponderConfig implements Parcelable {
     * Creates a Responder configuration from a MAC address corresponding to a Wi-Fi Aware
     * Responder. The Responder parameters are set to defaults.
     */
    public static ResponderConfig fromWifiAwarePeerMacAddressWithDefaults(byte[] macAddress) {
    public static ResponderConfig fromWifiAwarePeerMacAddressWithDefaults(MacAddress macAddress) {
        /* Note: the parameters are those of the Aware discovery channel (channel 6). A Responder
         * is expected to be brought up and available to negotiate a maximum accuracy channel
         * (i.e. Band 5 @ 80MHz). A Responder is brought up on the peer by starting an Aware
@@ -323,11 +321,16 @@ public class ResponderConfig implements Parcelable {

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByteArray(macAddress);
        if (macAddress == null) {
            dest.writeBoolean(false);
        } else {
            dest.writeBoolean(true);
            macAddress.writeToParcel(dest, flags);
        }
        if (peerHandle == null) {
            dest.writeInt(0);
            dest.writeBoolean(false);
        } else {
            dest.writeInt(1);
            dest.writeBoolean(true);
            dest.writeInt(peerHandle.peerId);
        }
        dest.writeInt(responderType);
@@ -347,10 +350,14 @@ public class ResponderConfig implements Parcelable {

        @Override
        public ResponderConfig createFromParcel(Parcel in) {
            byte[] macAddress = in.createByteArray();
            int peerHandleFlag = in.readInt();
            boolean macAddressPresent = in.readBoolean();
            MacAddress macAddress = null;
            if (macAddressPresent) {
                macAddress = MacAddress.CREATOR.createFromParcel(in);
            }
            boolean peerHandlePresent = in.readBoolean();
            PeerHandle peerHandle = null;
            if (peerHandleFlag == 1) {
            if (peerHandlePresent) {
                peerHandle = new PeerHandle(in.readInt());
            }
            int responderType = in.readInt();
@@ -383,7 +390,7 @@ public class ResponderConfig implements Parcelable {

        ResponderConfig lhs = (ResponderConfig) o;

        return Arrays.equals(macAddress, lhs.macAddress) && Objects.equals(peerHandle,
        return Objects.equals(macAddress, lhs.macAddress) && Objects.equals(peerHandle,
                lhs.peerHandle) && responderType == lhs.responderType
                && supports80211mc == lhs.supports80211mc && channelWidth == lhs.channelWidth
                && frequency == lhs.frequency && centerFreq0 == lhs.centerFreq0
@@ -399,8 +406,7 @@ public class ResponderConfig implements Parcelable {
    /** @hide */
    @Override
    public String toString() {
        return new StringBuffer("ResponderConfig: macAddress=").append(
                macAddress == null ? "<null>" : new String(HexEncoding.encode(macAddress))).append(
        return new StringBuffer("ResponderConfig: macAddress=").append(macAddress).append(
                ", peerHandle=").append(peerHandle == null ? "<null>" : peerHandle.peerId).append(
                ", responderType=").append(responderType).append(", supports80211mc=").append(
                supports80211mc).append(", channelWidth=").append(channelWidth).append(
+10 −8
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.net.MacAddress;
import android.net.wifi.ScanResult;
import android.net.wifi.aware.PeerHandle;
import android.os.Handler;
@@ -33,8 +34,6 @@ import android.os.Parcel;
import android.os.test.TestLooper;
import android.test.suitebuilder.annotation.SmallTest;

import libcore.util.HexEncoding;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
@@ -73,13 +72,15 @@ public class WifiRttManagerTest {
    }

    /**
     * Validate ranging call flow with succesful results.
     * Validate ranging call flow with successful results.
     */
    @Test
    public void testRangeSuccess() throws Exception {
        RangingRequest request = new RangingRequest.Builder().build();
        List<RangingResult> results = new ArrayList<>();
        results.add(new RangingResult(RangingResult.STATUS_SUCCESS, (byte[]) null, 15, 5, 10, 666));
        results.add(
                new RangingResult(RangingResult.STATUS_SUCCESS, MacAddress.BROADCAST_ADDRESS, 15, 5,
                        10, 666));
        RangingResultCallback callbackMock = mock(RangingResultCallback.class);
        ArgumentCaptor<IRttCallback> callbackCaptor = ArgumentCaptor.forClass(IRttCallback.class);

@@ -135,7 +136,7 @@ public class WifiRttManagerTest {
        List<ScanResult> scanResults2and3 = new ArrayList<>(2);
        scanResults2and3.add(scanResult2);
        scanResults2and3.add(scanResult3);
        final byte[] mac1 = HexEncoding.decode("000102030405".toCharArray(), false);
        MacAddress mac1 = MacAddress.fromString("00:01:02:03:04:05");
        PeerHandle peerHandle1 = new PeerHandle(12);

        RangingRequest.Builder builder = new RangingRequest.Builder();
@@ -169,7 +170,7 @@ public class WifiRttManagerTest {
        for (int i = 0; i < RangingRequest.getMaxPeers() - 3; ++i) {
            scanResultList.add(scanResult);
        }
        final byte[] mac1 = HexEncoding.decode("000102030405".toCharArray(), false);
        MacAddress mac1 = MacAddress.fromString("00:01:02:03:04:05");

        // create request
        RangingRequest.Builder builder = new RangingRequest.Builder();
@@ -189,11 +190,12 @@ public class WifiRttManagerTest {
    @Test(expected = IllegalArgumentException.class)
    public void testRangingRequestPastLimit() {
        ScanResult scanResult = new ScanResult();
        scanResult.BSSID = "00:01:02:03:04:05";
        List<ScanResult> scanResultList = new ArrayList<>();
        for (int i = 0; i < RangingRequest.getMaxPeers() - 2; ++i) {
            scanResultList.add(scanResult);
        }
        final byte[] mac1 = HexEncoding.decode("000102030405".toCharArray(), false);
        MacAddress mac1 = MacAddress.fromString("00:01:02:03:04:05");

        // create request
        RangingRequest.Builder builder = new RangingRequest.Builder();
@@ -228,7 +230,7 @@ public class WifiRttManagerTest {
    public void testRangingResultsParcel() {
        // Note: not validating parcel code of ScanResult (assumed to work)
        int status = RangingResult.STATUS_SUCCESS;
        final byte[] mac = HexEncoding.decode("000102030405".toCharArray(), false);
        final MacAddress mac = MacAddress.fromString("00:01:02:03:04:05");
        PeerHandle peerHandle = new PeerHandle(10);
        int distanceCm = 105;
        int distanceStdDevCm = 10;