WifiServiceImpl.java revision b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05
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;
35155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.NetworkUtils;
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.RouteInfo;
37a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport android.net.Uri;
38c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalleimport android.net.wifi.*;
393ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.net.wifi.IWifiManager;
403ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.os.AsyncTask;
41155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Binder;
42155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Handler;
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.HandlerThread;
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.IBinder;
45155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Message;
463ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.os.Messenger;
47155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.RemoteException;
48155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.SystemProperties;
49155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.UserHandle;
50fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghinaimport android.os.UserManager;
51155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.WorkSource;
52155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.provider.Settings;
534d381bc39f5263effdae73ec99065eb299b806caVinit Deshpandeimport android.text.TextUtils;
54992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalleimport android.util.Log;
55155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Slog;
56155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
57155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileNotFoundException;
58155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.BufferedReader;
59155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileDescriptor;
60155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileReader;
61155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.IOException;
62155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.PrintWriter;
63155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.lang.Override;
64155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.net.InetAddress;
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.net.Inet4Address;
66a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport java.net.URISyntaxException;
67a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport java.security.GeneralSecurityException;
68155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
69155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
70155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
71155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.R;
72155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.app.IBatteryStats;
73155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.telephony.TelephonyIntents;
74155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.util.AsyncChannel;
75155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.server.am.BatteryStatsService;
76a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport com.android.server.wifi.configparse.ConfigBuilder;
77a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist
78a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport org.xml.sax.SAXException;
79a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
80155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_AIRPLANE_TOGGLED;
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_BATTERY_CHANGED;
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_EMERGENCY_MODE_CHANGED;
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_LOCKS_CHANGED;
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCAN_ALWAYS_MODE_CHANGED;
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCREEN_OFF;
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCREEN_ON;
87155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SET_AP;
88155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_USER_PRESENT;
89155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED;
90155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
91155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * WifiService handles remote WiFi operation requests by implementing
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * the IWifiManager interface.
93155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
94155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * @hide
95155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
96155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic final class WifiServiceImpl extends IWifiManager.Stub {
97155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final String TAG = "WifiService";
98f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final boolean DBG = true;
99155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final WifiStateMachine mWifiStateMachine;
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final Context mContext;
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final LockList mLocks = new LockList();
105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // some wifi lock statistics
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullHighPerfLocksAcquired;
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullHighPerfLocksReleased;
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullLocksAcquired;
109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullLocksReleased;
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mScanLocksAcquired;
111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mScanLocksReleased;
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final List<Multicaster> mMulticasters =
114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            new ArrayList<Multicaster>();
115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mMulticastEnabled;
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mMulticastDisabled;
117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final IBatteryStats mBatteryStats;
119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final AppOpsManager mAppOps;
120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String mInterfaceName;
122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
123ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle    // Debug counter tracking scan requests sent by WifiManager
124ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle    private int scanRequestCounter = 0;
125ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle
126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Tracks the open wi-fi network notification */
127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiNotificationController mNotificationController;
128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Polls traffic stats and notifies clients */
129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiTrafficPoller mTrafficPoller;
130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Tracks the persisted states for wi-fi & airplane mode */
131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final WifiSettingsStore mSettingsStore;
132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Asynchronous channel to WifiStateMachine
135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private AsyncChannel mWifiStateMachineChannel;
137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Handles client connections
140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class ClientHandler extends Handler {
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        ClientHandler(android.os.Looper looper) {
144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(looper);
145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void handleMessage(Message msg) {
149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch (msg.what) {
150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "New client listening to asynchronous messages");
153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        // We track the clients by the Messenger
154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        // since it is expected to be always available
155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mTrafficPoller.addClient(msg.replyTo);
156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Send failed, client connection lost");
164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mTrafficPoller.removeClient(msg.replyTo);
168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    AsyncChannel ac = new AsyncChannel();
172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    ac.connect(mContext, this, msg.replyTo);
173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                /* Client commands are forwarded to state machine */
176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.CONNECT_NETWORK:
177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.SAVE_NETWORK: {
178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    WifiConfiguration config = (WifiConfiguration) msg.obj;
179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    int networkId = msg.arg1;
180992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (msg.what == WifiManager.SAVE_NETWORK) {
181992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        if (config != null) {
182992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                            if (config.networkId == WifiConfiguration.INVALID_NETWORK_ID) {
183992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                config.creatorUid = Binder.getCallingUid();
184992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                            } else {
185992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                config.lastUpdateUid = Binder.getCallingUid();
186992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                            }
187992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        }
188992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        Slog.e("WiFiServiceImpl ", "SAVE"
189992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + " nid=" + Integer.toString(networkId)
190992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + " uid=" + Integer.toString(config.creatorUid)
191992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + "/" + Integer.toString(config.lastUpdateUid));
192992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
193992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (msg.what == WifiManager.CONNECT_NETWORK) {
194992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        if (config != null) {
195992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                            if (config.networkId == WifiConfiguration.INVALID_NETWORK_ID) {
196992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                config.creatorUid = Binder.getCallingUid();
197992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                            } else {
198992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                config.lastUpdateUid = Binder.getCallingUid();
199992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                            }
200992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        }
201992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        Slog.e("WiFiServiceImpl ", "CONNECT "
202992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + " nid=" + Integer.toString(networkId)
203992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + " uid=" + Binder.getCallingUid());
204992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
2054d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                    if (config != null && isValid(config)) {
20664c98f4afda6c7970c5d563580165a308d5e7c15Jason Monk                        if (DBG) Slog.d(TAG, "Connect with config" + config);
20764c98f4afda6c7970c5d563580165a308d5e7c15Jason Monk                        mWifiStateMachine.sendMessage(Message.obtain(msg));
208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else if (config == null
209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            && networkId != WifiConfiguration.INVALID_NETWORK_ID) {
210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Connect with networkId" + networkId);
211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachine.sendMessage(Message.obtain(msg));
212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "ClientHandler.handleMessage ignoring invalid msg=" + msg);
214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (msg.what == WifiManager.CONNECT_NETWORK) {
215c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                            replyFailed(msg, WifiManager.CONNECT_NETWORK_FAILED,
216c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                                    WifiManager.INVALID_ARGS);
217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        } else {
218c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                            replyFailed(msg, WifiManager.SAVE_NETWORK_FAILED,
219c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                                    WifiManager.INVALID_ARGS);
220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.FORGET_NETWORK:
2252a325cd07d40941adb93c1284abb263ba650c213Sky Faber                    if (isOwner(msg.sendingUid)) {
226002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                        mWifiStateMachine.sendMessage(Message.obtain(msg));
227002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    } else {
228002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                        Slog.e(TAG, "Forget is not authorized for user");
229002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                        replyFailed(msg, WifiManager.FORGET_NETWORK_FAILED,
230002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                                WifiManager.NOT_AUTHORIZED);
231002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    }
232002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    break;
233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.START_WPS:
234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.CANCEL_WPS:
235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.DISABLE_NETWORK:
236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.RSSI_PKTCNT_FETCH: {
237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWifiStateMachine.sendMessage(Message.obtain(msg));
238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                default: {
241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.d(TAG, "ClientHandler.handleMessage ignoring msg=" + msg);
242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
247c35d728a15e9270f5642ef79f5245c89d749285fSky Faber        private void replyFailed(Message msg, int what, int why) {
248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Message reply = msg.obtain();
249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            reply.what = what;
250c35d728a15e9270f5642ef79f5245c89d749285fSky Faber            reply.arg1 = why;
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                msg.replyTo.send(reply);
253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (RemoteException e) {
254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // There's not much we can do if reply can't be sent!
255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private ClientHandler mClientHandler;
259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Handles interaction with WifiStateMachine
262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class WifiStateMachineHandler extends Handler {
264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private AsyncChannel mWsmChannel;
265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiStateMachineHandler(android.os.Looper looper) {
267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(looper);
268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWsmChannel = new AsyncChannel();
269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void handleMessage(Message msg) {
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch (msg.what) {
275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachineChannel = mWsmChannel;
278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachineChannel = null;
281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1);
286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWifiStateMachineChannel = null;
287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    //Re-establish connection to state machine
288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                default: {
292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    WifiStateMachineHandler mWifiStateMachineHandler;
300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiWatchdogStateMachine mWifiWatchdogStateMachine;
302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiController mWifiController;
304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiServiceImpl(Context context) {
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext = context;
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName =  SystemProperties.get("wifi.interface", "wlan0");
309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
310f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mTrafficPoller = new WifiTrafficPoller(mContext, mInterfaceName);
311f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName, mTrafficPoller);
312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.enableRssiPolling(true);
313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mBatteryStats = BatteryStatsService.getService();
314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mNotificationController = new WifiNotificationController(mContext, mWifiStateMachine);
317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mSettingsStore = new WifiSettingsStore(mContext);
318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        HandlerThread wifiThread = new HandlerThread("WifiService");
320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        wifiThread.start();
321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mClientHandler = new ClientHandler(wifiThread.getLooper());
322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiController = new WifiController(mContext, this, wifiThread.getLooper());
3241c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    }
3251c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde
3261c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde
3271c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    /**
3281c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * Check if Wi-Fi needs to be enabled and start
3291c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * if needed
3301c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     *
3311c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * This function is used only at boot time
3321c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     */
3331c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    public void checkAndStartWifi() {
3341c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        /* Check if wi-fi needs to be enabled */
3351c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled();
3361c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        Slog.i(TAG, "WifiService starting up with Wi-Fi " +
3371c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde                (wifiEnabled ? "enabled" : "disabled"));
338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerForScanModeChange();
340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.registerReceiver(
341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                new BroadcastReceiver() {
342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    @Override
343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    public void onReceive(Context context, Intent intent) {
344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (mSettingsStore.handleAirplaneModeToggled()) {
345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            mWifiController.sendMessage(CMD_AIRPLANE_TOGGLED);
346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                },
349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Adding optimizations of only receiving broadcasts when wifi is enabled
352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // can result in race conditions when apps toggle wifi in the background
353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // without active user involvement. Always receive broadcasts.
354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerForBroadcasts();
355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3561c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        mWifiController.start();
357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // If we are already disabled (could be due to airplane mode), avoid changing persist
359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // state here
360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (wifiEnabled) setWifiEnabled(wifiEnabled);
361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiWatchdogStateMachine = WifiWatchdogStateMachine.
363569b093c4afd30787308032c8259af2680a2874fNavtej Singh Mann               makeWifiWatchdogStateMachine(mContext, mWifiStateMachine.getMessenger());
364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#pingSupplicant()}
368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeds, {@code false} otherwise
369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean pingSupplicant() {
371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
381a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * see {@link android.net.wifi.WifiManager#getChannelList}
382a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     */
383a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public List<WifiChannel> getChannelList() {
384a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        enforceAccessPermission();
385a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        if (mWifiStateMachineChannel != null) {
386a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            return mWifiStateMachine.syncGetChannelList(mWifiStateMachineChannel);
387a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        } else {
388a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
389a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            return null;
390a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        }
391a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
392a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
3939086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    // Start a location scan.
3949086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    // L release: A location scan is implemented as a normal scan and avoids scanning DFS channels
3959086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    public void startLocationRestrictedScan(WorkSource workSource) {
3969086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        enforceChangePermission();
3979086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        enforceLocationHardwarePermission();
3989086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        List<WifiChannel> channels = getChannelList();
3999086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        if (channels == null) {
4009086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            Slog.e(TAG, "startLocationRestrictedScan cant get channels");
4019086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            return;
4029086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        }
4039086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        ScanSettings settings = new ScanSettings();
4049086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        for (WifiChannel channel : channels) {
4059086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            if (!channel.isDFS) {
4069086afccf6938a49eb9a2cd248917c1cb0943942vandwalle                settings.channelSet.add(channel);
4079086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            }
4089086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        }
4099086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        if (workSource == null) {
4109086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            // Make sure we always have a workSource indicating the origin of the scan
4119086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            // hence if there is none, pick an internal WifiStateMachine one
4129086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            workSource = new WorkSource(WifiStateMachine.DFS_RESTRICTED_SCAN_REQUEST);
4139086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        }
4149086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        startScan(settings, workSource);
4159086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    }
4169086afccf6938a49eb9a2cd248917c1cb0943942vandwalle
417a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    /**
418a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * see {@link android.net.wifi.WifiManager#startScan}
419a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * and {@link android.net.wifi.WifiManager#startCustomizedScan}
420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
421a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * @param settings If null, use default parameter, i.e. full scan.
422a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * @param workSource If null, all blame is given to the calling uid.
423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
424a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public void startScan(ScanSettings settings, WorkSource workSource) {
425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
426a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        if (settings != null) {
427a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            settings = new ScanSettings(settings);
428a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (!settings.isValid()) {
429a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng                Slog.e(TAG, "invalid scan setting");
430a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng                return;
431a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            }
432a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        }
433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (workSource != null) {
434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            enforceWorkSourcePermission();
435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // WifiManager currently doesn't use names, so need to clear names out of the
436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // supplied WorkSource to allow future WorkSource combining.
437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            workSource.clearNames();
438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
439ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle        mWifiStateMachine.startScan(Binder.getCallingUid(), scanRequestCounter++,
440ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle                settings, workSource);
441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isBatchedScanSupported() {
444ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande        return false;
445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
447ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande    public void pollBatchedScan() { }
448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4493f7ef65ab71619040032aee96b5599849881d6fdAndres Morales    public String getWpsNfcConfigurationToken(int netId) {
450f3f4029b3ac41da4cc6a1bc22bae47c750a47048Andres Morales        enforceConnectivityInternalPermission();
4513f7ef65ab71619040032aee96b5599849881d6fdAndres Morales        return mWifiStateMachine.syncGetWpsNfcConfigurationToken(netId);
4523f7ef65ab71619040032aee96b5599849881d6fdAndres Morales    }
4533f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#requestBatchedScan()}
456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean requestBatchedScan(BatchedScanSettings requested, IBinder binder,
458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WorkSource workSource) {
459ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande        return false;
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<BatchedScanResult> getBatchedScanResults(String callingPackage) {
463ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande        return null;
464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
466ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande    public void stopBatchedScan(BatchedScanSettings settings) { }
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceAccessPermission() {
469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
470a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande                "WifiService");
471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceChangePermission() {
474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                                "WifiService");
4769086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    }
477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4789086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    private void enforceLocationHardwarePermission() {
4799086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        mContext.enforceCallingOrSelfPermission(Manifest.permission.LOCATION_HARDWARE,
4809086afccf6938a49eb9a2cd248917c1cb0943942vandwalle                "LocationHardware");
481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4839878c61bbd81176561991be025af44efc67332feWenchao Tong    private void enforceReadCredentialPermission() {
4849878c61bbd81176561991be025af44efc67332feWenchao Tong        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.READ_WIFI_CREDENTIAL,
4859878c61bbd81176561991be025af44efc67332feWenchao Tong                                                "WifiService");
4869878c61bbd81176561991be025af44efc67332feWenchao Tong    }
4879878c61bbd81176561991be025af44efc67332feWenchao Tong
488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceWorkSourcePermission() {
489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingPermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
490a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande                "WifiService");
491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceMulticastChangePermission() {
495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(
496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                "WifiService");
498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceConnectivityInternalPermission() {
501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(
502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                android.Manifest.permission.CONNECTIVITY_INTERNAL,
503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                "ConnectivityService");
504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enable {@code true} to enable, {@code false} to disable.
509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the enable/disable operation was
510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         started or is already in the queue.
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public synchronized boolean setWifiEnabled(boolean enable) {
513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid()
515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + ", uid=" + Binder.getCallingUid());
516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) {
517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * Caller might not have WRITE_SECURE_SETTINGS,
522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * only CHANGE_WIFI_STATE is enforced
523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        */
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (! mSettingsStore.handleWifiToggled(enable)) {
528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Nothing to do if wifi cannot be toggled
529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return true;
530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiController.sendMessage(CMD_WIFI_TOGGLED);
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiState()}
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_DISABLING},
543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_ENABLED},
544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_ENABLING},
545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_UNKNOWN}
546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getWifiEnabledState() {
548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiState();
550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param wifiConfig SSID, security and channel details as
555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *        part of WifiConfiguration
556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enabled true to enable and false to disable
557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
559328fc5407927799843c11f2b767a8cf47b89f366Robert Greenwalt        enforceChangePermission();
56034f173a54307c39ffe5dfab52e7ef8166a00539eRobert Greenwalt        ConnectivityManager.enforceTetherChangePermission(mContext);
56113cddb5337418359eb5f9bebd0504fbc5c47fc41Julia Reynolds        UserManager um = UserManager.get(mContext);
56213cddb5337418359eb5f9bebd0504fbc5c47fc41Julia Reynolds        if (um.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
56313cddb5337418359eb5f9bebd0504fbc5c47fc41Julia Reynolds            throw new SecurityException("DISALLOW_CONFIG_TETHERING is enabled for this user.");
56413cddb5337418359eb5f9bebd0504fbc5c47fc41Julia Reynolds        }
565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // null wifiConfig is a meaningful input for CMD_SET_AP
5664d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (wifiConfig == null || isValid(wifiConfig)) {
567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiController.obtainMessage(CMD_SET_AP, enabled ? 1 : 0, 0, wifiConfig).sendToTarget();
568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invalid WifiConfiguration");
570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiApState()}
575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_DISABLING},
577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_ENABLED},
578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_ENABLING},
579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_FAILED}
580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getWifiApEnabledState() {
582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiApState();
584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiApConfiguration()}
588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return soft access point configuration
589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiConfiguration getWifiApConfiguration() {
591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiApConfiguration();
593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
596a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist     * see {@link WifiManager#buildWifiConfig()}
597a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist     * @return a WifiConfiguration.
598a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist     */
599a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist    public WifiConfiguration buildWifiConfig(String uriString, String mimeType, byte[] data) {
6004e940bb499f65b4305dbeb2e01237c43c2b0b42dJan Nordqvist        if (mimeType.equals(ConfigBuilder.WifiConfigType)) {
601a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            try {
602a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist                return ConfigBuilder.buildConfig(uriString, data, mContext);
603a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            }
604a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            catch (IOException | GeneralSecurityException | SAXException e) {
605a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist                Log.e(TAG, "Failed to parse wi-fi configuration: " + e);
606a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            }
607a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        }
608a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        else {
609a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            Log.i(TAG, "Unknown wi-fi config type: " + mimeType);
610a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        }
611a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        return null;
612a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist    }
613a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist
614a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist    /**
615155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param wifiConfig WifiConfiguration details for soft access point
617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (wifiConfig == null)
621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
6224d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (isValid(wifiConfig)) {
623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.setWifiApConfiguration(wifiConfig);
624155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
625155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invalid WifiConfiguration");
626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enable {@code true} to enable, {@code false} to disable.
631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the enable/disable operation was
632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         started or is already in the queue.
633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isScanAlwaysAvailable() {
635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mSettingsStore.isScanAlwaysAvailable();
637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
638155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
639155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
640155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#disconnect()}
641155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void disconnect() {
643155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
644155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.disconnectCommand();
645155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
646155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#reconnect()}
649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void reconnect() {
651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reconnectCommand();
653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#reassociate()}
657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void reassociate() {
659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reassociateCommand();
661155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
663155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
664048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande     * see {@link android.net.wifi.WifiManager#getSupportedFeatures}
665a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande     */
666048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande    public int getSupportedFeatures() {
667a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        enforceAccessPermission();
668a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        if (mWifiStateMachineChannel != null) {
669048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            return mWifiStateMachine.syncGetSupportedFeatures(mWifiStateMachineChannel);
670a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        } else {
671a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
672048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            return 0;
673a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        }
674a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
675a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande
676a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    /**
677200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle     * see {@link android.net.wifi.WifiAdapter#reportActivityInfo}
678200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle     */
679048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande    public WifiActivityEnergyInfo reportActivityInfo() {
680200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        enforceAccessPermission();
681200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        WifiLinkLayerStats stats;
682200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        WifiActivityEnergyInfo energyInfo = null;
683200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (mWifiStateMachineChannel != null) {
684048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            stats = mWifiStateMachine.syncGetLinkLayerStats(mWifiStateMachineChannel);
685200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            if (stats != null) {
68684a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle
68784a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int rxIdleCurrent =
68884a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                     mContext.getResources().getInteger(
6892740001002132ed305034de64a58b0805fa4e392Pierre Vandwalle                             com.android.internal.R.integer.config_wifi_idle_receive_cur_ma);
69084a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int rxCurrent =
69184a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                     mContext.getResources().getInteger(
69284a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                             com.android.internal.R.integer.config_wifi_active_rx_cur_ma);
69384a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int txCurrent =
69484a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                     mContext.getResources().getInteger(
69584a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                             com.android.internal.R.integer.config_wifi_tx_cur_ma);
69684a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int voltage =
69784a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                     mContext.getResources().getInteger(
69884a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                             com.android.internal.R.integer.config_wifi_operating_voltage_mv);
69984a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int rxIdleTime = stats.on_time - stats.tx_time - stats.rx_time;
70084a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle
70184a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                int energyUsed = (stats.tx_time * txCurrent + stats.rx_time * rxCurrent
70284a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                        + rxIdleTime * rxIdleCurrent ) * voltage / 1000;
70384a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle
704200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle                // Convert the LinkLayerStats into EnergyActivity
705200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle                energyInfo = new WifiActivityEnergyInfo(
706200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle                        WifiActivityEnergyInfo.STACK_STATE_STATE_IDLE, stats.tx_time,
70784a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle                        stats.rx_time, rxIdleTime, energyUsed);
708200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            }
709200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            return energyInfo;
710200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        } else {
711200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
712200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            return null;
713200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        }
714200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle    }
715200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle
716200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle    /**
717155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
718155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the list of configured networks
719155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
720155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<WifiConfiguration> getConfiguredNetworks() {
721155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
722155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
72382199a285f4a45a46b44eb8253999aa918534753vandwalle            return mWifiStateMachine.syncGetConfiguredNetworks(Binder.getCallingUid(),
72482199a285f4a45a46b44eb8253999aa918534753vandwalle                    mWifiStateMachineChannel);
725155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
726155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return null;
728155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
729155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
730155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
7329878c61bbd81176561991be025af44efc67332feWenchao Tong     * see {@link android.net.wifi.WifiManager#getPrivilegedConfiguredNetworks()}
7339878c61bbd81176561991be025af44efc67332feWenchao Tong     * @return the list of configured networks with real preSharedKey
7349878c61bbd81176561991be025af44efc67332feWenchao Tong     */
7359878c61bbd81176561991be025af44efc67332feWenchao Tong    public List<WifiConfiguration> getPrivilegedConfiguredNetworks() {
7369878c61bbd81176561991be025af44efc67332feWenchao Tong        enforceReadCredentialPermission();
7379878c61bbd81176561991be025af44efc67332feWenchao Tong        enforceAccessPermission();
7389878c61bbd81176561991be025af44efc67332feWenchao Tong        if (mWifiStateMachineChannel != null) {
7399878c61bbd81176561991be025af44efc67332feWenchao Tong            return mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel);
7409878c61bbd81176561991be025af44efc67332feWenchao Tong        } else {
7419878c61bbd81176561991be025af44efc67332feWenchao Tong            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
7429878c61bbd81176561991be025af44efc67332feWenchao Tong            return null;
7439878c61bbd81176561991be025af44efc67332feWenchao Tong        }
7449878c61bbd81176561991be025af44efc67332feWenchao Tong    }
7459878c61bbd81176561991be025af44efc67332feWenchao Tong
7469878c61bbd81176561991be025af44efc67332feWenchao Tong    /**
747ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * Returns a WifiConfiguration matching this ScanResult
748ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * @param scanResult scanResult that represents the BSSID
749ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * @return {@link WifiConfiguration} that matches this BSSID or null
750ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     */
751ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    public WifiConfiguration getMatchingWifiConfig(ScanResult scanResult) {
752ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande        enforceAccessPermission();
753ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande        return mWifiStateMachine.syncGetMatchingWifiConfig(scanResult, mWifiStateMachineChannel);
754ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    }
755ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande
756ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande
757ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    /**
758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the supplicant-assigned identifier for the new or updated
760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * network if the operation succeeds, or {@code -1} if it fails
761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addOrUpdateNetwork(WifiConfiguration config) {
763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
7644d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (isValid(config)) {
765992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            //TODO: pass the Uid the WifiStateMachine as a message parameter
766992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            Slog.e("addOrUpdateNetwork", " uid = " + Integer.toString(Binder.getCallingUid())
767992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + " SSID " + config.SSID
768992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + " nid=" + Integer.toString(config.networkId));
769992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            if (config.networkId == WifiConfiguration.INVALID_NETWORK_ID) {
770992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                config.creatorUid = Binder.getCallingUid();
771992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            } else {
772992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                config.lastUpdateUid = Binder.getCallingUid();
773992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            }
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mWifiStateMachineChannel != null) {
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return -1;
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "bad network configuration");
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return -1;
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     /**
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
794c35d728a15e9270f5642ef79f5245c89d749285fSky Faber
795002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        if (!isOwner(Binder.getCallingUid())) {
796002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            Slog.e(TAG, "Remove is not authorized for user");
797002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            return false;
798002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        }
799002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param disableOthers if true, disable all other networks.
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
815155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean enableNetwork(int netId, boolean disableOthers) {
816155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    disableOthers);
820155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
821155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
823155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
824155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
825155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
826155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
827155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
828155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
829155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
830155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
831155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
841155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
842155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
843155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
844155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the Wi-Fi information, contained in {@link WifiInfo}.
845155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
846155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiInfo getConnectionInfo() {
847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
848155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
849155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * Make sure we have the latest information, by sending
850155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * a status request to the supplicant.
851155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
852155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncRequestConnectionInfo();
853155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
854155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
855155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
856155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Return the results of the most recent access point scan, in the form of
857155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a list of {@link ScanResult} objects.
858155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the list of results
859155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
860155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<ScanResult> getScanResults(String callingPackage) {
861155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
862155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int userId = UserHandle.getCallingUserId();
863155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
864f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk        boolean hasInteractUsersFull = checkInteractAcrossUsersFull();
865155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, uid, callingPackage)
868155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    != AppOpsManager.MODE_ALLOWED) {
869155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return new ArrayList<ScanResult>();
870155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
871f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk            if (!isCurrentProfile(userId) && !hasInteractUsersFull) {
8721ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber                return new ArrayList<ScanResult>();
8731ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber            }
874fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina            return mWifiStateMachine.syncGetScanResultsList();
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
877155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
878155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
880155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
881f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk     * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
882f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk     */
883f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk    private boolean checkInteractAcrossUsersFull() {
884f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk        return mContext.checkCallingOrSelfPermission(
885f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk                android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
886f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk                == PackageManager.PERMISSION_GRANTED;
887f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk    }
888f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk
889f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk    /**
890b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy     * Returns true if the calling user is the current one or a profile of the
891b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy     * current user..
892fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina     */
8931ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber    private boolean isCurrentProfile(int userId) {
894fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        int currentUser = ActivityManager.getCurrentUser();
895fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        if (userId == currentUser) {
896fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina            return true;
897fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        }
898b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy        List<UserInfo> profiles = UserManager.get(mContext).getProfiles(currentUser);
899b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy        for (UserInfo user : profiles) {
900b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy            if (userId == user.id) {
901fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina                return true;
902fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina            }
903fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        }
904fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        return false;
905fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina    }
906fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina
907fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina    /**
9082a325cd07d40941adb93c1284abb263ba650c213Sky Faber     * Returns true if uid is an application running under the owner or a profile of the owner.
909002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber     *
910002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber     * Note: Should not be called if identity is cleared.
911002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber     */
9122a325cd07d40941adb93c1284abb263ba650c213Sky Faber    private boolean isOwner(int uid) {
913002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        long ident = Binder.clearCallingIdentity();
9142a325cd07d40941adb93c1284abb263ba650c213Sky Faber        int userId = UserHandle.getUserId(uid);
915002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        try {
916002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            int ownerUser = UserHandle.USER_OWNER;
917002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            if (userId == ownerUser) {
918002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                return true;
919002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            }
920002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            List<UserInfo> profiles = UserManager.get(mContext).getProfiles(ownerUser);
921002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            for (UserInfo profile : profiles) {
922002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                if (userId == profile.id) {
923002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    return true;
924002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                }
925002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            }
926002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            return false;
927002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        }
928002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        finally {
929002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            Binder.restoreCallingIdentity(ident);
930002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        }
931002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber    }
932002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber
933002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber
934002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber    /**
935155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Tell the supplicant to persist the current list of configured networks.
936155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
937155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
938155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TODO: deprecate this
939155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
940155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfiguration() {
941155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean result = true;
942155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
943155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
944155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
945155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
946155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
947155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
948155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
949155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
950155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
951155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
952155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Set the country code
953155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param countryCode ISO 3166 country code.
954155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param persist {@code true} if the setting should be remembered.
955155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
956155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The persist behavior exists so that wifi can fall back to the last
957155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * persisted country code on a restart, when the locale information is
958155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * not available from telephony.
959155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
960155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setCountryCode(String countryCode, boolean persist) {
961155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
962155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                " with persist set to " + persist);
9639c22c8ea193360f50ad490368ca6a11af0684362Robert Greenwalt        enforceConnectivityInternalPermission();
964155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long token = Binder.clearCallingIdentity();
965155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
966155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.setCountryCode(countryCode, persist);
967155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
968155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(token);
969155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
970155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
971155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
97280d14d67a956e6c69433470aaa73100898166efaxinhe     /**
97380d14d67a956e6c69433470aaa73100898166efaxinhe     * Get the country code
97480d14d67a956e6c69433470aaa73100898166efaxinhe     * @return ISO 3166 country code.
97580d14d67a956e6c69433470aaa73100898166efaxinhe     */
97680d14d67a956e6c69433470aaa73100898166efaxinhe    public String getCountryCode() {
97780d14d67a956e6c69433470aaa73100898166efaxinhe        enforceConnectivityInternalPermission();
97880d14d67a956e6c69433470aaa73100898166efaxinhe        String country = mWifiStateMachine.getCountryCode();
97980d14d67a956e6c69433470aaa73100898166efaxinhe        return country;
98080d14d67a956e6c69433470aaa73100898166efaxinhe    }
981155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
982155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Set the operational frequency band
983155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param band One of
984155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
985155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
986155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
987155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param persist {@code true} if the setting should be remembered.
988155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
989155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
990155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setFrequencyBand(int band, boolean persist) {
991155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
992155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!isDualBandSupported()) return;
993155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.i(TAG, "WifiService trying to set frequency band to " + band +
994155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                " with persist set to " + persist);
995155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long token = Binder.clearCallingIdentity();
996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.setFrequencyBand(band, persist);
998155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
999155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(token);
1000155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1001155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1002155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1003155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1004155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1005155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Get the operational frequency band
1006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1007155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getFrequencyBand() {
1008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.getFrequencyBand();
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isDualBandSupported() {
1013155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //TODO: Should move towards adding a driver API that checks at runtime
1014155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mContext.getResources().getBoolean(
1015155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                com.android.internal.R.bool.config_wifi_dual_band_support);
1016155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1017155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1018155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1019155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Return the DHCP-assigned addresses from the last successful DHCP request,
1020155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * if any.
1021155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the DHCP information
1022155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @deprecated
1023155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public DhcpInfo getDhcpInfo() {
1025155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1026155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults();
1027155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1028155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DhcpInfo info = new DhcpInfo();
10293b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
10303b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (dhcpResults.ipAddress != null &&
10313b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                dhcpResults.ipAddress.getAddress() instanceof Inet4Address) {
10323b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.ipAddress.getAddress());
1033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
10343b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
10353b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (dhcpResults.gateway != null) {
10363b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            info.gateway = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.gateway);
1037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
10383b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
1039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int dnsFound = 0;
10403b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        for (InetAddress dns : dhcpResults.dnsServers) {
1041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (dns instanceof Inet4Address) {
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (dnsFound == 0) {
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address)dns);
1044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address)dns);
1046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (++dnsFound > 1) break;
1048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        InetAddress serverAddress = dhcpResults.serverAddress;
1051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (serverAddress instanceof Inet4Address) {
1052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            info.serverAddress = NetworkUtils.inetAddressToInt((Inet4Address)serverAddress);
1053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        info.leaseDuration = dhcpResults.leaseDuration;
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return info;
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#startWifi}
1061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startWifi() {
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceConnectivityInternalPermission();
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* TODO: may be add permissions for access only to connectivity service
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * TODO: if a start issued, keep wifi alive until a stop issued irrespective
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * of WifiLock & device idle status unless wifi enabled status is toggled
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.setDriverStart(true);
1071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reconnectCommand();
1072155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1073155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1074155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1075155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#stopWifi}
1076155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1077155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void stopWifi() {
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceConnectivityInternalPermission();
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * TODO: if a stop is issued, wifi is brought up only by startWifi
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * unless wifi enabled status is toggled
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.setDriverStart(false);
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#addToBlacklist}
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1091155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void addToBlacklist(String bssid) {
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.addToBlacklist(bssid);
1095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#clearBlacklist}
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void clearBlacklist() {
1102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.clearBlacklist();
1105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * enable TDLS for the local NIC to remote NIC
1109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The APPs don't know the remote MAC address to identify NIC though,
1110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * so we need to do additional work to find it from remote IP address
1111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class TdlsTaskParams {
1114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public String remoteIpAddress;
1115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public boolean enable;
1116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class TdlsTask extends AsyncTask<TdlsTaskParams, Integer, Integer> {
1119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
1120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        protected Integer doInBackground(TdlsTaskParams... params) {
1121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Retrieve parameters for the call
1123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            TdlsTaskParams param = params[0];
1124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String remoteIpAddress = param.remoteIpAddress.trim();
1125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean enable = param.enable;
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Get MAC address of Remote IP
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String macAddress = null;
1129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            BufferedReader reader = null;
1131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
1133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                reader = new BufferedReader(new FileReader("/proc/net/arp"));
1134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Skip over the line bearing colum titles
1136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String line = reader.readLine();
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                while ((line = reader.readLine()) != null) {
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String[] tokens = line.split("[ ]+");
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (tokens.length < 6) {
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        continue;
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // ARP column format is
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Address HWType HWAddress Flags Mask IFace
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String ip = tokens[0];
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String mac = tokens[3];
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (remoteIpAddress.equals(ip)) {
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        macAddress = mac;
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (macAddress == null) {
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.w(TAG, "Did not find remoteAddress {" + remoteIpAddress + "} in " +
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            "/proc/net/arp");
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    enableTdlsWithMacAddress(macAddress, enable);
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (FileNotFoundException e) {
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "Could not open /proc/net/arp to lookup mac address");
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (IOException e) {
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "Could not read /proc/net/arp to lookup mac address");
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } finally {
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (reader != null) {
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        reader.close();
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                catch (IOException e) {
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Do nothing
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return 0;
1178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableTdls(String remoteAddress, boolean enable) {
11828e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        if (remoteAddress == null) {
11838e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer          throw new IllegalArgumentException("remoteAddress cannot be null");
11848e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        }
11858e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        TdlsTaskParams params = new TdlsTaskParams();
1187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        params.remoteIpAddress = remoteAddress;
1188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        params.enable = enable;
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        new TdlsTask().execute(params);
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableTdlsWithMacAddress(String remoteMacAddress, boolean enable) {
11948e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        if (remoteMacAddress == null) {
11958e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer          throw new IllegalArgumentException("remoteMacAddress cannot be null");
11968e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        }
11978e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.enableTdls(remoteMacAddress, enable);
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Get a reference to handler. This is used by a client to establish
1203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * an AsyncChannel communication with WifiService
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public Messenger getWifiServiceMessenger() {
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return new Messenger(mClientHandler);
1209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
12112ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    /**
12122ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle     * Disable an ephemeral network, i.e. network that is created thru a WiFi Scorer
12132ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle     */
12142ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    public void disableEphemeralNetwork(String SSID) {
12152ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        enforceAccessPermission();
12162ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        enforceChangePermission();
12172ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        mWifiStateMachine.disableEphemeralNetwork(SSID);
12182ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    }
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Get the IP and proxy configuration file
1222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getConfigFile() {
1224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.getConfigFile();
1226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
1230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void onReceive(Context context, Intent intent) {
1231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String action = intent.getAction();
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (action.equals(Intent.ACTION_SCREEN_ON)) {
1233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCREEN_ON);
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_USER_PRESENT);
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCREEN_OFF);
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int pluggedType = intent.getIntExtra("plugged", 0);
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_BATTERY_CHANGED, pluggedType, 0, null);
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        BluetoothAdapter.STATE_DISCONNECTED);
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiStateMachine.sendBluetoothAdapterStateChange(state);
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                boolean emergencyMode = intent.getBooleanExtra("phoneinECMState", false);
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, emergencyMode ? 1 : 0, 0);
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    };
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Observes settings changes to scan always mode.
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void registerForScanModeChange() {
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        ContentObserver contentObserver = new ContentObserver(null) {
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            @Override
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            public void onChange(boolean selfChange) {
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mSettingsStore.handleWifiScanAlwaysAvailableToggled();
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCAN_ALWAYS_MODE_CHANGED);
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        };
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.getContentResolver().registerContentObserver(
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Settings.Global.getUriFor(Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE),
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                false, contentObserver);
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void registerForBroadcasts() {
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        IntentFilter intentFilter = new IntentFilter();
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_USER_PRESENT);
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.registerReceiver(mReceiver, intentFilter);
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    @Override
1282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                != PackageManager.PERMISSION_GRANTED) {
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            pw.println("Permission Denial: can't dump WifiService from from pid="
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + Binder.getCallingPid()
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + ", uid=" + Binder.getCallingUid());
1288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
1289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
1291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Stay-awake conditions: " +
1292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Settings.Global.getInt(mContext.getContentResolver(),
1293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                       Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0));
1294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("mMulticastEnabled " + mMulticastEnabled);
1295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("mMulticastDisabled " + mMulticastDisabled);
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiController.dump(fd, pw, args);
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mSettingsStore.dump(fd, pw, args);
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mNotificationController.dump(fd, pw, args);
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTrafficPoller.dump(fd, pw, args);
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Latest scan results:");
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
1303a0708b09ad17b086c008ab100aec7143d7613c80vandwalle        long nowMs = System.currentTimeMillis();
1304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (scanResults != null && scanResults.size() != 0) {
1305a0708b09ad17b086c008ab100aec7143d7613c80vandwalle            pw.println("    BSSID              Frequency  RSSI    Age      SSID " +
1306a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                    "                                Flags");
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (ScanResult r : scanResults) {
1308a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                long ageSec = 0;
1309a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                long ageMilli = 0;
1310a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                if (nowMs > r.seen && r.seen > 0) {
1311a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                    ageSec = (nowMs - r.seen) / 1000;
1312a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                    ageMilli = (nowMs - r.seen) % 1000;
1313a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                }
1314a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                String candidate = " ";
1315a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                if (r.isAutoJoinCandidate > 0) candidate = "+";
1316a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                pw.printf("  %17s  %9d  %5d  %3d.%03d%s   %-32s  %s\n",
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                         r.BSSID,
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                         r.frequency,
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                         r.level,
1320a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         ageSec, ageMilli,
1321a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         candidate,
1322a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         r.SSID == null ? "" : r.SSID,
1323a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         r.capabilities);
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mFullHighPerfLocksAcquired + " full high perf, " +
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mScanLocksAcquired + " scan");
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Locks released: " + mFullLocksReleased + " full, " +
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mFullHighPerfLocksReleased + " full high perf, " +
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mScanLocksReleased + " scan");
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Locks held:");
1335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mLocks.dump(pw);
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiWatchdogStateMachine.dump(fd, pw, args);
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.dump(fd, pw, args);
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class WifiLock extends DeathRecipient {
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(lockMode, tag, binder, ws);
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void binderDied() {
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            synchronized (mLocks) {
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                releaseWifiLockLocked(mBinder);
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public String toString() {
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class LockList {
1360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private List<WifiLock> mList;
1361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private LockList() {
1363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mList = new ArrayList<WifiLock>();
1364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized boolean hasLocks() {
1367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return !mList.isEmpty();
1368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized int getStrongestLockMode() {
1371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mList.isEmpty()) {
1372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return WifiManager.WIFI_MODE_FULL;
1373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
1377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mFullLocksAcquired > mFullLocksReleased) {
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return WifiManager.WIFI_MODE_FULL;
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return WifiManager.WIFI_MODE_SCAN_ONLY;
1384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized void updateWorkSource(WorkSource ws) {
1387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (int i = 0; i < mLocks.mList.size(); i++) {
1388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ws.add(mLocks.mList.get(i).mWorkSource);
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private void addLock(WifiLock lock) {
1393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (findLockByBinder(lock.mBinder) < 0) {
1394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mList.add(lock);
1395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private WifiLock removeLock(IBinder binder) {
1399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int index = findLockByBinder(binder);
1400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (index >= 0) {
1401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiLock ret = mList.remove(index);
1402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ret.unlinkDeathRecipient();
1403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return ret;
1404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return null;
1406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private int findLockByBinder(IBinder binder) {
1410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int size = mList.size();
1411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (int i = size - 1; i >= 0; i--) {
1412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (mList.get(i).mBinder == binder)
1413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return i;
1414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return -1;
1416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private void dump(PrintWriter pw) {
1419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (WifiLock l : mList) {
1420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                pw.print("    ");
1421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                pw.println(l);
1422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void enforceWakeSourcePermission(int uid, int pid) {
1427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (uid == android.os.Process.myUid()) {
1428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
1429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                pid, uid, null);
1432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
1435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (lockMode != WifiManager.WIFI_MODE_FULL &&
1437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null && ws.size() == 0) {
1444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ws = null;
1445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null) {
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws == null) {
1450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ws = new WorkSource(Binder.getCallingUid());
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLocks) {
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return acquireWifiLockLocked(wifiLock);
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch(wifiLock.mMode) {
1460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL:
1461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_SCAN_ONLY:
1463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch(wifiLock.mMode) {
1470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL:
1471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_SCAN_ONLY:
1473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean acquireWifiLockLocked(WifiLock wifiLock) {
1479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
1480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mLocks.addLock(wifiLock);
1482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
1484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            noteAcquireWifiLock(wifiLock);
1486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch(wifiLock.mMode) {
1487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL:
1488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++mFullLocksAcquired;
1489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++mFullHighPerfLocksAcquired;
1492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_SCAN_ONLY:
1495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++mScanLocksAcquired;
1496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiController.sendMessage(CMD_LOCKS_CHANGED);
1499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return true;
1500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
1509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int pid = Binder.getCallingPid();
1510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null && ws.size() == 0) {
1511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ws = null;
1512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null) {
1514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            enforceWakeSourcePermission(uid, pid);
1515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
1517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            synchronized (mLocks) {
1519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int index = mLocks.findLockByBinder(lock);
1520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (index < 0) {
1521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    throw new IllegalArgumentException("Wifi lock not active");
1522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiLock wl = mLocks.mList.get(index);
1524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                noteReleaseWifiLock(wl);
1525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                noteAcquireWifiLock(wl);
1527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean releaseWifiLock(IBinder lock) {
1535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLocks) {
1537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return releaseWifiLockLocked(lock);
1538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean releaseWifiLockLocked(IBinder lock) {
1542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean hadLock;
1543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiLock wifiLock = mLocks.removeLock(lock);
1545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
1547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        hadLock = (wifiLock != null);
1549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
1551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (hadLock) {
1553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                noteReleaseWifiLock(wifiLock);
1554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                switch(wifiLock.mMode) {
1555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    case WifiManager.WIFI_MODE_FULL:
1556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        ++mFullLocksReleased;
1557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        ++mFullHighPerfLocksReleased;
1560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    case WifiManager.WIFI_MODE_SCAN_ONLY:
1562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        ++mScanLocksReleased;
1563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_LOCKS_CHANGED);
1566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return hadLock;
1573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private abstract class DeathRecipient
1576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            implements IBinder.DeathRecipient {
1577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String mTag;
1578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int mMode;
1579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        IBinder mBinder;
1580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WorkSource mWorkSource;
1581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
1583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super();
1584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mTag = tag;
1585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMode = mode;
1586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBinder = binder;
1587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWorkSource = ws;
1588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
1589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mBinder.linkToDeath(this, 0);
1590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (RemoteException e) {
1591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                binderDied();
1592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        void unlinkDeathRecipient() {
1596155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBinder.unlinkToDeath(this, 0);
1597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1600155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class Multicaster extends DeathRecipient {
1601155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Multicaster(String tag, IBinder binder) {
1602155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(Binder.getCallingUid(), tag, binder, null);
1603155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void binderDied() {
1606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Multicaster binderDied");
1607155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            synchronized (mMulticasters) {
1608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int i = mMulticasters.indexOf(this);
1609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (i != -1) {
1610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    removeMulticasterLocked(i, mMode);
1611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1614155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1615155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public String toString() {
1616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return "Multicaster{" + mTag + " binder=" + mBinder + "}";
1617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public int getUid() {
1620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mMode;
1621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1622155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1624155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void initializeMulticastFiltering() {
1625155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // if anybody had requested filters be off, leave off
1629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mMulticasters.size() != 0) {
1630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return;
1631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiStateMachine.startFilteringMulticastV4Packets();
1633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void acquireMulticastLock(IBinder binder, String tag) {
1638155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1639155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1640155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1641155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMulticastEnabled++;
1642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMulticasters.add(new Multicaster(tag, binder));
1643155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Note that we could call stopFilteringMulticastV4Packets only when
1644155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // our new size == 1 (first call), but this function won't
1645155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // be called often and by making the stopPacket call each
1646155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // time we're less fragile and self-healing.
1647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.stopFilteringMulticastV4Packets();
1648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
1651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long ident = Binder.clearCallingIdentity();
1652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBatteryStats.noteWifiMulticastEnabled(uid);
1654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void releaseMulticastLock() {
1661155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1663155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
1664155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1665155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMulticastDisabled++;
1666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int size = mMulticasters.size();
1667155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (int i = size - 1; i >= 0; i--) {
1668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Multicaster m = mMulticasters.get(i);
1669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if ((m != null) && (m.getUid() == uid)) {
1670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    removeMulticasterLocked(i, uid);
1671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void removeMulticasterLocked(int i, int uid)
1677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    {
1678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Multicaster removed = mMulticasters.remove(i);
1679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (removed != null) {
1681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            removed.unlinkDeathRecipient();
1682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mMulticasters.size() == 0) {
1684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.startFilteringMulticastV4Packets();
1685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long ident = Binder.clearCallingIdentity();
1688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBatteryStats.noteWifiMulticastDisabled(uid);
1690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1693155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1696155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isMulticastEnabled() {
1697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return (mMulticasters.size() > 0);
1701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
17039ae6b45c038fa74a9e7285ef4834551dd93da332Yuhao Zheng
1704511a412adce5a46c41e61f90c0df6de5fb4e491cYuhao Zheng    public WifiMonitor getWifiMonitor() {
1705511a412adce5a46c41e61f90c0df6de5fb4e491cYuhao Zheng        return mWifiStateMachine.getWifiMonitor();
17069ae6b45c038fa74a9e7285ef4834551dd93da332Yuhao Zheng    }
1707ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
1708ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    public void enableVerboseLogging(int verbose) {
1709ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        enforceAccessPermission();
1710ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        mWifiStateMachine.enableVerboseLogging(verbose);
1711ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
1712ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
1713ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    public int getVerboseLoggingLevel() {
1714ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        enforceAccessPermission();
1715ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        return mWifiStateMachine.getVerboseLoggingLevel();
1716ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
1717c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1718c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public void enableAggressiveHandover(int enabled) {
1719c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1720c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        mWifiStateMachine.enableAggressiveHandover(enabled);
1721c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1722c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1723c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public int getAggressiveHandover() {
1724c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1725c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        return mWifiStateMachine.getAggressiveHandover();
1726c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1727c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1728c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public void setAllowScansWithTraffic(int enabled) {
1729c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1730c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        mWifiStateMachine.setAllowScansWithTraffic(enabled);
1731c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1732c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1733c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public int getAllowScansWithTraffic() {
1734c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1735c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        return mWifiStateMachine.getAllowScansWithTraffic();
1736c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1737c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
1738b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public void setAllowScansWhileAssociated(int enabled) {
1739e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle        enforceAccessPermission();
1740e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle        mWifiStateMachine.setAllowScansWhileAssociated(enabled);
1741e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle    }
1742e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle
1743b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public int getAllowScansWhileAssociated() {
1744e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle        enforceAccessPermission();
1745e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle        return mWifiStateMachine.getAllowScansWhileAssociated();
1746e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle    }
1747e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle
1748b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public void setHalBasedAutojoinOffload(int enabled) {
1749b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        enforceAccessPermission();
1750b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        mWifiStateMachine.setHalBasedAutojoinOffload(enabled);
1751b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    }
1752b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1753b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public int getHalBasedAutojoinOffload() {
1754b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        enforceAccessPermission();
1755b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        return mWifiStateMachine.getHalBasedAutojoinOffload();
1756b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    }
1757b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1758b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public void setAllowNetworkSwitchingWhileAssociated(int enabled) {
1759b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        enforceAccessPermission();
1760b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        mWifiStateMachine.setAllowNetworkSwitchingWhileAssociated(enabled);
1761b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    }
1762b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1763b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public int getAllowNetworkSwitchingWhileAssociated() {
1764b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        enforceAccessPermission();
1765b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        return mWifiStateMachine.getAllowNetworkSwitchingWhileAssociated();
1766b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    }
1767b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1768c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    /* Return the Wifi Connection statistics object */
1769c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    public WifiConnectionStatistics getConnectionStatistics() {
1770c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        enforceAccessPermission();
1771c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        enforceReadCredentialPermission();
1772c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        if (mWifiStateMachineChannel != null) {
1773c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            return mWifiStateMachine.syncGetConnectionStatistics(mWifiStateMachineChannel);
1774c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        } else {
1775c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1776c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            return null;
1777c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        }
1778c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    }
17794d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
17804d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    /* private methods */
17814d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    boolean logAndReturnFalse(String s) {
17824d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        Log.d(TAG, s);
17834d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        return false;
17844d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    }
17854d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
17864d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    boolean isValid(WifiConfiguration config) {
17874d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
17884d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (config == null)
17894d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            return logAndReturnFalse("invalid (null) configuration");
17904d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
17914d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (config.allowedKeyManagement == null) {
17924d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            return logAndReturnFalse("invalid allowed kmgmt");
17934d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        }
17944d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
17954d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (config.allowedKeyManagement.cardinality() > 1) {
17964d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (config.allowedKeyManagement.cardinality() != 2) {
17974d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                return logAndReturnFalse("cardinality != 2");
17984d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
17994d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)) {
18004d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                return logAndReturnFalse("not WPA_EAP");
18014d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
18024d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if ((!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X))
18034d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                    && (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK))) {
18044d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                return logAndReturnFalse("not PSK or 8021X");
18054d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
18064d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        }
18074d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
18084d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (!TextUtils.isEmpty(config.FQDN)) {
18094d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must not have an SSID */
18104d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (!TextUtils.isEmpty(config.SSID)) {
18114d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                return logAndReturnFalse("no SSID");
18124d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
18134d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must have a providerFriendlyName */
18144d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (TextUtils.isEmpty(config.providerFriendlyName)) {
18154d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                return logAndReturnFalse("no provider friendly name");
18164d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
18174d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must have enterprise config */
18184d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (config.enterpriseConfig == null
18194d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                    || config.enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.NONE ) {
18204d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                return logAndReturnFalse("no enterprise config");
18214d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
18224d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must have a CA certificate */
18234d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (config.enterpriseConfig.getCaCertificate() == null) {
18244d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                return logAndReturnFalse("no ca certificate");
18254d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
18264d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        }
18274d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
18284d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        return true;
18294d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    }
1830155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
1831