blob: 60cf5a229d1e4dcdb29b4dc629ad00996f57a13c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import static android.net.wifi.WifiManager.WIFI_STATE_DISABLED;
20import static android.net.wifi.WifiManager.WIFI_STATE_DISABLING;
21import static android.net.wifi.WifiManager.WIFI_STATE_ENABLED;
22import static android.net.wifi.WifiManager.WIFI_STATE_ENABLING;
23import static android.net.wifi.WifiManager.WIFI_STATE_UNKNOWN;
24
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.app.AlarmManager;
26import android.app.PendingIntent;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070027import android.bluetooth.BluetoothA2dp;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -080028import android.bluetooth.BluetoothDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.BroadcastReceiver;
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.Intent;
33import android.content.IntentFilter;
34import android.content.pm.PackageManager;
35import android.net.wifi.IWifiManager;
36import android.net.wifi.WifiInfo;
37import android.net.wifi.WifiManager;
38import android.net.wifi.WifiNative;
39import android.net.wifi.WifiStateTracker;
40import android.net.wifi.ScanResult;
41import android.net.wifi.WifiConfiguration;
San Mehat0310f9a2009-07-07 10:49:47 -070042import android.net.wifi.SupplicantState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.NetworkStateTracker;
44import android.net.DhcpInfo;
Mike Lockwood0900f362009-07-10 17:24:07 -040045import android.net.NetworkUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Binder;
47import android.os.Handler;
48import android.os.HandlerThread;
49import android.os.IBinder;
50import android.os.Looper;
51import android.os.Message;
52import android.os.PowerManager;
Dianne Hackborn617f8772009-03-31 15:04:46 -070053import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.RemoteException;
Amith Yamasani47873e52009-07-02 12:05:32 -070055import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.provider.Settings;
57import android.util.Log;
58import android.text.TextUtils;
59
60import java.util.ArrayList;
61import java.util.BitSet;
62import java.util.HashMap;
63import java.util.LinkedHashMap;
64import java.util.List;
65import java.util.Map;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -080066import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import java.util.regex.Pattern;
68import java.io.FileDescriptor;
69import java.io.PrintWriter;
70
The Android Open Source Project10592532009-03-18 17:39:46 -070071import com.android.internal.app.IBatteryStats;
Amith Yamasani47873e52009-07-02 12:05:32 -070072import android.backup.IBackupManager;
The Android Open Source Project10592532009-03-18 17:39:46 -070073import com.android.server.am.BatteryStatsService;
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075/**
76 * WifiService handles remote WiFi operation requests by implementing
77 * the IWifiManager interface. It also creates a WifiMonitor to listen
78 * for Wifi-related events.
79 *
80 * @hide
81 */
82public class WifiService extends IWifiManager.Stub {
83 private static final String TAG = "WifiService";
84 private static final boolean DBG = false;
85 private static final Pattern scanResultPattern = Pattern.compile("\t+");
86 private final WifiStateTracker mWifiStateTracker;
87
88 private Context mContext;
89 private int mWifiState;
90
91 private AlarmManager mAlarmManager;
92 private PendingIntent mIdleIntent;
93 private static final int IDLE_REQUEST = 0;
94 private boolean mScreenOff;
95 private boolean mDeviceIdle;
96 private int mPluggedType;
97
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -070098 // true if the user enabled Wifi while in airplane mode
99 private boolean mAirplaneModeOverwridden;
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private final LockList mLocks = new LockList();
Eric Shienbrood5711fad2009-03-27 20:25:31 -0700102 // some wifi lock statistics
103 private int mFullLocksAcquired;
104 private int mFullLocksReleased;
105 private int mScanLocksAcquired;
106 private int mScanLocksReleased;
The Android Open Source Project10592532009-03-18 17:39:46 -0700107
Robert Greenwalt58ff0212009-05-19 15:53:54 -0700108 private final List<Multicaster> mMulticasters =
109 new ArrayList<Multicaster>();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700110 private int mMulticastEnabled;
111 private int mMulticastDisabled;
112
The Android Open Source Project10592532009-03-18 17:39:46 -0700113 private final IBatteryStats mBatteryStats;
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 /**
Doug Zongker43866e02010-01-07 12:09:54 -0800116 * See {@link Settings.Secure#WIFI_IDLE_MS}. This is the default value if a
117 * Settings.Secure value is not present. This timeout value is chosen as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * the approximate point at which the battery drain caused by Wi-Fi
119 * being enabled but not active exceeds the battery drain caused by
120 * re-establishing a connection to the mobile data network.
121 */
122 private static final long DEFAULT_IDLE_MILLIS = 15 * 60 * 1000; /* 15 minutes */
123
124 private static final String WAKELOCK_TAG = "WifiService";
125
126 /**
127 * The maximum amount of time to hold the wake lock after a disconnect
128 * caused by stopping the driver. Establishing an EDGE connection has been
129 * observed to take about 5 seconds under normal circumstances. This
130 * provides a bit of extra margin.
131 * <p>
132 * See {@link android.provider.Settings.Secure#WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS}.
133 * This is the default value if a Settings.Secure value is not present.
134 */
135 private static final int DEFAULT_WAKELOCK_TIMEOUT = 8000;
136
137 // Wake lock used by driver-stop operation
138 private static PowerManager.WakeLock sDriverStopWakeLock;
139 // Wake lock used by other operations
140 private static PowerManager.WakeLock sWakeLock;
141
142 private static final int MESSAGE_ENABLE_WIFI = 0;
143 private static final int MESSAGE_DISABLE_WIFI = 1;
144 private static final int MESSAGE_STOP_WIFI = 2;
145 private static final int MESSAGE_START_WIFI = 3;
146 private static final int MESSAGE_RELEASE_WAKELOCK = 4;
Robert Greenwaltf75aa36fc2009-10-22 17:03:47 -0700147 private static final int MESSAGE_UPDATE_STATE = 5;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
149 private final WifiHandler mWifiHandler;
150
151 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 * Cache of scan results objects (size is somewhat arbitrary)
153 */
154 private static final int SCAN_RESULT_CACHE_SIZE = 80;
155 private final LinkedHashMap<String, ScanResult> mScanResultCache;
156
157 /*
158 * Character buffer used to parse scan results (optimization)
159 */
160 private static final int SCAN_RESULT_BUFFER_SIZE = 512;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 private boolean mNeedReconfig;
162
Dianne Hackborn617f8772009-03-31 15:04:46 -0700163 /*
164 * Last UID that asked to enable WIFI.
165 */
166 private int mLastEnableUid = Process.myUid();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /**
169 * Number of allowed radio frequency channels in various regulatory domains.
170 * This list is sufficient for 802.11b/g networks (2.4GHz range).
171 */
172 private static int[] sValidRegulatoryChannelCounts = new int[] {11, 13, 14};
173
174 private static final String ACTION_DEVICE_IDLE =
175 "com.android.server.WifiManager.action.DEVICE_IDLE";
176
177 WifiService(Context context, WifiStateTracker tracker) {
178 mContext = context;
179 mWifiStateTracker = tracker;
Mike Lockwoodf32be162009-07-14 17:44:37 -0400180 mWifiStateTracker.enableRssiPolling(true);
The Android Open Source Project10592532009-03-18 17:39:46 -0700181 mBatteryStats = BatteryStatsService.getService();
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 mScanResultCache = new LinkedHashMap<String, ScanResult>(
184 SCAN_RESULT_CACHE_SIZE, 0.75f, true) {
185 /*
186 * Limit the cache size by SCAN_RESULT_CACHE_SIZE
187 * elements
188 */
189 public boolean removeEldestEntry(Map.Entry eldest) {
190 return SCAN_RESULT_CACHE_SIZE < this.size();
191 }
192 };
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 HandlerThread wifiThread = new HandlerThread("WifiService");
195 wifiThread.start();
196 mWifiHandler = new WifiHandler(wifiThread.getLooper());
197
198 mWifiState = WIFI_STATE_DISABLED;
199 boolean wifiEnabled = getPersistedWifiEnabled();
200
201 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
202 Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
203 mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
204
205 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
206 sWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG);
207 sDriverStopWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG);
208 mWifiStateTracker.setReleaseWakeLockCallback(
209 new Runnable() {
210 public void run() {
211 mWifiHandler.removeMessages(MESSAGE_RELEASE_WAKELOCK);
212 synchronized (sDriverStopWakeLock) {
213 if (sDriverStopWakeLock.isHeld()) {
214 sDriverStopWakeLock.release();
215 }
216 }
217 }
218 }
219 );
220
221 Log.i(TAG, "WifiService starting up with Wi-Fi " +
222 (wifiEnabled ? "enabled" : "disabled"));
223
224 mContext.registerReceiver(
225 new BroadcastReceiver() {
226 @Override
227 public void onReceive(Context context, Intent intent) {
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700228 // clear our flag indicating the user has overwridden airplane mode
229 mAirplaneModeOverwridden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 updateWifiState();
231 }
232 },
233 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
234
Dianne Hackborn617f8772009-03-31 15:04:46 -0700235 setWifiEnabledBlocking(wifiEnabled, false, Process.myUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 private boolean getPersistedWifiEnabled() {
239 final ContentResolver cr = mContext.getContentResolver();
240 try {
241 return Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON) == 1;
242 } catch (Settings.SettingNotFoundException e) {
243 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, 0);
244 return false;
245 }
246 }
247
248 private void persistWifiEnabled(boolean enabled) {
249 final ContentResolver cr = mContext.getContentResolver();
250 Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, enabled ? 1 : 0);
251 }
252
253 NetworkStateTracker getNetworkStateTracker() {
254 return mWifiStateTracker;
255 }
256
257 /**
258 * see {@link android.net.wifi.WifiManager#pingSupplicant()}
259 * @return {@code true} if the operation succeeds
260 */
261 public boolean pingSupplicant() {
262 enforceChangePermission();
263 synchronized (mWifiStateTracker) {
264 return WifiNative.pingCommand();
265 }
266 }
267
268 /**
269 * see {@link android.net.wifi.WifiManager#startScan()}
270 * @return {@code true} if the operation succeeds
271 */
Mike Lockwooda5ec95c2009-07-08 17:11:17 -0400272 public boolean startScan(boolean forceActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 enforceChangePermission();
274 synchronized (mWifiStateTracker) {
275 switch (mWifiStateTracker.getSupplicantState()) {
276 case DISCONNECTED:
277 case INACTIVE:
278 case SCANNING:
279 case DORMANT:
280 break;
281 default:
282 WifiNative.setScanResultHandlingCommand(
283 WifiStateTracker.SUPPL_SCAN_HANDLING_LIST_ONLY);
284 break;
285 }
Mike Lockwooda5ec95c2009-07-08 17:11:17 -0400286 return WifiNative.scanCommand(forceActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288 }
289
290 /**
291 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
292 * @param enable {@code true} to enable, {@code false} to disable.
293 * @return {@code true} if the enable/disable operation was
294 * started or is already in the queue.
295 */
296 public boolean setWifiEnabled(boolean enable) {
297 enforceChangePermission();
298 if (mWifiHandler == null) return false;
299
300 synchronized (mWifiHandler) {
Robert Greenwalta99f4612009-09-19 18:14:32 -0700301 // caller may not have WAKE_LOCK permission - it's not required here
302 long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 sWakeLock.acquire();
Robert Greenwalta99f4612009-09-19 18:14:32 -0700304 Binder.restoreCallingIdentity(ident);
305
Dianne Hackborn617f8772009-03-31 15:04:46 -0700306 mLastEnableUid = Binder.getCallingUid();
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700307 // set a flag if the user is enabling Wifi while in airplane mode
308 mAirplaneModeOverwridden = (enable && isAirplaneModeOn() && isAirplaneToggleable());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700309 sendEnableMessage(enable, true, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
311
312 return true;
313 }
314
315 /**
316 * Enables/disables Wi-Fi synchronously.
317 * @param enable {@code true} to turn Wi-Fi on, {@code false} to turn it off.
318 * @param persist {@code true} if the setting should be persisted.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700319 * @param uid The UID of the process making the request.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 * @return {@code true} if the operation succeeds (or if the existing state
321 * is the same as the requested state)
322 */
Dianne Hackborn617f8772009-03-31 15:04:46 -0700323 private boolean setWifiEnabledBlocking(boolean enable, boolean persist, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 final int eventualWifiState = enable ? WIFI_STATE_ENABLED : WIFI_STATE_DISABLED;
325
326 if (mWifiState == eventualWifiState) {
327 return true;
328 }
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -0700329 if (enable && isAirplaneModeOn() && !mAirplaneModeOverwridden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 return false;
331 }
332
Irfan Sheriffcd770372010-01-08 09:36:04 -0800333 /**
334 * Multiple calls to unregisterReceiver() cause exception and a system crash.
335 * This can happen if a supplicant is lost (or firmware crash occurs) and user indicates
336 * disable wifi at the same time.
337 * Avoid doing a disable when the current Wifi state is UNKNOWN
338 * TODO: Handle driver load fail and supplicant lost as seperate states
339 */
340 if (mWifiState == WIFI_STATE_UNKNOWN && !enable) {
341 return false;
342 }
343
Dianne Hackborn617f8772009-03-31 15:04:46 -0700344 setWifiEnabledState(enable ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345
346 if (enable) {
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800347 synchronized (mWifiStateTracker) {
348 if (!WifiNative.loadDriver()) {
349 Log.e(TAG, "Failed to load Wi-Fi driver.");
350 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
351 return false;
352 }
353 if (!WifiNative.startSupplicant()) {
354 WifiNative.unloadDriver();
355 Log.e(TAG, "Failed to start supplicant daemon.");
356 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
357 return false;
358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
360 registerForBroadcasts();
361 mWifiStateTracker.startEventLoop();
362 } else {
363
364 mContext.unregisterReceiver(mReceiver);
365 // Remove notification (it will no-op if it isn't visible)
366 mWifiStateTracker.setNotificationVisible(false, 0, false, 0);
367
368 boolean failedToStopSupplicantOrUnloadDriver = false;
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800369 synchronized (mWifiStateTracker) {
370 if (!WifiNative.stopSupplicant()) {
371 Log.e(TAG, "Failed to stop supplicant daemon.");
Dianne Hackborn617f8772009-03-31 15:04:46 -0700372 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 failedToStopSupplicantOrUnloadDriver = true;
374 }
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800375
Irfan Sheriff102d05f2010-02-05 14:47:27 -0800376 /**
377 * Reset connections and disable interface
378 * before we unload the driver
379 */
380 mWifiStateTracker.resetConnections(true);
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800381
382 if (!WifiNative.unloadDriver()) {
383 Log.e(TAG, "Failed to unload Wi-Fi driver.");
384 if (!failedToStopSupplicantOrUnloadDriver) {
385 setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
386 failedToStopSupplicantOrUnloadDriver = true;
387 }
388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 }
390 if (failedToStopSupplicantOrUnloadDriver) {
391 return false;
392 }
393 }
394
395 // Success!
396
397 if (persist) {
398 persistWifiEnabled(enable);
399 }
Dianne Hackborn617f8772009-03-31 15:04:46 -0700400 setWifiEnabledState(eventualWifiState, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 return true;
402 }
403
Dianne Hackborn617f8772009-03-31 15:04:46 -0700404 private void setWifiEnabledState(int wifiState, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 final int previousWifiState = mWifiState;
406
The Android Open Source Project10592532009-03-18 17:39:46 -0700407 long ident = Binder.clearCallingIdentity();
408 try {
409 if (wifiState == WIFI_STATE_ENABLED) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700410 mBatteryStats.noteWifiOn(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700411 } else if (wifiState == WIFI_STATE_DISABLED) {
Dianne Hackborn617f8772009-03-31 15:04:46 -0700412 mBatteryStats.noteWifiOff(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700413 }
414 } catch (RemoteException e) {
415 } finally {
416 Binder.restoreCallingIdentity(ident);
417 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 // Update state
420 mWifiState = wifiState;
421
422 // Broadcast
423 final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
424 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
425 intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState);
426 intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, previousWifiState);
427 mContext.sendStickyBroadcast(intent);
428 }
429
430 private void enforceAccessPermission() {
431 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
432 "WifiService");
433 }
434
435 private void enforceChangePermission() {
436 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
437 "WifiService");
438
439 }
440
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -0700441 private void enforceMulticastChangePermission() {
442 mContext.enforceCallingOrSelfPermission(
443 android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
444 "WifiService");
445 }
446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 /**
448 * see {@link WifiManager#getWifiState()}
449 * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
450 * {@link WifiManager#WIFI_STATE_DISABLING},
451 * {@link WifiManager#WIFI_STATE_ENABLED},
452 * {@link WifiManager#WIFI_STATE_ENABLING},
453 * {@link WifiManager#WIFI_STATE_UNKNOWN}
454 */
455 public int getWifiEnabledState() {
456 enforceAccessPermission();
457 return mWifiState;
458 }
459
460 /**
461 * see {@link android.net.wifi.WifiManager#disconnect()}
462 * @return {@code true} if the operation succeeds
463 */
464 public boolean disconnect() {
465 enforceChangePermission();
466 synchronized (mWifiStateTracker) {
467 return WifiNative.disconnectCommand();
468 }
469 }
470
471 /**
472 * see {@link android.net.wifi.WifiManager#reconnect()}
473 * @return {@code true} if the operation succeeds
474 */
475 public boolean reconnect() {
476 enforceChangePermission();
477 synchronized (mWifiStateTracker) {
478 return WifiNative.reconnectCommand();
479 }
480 }
481
482 /**
483 * see {@link android.net.wifi.WifiManager#reassociate()}
484 * @return {@code true} if the operation succeeds
485 */
486 public boolean reassociate() {
487 enforceChangePermission();
488 synchronized (mWifiStateTracker) {
489 return WifiNative.reassociateCommand();
490 }
491 }
492
493 /**
494 * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
495 * @return the list of configured networks
496 */
497 public List<WifiConfiguration> getConfiguredNetworks() {
498 enforceAccessPermission();
499 String listStr;
500 /*
501 * We don't cache the list, because we want to allow
502 * for the possibility that the configuration file
503 * has been modified through some external means,
504 * such as the wpa_cli command line program.
505 */
506 synchronized (mWifiStateTracker) {
507 listStr = WifiNative.listNetworksCommand();
508 }
509 List<WifiConfiguration> networks =
510 new ArrayList<WifiConfiguration>();
511 if (listStr == null)
512 return networks;
513
514 String[] lines = listStr.split("\n");
515 // Skip the first line, which is a header
516 for (int i = 1; i < lines.length; i++) {
517 String[] result = lines[i].split("\t");
518 // network-id | ssid | bssid | flags
519 WifiConfiguration config = new WifiConfiguration();
520 try {
521 config.networkId = Integer.parseInt(result[0]);
522 } catch(NumberFormatException e) {
523 continue;
524 }
525 if (result.length > 3) {
526 if (result[3].indexOf("[CURRENT]") != -1)
527 config.status = WifiConfiguration.Status.CURRENT;
528 else if (result[3].indexOf("[DISABLED]") != -1)
529 config.status = WifiConfiguration.Status.DISABLED;
530 else
531 config.status = WifiConfiguration.Status.ENABLED;
532 } else
533 config.status = WifiConfiguration.Status.ENABLED;
534 synchronized (mWifiStateTracker) {
535 readNetworkVariables(config);
536 }
537 networks.add(config);
538 }
539
540 return networks;
541 }
542
543 /**
544 * Read the variables from the supplicant daemon that are needed to
545 * fill in the WifiConfiguration object.
546 * <p/>
547 * The caller must hold the synchronization monitor.
548 * @param config the {@link WifiConfiguration} object to be filled in.
549 */
550 private static void readNetworkVariables(WifiConfiguration config) {
551
552 int netId = config.networkId;
553 if (netId < 0)
554 return;
555
556 /*
557 * TODO: maybe should have a native method that takes an array of
558 * variable names and returns an array of values. But we'd still
559 * be doing a round trip to the supplicant daemon for each variable.
560 */
561 String value;
562
563 value = WifiNative.getNetworkVariableCommand(netId, WifiConfiguration.ssidVarName);
564 if (!TextUtils.isEmpty(value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800565 config.SSID = removeDoubleQuotes(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 } else {
567 config.SSID = null;
568 }
569
570 value = WifiNative.getNetworkVariableCommand(netId, WifiConfiguration.bssidVarName);
571 if (!TextUtils.isEmpty(value)) {
572 config.BSSID = value;
573 } else {
574 config.BSSID = null;
575 }
576
577 value = WifiNative.getNetworkVariableCommand(netId, WifiConfiguration.priorityVarName);
578 config.priority = -1;
579 if (!TextUtils.isEmpty(value)) {
580 try {
581 config.priority = Integer.parseInt(value);
582 } catch (NumberFormatException ignore) {
583 }
584 }
585
586 value = WifiNative.getNetworkVariableCommand(netId, WifiConfiguration.hiddenSSIDVarName);
587 config.hiddenSSID = false;
588 if (!TextUtils.isEmpty(value)) {
589 try {
590 config.hiddenSSID = Integer.parseInt(value) != 0;
591 } catch (NumberFormatException ignore) {
592 }
593 }
594
595 value = WifiNative.getNetworkVariableCommand(netId, WifiConfiguration.wepTxKeyIdxVarName);
596 config.wepTxKeyIndex = -1;
597 if (!TextUtils.isEmpty(value)) {
598 try {
599 config.wepTxKeyIndex = Integer.parseInt(value);
600 } catch (NumberFormatException ignore) {
601 }
602 }
603
604 /*
605 * Get up to 4 WEP keys. Note that the actual keys are not passed back,
606 * just a "*" if the key is set, or the null string otherwise.
607 */
608 for (int i = 0; i < 4; i++) {
609 value = WifiNative.getNetworkVariableCommand(netId, WifiConfiguration.wepKeyVarNames[i]);
610 if (!TextUtils.isEmpty(value)) {
611 config.wepKeys[i] = value;
612 } else {
613 config.wepKeys[i] = null;
614 }
615 }
616
617 /*
618 * Get the private shared key. Note that the actual keys are not passed back,
619 * just a "*" if the key is set, or the null string otherwise.
620 */
621 value = WifiNative.getNetworkVariableCommand(netId, WifiConfiguration.pskVarName);
622 if (!TextUtils.isEmpty(value)) {
623 config.preSharedKey = value;
624 } else {
625 config.preSharedKey = null;
626 }
627
628 value = WifiNative.getNetworkVariableCommand(config.networkId,
629 WifiConfiguration.Protocol.varName);
630 if (!TextUtils.isEmpty(value)) {
631 String vals[] = value.split(" ");
632 for (String val : vals) {
633 int index =
634 lookupString(val, WifiConfiguration.Protocol.strings);
635 if (0 <= index) {
636 config.allowedProtocols.set(index);
637 }
638 }
639 }
640
641 value = WifiNative.getNetworkVariableCommand(config.networkId,
642 WifiConfiguration.KeyMgmt.varName);
643 if (!TextUtils.isEmpty(value)) {
644 String vals[] = value.split(" ");
645 for (String val : vals) {
646 int index =
647 lookupString(val, WifiConfiguration.KeyMgmt.strings);
648 if (0 <= index) {
649 config.allowedKeyManagement.set(index);
650 }
651 }
652 }
653
654 value = WifiNative.getNetworkVariableCommand(config.networkId,
655 WifiConfiguration.AuthAlgorithm.varName);
656 if (!TextUtils.isEmpty(value)) {
657 String vals[] = value.split(" ");
658 for (String val : vals) {
659 int index =
660 lookupString(val, WifiConfiguration.AuthAlgorithm.strings);
661 if (0 <= index) {
662 config.allowedAuthAlgorithms.set(index);
663 }
664 }
665 }
666
667 value = WifiNative.getNetworkVariableCommand(config.networkId,
668 WifiConfiguration.PairwiseCipher.varName);
669 if (!TextUtils.isEmpty(value)) {
670 String vals[] = value.split(" ");
671 for (String val : vals) {
672 int index =
673 lookupString(val, WifiConfiguration.PairwiseCipher.strings);
674 if (0 <= index) {
675 config.allowedPairwiseCiphers.set(index);
676 }
677 }
678 }
679
680 value = WifiNative.getNetworkVariableCommand(config.networkId,
681 WifiConfiguration.GroupCipher.varName);
682 if (!TextUtils.isEmpty(value)) {
683 String vals[] = value.split(" ");
684 for (String val : vals) {
685 int index =
686 lookupString(val, WifiConfiguration.GroupCipher.strings);
687 if (0 <= index) {
688 config.allowedGroupCiphers.set(index);
689 }
690 }
691 }
Chung-yih Wang43374762009-09-16 14:28:42 +0800692
693 for (WifiConfiguration.EnterpriseField field :
694 config.enterpriseFields) {
695 value = WifiNative.getNetworkVariableCommand(netId,
696 field.varName());
697 if (!TextUtils.isEmpty(value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800698 if (field != config.eap) value = removeDoubleQuotes(value);
Chung-yih Wang43374762009-09-16 14:28:42 +0800699 field.setValue(value);
700 }
701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800704 private static String removeDoubleQuotes(String string) {
705 if (string.length() <= 2) return "";
706 return string.substring(1, string.length() - 1);
707 }
708
709 private static String convertToQuotedString(String string) {
710 return "\"" + string + "\"";
711 }
712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 /**
714 * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
715 * @return the supplicant-assigned identifier for the new or updated
716 * network if the operation succeeds, or {@code -1} if it fails
717 */
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800718 public int addOrUpdateNetwork(WifiConfiguration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 enforceChangePermission();
720 /*
721 * If the supplied networkId is -1, we create a new empty
722 * network configuration. Otherwise, the networkId should
723 * refer to an existing configuration.
724 */
725 int netId = config.networkId;
726 boolean newNetwork = netId == -1;
727 boolean doReconfig;
728 int currentPriority;
729 // networkId of -1 means we want to create a new network
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800730 synchronized (mWifiStateTracker) {
731 if (newNetwork) {
732 netId = WifiNative.addNetworkCommand();
733 if (netId < 0) {
734 if (DBG) {
735 Log.d(TAG, "Failed to add a network!");
736 }
737 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800739 doReconfig = true;
740 } else {
741 String priorityVal = WifiNative.getNetworkVariableCommand(netId, WifiConfiguration.priorityVarName);
742 currentPriority = -1;
743 if (!TextUtils.isEmpty(priorityVal)) {
744 try {
745 currentPriority = Integer.parseInt(priorityVal);
746 } catch (NumberFormatException ignore) {
747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800749 doReconfig = currentPriority != config.priority;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800751 mNeedReconfig = mNeedReconfig || doReconfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800753 setVariables: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 /*
755 * Note that if a networkId for a non-existent network
756 * was supplied, then the first setNetworkVariableCommand()
757 * will fail, so we don't bother to make a separate check
758 * for the validity of the ID up front.
759 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 if (config.SSID != null &&
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800761 !WifiNative.setNetworkVariableCommand(
762 netId,
763 WifiConfiguration.ssidVarName,
764 convertToQuotedString(config.SSID))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 if (DBG) {
766 Log.d(TAG, "failed to set SSID: "+config.SSID);
767 }
768 break setVariables;
769 }
770
771 if (config.BSSID != null &&
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800772 !WifiNative.setNetworkVariableCommand(
773 netId,
774 WifiConfiguration.bssidVarName,
775 config.BSSID)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 if (DBG) {
777 Log.d(TAG, "failed to set BSSID: "+config.BSSID);
778 }
779 break setVariables;
780 }
781
782 String allowedKeyManagementString =
783 makeString(config.allowedKeyManagement, WifiConfiguration.KeyMgmt.strings);
784 if (config.allowedKeyManagement.cardinality() != 0 &&
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800785 !WifiNative.setNetworkVariableCommand(
786 netId,
787 WifiConfiguration.KeyMgmt.varName,
788 allowedKeyManagementString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 if (DBG) {
790 Log.d(TAG, "failed to set key_mgmt: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800791 allowedKeyManagementString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 }
793 break setVariables;
794 }
795
796 String allowedProtocolsString =
797 makeString(config.allowedProtocols, WifiConfiguration.Protocol.strings);
798 if (config.allowedProtocols.cardinality() != 0 &&
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800799 !WifiNative.setNetworkVariableCommand(
800 netId,
801 WifiConfiguration.Protocol.varName,
802 allowedProtocolsString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 if (DBG) {
804 Log.d(TAG, "failed to set proto: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800805 allowedProtocolsString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
807 break setVariables;
808 }
809
810 String allowedAuthAlgorithmsString =
811 makeString(config.allowedAuthAlgorithms, WifiConfiguration.AuthAlgorithm.strings);
812 if (config.allowedAuthAlgorithms.cardinality() != 0 &&
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800813 !WifiNative.setNetworkVariableCommand(
814 netId,
815 WifiConfiguration.AuthAlgorithm.varName,
816 allowedAuthAlgorithmsString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 if (DBG) {
818 Log.d(TAG, "failed to set auth_alg: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800819 allowedAuthAlgorithmsString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 }
821 break setVariables;
822 }
823
824 String allowedPairwiseCiphersString =
825 makeString(config.allowedPairwiseCiphers, WifiConfiguration.PairwiseCipher.strings);
826 if (config.allowedPairwiseCiphers.cardinality() != 0 &&
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800827 !WifiNative.setNetworkVariableCommand(
828 netId,
829 WifiConfiguration.PairwiseCipher.varName,
830 allowedPairwiseCiphersString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 if (DBG) {
832 Log.d(TAG, "failed to set pairwise: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800833 allowedPairwiseCiphersString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 }
835 break setVariables;
836 }
837
838 String allowedGroupCiphersString =
839 makeString(config.allowedGroupCiphers, WifiConfiguration.GroupCipher.strings);
840 if (config.allowedGroupCiphers.cardinality() != 0 &&
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800841 !WifiNative.setNetworkVariableCommand(
842 netId,
843 WifiConfiguration.GroupCipher.varName,
844 allowedGroupCiphersString)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 if (DBG) {
846 Log.d(TAG, "failed to set group: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800847 allowedGroupCiphersString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
849 break setVariables;
850 }
851
852 // Prevent client screw-up by passing in a WifiConfiguration we gave it
853 // by preventing "*" as a key.
854 if (config.preSharedKey != null && !config.preSharedKey.equals("*") &&
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800855 !WifiNative.setNetworkVariableCommand(
856 netId,
857 WifiConfiguration.pskVarName,
858 config.preSharedKey)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 if (DBG) {
860 Log.d(TAG, "failed to set psk: "+config.preSharedKey);
861 }
862 break setVariables;
863 }
864
865 boolean hasSetKey = false;
866 if (config.wepKeys != null) {
867 for (int i = 0; i < config.wepKeys.length; i++) {
868 // Prevent client screw-up by passing in a WifiConfiguration we gave it
869 // by preventing "*" as a key.
870 if (config.wepKeys[i] != null && !config.wepKeys[i].equals("*")) {
871 if (!WifiNative.setNetworkVariableCommand(
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800872 netId,
873 WifiConfiguration.wepKeyVarNames[i],
874 config.wepKeys[i])) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 if (DBG) {
876 Log.d(TAG,
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800877 "failed to set wep_key"+i+": " +
878 config.wepKeys[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
880 break setVariables;
881 }
882 hasSetKey = true;
883 }
884 }
885 }
886
887 if (hasSetKey) {
888 if (!WifiNative.setNetworkVariableCommand(
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800889 netId,
890 WifiConfiguration.wepTxKeyIdxVarName,
891 Integer.toString(config.wepTxKeyIndex))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 if (DBG) {
893 Log.d(TAG,
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800894 "failed to set wep_tx_keyidx: "+
895 config.wepTxKeyIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 }
897 break setVariables;
898 }
899 }
900
901 if (!WifiNative.setNetworkVariableCommand(
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800902 netId,
903 WifiConfiguration.priorityVarName,
904 Integer.toString(config.priority))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 if (DBG) {
906 Log.d(TAG, config.SSID + ": failed to set priority: "
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800907 +config.priority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 }
909 break setVariables;
910 }
911
912 if (config.hiddenSSID && !WifiNative.setNetworkVariableCommand(
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800913 netId,
914 WifiConfiguration.hiddenSSIDVarName,
915 Integer.toString(config.hiddenSSID ? 1 : 0))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 if (DBG) {
917 Log.d(TAG, config.SSID + ": failed to set hiddenSSID: "+
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800918 config.hiddenSSID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
920 break setVariables;
921 }
922
Chung-yih Wang43374762009-09-16 14:28:42 +0800923 for (WifiConfiguration.EnterpriseField field
924 : config.enterpriseFields) {
925 String varName = field.varName();
926 String value = field.value();
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800927 if (value != null) {
928 if (field != config.eap) {
Chia-chi Yeh784d53e2010-01-29 16:26:28 +0800929 value = (value.length() == 0) ? "NULL" : convertToQuotedString(value);
Chung-yih Wang43374762009-09-16 14:28:42 +0800930 }
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800931 if (!WifiNative.setNetworkVariableCommand(
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800932 netId,
933 varName,
934 value)) {
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800935 if (DBG) {
936 Log.d(TAG, config.SSID + ": failed to set " + varName +
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800937 ": " + value);
Chung-yih Wanga8d15942009-10-09 11:01:49 +0800938 }
939 break setVariables;
940 }
Chung-yih Wang5069cc72009-06-03 17:33:47 +0800941 }
Chung-yih Wang5069cc72009-06-03 17:33:47 +0800942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 return netId;
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945
Irfan Sheriff7aac5542009-12-22 21:42:17 -0800946 /*
947 * For an update, if one of the setNetworkVariable operations fails,
948 * we might want to roll back all the changes already made. But the
949 * chances are that if anything is going to go wrong, it'll happen
950 * the first time we try to set one of the variables.
951 */
952 if (newNetwork) {
953 removeNetwork(netId);
954 if (DBG) {
955 Log.d(TAG,
956 "Failed to set a network variable, removed network: "
957 + netId);
958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960 }
961 return -1;
962 }
963
964 private static String makeString(BitSet set, String[] strings) {
965 StringBuffer buf = new StringBuffer();
966 int nextSetBit = -1;
967
968 /* Make sure all set bits are in [0, strings.length) to avoid
969 * going out of bounds on strings. (Shouldn't happen, but...) */
970 set = set.get(0, strings.length);
971
972 while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1) {
973 buf.append(strings[nextSetBit].replace('_', '-')).append(' ');
974 }
975
976 // remove trailing space
977 if (set.cardinality() > 0) {
978 buf.setLength(buf.length() - 1);
979 }
980
981 return buf.toString();
982 }
983
984 private static int lookupString(String string, String[] strings) {
985 int size = strings.length;
986
987 string = string.replace('-', '_');
988
989 for (int i = 0; i < size; i++)
990 if (string.equals(strings[i]))
991 return i;
992
993 if (DBG) {
994 // if we ever get here, we should probably add the
995 // value to WifiConfiguration to reflect that it's
996 // supported by the WPA supplicant
997 Log.w(TAG, "Failed to look-up a string: " + string);
998 }
999
1000 return -1;
1001 }
1002
1003 /**
1004 * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
1005 * @param netId the integer that identifies the network configuration
1006 * to the supplicant
1007 * @return {@code true} if the operation succeeded
1008 */
1009 public boolean removeNetwork(int netId) {
1010 enforceChangePermission();
1011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 return mWifiStateTracker.removeNetwork(netId);
1013 }
1014
1015 /**
1016 * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
1017 * @param netId the integer that identifies the network configuration
1018 * to the supplicant
1019 * @param disableOthers if true, disable all other networks.
1020 * @return {@code true} if the operation succeeded
1021 */
1022 public boolean enableNetwork(int netId, boolean disableOthers) {
1023 enforceChangePermission();
1024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 synchronized (mWifiStateTracker) {
Mike Lockwood0900f362009-07-10 17:24:07 -04001026 String ifname = mWifiStateTracker.getInterfaceName();
1027 NetworkUtils.enableInterface(ifname);
1028 boolean result = WifiNative.enableNetworkCommand(netId, disableOthers);
1029 if (!result) {
1030 NetworkUtils.disableInterface(ifname);
1031 }
1032 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034 }
1035
1036 /**
1037 * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
1038 * @param netId the integer that identifies the network configuration
1039 * to the supplicant
1040 * @return {@code true} if the operation succeeded
1041 */
1042 public boolean disableNetwork(int netId) {
1043 enforceChangePermission();
1044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 synchronized (mWifiStateTracker) {
1046 return WifiNative.disableNetworkCommand(netId);
1047 }
1048 }
1049
1050 /**
1051 * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
1052 * @return the Wi-Fi information, contained in {@link WifiInfo}.
1053 */
1054 public WifiInfo getConnectionInfo() {
1055 enforceAccessPermission();
1056 /*
1057 * Make sure we have the latest information, by sending
1058 * a status request to the supplicant.
1059 */
1060 return mWifiStateTracker.requestConnectionInfo();
1061 }
1062
1063 /**
1064 * Return the results of the most recent access point scan, in the form of
1065 * a list of {@link ScanResult} objects.
1066 * @return the list of results
1067 */
1068 public List<ScanResult> getScanResults() {
1069 enforceAccessPermission();
1070 String reply;
1071 synchronized (mWifiStateTracker) {
1072 reply = WifiNative.scanResultsCommand();
1073 }
1074 if (reply == null) {
1075 return null;
1076 }
1077
1078 List<ScanResult> scanList = new ArrayList<ScanResult>();
1079
1080 int lineCount = 0;
1081
1082 int replyLen = reply.length();
1083 // Parse the result string, keeping in mind that the last line does
1084 // not end with a newline.
1085 for (int lineBeg = 0, lineEnd = 0; lineEnd <= replyLen; ++lineEnd) {
1086 if (lineEnd == replyLen || reply.charAt(lineEnd) == '\n') {
1087 ++lineCount;
1088 /*
1089 * Skip the first line, which is a header
1090 */
1091 if (lineCount == 1) {
1092 lineBeg = lineEnd + 1;
1093 continue;
1094 }
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001095 if (lineEnd > lineBeg) {
1096 String line = reply.substring(lineBeg, lineEnd);
1097 ScanResult scanResult = parseScanResult(line);
1098 if (scanResult != null) {
1099 scanList.add(scanResult);
1100 } else if (DBG) {
1101 Log.w(TAG, "misformatted scan result for: " + line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103 }
1104 lineBeg = lineEnd + 1;
1105 }
1106 }
1107 mWifiStateTracker.setScanResultsList(scanList);
1108 return scanList;
1109 }
1110
1111 /**
1112 * Parse the scan result line passed to us by wpa_supplicant (helper).
1113 * @param line the line to parse
1114 * @return the {@link ScanResult} object
1115 */
1116 private ScanResult parseScanResult(String line) {
1117 ScanResult scanResult = null;
1118 if (line != null) {
1119 /*
1120 * Cache implementation (LinkedHashMap) is not synchronized, thus,
1121 * must synchronized here!
1122 */
1123 synchronized (mScanResultCache) {
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001124 String[] result = scanResultPattern.split(line);
1125 if (3 <= result.length && result.length <= 5) {
1126 String bssid = result[0];
1127 // bssid | frequency | level | flags | ssid
1128 int frequency;
1129 int level;
1130 try {
1131 frequency = Integer.parseInt(result[1]);
1132 level = Integer.parseInt(result[2]);
1133 /* some implementations avoid negative values by adding 256
1134 * so we need to adjust for that here.
1135 */
1136 if (level > 0) level -= 256;
1137 } catch (NumberFormatException e) {
1138 frequency = 0;
1139 level = 0;
1140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141
Mike Lockwood1a645052009-06-25 13:01:12 -04001142 /*
1143 * The formatting of the results returned by
1144 * wpa_supplicant is intended to make the fields
1145 * line up nicely when printed,
1146 * not to make them easy to parse. So we have to
1147 * apply some heuristics to figure out which field
1148 * is the SSID and which field is the flags.
1149 */
1150 String ssid;
1151 String flags;
1152 if (result.length == 4) {
1153 if (result[3].charAt(0) == '[') {
1154 flags = result[3];
1155 ssid = "";
1156 } else {
1157 flags = "";
1158 ssid = result[3];
1159 }
1160 } else if (result.length == 5) {
1161 flags = result[3];
1162 ssid = result[4];
1163 } else {
1164 // Here, we must have 3 fields: no flags and ssid
1165 // set
1166 flags = "";
1167 ssid = "";
1168 }
1169
Mike Lockwood00717e22009-08-17 10:09:36 -04001170 // bssid + ssid is the hash key
1171 String key = bssid + ssid;
1172 scanResult = mScanResultCache.get(key);
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001173 if (scanResult != null) {
1174 scanResult.level = level;
Mike Lockwood1a645052009-06-25 13:01:12 -04001175 scanResult.SSID = ssid;
1176 scanResult.capabilities = flags;
1177 scanResult.frequency = frequency;
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001178 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 // Do not add scan results that have no SSID set
1180 if (0 < ssid.trim().length()) {
1181 scanResult =
1182 new ScanResult(
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001183 ssid, bssid, flags, level, frequency);
Mike Lockwood00717e22009-08-17 10:09:36 -04001184 mScanResultCache.put(key, scanResult);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
Mike Lockwoodb30475e2009-04-21 13:55:07 -07001187 } else {
1188 Log.w(TAG, "Misformatted scan result text with " +
1189 result.length + " fields: " + line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
1191 }
1192 }
1193
1194 return scanResult;
1195 }
1196
1197 /**
1198 * Parse the "flags" field passed back in a scan result by wpa_supplicant,
1199 * and construct a {@code WifiConfiguration} that describes the encryption,
1200 * key management, and authenticaion capabilities of the access point.
1201 * @param flags the string returned by wpa_supplicant
1202 * @return the {@link WifiConfiguration} object, filled in
1203 */
1204 WifiConfiguration parseScanFlags(String flags) {
1205 WifiConfiguration config = new WifiConfiguration();
1206
1207 if (flags.length() == 0) {
1208 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
1209 }
1210 // ... to be implemented
1211 return config;
1212 }
1213
1214 /**
1215 * Tell the supplicant to persist the current list of configured networks.
1216 * @return {@code true} if the operation succeeded
1217 */
1218 public boolean saveConfiguration() {
1219 boolean result;
1220 enforceChangePermission();
1221 synchronized (mWifiStateTracker) {
1222 result = WifiNative.saveConfigCommand();
1223 if (result && mNeedReconfig) {
1224 mNeedReconfig = false;
1225 result = WifiNative.reloadConfigCommand();
1226
1227 if (result) {
1228 Intent intent = new Intent(WifiManager.NETWORK_IDS_CHANGED_ACTION);
1229 mContext.sendBroadcast(intent);
1230 }
1231 }
1232 }
Amith Yamasani47873e52009-07-02 12:05:32 -07001233 // Inform the backup manager about a data change
1234 IBackupManager ibm = IBackupManager.Stub.asInterface(
1235 ServiceManager.getService(Context.BACKUP_SERVICE));
1236 if (ibm != null) {
1237 try {
1238 ibm.dataChanged("com.android.providers.settings");
1239 } catch (Exception e) {
1240 // Try again later
1241 }
1242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 return result;
1244 }
1245
1246 /**
1247 * Set the number of radio frequency channels that are allowed to be used
1248 * in the current regulatory domain. This method should be used only
1249 * if the correct number of channels cannot be determined automatically
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001250 * for some reason. If the operation is successful, the new value may be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 * persisted as a Secure setting.
1252 * @param numChannels the number of allowed channels. Must be greater than 0
1253 * and less than or equal to 16.
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001254 * @param persist {@code true} if the setting should be remembered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 * @return {@code true} if the operation succeeds, {@code false} otherwise, e.g.,
1256 * {@code numChannels} is outside the valid range.
1257 */
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001258 public boolean setNumAllowedChannels(int numChannels, boolean persist) {
1259 Log.i(TAG, "WifiService trying to setNumAllowed to "+numChannels+
1260 " with persist set to "+persist);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 enforceChangePermission();
1262 /*
1263 * Validate the argument. We'd like to let the Wi-Fi driver do this,
1264 * but if Wi-Fi isn't currently enabled, that's not possible, and
1265 * we want to persist the setting anyway,so that it will take
1266 * effect when Wi-Fi does become enabled.
1267 */
1268 boolean found = false;
1269 for (int validChan : sValidRegulatoryChannelCounts) {
1270 if (validChan == numChannels) {
1271 found = true;
1272 break;
1273 }
1274 }
1275 if (!found) {
1276 return false;
1277 }
1278
Robert Greenwaltb5010cc2009-05-21 15:11:40 -07001279 if (persist) {
1280 Settings.Secure.putInt(mContext.getContentResolver(),
1281 Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS,
1282 numChannels);
1283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 mWifiStateTracker.setNumAllowedChannels(numChannels);
1285 return true;
1286 }
1287
1288 /**
1289 * Return the number of frequency channels that are allowed
1290 * to be used in the current regulatory domain.
1291 * @return the number of allowed channels, or {@code -1} if an error occurs
1292 */
1293 public int getNumAllowedChannels() {
1294 int numChannels;
1295
1296 enforceAccessPermission();
1297 synchronized (mWifiStateTracker) {
1298 /*
1299 * If we can't get the value from the driver (e.g., because
1300 * Wi-Fi is not currently enabled), get the value from
1301 * Settings.
1302 */
1303 numChannels = WifiNative.getNumAllowedChannelsCommand();
1304 if (numChannels < 0) {
1305 numChannels = Settings.Secure.getInt(mContext.getContentResolver(),
1306 Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS,
1307 -1);
1308 }
1309 }
1310 return numChannels;
1311 }
1312
1313 /**
1314 * Return the list of valid values for the number of allowed radio channels
1315 * for various regulatory domains.
1316 * @return the list of channel counts
1317 */
1318 public int[] getValidChannelCounts() {
1319 enforceAccessPermission();
1320 return sValidRegulatoryChannelCounts;
1321 }
1322
1323 /**
1324 * Return the DHCP-assigned addresses from the last successful DHCP request,
1325 * if any.
1326 * @return the DHCP information
1327 */
1328 public DhcpInfo getDhcpInfo() {
1329 enforceAccessPermission();
1330 return mWifiStateTracker.getDhcpInfo();
1331 }
1332
1333 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1334 @Override
1335 public void onReceive(Context context, Intent intent) {
1336 String action = intent.getAction();
1337
Doug Zongker43866e02010-01-07 12:09:54 -08001338 long idleMillis =
1339 Settings.Secure.getLong(mContext.getContentResolver(),
1340 Settings.Secure.WIFI_IDLE_MS, DEFAULT_IDLE_MILLIS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 int stayAwakeConditions =
Doug Zongker43866e02010-01-07 12:09:54 -08001342 Settings.System.getInt(mContext.getContentResolver(),
1343 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 if (action.equals(Intent.ACTION_SCREEN_ON)) {
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001345 Log.d(TAG, "ACTION_SCREEN_ON");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 mAlarmManager.cancel(mIdleIntent);
1347 mDeviceIdle = false;
1348 mScreenOff = false;
Mike Lockwoodf32be162009-07-14 17:44:37 -04001349 mWifiStateTracker.enableRssiPolling(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001351 Log.d(TAG, "ACTION_SCREEN_OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 mScreenOff = true;
Mike Lockwoodf32be162009-07-14 17:44:37 -04001353 mWifiStateTracker.enableRssiPolling(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 /*
1355 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
1356 * AND the "stay on while plugged in" setting doesn't match the
1357 * current power conditions (i.e, not plugged in, plugged in to USB,
1358 * or plugged in to AC).
1359 */
1360 if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
San Mehatfa6c7112009-07-07 09:34:44 -07001361 WifiInfo info = mWifiStateTracker.requestConnectionInfo();
1362 if (info.getSupplicantState() != SupplicantState.COMPLETED) {
Robert Greenwalt84612ea62009-09-30 09:04:22 -07001363 // we used to go to sleep immediately, but this caused some race conditions
1364 // we don't have time to track down for this release. Delay instead, but not
1365 // as long as we would if connected (below)
1366 // TODO - fix the race conditions and switch back to the immediate turn-off
1367 long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
1368 Log.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
1369 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1370 // // do not keep Wifi awake when screen is off if Wifi is not associated
1371 // mDeviceIdle = true;
1372 // updateWifiState();
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001373 } else {
1374 long triggerTime = System.currentTimeMillis() + idleMillis;
1375 Log.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
1376 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 }
1379 /* we can return now -- there's nothing to do until we get the idle intent back */
1380 return;
1381 } else if (action.equals(ACTION_DEVICE_IDLE)) {
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001382 Log.d(TAG, "got ACTION_DEVICE_IDLE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 mDeviceIdle = true;
1384 } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
1385 /*
1386 * Set a timer to put Wi-Fi to sleep, but only if the screen is off
1387 * AND we are transitioning from a state in which the device was supposed
1388 * to stay awake to a state in which it is not supposed to stay awake.
1389 * If "stay awake" state is not changing, we do nothing, to avoid resetting
1390 * the already-set timer.
1391 */
1392 int pluggedType = intent.getIntExtra("plugged", 0);
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001393 Log.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) &&
1395 !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) {
1396 long triggerTime = System.currentTimeMillis() + idleMillis;
Mike Lockwoodd9c32bc2009-05-18 14:14:15 -04001397 Log.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
1399 mPluggedType = pluggedType;
1400 return;
1401 }
1402 mPluggedType = pluggedType;
Nick Pelly005b2282009-09-10 10:21:56 -07001403 } else if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) {
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001404 BluetoothA2dp a2dp = new BluetoothA2dp(mContext);
1405 Set<BluetoothDevice> sinks = a2dp.getConnectedSinks();
1406 boolean isBluetoothPlaying = false;
1407 for (BluetoothDevice sink : sinks) {
1408 if (a2dp.getSinkState(sink) == BluetoothA2dp.STATE_PLAYING) {
1409 isBluetoothPlaying = true;
1410 }
1411 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001412 mWifiStateTracker.setBluetoothScanMode(isBluetoothPlaying);
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 } else {
1415 return;
1416 }
1417
1418 updateWifiState();
1419 }
1420
1421 /**
1422 * Determines whether the Wi-Fi chipset should stay awake or be put to
1423 * sleep. Looks at the setting for the sleep policy and the current
1424 * conditions.
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001425 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 * @see #shouldDeviceStayAwake(int, int)
1427 */
1428 private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) {
1429 int wifiSleepPolicy = Settings.System.getInt(mContext.getContentResolver(),
1430 Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
1431
1432 if (wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER) {
1433 // Never sleep
1434 return true;
1435 } else if ((wifiSleepPolicy == Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
1436 (pluggedType != 0)) {
1437 // Never sleep while plugged, and we're plugged
1438 return true;
1439 } else {
1440 // Default
1441 return shouldDeviceStayAwake(stayAwakeConditions, pluggedType);
1442 }
1443 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 /**
1446 * Determine whether the bit value corresponding to {@code pluggedType} is set in
1447 * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value
1448 * of {@code 0} isn't really a plugged type, but rather an indication that the
1449 * device isn't plugged in at all, there is no bit value corresponding to a
1450 * {@code pluggedType} value of {@code 0}. That is why we shift by
1451 * {@code pluggedType&nbsp;&#8212;&nbsp;1} instead of by {@code pluggedType}.
1452 * @param stayAwakeConditions a bit string specifying which "plugged types" should
1453 * keep the device (and hence Wi-Fi) awake.
1454 * @param pluggedType the type of plug (USB, AC, or none) for which the check is
1455 * being made
1456 * @return {@code true} if {@code pluggedType} indicates that the device is
1457 * supposed to stay awake, {@code false} otherwise.
1458 */
1459 private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) {
1460 return (stayAwakeConditions & pluggedType) != 0;
1461 }
1462 };
1463
Dianne Hackborn617f8772009-03-31 15:04:46 -07001464 private void sendEnableMessage(boolean enable, boolean persist, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 Message msg = Message.obtain(mWifiHandler,
1466 (enable ? MESSAGE_ENABLE_WIFI : MESSAGE_DISABLE_WIFI),
Dianne Hackborn617f8772009-03-31 15:04:46 -07001467 (persist ? 1 : 0), uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 msg.sendToTarget();
1469 }
1470
1471 private void sendStartMessage(boolean scanOnlyMode) {
1472 Message.obtain(mWifiHandler, MESSAGE_START_WIFI, scanOnlyMode ? 1 : 0, 0).sendToTarget();
1473 }
1474
1475 private void updateWifiState() {
Robert Greenwaltf75aa36fc2009-10-22 17:03:47 -07001476 // send a message so it's all serialized
1477 Message.obtain(mWifiHandler, MESSAGE_UPDATE_STATE, 0, 0).sendToTarget();
1478 }
1479
1480 private void doUpdateWifiState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 boolean wifiEnabled = getPersistedWifiEnabled();
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001482 boolean airplaneMode = isAirplaneModeOn() && !mAirplaneModeOverwridden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 boolean lockHeld = mLocks.hasLocks();
1484 int strongestLockMode;
1485 boolean wifiShouldBeEnabled = wifiEnabled && !airplaneMode;
1486 boolean wifiShouldBeStarted = !mDeviceIdle || lockHeld;
1487 if (mDeviceIdle && lockHeld) {
1488 strongestLockMode = mLocks.getStrongestLockMode();
1489 } else {
1490 strongestLockMode = WifiManager.WIFI_MODE_FULL;
1491 }
1492
1493 synchronized (mWifiHandler) {
1494 if (mWifiState == WIFI_STATE_ENABLING && !airplaneMode) {
1495 return;
1496 }
1497 if (wifiShouldBeEnabled) {
1498 if (wifiShouldBeStarted) {
1499 sWakeLock.acquire();
Dianne Hackborn617f8772009-03-31 15:04:46 -07001500 sendEnableMessage(true, false, mLastEnableUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 sWakeLock.acquire();
1502 sendStartMessage(strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
1503 } else {
1504 int wakeLockTimeout =
1505 Settings.Secure.getInt(
1506 mContext.getContentResolver(),
1507 Settings.Secure.WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS,
1508 DEFAULT_WAKELOCK_TIMEOUT);
1509 /*
1510 * The following wakelock is held in order to ensure
1511 * that the connectivity manager has time to fail over
1512 * to the mobile data network. The connectivity manager
1513 * releases it once mobile data connectivity has been
1514 * established. If connectivity cannot be established,
1515 * the wakelock is released after wakeLockTimeout
1516 * milliseconds have elapsed.
1517 */
1518 sDriverStopWakeLock.acquire();
1519 mWifiHandler.sendEmptyMessage(MESSAGE_STOP_WIFI);
1520 mWifiHandler.sendEmptyMessageDelayed(MESSAGE_RELEASE_WAKELOCK, wakeLockTimeout);
1521 }
1522 } else {
1523 sWakeLock.acquire();
Dianne Hackborn617f8772009-03-31 15:04:46 -07001524 sendEnableMessage(false, false, mLastEnableUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
1526 }
1527 }
1528
1529 private void registerForBroadcasts() {
1530 IntentFilter intentFilter = new IntentFilter();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1532 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1533 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1534 intentFilter.addAction(ACTION_DEVICE_IDLE);
Nick Pelly005b2282009-09-10 10:21:56 -07001535 intentFilter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 mContext.registerReceiver(mReceiver, intentFilter);
1537 }
Jaikumar Ganesh084c6652009-12-07 10:58:18 -08001538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 private boolean isAirplaneSensitive() {
1540 String airplaneModeRadios = Settings.System.getString(mContext.getContentResolver(),
1541 Settings.System.AIRPLANE_MODE_RADIOS);
1542 return airplaneModeRadios == null
1543 || airplaneModeRadios.contains(Settings.System.RADIO_WIFI);
1544 }
1545
Mike Lockwoodbd5ddf02009-07-29 21:37:14 -07001546 private boolean isAirplaneToggleable() {
1547 String toggleableRadios = Settings.System.getString(mContext.getContentResolver(),
1548 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
1549 return toggleableRadios != null
1550 && toggleableRadios.contains(Settings.System.RADIO_WIFI);
1551 }
1552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 /**
1554 * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
1555 * currently on.
1556 * @return {@code true} if airplane mode is on.
1557 */
1558 private boolean isAirplaneModeOn() {
1559 return isAirplaneSensitive() && Settings.System.getInt(mContext.getContentResolver(),
1560 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
1561 }
1562
1563 /**
1564 * Handler that allows posting to the WifiThread.
1565 */
1566 private class WifiHandler extends Handler {
1567 public WifiHandler(Looper looper) {
1568 super(looper);
1569 }
1570
1571 @Override
1572 public void handleMessage(Message msg) {
1573 switch (msg.what) {
1574
1575 case MESSAGE_ENABLE_WIFI:
Dianne Hackborn617f8772009-03-31 15:04:46 -07001576 setWifiEnabledBlocking(true, msg.arg1 == 1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 sWakeLock.release();
1578 break;
1579
1580 case MESSAGE_START_WIFI:
1581 mWifiStateTracker.setScanOnlyMode(msg.arg1 != 0);
1582 mWifiStateTracker.restart();
1583 sWakeLock.release();
1584 break;
1585
Robert Greenwaltf75aa36fc2009-10-22 17:03:47 -07001586 case MESSAGE_UPDATE_STATE:
1587 doUpdateWifiState();
1588 break;
1589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 case MESSAGE_DISABLE_WIFI:
1591 // a non-zero msg.arg1 value means the "enabled" setting
1592 // should be persisted
Dianne Hackborn617f8772009-03-31 15:04:46 -07001593 setWifiEnabledBlocking(false, msg.arg1 == 1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 sWakeLock.release();
1595 break;
1596
1597 case MESSAGE_STOP_WIFI:
1598 mWifiStateTracker.disconnectAndStop();
1599 // don't release wakelock
1600 break;
1601
1602 case MESSAGE_RELEASE_WAKELOCK:
1603 synchronized (sDriverStopWakeLock) {
1604 if (sDriverStopWakeLock.isHeld()) {
1605 sDriverStopWakeLock.release();
1606 }
1607 }
1608 break;
1609 }
1610 }
1611 }
1612
1613 @Override
1614 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1615 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1616 != PackageManager.PERMISSION_GRANTED) {
1617 pw.println("Permission Denial: can't dump WifiService from from pid="
1618 + Binder.getCallingPid()
1619 + ", uid=" + Binder.getCallingUid());
1620 return;
1621 }
1622 pw.println("Wi-Fi is " + stateName(mWifiState));
1623 pw.println("Stay-awake conditions: " +
1624 Settings.System.getInt(mContext.getContentResolver(),
1625 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
1626 pw.println();
1627
1628 pw.println("Internal state:");
1629 pw.println(mWifiStateTracker);
1630 pw.println();
1631 pw.println("Latest scan results:");
1632 List<ScanResult> scanResults = mWifiStateTracker.getScanResultsList();
1633 if (scanResults != null && scanResults.size() != 0) {
1634 pw.println(" BSSID Frequency RSSI Flags SSID");
1635 for (ScanResult r : scanResults) {
1636 pw.printf(" %17s %9d %5d %-16s %s%n",
1637 r.BSSID,
1638 r.frequency,
1639 r.level,
1640 r.capabilities,
1641 r.SSID == null ? "" : r.SSID);
1642 }
1643 }
1644 pw.println();
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001645 pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
1646 mScanLocksAcquired + " scan");
1647 pw.println("Locks released: " + mFullLocksReleased + " full, " +
1648 mScanLocksReleased + " scan");
1649 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 pw.println("Locks held:");
1651 mLocks.dump(pw);
1652 }
1653
1654 private static String stateName(int wifiState) {
1655 switch (wifiState) {
1656 case WIFI_STATE_DISABLING:
1657 return "disabling";
1658 case WIFI_STATE_DISABLED:
1659 return "disabled";
1660 case WIFI_STATE_ENABLING:
1661 return "enabling";
1662 case WIFI_STATE_ENABLED:
1663 return "enabled";
1664 case WIFI_STATE_UNKNOWN:
1665 return "unknown state";
1666 default:
1667 return "[invalid state]";
1668 }
1669 }
1670
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001671 private class WifiLock extends DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 WifiLock(int lockMode, String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001673 super(lockMode, tag, binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
1675
1676 public void binderDied() {
1677 synchronized (mLocks) {
1678 releaseWifiLockLocked(mBinder);
1679 }
1680 }
1681
1682 public String toString() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001683 return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
1685 }
1686
1687 private class LockList {
1688 private List<WifiLock> mList;
1689
1690 private LockList() {
1691 mList = new ArrayList<WifiLock>();
1692 }
1693
1694 private synchronized boolean hasLocks() {
1695 return !mList.isEmpty();
1696 }
1697
1698 private synchronized int getStrongestLockMode() {
1699 if (mList.isEmpty()) {
1700 return WifiManager.WIFI_MODE_FULL;
1701 }
1702 for (WifiLock l : mList) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001703 if (l.mMode == WifiManager.WIFI_MODE_FULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 return WifiManager.WIFI_MODE_FULL;
1705 }
1706 }
1707 return WifiManager.WIFI_MODE_SCAN_ONLY;
1708 }
1709
1710 private void addLock(WifiLock lock) {
1711 if (findLockByBinder(lock.mBinder) < 0) {
1712 mList.add(lock);
1713 }
1714 }
1715
1716 private WifiLock removeLock(IBinder binder) {
1717 int index = findLockByBinder(binder);
1718 if (index >= 0) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001719 WifiLock ret = mList.remove(index);
1720 ret.unlinkDeathRecipient();
1721 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 } else {
1723 return null;
1724 }
1725 }
1726
1727 private int findLockByBinder(IBinder binder) {
1728 int size = mList.size();
1729 for (int i = size - 1; i >= 0; i--)
1730 if (mList.get(i).mBinder == binder)
1731 return i;
1732 return -1;
1733 }
1734
1735 private void dump(PrintWriter pw) {
1736 for (WifiLock l : mList) {
1737 pw.print(" ");
1738 pw.println(l);
1739 }
1740 }
1741 }
1742
1743 public boolean acquireWifiLock(IBinder binder, int lockMode, String tag) {
1744 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1745 if (lockMode != WifiManager.WIFI_MODE_FULL && lockMode != WifiManager.WIFI_MODE_SCAN_ONLY) {
1746 return false;
1747 }
1748 WifiLock wifiLock = new WifiLock(lockMode, tag, binder);
1749 synchronized (mLocks) {
1750 return acquireWifiLockLocked(wifiLock);
1751 }
1752 }
1753
1754 private boolean acquireWifiLockLocked(WifiLock wifiLock) {
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001755 Log.d(TAG, "acquireWifiLockLocked: " + wifiLock);
1756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 mLocks.addLock(wifiLock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001758
The Android Open Source Project10592532009-03-18 17:39:46 -07001759 int uid = Binder.getCallingUid();
1760 long ident = Binder.clearCallingIdentity();
1761 try {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001762 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001763 case WifiManager.WIFI_MODE_FULL:
1764 ++mFullLocksAcquired;
1765 mBatteryStats.noteFullWifiLockAcquired(uid);
1766 break;
1767 case WifiManager.WIFI_MODE_SCAN_ONLY:
1768 ++mScanLocksAcquired;
1769 mBatteryStats.noteScanWifiLockAcquired(uid);
1770 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001771 }
1772 } catch (RemoteException e) {
1773 } finally {
1774 Binder.restoreCallingIdentity(ident);
1775 }
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 updateWifiState();
1778 return true;
1779 }
1780
1781 public boolean releaseWifiLock(IBinder lock) {
1782 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1783 synchronized (mLocks) {
1784 return releaseWifiLockLocked(lock);
1785 }
1786 }
1787
1788 private boolean releaseWifiLockLocked(IBinder lock) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001789 boolean hadLock;
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001790
The Android Open Source Project10592532009-03-18 17:39:46 -07001791 WifiLock wifiLock = mLocks.removeLock(lock);
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001792
1793 Log.d(TAG, "releaseWifiLockLocked: " + wifiLock);
1794
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001795 hadLock = (wifiLock != null);
1796
1797 if (hadLock) {
1798 int uid = Binder.getCallingUid();
1799 long ident = Binder.clearCallingIdentity();
1800 try {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001801 switch(wifiLock.mMode) {
Eric Shienbrood5711fad2009-03-27 20:25:31 -07001802 case WifiManager.WIFI_MODE_FULL:
1803 ++mFullLocksReleased;
1804 mBatteryStats.noteFullWifiLockReleased(uid);
1805 break;
1806 case WifiManager.WIFI_MODE_SCAN_ONLY:
1807 ++mScanLocksReleased;
1808 mBatteryStats.noteScanWifiLockReleased(uid);
1809 break;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001810 }
1811 } catch (RemoteException e) {
1812 } finally {
1813 Binder.restoreCallingIdentity(ident);
The Android Open Source Project10592532009-03-18 17:39:46 -07001814 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001815 }
Robert Greenwaltf1acb2d2009-10-13 08:20:55 -07001816 // TODO - should this only happen if you hadLock?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 updateWifiState();
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001818 return hadLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001820
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001821 private abstract class DeathRecipient
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001822 implements IBinder.DeathRecipient {
1823 String mTag;
1824 int mMode;
1825 IBinder mBinder;
1826
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001827 DeathRecipient(int mode, String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001828 super();
1829 mTag = tag;
1830 mMode = mode;
1831 mBinder = binder;
1832 try {
1833 mBinder.linkToDeath(this, 0);
1834 } catch (RemoteException e) {
1835 binderDied();
1836 }
1837 }
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001838
1839 void unlinkDeathRecipient() {
1840 mBinder.unlinkToDeath(this, 0);
1841 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001842 }
1843
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001844 private class Multicaster extends DeathRecipient {
1845 Multicaster(String tag, IBinder binder) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001846 super(Binder.getCallingUid(), tag, binder);
1847 }
1848
1849 public void binderDied() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001850 Log.e(TAG, "Multicaster binderDied");
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001851 synchronized (mMulticasters) {
1852 int i = mMulticasters.indexOf(this);
1853 if (i != -1) {
1854 removeMulticasterLocked(i, mMode);
1855 }
1856 }
1857 }
1858
1859 public String toString() {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001860 return "Multicaster{" + mTag + " binder=" + mBinder + "}";
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001861 }
1862
1863 public int getUid() {
1864 return mMode;
1865 }
1866 }
1867
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001868 public void initializeMulticastFiltering() {
1869 enforceMulticastChangePermission();
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001870 synchronized (mMulticasters) {
1871 // if anybody had requested filters be off, leave off
1872 if (mMulticasters.size() != 0) {
1873 return;
1874 } else {
Irfan Sheriff7aac5542009-12-22 21:42:17 -08001875 synchronized (mWifiStateTracker) {
1876 WifiNative.startPacketFiltering();
1877 }
Robert Greenwalte2d155a2009-10-21 14:58:34 -07001878 }
1879 }
1880 }
1881
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001882 public void acquireMulticastLock(IBinder binder, String tag) {
1883 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001884
1885 synchronized (mMulticasters) {
1886 mMulticastEnabled++;
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001887 mMulticasters.add(new Multicaster(tag, binder));
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001888 // Note that we could call stopPacketFiltering only when
1889 // our new size == 1 (first call), but this function won't
1890 // be called often and by making the stopPacket call each
1891 // time we're less fragile and self-healing.
Zheng BaoZhong8d1668d2009-08-05 08:57:49 -04001892 synchronized (mWifiStateTracker) {
1893 WifiNative.stopPacketFiltering();
1894 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001895 }
1896
1897 int uid = Binder.getCallingUid();
1898 Long ident = Binder.clearCallingIdentity();
1899 try {
1900 mBatteryStats.noteWifiMulticastEnabled(uid);
1901 } catch (RemoteException e) {
1902 } finally {
1903 Binder.restoreCallingIdentity(ident);
1904 }
1905 }
1906
Robert Greenwaltfc1b15c2009-05-22 15:09:51 -07001907 public void releaseMulticastLock() {
1908 enforceMulticastChangePermission();
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001909
1910 int uid = Binder.getCallingUid();
1911 synchronized (mMulticasters) {
1912 mMulticastDisabled++;
1913 int size = mMulticasters.size();
1914 for (int i = size - 1; i >= 0; i--) {
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001915 Multicaster m = mMulticasters.get(i);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001916 if ((m != null) && (m.getUid() == uid)) {
1917 removeMulticasterLocked(i, uid);
1918 }
1919 }
1920 }
1921 }
1922
1923 private void removeMulticasterLocked(int i, int uid)
1924 {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001925 Multicaster removed = mMulticasters.remove(i);
1926 if (removed != null) {
1927 removed.unlinkDeathRecipient();
1928 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001929 if (mMulticasters.size() == 0) {
Zheng BaoZhong8d1668d2009-08-05 08:57:49 -04001930 synchronized (mWifiStateTracker) {
1931 WifiNative.startPacketFiltering();
1932 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001933 }
1934
1935 Long ident = Binder.clearCallingIdentity();
1936 try {
1937 mBatteryStats.noteWifiMulticastDisabled(uid);
1938 } catch (RemoteException e) {
1939 } finally {
1940 Binder.restoreCallingIdentity(ident);
1941 }
1942 }
1943
Robert Greenwalt58ff0212009-05-19 15:53:54 -07001944 public boolean isMulticastEnabled() {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07001945 enforceAccessPermission();
1946
1947 synchronized (mMulticasters) {
1948 return (mMulticasters.size() > 0);
1949 }
1950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951}