19ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills/*
29ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills * Copyright (C) 2015 The Android Open Source Project
39ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills *
49ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills * Licensed under the Apache License, Version 2.0 (the "License");
59ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills * you may not use this file except in compliance with the License.
69ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills * You may obtain a copy of the License at
79ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills *
89ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills *      http://www.apache.org/licenses/LICENSE-2.0
99ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills *
109ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills * Unless required by applicable law or agreed to in writing, software
119ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills * distributed under the License is distributed on an "AS IS" BASIS,
129ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills * See the License for the specific language governing permissions and
149ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills * limitations under the License.
159ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills */
169ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
17a8367288377cbaed6371256ca837b7aa22280706Mitchell Willspackage com.android.server.wifi.scanner;
189ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
199ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Willsimport android.content.Context;
202771787818003e53e8175036a3d09688c783f350Mitchell Willsimport android.net.wifi.ScanResult;
219ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Willsimport android.net.wifi.WifiScanner;
229ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Willsimport android.os.Looper;
239ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
24ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Piusimport com.android.server.wifi.Clock;
25a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.WifiNative;
269ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
272771787818003e53e8175036a3d09688c783f350Mitchell Willsimport java.util.Comparator;
282771787818003e53e8175036a3d09688c783f350Mitchell Wills
29f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills/**
30f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills * Defines the interface to the Wifi hardware required for the WifiScanner API
31f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills */
329ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Willspublic abstract class WifiScannerImpl {
3394bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills
34f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills    /**
35a8367288377cbaed6371256ca837b7aa22280706Mitchell Wills     * A factory that create a {@link com.android.server.wifi.scanner.WifiScannerImpl}
36f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     */
3794bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills    public static interface WifiScannerImplFactory {
38ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius        WifiScannerImpl create(Context context, Looper looper, Clock clock);
399ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    }
409ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
412771787818003e53e8175036a3d09688c783f350Mitchell Wills    /**
4294bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills     * Factory that create the implementation that is most appropriate for the system.
4394bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills     * This factory should only ever be used once.
4494bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills     */
4594bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills    public static final WifiScannerImplFactory DEFAULT_FACTORY = new WifiScannerImplFactory() {
46ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius            public WifiScannerImpl create(Context context, Looper looper, Clock clock) {
4794bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills                WifiNative wifiNative = WifiNative.getWlanNativeInterface();
4894bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills                if (wifiNative.getScanCapabilities(new WifiNative.ScanCapabilities())) {
49ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius                    return new HalWifiScannerImpl(context, wifiNative, looper, clock);
5094bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills                } else {
51ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius                    return new SupplicantWifiScannerImpl(context, wifiNative, looper, clock);
5294bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills                }
5394bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills            }
5494bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills        };
5594bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills
5694bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills    /**
572771787818003e53e8175036a3d09688c783f350Mitchell Wills     * A comparator that implements the sort order that is expected for scan results
582771787818003e53e8175036a3d09688c783f350Mitchell Wills     */
592771787818003e53e8175036a3d09688c783f350Mitchell Wills    protected static final Comparator<ScanResult> SCAN_RESULT_SORT_COMPARATOR =
602771787818003e53e8175036a3d09688c783f350Mitchell Wills            new Comparator<ScanResult>() {
612771787818003e53e8175036a3d09688c783f350Mitchell Wills        public int compare(ScanResult r1, ScanResult r2) {
622771787818003e53e8175036a3d09688c783f350Mitchell Wills            return r2.level - r1.level;
632771787818003e53e8175036a3d09688c783f350Mitchell Wills        }
642771787818003e53e8175036a3d09688c783f350Mitchell Wills    };
652771787818003e53e8175036a3d09688c783f350Mitchell Wills
667e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills    /**
671e806a7aac77f6f65ba299329e1fc452e0148b8dMitchell Wills     * Cleanup any ongoing operations. This may be called when the driver is unloaded.
681e806a7aac77f6f65ba299329e1fc452e0148b8dMitchell Wills     * There is no expectation that failure events are returned for ongoing operations.
691e806a7aac77f6f65ba299329e1fc452e0148b8dMitchell Wills     */
701e806a7aac77f6f65ba299329e1fc452e0148b8dMitchell Wills    public abstract void cleanup();
711e806a7aac77f6f65ba299329e1fc452e0148b8dMitchell Wills
721e806a7aac77f6f65ba299329e1fc452e0148b8dMitchell Wills    /**
737e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills     * Get the supported scan capabilities.
747e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills     *
757e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills     * @param capabilities Object that will be filled with the supported capabilities if successful
767e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills     * @return true if the scan capabilities were retrieved successfully
777e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills     */
789ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    public abstract boolean getScanCapabilities(WifiNative.ScanCapabilities capabilities);
799ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills
80f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills    /**
817e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills     * Get a ChannelHelper that can be used to perform operations on scan channels
827e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills     */
837e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills    public abstract ChannelHelper getChannelHelper();
847e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills
857e3e85327ca82a83de84b4750e793f2e3d1b3bfcMitchell Wills    /**
86f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * Start a one time scan. This method should only be called when there is no scan going on
87f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * (after a callback indicating that the previous scan succeeded/failed).
88f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * @return if the scan paramaters are valid
89f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * Note this may return true even if the parameters are not accepted by the chip because the
90f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * scan may be scheduled async.
91f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     */
92f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills    public abstract boolean startSingleScan(WifiNative.ScanSettings settings,
93f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills            WifiNative.ScanEventHandler eventHandler);
94f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills    /**
95f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * Get the scan results of the most recent single scan. This should be called immediately when
96f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * the scan success callback is receieved.
97f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     */
98f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills    public abstract WifiScanner.ScanData getLatestSingleScanResults();
99f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills
100f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills    /**
101f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * Start a background scan. Calling this method while a background scan is already in process
102f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * will interrupt the previous scan settings and replace it with the new ones.
103f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * @return if the scan paramaters are valid
104f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * Note this may return true even if the parameters are not accepted by the chip because the
105f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * scan may be scheduled async.
106f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     */
1079ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    public abstract boolean startBatchedScan(WifiNative.ScanSettings settings,
1089ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            WifiNative.ScanEventHandler eventHandler);
109f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills    /**
110f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     * Stop the currently active background scan
111f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     */
1129ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    public abstract void stopBatchedScan();
113f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills
1142332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius    /**
1152332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     * Pause the currently active background scan
1162332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     */
1179ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    public abstract void pauseBatchedScan();
1182332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius
1192332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius    /**
1202332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     * Restart the currently paused background scan
1212332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     */
1229ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    public abstract void restartBatchedScan();
123f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills
124f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills    /**
1252332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     * Get the latest cached scan results from the last scan event. This should be called
1262332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     * immediately when the scan success callback is receieved.
1272332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     */
1282332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius    public abstract WifiScanner.ScanData[] getLatestBatchedScanResults(boolean flush);
1292332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius
1302332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius    /**
1319153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * Set PNO list to start PNO background scan.
1329153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * @param settings PNO settings for this scan.
1339153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * @param eventHandler Event handler for notifying the scan results.
1349153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * @return true if success, false otherwise
1359153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     */
1366c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius    public abstract boolean setHwPnoList(WifiNative.PnoSettings settings,
1379153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius            WifiNative.PnoEventHandler eventHandler);
1389153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius
1399153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    /**
1409153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * Reset PNO list to terminate PNO background scan.
1419153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * @return true if success, false otherwise
1429153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     */
143476172f2eef83ff367fae4344eda8129ea9d16f7Roshan Pius    public abstract boolean resetHwPnoList();
1449153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius
1459153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    /**
1466c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * This returns whether HW PNO is supported or not.
147dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius     * @param isConnectedPno Whether this is connected PNO vs disconnected PNO.
1486c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * @return true if HW PNO is supported, false otherwise.
1496c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     */
1506c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius    public abstract boolean isHwPnoSupported(boolean isConnectedPno);
1516c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius
1526c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius    /**
1536c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * This returns whether a background scan should be running for HW PNO scan or not.
1549153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     * @return true if background scan needs to be started, false otherwise.
1559153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius     */
1566c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius    public abstract boolean shouldScheduleBackgroundScanForHwPno();
1579153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius
1589153bd67d51b305ffdd61355e0748e3c332c2cafRoshan Pius    /**
1592332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     * Set a new hotlist
160f2f6f79242454ff9161f5af772292fa0180436f8Mitchell Wills     */
1619ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    public abstract boolean setHotlist(WifiScanner.HotlistSettings settings,
1629ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills            WifiNative.HotlistEventHandler eventHandler);
1632332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius
1642332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius    /**
1652332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     * Reset any active hotlist
1662332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     */
1679ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills    public abstract void resetHotlist();
16894bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills
1692332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius    /**
1702332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     * Start tracking significant wifi changes
1712332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     */
17294bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills    public abstract boolean trackSignificantWifiChange(WifiScanner.WifiChangeSettings settings,
17394bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills            WifiNative.SignificantWifiChangeEventHandler handler);
1742332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius
1752332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius    /**
1762332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     * Stop tracking significant wifi changes
1772332e9ea1b691fa0ea3a340feec159f2040aa6caRoshan Pius     */
17894bd575cb4766ed0dfbaad0fc7719a9e9e85a260Mitchell Wills    public abstract void untrackSignificantWifiChange();
1799ec71f6499e0e3d6f52310a41ff4a59d2fa4f8b2Mitchell Wills}
180