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

Commit 00bbf34e authored by Roshan Pius's avatar Roshan Pius
Browse files

WifiScanner: Add scan request type to ScanSettings

This new 'type' parameter in ScanSettings is used to convey the goal of
the scan to the underlying wifi chip. This parameter is intentionally
abstract to let the wifi chip vendors decide what type of scan to
perform on devices which support multiple scan modes (DBS, MCC, etc).

Bug: 68335251
Test: Unit tests
Change-Id: Iabddad980128848938b6e2223ad199345812615b
parent cdba028b
Loading
Loading
Loading
Loading
+29 −1
Original line number Diff line number Diff line
@@ -160,6 +160,24 @@ public class WifiScanner {
     */
    public static final int REPORT_EVENT_NO_BATCH = (1 << 2);

    /**
     * This is used to indicate the purpose of the scan to the wifi chip in
     * {@link ScanSettings#type}.
     * On devices with multiple hardware radio chains (and hence different modes of scan),
     * this type serves as an indication to the hardware on what mode of scan to perform.
     * Only apps holding android.Manifest.permission.NETWORK_STACK permission can set this value.
     *
     * Note: This serves as an intent and not as a stipulation, the wifi chip
     * might honor or ignore the indication based on the current radio conditions. Always
     * use the {@link ScanResult#radioChainInfos} to figure out the radio chain configuration used
     * to receive the corresponding scan result.
     */
    /** {@hide} */
    public static final int TYPE_LOW_LATENCY = 0;
    /** {@hide} */
    public static final int TYPE_LOW_POWER = 1;
    /** {@hide} */
    public static final int TYPE_HIGH_ACCURACY = 2;

    /** {@hide} */
    public static final String SCAN_PARAMS_SCAN_SETTINGS_KEY = "ScanSettings";
@@ -193,7 +211,8 @@ public class WifiScanner {
         * list of hidden networks to scan for. Explicit probe requests are sent out for such
         * networks during scan. Only valid for single scan requests.
         * {@hide}
         * */
         */
        @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
        public HiddenNetwork[] hiddenNetworks;
        /** period of background scan; in millisecond, 0 => single shot scan */
        public int periodInMs;
@@ -223,6 +242,13 @@ public class WifiScanner {
         * {@hide}
         */
        public boolean isPnoScan;
        /**
         * Indicate the type of scan to be performed by the wifi chip.
         * Default value: {@link #TYPE_LOW_LATENCY}.
         * {@hide}
         */
        @RequiresPermission(android.Manifest.permission.NETWORK_STACK)
        public int type = TYPE_LOW_LATENCY;

        /** Implement the Parcelable interface {@hide} */
        public int describeContents() {
@@ -239,6 +265,7 @@ public class WifiScanner {
            dest.writeInt(maxPeriodInMs);
            dest.writeInt(stepCount);
            dest.writeInt(isPnoScan ? 1 : 0);
            dest.writeInt(type);
            if (channels != null) {
                dest.writeInt(channels.length);
                for (int i = 0; i < channels.length; i++) {
@@ -272,6 +299,7 @@ public class WifiScanner {
                        settings.maxPeriodInMs = in.readInt();
                        settings.stepCount = in.readInt();
                        settings.isPnoScan = in.readInt() == 1;
                        settings.type = in.readInt();
                        int num_channels = in.readInt();
                        settings.channels = new ChannelSpec[num_channels];
                        for (int i = 0; i < num_channels; i++) {
+50 −0
Original line number Diff line number Diff line
@@ -16,19 +16,23 @@

package android.net.wifi;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.validateMockitoUsage;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.os.Handler;
import android.os.Parcel;
import android.os.test.TestLooper;
import android.test.suitebuilder.annotation.SmallTest;
import android.net.wifi.WifiScanner.ScanSettings;

import com.android.internal.util.test.BidirectionalAsyncChannelServer;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@@ -70,4 +74,50 @@ public class WifiScannerTest {
        validateMockitoUsage();
    }

    /**
     * Verify parcel read/write for ScanSettings.
     */
    @Test
    public void verifyScanSettingsParcelWithBand() throws Exception {
        ScanSettings writeSettings = new ScanSettings();
        writeSettings.type = WifiScanner.TYPE_LOW_POWER;
        writeSettings.band = WifiScanner.WIFI_BAND_BOTH_WITH_DFS;

        ScanSettings readSettings = parcelWriteRead(writeSettings);
        assertEquals(readSettings.type, writeSettings.type);
        assertEquals(readSettings.band, writeSettings.band);
        assertEquals(0, readSettings.channels.length);
    }

    /**
     * Verify parcel read/write for ScanSettings.
     */
    @Test
    public void verifyScanSettingsParcelWithChannels() throws Exception {
        ScanSettings writeSettings = new ScanSettings();
        writeSettings.type = WifiScanner.TYPE_HIGH_ACCURACY;
        writeSettings.band = WifiScanner.WIFI_BAND_UNSPECIFIED;
        writeSettings.channels = new WifiScanner.ChannelSpec[] {
                new WifiScanner.ChannelSpec(5),
                new WifiScanner.ChannelSpec(7)
        };

        ScanSettings readSettings = parcelWriteRead(writeSettings);
        assertEquals(writeSettings.type, readSettings.type);
        assertEquals(writeSettings.band, readSettings.band);
        assertEquals(2, readSettings.channels.length);
        assertEquals(5, readSettings.channels[0].frequency);
        assertEquals(7, readSettings.channels[1].frequency);
    }

    /**
     * Write the provided {@link ScanSettings} to a parcel and deserialize it.
     */
    private static ScanSettings parcelWriteRead(ScanSettings writeSettings) throws Exception {
        Parcel parcel = Parcel.obtain();
        writeSettings.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
        return ScanSettings.CREATOR.createFromParcel(parcel);
    }

}