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