WifiServiceImpl.java revision d03a9283285bb7adef6c687eb3a91fa4a8c4b502
1155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/*
2155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Copyright (C) 2010 The Android Open Source Project
3155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
4155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Licensed under the Apache License, Version 2.0 (the "License");
5155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * you may not use this file except in compliance with the License.
6155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * You may obtain a copy of the License at
7155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
8155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *      http://www.apache.org/licenses/LICENSE-2.0
9155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
10155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Unless required by applicable law or agreed to in writing, software
11155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * distributed under the License is distributed on an "AS IS" BASIS,
12155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * See the License for the specific language governing permissions and
14155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * limitations under the License.
15155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
16155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepackage com.android.server.wifi;
18155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
199086afccf6938a49eb9a2cd248917c1cb0943942vandwalleimport android.Manifest;
20155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.app.ActivityManager;
21155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.app.AppOpsManager;
22155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.bluetooth.BluetoothAdapter;
23155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.BroadcastReceiver;
24155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.Context;
25155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.Intent;
26155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.IntentFilter;
27155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.pm.PackageManager;
28fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghinaimport android.content.pm.UserInfo;
29155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.database.ContentObserver;
3034f173a54307c39ffe5dfab52e7ef8166a00539eRobert Greenwaltimport android.net.ConnectivityManager;
31155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.DhcpInfo;
32155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.DhcpResults;
333ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.net.IpConfiguration.ProxySettings;
34155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.LinkAddress;
359846078598c3468f8813dbfa58238a1846bd81efSanket Padaweimport android.net.Network;
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.NetworkUtils;
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.RouteInfo;
38e487a4648dd41881e754f1224aaedead78a0777dSky Faberimport android.net.Uri;
39c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalleimport android.net.wifi.*;
403ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.net.wifi.IWifiManager;
413ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.os.AsyncTask;
42155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Binder;
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Handler;
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.HandlerThread;
45155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.IBinder;
46155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Message;
473ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.os.Messenger;
4803b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackbornimport android.os.PowerManager;
49155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.RemoteException;
50186f7513a080eb1a419b6d83286d62961168a5feAdam Lesinskiimport android.os.SystemClock;
51155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.SystemProperties;
52155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.UserHandle;
53fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghinaimport android.os.UserManager;
54155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.WorkSource;
55155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.provider.Settings;
564d381bc39f5263effdae73ec99065eb299b806caVinit Deshpandeimport android.text.TextUtils;
57992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalleimport android.util.Log;
58155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Slog;
59155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
60155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileNotFoundException;
61155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.BufferedReader;
62155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileDescriptor;
63155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileReader;
64155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.IOException;
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.PrintWriter;
66155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.lang.Override;
67155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.net.InetAddress;
68155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.net.Inet4Address;
69a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport java.net.URISyntaxException;
70a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport java.security.GeneralSecurityException;
7131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.KeyStore;
7231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertPath;
7331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertPathValidator;
7431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertPathValidatorException;
7531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertificateFactory;
7631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.PKIXParameters;
7731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.X509Certificate;
78155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
7931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.util.Arrays;
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.R;
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.app.IBatteryStats;
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.telephony.TelephonyIntents;
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.util.AsyncChannel;
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.server.am.BatteryStatsService;
87a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport com.android.server.wifi.configparse.ConfigBuilder;
88a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist
89a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport org.xml.sax.SAXException;
90a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
91155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_AIRPLANE_TOGGLED;
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_BATTERY_CHANGED;
93155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_EMERGENCY_MODE_CHANGED;
94155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_LOCKS_CHANGED;
95155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCAN_ALWAYS_MODE_CHANGED;
96155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCREEN_OFF;
97155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCREEN_ON;
98155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SET_AP;
99155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_USER_PRESENT;
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED;
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * WifiService handles remote WiFi operation requests by implementing
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * the IWifiManager interface.
104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * @hide
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic final class WifiServiceImpl extends IWifiManager.Stub {
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final String TAG = "WifiService";
109f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final boolean DBG = true;
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final WifiStateMachine mWifiStateMachine;
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final Context mContext;
114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final LockList mLocks = new LockList();
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // some wifi lock statistics
117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullHighPerfLocksAcquired;
118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullHighPerfLocksReleased;
119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullLocksAcquired;
120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullLocksReleased;
121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mScanLocksAcquired;
122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mScanLocksReleased;
123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final List<Multicaster> mMulticasters =
125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            new ArrayList<Multicaster>();
126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mMulticastEnabled;
127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mMulticastDisabled;
128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final IBatteryStats mBatteryStats;
13003b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn    private final PowerManager mPowerManager;
131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final AppOpsManager mAppOps;
132f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott    private final UserManager mUserManager;
133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String mInterfaceName;
135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
136ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle    // Debug counter tracking scan requests sent by WifiManager
137ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle    private int scanRequestCounter = 0;
138ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle
139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Tracks the open wi-fi network notification */
140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiNotificationController mNotificationController;
141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Polls traffic stats and notifies clients */
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiTrafficPoller mTrafficPoller;
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Tracks the persisted states for wi-fi & airplane mode */
144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final WifiSettingsStore mSettingsStore;
145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Asynchronous channel to WifiStateMachine
148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private AsyncChannel mWifiStateMachineChannel;
150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Handles client connections
153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class ClientHandler extends Handler {
155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        ClientHandler(android.os.Looper looper) {
157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(looper);
158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void handleMessage(Message msg) {
162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch (msg.what) {
163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "New client listening to asynchronous messages");
166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        // We track the clients by the Messenger
167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        // since it is expected to be always available
168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mTrafficPoller.addClient(msg.replyTo);
169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Send failed, client connection lost");
177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mTrafficPoller.removeClient(msg.replyTo);
181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    AsyncChannel ac = new AsyncChannel();
185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    ac.connect(mContext, this, msg.replyTo);
186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                /* Client commands are forwarded to state machine */
189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.CONNECT_NETWORK:
190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.SAVE_NETWORK: {
191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    WifiConfiguration config = (WifiConfiguration) msg.obj;
192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    int networkId = msg.arg1;
193992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (msg.what == WifiManager.SAVE_NETWORK) {
194992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        Slog.e("WiFiServiceImpl ", "SAVE"
195992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + " nid=" + Integer.toString(networkId)
196e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " uid=" + msg.sendingUid
197e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " name="
198e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + mContext.getPackageManager().getNameForUid(msg.sendingUid));
199992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
200992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (msg.what == WifiManager.CONNECT_NETWORK) {
201992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        Slog.e("WiFiServiceImpl ", "CONNECT "
202992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + " nid=" + Integer.toString(networkId)
203e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " uid=" + msg.sendingUid
204e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " name="
205e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + mContext.getPackageManager().getNameForUid(msg.sendingUid));
206992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
207e487a4648dd41881e754f1224aaedead78a0777dSky Faber
208d03a9283285bb7adef6c687eb3a91fa4a8c4b502Vinit Deshpande                    if (config != null && isValid(config)) {
20964c98f4afda6c7970c5d563580165a308d5e7c15Jason Monk                        if (DBG) Slog.d(TAG, "Connect with config" + config);
21064c98f4afda6c7970c5d563580165a308d5e7c15Jason Monk                        mWifiStateMachine.sendMessage(Message.obtain(msg));
211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else if (config == null
212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            && networkId != WifiConfiguration.INVALID_NETWORK_ID) {
213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Connect with networkId" + networkId);
214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachine.sendMessage(Message.obtain(msg));
215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "ClientHandler.handleMessage ignoring invalid msg=" + msg);
217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (msg.what == WifiManager.CONNECT_NETWORK) {
218c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                            replyFailed(msg, WifiManager.CONNECT_NETWORK_FAILED,
219c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                                    WifiManager.INVALID_ARGS);
220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        } else {
221c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                            replyFailed(msg, WifiManager.SAVE_NETWORK_FAILED,
222c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                                    WifiManager.INVALID_ARGS);
223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.FORGET_NETWORK:
2282a325cd07d40941adb93c1284abb263ba650c213Sky Faber                    if (isOwner(msg.sendingUid)) {
229002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                        mWifiStateMachine.sendMessage(Message.obtain(msg));
230002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    } else {
231002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                        Slog.e(TAG, "Forget is not authorized for user");
232002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                        replyFailed(msg, WifiManager.FORGET_NETWORK_FAILED,
233002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                                WifiManager.NOT_AUTHORIZED);
234002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    }
235002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    break;
236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.START_WPS:
237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.CANCEL_WPS:
238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.DISABLE_NETWORK:
239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.RSSI_PKTCNT_FETCH: {
240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWifiStateMachine.sendMessage(Message.obtain(msg));
241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                default: {
244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.d(TAG, "ClientHandler.handleMessage ignoring msg=" + msg);
245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
250c35d728a15e9270f5642ef79f5245c89d749285fSky Faber        private void replyFailed(Message msg, int what, int why) {
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Message reply = msg.obtain();
252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            reply.what = what;
253c35d728a15e9270f5642ef79f5245c89d749285fSky Faber            reply.arg1 = why;
254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                msg.replyTo.send(reply);
256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (RemoteException e) {
257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // There's not much we can do if reply can't be sent!
258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private ClientHandler mClientHandler;
262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Handles interaction with WifiStateMachine
265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class WifiStateMachineHandler extends Handler {
267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private AsyncChannel mWsmChannel;
268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiStateMachineHandler(android.os.Looper looper) {
270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(looper);
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWsmChannel = new AsyncChannel();
272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void handleMessage(Message msg) {
277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch (msg.what) {
278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachineChannel = mWsmChannel;
281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachineChannel = null;
284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1);
289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWifiStateMachineChannel = null;
290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    //Re-establish connection to state machine
291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                default: {
295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    WifiStateMachineHandler mWifiStateMachineHandler;
303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiWatchdogStateMachine mWifiWatchdogStateMachine;
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiController mWifiController;
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiServiceImpl(Context context) {
309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext = context;
310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName =  SystemProperties.get("wifi.interface", "wlan0");
312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
313f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mTrafficPoller = new WifiTrafficPoller(mContext, mInterfaceName);
314f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName, mTrafficPoller);
315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.enableRssiPolling(true);
316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mBatteryStats = BatteryStatsService.getService();
31703b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        mPowerManager = context.getSystemService(PowerManager.class);
318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
319f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        mUserManager = UserManager.get(mContext);
320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mNotificationController = new WifiNotificationController(mContext, mWifiStateMachine);
322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mSettingsStore = new WifiSettingsStore(mContext);
323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        HandlerThread wifiThread = new HandlerThread("WifiService");
325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        wifiThread.start();
326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mClientHandler = new ClientHandler(wifiThread.getLooper());
327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiController = new WifiController(mContext, this, wifiThread.getLooper());
3291c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    }
3301c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde
3311c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde
3321c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    /**
3331c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * Check if Wi-Fi needs to be enabled and start
3341c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * if needed
3351c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     *
3361c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * This function is used only at boot time
3371c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     */
3381c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    public void checkAndStartWifi() {
3391c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        /* Check if wi-fi needs to be enabled */
3401c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled();
3411c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        Slog.i(TAG, "WifiService starting up with Wi-Fi " +
3421c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde                (wifiEnabled ? "enabled" : "disabled"));
343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerForScanModeChange();
345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.registerReceiver(
346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                new BroadcastReceiver() {
347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    @Override
348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    public void onReceive(Context context, Intent intent) {
349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (mSettingsStore.handleAirplaneModeToggled()) {
350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            mWifiController.sendMessage(CMD_AIRPLANE_TOGGLED);
351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                },
354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Adding optimizations of only receiving broadcasts when wifi is enabled
357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // can result in race conditions when apps toggle wifi in the background
358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // without active user involvement. Always receive broadcasts.
359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerForBroadcasts();
360faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        registerForPackageOrUserRemoval();
36103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        mInIdleMode = mPowerManager.isDeviceIdleMode();
362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3631c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        mWifiController.start();
364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // If we are already disabled (could be due to airplane mode), avoid changing persist
366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // state here
367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (wifiEnabled) setWifiEnabled(wifiEnabled);
368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiWatchdogStateMachine = WifiWatchdogStateMachine.
370569b093c4afd30787308032c8259af2680a2874fNavtej Singh Mann               makeWifiWatchdogStateMachine(mContext, mWifiStateMachine.getMessenger());
371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#pingSupplicant()}
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeds, {@code false} otherwise
376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean pingSupplicant() {
378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
388a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * see {@link android.net.wifi.WifiManager#getChannelList}
389a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     */
390a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public List<WifiChannel> getChannelList() {
391a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        enforceAccessPermission();
392a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        if (mWifiStateMachineChannel != null) {
393a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            return mWifiStateMachine.syncGetChannelList(mWifiStateMachineChannel);
394a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        } else {
395a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
396a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            return null;
397a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        }
398a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
399a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
4009086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    // Start a location scan.
4019086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    // L release: A location scan is implemented as a normal scan and avoids scanning DFS channels
4021227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande    // Deprecated: Will soon remove implementation
4039086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    public void startLocationRestrictedScan(WorkSource workSource) {
4049086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        enforceChangePermission();
4059086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        enforceLocationHardwarePermission();
4069086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        List<WifiChannel> channels = getChannelList();
4079086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        if (channels == null) {
4089086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            Slog.e(TAG, "startLocationRestrictedScan cant get channels");
4099086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            return;
4109086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        }
4119086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        ScanSettings settings = new ScanSettings();
4129086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        for (WifiChannel channel : channels) {
4139086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            if (!channel.isDFS) {
4149086afccf6938a49eb9a2cd248917c1cb0943942vandwalle                settings.channelSet.add(channel);
4159086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            }
4169086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        }
4179086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        if (workSource == null) {
4189086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            // Make sure we always have a workSource indicating the origin of the scan
4199086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            // hence if there is none, pick an internal WifiStateMachine one
4209086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            workSource = new WorkSource(WifiStateMachine.DFS_RESTRICTED_SCAN_REQUEST);
4219086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        }
4229086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        startScan(settings, workSource);
4239086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    }
4249086afccf6938a49eb9a2cd248917c1cb0943942vandwalle
425a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    /**
426a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * see {@link android.net.wifi.WifiManager#startScan}
427a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * and {@link android.net.wifi.WifiManager#startCustomizedScan}
428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
429a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * @param settings If null, use default parameter, i.e. full scan.
430a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * @param workSource If null, all blame is given to the calling uid.
431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
432a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public void startScan(ScanSettings settings, WorkSource workSource) {
433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
4341227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande        synchronized (this) {
43503b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            if (mInIdleMode) {
43649dfc6768889a33eb67102b16faf21667af2a60dVinit Deshpande                // Need to send an immediate scan result broadcast in case the
43749dfc6768889a33eb67102b16faf21667af2a60dVinit Deshpande                // caller is waiting for a result ..
43849dfc6768889a33eb67102b16faf21667af2a60dVinit Deshpande                mWifiStateMachine.sendScanResultsAvailableBroadcast(/* scanSucceeded = */ false);
43903b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                mScanPending = true;
44003b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                return;
44103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            }
44203b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        }
443a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        if (settings != null) {
444a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            settings = new ScanSettings(settings);
445a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (!settings.isValid()) {
446a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng                Slog.e(TAG, "invalid scan setting");
447a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng                return;
448a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            }
449a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        }
450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (workSource != null) {
451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            enforceWorkSourcePermission();
452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // WifiManager currently doesn't use names, so need to clear names out of the
453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // supplied WorkSource to allow future WorkSource combining.
454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            workSource.clearNames();
455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
456ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle        mWifiStateMachine.startScan(Binder.getCallingUid(), scanRequestCounter++,
457ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle                settings, workSource);
458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isBatchedScanSupported() {
461ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande        return false;
462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
464ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande    public void pollBatchedScan() { }
465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4663f7ef65ab71619040032aee96b5599849881d6fdAndres Morales    public String getWpsNfcConfigurationToken(int netId) {
467f3f4029b3ac41da4cc6a1bc22bae47c750a47048Andres Morales        enforceConnectivityInternalPermission();
4683f7ef65ab71619040032aee96b5599849881d6fdAndres Morales        return mWifiStateMachine.syncGetWpsNfcConfigurationToken(netId);
4693f7ef65ab71619040032aee96b5599849881d6fdAndres Morales    }
4703f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#requestBatchedScan()}
473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean requestBatchedScan(BatchedScanSettings requested, IBinder binder,
475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WorkSource workSource) {
476ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande        return false;
477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<BatchedScanResult> getBatchedScanResults(String callingPackage) {
480ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande        return null;
481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
483ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande    public void stopBatchedScan(BatchedScanSettings settings) { }
484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4851227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande    boolean mInIdleMode;
4861227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande    boolean mScanPending;
4871227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande
48803b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn    void handleIdleModeChanged() {
48903b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        boolean doScan = false;
4901227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande        synchronized (this) {
49103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            boolean idle = mPowerManager.isDeviceIdleMode();
49203b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            if (mInIdleMode != idle) {
49303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                mInIdleMode = idle;
49403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                if (!idle) {
49503b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                    if (mScanPending) {
49603b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                        mScanPending = false;
49703b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                        doScan = true;
49803b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                    }
49903b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                }
50003b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            }
50103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        }
50203b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        if (doScan) {
50303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            // Someone requested a scan while we were idle; do a full scan now.
50403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            startScan(null, null);
50503b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        }
50603b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn    }
50703b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn
508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceAccessPermission() {
509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
510a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande                "WifiService");
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceChangePermission() {
514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
51531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                "WifiService");
5169086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    }
517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5189086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    private void enforceLocationHardwarePermission() {
5199086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        mContext.enforceCallingOrSelfPermission(Manifest.permission.LOCATION_HARDWARE,
5209086afccf6938a49eb9a2cd248917c1cb0943942vandwalle                "LocationHardware");
521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5239878c61bbd81176561991be025af44efc67332feWenchao Tong    private void enforceReadCredentialPermission() {
5249878c61bbd81176561991be025af44efc67332feWenchao Tong        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.READ_WIFI_CREDENTIAL,
5259878c61bbd81176561991be025af44efc67332feWenchao Tong                                                "WifiService");
5269878c61bbd81176561991be025af44efc67332feWenchao Tong    }
5279878c61bbd81176561991be025af44efc67332feWenchao Tong
528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceWorkSourcePermission() {
529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingPermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
530a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande                "WifiService");
531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceMulticastChangePermission() {
535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                "WifiService");
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceConnectivityInternalPermission() {
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(
542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                android.Manifest.permission.CONNECTIVITY_INTERNAL,
543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                "ConnectivityService");
544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enable {@code true} to enable, {@code false} to disable.
549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the enable/disable operation was
550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         started or is already in the queue.
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public synchronized boolean setWifiEnabled(boolean enable) {
553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid()
555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + ", uid=" + Binder.getCallingUid());
556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) {
557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * Caller might not have WRITE_SECURE_SETTINGS,
562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * only CHANGE_WIFI_STATE is enforced
563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        */
564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (! mSettingsStore.handleWifiToggled(enable)) {
568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Nothing to do if wifi cannot be toggled
569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return true;
570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiController.sendMessage(CMD_WIFI_TOGGLED);
576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiState()}
581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_DISABLING},
583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_ENABLED},
584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_ENABLING},
585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_UNKNOWN}
586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getWifiEnabledState() {
588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiState();
590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param wifiConfig SSID, security and channel details as
595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *        part of WifiConfiguration
596155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enabled true to enable and false to disable
597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
599328fc5407927799843c11f2b767a8cf47b89f366Robert Greenwalt        enforceChangePermission();
60034f173a54307c39ffe5dfab52e7ef8166a00539eRobert Greenwalt        ConnectivityManager.enforceTetherChangePermission(mContext);
601f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
60213cddb5337418359eb5f9bebd0504fbc5c47fc41Julia Reynolds            throw new SecurityException("DISALLOW_CONFIG_TETHERING is enabled for this user.");
60313cddb5337418359eb5f9bebd0504fbc5c47fc41Julia Reynolds        }
604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // null wifiConfig is a meaningful input for CMD_SET_AP
605d03a9283285bb7adef6c687eb3a91fa4a8c4b502Vinit Deshpande        if (wifiConfig == null || isValid(wifiConfig)) {
606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiController.obtainMessage(CMD_SET_AP, enabled ? 1 : 0, 0, wifiConfig).sendToTarget();
607155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invalid WifiConfiguration");
609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiApState()}
614155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
615155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_DISABLING},
616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_ENABLED},
617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_ENABLING},
618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_FAILED}
619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getWifiApEnabledState() {
621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
622155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiApState();
623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
624155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
625155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiApConfiguration()}
627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return soft access point configuration
628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiConfiguration getWifiApConfiguration() {
630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiApConfiguration();
632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
635a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist     * see {@link WifiManager#buildWifiConfig()}
636a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist     * @return a WifiConfiguration.
637a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist     */
638a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist    public WifiConfiguration buildWifiConfig(String uriString, String mimeType, byte[] data) {
6394e940bb499f65b4305dbeb2e01237c43c2b0b42dJan Nordqvist        if (mimeType.equals(ConfigBuilder.WifiConfigType)) {
640a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            try {
641a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist                return ConfigBuilder.buildConfig(uriString, data, mContext);
642a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            }
643a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            catch (IOException | GeneralSecurityException | SAXException e) {
644a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist                Log.e(TAG, "Failed to parse wi-fi configuration: " + e);
645a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            }
646a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        }
647a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        else {
648a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            Log.i(TAG, "Unknown wi-fi config type: " + mimeType);
649a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        }
650a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        return null;
651a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist    }
652a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist
653a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist    /**
654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param wifiConfig WifiConfiguration details for soft access point
656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (wifiConfig == null)
660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
661d03a9283285bb7adef6c687eb3a91fa4a8c4b502Vinit Deshpande        if (isValid(wifiConfig)) {
662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.setWifiApConfiguration(wifiConfig);
663155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
664155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invalid WifiConfiguration");
665155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
667155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enable {@code true} to enable, {@code false} to disable.
670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the enable/disable operation was
671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         started or is already in the queue.
672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isScanAlwaysAvailable() {
674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mSettingsStore.isScanAlwaysAvailable();
676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#disconnect()}
680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void disconnect() {
682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.disconnectCommand();
684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#reconnect()}
688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void reconnect() {
690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reconnectCommand();
692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
693155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#reassociate()}
696155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void reassociate() {
698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reassociateCommand();
700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
703048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande     * see {@link android.net.wifi.WifiManager#getSupportedFeatures}
704a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande     */
705048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande    public int getSupportedFeatures() {
706a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        enforceAccessPermission();
707a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        if (mWifiStateMachineChannel != null) {
708048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            return mWifiStateMachine.syncGetSupportedFeatures(mWifiStateMachineChannel);
709a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        } else {
710a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
711048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            return 0;
712a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        }
713a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
714a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande
715a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    /**
716200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle     * see {@link android.net.wifi.WifiAdapter#reportActivityInfo}
717200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle     */
718048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande    public WifiActivityEnergyInfo reportActivityInfo() {
719200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        enforceAccessPermission();
720200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        WifiLinkLayerStats stats;
721200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        WifiActivityEnergyInfo energyInfo = null;
722200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (mWifiStateMachineChannel != null) {
723048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            stats = mWifiStateMachine.syncGetLinkLayerStats(mWifiStateMachineChannel);
724200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            if (stats != null) {
72584a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle
72684a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int rxIdleCurrent =
72784a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                     mContext.getResources().getInteger(
7282740001002132ed305034de64a58b0805fa4e392Pierre Vandwalle                             com.android.internal.R.integer.config_wifi_idle_receive_cur_ma);
72984a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int rxCurrent =
73084a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                     mContext.getResources().getInteger(
73184a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                             com.android.internal.R.integer.config_wifi_active_rx_cur_ma);
73284a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int txCurrent =
73384a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                     mContext.getResources().getInteger(
73484a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                             com.android.internal.R.integer.config_wifi_tx_cur_ma);
73584a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int voltage =
73684a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                     mContext.getResources().getInteger(
73784a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                             com.android.internal.R.integer.config_wifi_operating_voltage_mv);
73884a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int rxIdleTime = stats.on_time - stats.tx_time - stats.rx_time;
73984a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle
74084a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int energyUsed = (stats.tx_time * txCurrent + stats.rx_time * rxCurrent
74184a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                        + rxIdleTime * rxIdleCurrent ) * voltage / 1000;
74284a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle
743200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle                // Convert the LinkLayerStats into EnergyActivity
744186f7513a080eb1a419b6d83286d62961168a5feAdam Lesinski                energyInfo = new WifiActivityEnergyInfo(SystemClock.elapsedRealtime(),
745200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle                        WifiActivityEnergyInfo.STACK_STATE_STATE_IDLE, stats.tx_time,
74684a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                        stats.rx_time, rxIdleTime, energyUsed);
747200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            }
748200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            return energyInfo;
749200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        } else {
750200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
751200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            return null;
752200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        }
753200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle    }
754200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle
755200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle    /**
756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the list of configured networks
758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<WifiConfiguration> getConfiguredNetworks() {
760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
76282199a285f4a45a46b44eb8253999aa918534753vandwalle            return mWifiStateMachine.syncGetConfiguredNetworks(Binder.getCallingUid(),
76382199a285f4a45a46b44eb8253999aa918534753vandwalle                    mWifiStateMachineChannel);
764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return null;
767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
7719878c61bbd81176561991be025af44efc67332feWenchao Tong     * see {@link android.net.wifi.WifiManager#getPrivilegedConfiguredNetworks()}
7729878c61bbd81176561991be025af44efc67332feWenchao Tong     * @return the list of configured networks with real preSharedKey
7739878c61bbd81176561991be025af44efc67332feWenchao Tong     */
7749878c61bbd81176561991be025af44efc67332feWenchao Tong    public List<WifiConfiguration> getPrivilegedConfiguredNetworks() {
7759878c61bbd81176561991be025af44efc67332feWenchao Tong        enforceReadCredentialPermission();
7769878c61bbd81176561991be025af44efc67332feWenchao Tong        enforceAccessPermission();
7779878c61bbd81176561991be025af44efc67332feWenchao Tong        if (mWifiStateMachineChannel != null) {
7789878c61bbd81176561991be025af44efc67332feWenchao Tong            return mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel);
7799878c61bbd81176561991be025af44efc67332feWenchao Tong        } else {
7809878c61bbd81176561991be025af44efc67332feWenchao Tong            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
7819878c61bbd81176561991be025af44efc67332feWenchao Tong            return null;
7829878c61bbd81176561991be025af44efc67332feWenchao Tong        }
7839878c61bbd81176561991be025af44efc67332feWenchao Tong    }
7849878c61bbd81176561991be025af44efc67332feWenchao Tong
7859878c61bbd81176561991be025af44efc67332feWenchao Tong    /**
786ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * Returns a WifiConfiguration matching this ScanResult
787ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * @param scanResult scanResult that represents the BSSID
788ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * @return {@link WifiConfiguration} that matches this BSSID or null
789ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     */
790ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    public WifiConfiguration getMatchingWifiConfig(ScanResult scanResult) {
791ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande        enforceAccessPermission();
792ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande        return mWifiStateMachine.syncGetMatchingWifiConfig(scanResult, mWifiStateMachineChannel);
793ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    }
794ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande
795ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande
796ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    /**
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the supplicant-assigned identifier for the new or updated
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * network if the operation succeeds, or {@code -1} if it fails
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addOrUpdateNetwork(WifiConfiguration config) {
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
8034d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (isValid(config)) {
804cd0af1c9ddcac881f49c815fb9da45433eb50736Vinit Deshpande
80531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig;
80631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist
80731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            if (config.isPasspoint() &&
80831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    (enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TLS ||
80931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TTLS)) {
81031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                try {
81131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    verifyCert(enterpriseConfig.getCaCertificate());
81231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                } catch (CertPathValidatorException cpve) {
81331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    Slog.e(TAG, "CA Cert " +
81431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                            enterpriseConfig.getCaCertificate().getSubjectX500Principal() +
81531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                            " untrusted: " + cpve.getMessage());
81631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    return -1;
81731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                } catch (GeneralSecurityException | IOException e) {
81831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    Slog.e(TAG, "Failed to verify certificate" +
81931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                            enterpriseConfig.getCaCertificate().getSubjectX500Principal() +
82031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                            ": " + e);
82131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    return -1;
82231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                }
82331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            }
82431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist
825992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            //TODO: pass the Uid the WifiStateMachine as a message parameter
82631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            Slog.i("addOrUpdateNetwork", " uid = " + Integer.toString(Binder.getCallingUid())
827992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + " SSID " + config.SSID
828992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + " nid=" + Integer.toString(config.networkId));
829992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            if (config.networkId == WifiConfiguration.INVALID_NETWORK_ID) {
830992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                config.creatorUid = Binder.getCallingUid();
831992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            } else {
832992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                config.lastUpdateUid = Binder.getCallingUid();
833992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            }
834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mWifiStateMachineChannel != null) {
835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return -1;
839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
841155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "bad network configuration");
842155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return -1;
843155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
844155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
845155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
84631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static void verifyCert(X509Certificate caCert)
84731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            throws GeneralSecurityException, IOException {
84831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        CertificateFactory factory = CertificateFactory.getInstance("X.509");
84931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        CertPathValidator validator =
85031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                CertPathValidator.getInstance(CertPathValidator.getDefaultType());
85131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        CertPath path = factory.generateCertPath(
85231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                Arrays.asList(caCert));
85331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        KeyStore ks = KeyStore.getInstance("AndroidCAStore");
85431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        ks.load(null, null);
85531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        PKIXParameters params = new PKIXParameters(ks);
85631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        params.setRevocationEnabled(false);
85731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        Log.d("HS2J", "CA Cert: " + caCert.getSubjectX500Principal());
85831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        validator.validate(path, params);
85931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    }
86031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist
86131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    /**
862155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
863155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
864155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
865155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
868155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
869c35d728a15e9270f5642ef79f5245c89d749285fSky Faber
870002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        if (!isOwner(Binder.getCallingUid())) {
871002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            Slog.e(TAG, "Remove is not authorized for user");
872002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            return false;
873002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        }
874002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
877155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
878155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
880155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
881155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
882155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
883155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
884155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
885155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
886155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
887155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param disableOthers if true, disable all other networks.
888155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
889155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
890155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean enableNetwork(int netId, boolean disableOthers) {
891155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
892155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
893155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
894155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    disableOthers);
895155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
896155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
897155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
898155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
899155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
900155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
901155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
902155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
903155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
904155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
905155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
906155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
907155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
908155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
909155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
910155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
911155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
912155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
913155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
914155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
915155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
916155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
917155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
918155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
919155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the Wi-Fi information, contained in {@link WifiInfo}.
920155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
921155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiInfo getConnectionInfo() {
922155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
923155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
924155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * Make sure we have the latest information, by sending
925155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * a status request to the supplicant.
926155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
927155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncRequestConnectionInfo();
928155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
929155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
930155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
931155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Return the results of the most recent access point scan, in the form of
932155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a list of {@link ScanResult} objects.
933155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the list of results
934155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
935155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<ScanResult> getScanResults(String callingPackage) {
936155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
937155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int userId = UserHandle.getCallingUserId();
938155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
939f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk        boolean hasInteractUsersFull = checkInteractAcrossUsersFull();
940155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
941155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
942155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, uid, callingPackage)
943155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    != AppOpsManager.MODE_ALLOWED) {
944155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return new ArrayList<ScanResult>();
945155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
946f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk            if (!isCurrentProfile(userId) && !hasInteractUsersFull) {
9471ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber                return new ArrayList<ScanResult>();
9481ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber            }
949fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina            return mWifiStateMachine.syncGetScanResultsList();
950155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
951155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
952155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
953155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
954155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
955155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
956f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk     * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
957f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk     */
958f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk    private boolean checkInteractAcrossUsersFull() {
959f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk        return mContext.checkCallingOrSelfPermission(
960f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk                android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
961f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk                == PackageManager.PERMISSION_GRANTED;
962f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk    }
963f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk
964f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk    /**
965b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy     * Returns true if the calling user is the current one or a profile of the
966b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy     * current user..
967fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina     */
9681ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber    private boolean isCurrentProfile(int userId) {
969fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        int currentUser = ActivityManager.getCurrentUser();
970fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        if (userId == currentUser) {
971fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina            return true;
972fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        }
973f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        List<UserInfo> profiles = mUserManager.getProfiles(currentUser);
974b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy        for (UserInfo user : profiles) {
975b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy            if (userId == user.id) {
976fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina                return true;
977fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina            }
978fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        }
979fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        return false;
980fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina    }
981fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina
982fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina    /**
9832a325cd07d40941adb93c1284abb263ba650c213Sky Faber     * Returns true if uid is an application running under the owner or a profile of the owner.
984002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber     *
985002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber     * Note: Should not be called if identity is cleared.
986002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber     */
9872a325cd07d40941adb93c1284abb263ba650c213Sky Faber    private boolean isOwner(int uid) {
988002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        long ident = Binder.clearCallingIdentity();
9892a325cd07d40941adb93c1284abb263ba650c213Sky Faber        int userId = UserHandle.getUserId(uid);
990002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        try {
991002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            int ownerUser = UserHandle.USER_OWNER;
992002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            if (userId == ownerUser) {
993002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                return true;
994002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            }
995f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            List<UserInfo> profiles = mUserManager.getProfiles(ownerUser);
996002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            for (UserInfo profile : profiles) {
997002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                if (userId == profile.id) {
998002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    return true;
999002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                }
1000002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            }
1001002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            return false;
1002002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        }
1003002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        finally {
1004002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            Binder.restoreCallingIdentity(ident);
1005002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        }
1006002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber    }
1007002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber
1008002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber
1009002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber    /**
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Tell the supplicant to persist the current list of configured networks.
1011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
1012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1013155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TODO: deprecate this
1014155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1015155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfiguration() {
1016155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean result = true;
1017155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1018155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
1019155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
1020155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1021155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1022155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1023155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1025155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1026155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1027155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Set the country code
1028155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param countryCode ISO 3166 country code.
1029155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param persist {@code true} if the setting should be remembered.
1030155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1031155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The persist behavior exists so that wifi can fall back to the last
1032155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * persisted country code on a restart, when the locale information is
1033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * not available from telephony.
1034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1035155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setCountryCode(String countryCode, boolean persist) {
1036155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
1037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                " with persist set to " + persist);
10389c22c8ea193360f50ad490368ca6a11af0684362Robert Greenwalt        enforceConnectivityInternalPermission();
1039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long token = Binder.clearCallingIdentity();
1040155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.setCountryCode(countryCode, persist);
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(token);
1044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
104780d14d67a956e6c69433470aaa73100898166efaxinhe     /**
104880d14d67a956e6c69433470aaa73100898166efaxinhe     * Get the country code
104980d14d67a956e6c69433470aaa73100898166efaxinhe     * @return ISO 3166 country code.
105080d14d67a956e6c69433470aaa73100898166efaxinhe     */
105180d14d67a956e6c69433470aaa73100898166efaxinhe    public String getCountryCode() {
105280d14d67a956e6c69433470aaa73100898166efaxinhe        enforceConnectivityInternalPermission();
105380d14d67a956e6c69433470aaa73100898166efaxinhe        String country = mWifiStateMachine.getCountryCode();
105480d14d67a956e6c69433470aaa73100898166efaxinhe        return country;
105580d14d67a956e6c69433470aaa73100898166efaxinhe    }
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Set the operational frequency band
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param band One of
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
1060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
1061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param persist {@code true} if the setting should be remembered.
1063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setFrequencyBand(int band, boolean persist) {
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!isDualBandSupported()) return;
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.i(TAG, "WifiService trying to set frequency band to " + band +
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                " with persist set to " + persist);
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long token = Binder.clearCallingIdentity();
1071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1072155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.setFrequencyBand(band, persist);
1073155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1074155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(token);
1075155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1076155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1077155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Get the operational frequency band
1081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getFrequencyBand() {
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.getFrequencyBand();
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isDualBandSupported() {
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //TODO: Should move towards adding a driver API that checks at runtime
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mContext.getResources().getBoolean(
1090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                com.android.internal.R.bool.config_wifi_dual_band_support);
1091155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Return the DHCP-assigned addresses from the last successful DHCP request,
1095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * if any.
1096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the DHCP information
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @deprecated
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public DhcpInfo getDhcpInfo() {
1100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults();
1102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DhcpInfo info = new DhcpInfo();
11043b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
11053b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (dhcpResults.ipAddress != null &&
11063b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                dhcpResults.ipAddress.getAddress() instanceof Inet4Address) {
11073b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.ipAddress.getAddress());
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
11093b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
11103b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (dhcpResults.gateway != null) {
11113b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            info.gateway = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.gateway);
1112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
11133b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
1114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int dnsFound = 0;
11153b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        for (InetAddress dns : dhcpResults.dnsServers) {
1116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (dns instanceof Inet4Address) {
1117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (dnsFound == 0) {
1118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address)dns);
1119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address)dns);
1121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (++dnsFound > 1) break;
1123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        InetAddress serverAddress = dhcpResults.serverAddress;
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (serverAddress instanceof Inet4Address) {
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            info.serverAddress = NetworkUtils.inetAddressToInt((Inet4Address)serverAddress);
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        info.leaseDuration = dhcpResults.leaseDuration;
1130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return info;
1132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#startWifi}
1136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startWifi() {
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceConnectivityInternalPermission();
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* TODO: may be add permissions for access only to connectivity service
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * TODO: if a start issued, keep wifi alive until a stop issued irrespective
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * of WifiLock & device idle status unless wifi enabled status is toggled
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.setDriverStart(true);
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reconnectCommand();
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#stopWifi}
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void stopWifi() {
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceConnectivityInternalPermission();
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * TODO: if a stop is issued, wifi is brought up only by startWifi
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * unless wifi enabled status is toggled
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.setDriverStart(false);
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#addToBlacklist}
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void addToBlacklist(String bssid) {
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.addToBlacklist(bssid);
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#clearBlacklist}
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void clearBlacklist() {
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.clearBlacklist();
1180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * enable TDLS for the local NIC to remote NIC
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The APPs don't know the remote MAC address to identify NIC though,
1185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * so we need to do additional work to find it from remote IP address
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class TdlsTaskParams {
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public String remoteIpAddress;
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public boolean enable;
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class TdlsTask extends AsyncTask<TdlsTaskParams, Integer, Integer> {
1194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        protected Integer doInBackground(TdlsTaskParams... params) {
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Retrieve parameters for the call
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            TdlsTaskParams param = params[0];
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String remoteIpAddress = param.remoteIpAddress.trim();
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean enable = param.enable;
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Get MAC address of Remote IP
1203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String macAddress = null;
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            BufferedReader reader = null;
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                reader = new BufferedReader(new FileReader("/proc/net/arp"));
1209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Skip over the line bearing colum titles
1211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String line = reader.readLine();
1212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                while ((line = reader.readLine()) != null) {
1214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String[] tokens = line.split("[ ]+");
1215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (tokens.length < 6) {
1216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        continue;
1217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // ARP column format is
1220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Address HWType HWAddress Flags Mask IFace
1221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String ip = tokens[0];
1222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String mac = tokens[3];
1223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (remoteIpAddress.equals(ip)) {
1225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        macAddress = mac;
1226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (macAddress == null) {
1231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.w(TAG, "Did not find remoteAddress {" + remoteIpAddress + "} in " +
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            "/proc/net/arp");
1233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    enableTdlsWithMacAddress(macAddress, enable);
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (FileNotFoundException e) {
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "Could not open /proc/net/arp to lookup mac address");
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (IOException e) {
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "Could not read /proc/net/arp to lookup mac address");
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } finally {
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (reader != null) {
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        reader.close();
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                catch (IOException e) {
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Do nothing
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return 0;
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableTdls(String remoteAddress, boolean enable) {
12578e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        if (remoteAddress == null) {
12588e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer          throw new IllegalArgumentException("remoteAddress cannot be null");
12598e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        }
12608e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        TdlsTaskParams params = new TdlsTaskParams();
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        params.remoteIpAddress = remoteAddress;
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        params.enable = enable;
1264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        new TdlsTask().execute(params);
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableTdlsWithMacAddress(String remoteMacAddress, boolean enable) {
12698e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        if (remoteMacAddress == null) {
12708e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer          throw new IllegalArgumentException("remoteMacAddress cannot be null");
12718e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        }
12728e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.enableTdls(remoteMacAddress, enable);
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Get a reference to handler. This is used by a client to establish
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * an AsyncChannel communication with WifiService
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public Messenger getWifiServiceMessenger() {
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return new Messenger(mClientHandler);
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
12862ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    /**
12872ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle     * Disable an ephemeral network, i.e. network that is created thru a WiFi Scorer
12882ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle     */
12892ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    public void disableEphemeralNetwork(String SSID) {
12902ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        enforceAccessPermission();
12912ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        enforceChangePermission();
12922ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        mWifiStateMachine.disableEphemeralNetwork(SSID);
12932ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    }
1294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Get the IP and proxy configuration file
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getConfigFile() {
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.getConfigFile();
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void onReceive(Context context, Intent intent) {
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String action = intent.getAction();
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (action.equals(Intent.ACTION_SCREEN_ON)) {
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCREEN_ON);
1309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_USER_PRESENT);
1311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
1312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCREEN_OFF);
1313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
1314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int pluggedType = intent.getIntExtra("plugged", 0);
1315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_BATTERY_CHANGED, pluggedType, 0, null);
1316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        BluetoothAdapter.STATE_DISCONNECTED);
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiStateMachine.sendBluetoothAdapterStateChange(state);
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                boolean emergencyMode = intent.getBooleanExtra("phoneinECMState", false);
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, emergencyMode ? 1 : 0, 0);
132303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            } else if (action.equals(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED)) {
132403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                handleIdleModeChanged();
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    };
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Observes settings changes to scan always mode.
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void registerForScanModeChange() {
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        ContentObserver contentObserver = new ContentObserver(null) {
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            @Override
1335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            public void onChange(boolean selfChange) {
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mSettingsStore.handleWifiScanAlwaysAvailableToggled();
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCAN_ALWAYS_MODE_CHANGED);
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        };
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.getContentResolver().registerContentObserver(
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Settings.Global.getUriFor(Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE),
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                false, contentObserver);
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void registerForBroadcasts() {
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        IntentFilter intentFilter = new IntentFilter();
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_USER_PRESENT);
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
135503b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        intentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.registerReceiver(mReceiver, intentFilter);
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1359faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee    private void registerForPackageOrUserRemoval() {
1360e487a4648dd41881e754f1224aaedead78a0777dSky Faber        IntentFilter intentFilter = new IntentFilter();
1361e487a4648dd41881e754f1224aaedead78a0777dSky Faber        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1362faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        intentFilter.addAction(Intent.ACTION_USER_REMOVED);
1363faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        mContext.registerReceiverAsUser(new BroadcastReceiver() {
1364e487a4648dd41881e754f1224aaedead78a0777dSky Faber            @Override
1365e487a4648dd41881e754f1224aaedead78a0777dSky Faber            public void onReceive(Context context, Intent intent) {
1366faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                switch (intent.getAction()) {
1367faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                    case Intent.ACTION_PACKAGE_REMOVED: {
1368faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
1369faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                            return;
1370faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        }
1371faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1372faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        Uri uri = intent.getData();
1373faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        if (uid == -1 || uri == null) {
1374faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                            return;
1375faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        }
1376faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        String pkgName = uri.getSchemeSpecificPart();
1377faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        mWifiStateMachine.removeAppConfigs(pkgName, uid);
1378faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        break;
1379faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                    }
1380faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                    case Intent.ACTION_USER_REMOVED: {
1381faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
1382faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        mWifiStateMachine.removeUserConfigs(userHandle);
1383faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        break;
1384e487a4648dd41881e754f1224aaedead78a0777dSky Faber                    }
1385e487a4648dd41881e754f1224aaedead78a0777dSky Faber                }
1386e487a4648dd41881e754f1224aaedead78a0777dSky Faber            }
1387faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        }, UserHandle.ALL, intentFilter, null, null);
1388e487a4648dd41881e754f1224aaedead78a0777dSky Faber    }
1389e487a4648dd41881e754f1224aaedead78a0777dSky Faber
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    @Override
1391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                != PackageManager.PERMISSION_GRANTED) {
1394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            pw.println("Permission Denial: can't dump WifiService from from pid="
1395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + Binder.getCallingPid()
1396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + ", uid=" + Binder.getCallingUid());
1397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
1398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
1400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Stay-awake conditions: " +
1401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Settings.Global.getInt(mContext.getContentResolver(),
1402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                       Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0));
1403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("mMulticastEnabled " + mMulticastEnabled);
1404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("mMulticastDisabled " + mMulticastDisabled);
140503b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        pw.println("mInIdleMode " + mInIdleMode);
140603b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        pw.println("mScanPending " + mScanPending);
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiController.dump(fd, pw, args);
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mSettingsStore.dump(fd, pw, args);
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mNotificationController.dump(fd, pw, args);
1410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTrafficPoller.dump(fd, pw, args);
1411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Latest scan results:");
1413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
1414a0708b09ad17b086c008ab100aec7143d7613c80vandwalle        long nowMs = System.currentTimeMillis();
1415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (scanResults != null && scanResults.size() != 0) {
1416a0708b09ad17b086c008ab100aec7143d7613c80vandwalle            pw.println("    BSSID              Frequency  RSSI    Age      SSID " +
1417a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                    "                                Flags");
1418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (ScanResult r : scanResults) {
1419a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                long ageSec = 0;
1420a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                long ageMilli = 0;
1421a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                if (nowMs > r.seen && r.seen > 0) {
1422a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                    ageSec = (nowMs - r.seen) / 1000;
1423a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                    ageMilli = (nowMs - r.seen) % 1000;
1424a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                }
1425a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                String candidate = " ";
1426a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                if (r.isAutoJoinCandidate > 0) candidate = "+";
1427a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                pw.printf("  %17s  %9d  %5d  %3d.%03d%s   %-32s  %s\n",
1428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                         r.BSSID,
1429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                         r.frequency,
1430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                         r.level,
1431a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         ageSec, ageMilli,
1432a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         candidate,
1433a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         r.SSID == null ? "" : r.SSID,
1434a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         r.capabilities);
1435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
1439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mFullHighPerfLocksAcquired + " full high perf, " +
1440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mScanLocksAcquired + " scan");
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Locks released: " + mFullLocksReleased + " full, " +
1442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mFullHighPerfLocksReleased + " full high perf, " +
1443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mScanLocksReleased + " scan");
1444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Locks held:");
1446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mLocks.dump(pw);
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiWatchdogStateMachine.dump(fd, pw, args);
1449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.dump(fd, pw, args);
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class WifiLock extends DeathRecipient {
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(lockMode, tag, binder, ws);
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void binderDied() {
1460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            synchronized (mLocks) {
1461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                releaseWifiLockLocked(mBinder);
1462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public String toString() {
1466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
1467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class LockList {
1471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private List<WifiLock> mList;
1472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private LockList() {
1474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mList = new ArrayList<WifiLock>();
1475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized boolean hasLocks() {
1478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return !mList.isEmpty();
1479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized int getStrongestLockMode() {
1482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mList.isEmpty()) {
1483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return WifiManager.WIFI_MODE_FULL;
1484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
1488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mFullLocksAcquired > mFullLocksReleased) {
1491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return WifiManager.WIFI_MODE_FULL;
1492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return WifiManager.WIFI_MODE_SCAN_ONLY;
1495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized void updateWorkSource(WorkSource ws) {
1498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (int i = 0; i < mLocks.mList.size(); i++) {
1499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ws.add(mLocks.mList.get(i).mWorkSource);
1500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private void addLock(WifiLock lock) {
1504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (findLockByBinder(lock.mBinder) < 0) {
1505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mList.add(lock);
1506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private WifiLock removeLock(IBinder binder) {
1510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int index = findLockByBinder(binder);
1511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (index >= 0) {
1512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiLock ret = mList.remove(index);
1513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ret.unlinkDeathRecipient();
1514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return ret;
1515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return null;
1517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private int findLockByBinder(IBinder binder) {
1521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int size = mList.size();
1522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (int i = size - 1; i >= 0; i--) {
1523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (mList.get(i).mBinder == binder)
1524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return i;
1525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return -1;
1527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private void dump(PrintWriter pw) {
1530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (WifiLock l : mList) {
1531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                pw.print("    ");
1532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                pw.println(l);
1533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void enforceWakeSourcePermission(int uid, int pid) {
1538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (uid == android.os.Process.myUid()) {
1539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
1540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                pid, uid, null);
1543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
1546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (lockMode != WifiManager.WIFI_MODE_FULL &&
1548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
1552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null && ws.size() == 0) {
1555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ws = null;
1556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null) {
1558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws == null) {
1561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ws = new WorkSource(Binder.getCallingUid());
1562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
1564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLocks) {
1565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return acquireWifiLockLocked(wifiLock);
1566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch(wifiLock.mMode) {
1571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL:
1572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_SCAN_ONLY:
1574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch(wifiLock.mMode) {
1581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL:
1582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_SCAN_ONLY:
1584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean acquireWifiLockLocked(WifiLock wifiLock) {
1590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
1591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mLocks.addLock(wifiLock);
1593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
1595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1596155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            noteAcquireWifiLock(wifiLock);
1597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch(wifiLock.mMode) {
1598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL:
1599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++mFullLocksAcquired;
1600155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1601155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1602155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++mFullHighPerfLocksAcquired;
1603155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_SCAN_ONLY:
1606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++mScanLocksAcquired;
1607155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiController.sendMessage(CMD_LOCKS_CHANGED);
1610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return true;
1611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1614155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1615155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
1620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int pid = Binder.getCallingPid();
1621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null && ws.size() == 0) {
1622155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ws = null;
1623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1624155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null) {
1625155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            enforceWakeSourcePermission(uid, pid);
1626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
1628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            synchronized (mLocks) {
1630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int index = mLocks.findLockByBinder(lock);
1631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (index < 0) {
1632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    throw new IllegalArgumentException("Wifi lock not active");
1633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiLock wl = mLocks.mList.get(index);
1635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                noteReleaseWifiLock(wl);
1636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                noteAcquireWifiLock(wl);
1638155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1639155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1640155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1641155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1643155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1644155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1645155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean releaseWifiLock(IBinder lock) {
1646155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLocks) {
1648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return releaseWifiLockLocked(lock);
1649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean releaseWifiLockLocked(IBinder lock) {
1653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean hadLock;
1654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiLock wifiLock = mLocks.removeLock(lock);
1656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
1658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        hadLock = (wifiLock != null);
1660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1661155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
1662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1663155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (hadLock) {
1664155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                noteReleaseWifiLock(wifiLock);
1665155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                switch(wifiLock.mMode) {
1666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    case WifiManager.WIFI_MODE_FULL:
1667155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        ++mFullLocksReleased;
1668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        ++mFullHighPerfLocksReleased;
1671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    case WifiManager.WIFI_MODE_SCAN_ONLY:
1673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        ++mScanLocksReleased;
1674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_LOCKS_CHANGED);
1677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return hadLock;
1684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private abstract class DeathRecipient
1687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            implements IBinder.DeathRecipient {
1688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String mTag;
1689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int mMode;
1690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        IBinder mBinder;
1691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WorkSource mWorkSource;
1692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1693155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
1694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super();
1695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mTag = tag;
1696155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMode = mode;
1697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBinder = binder;
1698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWorkSource = ws;
1699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
1700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mBinder.linkToDeath(this, 0);
1701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (RemoteException e) {
1702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                binderDied();
1703155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1704155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1705155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1706155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        void unlinkDeathRecipient() {
1707155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBinder.unlinkToDeath(this, 0);
1708155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1709155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1710155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1711155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class Multicaster extends DeathRecipient {
1712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Multicaster(String tag, IBinder binder) {
1713155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(Binder.getCallingUid(), tag, binder, null);
1714155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1715155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1716155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void binderDied() {
1717155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Multicaster binderDied");
1718155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            synchronized (mMulticasters) {
1719155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int i = mMulticasters.indexOf(this);
1720155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (i != -1) {
1721155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    removeMulticasterLocked(i, mMode);
1722155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1723155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1724155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1725155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1726155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public String toString() {
1727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return "Multicaster{" + mTag + " binder=" + mBinder + "}";
1728155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1729155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1730155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public int getUid() {
1731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mMode;
1732155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1733155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void initializeMulticastFiltering() {
1736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1737155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1738155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // if anybody had requested filters be off, leave off
1740155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mMulticasters.size() != 0) {
1741155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return;
1742155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1743155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiStateMachine.startFilteringMulticastV4Packets();
1744155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1747155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1748155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void acquireMulticastLock(IBinder binder, String tag) {
1749155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1750155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMulticastEnabled++;
1753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMulticasters.add(new Multicaster(tag, binder));
1754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Note that we could call stopFilteringMulticastV4Packets only when
1755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // our new size == 1 (first call), but this function won't
1756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // be called often and by making the stopPacket call each
1757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // time we're less fragile and self-healing.
1758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.stopFilteringMulticastV4Packets();
1759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
1762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long ident = Binder.clearCallingIdentity();
1763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBatteryStats.noteWifiMulticastEnabled(uid);
1765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void releaseMulticastLock() {
1772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
1775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMulticastDisabled++;
1777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int size = mMulticasters.size();
1778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (int i = size - 1; i >= 0; i--) {
1779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Multicaster m = mMulticasters.get(i);
1780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if ((m != null) && (m.getUid() == uid)) {
1781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    removeMulticasterLocked(i, uid);
1782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void removeMulticasterLocked(int i, int uid)
1788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    {
1789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Multicaster removed = mMulticasters.remove(i);
1790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (removed != null) {
1792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            removed.unlinkDeathRecipient();
1793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mMulticasters.size() == 0) {
1795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.startFilteringMulticastV4Packets();
1796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long ident = Binder.clearCallingIdentity();
1799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBatteryStats.noteWifiMulticastDisabled(uid);
1801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isMulticastEnabled() {
1808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return (mMulticasters.size() > 0);
1812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
18149ae6b45c038fa74a9e7285ef4834551dd93da332Yuhao Zheng
1815511a412adce5a46c41e61f90c0df6de5fb4e491cYuhao Zheng    public WifiMonitor getWifiMonitor() {
1816511a412adce5a46c41e61f90c0df6de5fb4e491cYuhao Zheng        return mWifiStateMachine.getWifiMonitor();
18179ae6b45c038fa74a9e7285ef4834551dd93da332Yuhao Zheng    }
1818ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
1819ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    public void enableVerboseLogging(int verbose) {
1820ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        enforceAccessPermission();
1821ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        mWifiStateMachine.enableVerboseLogging(verbose);
1822ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
1823ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
1824ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    public int getVerboseLoggingLevel() {
1825ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        enforceAccessPermission();
1826ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        return mWifiStateMachine.getVerboseLoggingLevel();
1827ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
1828c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1829c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public void enableAggressiveHandover(int enabled) {
1830c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1831c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        mWifiStateMachine.enableAggressiveHandover(enabled);
1832c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1833c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1834c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public int getAggressiveHandover() {
1835c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1836c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        return mWifiStateMachine.getAggressiveHandover();
1837c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1838c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1839c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public void setAllowScansWithTraffic(int enabled) {
1840c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1841c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        mWifiStateMachine.setAllowScansWithTraffic(enabled);
1842c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1843c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1844c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public int getAllowScansWithTraffic() {
1845c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1846c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        return mWifiStateMachine.getAllowScansWithTraffic();
1847c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1848c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
1849b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public void setAllowScansWhileAssociated(int enabled) {
1850e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle        enforceAccessPermission();
1851e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle        mWifiStateMachine.setAllowScansWhileAssociated(enabled);
1852e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle    }
1853e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle
1854b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public int getAllowScansWhileAssociated() {
1855e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle        enforceAccessPermission();
1856e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle        return mWifiStateMachine.getAllowScansWhileAssociated();
1857e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle    }
1858e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle
1859b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public void setHalBasedAutojoinOffload(int enabled) {
1860b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        enforceAccessPermission();
1861b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        mWifiStateMachine.setHalBasedAutojoinOffload(enabled);
1862b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    }
1863b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1864b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public int getHalBasedAutojoinOffload() {
1865b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        enforceAccessPermission();
1866b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        return mWifiStateMachine.getHalBasedAutojoinOffload();
1867b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    }
1868b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1869b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public void setAllowNetworkSwitchingWhileAssociated(int enabled) {
1870b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        enforceAccessPermission();
1871b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        mWifiStateMachine.setAllowNetworkSwitchingWhileAssociated(enabled);
1872b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    }
1873b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1874b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public int getAllowNetworkSwitchingWhileAssociated() {
1875b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        enforceAccessPermission();
1876b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        return mWifiStateMachine.getAllowNetworkSwitchingWhileAssociated();
1877b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    }
1878b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1879c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    /* Return the Wifi Connection statistics object */
1880c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    public WifiConnectionStatistics getConnectionStatistics() {
1881c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        enforceAccessPermission();
1882c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        enforceReadCredentialPermission();
1883c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        if (mWifiStateMachineChannel != null) {
1884c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            return mWifiStateMachine.syncGetConnectionStatistics(mWifiStateMachineChannel);
1885c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        } else {
1886c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1887c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            return null;
1888c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        }
1889c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    }
1890b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
1891b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott    public void factoryReset() {
1892b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott        enforceConnectivityInternalPermission();
1893b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
1894f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
1895f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            return;
1896f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        }
1897b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
1898f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
1899f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            // Turn mobile hotspot off
1900f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            setWifiApEnabled(null, false);
1901f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        }
1902f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott
1903f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI)) {
1904f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            // Enable wifi
1905f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            setWifiEnabled(true);
1906f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            // Delete all Wifi SSIDs
1907f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            List<WifiConfiguration> networks = getConfiguredNetworks();
1908f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            if (networks != null) {
1909f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                for (WifiConfiguration config : networks) {
1910f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                    removeNetwork(config.networkId);
1911f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                }
1912f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                saveConfiguration();
1913b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott            }
1914b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott        }
1915b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott    }
1916cd0af1c9ddcac881f49c815fb9da45433eb50736Vinit Deshpande
19174d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    /* private methods */
191831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    static boolean logAndReturnFalse(String s) {
19194d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        Log.d(TAG, s);
19204d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        return false;
19214d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    }
19224d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
192331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static boolean isValid(WifiConfiguration config) {
192431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        String validity = checkValidity(config);
192531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        return validity == null || logAndReturnFalse(validity);
192631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    }
19274d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
192831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static String checkValidity(WifiConfiguration config) {
192931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        if (config.allowedKeyManagement == null)
193031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            return "allowed kmgmt";
19314d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
19324d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (config.allowedKeyManagement.cardinality() > 1) {
19334d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (config.allowedKeyManagement.cardinality() != 2) {
193431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "cardinality != 2";
19354d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19364d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)) {
193731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "not WPA_EAP";
19384d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19394d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if ((!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X))
19404d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                    && (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK))) {
194131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "not PSK or 8021X";
19424d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19434d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        }
19444d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
19454d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (!TextUtils.isEmpty(config.FQDN)) {
19464d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must not have an SSID */
19474d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (!TextUtils.isEmpty(config.SSID)) {
194831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "SSID not expected for Passpoint: '" + config.SSID +
194931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                        "' FQDN " + toHexString(config.FQDN);
19504d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19514d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must have a providerFriendlyName */
19524d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (TextUtils.isEmpty(config.providerFriendlyName)) {
195331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "no provider friendly name";
19544d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19554d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must have enterprise config */
19564d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (config.enterpriseConfig == null
195731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    ||config. enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.NONE ) {
195831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "no enterprise config";
19594d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19604d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (config.enterpriseConfig.getCaCertificate() == null) {
196131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "no CA certificate";
19624d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19634d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        }
19644d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
196531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        // TODO: Add more checks
196631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        return null;
1967cd0af1c9ddcac881f49c815fb9da45433eb50736Vinit Deshpande    }
1968b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
19699846078598c3468f8813dbfa58238a1846bd81efSanket Padawe    public Network getCurrentNetwork() {
19709846078598c3468f8813dbfa58238a1846bd81efSanket Padawe        enforceAccessPermission();
19719846078598c3468f8813dbfa58238a1846bd81efSanket Padawe        return mWifiStateMachine.getCurrentNetwork();
19729846078598c3468f8813dbfa58238a1846bd81efSanket Padawe    }
19739846078598c3468f8813dbfa58238a1846bd81efSanket Padawe
197431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static String toHexString(String s) {
197531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        if (s == null) {
197631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            return "null";
197731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        }
197831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        StringBuilder sb = new StringBuilder();
197931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        sb.append('\'').append(s).append('\'');
198031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        for (int n = 0; n < s.length(); n++) {
198131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            sb.append(String.format(" %02x", s.charAt(n) & 0xffff));
198231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        }
198331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        return sb.toString();
19844d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    }
1985cd0af1c9ddcac881f49c815fb9da45433eb50736Vinit Deshpande
1986155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
1987