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