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