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