WifiInjector.java revision ae791278c9032a8b10cf818b98b571c0396add4a
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.PasspointManager;
43import com.android.server.wifi.hotspot2.PasspointObjectFactory;
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, mWifiNative, mWifiKeyStore, mClock,
174                mSimAccessor, new PasspointObjectFactory());
175    }
176
177    /**
178     *  Obtain an instance of the WifiInjector class.
179     *
180     *  This is the generic method to get an instance of the class. The first instance should be
181     *  retrieved using the getInstanceWithContext method.
182     */
183    public static WifiInjector getInstance() {
184        if (sWifiInjector == null) {
185            throw new IllegalStateException(
186                    "Attempted to retrieve a WifiInjector instance before constructor was called.");
187        }
188        return sWifiInjector;
189    }
190
191    public WifiMetrics getWifiMetrics() {
192        return mWifiMetrics;
193    }
194
195    public BackupManagerProxy getBackupManagerProxy() {
196        return mBackupManagerProxy;
197    }
198
199    public FrameworkFacade getFrameworkFacade() {
200        return mFrameworkFacade;
201    }
202
203    public HandlerThread getWifiServiceHandlerThread() {
204        return mWifiServiceHandlerThread;
205    }
206
207    public HandlerThread getWifiStateMachineHandlerThread() {
208        return mWifiStateMachineHandlerThread;
209    }
210
211    public WifiTrafficPoller getWifiTrafficPoller() {
212        return mTrafficPoller;
213    }
214
215    public WifiCountryCode getWifiCountryCode() {
216        return mCountryCode;
217    }
218
219    public WifiApConfigStore getWifiApConfigStore() {
220        return mWifiApConfigStore;
221    }
222
223    public WifiStateMachine getWifiStateMachine() {
224        return mWifiStateMachine;
225    }
226
227    public WifiSettingsStore getWifiSettingsStore() {
228        return mSettingsStore;
229    }
230
231    public WifiCertManager getWifiCertManager() {
232        return mCertManager;
233    }
234
235    public WifiNotificationController getWifiNotificationController() {
236        return mNotificationController;
237    }
238
239    public WifiLockManager getWifiLockManager() {
240        return mLockManager;
241    }
242
243    public WifiController getWifiController() {
244        return mWifiController;
245    }
246
247    public WifiLastResortWatchdog getWifiLastResortWatchdog() {
248        return mWifiLastResortWatchdog;
249    }
250
251    public Clock getClock() {
252        return mClock;
253    }
254
255    public PropertyService getPropertyService() {
256        return mPropertyService;
257    }
258
259    public BuildProperties getBuildProperties() {
260        return mBuildProperties;
261    }
262
263    public KeyStore getKeyStore() {
264        return mKeyStore;
265    }
266
267    public WifiBackupRestore getWifiBackupRestore() {
268        return mWifiBackupRestore;
269    }
270
271    public WifiMulticastLockManager getWifiMulticastLockManager() {
272        return mWifiMulticastLockManager;
273    }
274
275    public WifiSupplicantControl getWifiSupplicantControl() {
276        return mWifiSupplicantControl;
277    }
278
279    public WifiConfigManager getWifiConfigManager() {
280        return mWifiConfigManager;
281    }
282
283    public PasspointManager getPasspointManager() {
284        return mPasspointManager;
285    }
286
287    public TelephonyManager makeTelephonyManager() {
288        // may not be available when WiFi starts
289        return (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
290    }
291
292    public IWificond makeWificond() {
293        // We depend on being able to refresh our binder in WifiStateMachine, so don't cache it.
294        IBinder binder = ServiceManager.getService(WIFICOND_SERVICE_NAME);
295        return IWificond.Stub.asInterface(binder);
296    }
297
298    /**
299     * Create a SoftApManager.
300     * @param nmService NetworkManagementService allowing SoftApManager to listen for interface
301     * changes
302     * @param listener listener for SoftApManager
303     * @param apInterface network interface to start hostapd against
304     * @param config softAp WifiConfiguration
305     * @return an instance of SoftApManager
306     */
307    public SoftApManager makeSoftApManager(INetworkManagementService nmService,
308                                           SoftApManager.Listener listener,
309                                           IApInterface apInterface,
310                                           WifiConfiguration config) {
311        return new SoftApManager(mWifiServiceHandlerThread.getLooper(),
312                                 mWifiNative, mCountryCode.getCountryCode(),
313                                 listener, apInterface, nmService,
314                                 mWifiApConfigStore, config);
315    }
316
317    /**
318     * Create a WifiLog instance.
319     * @param tag module name to include in all log messages
320     */
321    public WifiLog makeLog(String tag) {
322        return new LogcatLog(tag);
323    }
324
325    public BaseWifiDiagnostics makeWifiDiagnostics(WifiNative wifiNative) {
326        if (mUseRealLogger) {
327            return new WifiDiagnostics(
328                    mContext, this, mWifiStateMachine, wifiNative, mBuildProperties);
329        } else {
330            return new BaseWifiDiagnostics();
331        }
332    }
333
334    /**
335     * Obtain an instance of WifiScanner.
336     * If it was not already created, then obtain an instance.  Note, this must be done lazily since
337     * WifiScannerService is separate and created later.
338     */
339    public synchronized WifiScanner getWifiScanner() {
340        if (mWifiScanner == null) {
341            mWifiScanner = new WifiScanner(mContext,
342                    IWifiScanner.Stub.asInterface(ServiceManager.getService(
343                            Context.WIFI_SCANNING_SERVICE)),
344                    mWifiStateMachineHandlerThread.getLooper());
345        }
346        return mWifiScanner;
347    }
348
349    /**
350     * Obtain an instance of WifiNetworkSelector.
351     */
352    public WifiNetworkSelector getWifiNetworkSelector() {
353        return mWifiNetworkSelector;
354    }
355
356    /**
357     * Obtain a new instance of WifiConnectivityManager.
358     *
359     * Create and return a new WifiConnectivityManager.
360     * @param wifiInfo WifiInfo object for updating wifi state.
361     * @param hasConnectionRequests boolean indicating if WifiConnectivityManager to start
362     * immediately based on connection requests.
363     */
364    public WifiConnectivityManager makeWifiConnectivityManager(WifiInfo wifiInfo,
365                                                               boolean hasConnectionRequests) {
366        return new WifiConnectivityManager(mContext, mWifiStateMachine, getWifiScanner(),
367                                           mWifiConfigManager, wifiInfo, mWifiNetworkSelector,
368                                           mWifiLastResortWatchdog, mWifiMetrics,
369                                           mWifiStateMachineHandlerThread.getLooper(), mClock,
370                                           hasConnectionRequests);
371    }
372
373    public WifiPermissionsUtil getWifiPermissionsUtil() {
374        return mWifiPermissionsUtil;
375    }
376
377    public WifiPermissionsWrapper getWifiPermissionsWrapper() {
378        return mWifiPermissionsWrapper;
379    }
380}
381