1712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills/*
2712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * Copyright (C) 2016 The Android Open Source Project
3712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills *
4712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * Licensed under the Apache License, Version 2.0 (the "License");
5712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * you may not use this file except in compliance with the License.
6712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * You may obtain a copy of the License at
7712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills *
8712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills *      http://www.apache.org/licenses/LICENSE-2.0
9712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills *
10712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * Unless required by applicable law or agreed to in writing, software
11712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * distributed under the License is distributed on an "AS IS" BASIS,
12712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * See the License for the specific language governing permissions and
14712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * limitations under the License.
15712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills */
16712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills
17712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Willspackage com.android.server.wifi.scanner;
18712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills
19712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Willsimport android.net.wifi.WifiScanner;
20216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Piusimport android.util.ArraySet;
21712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills
22712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Willsimport com.android.server.wifi.WifiNative;
23712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills
2439175cc807488dd849e2c530f8be30dd674fbd9fMitchell Willsimport java.util.Set;
2539175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills
26712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills/**
27712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * ChannelHelper offers an abstraction for channel manipulation utilities allowing operation to be
28712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills * adjusted based on the amount of information known about the available channels.
29712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills */
30712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Willspublic abstract class ChannelHelper {
31712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills
32798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills    // TODO: Currently this is simply an estimate and is used for both active and passive channels
33798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills    //       scans. Eventually it should be split between passive and active and perhaps retrieved
34798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills    //       from the driver.
35798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills    /**
36798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills     * The estimated period spent scanning each channel. This is used for estimating scan duration.
37798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills     */
38798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills    public static final int SCAN_PERIOD_PER_CHANNEL_MS = 200;
39798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills
40e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills    protected static final WifiScanner.ChannelSpec[] NO_CHANNELS = new WifiScanner.ChannelSpec[0];
41e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills
42712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills    /**
43712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills     * Create a new collection that can be used to store channels
44712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills     */
45712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills    public abstract ChannelCollection createChannelCollection();
46712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills
47712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills    /**
4839175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills     * Return true if the specified channel is expected for a scan with the given settings
4939175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills     */
5039175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills    public abstract boolean settingsContainChannel(WifiScanner.ScanSettings settings, int channel);
5139175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills
5239175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills    /**
53798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills     * Get the channels that are available for scanning on the supplied band.
54798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills     * This method may return empty if the information is not available.
55798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills     */
56798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills    public abstract WifiScanner.ChannelSpec[] getAvailableScanChannels(int band);
57798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills
58798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills    /**
59798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills     * Estimates the duration that the chip will spend scanning with the given settings
60798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills     */
61798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills    public abstract int estimateScanDuration(WifiScanner.ScanSettings settings);
62798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills
63798a997d2e5bf8b3278bfeaa7ca841394e2db4b9Mitchell Wills    /**
64e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills     * Update the channel information that this object has. The source of the update is
65e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills     * implementation dependent and may result in no change. Warning the behavior of a
66e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills     * ChannelCollection created using {@link #createChannelCollection createChannelCollection} is
67e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills     * undefined after calling this method until the {@link ChannelColleciton#clear() clear} method
68e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills     * is called on it.
69e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills     */
70e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills    public void updateChannels() {
71e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills        // default implementation does nothing
72e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills    }
73e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills
74e6d8fa5fb50afdfc04922f7f87c2cac08db5bbecMitchell Wills    /**
75712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills     * Object that supports accumulation of channels and bands
76712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills     */
77712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills    public abstract class ChannelCollection {
78712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        /**
79712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         * Add a channel to the collection
80712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         */
81712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public abstract void addChannel(int channel);
82712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        /**
83712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         * Add all channels in the band to the collection
84712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         */
85712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public abstract void addBand(int band);
86712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        /**
8739175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills         * @return true if the collection contains the supplied channel
8839175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills         */
8939175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills        public abstract boolean containsChannel(int channel);
9039175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills        /**
91216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         * @return true if the collection contains all the channels of the supplied band
92216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         */
93216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        public abstract boolean containsBand(int band);
94216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        /**
95216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         * @return true if the collection contains some of the channels of the supplied band
96216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         */
97216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        public abstract boolean partiallyContainsBand(int band);
98216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        /**
9939175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills         * @return true if the collection contains no channels
10039175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills         */
10139175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills        public abstract boolean isEmpty();
10239175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills        /**
103712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         * Remove all channels from the collection
104712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         */
105712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public abstract void clear();
106216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        /**
107216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         * Retrieves a list of channels from the band which are missing in the channel collection.
108216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         */
109216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        public abstract Set<Integer> getMissingChannelsFromBand(int band);
110216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        /**
111216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         * Retrieves a list of channels from the band which are contained in the channel collection.
112216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         */
113216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        public abstract Set<Integer> getContainingChannelsFromBand(int band);
114216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        /**
115216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         * Gets a list of channels specified in the current channel collection. This will return
116216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         * an empty set if an entire Band if specified or if the list is empty.
117216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         */
118216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        public abstract Set<Integer> getChannelSet();
119712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills
120712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        /**
121712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         * Add all channels in the ScanSetting to the collection
122712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         */
12339175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills        public void addChannels(WifiScanner.ScanSettings scanSettings) {
12439175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills            if (scanSettings.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
12539175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills                for (int j = 0; j < scanSettings.channels.length; ++j) {
12639175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills                    addChannel(scanSettings.channels[j].frequency);
12739175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills                }
12839175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills            } else {
12939175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills                addBand(scanSettings.band);
13039175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills            }
13139175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills        }
13239175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills
13339175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills        /**
13439175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills         * Add all channels in the BucketSettings to the collection
13539175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills         */
13639175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills        public void addChannels(WifiNative.BucketSettings bucketSettings) {
13739175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills            if (bucketSettings.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
13839175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills                for (int j = 0; j < bucketSettings.channels.length; ++j) {
13939175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills                    addChannel(bucketSettings.channels[j].frequency);
140712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills                }
141712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills            } else {
14239175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills                addBand(bucketSettings.band);
143712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills            }
144712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        }
145712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills
146712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        /**
147216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         * Checks if all channels in ScanSetting is in the collection
148216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         */
149216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        public boolean containsSettings(WifiScanner.ScanSettings scanSettings) {
150216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            if (scanSettings.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
151216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                for (int j = 0; j < scanSettings.channels.length; ++j) {
152216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                    if (!containsChannel(scanSettings.channels[j].frequency)) {
153216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                        return false;
154216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                    }
155216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                }
156216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                return true;
157216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            } else {
158216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                return containsBand(scanSettings.band);
159216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            }
160216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        }
161216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius
162216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        /**
163216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         * Checks if at least some of the channels in ScanSetting is in the collection
164216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         */
165216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        public boolean partiallyContainsSettings(WifiScanner.ScanSettings scanSettings) {
166216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            if (scanSettings.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
167216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                for (int j = 0; j < scanSettings.channels.length; ++j) {
168216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                    if (containsChannel(scanSettings.channels[j].frequency)) {
169216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                        return true;
170216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                    }
171216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                }
172216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                return false;
173216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            } else {
174216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                return partiallyContainsBand(scanSettings.band);
175216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            }
176216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        }
177216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius
178216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        /**
179216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         * Retrieves a list of missing channels in the collection from the provided settings.
180216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         */
181216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        public Set<Integer> getMissingChannelsFromSettings(WifiScanner.ScanSettings scanSettings) {
182216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            if (scanSettings.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
183216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                ArraySet<Integer> missingChannels = new ArraySet<>();
184216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                for (int j = 0; j < scanSettings.channels.length; ++j) {
185216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                    if (!containsChannel(scanSettings.channels[j].frequency)) {
186216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                        missingChannels.add(scanSettings.channels[j].frequency);
187216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                    }
188216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                }
189216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                return missingChannels;
190216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            } else {
191216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                return getMissingChannelsFromBand(scanSettings.band);
192216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            }
193216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        }
194216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius
195216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        /**
196216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         * Retrieves a list of containing channels in the collection from the provided settings.
197216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius         */
198216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        public Set<Integer> getContainingChannelsFromSettings(
199216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                WifiScanner.ScanSettings scanSettings) {
200216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            if (scanSettings.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
201216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                ArraySet<Integer> containingChannels = new ArraySet<>();
202216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                for (int j = 0; j < scanSettings.channels.length; ++j) {
203216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                    if (containsChannel(scanSettings.channels[j].frequency)) {
204216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                        containingChannels.add(scanSettings.channels[j].frequency);
205216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                    }
206216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                }
207216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                return containingChannels;
208216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            } else {
209216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius                return getContainingChannelsFromBand(scanSettings.band);
210216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius            }
211216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        }
212216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius
213216eb45e7fb44a1ba8edc156d08b532a17219f66Roshan Pius        /**
214712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         * Store the channels in this collection in the supplied BucketSettings. If maxChannels is
215712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         * exceeded or a band better describes the channels then a band is specified instead of a
216712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         * channel list.
217712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills         */
218712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills        public abstract void fillBucketSettings(WifiNative.BucketSettings bucket, int maxChannels);
21939175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills
22039175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills        /**
22139175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills         * Gets the list of channels that should be supplied to supplicant for a scan. Will either
22239175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills         * be a collection of all channels or null if all channels should be scanned.
22339175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills         */
22439175cc807488dd849e2c530f8be30dd674fbd9fMitchell Wills        public abstract Set<Integer> getSupplicantScanFreqs();
225712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills    }
22654e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills
22754e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills
22854e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    /*
22954e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills     * Utility methods for converting band/channels to strings
23054e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills     */
23154e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills
23254e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    /**
23354e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills     * Create a string representation of the channels in the ScanSettings.
23454e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills     * If it contains a list of channels then the channels are returned, otherwise a string name of
23554e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills     * the band is returned.
23654e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills     */
23754e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    public static String toString(WifiScanner.ScanSettings scanSettings) {
23854e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        if (scanSettings.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
23954e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            return toString(scanSettings.channels);
24054e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        } else {
24154e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            return toString(scanSettings.band);
24254e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        }
24354e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    }
24454e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills
24554e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    /**
24654e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills     * Create a string representation of the channels in the BucketSettings.
24754e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills     * If it contains a list of channels then the channels are returned, otherwise a string name of
24854e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills     * the band is returned.
24954e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills     */
25054e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    public static String toString(WifiNative.BucketSettings bucketSettings) {
25154e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        if (bucketSettings.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
25254e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            return toString(bucketSettings.channels, bucketSettings.num_channels);
25354e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        } else {
25454e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            return toString(bucketSettings.band);
25554e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        }
25654e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    }
25754e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills
25854e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    private static String toString(WifiScanner.ChannelSpec[] channels) {
25954e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        if (channels == null) {
26054e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            return "null";
26154e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        }
26254e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills
26354e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        StringBuilder sb = new StringBuilder();
26454e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        sb.append("[");
26554e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        for (int c = 0; c < channels.length; c++) {
26654e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            sb.append(channels[c].frequency);
26754e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            if (c != channels.length - 1) {
26854e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills                sb.append(",");
26954e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            }
27054e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        }
27154e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        sb.append("]");
27254e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        return sb.toString();
27354e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    }
27454e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills
27554e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    private static String toString(WifiNative.ChannelSettings[] channels, int numChannels) {
27654e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        if (channels == null) {
27754e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            return "null";
27854e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        }
27954e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills
28054e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        StringBuilder sb = new StringBuilder();
28154e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        sb.append("[");
28254e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        for (int c = 0; c < numChannels; c++) {
28354e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            sb.append(channels[c].frequency);
28454e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            if (c != numChannels - 1) {
28554e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills                sb.append(",");
28654e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            }
28754e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        }
28854e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        sb.append("]");
28954e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        return sb.toString();
29054e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    }
29154e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills
29254e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    private static String toString(int band) {
29354e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        switch (band) {
29454e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            case WifiScanner.WIFI_BAND_UNSPECIFIED:
29554e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills                return "unspecified";
29654e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            case WifiScanner.WIFI_BAND_24_GHZ:
29754e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills                return "24Ghz";
29854e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            case WifiScanner.WIFI_BAND_5_GHZ:
29954e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills                return "5Ghz (no DFS)";
30054e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            case WifiScanner.WIFI_BAND_5_GHZ_DFS_ONLY:
30154e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills                return "5Ghz (DFS only)";
30254e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            case WifiScanner.WIFI_BAND_5_GHZ_WITH_DFS:
30354e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills                return "5Ghz (DFS incl)";
30454e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            case WifiScanner.WIFI_BAND_BOTH:
30554e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills                return "24Ghz & 5Ghz (no DFS)";
30654e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills            case WifiScanner.WIFI_BAND_BOTH_WITH_DFS:
30754e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills                return "24Ghz & 5Ghz (DFS incl)";
30854e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        }
30954e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills
31054e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills        return "invalid band";
31154e2c84547e22622ba4a08bddd1c632d1d937be3Mitchell Wills    }
312712ef6246834caeac3d5b06bea08e85d6b29cd7aMitchell Wills}
313