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