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
17package com.android.server.wifi.scanner;
18
19import android.content.Context;
20import android.net.wifi.WifiScanner;
21import android.os.Handler;
22import android.os.Looper;
23import android.os.Message;
24import android.util.Log;
25
26import com.android.server.wifi.Clock;
27import com.android.server.wifi.WifiNative;
28
29/**
30 * WifiScanner implementation that takes advantage of the gscan HAL API
31 * The gscan API is used to perform background scans and wpa_supplicant is used for onehot scans.
32 * @see com.android.server.wifi.scanner.WifiScannerImpl for more details on each method.
33 */
34public class HalWifiScannerImpl extends WifiScannerImpl implements Handler.Callback {
35    private static final String TAG = "HalWifiScannerImpl";
36    private static final boolean DBG = false;
37
38    private final WifiNative mWifiNative;
39    private final ChannelHelper mChannelHelper;
40    private final SupplicantWifiScannerImpl mSupplicantScannerDelegate;
41    private final boolean mHalBasedPnoSupported;
42
43    public HalWifiScannerImpl(Context context, WifiNative wifiNative, Looper looper, Clock clock) {
44        mWifiNative = wifiNative;
45        mChannelHelper = new HalChannelHelper(wifiNative);
46        mSupplicantScannerDelegate =
47                new SupplicantWifiScannerImpl(context, wifiNative, mChannelHelper, looper, clock);
48
49        // We are not going to support HAL ePNO currently.
50        mHalBasedPnoSupported = false;
51    }
52
53    @Override
54    public boolean handleMessage(Message msg) {
55        Log.w(TAG, "Unknown message received: " + msg.what);
56        return true;
57    }
58
59    @Override
60    public void cleanup() {
61        mSupplicantScannerDelegate.cleanup();
62    }
63
64    @Override
65    public boolean getScanCapabilities(WifiNative.ScanCapabilities capabilities) {
66        return mWifiNative.getScanCapabilities(capabilities);
67    }
68
69    @Override
70    public ChannelHelper getChannelHelper() {
71        return mChannelHelper;
72    }
73
74    public boolean startSingleScan(WifiNative.ScanSettings settings,
75            WifiNative.ScanEventHandler eventHandler) {
76        return mSupplicantScannerDelegate.startSingleScan(settings, eventHandler);
77    }
78
79    @Override
80    public WifiScanner.ScanData getLatestSingleScanResults() {
81        return mSupplicantScannerDelegate.getLatestSingleScanResults();
82    }
83
84    @Override
85    public boolean startBatchedScan(WifiNative.ScanSettings settings,
86            WifiNative.ScanEventHandler eventHandler) {
87        if (settings == null || eventHandler == null) {
88            Log.w(TAG, "Invalid arguments for startBatched: settings=" + settings
89                    + ",eventHandler=" + eventHandler);
90            return false;
91        }
92        return mWifiNative.startScan(settings, eventHandler);
93    }
94
95    @Override
96    public void stopBatchedScan() {
97        mWifiNative.stopScan();
98    }
99
100    @Override
101    public void pauseBatchedScan() {
102        mWifiNative.pauseScan();
103    }
104
105    @Override
106    public void restartBatchedScan() {
107        mWifiNative.restartScan();
108    }
109
110    @Override
111    public WifiScanner.ScanData[] getLatestBatchedScanResults(boolean flush) {
112        return mWifiNative.getScanResults(flush);
113    }
114
115    @Override
116    public boolean setHwPnoList(WifiNative.PnoSettings settings,
117            WifiNative.PnoEventHandler eventHandler) {
118        if (mHalBasedPnoSupported) {
119            return mWifiNative.setPnoList(settings, eventHandler);
120        } else {
121            return mSupplicantScannerDelegate.setHwPnoList(settings, eventHandler);
122        }
123    }
124
125    @Override
126    public boolean resetHwPnoList() {
127        if (mHalBasedPnoSupported) {
128            return mWifiNative.resetPnoList();
129        } else {
130            return mSupplicantScannerDelegate.resetHwPnoList();
131        }
132    }
133
134    @Override
135    public boolean isHwPnoSupported(boolean isConnectedPno) {
136        if (mHalBasedPnoSupported) {
137            return true;
138        } else {
139            return mSupplicantScannerDelegate.isHwPnoSupported(isConnectedPno);
140        }
141    }
142
143    @Override
144    public boolean shouldScheduleBackgroundScanForHwPno() {
145        if (mHalBasedPnoSupported) {
146            return true;
147        } else {
148            return mSupplicantScannerDelegate.shouldScheduleBackgroundScanForHwPno();
149        }
150    }
151
152    @Override
153    public boolean setHotlist(WifiScanner.HotlistSettings settings,
154            WifiNative.HotlistEventHandler eventHandler) {
155        return mWifiNative.setHotlist(settings, eventHandler);
156    }
157
158    @Override
159    public void resetHotlist() {
160        mWifiNative.resetHotlist();
161    }
162
163    @Override
164    public boolean trackSignificantWifiChange(WifiScanner.WifiChangeSettings settings,
165            WifiNative.SignificantWifiChangeEventHandler handler) {
166        return mWifiNative.trackSignificantWifiChange(settings, handler);
167    }
168
169    @Override
170    public void untrackSignificantWifiChange() {
171        mWifiNative.untrackSignificantWifiChange();
172    }
173}
174