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