WifiInjector.java revision cb46b737eaf8f59f07270e57b37bfddcfc121fdb
1/*
2 * Copyright (C) 2016 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;
18
19import android.content.Context;
20import android.net.NetworkScorerAppManager;
21import android.net.wifi.IApInterface;
22import android.net.wifi.IWifiScanner;
23import android.net.wifi.IWificond;
24import android.net.wifi.WifiConfiguration;
25import android.net.wifi.WifiInfo;
26import android.net.wifi.WifiScanner;
27import android.os.HandlerThread;
28import android.os.IBinder;
29import android.os.INetworkManagementService;
30import android.os.ServiceManager;
31import android.os.SystemProperties;
32import android.os.UserHandle;
33import android.os.UserManager;
34import android.provider.Settings;
35import android.security.KeyStore;
36import android.telephony.TelephonyManager;
37
38import com.android.internal.R;
39import com.android.server.am.BatteryStatsService;
40import com.android.server.net.DelayedDiskWrite;
41import com.android.server.net.IpConfigStore;
42import com.android.server.wifi.hotspot2.PasspointEventHandler;
43import com.android.server.wifi.hotspot2.PasspointManager;
44import com.android.server.wifi.util.WifiPermissionsUtil;
45import com.android.server.wifi.util.WifiPermissionsWrapper;
46
47/**
48 *  WiFi dependency injector. To be used for accessing various WiFi class instances and as a
49 *  handle for mock injection.
50 *
51 *  Some WiFi class instances currently depend on having a Looper from a HandlerThread that has
52 *  been started. To accommodate this, we have a two-phased approach to initialize and retrieve
53 *  an instance of the WifiInjector.
54 */
55public class WifiInjector {
56    private static final String BOOT_DEFAULT_WIFI_COUNTRY_CODE = "ro.boot.wificountrycode";
57    private static final String WIFICOND_SERVICE_NAME = "wificond";
58
59    static WifiInjector sWifiInjector = null;
60
61    private final Context mContext;
62    private final FrameworkFacade mFrameworkFacade = new FrameworkFacade();
63    private final HandlerThread mWifiServiceHandlerThread;
64    private final HandlerThread mWifiStateMachineHandlerThread;
65    private final WifiTrafficPoller mTrafficPoller;
66    private final WifiCountryCode mCountryCode;
67    private final BackupManagerProxy mBackupManagerProxy = new BackupManagerProxy();
68    private final WifiApConfigStore mWifiApConfigStore;
69    private final WifiNative mWifiNative;
70    private final WifiStateMachine mWifiStateMachine;
71    private final WifiSettingsStore mSettingsStore;
72    private final WifiCertManager mCertManager;
73    private final WifiNotificationController mNotificationController;
74    private final WifiLockManager mLockManager;
75    private final WifiController mWifiController;
76    private final Clock mClock = new Clock();
77    private final WifiMetrics mWifiMetrics = new WifiMetrics(mClock);
78    private final WifiLastResortWatchdog mWifiLastResortWatchdog;
79    private final PropertyService mPropertyService = new SystemPropertyService();
80    private final BuildProperties mBuildProperties = new SystemBuildProperties();
81    private final KeyStore mKeyStore = KeyStore.getInstance();
82    private final WifiBackupRestore mWifiBackupRestore = new WifiBackupRestore();
83    private final WifiMulticastLockManager mWifiMulticastLockManager;
84    private final WifiConfigStore mWifiConfigStore;
85    private final WifiKeyStore mWifiKeyStore;
86    private final WifiNetworkHistory mWifiNetworkHistory;
87    private final WifiSupplicantControl mWifiSupplicantControl;
88    private final IpConfigStore mIpConfigStore;
89    private final WifiConfigStoreLegacy mWifiConfigStoreLegacy;
90    private final WifiConfigManager mWifiConfigManager;
91    private final WifiNetworkSelector mWifiNetworkSelector;
92    private WifiScanner mWifiScanner;
93    private final WifiPermissionsWrapper mWifiPermissionsWrapper;
94    private final WifiPermissionsUtil mWifiPermissionsUtil;
95    private final PasspointManager mPasspointManager;
96    private final SIMAccessor mSimAccessor;
97
98    private final boolean mUseRealLogger;
99
100    public WifiInjector(Context context) {
101        if (context == null) {
102            throw new IllegalStateException(
103                    "WifiInjector should not be initialized with a null Context.");
104        }
105
106        if (sWifiInjector != null) {
107            throw new IllegalStateException(
108                    "WifiInjector was already created, use getInstance instead.");
109        }
110
111        sWifiInjector = this;
112
113        mContext = context;
114        mUseRealLogger = mContext.getResources().getBoolean(
115                R.bool.config_wifi_enable_wifi_firmware_debugging);
116
117        // Now create and start handler threads
118        mWifiServiceHandlerThread = new HandlerThread("WifiService");
119        mWifiServiceHandlerThread.start();
120        mWifiStateMachineHandlerThread = new HandlerThread("WifiStateMachine");
121        mWifiStateMachineHandlerThread.start();
122
123        // Now get instances of all the objects that depend on the HandlerThreads
124        mTrafficPoller =  new WifiTrafficPoller(mContext, mWifiServiceHandlerThread.getLooper(),
125                WifiNative.getWlanNativeInterface().getInterfaceName());
126        mCountryCode = new WifiCountryCode(WifiNative.getWlanNativeInterface(),
127                SystemProperties.get(BOOT_DEFAULT_WIFI_COUNTRY_CODE),
128                mFrameworkFacade.getStringSetting(mContext, Settings.Global.WIFI_COUNTRY_CODE),
129                mContext.getResources()
130                        .getBoolean(R.bool.config_wifi_revert_country_code_on_cellular_loss));
131        mWifiApConfigStore = new WifiApConfigStore(mContext, mBackupManagerProxy);
132        mWifiNative = WifiNative.getWlanNativeInterface();
133
134        // WifiConfigManager/Store objects and their dependencies.
135        // New config store
136        mWifiKeyStore = new WifiKeyStore(mKeyStore);
137        mWifiConfigStore = new WifiConfigStore(
138                mContext, mWifiStateMachineHandlerThread.getLooper(), mClock,
139                WifiConfigStore.createSharedFile(),
140                WifiConfigStore.createUserFile(UserHandle.USER_SYSTEM));
141        // Legacy config store
142        DelayedDiskWrite writer = new DelayedDiskWrite();
143        mWifiNetworkHistory = new WifiNetworkHistory(mContext, mWifiNative.getLocalLog(), writer);
144        mWifiSupplicantControl = new WifiSupplicantControl(
145                TelephonyManager.from(mContext), mWifiNative, mWifiNative.getLocalLog());
146        mIpConfigStore = new IpConfigStore(writer);
147        mWifiConfigStoreLegacy = new WifiConfigStoreLegacy(
148                mWifiNetworkHistory, mWifiSupplicantControl, mIpConfigStore);
149        // Config Manager
150        mWifiConfigManager = new WifiConfigManager(mContext, mFrameworkFacade, mClock,
151                UserManager.get(mContext), TelephonyManager.from(mContext),
152                mWifiKeyStore, mWifiConfigStore, mWifiConfigStoreLegacy);
153        mWifiNetworkSelector = new WifiNetworkSelector(mContext, mWifiConfigManager, mClock);
154
155        mWifiStateMachine = new WifiStateMachine(mContext, mFrameworkFacade,
156                mWifiStateMachineHandlerThread.getLooper(), UserManager.get(mContext),
157                this, mBackupManagerProxy, mCountryCode, mWifiNative);
158        mSettingsStore = new WifiSettingsStore(mContext);
159        mCertManager = new WifiCertManager(mContext);
160        mNotificationController = new WifiNotificationController(mContext,
161                mWifiServiceHandlerThread.getLooper(), mWifiStateMachine,
162                mFrameworkFacade, null, this);
163        mLockManager = new WifiLockManager(mContext, BatteryStatsService.getService());
164        mWifiController = new WifiController(mContext, mWifiStateMachine, mSettingsStore,
165                mLockManager, mWifiServiceHandlerThread.getLooper(), mFrameworkFacade);
166        mWifiLastResortWatchdog = new WifiLastResortWatchdog(mWifiController, mWifiMetrics);
167        mWifiMulticastLockManager = new WifiMulticastLockManager(mWifiStateMachine,
168                BatteryStatsService.getService());
169        mWifiPermissionsWrapper = new WifiPermissionsWrapper(mContext);
170        mWifiPermissionsUtil = new WifiPermissionsUtil(mWifiPermissionsWrapper, mContext,
171                mSettingsStore, UserManager.get(mContext), new NetworkScorerAppManager(mContext));
172        mSimAccessor = new SIMAccessor(mContext);
173        mPasspointManager = new PasspointManager(mContext, this, mSimAccessor);
174    }
175
176    /**
177     *  Obtain an instance of the WifiInjector class.
178     *
179     *  This is the generic method to get an instance of the class. The first instance should be
180     *  retrieved using the getInstanceWithContext method.
181     */
182    public static WifiInjector getInstance() {
183        if (sWifiInjector == null) {
184            throw new IllegalStateException(
185                    "Attempted to retrieve a WifiInjector instance before constructor was called.");
186        }
187        return sWifiInjector;
188    }
189
190    public WifiMetrics getWifiMetrics() {
191        return mWifiMetrics;
192    }
193
194    public BackupManagerProxy getBackupManagerProxy() {
195        return mBackupManagerProxy;
196    }
197
198    public FrameworkFacade getFrameworkFacade() {
199        return mFrameworkFacade;
200    }
201
202    public HandlerThread getWifiServiceHandlerThread() {
203        return mWifiServiceHandlerThread;
204    }
205
206    public HandlerThread getWifiStateMachineHandlerThread() {
207        return mWifiStateMachineHandlerThread;
208    }
209
210    public WifiTrafficPoller getWifiTrafficPoller() {
211        return mTrafficPoller;
212    }
213
214    public WifiCountryCode getWifiCountryCode() {
215        return mCountryCode;
216    }
217
218    public WifiApConfigStore getWifiApConfigStore() {
219        return mWifiApConfigStore;
220    }
221
222    public WifiStateMachine getWifiStateMachine() {
223        return mWifiStateMachine;
224    }
225
226    public WifiSettingsStore getWifiSettingsStore() {
227        return mSettingsStore;
228    }
229
230    public WifiCertManager getWifiCertManager() {
231        return mCertManager;
232    }
233
234    public WifiNotificationController getWifiNotificationController() {
235        return mNotificationController;
236    }
237
238    public WifiLockManager getWifiLockManager() {
239        return mLockManager;
240    }
241
242    public WifiController getWifiController() {
243        return mWifiController;
244    }
245
246    public WifiLastResortWatchdog getWifiLastResortWatchdog() {
247        return mWifiLastResortWatchdog;
248    }
249
250    public Clock getClock() {
251        return mClock;
252    }
253
254    public PropertyService getPropertyService() {
255        return mPropertyService;
256    }
257
258    public BuildProperties getBuildProperties() {
259        return mBuildProperties;
260    }
261
262    public KeyStore getKeyStore() {
263        return mKeyStore;
264    }
265
266    public WifiBackupRestore getWifiBackupRestore() {
267        return mWifiBackupRestore;
268    }
269
270    public WifiMulticastLockManager getWifiMulticastLockManager() {
271        return mWifiMulticastLockManager;
272    }
273
274    public WifiSupplicantControl getWifiSupplicantControl() {
275        return mWifiSupplicantControl;
276    }
277
278    public WifiConfigManager getWifiConfigManager() {
279        return mWifiConfigManager;
280    }
281
282    public PasspointManager getPasspointManager() {
283        return mPasspointManager;
284    }
285
286    public TelephonyManager makeTelephonyManager() {
287        // may not be available when WiFi starts
288        return (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
289    }
290
291    public IWificond makeWificond() {
292        // We depend on being able to refresh our binder in WifiStateMachine, so don't cache it.
293        IBinder binder = ServiceManager.getService(WIFICOND_SERVICE_NAME);
294        return IWificond.Stub.asInterface(binder);
295    }
296
297    /**
298     * Create a SoftApManager.
299     * @param nmService NetworkManagementService allowing SoftApManager to listen for interface
300     * changes
301     * @param listener listener for SoftApManager
302     * @param apInterface network interface to start hostapd against
303     * @param config softAp WifiConfiguration
304     * @return an instance of SoftApManager
305     */
306    public SoftApManager makeSoftApManager(INetworkManagementService nmService,
307                                           SoftApManager.Listener listener,
308                                           IApInterface apInterface,
309                                           WifiConfiguration config) {
310        return new SoftApManager(mWifiServiceHandlerThread.getLooper(),
311                                 mWifiNative, mCountryCode.getCountryCode(),
312                                 listener, apInterface, nmService,
313                                 mWifiApConfigStore, config);
314    }
315
316    /**
317     * Create a WifiLog instance.
318     * @param tag module name to include in all log messages
319     */
320    public WifiLog makeLog(String tag) {
321        return new LogcatLog(tag);
322    }
323
324    public BaseWifiDiagnostics makeWifiDiagnostics(WifiNative wifiNative) {
325        if (mUseRealLogger) {
326            return new WifiDiagnostics(
327                    mContext, this, mWifiStateMachine, wifiNative, mBuildProperties);
328        } else {
329            return new BaseWifiDiagnostics();
330        }
331    }
332
333    /**
334     * Create a PasspointEventHandler instance with the given callbacks.
335     */
336    public PasspointEventHandler makePasspointEventHandler(
337            PasspointEventHandler.Callbacks callbacks) {
338        return new PasspointEventHandler(mWifiNative, callbacks);
339    }
340
341    /**
342     * Obtain an instance of WifiScanner.
343     * If it was not already created, then obtain an instance.  Note, this must be done lazily since
344     * WifiScannerService is separate and created later.
345     */
346    public synchronized WifiScanner getWifiScanner() {
347        if (mWifiScanner == null) {
348            mWifiScanner = new WifiScanner(mContext,
349                    IWifiScanner.Stub.asInterface(ServiceManager.getService(
350                            Context.WIFI_SCANNING_SERVICE)),
351                    mWifiStateMachineHandlerThread.getLooper());
352        }
353        return mWifiScanner;
354    }
355
356    /**
357     * Obtain an instance of WifiNetworkSelector.
358     */
359    public WifiNetworkSelector getWifiNetworkSelector() {
360        return mWifiNetworkSelector;
361    }
362
363    /**
364     * Obtain a new instance of WifiConnectivityManager.
365     *
366     * Create and return a new WifiConnectivityManager.
367     * @param wifiInfo WifiInfo object for updating wifi state.
368     * @param hasConnectionRequests boolean indicating if WifiConnectivityManager to start
369     * immediately based on connection requests.
370     */
371    public WifiConnectivityManager makeWifiConnectivityManager(WifiInfo wifiInfo,
372                                                               boolean hasConnectionRequests) {
373        return new WifiConnectivityManager(mContext, mWifiStateMachine, getWifiScanner(),
374                                           mWifiConfigManager, wifiInfo, mWifiNetworkSelector,
375                                           mWifiLastResortWatchdog, mWifiMetrics,
376                                           mWifiStateMachineHandlerThread.getLooper(), mClock,
377                                           hasConnectionRequests);
378    }
379
380    public WifiPermissionsUtil getWifiPermissionsUtil() {
381        return mWifiPermissionsUtil;
382    }
383
384    public WifiPermissionsWrapper getWifiPermissionsWrapper() {
385        return mWifiPermissionsWrapper;
386    }
387}
388