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