WifiInjector.java revision 17c2a7b30e5680b11fc0073ce322ee7bc14ef2c5
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.app.ActivityManager;
20import android.content.Context;
21import android.net.NetworkScoreManager;
22import android.net.wifi.IApInterface;
23import android.net.wifi.IWifiScanner;
24import android.net.wifi.IWificond;
25import android.net.wifi.WifiConfiguration;
26import android.net.wifi.WifiInfo;
27import android.net.wifi.WifiScanner;
28import android.os.BatteryStats;
29import android.os.HandlerThread;
30import android.os.IBinder;
31import android.os.INetworkManagementService;
32import android.os.Looper;
33import android.os.ServiceManager;
34import android.os.SystemProperties;
35import android.os.UserManager;
36import android.security.KeyStore;
37import android.telephony.TelephonyManager;
38import android.util.LocalLog;
39
40import com.android.internal.R;
41import com.android.internal.app.IBatteryStats;
42import com.android.server.am.BatteryStatsService;
43import com.android.server.net.DelayedDiskWrite;
44import com.android.server.net.IpConfigStore;
45import com.android.server.wifi.hotspot2.LegacyPasspointConfigParser;
46import com.android.server.wifi.hotspot2.PasspointManager;
47import com.android.server.wifi.hotspot2.PasspointNetworkEvaluator;
48import com.android.server.wifi.hotspot2.PasspointObjectFactory;
49import com.android.server.wifi.p2p.SupplicantP2pIfaceHal;
50import com.android.server.wifi.p2p.WifiP2pMonitor;
51import com.android.server.wifi.p2p.WifiP2pNative;
52import com.android.server.wifi.util.WifiPermissionsUtil;
53import com.android.server.wifi.util.WifiPermissionsWrapper;
54
55/**
56 *  WiFi dependency injector. To be used for accessing various WiFi class instances and as a
57 *  handle for mock injection.
58 *
59 *  Some WiFi class instances currently depend on having a Looper from a HandlerThread that has
60 *  been started. To accommodate this, we have a two-phased approach to initialize and retrieve
61 *  an instance of the WifiInjector.
62 */
63public class WifiInjector {
64    private static final String BOOT_DEFAULT_WIFI_COUNTRY_CODE = "ro.boot.wificountrycode";
65    private static final String WIFICOND_SERVICE_NAME = "wificond";
66
67    static WifiInjector sWifiInjector = null;
68
69    private final Context mContext;
70    private final FrameworkFacade mFrameworkFacade = new FrameworkFacade();
71    private final HandlerThread mWifiServiceHandlerThread;
72    private final HandlerThread mWifiStateMachineHandlerThread;
73    private final WifiTrafficPoller mTrafficPoller;
74    private final WifiCountryCode mCountryCode;
75    private final BackupManagerProxy mBackupManagerProxy = new BackupManagerProxy();
76    private final WifiApConfigStore mWifiApConfigStore;
77    private final WifiNative mWifiNative;
78    private final WifiMonitor mWifiMonitor;
79    private final WifiP2pNative mWifiP2pNative;
80    private final WifiP2pMonitor mWifiP2pMonitor;
81    private final SupplicantStaIfaceHal mSupplicantStaIfaceHal;
82    private final SupplicantP2pIfaceHal mSupplicantP2pIfaceHal;
83    private final WifiVendorHal mWifiVendorHal;
84    private final WifiStateMachine mWifiStateMachine;
85    private final WifiSettingsStore mSettingsStore;
86    private final WifiCertManager mCertManager;
87    private final WifiLockManager mLockManager;
88    private final WifiController mWifiController;
89    private final WificondControl mWificondControl;
90    private final Clock mClock = new Clock();
91    private final WifiMetrics mWifiMetrics = new WifiMetrics(mClock);
92    private final WifiLastResortWatchdog mWifiLastResortWatchdog;
93    private final PropertyService mPropertyService = new SystemPropertyService();
94    private final BuildProperties mBuildProperties = new SystemBuildProperties();
95    private final KeyStore mKeyStore = KeyStore.getInstance();
96    private final WifiBackupRestore mWifiBackupRestore = new WifiBackupRestore();
97    private final WifiMulticastLockManager mWifiMulticastLockManager;
98    private final WifiConfigStore mWifiConfigStore;
99    private final WifiKeyStore mWifiKeyStore;
100    private final WifiNetworkHistory mWifiNetworkHistory;
101    private final IpConfigStore mIpConfigStore;
102    private final WifiConfigStoreLegacy mWifiConfigStoreLegacy;
103    private final WifiConfigManager mWifiConfigManager;
104    private final WifiConnectivityHelper mWifiConnectivityHelper;
105    private final LocalLog mConnectivityLocalLog;
106    private final WifiNetworkSelector mWifiNetworkSelector;
107    private final SavedNetworkEvaluator mSavedNetworkEvaluator;
108    private final PasspointNetworkEvaluator mPasspointNetworkEvaluator;
109    private final RecommendedNetworkEvaluator mRecommendedNetworkEvaluator;
110    private final NetworkScoreManager mNetworkScoreManager;
111    private WifiScanner mWifiScanner;
112    private final WifiPermissionsWrapper mWifiPermissionsWrapper;
113    private final WifiPermissionsUtil mWifiPermissionsUtil;
114    private final PasspointManager mPasspointManager;
115    private final SIMAccessor mSimAccessor;
116    private HandlerThread mWifiAwareHandlerThread;
117    private HalDeviceManager mHalDeviceManager;
118    private final IBatteryStats mBatteryStats;
119    private final WifiStateTracker mWifiStateTracker;
120    private final Runtime mJavaRuntime;
121
122    private final boolean mUseRealLogger;
123
124    public WifiInjector(Context context) {
125        if (context == null) {
126            throw new IllegalStateException(
127                    "WifiInjector should not be initialized with a null Context.");
128        }
129
130        if (sWifiInjector != null) {
131            throw new IllegalStateException(
132                    "WifiInjector was already created, use getInstance instead.");
133        }
134
135        sWifiInjector = this;
136
137        mContext = context;
138        mUseRealLogger = mContext.getResources().getBoolean(
139                R.bool.config_wifi_enable_wifi_firmware_debugging);
140        mSettingsStore = new WifiSettingsStore(mContext);
141        mWifiPermissionsWrapper = new WifiPermissionsWrapper(mContext);
142        mNetworkScoreManager = mContext.getSystemService(NetworkScoreManager.class);
143        mWifiPermissionsUtil = new WifiPermissionsUtil(mWifiPermissionsWrapper, mContext,
144                mSettingsStore, UserManager.get(mContext), mNetworkScoreManager, this);
145        mBatteryStats = IBatteryStats.Stub.asInterface(mFrameworkFacade.getService(
146                BatteryStats.SERVICE_NAME));
147        mWifiStateTracker = new WifiStateTracker(mBatteryStats);
148        // Now create and start handler threads
149        mWifiServiceHandlerThread = new HandlerThread("WifiService");
150        mWifiServiceHandlerThread.start();
151        mWifiStateMachineHandlerThread = new HandlerThread("WifiStateMachine");
152        mWifiStateMachineHandlerThread.start();
153        Looper wifiStateMachineLooper = mWifiStateMachineHandlerThread.getLooper();
154
155        // Modules interacting with Native.
156        mWifiMonitor = new WifiMonitor(this);
157        mHalDeviceManager = new HalDeviceManager();
158        mWifiVendorHal =
159                new WifiVendorHal(mHalDeviceManager, mWifiStateMachineHandlerThread.getLooper());
160        mSupplicantStaIfaceHal = new SupplicantStaIfaceHal(mContext, mWifiMonitor);
161        mWificondControl = new WificondControl(this, mWifiMonitor);
162        mWifiNative = new WifiNative(SystemProperties.get("wifi.interface", "wlan0"),
163                mWifiVendorHal, mSupplicantStaIfaceHal, mWificondControl);
164        mWifiP2pMonitor = new WifiP2pMonitor(this);
165        mSupplicantP2pIfaceHal = new SupplicantP2pIfaceHal(mWifiP2pMonitor);
166        mWifiP2pNative = new WifiP2pNative(SystemProperties.get("wifi.direct.interface", "p2p0"),
167                mSupplicantP2pIfaceHal);
168
169        // Now get instances of all the objects that depend on the HandlerThreads
170        mTrafficPoller =  new WifiTrafficPoller(mContext, mWifiServiceHandlerThread.getLooper(),
171                mWifiNative.getInterfaceName());
172        mCountryCode = new WifiCountryCode(mWifiNative,
173                SystemProperties.get(BOOT_DEFAULT_WIFI_COUNTRY_CODE),
174                mContext.getResources()
175                        .getBoolean(R.bool.config_wifi_revert_country_code_on_cellular_loss));
176        mWifiApConfigStore = new WifiApConfigStore(mContext, mBackupManagerProxy);
177
178        // WifiConfigManager/Store objects and their dependencies.
179        // New config store
180        mWifiKeyStore = new WifiKeyStore(mKeyStore);
181        mWifiConfigStore = new WifiConfigStore(
182                mContext, wifiStateMachineLooper, mClock,
183                WifiConfigStore.createSharedFile());
184        // Legacy config store
185        DelayedDiskWrite writer = new DelayedDiskWrite();
186        mWifiNetworkHistory = new WifiNetworkHistory(mContext, writer);
187        mIpConfigStore = new IpConfigStore(writer);
188        mWifiConfigStoreLegacy = new WifiConfigStoreLegacy(
189                mWifiNetworkHistory, mWifiNative, mIpConfigStore,
190                new LegacyPasspointConfigParser());
191        // Config Manager
192        mWifiConfigManager = new WifiConfigManager(mContext, mClock,
193                UserManager.get(mContext), TelephonyManager.from(mContext),
194                mWifiKeyStore, mWifiConfigStore, mWifiConfigStoreLegacy, mWifiPermissionsUtil,
195                mWifiPermissionsWrapper, new NetworkListStoreData(),
196                new DeletedEphemeralSsidsStoreData());
197        mWifiConnectivityHelper = new WifiConnectivityHelper(mWifiNative);
198        mConnectivityLocalLog = new LocalLog(ActivityManager.isLowRamDeviceStatic() ? 256 : 512);
199        mWifiNetworkSelector = new WifiNetworkSelector(mContext, mWifiConfigManager, mClock,
200                mConnectivityLocalLog);
201        mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext,
202                mWifiConfigManager, mClock, mConnectivityLocalLog, wifiStateMachineLooper,
203                mFrameworkFacade);
204        mRecommendedNetworkEvaluator = new RecommendedNetworkEvaluator(context,
205                context.getContentResolver(), wifiStateMachineLooper, mFrameworkFacade,
206                mNetworkScoreManager, mWifiConfigManager, mConnectivityLocalLog);
207        mSimAccessor = new SIMAccessor(mContext);
208        mPasspointManager = new PasspointManager(mContext, mWifiNative, mWifiKeyStore, mClock,
209                mSimAccessor, new PasspointObjectFactory(), mWifiConfigManager, mWifiConfigStore);
210        mPasspointNetworkEvaluator = new PasspointNetworkEvaluator(
211                mPasspointManager, mWifiConfigManager, mConnectivityLocalLog);
212        // mWifiStateMachine has an implicit dependency on mJavaRuntime due to WifiDiagnostics.
213        mJavaRuntime = Runtime.getRuntime();
214        mWifiStateMachine = new WifiStateMachine(mContext, mFrameworkFacade,
215                wifiStateMachineLooper, UserManager.get(mContext),
216                this, mBackupManagerProxy, mCountryCode, mWifiNative);
217        mCertManager = new WifiCertManager(mContext);
218        mLockManager = new WifiLockManager(mContext, BatteryStatsService.getService());
219        mWifiController = new WifiController(mContext, mWifiStateMachine, mSettingsStore,
220                mLockManager, mWifiServiceHandlerThread.getLooper(), mFrameworkFacade);
221        mWifiLastResortWatchdog = new WifiLastResortWatchdog(mWifiController, mWifiMetrics);
222        mWifiMulticastLockManager = new WifiMulticastLockManager(mWifiStateMachine,
223                BatteryStatsService.getService());
224    }
225
226    /**
227     *  Obtain an instance of the WifiInjector class.
228     *
229     *  This is the generic method to get an instance of the class. The first instance should be
230     *  retrieved using the getInstanceWithContext method.
231     */
232    public static WifiInjector getInstance() {
233        if (sWifiInjector == null) {
234            throw new IllegalStateException(
235                    "Attempted to retrieve a WifiInjector instance before constructor was called.");
236        }
237        return sWifiInjector;
238    }
239
240    public WifiMetrics getWifiMetrics() {
241        return mWifiMetrics;
242    }
243
244    public SupplicantStaIfaceHal getSupplicantStaIfaceHal() {
245        return mSupplicantStaIfaceHal;
246    }
247
248    public BackupManagerProxy getBackupManagerProxy() {
249        return mBackupManagerProxy;
250    }
251
252    public FrameworkFacade getFrameworkFacade() {
253        return mFrameworkFacade;
254    }
255
256    public HandlerThread getWifiServiceHandlerThread() {
257        return mWifiServiceHandlerThread;
258    }
259
260    public HandlerThread getWifiStateMachineHandlerThread() {
261        return mWifiStateMachineHandlerThread;
262    }
263
264    public WifiTrafficPoller getWifiTrafficPoller() {
265        return mTrafficPoller;
266    }
267
268    public WifiCountryCode getWifiCountryCode() {
269        return mCountryCode;
270    }
271
272    public WifiApConfigStore getWifiApConfigStore() {
273        return mWifiApConfigStore;
274    }
275
276    public WifiStateMachine getWifiStateMachine() {
277        return mWifiStateMachine;
278    }
279
280    public WifiSettingsStore getWifiSettingsStore() {
281        return mSettingsStore;
282    }
283
284    public WifiCertManager getWifiCertManager() {
285        return mCertManager;
286    }
287
288    public WifiLockManager getWifiLockManager() {
289        return mLockManager;
290    }
291
292    public WifiController getWifiController() {
293        return mWifiController;
294    }
295
296    public WifiLastResortWatchdog getWifiLastResortWatchdog() {
297        return mWifiLastResortWatchdog;
298    }
299
300    public Clock getClock() {
301        return mClock;
302    }
303
304    public PropertyService getPropertyService() {
305        return mPropertyService;
306    }
307
308    public BuildProperties getBuildProperties() {
309        return mBuildProperties;
310    }
311
312    public KeyStore getKeyStore() {
313        return mKeyStore;
314    }
315
316    public WifiBackupRestore getWifiBackupRestore() {
317        return mWifiBackupRestore;
318    }
319
320    public WifiMulticastLockManager getWifiMulticastLockManager() {
321        return mWifiMulticastLockManager;
322    }
323
324    public WifiConfigManager getWifiConfigManager() {
325        return mWifiConfigManager;
326    }
327
328    public PasspointManager getPasspointManager() {
329        return mPasspointManager;
330    }
331
332    public TelephonyManager makeTelephonyManager() {
333        // may not be available when WiFi starts
334        return (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
335    }
336
337    public WifiStateTracker getWifiStateTracker() {
338        return mWifiStateTracker;
339    }
340
341    public IWificond makeWificond() {
342        // We depend on being able to refresh our binder in WifiStateMachine, so don't cache it.
343        IBinder binder = ServiceManager.getService(WIFICOND_SERVICE_NAME);
344        return IWificond.Stub.asInterface(binder);
345    }
346
347    /**
348     * Create a SoftApManager.
349     * @param nmService NetworkManagementService allowing SoftApManager to listen for interface
350     * changes
351     * @param listener listener for SoftApManager
352     * @param apInterface network interface to start hostapd against
353     * @param config softAp WifiConfiguration
354     * @return an instance of SoftApManager
355     */
356    public SoftApManager makeSoftApManager(INetworkManagementService nmService,
357                                           SoftApManager.Listener listener,
358                                           IApInterface apInterface,
359                                           WifiConfiguration config) {
360        return new SoftApManager(mWifiServiceHandlerThread.getLooper(),
361                                 mWifiNative, mCountryCode.getCountryCode(),
362                                 listener, apInterface, nmService,
363                                 mWifiApConfigStore, config, mWifiMetrics);
364    }
365
366    /**
367     * Create a WifiLog instance.
368     * @param tag module name to include in all log messages
369     */
370    public WifiLog makeLog(String tag) {
371        return new LogcatLog(tag);
372    }
373
374    public BaseWifiDiagnostics makeWifiDiagnostics(WifiNative wifiNative) {
375        if (mUseRealLogger) {
376            return new WifiDiagnostics(
377                    mContext, this, mWifiStateMachine, wifiNative, mBuildProperties,
378                    new LastMileLogger(this));
379        } else {
380            return new BaseWifiDiagnostics(wifiNative);
381        }
382    }
383
384    /**
385     * Obtain an instance of WifiScanner.
386     * If it was not already created, then obtain an instance.  Note, this must be done lazily since
387     * WifiScannerService is separate and created later.
388     */
389    public synchronized WifiScanner getWifiScanner() {
390        if (mWifiScanner == null) {
391            mWifiScanner = new WifiScanner(mContext,
392                    IWifiScanner.Stub.asInterface(ServiceManager.getService(
393                            Context.WIFI_SCANNING_SERVICE)),
394                    mWifiStateMachineHandlerThread.getLooper());
395        }
396        return mWifiScanner;
397    }
398
399    /**
400     * Obtain a new instance of WifiConnectivityManager.
401     *
402     * Create and return a new WifiConnectivityManager.
403     * @param wifiInfo WifiInfo object for updating wifi state.
404     * @param hasConnectionRequests boolean indicating if WifiConnectivityManager to start
405     * immediately based on connection requests.
406     */
407    public WifiConnectivityManager makeWifiConnectivityManager(WifiInfo wifiInfo,
408                                                               boolean hasConnectionRequests) {
409        return new WifiConnectivityManager(mContext, mWifiStateMachine, getWifiScanner(),
410                mWifiConfigManager, wifiInfo, mWifiNetworkSelector, mWifiConnectivityHelper,
411                mWifiLastResortWatchdog, mWifiMetrics, mWifiStateMachineHandlerThread.getLooper(),
412                mClock, mConnectivityLocalLog, hasConnectionRequests, mFrameworkFacade,
413                mSavedNetworkEvaluator, mRecommendedNetworkEvaluator, mPasspointNetworkEvaluator);
414    }
415
416    public WifiPermissionsUtil getWifiPermissionsUtil() {
417        return mWifiPermissionsUtil;
418    }
419
420    public WifiPermissionsWrapper getWifiPermissionsWrapper() {
421        return mWifiPermissionsWrapper;
422    }
423
424    /**
425     * Returns a singleton instance of a HandlerThread for injection. Uses lazy initialization.
426     *
427     * TODO: share worker thread with other Wi-Fi handlers (b/27924886)
428     */
429    public HandlerThread getWifiAwareHandlerThread() {
430        if (mWifiAwareHandlerThread == null) { // lazy initialization
431            mWifiAwareHandlerThread = new HandlerThread("wifiAwareService");
432            mWifiAwareHandlerThread.start();
433        }
434        return mWifiAwareHandlerThread;
435    }
436
437    /**
438     * Returns a single instance of HalDeviceManager for injection.
439     */
440    public HalDeviceManager getHalDeviceManager() {
441        return mHalDeviceManager;
442    }
443
444    public Runtime getJavaRuntime() {
445        return mJavaRuntime;
446    }
447
448    public WifiNative getWifiNative() {
449        return mWifiNative;
450    }
451
452    public WifiMonitor getWifiMonitor() {
453        return mWifiMonitor;
454    }
455
456    public WifiP2pNative getWifiP2pNative() {
457        return mWifiP2pNative;
458    }
459
460    public WifiP2pMonitor getWifiP2pMonitor() {
461        return mWifiP2pMonitor;
462    }
463}
464