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