WifiServiceImpl.java revision 61312e14a088a9487d4db64f08285162476e870f
1/* 2 * Copyright (C) 2010 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 static com.android.server.wifi.WifiController.CMD_AIRPLANE_TOGGLED; 20import static com.android.server.wifi.WifiController.CMD_BATTERY_CHANGED; 21import static com.android.server.wifi.WifiController.CMD_EMERGENCY_CALL_STATE_CHANGED; 22import static com.android.server.wifi.WifiController.CMD_EMERGENCY_MODE_CHANGED; 23import static com.android.server.wifi.WifiController.CMD_LOCKS_CHANGED; 24import static com.android.server.wifi.WifiController.CMD_SCAN_ALWAYS_MODE_CHANGED; 25import static com.android.server.wifi.WifiController.CMD_SCREEN_OFF; 26import static com.android.server.wifi.WifiController.CMD_SCREEN_ON; 27import static com.android.server.wifi.WifiController.CMD_SET_AP; 28import static com.android.server.wifi.WifiController.CMD_USER_PRESENT; 29import static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED; 30 31import android.Manifest; 32import android.app.ActivityManager; 33import android.app.AppOpsManager; 34import android.bluetooth.BluetoothAdapter; 35import android.content.BroadcastReceiver; 36import android.content.Context; 37import android.content.Intent; 38import android.content.IntentFilter; 39import android.content.pm.PackageManager; 40import android.content.pm.UserInfo; 41import android.database.ContentObserver; 42import android.net.ConnectivityManager; 43import android.net.DhcpInfo; 44import android.net.DhcpResults; 45import android.net.Network; 46import android.net.NetworkScorerAppManager; 47import android.net.NetworkUtils; 48import android.net.Uri; 49import android.net.ip.IpManager; 50import android.net.wifi.IWifiManager; 51import android.net.wifi.PasspointManagementObjectDefinition; 52import android.net.wifi.ScanResult; 53import android.net.wifi.ScanSettings; 54import android.net.wifi.WifiActivityEnergyInfo; 55import android.net.wifi.WifiConfiguration; 56import android.net.wifi.WifiConnectionStatistics; 57import android.net.wifi.WifiEnterpriseConfig; 58import android.net.wifi.WifiInfo; 59import android.net.wifi.WifiLinkLayerStats; 60import android.net.wifi.WifiManager; 61import android.os.AsyncTask; 62import android.os.BatteryStats; 63import android.os.Binder; 64import android.os.Build; 65import android.os.Bundle; 66import android.os.Handler; 67import android.os.HandlerThread; 68import android.os.IBinder; 69import android.os.Looper; 70import android.os.Message; 71import android.os.Messenger; 72import android.os.PowerManager; 73import android.os.RemoteException; 74import android.os.ResultReceiver; 75import android.os.SystemClock; 76import android.os.UserHandle; 77import android.os.UserManager; 78import android.os.WorkSource; 79import android.provider.Settings; 80import android.text.TextUtils; 81import android.util.Log; 82import android.util.Slog; 83 84import com.android.internal.telephony.IccCardConstants; 85import com.android.internal.telephony.PhoneConstants; 86import com.android.internal.telephony.TelephonyIntents; 87import com.android.internal.util.AsyncChannel; 88import com.android.server.wifi.configparse.ConfigBuilder; 89 90import org.xml.sax.SAXException; 91 92import java.io.BufferedReader; 93import java.io.FileDescriptor; 94import java.io.FileNotFoundException; 95import java.io.FileReader; 96import java.io.IOException; 97import java.io.PrintWriter; 98import java.net.Inet4Address; 99import java.net.InetAddress; 100import java.security.GeneralSecurityException; 101import java.security.KeyStore; 102import java.security.cert.CertPath; 103import java.security.cert.CertPathValidator; 104import java.security.cert.CertPathValidatorException; 105import java.security.cert.CertificateFactory; 106import java.security.cert.PKIXParameters; 107import java.security.cert.X509Certificate; 108import java.util.ArrayList; 109import java.util.Arrays; 110import java.util.List; 111 112/** 113 * WifiService handles remote WiFi operation requests by implementing 114 * the IWifiManager interface. 115 * 116 * @hide 117 */ 118public class WifiServiceImpl extends IWifiManager.Stub { 119 private static final String TAG = "WifiService"; 120 private static final boolean DBG = true; 121 private static final boolean VDBG = false; 122 123 final WifiStateMachine mWifiStateMachine; 124 125 private final Context mContext; 126 private final FrameworkFacade mFacade; 127 128 private final PowerManager mPowerManager; 129 private final AppOpsManager mAppOps; 130 private final UserManager mUserManager; 131 private final WifiCountryCode mCountryCode; 132 // Debug counter tracking scan requests sent by WifiManager 133 private int scanRequestCounter = 0; 134 135 /* Tracks the open wi-fi network notification */ 136 private WifiNotificationController mNotificationController; 137 /* Polls traffic stats and notifies clients */ 138 private WifiTrafficPoller mTrafficPoller; 139 /* Tracks the persisted states for wi-fi & airplane mode */ 140 final WifiSettingsStore mSettingsStore; 141 /* Logs connection events and some general router and scan stats */ 142 private final WifiMetrics mWifiMetrics; 143 /* Manages affiliated certificates for current user */ 144 private final WifiCertManager mCertManager; 145 146 private final WifiInjector mWifiInjector; 147 /* Backup/Restore Module */ 148 private final WifiBackupRestore mWifiBackupRestore; 149 150 /** 151 * Asynchronous channel to WifiStateMachine 152 */ 153 private AsyncChannel mWifiStateMachineChannel; 154 155 /** 156 * Handles client connections 157 */ 158 private class ClientHandler extends Handler { 159 160 ClientHandler(Looper looper) { 161 super(looper); 162 } 163 164 @Override 165 public void handleMessage(Message msg) { 166 switch (msg.what) { 167 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { 168 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { 169 if (DBG) Slog.d(TAG, "New client listening to asynchronous messages"); 170 // We track the clients by the Messenger 171 // since it is expected to be always available 172 mTrafficPoller.addClient(msg.replyTo); 173 } else { 174 Slog.e(TAG, "Client connection failure, error=" + msg.arg1); 175 } 176 break; 177 } 178 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { 179 if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) { 180 if (DBG) Slog.d(TAG, "Send failed, client connection lost"); 181 } else { 182 if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1); 183 } 184 mTrafficPoller.removeClient(msg.replyTo); 185 break; 186 } 187 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: { 188 AsyncChannel ac = new AsyncChannel(); 189 ac.connect(mContext, this, msg.replyTo); 190 break; 191 } 192 /* Client commands are forwarded to state machine */ 193 case WifiManager.CONNECT_NETWORK: 194 case WifiManager.SAVE_NETWORK: { 195 WifiConfiguration config = (WifiConfiguration) msg.obj; 196 int networkId = msg.arg1; 197 if (msg.what == WifiManager.SAVE_NETWORK) { 198 Slog.d("WiFiServiceImpl ", "SAVE" 199 + " nid=" + Integer.toString(networkId) 200 + " uid=" + msg.sendingUid 201 + " name=" 202 + mContext.getPackageManager().getNameForUid(msg.sendingUid)); 203 } 204 if (msg.what == WifiManager.CONNECT_NETWORK) { 205 Slog.d("WiFiServiceImpl ", "CONNECT " 206 + " nid=" + Integer.toString(networkId) 207 + " uid=" + msg.sendingUid 208 + " name=" 209 + mContext.getPackageManager().getNameForUid(msg.sendingUid)); 210 } 211 212 if (config != null && isValid(config)) { 213 if (DBG) Slog.d(TAG, "Connect with config" + config); 214 mWifiStateMachine.sendMessage(Message.obtain(msg)); 215 } else if (config == null 216 && networkId != WifiConfiguration.INVALID_NETWORK_ID) { 217 if (DBG) Slog.d(TAG, "Connect with networkId" + networkId); 218 mWifiStateMachine.sendMessage(Message.obtain(msg)); 219 } else { 220 Slog.e(TAG, "ClientHandler.handleMessage ignoring invalid msg=" + msg); 221 if (msg.what == WifiManager.CONNECT_NETWORK) { 222 replyFailed(msg, WifiManager.CONNECT_NETWORK_FAILED, 223 WifiManager.INVALID_ARGS); 224 } else { 225 replyFailed(msg, WifiManager.SAVE_NETWORK_FAILED, 226 WifiManager.INVALID_ARGS); 227 } 228 } 229 break; 230 } 231 case WifiManager.FORGET_NETWORK: 232 mWifiStateMachine.sendMessage(Message.obtain(msg)); 233 break; 234 case WifiManager.START_WPS: 235 case WifiManager.CANCEL_WPS: 236 case WifiManager.DISABLE_NETWORK: 237 case WifiManager.RSSI_PKTCNT_FETCH: { 238 mWifiStateMachine.sendMessage(Message.obtain(msg)); 239 break; 240 } 241 default: { 242 Slog.d(TAG, "ClientHandler.handleMessage ignoring msg=" + msg); 243 break; 244 } 245 } 246 } 247 248 private void replyFailed(Message msg, int what, int why) { 249 Message reply = Message.obtain(); 250 reply.what = what; 251 reply.arg1 = why; 252 try { 253 msg.replyTo.send(reply); 254 } catch (RemoteException e) { 255 // There's not much we can do if reply can't be sent! 256 } 257 } 258 } 259 private ClientHandler mClientHandler; 260 261 /** 262 * Handles interaction with WifiStateMachine 263 */ 264 private class WifiStateMachineHandler extends Handler { 265 private AsyncChannel mWsmChannel; 266 267 WifiStateMachineHandler(Looper looper) { 268 super(looper); 269 mWsmChannel = new AsyncChannel(); 270 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler()); 271 } 272 273 @Override 274 public void handleMessage(Message msg) { 275 switch (msg.what) { 276 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { 277 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { 278 mWifiStateMachineChannel = mWsmChannel; 279 } else { 280 Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1); 281 mWifiStateMachineChannel = null; 282 } 283 break; 284 } 285 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { 286 Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1); 287 mWifiStateMachineChannel = null; 288 //Re-establish connection to state machine 289 mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler()); 290 break; 291 } 292 default: { 293 Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg); 294 break; 295 } 296 } 297 } 298 } 299 300 WifiStateMachineHandler mWifiStateMachineHandler; 301 private WifiController mWifiController; 302 private final WifiLockManager mWifiLockManager; 303 private final WifiMulticastLockManager mWifiMulticastLockManager; 304 305 public WifiServiceImpl(Context context) { 306 mContext = context; 307 mWifiInjector = new WifiInjector(context); 308 309 mFacade = mWifiInjector.getFrameworkFacade(); 310 mWifiMetrics = mWifiInjector.getWifiMetrics(); 311 mTrafficPoller = mWifiInjector.getWifiTrafficPoller(); 312 mUserManager = UserManager.get(mContext); 313 mCountryCode = mWifiInjector.getWifiCountryCode(); 314 mWifiStateMachine = mWifiInjector.getWifiStateMachine(); 315 mWifiStateMachine.enableRssiPolling(true); 316 mSettingsStore = mWifiInjector.getWifiSettingsStore(); 317 mPowerManager = mContext.getSystemService(PowerManager.class); 318 mAppOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); 319 mCertManager = mWifiInjector.getWifiCertManager(); 320 321 mNotificationController = mWifiInjector.getWifiNotificationController(); 322 323 mWifiLockManager = mWifiInjector.getWifiLockManager(); 324 mWifiMulticastLockManager = mWifiInjector.getWifiMulticastLockManager(); 325 HandlerThread wifiServiceHandlerThread = mWifiInjector.getWifiServiceHandlerThread(); 326 mClientHandler = new ClientHandler(wifiServiceHandlerThread.getLooper()); 327 mWifiStateMachineHandler = 328 new WifiStateMachineHandler(wifiServiceHandlerThread.getLooper()); 329 mWifiController = mWifiInjector.getWifiController(); 330 mWifiBackupRestore = mWifiInjector.getWifiBackupRestore(); 331 332 enableVerboseLoggingInternal(getVerboseLoggingLevel()); 333 } 334 335 336 /** 337 * Check if Wi-Fi needs to be enabled and start 338 * if needed 339 * 340 * This function is used only at boot time 341 */ 342 public void checkAndStartWifi() { 343 /* Check if wi-fi needs to be enabled */ 344 boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled(); 345 Slog.i(TAG, "WifiService starting up with Wi-Fi " + 346 (wifiEnabled ? "enabled" : "disabled")); 347 348 registerForScanModeChange(); 349 mContext.registerReceiver( 350 new BroadcastReceiver() { 351 @Override 352 public void onReceive(Context context, Intent intent) { 353 if (mSettingsStore.handleAirplaneModeToggled()) { 354 mWifiController.sendMessage(CMD_AIRPLANE_TOGGLED); 355 } 356 if (mSettingsStore.isAirplaneModeOn()) { 357 Log.d(TAG, "resetting country code because Airplane mode is ON"); 358 mCountryCode.airplaneModeEnabled(); 359 } 360 } 361 }, 362 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)); 363 364 mContext.registerReceiver( 365 new BroadcastReceiver() { 366 @Override 367 public void onReceive(Context context, Intent intent) { 368 String state = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE); 369 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(state)) { 370 Log.d(TAG, "resetting networks because SIM was removed"); 371 mWifiStateMachine.resetSimAuthNetworks(false); 372 Log.d(TAG, "resetting country code because SIM is removed"); 373 mCountryCode.simCardRemoved(); 374 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(state)) { 375 Log.d(TAG, "resetting networks because SIM was loaded"); 376 mWifiStateMachine.resetSimAuthNetworks(true); 377 } 378 } 379 }, 380 new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED)); 381 382 // Adding optimizations of only receiving broadcasts when wifi is enabled 383 // can result in race conditions when apps toggle wifi in the background 384 // without active user involvement. Always receive broadcasts. 385 registerForBroadcasts(); 386 registerForPackageOrUserRemoval(); 387 mInIdleMode = mPowerManager.isDeviceIdleMode(); 388 389 mWifiController.start(); 390 391 // If we are already disabled (could be due to airplane mode), avoid changing persist 392 // state here 393 if (wifiEnabled) setWifiEnabled(wifiEnabled); 394 } 395 396 public void handleUserSwitch(int userId) { 397 mWifiStateMachine.handleUserSwitch(userId); 398 } 399 400 /** 401 * see {@link android.net.wifi.WifiManager#pingSupplicant()} 402 * @return {@code true} if the operation succeeds, {@code false} otherwise 403 */ 404 @Override 405 public boolean pingSupplicant() { 406 enforceAccessPermission(); 407 if (mWifiStateMachineChannel != null) { 408 return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel); 409 } else { 410 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 411 return false; 412 } 413 } 414 415 /** 416 * see {@link android.net.wifi.WifiManager#startScan} 417 * and {@link android.net.wifi.WifiManager#startCustomizedScan} 418 * 419 * @param settings If null, use default parameter, i.e. full scan. 420 * @param workSource If null, all blame is given to the calling uid. 421 */ 422 @Override 423 public void startScan(ScanSettings settings, WorkSource workSource) { 424 enforceChangePermission(); 425 synchronized (this) { 426 if (mInIdleMode) { 427 // Need to send an immediate scan result broadcast in case the 428 // caller is waiting for a result .. 429 430 // clear calling identity to send broadcast 431 long callingIdentity = Binder.clearCallingIdentity(); 432 try { 433 mWifiStateMachine.sendScanResultsAvailableBroadcast(/* scanSucceeded = */ false); 434 } finally { 435 // restore calling identity 436 Binder.restoreCallingIdentity(callingIdentity); 437 } 438 mScanPending = true; 439 return; 440 } 441 } 442 if (settings != null) { 443 settings = new ScanSettings(settings); 444 if (!settings.isValid()) { 445 Slog.e(TAG, "invalid scan setting"); 446 return; 447 } 448 } 449 if (workSource != null) { 450 enforceWorkSourcePermission(); 451 // WifiManager currently doesn't use names, so need to clear names out of the 452 // supplied WorkSource to allow future WorkSource combining. 453 workSource.clearNames(); 454 } 455 if (workSource == null && Binder.getCallingUid() >= 0) { 456 workSource = new WorkSource(Binder.getCallingUid()); 457 } 458 mWifiStateMachine.startScan(Binder.getCallingUid(), scanRequestCounter++, 459 settings, workSource); 460 } 461 462 @Override 463 public String getWpsNfcConfigurationToken(int netId) { 464 enforceConnectivityInternalPermission(); 465 return mWifiStateMachine.syncGetWpsNfcConfigurationToken(netId); 466 } 467 468 boolean mInIdleMode; 469 boolean mScanPending; 470 471 void handleIdleModeChanged() { 472 boolean doScan = false; 473 synchronized (this) { 474 boolean idle = mPowerManager.isDeviceIdleMode(); 475 if (mInIdleMode != idle) { 476 mInIdleMode = idle; 477 if (!idle) { 478 if (mScanPending) { 479 mScanPending = false; 480 doScan = true; 481 } 482 } 483 } 484 } 485 if (doScan) { 486 // Someone requested a scan while we were idle; do a full scan now. 487 startScan(null, null); 488 } 489 } 490 491 private void enforceAccessPermission() { 492 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE, 493 "WifiService"); 494 } 495 496 private void enforceChangePermission() { 497 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE, 498 "WifiService"); 499 } 500 501 private void enforceLocationHardwarePermission() { 502 mContext.enforceCallingOrSelfPermission(Manifest.permission.LOCATION_HARDWARE, 503 "LocationHardware"); 504 } 505 506 private void enforceReadCredentialPermission() { 507 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.READ_WIFI_CREDENTIAL, 508 "WifiService"); 509 } 510 511 private void enforceWorkSourcePermission() { 512 mContext.enforceCallingPermission(android.Manifest.permission.UPDATE_DEVICE_STATS, 513 "WifiService"); 514 515 } 516 517 private void enforceMulticastChangePermission() { 518 mContext.enforceCallingOrSelfPermission( 519 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, 520 "WifiService"); 521 } 522 523 private void enforceConnectivityInternalPermission() { 524 mContext.enforceCallingOrSelfPermission( 525 android.Manifest.permission.CONNECTIVITY_INTERNAL, 526 "ConnectivityService"); 527 } 528 529 /** 530 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)} 531 * @param enable {@code true} to enable, {@code false} to disable. 532 * @return {@code true} if the enable/disable operation was 533 * started or is already in the queue. 534 */ 535 @Override 536 public synchronized boolean setWifiEnabled(boolean enable) { 537 enforceChangePermission(); 538 Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid() 539 + ", uid=" + Binder.getCallingUid()); 540 541 /* 542 * Caller might not have WRITE_SECURE_SETTINGS, 543 * only CHANGE_WIFI_STATE is enforced 544 */ 545 546 long ident = Binder.clearCallingIdentity(); 547 try { 548 if (! mSettingsStore.handleWifiToggled(enable)) { 549 // Nothing to do if wifi cannot be toggled 550 return true; 551 } 552 } finally { 553 Binder.restoreCallingIdentity(ident); 554 } 555 556 mWifiController.sendMessage(CMD_WIFI_TOGGLED); 557 return true; 558 } 559 560 /** 561 * see {@link WifiManager#getWifiState()} 562 * @return One of {@link WifiManager#WIFI_STATE_DISABLED}, 563 * {@link WifiManager#WIFI_STATE_DISABLING}, 564 * {@link WifiManager#WIFI_STATE_ENABLED}, 565 * {@link WifiManager#WIFI_STATE_ENABLING}, 566 * {@link WifiManager#WIFI_STATE_UNKNOWN} 567 */ 568 @Override 569 public int getWifiEnabledState() { 570 enforceAccessPermission(); 571 return mWifiStateMachine.syncGetWifiState(); 572 } 573 574 /** 575 * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)} 576 * @param wifiConfig SSID, security and channel details as 577 * part of WifiConfiguration 578 * @param enabled true to enable and false to disable 579 */ 580 @Override 581 public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) { 582 enforceChangePermission(); 583 ConnectivityManager.enforceTetherChangePermission(mContext); 584 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { 585 throw new SecurityException("DISALLOW_CONFIG_TETHERING is enabled for this user."); 586 } 587 // null wifiConfig is a meaningful input for CMD_SET_AP 588 if (wifiConfig == null || isValid(wifiConfig)) { 589 mWifiController.obtainMessage(CMD_SET_AP, enabled ? 1 : 0, 0, wifiConfig).sendToTarget(); 590 } else { 591 Slog.e(TAG, "Invalid WifiConfiguration"); 592 } 593 } 594 595 /** 596 * see {@link WifiManager#getWifiApState()} 597 * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED}, 598 * {@link WifiManager#WIFI_AP_STATE_DISABLING}, 599 * {@link WifiManager#WIFI_AP_STATE_ENABLED}, 600 * {@link WifiManager#WIFI_AP_STATE_ENABLING}, 601 * {@link WifiManager#WIFI_AP_STATE_FAILED} 602 */ 603 @Override 604 public int getWifiApEnabledState() { 605 enforceAccessPermission(); 606 return mWifiStateMachine.syncGetWifiApState(); 607 } 608 609 /** 610 * see {@link WifiManager#getWifiApConfiguration()} 611 * @return soft access point configuration 612 */ 613 @Override 614 public WifiConfiguration getWifiApConfiguration() { 615 enforceAccessPermission(); 616 return mWifiStateMachine.syncGetWifiApConfiguration(); 617 } 618 619 /** 620 * see {@link WifiManager#buildWifiConfig()} 621 * @return a WifiConfiguration. 622 */ 623 @Override 624 public WifiConfiguration buildWifiConfig(String uriString, String mimeType, byte[] data) { 625 if (mimeType.equals(ConfigBuilder.WifiConfigType)) { 626 try { 627 return ConfigBuilder.buildConfig(uriString, data, mContext); 628 } 629 catch (IOException | GeneralSecurityException | SAXException e) { 630 Log.e(TAG, "Failed to parse wi-fi configuration: " + e); 631 } 632 } 633 else { 634 Log.i(TAG, "Unknown wi-fi config type: " + mimeType); 635 } 636 return null; 637 } 638 639 /** 640 * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)} 641 * @param wifiConfig WifiConfiguration details for soft access point 642 */ 643 @Override 644 public void setWifiApConfiguration(WifiConfiguration wifiConfig) { 645 enforceChangePermission(); 646 if (wifiConfig == null) 647 return; 648 if (isValid(wifiConfig)) { 649 mWifiStateMachine.setWifiApConfiguration(wifiConfig); 650 } else { 651 Slog.e(TAG, "Invalid WifiConfiguration"); 652 } 653 } 654 655 /** 656 * see {@link android.net.wifi.WifiManager#isScanAlwaysAvailable()} 657 */ 658 @Override 659 public boolean isScanAlwaysAvailable() { 660 enforceAccessPermission(); 661 return mSettingsStore.isScanAlwaysAvailable(); 662 } 663 664 /** 665 * see {@link android.net.wifi.WifiManager#disconnect()} 666 */ 667 @Override 668 public void disconnect() { 669 enforceChangePermission(); 670 mWifiStateMachine.disconnectCommand(); 671 } 672 673 /** 674 * see {@link android.net.wifi.WifiManager#reconnect()} 675 */ 676 @Override 677 public void reconnect() { 678 enforceChangePermission(); 679 mWifiStateMachine.reconnectCommand(); 680 } 681 682 /** 683 * see {@link android.net.wifi.WifiManager#reassociate()} 684 */ 685 @Override 686 public void reassociate() { 687 enforceChangePermission(); 688 mWifiStateMachine.reassociateCommand(); 689 } 690 691 /** 692 * see {@link android.net.wifi.WifiManager#getSupportedFeatures} 693 */ 694 @Override 695 public int getSupportedFeatures() { 696 enforceAccessPermission(); 697 if (mWifiStateMachineChannel != null) { 698 return mWifiStateMachine.syncGetSupportedFeatures(mWifiStateMachineChannel); 699 } else { 700 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 701 return 0; 702 } 703 } 704 705 @Override 706 public void requestActivityInfo(ResultReceiver result) { 707 Bundle bundle = new Bundle(); 708 bundle.putParcelable(BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, reportActivityInfo()); 709 result.send(0, bundle); 710 } 711 712 /** 713 * see {@link android.net.wifi.WifiManager#getControllerActivityEnergyInfo(int)} 714 */ 715 @Override 716 public WifiActivityEnergyInfo reportActivityInfo() { 717 enforceAccessPermission(); 718 if ((getSupportedFeatures() & WifiManager.WIFI_FEATURE_LINK_LAYER_STATS) == 0) { 719 return null; 720 } 721 WifiLinkLayerStats stats; 722 WifiActivityEnergyInfo energyInfo = null; 723 if (mWifiStateMachineChannel != null) { 724 stats = mWifiStateMachine.syncGetLinkLayerStats(mWifiStateMachineChannel); 725 if (stats != null) { 726 final long rxIdleCurrent = mContext.getResources().getInteger( 727 com.android.internal.R.integer.config_wifi_idle_receive_cur_ma); 728 final long rxCurrent = mContext.getResources().getInteger( 729 com.android.internal.R.integer.config_wifi_active_rx_cur_ma); 730 final long txCurrent = mContext.getResources().getInteger( 731 com.android.internal.R.integer.config_wifi_tx_cur_ma); 732 final double voltage = mContext.getResources().getInteger( 733 com.android.internal.R.integer.config_wifi_operating_voltage_mv) 734 / 1000.0; 735 736 final long rxIdleTime = stats.on_time - stats.tx_time - stats.rx_time; 737 final long[] txTimePerLevel; 738 if (stats.tx_time_per_level != null) { 739 txTimePerLevel = new long[stats.tx_time_per_level.length]; 740 for (int i = 0; i < txTimePerLevel.length; i++) { 741 txTimePerLevel[i] = stats.tx_time_per_level[i]; 742 // TODO(b/27227497): Need to read the power consumed per level from config 743 } 744 } else { 745 // This will happen if the HAL get link layer API returned null. 746 txTimePerLevel = new long[0]; 747 } 748 final long energyUsed = (long)((stats.tx_time * txCurrent + 749 stats.rx_time * rxCurrent + 750 rxIdleTime * rxIdleCurrent) * voltage); 751 if (VDBG || rxIdleTime < 0 || stats.on_time < 0 || stats.tx_time < 0 || 752 stats.rx_time < 0 || energyUsed < 0) { 753 StringBuilder sb = new StringBuilder(); 754 sb.append(" rxIdleCur=" + rxIdleCurrent); 755 sb.append(" rxCur=" + rxCurrent); 756 sb.append(" txCur=" + txCurrent); 757 sb.append(" voltage=" + voltage); 758 sb.append(" on_time=" + stats.on_time); 759 sb.append(" tx_time=" + stats.tx_time); 760 sb.append(" tx_time_per_level=" + Arrays.toString(txTimePerLevel)); 761 sb.append(" rx_time=" + stats.rx_time); 762 sb.append(" rxIdleTime=" + rxIdleTime); 763 sb.append(" energy=" + energyUsed); 764 Log.d(TAG, " reportActivityInfo: " + sb.toString()); 765 } 766 767 // Convert the LinkLayerStats into EnergyActivity 768 energyInfo = new WifiActivityEnergyInfo(SystemClock.elapsedRealtime(), 769 WifiActivityEnergyInfo.STACK_STATE_STATE_IDLE, stats.tx_time, 770 txTimePerLevel, stats.rx_time, rxIdleTime, energyUsed); 771 } 772 if (energyInfo != null && energyInfo.isValid()) { 773 return energyInfo; 774 } else { 775 return null; 776 } 777 } else { 778 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 779 return null; 780 } 781 } 782 783 /** 784 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()} 785 * @return the list of configured networks 786 */ 787 @Override 788 public List<WifiConfiguration> getConfiguredNetworks() { 789 enforceAccessPermission(); 790 if (mWifiStateMachineChannel != null) { 791 return mWifiStateMachine.syncGetConfiguredNetworks(Binder.getCallingUid(), 792 mWifiStateMachineChannel); 793 } else { 794 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 795 return null; 796 } 797 } 798 799 /** 800 * see {@link android.net.wifi.WifiManager#getPrivilegedConfiguredNetworks()} 801 * @return the list of configured networks with real preSharedKey 802 */ 803 @Override 804 public List<WifiConfiguration> getPrivilegedConfiguredNetworks() { 805 enforceReadCredentialPermission(); 806 enforceAccessPermission(); 807 if (mWifiStateMachineChannel != null) { 808 return mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel); 809 } else { 810 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 811 return null; 812 } 813 } 814 815 /** 816 * Returns a WifiConfiguration matching this ScanResult 817 * @param scanResult scanResult that represents the BSSID 818 * @return {@link WifiConfiguration} that matches this BSSID or null 819 */ 820 @Override 821 public WifiConfiguration getMatchingWifiConfig(ScanResult scanResult) { 822 enforceAccessPermission(); 823 return mWifiStateMachine.syncGetMatchingWifiConfig(scanResult, mWifiStateMachineChannel); 824 } 825 826 /** 827 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)} 828 * @return the supplicant-assigned identifier for the new or updated 829 * network if the operation succeeds, or {@code -1} if it fails 830 */ 831 @Override 832 public int addOrUpdateNetwork(WifiConfiguration config) { 833 enforceChangePermission(); 834 if (isValid(config) && isValidPasspoint(config)) { 835 836 WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig; 837 838 if (config.isPasspoint() && 839 (enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TLS || 840 enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TTLS)) { 841 if (config.updateIdentifier != null) { 842 enforceAccessPermission(); 843 } 844 else { 845 try { 846 verifyCert(enterpriseConfig.getCaCertificate()); 847 } catch (CertPathValidatorException cpve) { 848 Slog.e(TAG, "CA Cert " + 849 enterpriseConfig.getCaCertificate().getSubjectX500Principal() + 850 " untrusted: " + cpve.getMessage()); 851 return -1; 852 } catch (GeneralSecurityException | IOException e) { 853 Slog.e(TAG, "Failed to verify certificate" + 854 enterpriseConfig.getCaCertificate().getSubjectX500Principal() + 855 ": " + e); 856 return -1; 857 } 858 } 859 } 860 861 //TODO: pass the Uid the WifiStateMachine as a message parameter 862 Slog.i("addOrUpdateNetwork", " uid = " + Integer.toString(Binder.getCallingUid()) 863 + " SSID " + config.SSID 864 + " nid=" + Integer.toString(config.networkId)); 865 if (config.networkId == WifiConfiguration.INVALID_NETWORK_ID) { 866 config.creatorUid = Binder.getCallingUid(); 867 } else { 868 config.lastUpdateUid = Binder.getCallingUid(); 869 } 870 if (mWifiStateMachineChannel != null) { 871 return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config); 872 } else { 873 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 874 return -1; 875 } 876 } else { 877 Slog.e(TAG, "bad network configuration"); 878 return -1; 879 } 880 } 881 882 public static void verifyCert(X509Certificate caCert) 883 throws GeneralSecurityException, IOException { 884 CertificateFactory factory = CertificateFactory.getInstance("X.509"); 885 CertPathValidator validator = 886 CertPathValidator.getInstance(CertPathValidator.getDefaultType()); 887 CertPath path = factory.generateCertPath( 888 Arrays.asList(caCert)); 889 KeyStore ks = KeyStore.getInstance("AndroidCAStore"); 890 ks.load(null, null); 891 PKIXParameters params = new PKIXParameters(ks); 892 params.setRevocationEnabled(false); 893 validator.validate(path, params); 894 } 895 896 /** 897 * See {@link android.net.wifi.WifiManager#removeNetwork(int)} 898 * @param netId the integer that identifies the network configuration 899 * to the supplicant 900 * @return {@code true} if the operation succeeded 901 */ 902 @Override 903 public boolean removeNetwork(int netId) { 904 enforceChangePermission(); 905 906 if (mWifiStateMachineChannel != null) { 907 return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId); 908 } else { 909 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 910 return false; 911 } 912 } 913 914 /** 915 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)} 916 * @param netId the integer that identifies the network configuration 917 * to the supplicant 918 * @param disableOthers if true, disable all other networks. 919 * @return {@code true} if the operation succeeded 920 */ 921 @Override 922 public boolean enableNetwork(int netId, boolean disableOthers) { 923 enforceChangePermission(); 924 if (mWifiStateMachineChannel != null) { 925 return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId, 926 disableOthers); 927 } else { 928 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 929 return false; 930 } 931 } 932 933 /** 934 * See {@link android.net.wifi.WifiManager#disableNetwork(int)} 935 * @param netId the integer that identifies the network configuration 936 * to the supplicant 937 * @return {@code true} if the operation succeeded 938 */ 939 @Override 940 public boolean disableNetwork(int netId) { 941 enforceChangePermission(); 942 if (mWifiStateMachineChannel != null) { 943 return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId); 944 } else { 945 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 946 return false; 947 } 948 } 949 950 /** 951 * See {@link android.net.wifi.WifiManager#getConnectionInfo()} 952 * @return the Wi-Fi information, contained in {@link WifiInfo}. 953 */ 954 @Override 955 public WifiInfo getConnectionInfo() { 956 enforceAccessPermission(); 957 /* 958 * Make sure we have the latest information, by sending 959 * a status request to the supplicant. 960 */ 961 return mWifiStateMachine.syncRequestConnectionInfo(); 962 } 963 964 /** 965 * Return the results of the most recent access point scan, in the form of 966 * a list of {@link ScanResult} objects. 967 * @return the list of results 968 */ 969 @Override 970 public List<ScanResult> getScanResults(String callingPackage) { 971 enforceAccessPermission(); 972 int userId = UserHandle.getCallingUserId(); 973 int uid = Binder.getCallingUid(); 974 boolean canReadPeerMacAddresses = checkPeersMacAddress(); 975 boolean isActiveNetworkScorer = 976 NetworkScorerAppManager.isCallerActiveScorer(mContext, uid); 977 boolean hasInteractUsersFull = checkInteractAcrossUsersFull(); 978 long ident = Binder.clearCallingIdentity(); 979 try { 980 if (!canReadPeerMacAddresses && !isActiveNetworkScorer 981 && !isLocationEnabled(callingPackage)) { 982 return new ArrayList<ScanResult>(); 983 } 984 if (!canReadPeerMacAddresses && !isActiveNetworkScorer 985 && !checkCallerCanAccessScanResults(callingPackage, uid)) { 986 return new ArrayList<ScanResult>(); 987 } 988 if (mAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, uid, callingPackage) 989 != AppOpsManager.MODE_ALLOWED) { 990 return new ArrayList<ScanResult>(); 991 } 992 if (!isCurrentProfile(userId) && !hasInteractUsersFull) { 993 return new ArrayList<ScanResult>(); 994 } 995 return mWifiStateMachine.syncGetScanResultsList(); 996 } finally { 997 Binder.restoreCallingIdentity(ident); 998 } 999 } 1000 1001 /** 1002 * Add a Hotspot 2.0 release 2 Management Object 1003 * @param mo The MO in XML form 1004 * @return -1 for failure 1005 */ 1006 @Override 1007 public int addPasspointManagementObject(String mo) { 1008 return mWifiStateMachine.syncAddPasspointManagementObject(mWifiStateMachineChannel, mo); 1009 } 1010 1011 /** 1012 * Modify a Hotspot 2.0 release 2 Management Object 1013 * @param fqdn The FQDN of the service provider 1014 * @param mos A List of MO definitions to be updated 1015 * @return the number of nodes updated, or -1 for failure 1016 */ 1017 @Override 1018 public int modifyPasspointManagementObject(String fqdn, List<PasspointManagementObjectDefinition> mos) { 1019 return mWifiStateMachine.syncModifyPasspointManagementObject(mWifiStateMachineChannel, fqdn, mos); 1020 } 1021 1022 /** 1023 * Query for a Hotspot 2.0 release 2 OSU icon 1024 * @param bssid The BSSID of the AP 1025 * @param fileName Icon file name 1026 */ 1027 @Override 1028 public void queryPasspointIcon(long bssid, String fileName) { 1029 mWifiStateMachine.syncQueryPasspointIcon(mWifiStateMachineChannel, bssid, fileName); 1030 } 1031 1032 /** 1033 * Match the currently associated network against the SP matching the given FQDN 1034 * @param fqdn FQDN of the SP 1035 * @return ordinal [HomeProvider, RoamingProvider, Incomplete, None, Declined] 1036 */ 1037 @Override 1038 public int matchProviderWithCurrentNetwork(String fqdn) { 1039 return mWifiStateMachine.matchProviderWithCurrentNetwork(mWifiStateMachineChannel, fqdn); 1040 } 1041 1042 /** 1043 * Deauthenticate and set the re-authentication hold off time for the current network 1044 * @param holdoff hold off time in milliseconds 1045 * @param ess set if the hold off pertains to an ESS rather than a BSS 1046 */ 1047 @Override 1048 public void deauthenticateNetwork(long holdoff, boolean ess) { 1049 mWifiStateMachine.deauthenticateNetwork(mWifiStateMachineChannel, holdoff, ess); 1050 } 1051 1052 private boolean isLocationEnabled(String callingPackage) { 1053 boolean legacyForegroundApp = !isMApp(mContext, callingPackage) 1054 && isForegroundApp(callingPackage); 1055 return legacyForegroundApp || Settings.Secure.getInt(mContext.getContentResolver(), 1056 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF) 1057 != Settings.Secure.LOCATION_MODE_OFF; 1058 } 1059 1060 /** 1061 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL. 1062 */ 1063 private boolean checkInteractAcrossUsersFull() { 1064 return mContext.checkCallingOrSelfPermission( 1065 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) 1066 == PackageManager.PERMISSION_GRANTED; 1067 } 1068 1069 /** 1070 * Returns true if the caller holds PEERS_MAC_ADDRESS. 1071 */ 1072 private boolean checkPeersMacAddress() { 1073 return mContext.checkCallingOrSelfPermission( 1074 android.Manifest.permission.PEERS_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED; 1075 } 1076 1077 /** 1078 * Returns true if the calling user is the current one or a profile of the 1079 * current user.. 1080 */ 1081 private boolean isCurrentProfile(int userId) { 1082 int currentUser = ActivityManager.getCurrentUser(); 1083 if (userId == currentUser) { 1084 return true; 1085 } 1086 List<UserInfo> profiles = mUserManager.getProfiles(currentUser); 1087 for (UserInfo user : profiles) { 1088 if (userId == user.id) { 1089 return true; 1090 } 1091 } 1092 return false; 1093 } 1094 1095 /** 1096 * Tell the supplicant to persist the current list of configured networks. 1097 * @return {@code true} if the operation succeeded 1098 * 1099 * TODO: deprecate this 1100 */ 1101 @Override 1102 public boolean saveConfiguration() { 1103 enforceChangePermission(); 1104 if (mWifiStateMachineChannel != null) { 1105 return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel); 1106 } else { 1107 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1108 return false; 1109 } 1110 } 1111 1112 /** 1113 * Set the country code 1114 * @param countryCode ISO 3166 country code. 1115 * @param persist {@code true} if the setting should be remembered. 1116 * 1117 * The persist behavior exists so that wifi can fall back to the last 1118 * persisted country code on a restart, when the locale information is 1119 * not available from telephony. 1120 */ 1121 @Override 1122 public void setCountryCode(String countryCode, boolean persist) { 1123 Slog.i(TAG, "WifiService trying to set country code to " + countryCode + 1124 " with persist set to " + persist); 1125 enforceConnectivityInternalPermission(); 1126 final long token = Binder.clearCallingIdentity(); 1127 try { 1128 if (mCountryCode.setCountryCode(countryCode, persist) && persist) { 1129 // Save this country code to persistent storage 1130 mFacade.setStringSetting(mContext, 1131 Settings.Global.WIFI_COUNTRY_CODE, 1132 countryCode); 1133 } 1134 } finally { 1135 Binder.restoreCallingIdentity(token); 1136 } 1137 } 1138 1139 /** 1140 * Get the country code 1141 * @return ISO 3166 country code. 1142 */ 1143 @Override 1144 public String getCountryCode() { 1145 enforceConnectivityInternalPermission(); 1146 String country = mCountryCode.getCurrentCountryCode(); 1147 return country; 1148 } 1149 /** 1150 * Set the operational frequency band 1151 * @param band One of 1152 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO}, 1153 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ}, 1154 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ}, 1155 * @param persist {@code true} if the setting should be remembered. 1156 * 1157 */ 1158 @Override 1159 public void setFrequencyBand(int band, boolean persist) { 1160 enforceChangePermission(); 1161 if (!isDualBandSupported()) return; 1162 Slog.i(TAG, "WifiService trying to set frequency band to " + band + 1163 " with persist set to " + persist); 1164 final long token = Binder.clearCallingIdentity(); 1165 try { 1166 mWifiStateMachine.setFrequencyBand(band, persist); 1167 } finally { 1168 Binder.restoreCallingIdentity(token); 1169 } 1170 } 1171 1172 1173 /** 1174 * Get the operational frequency band 1175 */ 1176 @Override 1177 public int getFrequencyBand() { 1178 enforceAccessPermission(); 1179 return mWifiStateMachine.getFrequencyBand(); 1180 } 1181 1182 @Override 1183 public boolean isDualBandSupported() { 1184 //TODO: Should move towards adding a driver API that checks at runtime 1185 return mContext.getResources().getBoolean( 1186 com.android.internal.R.bool.config_wifi_dual_band_support); 1187 } 1188 1189 /** 1190 * Return the DHCP-assigned addresses from the last successful DHCP request, 1191 * if any. 1192 * @return the DHCP information 1193 * @deprecated 1194 */ 1195 @Override 1196 @Deprecated 1197 public DhcpInfo getDhcpInfo() { 1198 enforceAccessPermission(); 1199 DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults(); 1200 1201 DhcpInfo info = new DhcpInfo(); 1202 1203 if (dhcpResults.ipAddress != null && 1204 dhcpResults.ipAddress.getAddress() instanceof Inet4Address) { 1205 info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.ipAddress.getAddress()); 1206 } 1207 1208 if (dhcpResults.gateway != null) { 1209 info.gateway = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.gateway); 1210 } 1211 1212 int dnsFound = 0; 1213 for (InetAddress dns : dhcpResults.dnsServers) { 1214 if (dns instanceof Inet4Address) { 1215 if (dnsFound == 0) { 1216 info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address)dns); 1217 } else { 1218 info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address)dns); 1219 } 1220 if (++dnsFound > 1) break; 1221 } 1222 } 1223 Inet4Address serverAddress = dhcpResults.serverAddress; 1224 if (serverAddress != null) { 1225 info.serverAddress = NetworkUtils.inetAddressToInt(serverAddress); 1226 } 1227 info.leaseDuration = dhcpResults.leaseDuration; 1228 1229 return info; 1230 } 1231 1232 /** 1233 * see {@link android.net.wifi.WifiManager#addToBlacklist} 1234 * 1235 */ 1236 @Override 1237 public void addToBlacklist(String bssid) { 1238 enforceChangePermission(); 1239 1240 mWifiStateMachine.addToBlacklist(bssid); 1241 } 1242 1243 /** 1244 * see {@link android.net.wifi.WifiManager#clearBlacklist} 1245 * 1246 */ 1247 @Override 1248 public void clearBlacklist() { 1249 enforceChangePermission(); 1250 1251 mWifiStateMachine.clearBlacklist(); 1252 } 1253 1254 /** 1255 * enable TDLS for the local NIC to remote NIC 1256 * The APPs don't know the remote MAC address to identify NIC though, 1257 * so we need to do additional work to find it from remote IP address 1258 */ 1259 1260 class TdlsTaskParams { 1261 public String remoteIpAddress; 1262 public boolean enable; 1263 } 1264 1265 class TdlsTask extends AsyncTask<TdlsTaskParams, Integer, Integer> { 1266 @Override 1267 protected Integer doInBackground(TdlsTaskParams... params) { 1268 1269 // Retrieve parameters for the call 1270 TdlsTaskParams param = params[0]; 1271 String remoteIpAddress = param.remoteIpAddress.trim(); 1272 boolean enable = param.enable; 1273 1274 // Get MAC address of Remote IP 1275 String macAddress = null; 1276 1277 BufferedReader reader = null; 1278 1279 try { 1280 reader = new BufferedReader(new FileReader("/proc/net/arp")); 1281 1282 // Skip over the line bearing colum titles 1283 String line = reader.readLine(); 1284 1285 while ((line = reader.readLine()) != null) { 1286 String[] tokens = line.split("[ ]+"); 1287 if (tokens.length < 6) { 1288 continue; 1289 } 1290 1291 // ARP column format is 1292 // Address HWType HWAddress Flags Mask IFace 1293 String ip = tokens[0]; 1294 String mac = tokens[3]; 1295 1296 if (remoteIpAddress.equals(ip)) { 1297 macAddress = mac; 1298 break; 1299 } 1300 } 1301 1302 if (macAddress == null) { 1303 Slog.w(TAG, "Did not find remoteAddress {" + remoteIpAddress + "} in " + 1304 "/proc/net/arp"); 1305 } else { 1306 enableTdlsWithMacAddress(macAddress, enable); 1307 } 1308 1309 } catch (FileNotFoundException e) { 1310 Slog.e(TAG, "Could not open /proc/net/arp to lookup mac address"); 1311 } catch (IOException e) { 1312 Slog.e(TAG, "Could not read /proc/net/arp to lookup mac address"); 1313 } finally { 1314 try { 1315 if (reader != null) { 1316 reader.close(); 1317 } 1318 } 1319 catch (IOException e) { 1320 // Do nothing 1321 } 1322 } 1323 1324 return 0; 1325 } 1326 } 1327 1328 @Override 1329 public void enableTdls(String remoteAddress, boolean enable) { 1330 if (remoteAddress == null) { 1331 throw new IllegalArgumentException("remoteAddress cannot be null"); 1332 } 1333 1334 TdlsTaskParams params = new TdlsTaskParams(); 1335 params.remoteIpAddress = remoteAddress; 1336 params.enable = enable; 1337 new TdlsTask().execute(params); 1338 } 1339 1340 1341 @Override 1342 public void enableTdlsWithMacAddress(String remoteMacAddress, boolean enable) { 1343 if (remoteMacAddress == null) { 1344 throw new IllegalArgumentException("remoteMacAddress cannot be null"); 1345 } 1346 1347 mWifiStateMachine.enableTdls(remoteMacAddress, enable); 1348 } 1349 1350 /** 1351 * Get a reference to handler. This is used by a client to establish 1352 * an AsyncChannel communication with WifiService 1353 */ 1354 @Override 1355 public Messenger getWifiServiceMessenger() { 1356 enforceAccessPermission(); 1357 enforceChangePermission(); 1358 return new Messenger(mClientHandler); 1359 } 1360 1361 /** 1362 * Disable an ephemeral network, i.e. network that is created thru a WiFi Scorer 1363 */ 1364 @Override 1365 public void disableEphemeralNetwork(String SSID) { 1366 enforceAccessPermission(); 1367 enforceChangePermission(); 1368 mWifiStateMachine.disableEphemeralNetwork(SSID); 1369 } 1370 1371 /** 1372 * Get the IP and proxy configuration file 1373 */ 1374 @Override 1375 public String getConfigFile() { 1376 enforceAccessPermission(); 1377 return mWifiStateMachine.getConfigFile(); 1378 } 1379 1380 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 1381 @Override 1382 public void onReceive(Context context, Intent intent) { 1383 String action = intent.getAction(); 1384 if (action.equals(Intent.ACTION_SCREEN_ON)) { 1385 mWifiController.sendMessage(CMD_SCREEN_ON); 1386 } else if (action.equals(Intent.ACTION_USER_PRESENT)) { 1387 mWifiController.sendMessage(CMD_USER_PRESENT); 1388 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { 1389 mWifiController.sendMessage(CMD_SCREEN_OFF); 1390 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { 1391 int pluggedType = intent.getIntExtra("plugged", 0); 1392 mWifiController.sendMessage(CMD_BATTERY_CHANGED, pluggedType, 0, null); 1393 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { 1394 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 1395 BluetoothAdapter.STATE_DISCONNECTED); 1396 mWifiStateMachine.sendBluetoothAdapterStateChange(state); 1397 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) { 1398 boolean emergencyMode = intent.getBooleanExtra("phoneinECMState", false); 1399 mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, emergencyMode ? 1 : 0, 0); 1400 } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED)) { 1401 boolean inCall = intent.getBooleanExtra(PhoneConstants.PHONE_IN_EMERGENCY_CALL, false); 1402 mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, inCall ? 1 : 0, 0); 1403 } else if (action.equals(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED)) { 1404 handleIdleModeChanged(); 1405 } 1406 } 1407 }; 1408 1409 /** 1410 * Observes settings changes to scan always mode. 1411 */ 1412 private void registerForScanModeChange() { 1413 ContentObserver contentObserver = new ContentObserver(null) { 1414 @Override 1415 public void onChange(boolean selfChange) { 1416 mSettingsStore.handleWifiScanAlwaysAvailableToggled(); 1417 mWifiController.sendMessage(CMD_SCAN_ALWAYS_MODE_CHANGED); 1418 } 1419 }; 1420 1421 mContext.getContentResolver().registerContentObserver( 1422 Settings.Global.getUriFor(Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE), 1423 false, contentObserver); 1424 } 1425 1426 private void registerForBroadcasts() { 1427 IntentFilter intentFilter = new IntentFilter(); 1428 intentFilter.addAction(Intent.ACTION_SCREEN_ON); 1429 intentFilter.addAction(Intent.ACTION_USER_PRESENT); 1430 intentFilter.addAction(Intent.ACTION_SCREEN_OFF); 1431 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 1432 intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 1433 intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); 1434 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); 1435 intentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); 1436 1437 boolean trackEmergencyCallState = mContext.getResources().getBoolean( 1438 com.android.internal.R.bool.config_wifi_turn_off_during_emergency_call); 1439 if (trackEmergencyCallState) { 1440 intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED); 1441 } 1442 1443 mContext.registerReceiver(mReceiver, intentFilter); 1444 } 1445 1446 private void registerForPackageOrUserRemoval() { 1447 IntentFilter intentFilter = new IntentFilter(); 1448 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); 1449 intentFilter.addAction(Intent.ACTION_USER_REMOVED); 1450 mContext.registerReceiverAsUser(new BroadcastReceiver() { 1451 @Override 1452 public void onReceive(Context context, Intent intent) { 1453 switch (intent.getAction()) { 1454 case Intent.ACTION_PACKAGE_REMOVED: { 1455 if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { 1456 return; 1457 } 1458 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); 1459 Uri uri = intent.getData(); 1460 if (uid == -1 || uri == null) { 1461 return; 1462 } 1463 String pkgName = uri.getSchemeSpecificPart(); 1464 mWifiStateMachine.removeAppConfigs(pkgName, uid); 1465 break; 1466 } 1467 case Intent.ACTION_USER_REMOVED: { 1468 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); 1469 mWifiStateMachine.removeUserConfigs(userHandle); 1470 break; 1471 } 1472 } 1473 } 1474 }, UserHandle.ALL, intentFilter, null, null); 1475 } 1476 1477 @Override 1478 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 1479 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 1480 != PackageManager.PERMISSION_GRANTED) { 1481 pw.println("Permission Denial: can't dump WifiService from from pid=" 1482 + Binder.getCallingPid() 1483 + ", uid=" + Binder.getCallingUid()); 1484 return; 1485 } 1486 if (args.length > 0 && WifiMetrics.PROTO_DUMP_ARG.equals(args[0])) { 1487 // WifiMetrics proto bytes were requested. Dump only these. 1488 mWifiStateMachine.updateWifiMetrics(); 1489 mWifiMetrics.dump(fd, pw, args); 1490 } else if (args.length > 0 && IpManager.DUMP_ARG.equals(args[0])) { 1491 // IpManager dump was requested. Pass it along and take no further action. 1492 String[] ipManagerArgs = new String[args.length - 1]; 1493 System.arraycopy(args, 1, ipManagerArgs, 0, ipManagerArgs.length); 1494 mWifiStateMachine.dumpIpManager(fd, pw, ipManagerArgs); 1495 } else { 1496 pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName()); 1497 pw.println("Stay-awake conditions: " + 1498 Settings.Global.getInt(mContext.getContentResolver(), 1499 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0)); 1500 pw.println("mInIdleMode " + mInIdleMode); 1501 pw.println("mScanPending " + mScanPending); 1502 mWifiController.dump(fd, pw, args); 1503 mSettingsStore.dump(fd, pw, args); 1504 mNotificationController.dump(fd, pw, args); 1505 mTrafficPoller.dump(fd, pw, args); 1506 1507 pw.println("Latest scan results:"); 1508 List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList(); 1509 long nowMs = System.currentTimeMillis(); 1510 if (scanResults != null && scanResults.size() != 0) { 1511 pw.println(" BSSID Frequency RSSI Age SSID " + 1512 " Flags"); 1513 for (ScanResult r : scanResults) { 1514 long ageSec = 0; 1515 long ageMilli = 0; 1516 if (nowMs > r.seen && r.seen > 0) { 1517 ageSec = (nowMs - r.seen) / 1000; 1518 ageMilli = (nowMs - r.seen) % 1000; 1519 } 1520 String candidate = " "; 1521 if (r.isAutoJoinCandidate > 0) candidate = "+"; 1522 pw.printf(" %17s %9d %5d %3d.%03d%s %-32s %s\n", 1523 r.BSSID, 1524 r.frequency, 1525 r.level, 1526 ageSec, ageMilli, 1527 candidate, 1528 r.SSID == null ? "" : r.SSID, 1529 r.capabilities); 1530 } 1531 } 1532 pw.println(); 1533 pw.println("Locks held:"); 1534 mWifiLockManager.dump(pw); 1535 pw.println(); 1536 mWifiMulticastLockManager.dump(pw); 1537 pw.println(); 1538 mWifiStateMachine.dump(fd, pw, args); 1539 pw.println(); 1540 mWifiBackupRestore.dump(fd, pw, args); 1541 pw.println(); 1542 } 1543 } 1544 1545 @Override 1546 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) { 1547 if (mWifiLockManager.acquireWifiLock(lockMode, tag, binder, ws)) { 1548 mWifiController.sendMessage(CMD_LOCKS_CHANGED); 1549 return true; 1550 } 1551 return false; 1552 } 1553 1554 @Override 1555 public void updateWifiLockWorkSource(IBinder binder, WorkSource ws) { 1556 mWifiLockManager.updateWifiLockWorkSource(binder, ws); 1557 } 1558 1559 @Override 1560 public boolean releaseWifiLock(IBinder binder) { 1561 if (mWifiLockManager.releaseWifiLock(binder)) { 1562 mWifiController.sendMessage(CMD_LOCKS_CHANGED); 1563 return true; 1564 } 1565 return false; 1566 } 1567 1568 @Override 1569 public void initializeMulticastFiltering() { 1570 enforceMulticastChangePermission(); 1571 mWifiMulticastLockManager.initializeFiltering(); 1572 } 1573 1574 @Override 1575 public void acquireMulticastLock(IBinder binder, String tag) { 1576 enforceMulticastChangePermission(); 1577 mWifiMulticastLockManager.acquireLock(binder, tag); 1578 } 1579 1580 @Override 1581 public void releaseMulticastLock() { 1582 enforceMulticastChangePermission(); 1583 mWifiMulticastLockManager.releaseLock(); 1584 } 1585 1586 @Override 1587 public boolean isMulticastEnabled() { 1588 enforceAccessPermission(); 1589 return mWifiMulticastLockManager.isMulticastEnabled(); 1590 } 1591 1592 @Override 1593 public void enableVerboseLogging(int verbose) { 1594 enforceAccessPermission(); 1595 mFacade.setIntegerSetting( 1596 mContext, Settings.Global.WIFI_VERBOSE_LOGGING_ENABLED, verbose); 1597 enableVerboseLoggingInternal(verbose); 1598 } 1599 1600 private void enableVerboseLoggingInternal(int verbose) { 1601 mWifiStateMachine.enableVerboseLogging(verbose); 1602 mWifiLockManager.enableVerboseLogging(verbose); 1603 mWifiMulticastLockManager.enableVerboseLogging(verbose); 1604 mWifiInjector.getWifiLastResortWatchdog().enableVerboseLogging(verbose); 1605 mWifiInjector.getWifiBackupRestore().enableVerboseLogging(verbose); 1606 } 1607 1608 @Override 1609 public int getVerboseLoggingLevel() { 1610 enforceAccessPermission(); 1611 return mFacade.getIntegerSetting( 1612 mContext, Settings.Global.WIFI_VERBOSE_LOGGING_ENABLED, 0); 1613 } 1614 1615 @Override 1616 public void enableAggressiveHandover(int enabled) { 1617 enforceAccessPermission(); 1618 mWifiStateMachine.enableAggressiveHandover(enabled); 1619 } 1620 1621 @Override 1622 public int getAggressiveHandover() { 1623 enforceAccessPermission(); 1624 return mWifiStateMachine.getAggressiveHandover(); 1625 } 1626 1627 @Override 1628 public void setAllowScansWithTraffic(int enabled) { 1629 enforceAccessPermission(); 1630 mWifiStateMachine.setAllowScansWithTraffic(enabled); 1631 } 1632 1633 @Override 1634 public int getAllowScansWithTraffic() { 1635 enforceAccessPermission(); 1636 return mWifiStateMachine.getAllowScansWithTraffic(); 1637 } 1638 1639 @Override 1640 public boolean setEnableAutoJoinWhenAssociated(boolean enabled) { 1641 enforceChangePermission(); 1642 return mWifiStateMachine.setEnableAutoJoinWhenAssociated(enabled); 1643 } 1644 1645 @Override 1646 public boolean getEnableAutoJoinWhenAssociated() { 1647 enforceAccessPermission(); 1648 return mWifiStateMachine.getEnableAutoJoinWhenAssociated(); 1649 } 1650 1651 /* Return the Wifi Connection statistics object */ 1652 @Override 1653 public WifiConnectionStatistics getConnectionStatistics() { 1654 enforceAccessPermission(); 1655 enforceReadCredentialPermission(); 1656 if (mWifiStateMachineChannel != null) { 1657 return mWifiStateMachine.syncGetConnectionStatistics(mWifiStateMachineChannel); 1658 } else { 1659 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1660 return null; 1661 } 1662 } 1663 1664 @Override 1665 public void factoryReset() { 1666 enforceConnectivityInternalPermission(); 1667 1668 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { 1669 return; 1670 } 1671 1672 if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { 1673 // Turn mobile hotspot off 1674 setWifiApEnabled(null, false); 1675 } 1676 1677 if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI)) { 1678 // Enable wifi 1679 setWifiEnabled(true); 1680 // Delete all Wifi SSIDs 1681 List<WifiConfiguration> networks = getConfiguredNetworks(); 1682 if (networks != null) { 1683 for (WifiConfiguration config : networks) { 1684 removeNetwork(config.networkId); 1685 } 1686 saveConfiguration(); 1687 } 1688 } 1689 } 1690 1691 /* private methods */ 1692 static boolean logAndReturnFalse(String s) { 1693 Log.d(TAG, s); 1694 return false; 1695 } 1696 1697 public static boolean isValid(WifiConfiguration config) { 1698 String validity = checkValidity(config); 1699 return validity == null || logAndReturnFalse(validity); 1700 } 1701 1702 public static boolean isValidPasspoint(WifiConfiguration config) { 1703 String validity = checkPasspointValidity(config); 1704 return validity == null || logAndReturnFalse(validity); 1705 } 1706 1707 public static String checkValidity(WifiConfiguration config) { 1708 if (config.allowedKeyManagement == null) 1709 return "allowed kmgmt"; 1710 1711 if (config.allowedKeyManagement.cardinality() > 1) { 1712 if (config.allowedKeyManagement.cardinality() != 2) { 1713 return "cardinality != 2"; 1714 } 1715 if (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)) { 1716 return "not WPA_EAP"; 1717 } 1718 if ((!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) 1719 && (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK))) { 1720 return "not PSK or 8021X"; 1721 } 1722 } 1723 return null; 1724 } 1725 1726 public static String checkPasspointValidity(WifiConfiguration config) { 1727 if (!TextUtils.isEmpty(config.FQDN)) { 1728 /* this is passpoint configuration; it must not have an SSID */ 1729 if (!TextUtils.isEmpty(config.SSID)) { 1730 return "SSID not expected for Passpoint: '" + config.SSID + 1731 "' FQDN " + toHexString(config.FQDN); 1732 } 1733 /* this is passpoint configuration; it must have a providerFriendlyName */ 1734 if (TextUtils.isEmpty(config.providerFriendlyName)) { 1735 return "no provider friendly name"; 1736 } 1737 WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig; 1738 /* this is passpoint configuration; it must have enterprise config */ 1739 if (enterpriseConfig == null 1740 || enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.NONE ) { 1741 return "no enterprise config"; 1742 } 1743 if ((enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TLS || 1744 enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TTLS || 1745 enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.PEAP) && 1746 enterpriseConfig.getCaCertificate() == null) { 1747 return "no CA certificate"; 1748 } 1749 } 1750 return null; 1751 } 1752 1753 @Override 1754 public Network getCurrentNetwork() { 1755 enforceAccessPermission(); 1756 return mWifiStateMachine.getCurrentNetwork(); 1757 } 1758 1759 public static String toHexString(String s) { 1760 if (s == null) { 1761 return "null"; 1762 } 1763 StringBuilder sb = new StringBuilder(); 1764 sb.append('\'').append(s).append('\''); 1765 for (int n = 0; n < s.length(); n++) { 1766 sb.append(String.format(" %02x", s.charAt(n) & 0xffff)); 1767 } 1768 return sb.toString(); 1769 } 1770 1771 /** 1772 * Checks that calling process has android.Manifest.permission.ACCESS_COARSE_LOCATION or 1773 * android.Manifest.permission.ACCESS_FINE_LOCATION and a corresponding app op is allowed 1774 */ 1775 private boolean checkCallerCanAccessScanResults(String callingPackage, int uid) { 1776 if (ActivityManager.checkUidPermission(Manifest.permission.ACCESS_FINE_LOCATION, uid) 1777 == PackageManager.PERMISSION_GRANTED 1778 && checkAppOppAllowed(AppOpsManager.OP_FINE_LOCATION, callingPackage, uid)) { 1779 return true; 1780 } 1781 1782 if (ActivityManager.checkUidPermission(Manifest.permission.ACCESS_COARSE_LOCATION, uid) 1783 == PackageManager.PERMISSION_GRANTED 1784 && checkAppOppAllowed(AppOpsManager.OP_COARSE_LOCATION, callingPackage, uid)) { 1785 return true; 1786 } 1787 boolean apiLevel23App = isMApp(mContext, callingPackage); 1788 // Pre-M apps running in the foreground should continue getting scan results 1789 if (!apiLevel23App && isForegroundApp(callingPackage)) { 1790 return true; 1791 } 1792 Log.e(TAG, "Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION " 1793 + "permission to get scan results"); 1794 return false; 1795 } 1796 1797 private boolean checkAppOppAllowed(int op, String callingPackage, int uid) { 1798 return mAppOps.noteOp(op, uid, callingPackage) == AppOpsManager.MODE_ALLOWED; 1799 } 1800 1801 private static boolean isMApp(Context context, String pkgName) { 1802 try { 1803 return context.getPackageManager().getApplicationInfo(pkgName, 0) 1804 .targetSdkVersion >= Build.VERSION_CODES.M; 1805 } catch (PackageManager.NameNotFoundException e) { 1806 // In case of exception, assume M app (more strict checking) 1807 } 1808 return true; 1809 } 1810 1811 public void hideCertFromUnaffiliatedUsers(String alias) { 1812 mCertManager.hideCertFromUnaffiliatedUsers(alias); 1813 } 1814 1815 public String[] listClientCertsForCurrentUser() { 1816 return mCertManager.listClientCertsForCurrentUser(); 1817 } 1818 1819 /** 1820 * Return true if the specified package name is a foreground app. 1821 * 1822 * @param pkgName application package name. 1823 */ 1824 private boolean isForegroundApp(String pkgName) { 1825 ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE); 1826 List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); 1827 return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName()); 1828 } 1829 1830 /** 1831 * Enable/disable WifiConnectivityManager at runtime 1832 * 1833 * @param enabled true-enable; false-disable 1834 */ 1835 @Override 1836 public void enableWifiConnectivityManager(boolean enabled) { 1837 enforceConnectivityInternalPermission(); 1838 mWifiStateMachine.enableWifiConnectivityManager(enabled); 1839 } 1840 1841 /** 1842 * Retrieve the data to be backed to save the current state. 1843 * 1844 * @return Raw byte stream of the data to be backed up. 1845 */ 1846 @Override 1847 public byte[] retrieveBackupData() { 1848 enforceReadCredentialPermission(); 1849 enforceAccessPermission(); 1850 if (mWifiStateMachineChannel == null) { 1851 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1852 return null; 1853 } 1854 1855 Slog.d(TAG, "Retrieving backup data"); 1856 List<WifiConfiguration> wifiConfigurations = 1857 mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel); 1858 byte[] backupData = 1859 mWifiBackupRestore.retrieveBackupDataFromConfigurations(wifiConfigurations); 1860 Slog.d(TAG, "Retrieved backup data"); 1861 return backupData; 1862 } 1863 1864 /** 1865 * Helper method to restore networks retrieved from backup data. 1866 * 1867 * @param configurations list of WifiConfiguration objects parsed from the backup data. 1868 */ 1869 private void restoreNetworks(List<WifiConfiguration> configurations) { 1870 if (configurations == null) { 1871 Slog.e(TAG, "Backup data parse failed"); 1872 return; 1873 } 1874 for (WifiConfiguration configuration : configurations) { 1875 int networkId = mWifiStateMachine.syncAddOrUpdateNetwork( 1876 mWifiStateMachineChannel, configuration); 1877 if (networkId == WifiConfiguration.INVALID_NETWORK_ID) { 1878 Slog.e(TAG, "Restore network failed: " + configuration.configKey()); 1879 continue; 1880 } 1881 // Enable all networks restored. 1882 mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, networkId, false); 1883 } 1884 } 1885 1886 /** 1887 * Restore state from the backed up data. 1888 * 1889 * @param data Raw byte stream of the backed up data. 1890 */ 1891 @Override 1892 public void restoreBackupData(byte[] data) { 1893 enforceChangePermission(); 1894 if (mWifiStateMachineChannel == null) { 1895 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1896 return; 1897 } 1898 1899 Slog.d(TAG, "Restoring backup data"); 1900 List<WifiConfiguration> wifiConfigurations = 1901 mWifiBackupRestore.retrieveConfigurationsFromBackupData(data); 1902 restoreNetworks(wifiConfigurations); 1903 Slog.d(TAG, "Restored backup data"); 1904 } 1905 1906 /** 1907 * Restore state from the older supplicant back up data. 1908 * The old backup data was essentially a backup of wpa_supplicant.conf & ipconfig.txt file. 1909 * 1910 * @param supplicantData Raw byte stream of wpa_supplicant.conf 1911 * @param ipConfigData Raw byte stream of ipconfig.txt 1912 */ 1913 public void restoreSupplicantBackupData(byte[] supplicantData, byte[] ipConfigData) { 1914 enforceChangePermission(); 1915 if (mWifiStateMachineChannel == null) { 1916 Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); 1917 return; 1918 } 1919 1920 Slog.d(TAG, "Restoring supplicant backup data"); 1921 List<WifiConfiguration> wifiConfigurations = 1922 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 1923 supplicantData, ipConfigData); 1924 restoreNetworks(wifiConfigurations); 1925 Slog.d(TAG, "Restored supplicant backup data"); 1926 } 1927} 1928