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