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