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