Home | History | Annotate | Download | only in scanner
      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 
     17 package com.android.server.wifi.scanner;
     18 
     19 import android.content.Context;
     20 import android.net.wifi.ScanResult;
     21 import android.net.wifi.WifiScanner;
     22 import android.os.Looper;
     23 
     24 import com.android.server.wifi.Clock;
     25 import com.android.server.wifi.WifiInjector;
     26 import com.android.server.wifi.WifiMonitor;
     27 import com.android.server.wifi.WifiNative;
     28 
     29 import java.util.Comparator;
     30 
     31 /**
     32  * Defines the interface to the Wifi hardware required for the WifiScanner API
     33  */
     34 public abstract class WifiScannerImpl {
     35 
     36     /**
     37      * A factory that create a {@link com.android.server.wifi.scanner.WifiScannerImpl}
     38      */
     39     public static interface WifiScannerImplFactory {
     40         WifiScannerImpl create(Context context, Looper looper, Clock clock);
     41     }
     42 
     43     /**
     44      * Factory that create the implementation that is most appropriate for the system.
     45      * This factory should only ever be used once.
     46      */
     47     public static final WifiScannerImplFactory DEFAULT_FACTORY = new WifiScannerImplFactory() {
     48             public WifiScannerImpl create(Context context, Looper looper, Clock clock) {
     49                 WifiNative wifiNative = WifiInjector.getInstance().getWifiNative();
     50                 WifiMonitor wifiMonitor = WifiInjector.getInstance().getWifiMonitor();
     51                 if (wifiNative.getBgScanCapabilities(new WifiNative.ScanCapabilities())) {
     52                     return new HalWifiScannerImpl(context, wifiNative, wifiMonitor, looper, clock);
     53                 } else {
     54                     return new WificondScannerImpl(context, wifiNative, wifiMonitor, looper,
     55                             clock);
     56                 }
     57             }
     58         };
     59 
     60     /**
     61      * A comparator that implements the sort order that is expected for scan results
     62      */
     63     protected static final Comparator<ScanResult> SCAN_RESULT_SORT_COMPARATOR =
     64             new Comparator<ScanResult>() {
     65         public int compare(ScanResult r1, ScanResult r2) {
     66             return r2.level - r1.level;
     67         }
     68     };
     69 
     70     /**
     71      * Cleanup any ongoing operations. This may be called when the driver is unloaded.
     72      * There is no expectation that failure events are returned for ongoing operations.
     73      */
     74     public abstract void cleanup();
     75 
     76     /**
     77      * Get the supported scan capabilities.
     78      *
     79      * @param capabilities Object that will be filled with the supported capabilities if successful
     80      * @return true if the scan capabilities were retrieved successfully
     81      */
     82     public abstract boolean getScanCapabilities(WifiNative.ScanCapabilities capabilities);
     83 
     84     /**
     85      * Get a ChannelHelper that can be used to perform operations on scan channels
     86      */
     87     public abstract ChannelHelper getChannelHelper();
     88 
     89     /**
     90      * Start a one time scan. This method should only be called when there is no scan going on
     91      * (after a callback indicating that the previous scan succeeded/failed).
     92      * @return if the scan paramaters are valid
     93      * Note this may return true even if the parameters are not accepted by the chip because the
     94      * scan may be scheduled async.
     95      */
     96     public abstract boolean startSingleScan(WifiNative.ScanSettings settings,
     97             WifiNative.ScanEventHandler eventHandler);
     98     /**
     99      * Get the scan results of the most recent single scan. This should be called immediately when
    100      * the scan success callback is receieved.
    101      */
    102     public abstract WifiScanner.ScanData getLatestSingleScanResults();
    103 
    104     /**
    105      * Start a background scan. Calling this method while a background scan is already in process
    106      * will interrupt the previous scan settings and replace it with the new ones.
    107      * @return if the scan paramaters are valid
    108      * Note this may return true even if the parameters are not accepted by the chip because the
    109      * scan may be scheduled async.
    110      */
    111     public abstract boolean startBatchedScan(WifiNative.ScanSettings settings,
    112             WifiNative.ScanEventHandler eventHandler);
    113     /**
    114      * Stop the currently active background scan
    115      */
    116     public abstract void stopBatchedScan();
    117 
    118     /**
    119      * Pause the currently active background scan
    120      */
    121     public abstract void pauseBatchedScan();
    122 
    123     /**
    124      * Restart the currently paused background scan
    125      */
    126     public abstract void restartBatchedScan();
    127 
    128     /**
    129      * Get the latest cached scan results from the last scan event. This should be called
    130      * immediately when the scan success callback is receieved.
    131      */
    132     public abstract WifiScanner.ScanData[] getLatestBatchedScanResults(boolean flush);
    133 
    134     /**
    135      * Set PNO list to start PNO background scan.
    136      * @param settings PNO settings for this scan.
    137      * @param eventHandler Event handler for notifying the scan results.
    138      * @return true if success, false otherwise
    139      */
    140     public abstract boolean setHwPnoList(WifiNative.PnoSettings settings,
    141             WifiNative.PnoEventHandler eventHandler);
    142 
    143     /**
    144      * Reset PNO list to terminate PNO background scan.
    145      * @return true if success, false otherwise
    146      */
    147     public abstract boolean resetHwPnoList();
    148 
    149     /**
    150      * This returns whether HW PNO is supported or not.
    151      * @param isConnectedPno Whether this is connected PNO vs disconnected PNO.
    152      * @return true if HW PNO is supported, false otherwise.
    153      */
    154     public abstract boolean isHwPnoSupported(boolean isConnectedPno);
    155 
    156     /**
    157      * This returns whether a background scan should be running for HW PNO scan or not.
    158      * @return true if background scan needs to be started, false otherwise.
    159      */
    160     public abstract boolean shouldScheduleBackgroundScanForHwPno();
    161 }
    162