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