ScanTestUtil.java revision 297c3acabe7a85eb87240fe3ccf772e57ce6aef7
1/*
2 * Copyright (C) 2015 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
17package com.android.server.wifi;
18
19import static org.mockito.Mockito.when;
20
21import android.net.wifi.ScanResult;
22import android.net.wifi.WifiScanner;
23import android.net.wifi.WifiScanner.ChannelSpec;
24import android.net.wifi.WifiScanner.ScanData;
25import android.net.wifi.WifiScanner.ScanSettings;
26import android.net.wifi.WifiSsid;
27
28import com.android.server.wifi.WifiNative.BucketSettings;
29
30import java.lang.reflect.Field;
31
32/**
33 * Utilities for testing Wifi Scanning
34 */
35
36public class ScanTestUtil {
37
38    public static void installWlanWifiNative(WifiNative wifiNative) throws Exception {
39        Field field = WifiNative.class.getDeclaredField("wlanNativeInterface");
40        field.setAccessible(true);
41        field.set(null, wifiNative);
42
43        // Clear static state
44        WifiChannelHelper.clearChannelCache();
45    }
46
47    public static void setupMockChannels(WifiNative wifiNative, int[] channels24, int[] channels5,
48            int[] channelsDfs) throws Exception {
49        when(wifiNative.getChannelsForBand(WifiScanner.WIFI_BAND_24_GHZ))
50                .thenReturn(channels24);
51        when(wifiNative.getChannelsForBand(WifiScanner.WIFI_BAND_5_GHZ))
52                .thenReturn(channels5);
53        when(wifiNative.getChannelsForBand(WifiScanner.WIFI_BAND_5_GHZ_DFS_ONLY))
54                .thenReturn(channelsDfs);
55    }
56
57    public static ScanSettings createRequest(WifiScanner.ChannelSpec[] channels, int period,
58            int batch, int bssidsPerScan, int reportEvents) {
59        ScanSettings request = new ScanSettings();
60        request.band = WifiScanner.WIFI_BAND_UNSPECIFIED;
61        request.channels = channels;
62        request.periodInMs = period;
63        request.numBssidsPerScan = bssidsPerScan;
64        request.maxScansToCache = batch;
65        request.reportEvents = reportEvents;
66        return request;
67    }
68
69    public static ScanSettings createRequest(int band, int period, int batch, int bssidsPerScan,
70            int reportEvents) {
71        ScanSettings request = new ScanSettings();
72        request.band = band;
73        request.channels = null;
74        request.periodInMs = period;
75        request.numBssidsPerScan = bssidsPerScan;
76        request.maxScansToCache = batch;
77        request.reportEvents = reportEvents;
78        return request;
79    }
80
81    public static ScanResult createScanResult(int freq) {
82        return new ScanResult(WifiSsid.createFromAsciiEncoded("AN SSID"), "00:00:00:00:00:00", "",
83                0, freq, 0);
84    }
85
86    public static ScanData createScanData(int... freqs) {
87        ScanResult[] results = new ScanResult[freqs.length];
88        for (int i = 0; i < freqs.length; ++i) {
89            results[i] = createScanResult(freqs[i]);
90        }
91        return new ScanData(0, 0, results);
92    }
93
94    public static ScanData[] createScanDatas(int[][] freqs) {
95        ScanData[] data = new ScanData[freqs.length];
96        for (int i = 0; i < freqs.length; ++i) {
97            data[i] = createScanData(freqs[i]);
98        }
99        return data;
100    }
101
102    public static WifiScanner.ChannelSpec[] channelsToSpec(int... channels) {
103        WifiScanner.ChannelSpec[] channelSpecs = new WifiScanner.ChannelSpec[channels.length];
104        for (int i = 0; i < channels.length; ++i) {
105            channelSpecs[i] = new WifiScanner.ChannelSpec(channels[i]);
106        }
107        return channelSpecs;
108    }
109
110    public static ChannelSpec[] getAllChannels(BucketSettings bucket) {
111        if (bucket.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
112            ChannelSpec[] channels = new ChannelSpec[bucket.num_channels];
113            for (int i = 0; i < bucket.num_channels; i++) {
114                channels[i] = new ChannelSpec(bucket.channels[i].frequency);
115            }
116            return channels;
117        } else {
118            return WifiChannelHelper.getChannelsForBand(bucket.band);
119        }
120    }
121    public static ChannelSpec[] getAllChannels(ScanSettings settings) {
122        if (settings.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
123            ChannelSpec[] channels = new ChannelSpec[settings.channels.length];
124            for (int i = 0; i < settings.channels.length; i++) {
125                channels[i] = new ChannelSpec(settings.channels[i].frequency);
126            }
127            return channels;
128        } else {
129            return WifiChannelHelper.getChannelsForBand(settings.band);
130        }
131    }
132}
133