blob: 0c801660ff8871f0724c36cdb62695781a30b47b [file] [log] [blame]
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.connectivity;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080022import android.bluetooth.BluetoothAdapter;
23import android.bluetooth.BluetoothPan;
24import android.bluetooth.BluetoothProfile;
25import android.bluetooth.BluetoothProfile.ServiceListener;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080026import android.content.BroadcastReceiver;
Robert Greenwalt2ffe4122014-12-12 12:22:31 -080027import android.content.ComponentName;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080031import android.content.pm.PackageManager;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080032import android.content.res.Resources;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080033import android.hardware.usb.UsbManager;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080034import android.net.ConnectivityManager;
Christopher Wileydeebfec2016-09-16 11:14:36 -070035import android.net.INetworkPolicyManager;
Jeff Sharkey367d15a2011-09-22 14:59:51 -070036import android.net.INetworkStatsService;
Robert Greenwalt05d89362011-01-23 16:04:05 -080037import android.net.LinkProperties;
Lorenzo Colittib57edc52014-08-22 17:10:50 -070038import android.net.Network;
Erik Kline0c9cb2b2015-11-20 17:47:08 +090039import android.net.NetworkCapabilities;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080040import android.net.NetworkInfo;
Erik Kline0c9cb2b2015-11-20 17:47:08 +090041import android.net.NetworkRequest;
Erik Kline6ff17f72015-12-10 20:42:12 +090042import android.net.NetworkState;
Robert Greenwalte5903732011-02-22 16:00:42 -080043import android.net.NetworkUtils;
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +090044import android.net.RouteInfo;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080045import android.net.wifi.WifiManager;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080046import android.os.Binder;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080047import android.os.Bundle;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080048import android.os.INetworkManagementService;
Robert Greenwaltdfadaea2010-03-11 15:03:08 -080049import android.os.Looper;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080050import android.os.Message;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080051import android.os.Parcel;
Christopher Wileydeebfec2016-09-16 11:14:36 -070052import android.os.RemoteException;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080053import android.os.ResultReceiver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070054import android.os.UserHandle;
Jeremy Klein15f3d212016-01-24 17:01:09 -080055import android.provider.Settings;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080056import android.telephony.CarrierConfigManager;
Junda Liu38531582014-10-28 13:52:51 -070057import android.telephony.TelephonyManager;
Lorenzo Colitticd63d242016-04-10 15:39:53 +090058import android.text.TextUtils;
Christopher Wileye9490392016-05-26 15:57:29 -070059import android.util.ArrayMap;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080060import android.util.Log;
Lorenzo Colitticd63d242016-04-10 15:39:53 +090061import android.util.SparseArray;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080062
Christopher Wiley497c1472016-10-11 13:26:03 -070063import com.android.internal.annotations.VisibleForTesting;
Robert Greenwalt2ffe4122014-12-12 12:22:31 -080064import com.android.internal.telephony.IccCardConstants;
Robert Greenwalt2ffe4122014-12-12 12:22:31 -080065import com.android.internal.telephony.TelephonyIntents;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080066import com.android.internal.util.IndentingPrintWriter;
Lorenzo Colitticd63d242016-04-10 15:39:53 +090067import com.android.internal.util.MessageUtils;
68import com.android.internal.util.Protocol;
Wink Saville64c42ca2011-04-18 14:55:10 -070069import com.android.internal.util.State;
70import com.android.internal.util.StateMachine;
Christopher Wileye03fb442016-05-18 13:45:20 -070071import com.android.server.connectivity.tethering.IControlsTethering;
Erik Kline1eb8c692016-07-08 17:21:26 +090072import com.android.server.connectivity.tethering.IPv6TetheringCoordinator;
Lorenzo Colitti14422332016-10-28 17:45:55 +090073import com.android.server.connectivity.tethering.IPv6TetheringInterfaceServices;
Mitchell Wills7040b4e2016-05-23 16:40:10 -070074import com.android.server.connectivity.tethering.TetherInterfaceStateMachine;
Erik Kline9bba3402017-01-13 16:46:52 +090075import com.android.server.connectivity.tethering.UpstreamNetworkMonitor;
Lorenzo Colittif4e90ea2013-10-31 23:30:47 +090076import com.android.server.net.BaseNetworkObserver;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080077
78import java.io.FileDescriptor;
79import java.io.PrintWriter;
Robert Greenwalt4f74d552011-12-19 16:59:31 -080080import java.net.Inet4Address;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080081import java.net.InetAddress;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080082import java.util.ArrayList;
Lorenzo Colittib57edc52014-08-22 17:10:50 -070083import java.util.Arrays;
Robert Greenwaltccf83af12011-06-02 17:30:47 -070084import java.util.Collection;
Robert Greenwalt2a091d72010-02-11 18:18:40 -080085import java.util.HashMap;
Robert Greenwaltccf83af12011-06-02 17:30:47 -070086import java.util.Iterator;
Robert Greenwalt2ffe4122014-12-12 12:22:31 -080087import java.util.concurrent.atomic.AtomicInteger;
88
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070089
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080090/**
91 * @hide
Robert Greenwalt2a091d72010-02-11 18:18:40 -080092 *
Christopher Wiley3b1d9222016-05-20 16:44:04 -070093 * This class holds much of the business logic to allow Android devices
94 * to act as IP gateways via USB, BT, and WiFi interfaces.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080095 */
Christopher Wileye03fb442016-05-18 13:45:20 -070096public class Tethering extends BaseNetworkObserver implements IControlsTethering {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080097
Erik Kline6ff17f72015-12-10 20:42:12 +090098 private final Context mContext;
Robert Greenwalt924cc942010-06-28 10:26:19 -070099 private final static String TAG = "Tethering";
Joe Onorato12acbd72016-02-01 17:49:31 -0800100 private final static boolean DBG = false;
Robert Greenwaltfd1be2b2011-11-11 12:30:19 -0800101 private final static boolean VDBG = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800102
Christopher Wiley497c1472016-10-11 13:26:03 -0700103 protected static final String DISABLE_PROVISIONING_SYSPROP_KEY = "net.tethering.noprovisioning";
104
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900105 private static final Class[] messageClasses = {
Mitchell Wills7040b4e2016-05-23 16:40:10 -0700106 Tethering.class, TetherMasterSM.class, TetherInterfaceStateMachine.class
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900107 };
108 private static final SparseArray<String> sMagicDecoderRing =
109 MessageUtils.findMessageNames(messageClasses);
110
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800111 // TODO - remove both of these - should be part of interface inspection/selection stuff
112 private String[] mTetherableUsbRegexs;
113 private String[] mTetherableWifiRegexs;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700114 private String[] mTetherableBluetoothRegexs;
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700115 private Collection<Integer> mUpstreamIfaceTypes;
116
Robert Greenwaltb4453622011-11-03 16:01:40 -0700117 // used to synchronize public access to members
Erik Kline6ff17f72015-12-10 20:42:12 +0900118 private final Object mPublicSync;
Robert Greenwaltb4453622011-11-03 16:01:40 -0700119
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700120 private static final Integer MOBILE_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE);
121 private static final Integer HIPRI_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_HIPRI);
122 private static final Integer DUN_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_DUN);
123
124 // if we have to connect to mobile, what APN type should we use? Calculated by examining the
125 // upstream type list and the DUN_REQUIRED secure-setting
126 private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800127
Jeff Sharkey367d15a2011-09-22 14:59:51 -0700128 private final INetworkManagementService mNMService;
129 private final INetworkStatsService mStatsService;
Christopher Wileydeebfec2016-09-16 11:14:36 -0700130 private final INetworkPolicyManager mPolicyManager;
Erik Kline6ff17f72015-12-10 20:42:12 +0900131 private final Looper mLooper;
Christopher Wiley497c1472016-10-11 13:26:03 -0700132 private final MockableSystemProperties mSystemProperties;
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800133
Christopher Wileyd985dde2016-05-31 10:44:35 -0700134 private static class TetherState {
135 public final TetherInterfaceStateMachine mStateMachine;
136 public int mLastState;
137 public int mLastError;
138 public TetherState(TetherInterfaceStateMachine sm) {
139 mStateMachine = sm;
140 // Assume all state machines start out available and with no errors.
141 mLastState = IControlsTethering.STATE_AVAILABLE;
142 mLastError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
143 }
144 }
145 private final ArrayMap<String, TetherState> mTetherStates;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800146
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700147 private final BroadcastReceiver mStateReceiver;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800148
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800149 // {@link ComponentName} of the Service used to run tether provisioning.
150 private static final ComponentName TETHER_SERVICE = ComponentName.unflattenFromString(Resources
151 .getSystem().getString(com.android.internal.R.string.config_wifi_tether_enable));
152
Jaikumar Ganesh707952e2010-09-13 19:04:54 -0700153 // USB is 192.168.42.1 and 255.255.255.0
154 // Wifi is 192.168.43.1 and 255.255.255.0
155 // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
156 // with 255.255.255.0
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -0700157 // P2P is 192.168.49.1 and 255.255.255.0
Robert Greenwaltd70a3d42010-02-23 18:15:29 -0800158
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800159 private String[] mDhcpRange;
Chia-chi Yehc9338302011-05-11 16:35:13 -0700160 private static final String[] DHCP_DEFAULT_RANGE = {
161 "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
162 "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
163 "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -0700164 "192.168.48.2", "192.168.48.254", "192.168.49.2", "192.168.49.254",
Chia-chi Yehc9338302011-05-11 16:35:13 -0700165 };
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800166
Robert Greenwaltbb4fd0b2011-12-09 15:36:50 -0800167 private String[] mDefaultDnsServers;
Robert Greenwaltd70a3d42010-02-23 18:15:29 -0800168 private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8";
Robert Greenwaltdf255c22010-10-07 15:32:08 -0700169 private static final String DNS_DEFAULT_SERVER2 = "8.8.4.4";
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800170
Erik Kline6ff17f72015-12-10 20:42:12 +0900171 private final StateMachine mTetherMasterSM;
172 private final UpstreamNetworkMonitor mUpstreamNetworkMonitor;
173 private String mCurrentUpstreamIface;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800174
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400175 private Notification.Builder mTetheredNotificationBuilder;
176 private int mLastNotificationId;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800177
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700178 private boolean mRndisEnabled; // track the RNDIS function enabled state
179 private boolean mUsbTetherRequested; // true if USB tethering should be started
180 // when RNDIS is enabled
Robert Greenwalt030f5e12010-03-10 16:41:03 -0800181
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700182 // True iff WiFi tethering should be started when soft AP is ready.
183 private boolean mWifiTetherRequested;
184
Jeff Sharkey367d15a2011-09-22 14:59:51 -0700185 public Tethering(Context context, INetworkManagementService nmService,
Christopher Wiley497c1472016-10-11 13:26:03 -0700186 INetworkStatsService statsService, INetworkPolicyManager policyManager,
187 Looper looper, MockableSystemProperties systemProperties) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800188 mContext = context;
Chia-chi Yehc9338302011-05-11 16:35:13 -0700189 mNMService = nmService;
Jeff Sharkey367d15a2011-09-22 14:59:51 -0700190 mStatsService = statsService;
Christopher Wileydeebfec2016-09-16 11:14:36 -0700191 mPolicyManager = policyManager;
Christopher Wiley497c1472016-10-11 13:26:03 -0700192 mLooper = looper;
193 mSystemProperties = systemProperties;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800194
Robert Greenwaltb4453622011-11-03 16:01:40 -0700195 mPublicSync = new Object();
196
Christopher Wileyd985dde2016-05-31 10:44:35 -0700197 mTetherStates = new ArrayMap<>();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800198
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800199 mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800200 mTetherMasterSM.start();
201
Erik Klineebdb8c82017-01-10 17:37:54 +0900202 mUpstreamNetworkMonitor = new UpstreamNetworkMonitor(
203 mContext, mTetherMasterSM, TetherMasterSM.EVENT_UPSTREAM_CALLBACK);
Erik Kline6ff17f72015-12-10 20:42:12 +0900204
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800205 mStateReceiver = new StateReceiver();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800206 IntentFilter filter = new IntentFilter();
Mike Lockwood770126a2010-12-09 22:30:37 -0800207 filter.addAction(UsbManager.ACTION_USB_STATE);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800208 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700209 filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
Robert Greenwaltc13368b2013-07-18 14:24:42 -0700210 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800211 mContext.registerReceiver(mStateReceiver, filter);
212
213 filter = new IntentFilter();
Robert Greenwalt030f5e12010-03-10 16:41:03 -0800214 filter.addAction(Intent.ACTION_MEDIA_SHARED);
215 filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
Robert Greenwaltbb51d9f2010-03-12 10:23:35 -0800216 filter.addDataScheme("file");
217 mContext.registerReceiver(mStateReceiver, filter);
218
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800219 mDhcpRange = context.getResources().getStringArray(
220 com.android.internal.R.array.config_tether_dhcp_range);
Robert Greenwaltbfb7bfa2010-03-24 16:03:21 -0700221 if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
Chia-chi Yehc9338302011-05-11 16:35:13 -0700222 mDhcpRange = DHCP_DEFAULT_RANGE;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800223 }
224
Robert Greenwalt49348e72011-10-21 16:54:26 -0700225 // load device config info
226 updateConfiguration();
227
228 // TODO - remove and rely on real notifications of the current iface
Robert Greenwaltbb4fd0b2011-12-09 15:36:50 -0800229 mDefaultDnsServers = new String[2];
230 mDefaultDnsServers[0] = DNS_DEFAULT_SERVER1;
231 mDefaultDnsServers[1] = DNS_DEFAULT_SERVER2;
Robert Greenwalt49348e72011-10-21 16:54:26 -0700232 }
233
Sreeram Ramachandran62a61312014-07-24 16:05:17 -0700234 // We can't do this once in the Tethering() constructor and cache the value, because the
235 // CONNECTIVITY_SERVICE is registered only after the Tethering() constructor has completed.
236 private ConnectivityManager getConnectivityManager() {
237 return (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
238 }
239
Robert Greenwalt49348e72011-10-21 16:54:26 -0700240 void updateConfiguration() {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700241 String[] tetherableUsbRegexs = mContext.getResources().getStringArray(
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800242 com.android.internal.R.array.config_tether_usb_regexs);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700243 String[] tetherableWifiRegexs = mContext.getResources().getStringArray(
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800244 com.android.internal.R.array.config_tether_wifi_regexs);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700245 String[] tetherableBluetoothRegexs = mContext.getResources().getStringArray(
Danica Chang6fdd0c62010-08-11 14:54:43 -0700246 com.android.internal.R.array.config_tether_bluetooth_regexs);
Robert Greenwalt49348e72011-10-21 16:54:26 -0700247
248 int ifaceTypes[] = mContext.getResources().getIntArray(
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700249 com.android.internal.R.array.config_tether_upstream_types);
Christopher Wiley499a57a2016-05-16 16:19:07 -0700250 Collection<Integer> upstreamIfaceTypes = new ArrayList<>();
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700251 for (int i : ifaceTypes) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700252 upstreamIfaceTypes.add(new Integer(i));
253 }
254
255 synchronized (mPublicSync) {
256 mTetherableUsbRegexs = tetherableUsbRegexs;
257 mTetherableWifiRegexs = tetherableWifiRegexs;
258 mTetherableBluetoothRegexs = tetherableBluetoothRegexs;
259 mUpstreamIfaceTypes = upstreamIfaceTypes;
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700260 }
261
262 // check if the upstream type list needs to be modified due to secure-settings
263 checkDunRequired();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800264 }
265
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900266 @Override
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700267 public void interfaceStatusChanged(String iface, boolean up) {
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900268 // Never called directly: only called from interfaceLinkStateChanged.
269 // See NetlinkHandler.cpp:71.
Wink Savillec9acde92011-09-21 11:05:43 -0700270 if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700271 synchronized (mPublicSync) {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700272 int interfaceType = ifaceNameToType(iface);
273 if (interfaceType == ConnectivityManager.TETHERING_INVALID) {
274 return;
Robert Greenwaltb4453622011-11-03 16:01:40 -0700275 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800276
Christopher Wileyd985dde2016-05-31 10:44:35 -0700277 TetherState tetherState = mTetherStates.get(iface);
Mike J. Chen6143f5f2011-06-23 15:17:51 -0700278 if (up) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700279 if (tetherState == null) {
280 trackNewTetherableInterface(iface, interfaceType);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800281 }
282 } else {
Christopher Wileyd30aaeb2016-07-08 09:33:50 -0700283 if (interfaceType == ConnectivityManager.TETHERING_BLUETOOTH) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700284 tetherState.mStateMachine.sendMessage(
285 TetherInterfaceStateMachine.CMD_INTERFACE_DOWN);
286 mTetherStates.remove(iface);
Christopher Wileyd30aaeb2016-07-08 09:33:50 -0700287 } else {
288 // Ignore usb0 down after enabling RNDIS.
289 // We will handle disconnect in interfaceRemoved.
290 // Similarly, ignore interface down for WiFi. We monitor WiFi AP status
291 // through the WifiManager.WIFI_AP_STATE_CHANGED_ACTION intent.
292 if (VDBG) Log.d(TAG, "ignore interface down for " + iface);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800293 }
294 }
295 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800296 }
297
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900298 @Override
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700299 public void interfaceLinkStateChanged(String iface, boolean up) {
Irfan Sheriff23eb2972011-07-22 15:21:10 -0700300 interfaceStatusChanged(iface, up);
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700301 }
302
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800303 private boolean isUsb(String iface) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700304 synchronized (mPublicSync) {
305 for (String regex : mTetherableUsbRegexs) {
306 if (iface.matches(regex)) return true;
307 }
308 return false;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800309 }
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800310 }
311
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700312 private boolean isWifi(String iface) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700313 synchronized (mPublicSync) {
314 for (String regex : mTetherableWifiRegexs) {
315 if (iface.matches(regex)) return true;
316 }
317 return false;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800318 }
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800319 }
320
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700321 private boolean isBluetooth(String iface) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700322 synchronized (mPublicSync) {
323 for (String regex : mTetherableBluetoothRegexs) {
324 if (iface.matches(regex)) return true;
325 }
326 return false;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700327 }
Danica Chang6fdd0c62010-08-11 14:54:43 -0700328 }
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700329
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700330 private int ifaceNameToType(String iface) {
331 if (isWifi(iface)) {
332 return ConnectivityManager.TETHERING_WIFI;
333 } else if (isUsb(iface)) {
334 return ConnectivityManager.TETHERING_USB;
335 } else if (isBluetooth(iface)) {
336 return ConnectivityManager.TETHERING_BLUETOOTH;
337 }
338 return ConnectivityManager.TETHERING_INVALID;
339 }
340
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900341 @Override
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800342 public void interfaceAdded(String iface) {
Wink Savillec9acde92011-09-21 11:05:43 -0700343 if (VDBG) Log.d(TAG, "interfaceAdded " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700344 synchronized (mPublicSync) {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700345 int interfaceType = ifaceNameToType(iface);
346 if (interfaceType == ConnectivityManager.TETHERING_INVALID) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700347 if (VDBG) Log.d(TAG, iface + " is not a tetherable iface, ignoring");
348 return;
349 }
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800350
Christopher Wileyd985dde2016-05-31 10:44:35 -0700351 TetherState tetherState = mTetherStates.get(iface);
352 if (tetherState == null) {
353 trackNewTetherableInterface(iface, interfaceType);
354 } else {
Wink Savillec9acde92011-09-21 11:05:43 -0700355 if (VDBG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800356 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800357 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800358 }
359
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900360 @Override
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800361 public void interfaceRemoved(String iface) {
Wink Savillec9acde92011-09-21 11:05:43 -0700362 if (VDBG) Log.d(TAG, "interfaceRemoved " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700363 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700364 TetherState tetherState = mTetherStates.get(iface);
365 if (tetherState == null) {
Wink Savillec9acde92011-09-21 11:05:43 -0700366 if (VDBG) {
Robert Greenwalt924cc942010-06-28 10:26:19 -0700367 Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
368 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800369 return;
370 }
Christopher Wileyd985dde2016-05-31 10:44:35 -0700371 tetherState.mStateMachine.sendMessage(TetherInterfaceStateMachine.CMD_INTERFACE_DOWN);
372 mTetherStates.remove(iface);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800373 }
374 }
375
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800376 public void startTethering(int type, ResultReceiver receiver,
377 boolean showProvisioningUi) {
378 if (!isTetherProvisioningRequired()) {
379 enableTetheringInternal(type, true, receiver);
380 return;
381 }
382
383 if (showProvisioningUi) {
384 runUiTetherProvisioningAndEnable(type, receiver);
385 } else {
386 runSilentTetherProvisioningAndEnable(type, receiver);
387 }
388 }
389
390 public void stopTethering(int type) {
391 enableTetheringInternal(type, false, null);
392 if (isTetherProvisioningRequired()) {
393 cancelTetherProvisioningRechecks(type);
394 }
395 }
396
397 /**
398 * Check if the device requires a provisioning check in order to enable tethering.
399 *
400 * @return a boolean - {@code true} indicating tether provisioning is required by the carrier.
401 */
Christopher Wiley497c1472016-10-11 13:26:03 -0700402 @VisibleForTesting
403 protected boolean isTetherProvisioningRequired() {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800404 String[] provisionApp = mContext.getResources().getStringArray(
405 com.android.internal.R.array.config_mobile_hotspot_provision_app);
Christopher Wiley497c1472016-10-11 13:26:03 -0700406 if (mSystemProperties.getBoolean(DISABLE_PROVISIONING_SYSPROP_KEY, false)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800407 || provisionApp == null) {
408 return false;
409 }
410
411 // Check carrier config for entitlement checks
412 final CarrierConfigManager configManager = (CarrierConfigManager) mContext
413 .getSystemService(Context.CARRIER_CONFIG_SERVICE);
Rebecca Silbersteinb07f1402016-10-04 13:53:33 -0700414 if (configManager != null && configManager.getConfig() != null) {
415 // we do have a CarrierConfigManager and it has a config.
416 boolean isEntitlementCheckRequired = configManager.getConfig().getBoolean(
417 CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL);
418 if (!isEntitlementCheckRequired) {
419 return false;
420 }
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800421 }
422 return (provisionApp.length == 2);
423 }
424
425 /**
426 * Enables or disables tethering for the given type. This should only be called once
427 * provisioning has succeeded or is not necessary. It will also schedule provisioning rechecks
428 * for the specified interface.
429 */
430 private void enableTetheringInternal(int type, boolean enable, ResultReceiver receiver) {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700431 boolean isProvisioningRequired = enable && isTetherProvisioningRequired();
432 int result;
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800433 switch (type) {
434 case ConnectivityManager.TETHERING_WIFI:
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700435 result = setWifiTethering(enable);
436 if (isProvisioningRequired && result == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
437 scheduleProvisioningRechecks(type);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800438 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700439 sendTetherResult(receiver, result);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800440 break;
441 case ConnectivityManager.TETHERING_USB:
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700442 result = setUsbTethering(enable);
443 if (isProvisioningRequired && result == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800444 scheduleProvisioningRechecks(type);
445 }
446 sendTetherResult(receiver, result);
447 break;
448 case ConnectivityManager.TETHERING_BLUETOOTH:
449 setBluetoothTethering(enable, receiver);
450 break;
451 default:
452 Log.w(TAG, "Invalid tether type.");
453 sendTetherResult(receiver, ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE);
454 }
455 }
456
457 private void sendTetherResult(ResultReceiver receiver, int result) {
458 if (receiver != null) {
459 receiver.send(result, null);
460 }
461 }
462
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700463 private int setWifiTethering(final boolean enable) {
464 synchronized (mPublicSync) {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700465 mWifiTetherRequested = enable;
466 final WifiManager wifiManager =
467 (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
468 if (wifiManager.setWifiApEnabled(null /* use existing wifi config */, enable)) {
469 return ConnectivityManager.TETHER_ERROR_NO_ERROR;
470 }
471 return ConnectivityManager.TETHER_ERROR_MASTER_ERROR;
472 }
473 }
474
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800475 private void setBluetoothTethering(final boolean enable, final ResultReceiver receiver) {
476 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
477 if (adapter == null || !adapter.isEnabled()) {
478 Log.w(TAG, "Tried to enable bluetooth tethering with null or disabled adapter. null: " +
479 (adapter == null));
480 sendTetherResult(receiver, ConnectivityManager.TETHER_ERROR_SERVICE_UNAVAIL);
481 return;
482 }
483
484 adapter.getProfileProxy(mContext, new ServiceListener() {
485 @Override
486 public void onServiceDisconnected(int profile) { }
487
488 @Override
489 public void onServiceConnected(int profile, BluetoothProfile proxy) {
490 ((BluetoothPan) proxy).setBluetoothTethering(enable);
491 // TODO: Enabling bluetooth tethering can fail asynchronously here.
492 // We should figure out a way to bubble up that failure instead of sending success.
Jeremy Klein45ae04a2016-02-05 10:37:32 -0800493 int result = ((BluetoothPan) proxy).isTetheringOn() == enable ?
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800494 ConnectivityManager.TETHER_ERROR_NO_ERROR :
495 ConnectivityManager.TETHER_ERROR_MASTER_ERROR;
496 sendTetherResult(receiver, result);
497 if (enable && isTetherProvisioningRequired()) {
498 scheduleProvisioningRechecks(ConnectivityManager.TETHERING_BLUETOOTH);
499 }
500 adapter.closeProfileProxy(BluetoothProfile.PAN, proxy);
501 }
502 }, BluetoothProfile.PAN);
503 }
504
505 private void runUiTetherProvisioningAndEnable(int type, ResultReceiver receiver) {
Jeremy Klein15f3d212016-01-24 17:01:09 -0800506 ResultReceiver proxyReceiver = getProxyReceiver(type, receiver);
507 sendUiTetherProvisionIntent(type, proxyReceiver);
508 }
509
510 private void sendUiTetherProvisionIntent(int type, ResultReceiver receiver) {
511 Intent intent = new Intent(Settings.ACTION_TETHER_PROVISIONING);
512 intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type);
513 intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, receiver);
514 final long ident = Binder.clearCallingIdentity();
515 try {
516 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
517 } finally {
518 Binder.restoreCallingIdentity(ident);
519 }
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800520 }
521
522 /**
Erik Klinefdd41112017-01-12 17:02:51 +0900523 * Creates a proxy {@link ResultReceiver} which enables tethering if the provisioning result
524 * is successful before firing back up to the wrapped receiver.
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800525 *
526 * @param type The type of tethering being enabled.
527 * @param receiver A ResultReceiver which will be called back with an int resultCode.
528 * @return The proxy receiver.
529 */
530 private ResultReceiver getProxyReceiver(final int type, final ResultReceiver receiver) {
531 ResultReceiver rr = new ResultReceiver(null) {
532 @Override
533 protected void onReceiveResult(int resultCode, Bundle resultData) {
534 // If provisioning is successful, enable tethering, otherwise just send the error.
535 if (resultCode == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
536 enableTetheringInternal(type, true, receiver);
537 } else {
538 sendTetherResult(receiver, resultCode);
539 }
540 }
541 };
542
543 // The following is necessary to avoid unmarshalling issues when sending the receiver
Erik Kline6ff17f72015-12-10 20:42:12 +0900544 // across processes.
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800545 Parcel parcel = Parcel.obtain();
546 rr.writeToParcel(parcel,0);
547 parcel.setDataPosition(0);
548 ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel);
549 parcel.recycle();
550 return receiverForSending;
551 }
552
553 private void scheduleProvisioningRechecks(int type) {
554 Intent intent = new Intent();
555 intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type);
556 intent.putExtra(ConnectivityManager.EXTRA_SET_ALARM, true);
557 intent.setComponent(TETHER_SERVICE);
558 final long ident = Binder.clearCallingIdentity();
559 try {
560 mContext.startServiceAsUser(intent, UserHandle.CURRENT);
561 } finally {
562 Binder.restoreCallingIdentity(ident);
563 }
564 }
565
566 private void runSilentTetherProvisioningAndEnable(int type, ResultReceiver receiver) {
567 ResultReceiver proxyReceiver = getProxyReceiver(type, receiver);
568 sendSilentTetherProvisionIntent(type, proxyReceiver);
569 }
570
571 private void sendSilentTetherProvisionIntent(int type, ResultReceiver receiver) {
572 Intent intent = new Intent();
573 intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type);
574 intent.putExtra(ConnectivityManager.EXTRA_RUN_PROVISION, true);
575 intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, receiver);
576 intent.setComponent(TETHER_SERVICE);
577 final long ident = Binder.clearCallingIdentity();
578 try {
579 mContext.startServiceAsUser(intent, UserHandle.CURRENT);
580 } finally {
581 Binder.restoreCallingIdentity(ident);
582 }
583 }
584
585 private void cancelTetherProvisioningRechecks(int type) {
586 if (getConnectivityManager().isTetheringSupported()) {
587 Intent intent = new Intent();
588 intent.putExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE, type);
589 intent.setComponent(TETHER_SERVICE);
590 final long ident = Binder.clearCallingIdentity();
591 try {
592 mContext.startServiceAsUser(intent, UserHandle.CURRENT);
593 } finally {
594 Binder.restoreCallingIdentity(ident);
595 }
596 }
597 }
Erik Kline6ff17f72015-12-10 20:42:12 +0900598
Robert Greenwalt5a735062010-03-02 17:25:02 -0800599 public int tether(String iface) {
Wink Savillec9acde92011-09-21 11:05:43 -0700600 if (DBG) Log.d(TAG, "Tethering " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700601 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700602 TetherState tetherState = mTetherStates.get(iface);
603 if (tetherState == null) {
Erik Kline00019f42016-06-30 19:31:46 +0900604 Log.e(TAG, "Tried to Tether an unknown iface: " + iface + ", ignoring");
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700605 return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
606 }
607 // Ignore the error status of the interface. If the interface is available,
608 // the errors are referring to past tethering attempts anyway.
Christopher Wileyd985dde2016-05-31 10:44:35 -0700609 if (tetherState.mLastState != IControlsTethering.STATE_AVAILABLE) {
Erik Kline00019f42016-06-30 19:31:46 +0900610 Log.e(TAG, "Tried to Tether an unavailable iface: " + iface + ", ignoring");
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700611 return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700612 }
Christopher Wileyd985dde2016-05-31 10:44:35 -0700613 tetherState.mStateMachine.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED);
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700614 return ConnectivityManager.TETHER_ERROR_NO_ERROR;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800615 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800616 }
617
Robert Greenwalt5a735062010-03-02 17:25:02 -0800618 public int untether(String iface) {
Wink Savillec9acde92011-09-21 11:05:43 -0700619 if (DBG) Log.d(TAG, "Untethering " + iface);
Robert Greenwaltb4453622011-11-03 16:01:40 -0700620 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700621 TetherState tetherState = mTetherStates.get(iface);
622 if (tetherState == null) {
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700623 Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
624 return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
625 }
Christopher Wileyd985dde2016-05-31 10:44:35 -0700626 if (tetherState.mLastState != IControlsTethering.STATE_TETHERED) {
627 Log.e(TAG, "Tried to untether an untethered iface :" + iface + ", ignoring");
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700628 return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
629 }
Christopher Wileyd985dde2016-05-31 10:44:35 -0700630 tetherState.mStateMachine.sendMessage(
631 TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED);
Christopher Wiley4312a4c2016-05-23 13:58:00 -0700632 return ConnectivityManager.TETHER_ERROR_NO_ERROR;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800633 }
Robert Greenwalt5a735062010-03-02 17:25:02 -0800634 }
635
Felipe Leme70c8b9b2016-04-25 14:41:31 -0700636 public void untetherAll() {
Christopher Wileyfa6a4862016-09-19 08:58:06 -0700637 stopTethering(ConnectivityManager.TETHERING_WIFI);
638 stopTethering(ConnectivityManager.TETHERING_USB);
639 stopTethering(ConnectivityManager.TETHERING_BLUETOOTH);
Felipe Leme70c8b9b2016-04-25 14:41:31 -0700640 }
641
Robert Greenwalt5a735062010-03-02 17:25:02 -0800642 public int getLastTetherError(String iface) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700643 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700644 TetherState tetherState = mTetherStates.get(iface);
645 if (tetherState == null) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700646 Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface +
647 ", ignoring");
648 return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
649 }
Christopher Wileyd985dde2016-05-31 10:44:35 -0700650 return tetherState.mLastError;
Robert Greenwalt5a735062010-03-02 17:25:02 -0800651 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800652 }
653
Christopher Wileyd985dde2016-05-31 10:44:35 -0700654 private void sendTetherStateChangedBroadcast() {
Sreeram Ramachandran62a61312014-07-24 16:05:17 -0700655 if (!getConnectivityManager().isTetheringSupported()) return;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800656
657 ArrayList<String> availableList = new ArrayList<String>();
658 ArrayList<String> activeList = new ArrayList<String>();
659 ArrayList<String> erroredList = new ArrayList<String>();
660
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800661 boolean wifiTethered = false;
662 boolean usbTethered = false;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700663 boolean bluetoothTethered = false;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800664
Robert Greenwaltb4453622011-11-03 16:01:40 -0700665 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700666 for (int i = 0; i < mTetherStates.size(); i++) {
667 TetherState tetherState = mTetherStates.valueAt(i);
668 String iface = mTetherStates.keyAt(i);
669 if (tetherState.mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
670 erroredList.add(iface);
671 } else if (tetherState.mLastState == IControlsTethering.STATE_AVAILABLE) {
672 availableList.add(iface);
673 } else if (tetherState.mLastState == IControlsTethering.STATE_TETHERED) {
674 if (isUsb(iface)) {
675 usbTethered = true;
676 } else if (isWifi(iface)) {
677 wifiTethered = true;
678 } else if (isBluetooth(iface)) {
679 bluetoothTethered = true;
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800680 }
Christopher Wileyd985dde2016-05-31 10:44:35 -0700681 activeList.add(iface);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800682 }
683 }
684 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800685 Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Robert Greenwalt98c79e52011-07-28 11:51:11 -0700686 broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
687 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800688 broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
689 availableList);
690 broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
691 broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
692 erroredList);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700693 mContext.sendStickyBroadcastAsUser(broadcast, UserHandle.ALL);
Robert Greenwaltfd1be2b2011-11-11 12:30:19 -0800694 if (DBG) {
Lorenzo Colitticd63d242016-04-10 15:39:53 +0900695 Log.d(TAG, String.format(
696 "sendTetherStateChangedBroadcast avail=[%s] active=[%s] error=[%s]",
697 TextUtils.join(",", availableList),
698 TextUtils.join(",", activeList),
699 TextUtils.join(",", erroredList)));
Robert Greenwalt924cc942010-06-28 10:26:19 -0700700 }
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800701
702 if (usbTethered) {
Danica Chang6fdd0c62010-08-11 14:54:43 -0700703 if (wifiTethered || bluetoothTethered) {
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800704 showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
705 } else {
706 showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
707 }
708 } else if (wifiTethered) {
Danica Chang6fdd0c62010-08-11 14:54:43 -0700709 if (bluetoothTethered) {
710 showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
711 } else {
Vinit Deshpande2576a802014-11-18 13:56:15 -0800712 /* We now have a status bar icon for WifiTethering, so drop the notification */
713 clearTetheredNotification();
Danica Chang6fdd0c62010-08-11 14:54:43 -0700714 }
715 } else if (bluetoothTethered) {
716 showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_bluetooth);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800717 } else {
718 clearTetheredNotification();
719 }
720 }
721
722 private void showTetheredNotification(int icon) {
723 NotificationManager notificationManager =
724 (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
725 if (notificationManager == null) {
726 return;
727 }
728
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400729 if (mLastNotificationId != 0) {
730 if (mLastNotificationId == icon) {
Robert Greenwaltdb3fe9e2010-03-18 16:28:30 -0700731 return;
732 }
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400733 notificationManager.cancelAsUser(null, mLastNotificationId,
Robert Greenwalt3cab6b02012-10-04 16:44:26 -0700734 UserHandle.ALL);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400735 mLastNotificationId = 0;
Robert Greenwaltdb3fe9e2010-03-18 16:28:30 -0700736 }
737
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800738 Intent intent = new Intent();
739 intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
740 intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
741
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700742 PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0,
743 null, UserHandle.CURRENT);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800744
745 Resources r = Resources.getSystem();
746 CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
747 CharSequence message = r.getText(com.android.internal.R.string.
748 tethered_notification_message);
749
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400750 if (mTetheredNotificationBuilder == null) {
751 mTetheredNotificationBuilder = new Notification.Builder(mContext);
752 mTetheredNotificationBuilder.setWhen(0)
753 .setOngoing(true)
754 .setColor(mContext.getColor(
755 com.android.internal.R.color.system_notification_accent_color))
756 .setVisibility(Notification.VISIBILITY_PUBLIC)
757 .setCategory(Notification.CATEGORY_STATUS);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800758 }
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400759 mTetheredNotificationBuilder.setSmallIcon(icon)
760 .setContentTitle(title)
761 .setContentText(message)
762 .setContentIntent(pi);
763 mLastNotificationId = icon;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800764
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400765 notificationManager.notifyAsUser(null, mLastNotificationId,
766 mTetheredNotificationBuilder.build(), UserHandle.ALL);
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800767 }
768
769 private void clearTetheredNotification() {
770 NotificationManager notificationManager =
771 (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400772 if (notificationManager != null && mLastNotificationId != 0) {
773 notificationManager.cancelAsUser(null, mLastNotificationId,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700774 UserHandle.ALL);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400775 mLastNotificationId = 0;
Robert Greenwalta599fe7c2010-03-08 18:30:14 -0800776 }
Robert Greenwalt5a735062010-03-02 17:25:02 -0800777 }
778
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800779 private class StateReceiver extends BroadcastReceiver {
Nick Kralevich70c117a2014-05-27 15:30:02 -0700780 @Override
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800781 public void onReceive(Context content, Intent intent) {
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800782 String action = intent.getAction();
Nick Kralevich70c117a2014-05-27 15:30:02 -0700783 if (action == null) { return; }
Mike Lockwood770126a2010-12-09 22:30:37 -0800784 if (action.equals(UsbManager.ACTION_USB_STATE)) {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700785 synchronized (Tethering.this.mPublicSync) {
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700786 boolean usbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
787 mRndisEnabled = intent.getBooleanExtra(UsbManager.USB_FUNCTION_RNDIS, false);
788 // start tethering if we have a request pending
789 if (usbConnected && mRndisEnabled && mUsbTetherRequested) {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700790 tetherMatchingInterfaces(true, ConnectivityManager.TETHERING_USB);
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700791 }
792 mUsbTetherRequested = false;
793 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800794 } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Robert Greenwaltadfb0d02012-06-14 15:50:32 -0700795 NetworkInfo networkInfo = (NetworkInfo)intent.getParcelableExtra(
796 ConnectivityManager.EXTRA_NETWORK_INFO);
797 if (networkInfo != null &&
798 networkInfo.getDetailedState() != NetworkInfo.DetailedState.FAILED) {
799 if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION");
800 mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
801 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700802 } else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) {
803 synchronized (Tethering.this.mPublicSync) {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700804 int curState = intent.getIntExtra(WifiManager.EXTRA_WIFI_AP_STATE,
805 WifiManager.WIFI_AP_STATE_DISABLED);
806 switch (curState) {
807 case WifiManager.WIFI_AP_STATE_ENABLING:
808 // We can see this state on the way to both enabled and failure states.
809 break;
810 case WifiManager.WIFI_AP_STATE_ENABLED:
Christopher Wileye52b24a2016-07-13 18:57:34 -0700811 // When the AP comes up and we've been requested to tether it, do so.
812 if (mWifiTetherRequested) {
813 tetherMatchingInterfaces(true, ConnectivityManager.TETHERING_WIFI);
814 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700815 break;
816 case WifiManager.WIFI_AP_STATE_DISABLED:
817 case WifiManager.WIFI_AP_STATE_DISABLING:
818 case WifiManager.WIFI_AP_STATE_FAILED:
819 default:
820 if (DBG) {
821 Log.d(TAG, "Canceling WiFi tethering request - AP_STATE=" +
822 curState);
823 }
Christopher Wileye52b24a2016-07-13 18:57:34 -0700824 // Tell appropriate interface state machines that they should tear
825 // themselves down.
826 for (int i = 0; i < mTetherStates.size(); i++) {
827 TetherInterfaceStateMachine tism =
828 mTetherStates.valueAt(i).mStateMachine;
829 if (tism.interfaceType() == ConnectivityManager.TETHERING_WIFI) {
830 tism.sendMessage(
831 TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED);
832 break; // There should be at most one of these.
833 }
834 }
835 // Regardless of whether we requested this transition, the AP has gone
836 // down. Don't try to tether again unless we're requested to do so.
837 mWifiTetherRequested = false;
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700838 break;
839 }
840 }
Robert Greenwaltc13368b2013-07-18 14:24:42 -0700841 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
842 updateConfiguration();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800843 }
844 }
845 }
846
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700847 private void tetherMatchingInterfaces(boolean enable, int interfaceType) {
848 if (VDBG) Log.d(TAG, "tetherMatchingInterfaces(" + enable + ", " + interfaceType + ")");
Mike Lockwood3c2a2f62011-06-08 15:10:26 -0700849
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700850 String[] ifaces = null;
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800851 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -0700852 ifaces = mNMService.listInterfaces();
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800853 } catch (Exception e) {
Mike Lockwood3c2a2f62011-06-08 15:10:26 -0700854 Log.e(TAG, "Error listing Interfaces", e);
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800855 return;
856 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700857 String chosenIface = null;
858 if (ifaces != null) {
859 for (String iface : ifaces) {
860 if (ifaceNameToType(iface) == interfaceType) {
861 chosenIface = iface;
862 break;
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800863 }
864 }
865 }
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700866 if (chosenIface == null) {
867 Log.e(TAG, "could not find iface of type " + interfaceType);
868 return;
869 }
870
871 int result = (enable ? tether(chosenIface) : untether(chosenIface));
872 if (result != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
873 Log.e(TAG, "unable start or stop tethering on iface " + chosenIface);
874 return;
875 }
Robert Greenwalt65ae29b2010-02-18 11:25:54 -0800876 }
877
Robert Greenwaltb4453622011-11-03 16:01:40 -0700878 // TODO - return copies so people can't tamper
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800879 public String[] getTetherableUsbRegexs() {
880 return mTetherableUsbRegexs;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800881 }
882
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800883 public String[] getTetherableWifiRegexs() {
884 return mTetherableWifiRegexs;
885 }
886
Danica Chang6fdd0c62010-08-11 14:54:43 -0700887 public String[] getTetherableBluetoothRegexs() {
888 return mTetherableBluetoothRegexs;
889 }
890
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700891 public int setUsbTethering(boolean enable) {
Wink Savillec9acde92011-09-21 11:05:43 -0700892 if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")");
Erik Klinefdd41112017-01-12 17:02:51 +0900893 UsbManager usbManager = mContext.getSystemService(UsbManager.class);
894 if (usbManager == null) {
895 return enable ? ConnectivityManager.TETHER_ERROR_MASTER_ERROR
896 : ConnectivityManager.TETHER_ERROR_NO_ERROR;
897 }
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700898
Robert Greenwaltb4453622011-11-03 16:01:40 -0700899 synchronized (mPublicSync) {
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700900 if (enable) {
901 if (mRndisEnabled) {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800902 final long ident = Binder.clearCallingIdentity();
903 try {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700904 tetherMatchingInterfaces(true, ConnectivityManager.TETHERING_USB);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800905 } finally {
906 Binder.restoreCallingIdentity(ident);
907 }
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700908 } else {
909 mUsbTetherRequested = true;
Jerry Zhang935372b2016-10-12 15:49:32 -0700910 usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false);
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700911 }
912 } else {
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800913 final long ident = Binder.clearCallingIdentity();
914 try {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700915 tetherMatchingInterfaces(false, ConnectivityManager.TETHERING_USB);
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800916 } finally {
917 Binder.restoreCallingIdentity(ident);
918 }
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700919 if (mRndisEnabled) {
Jerry Zhang935372b2016-10-12 15:49:32 -0700920 usbManager.setCurrentFunction(null, false);
Mike Lockwood6c2260b2011-07-19 13:04:47 -0700921 }
922 mUsbTetherRequested = false;
923 }
924 }
925 return ConnectivityManager.TETHER_ERROR_NO_ERROR;
926 }
927
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700928 public int[] getUpstreamIfaceTypes() {
Robert Greenwaltb4453622011-11-03 16:01:40 -0700929 int values[];
930 synchronized (mPublicSync) {
Robert Greenwaltc13368b2013-07-18 14:24:42 -0700931 updateConfiguration(); // TODO - remove?
Robert Greenwaltb4453622011-11-03 16:01:40 -0700932 values = new int[mUpstreamIfaceTypes.size()];
933 Iterator<Integer> iterator = mUpstreamIfaceTypes.iterator();
934 for (int i=0; i < mUpstreamIfaceTypes.size(); i++) {
935 values[i] = iterator.next();
936 }
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700937 }
938 return values;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800939 }
940
Erik Kline0c9cb2b2015-11-20 17:47:08 +0900941 private void checkDunRequired() {
Junda Liu38531582014-10-28 13:52:51 -0700942 int secureSetting = 2;
Erik Klinefdd41112017-01-12 17:02:51 +0900943 TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
Junda Liu38531582014-10-28 13:52:51 -0700944 if (tm != null) {
945 secureSetting = tm.getTetherApnRequired();
946 }
Robert Greenwaltb4453622011-11-03 16:01:40 -0700947 synchronized (mPublicSync) {
948 // 2 = not set, 0 = DUN not required, 1 = DUN required
949 if (secureSetting != 2) {
950 int requiredApn = (secureSetting == 1 ?
951 ConnectivityManager.TYPE_MOBILE_DUN :
952 ConnectivityManager.TYPE_MOBILE_HIPRI);
953 if (requiredApn == ConnectivityManager.TYPE_MOBILE_DUN) {
954 while (mUpstreamIfaceTypes.contains(MOBILE_TYPE)) {
955 mUpstreamIfaceTypes.remove(MOBILE_TYPE);
956 }
957 while (mUpstreamIfaceTypes.contains(HIPRI_TYPE)) {
958 mUpstreamIfaceTypes.remove(HIPRI_TYPE);
959 }
960 if (mUpstreamIfaceTypes.contains(DUN_TYPE) == false) {
961 mUpstreamIfaceTypes.add(DUN_TYPE);
962 }
963 } else {
964 while (mUpstreamIfaceTypes.contains(DUN_TYPE)) {
965 mUpstreamIfaceTypes.remove(DUN_TYPE);
966 }
967 if (mUpstreamIfaceTypes.contains(MOBILE_TYPE) == false) {
968 mUpstreamIfaceTypes.add(MOBILE_TYPE);
969 }
970 if (mUpstreamIfaceTypes.contains(HIPRI_TYPE) == false) {
971 mUpstreamIfaceTypes.add(HIPRI_TYPE);
972 }
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700973 }
974 }
Robert Greenwaltb4453622011-11-03 16:01:40 -0700975 if (mUpstreamIfaceTypes.contains(DUN_TYPE)) {
976 mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_MOBILE_DUN;
977 } else {
978 mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_MOBILE_HIPRI;
979 }
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700980 }
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800981 }
982
Robert Greenwaltb4453622011-11-03 16:01:40 -0700983 // TODO review API - maybe return ArrayList<String> here and below?
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800984 public String[] getTetheredIfaces() {
985 ArrayList<String> list = new ArrayList<String>();
Robert Greenwaltb4453622011-11-03 16:01:40 -0700986 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -0700987 for (int i = 0; i < mTetherStates.size(); i++) {
988 TetherState tetherState = mTetherStates.valueAt(i);
989 if (tetherState.mLastState == IControlsTethering.STATE_TETHERED) {
990 list.add(mTetherStates.keyAt(i));
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800991 }
992 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800993 }
Christopher Wileyd985dde2016-05-31 10:44:35 -0700994 return list.toArray(new String[list.size()]);
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800995 }
996
997 public String[] getTetherableIfaces() {
998 ArrayList<String> list = new ArrayList<String>();
Robert Greenwaltb4453622011-11-03 16:01:40 -0700999 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001000 for (int i = 0; i < mTetherStates.size(); i++) {
1001 TetherState tetherState = mTetherStates.valueAt(i);
1002 if (tetherState.mLastState == IControlsTethering.STATE_AVAILABLE) {
1003 list.add(mTetherStates.keyAt(i));
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001004 }
1005 }
1006 }
Christopher Wileyd985dde2016-05-31 10:44:35 -07001007 return list.toArray(new String[list.size()]);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001008 }
1009
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001010 public String[] getTetheredDhcpRanges() {
1011 return mDhcpRange;
1012 }
1013
Robert Greenwalt5a735062010-03-02 17:25:02 -08001014 public String[] getErroredIfaces() {
1015 ArrayList<String> list = new ArrayList<String>();
Robert Greenwaltb4453622011-11-03 16:01:40 -07001016 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001017 for (int i = 0; i < mTetherStates.size(); i++) {
1018 TetherState tetherState = mTetherStates.valueAt(i);
1019 if (tetherState.mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
1020 list.add(mTetherStates.keyAt(i));
Robert Greenwalt5a735062010-03-02 17:25:02 -08001021 }
1022 }
1023 }
Christopher Wileyd985dde2016-05-31 10:44:35 -07001024 return list.toArray(new String[list.size()]);
Robert Greenwalt5a735062010-03-02 17:25:02 -08001025 }
1026
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001027 private void maybeLogMessage(State state, int what) {
1028 if (DBG) {
1029 Log.d(TAG, state.getName() + " got " +
1030 sMagicDecoderRing.get(what, Integer.toString(what)));
1031 }
1032 }
1033
Erik Kline00019f42016-06-30 19:31:46 +09001034 // Needed because the canonical source of upstream truth is just the
1035 // upstream interface name, |mCurrentUpstreamIface|. This is ripe for
1036 // future simplification, once the upstream Network is canonical.
1037 boolean pertainsToCurrentUpstream(NetworkState ns) {
1038 if (ns != null && ns.linkProperties != null && mCurrentUpstreamIface != null) {
1039 for (String ifname : ns.linkProperties.getAllInterfaceNames()) {
1040 if (mCurrentUpstreamIface.equals(ifname)) {
1041 return true;
Erik Kline6ff17f72015-12-10 20:42:12 +09001042 }
1043 }
Erik Kline6ff17f72015-12-10 20:42:12 +09001044 }
Erik Kline00019f42016-06-30 19:31:46 +09001045 return false;
Erik Kline6ff17f72015-12-10 20:42:12 +09001046 }
1047
Wink Saville64c42ca2011-04-18 14:55:10 -07001048 class TetherMasterSM extends StateMachine {
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001049 private static final int BASE_MASTER = Protocol.BASE_TETHERING;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001050 // an interface SM has requested Tethering
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001051 static final int CMD_TETHER_MODE_REQUESTED = BASE_MASTER + 1;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001052 // an interface SM has unrequested Tethering
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001053 static final int CMD_TETHER_MODE_UNREQUESTED = BASE_MASTER + 2;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001054 // upstream connection change - do the right thing
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001055 static final int CMD_UPSTREAM_CHANGED = BASE_MASTER + 3;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001056 // we don't have a valid upstream conn, check again after a delay
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001057 static final int CMD_RETRY_UPSTREAM = BASE_MASTER + 4;
Erik Kline6ff17f72015-12-10 20:42:12 +09001058 // Events from NetworkCallbacks that we process on the master state
1059 // machine thread on behalf of the UpstreamNetworkMonitor.
Erik Kline00019f42016-06-30 19:31:46 +09001060 static final int EVENT_UPSTREAM_CALLBACK = BASE_MASTER + 5;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001061
Wink Saville64c42ca2011-04-18 14:55:10 -07001062 private State mInitialState;
1063 private State mTetherModeAliveState;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001064
Wink Saville64c42ca2011-04-18 14:55:10 -07001065 private State mSetIpForwardingEnabledErrorState;
1066 private State mSetIpForwardingDisabledErrorState;
1067 private State mStartTetheringErrorState;
1068 private State mStopTetheringErrorState;
1069 private State mSetDnsForwardersErrorState;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001070
Christopher Wileyd985dde2016-05-31 10:44:35 -07001071 // This list is a little subtle. It contains all the interfaces that currently are
1072 // requesting tethering, regardless of whether these interfaces are still members of
1073 // mTetherStates. This allows us to maintain the following predicates:
1074 //
1075 // 1) mTetherStates contains the set of all currently existing, tetherable, link state up
1076 // interfaces.
1077 // 2) mNotifyList contains all state machines that may have outstanding tethering state
1078 // that needs to be torn down.
1079 //
1080 // Because we excise interfaces immediately from mTetherStates, we must maintain mNotifyList
1081 // so that the garbage collector does not clean up the state machine before it has a chance
1082 // to tear itself down.
Erik Kline1eb8c692016-07-08 17:21:26 +09001083 private final ArrayList<TetherInterfaceStateMachine> mNotifyList;
1084 private final IPv6TetheringCoordinator mIPv6TetheringCoordinator;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001085
Erik Klinefdd41112017-01-12 17:02:51 +09001086 private int mPreviousMobileApn = ConnectivityManager.TYPE_NONE;
Robert Greenwaltb37f0c62010-03-25 14:50:34 -07001087
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001088 private static final int UPSTREAM_SETTLE_TIME_MS = 10000;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001089
Robert Greenwaltdfadaea2010-03-11 15:03:08 -08001090 TetherMasterSM(String name, Looper looper) {
1091 super(name, looper);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001092
1093 //Add states
1094 mInitialState = new InitialState();
1095 addState(mInitialState);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001096 mTetherModeAliveState = new TetherModeAliveState();
1097 addState(mTetherModeAliveState);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001098
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001099 mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
1100 addState(mSetIpForwardingEnabledErrorState);
1101 mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
1102 addState(mSetIpForwardingDisabledErrorState);
1103 mStartTetheringErrorState = new StartTetheringErrorState();
1104 addState(mStartTetheringErrorState);
1105 mStopTetheringErrorState = new StopTetheringErrorState();
1106 addState(mStopTetheringErrorState);
1107 mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
1108 addState(mSetDnsForwardersErrorState);
1109
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001110 mNotifyList = new ArrayList<>();
Erik Kline1eb8c692016-07-08 17:21:26 +09001111 mIPv6TetheringCoordinator = new IPv6TetheringCoordinator(mNotifyList);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001112 setInitialState(mInitialState);
1113 }
1114
Wink Saville64c42ca2011-04-18 14:55:10 -07001115 class TetherMasterUtilState extends State {
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001116 @Override
1117 public boolean processMessage(Message m) {
1118 return false;
1119 }
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001120
Robert Greenwaltccf83af12011-06-02 17:30:47 -07001121 protected boolean turnOnUpstreamMobileConnection(int apnType) {
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001122 if (apnType == ConnectivityManager.TYPE_NONE) { return false; }
1123
Erik Klinefdd41112017-01-12 17:02:51 +09001124 if (apnType != mPreviousMobileApn) {
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001125 // Unregister any previous mobile upstream callback because
1126 // this request, if any, will be different.
1127 turnOffUpstreamMobileConnection();
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001128 }
1129
Erik Klinefdd41112017-01-12 17:02:51 +09001130 if (mUpstreamNetworkMonitor.mobileNetworkRequested()) {
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001131 // Looks like we already filed a request for this apnType.
1132 return true;
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001133 }
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001134
1135 switch (apnType) {
1136 case ConnectivityManager.TYPE_MOBILE_DUN:
1137 case ConnectivityManager.TYPE_MOBILE:
1138 case ConnectivityManager.TYPE_MOBILE_HIPRI:
Erik Klinefdd41112017-01-12 17:02:51 +09001139 mPreviousMobileApn = apnType;
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001140 break;
1141 default:
1142 return false;
1143 }
1144
Erik Klinefdd41112017-01-12 17:02:51 +09001145 // TODO: This should be called by the code that observes
1146 // configuration changes, once the above code in this function
1147 // is simplified (i.e. eradicated).
1148 mUpstreamNetworkMonitor.mobileUpstreamRequiresDun(
1149 apnType == ConnectivityManager.TYPE_MOBILE_DUN);
Erik Kline6ff17f72015-12-10 20:42:12 +09001150
Erik Klinefdd41112017-01-12 17:02:51 +09001151 mUpstreamNetworkMonitor.registerMobileNetworkRequest();
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001152 return true;
1153 }
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001154
1155 protected void turnOffUpstreamMobileConnection() {
Erik Klinefdd41112017-01-12 17:02:51 +09001156 mUpstreamNetworkMonitor.releaseMobileNetworkRequest();
1157 mPreviousMobileApn = ConnectivityManager.TYPE_NONE;
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001158 }
1159
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001160 protected boolean turnOnMasterTetherSettings() {
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001161 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001162 mNMService.setIpForwardingEnabled(true);
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001163 } catch (Exception e) {
Wink Saville951749b2010-03-19 09:03:13 -07001164 transitionTo(mSetIpForwardingEnabledErrorState);
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001165 return false;
1166 }
1167 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001168 mNMService.startTethering(mDhcpRange);
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001169 } catch (Exception e) {
Robert Greenwalt547e4e72011-01-14 17:24:03 -08001170 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001171 mNMService.stopTethering();
1172 mNMService.startTethering(mDhcpRange);
Robert Greenwalt547e4e72011-01-14 17:24:03 -08001173 } catch (Exception ee) {
1174 transitionTo(mStartTetheringErrorState);
1175 return false;
1176 }
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001177 }
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001178 return true;
1179 }
Erik Kline6ff17f72015-12-10 20:42:12 +09001180
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001181 protected boolean turnOffMasterTetherSettings() {
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001182 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001183 mNMService.stopTethering();
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001184 } catch (Exception e) {
1185 transitionTo(mStopTetheringErrorState);
1186 return false;
1187 }
1188 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001189 mNMService.setIpForwardingEnabled(false);
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001190 } catch (Exception e) {
1191 transitionTo(mSetIpForwardingDisabledErrorState);
1192 return false;
1193 }
1194 transitionTo(mInitialState);
1195 return true;
1196 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001197
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001198 protected void chooseUpstreamType(boolean tryCell) {
Erik Kline00019f42016-06-30 19:31:46 +09001199 final ConnectivityManager cm = getConnectivityManager();
Robert Greenwaltccf83af12011-06-02 17:30:47 -07001200 int upType = ConnectivityManager.TYPE_NONE;
1201 String iface = null;
1202
Robert Greenwaltc13368b2013-07-18 14:24:42 -07001203 updateConfiguration(); // TODO - remove?
Robert Greenwalt49348e72011-10-21 16:54:26 -07001204
Robert Greenwaltb4453622011-11-03 16:01:40 -07001205 synchronized (mPublicSync) {
1206 if (VDBG) {
1207 Log.d(TAG, "chooseUpstreamType has upstream iface types:");
1208 for (Integer netType : mUpstreamIfaceTypes) {
1209 Log.d(TAG, " " + netType);
1210 }
Robert Greenwalt49348e72011-10-21 16:54:26 -07001211 }
Robert Greenwalt49348e72011-10-21 16:54:26 -07001212
Robert Greenwaltb4453622011-11-03 16:01:40 -07001213 for (Integer netType : mUpstreamIfaceTypes) {
Erik Kline00019f42016-06-30 19:31:46 +09001214 NetworkInfo info = cm.getNetworkInfo(netType.intValue());
Erik Klinefa37b2f2016-08-02 18:27:03 +09001215 // TODO: if the network is suspended we should consider
1216 // that to be the same as connected here.
Robert Greenwaltb4453622011-11-03 16:01:40 -07001217 if ((info != null) && info.isConnected()) {
1218 upType = netType.intValue();
1219 break;
1220 }
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001221 }
1222 }
Robert Greenwaltccf83af12011-06-02 17:30:47 -07001223
Robert Greenwaltfd1be2b2011-11-11 12:30:19 -08001224 if (DBG) {
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001225 Log.d(TAG, "chooseUpstreamType(" + tryCell + "),"
1226 + " preferredApn="
1227 + ConnectivityManager.getNetworkTypeName(mPreferredUpstreamMobileApn)
1228 + ", got type="
1229 + ConnectivityManager.getNetworkTypeName(upType));
Robert Greenwaltccf83af12011-06-02 17:30:47 -07001230 }
1231
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001232 switch (upType) {
1233 case ConnectivityManager.TYPE_MOBILE_DUN:
1234 case ConnectivityManager.TYPE_MOBILE_HIPRI:
1235 // If we're on DUN, put our own grab on it.
1236 turnOnUpstreamMobileConnection(upType);
1237 break;
1238 case ConnectivityManager.TYPE_NONE:
1239 if (tryCell &&
1240 turnOnUpstreamMobileConnection(mPreferredUpstreamMobileApn)) {
1241 // We think mobile should be coming up; don't set a retry.
1242 } else {
1243 sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
1244 }
1245 break;
1246 default:
1247 /* If we've found an active upstream connection that's not DUN/HIPRI
1248 * we should stop any outstanding DUN/HIPRI start requests.
1249 *
1250 * If we found NONE we don't want to do this as we want any previous
1251 * requests to keep trying to bring up something we can use.
1252 */
1253 turnOffUpstreamMobileConnection();
1254 break;
Robert Greenwaltccf83af12011-06-02 17:30:47 -07001255 }
1256
Erik Kline00019f42016-06-30 19:31:46 +09001257 Network network = null;
Erik Kline0c9cb2b2015-11-20 17:47:08 +09001258 if (upType != ConnectivityManager.TYPE_NONE) {
Erik Kline00019f42016-06-30 19:31:46 +09001259 LinkProperties linkProperties = cm.getLinkProperties(upType);
Robert Greenwaltbb4fd0b2011-12-09 15:36:50 -08001260 if (linkProperties != null) {
Lorenzo Colitti13c9fde2013-03-15 04:22:37 +09001261 // Find the interface with the default IPv4 route. It may be the
1262 // interface described by linkProperties, or one of the interfaces
1263 // stacked on top of it.
1264 Log.i(TAG, "Finding IPv4 upstream interface on: " + linkProperties);
1265 RouteInfo ipv4Default = RouteInfo.selectBestRoute(
1266 linkProperties.getAllRoutes(), Inet4Address.ANY);
1267 if (ipv4Default != null) {
1268 iface = ipv4Default.getInterface();
1269 Log.i(TAG, "Found interface " + ipv4Default.getInterface());
1270 } else {
1271 Log.i(TAG, "No IPv4 upstream interface, giving up.");
1272 }
1273 }
1274
1275 if (iface != null) {
Erik Kline00019f42016-06-30 19:31:46 +09001276 network = cm.getNetworkForType(upType);
Erik Kline6ff17f72015-12-10 20:42:12 +09001277 if (network == null) {
1278 Log.e(TAG, "No Network for upstream type " + upType + "!");
Robert Greenwaltbb4fd0b2011-12-09 15:36:50 -08001279 }
Erik Kline6ff17f72015-12-10 20:42:12 +09001280 setDnsForwarders(network, linkProperties);
Robert Greenwaltbb4fd0b2011-12-09 15:36:50 -08001281 }
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001282 }
Robert Greenwaltb37f0c62010-03-25 14:50:34 -07001283 notifyTetheredOfNewUpstreamIface(iface);
Erik Kline00019f42016-06-30 19:31:46 +09001284 NetworkState ns = mUpstreamNetworkMonitor.lookup(network);
1285 if (ns != null && pertainsToCurrentUpstream(ns)) {
1286 // If we already have NetworkState for this network examine
1287 // it immediately, because there likely will be no second
1288 // EVENT_ON_AVAILABLE (it was already received).
1289 handleNewUpstreamNetworkState(ns);
Erik Klinefa37b2f2016-08-02 18:27:03 +09001290 } else if (mCurrentUpstreamIface == null) {
1291 // There are no available upstream networks, or none that
1292 // have an IPv4 default route (current metric for success).
1293 handleNewUpstreamNetworkState(null);
Erik Kline00019f42016-06-30 19:31:46 +09001294 }
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001295 }
Robert Greenwaltccf83af12011-06-02 17:30:47 -07001296
Erik Kline6ff17f72015-12-10 20:42:12 +09001297 protected void setDnsForwarders(final Network network, final LinkProperties lp) {
1298 String[] dnsServers = mDefaultDnsServers;
1299 final Collection<InetAddress> dnses = lp.getDnsServers();
1300 // TODO: Properly support the absence of DNS servers.
1301 if (dnses != null && !dnses.isEmpty()) {
1302 // TODO: remove this invocation of NetworkUtils.makeStrings().
1303 dnsServers = NetworkUtils.makeStrings(dnses);
1304 }
1305 if (VDBG) {
1306 Log.d(TAG, "Setting DNS forwarders: Network=" + network +
1307 ", dnsServers=" + Arrays.toString(dnsServers));
1308 }
1309 try {
1310 mNMService.setDnsForwarders(network, dnsServers);
1311 } catch (Exception e) {
1312 // TODO: Investigate how this can fail and what exactly
1313 // happens if/when such failures occur.
1314 Log.e(TAG, "Setting DNS forwarders failed!");
1315 transitionTo(mSetDnsForwardersErrorState);
1316 }
1317 }
1318
Robert Greenwaltb37f0c62010-03-25 14:50:34 -07001319 protected void notifyTetheredOfNewUpstreamIface(String ifaceName) {
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001320 if (DBG) Log.d(TAG, "Notifying tethered with upstream=" + ifaceName);
Erik Kline6ff17f72015-12-10 20:42:12 +09001321 mCurrentUpstreamIface = ifaceName;
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001322 for (TetherInterfaceStateMachine sm : mNotifyList) {
1323 sm.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED,
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001324 ifaceName);
1325 }
1326 }
Erik Kline00019f42016-06-30 19:31:46 +09001327
1328 protected void handleNewUpstreamNetworkState(NetworkState ns) {
1329 mIPv6TetheringCoordinator.updateUpstreamNetworkState(ns);
1330 }
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001331 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001332
Robert Greenwalt2ffe4122014-12-12 12:22:31 -08001333 private final AtomicInteger mSimBcastGenerationNumber = new AtomicInteger(0);
1334 private SimChangeBroadcastReceiver mBroadcastReceiver = null;
1335
Robert Greenwalt2ffe4122014-12-12 12:22:31 -08001336 private void startListeningForSimChanges() {
1337 if (DBG) Log.d(TAG, "startListeningForSimChanges");
1338 if (mBroadcastReceiver == null) {
1339 mBroadcastReceiver = new SimChangeBroadcastReceiver(
1340 mSimBcastGenerationNumber.incrementAndGet());
1341 final IntentFilter filter = new IntentFilter();
1342 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
1343
1344 mContext.registerReceiver(mBroadcastReceiver, filter);
1345 }
1346 }
1347
1348 private void stopListeningForSimChanges() {
1349 if (DBG) Log.d(TAG, "stopListeningForSimChanges");
1350 if (mBroadcastReceiver != null) {
1351 mSimBcastGenerationNumber.incrementAndGet();
1352 mContext.unregisterReceiver(mBroadcastReceiver);
1353 mBroadcastReceiver = null;
1354 }
1355 }
1356
1357 class SimChangeBroadcastReceiver extends BroadcastReceiver {
1358 // used to verify this receiver is still current
1359 final private int mGenerationNumber;
1360
1361 // we're interested in edge-triggered LOADED notifications, so
1362 // ignore LOADED unless we saw an ABSENT state first
1363 private boolean mSimAbsentSeen = false;
1364
1365 public SimChangeBroadcastReceiver(int generationNumber) {
1366 super();
1367 mGenerationNumber = generationNumber;
1368 }
1369
1370 @Override
1371 public void onReceive(Context context, Intent intent) {
1372 if (DBG) {
1373 Log.d(TAG, "simchange mGenerationNumber=" + mGenerationNumber +
1374 ", current generationNumber=" + mSimBcastGenerationNumber.get());
1375 }
1376 if (mGenerationNumber != mSimBcastGenerationNumber.get()) return;
1377
1378 final String state =
1379 intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
1380
1381 Log.d(TAG, "got Sim changed to state " + state + ", mSimAbsentSeen=" +
1382 mSimAbsentSeen);
1383 if (!mSimAbsentSeen && IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(state)) {
1384 mSimAbsentSeen = true;
1385 }
1386
1387 if (mSimAbsentSeen && IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(state)) {
1388 mSimAbsentSeen = false;
1389 try {
1390 if (mContext.getResources().getString(com.android.internal.R.string.
1391 config_mobile_hotspot_provision_app_no_ui).isEmpty() == false) {
Robert Greenwalt2ffe4122014-12-12 12:22:31 -08001392 ArrayList<Integer> tethered = new ArrayList<Integer>();
1393 synchronized (mPublicSync) {
Christopher Wileyd985dde2016-05-31 10:44:35 -07001394 for (int i = 0; i < mTetherStates.size(); i++) {
1395 TetherState tetherState = mTetherStates.valueAt(i);
1396 if (tetherState.mLastState !=
1397 IControlsTethering.STATE_TETHERED) {
1398 continue; // Skip interfaces that aren't tethered.
1399 }
1400 String iface = mTetherStates.keyAt(i);
1401 int interfaceType = ifaceNameToType(iface);
1402 if (interfaceType != ConnectivityManager.TETHERING_INVALID) {
1403 tethered.add(new Integer(interfaceType));
Robert Greenwalt2ffe4122014-12-12 12:22:31 -08001404 }
1405 }
1406 }
1407 for (int tetherType : tethered) {
1408 Intent startProvIntent = new Intent();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001409 startProvIntent.putExtra(
1410 ConnectivityManager.EXTRA_ADD_TETHER_TYPE, tetherType);
1411 startProvIntent.putExtra(
1412 ConnectivityManager.EXTRA_RUN_PROVISION, true);
1413 startProvIntent.setComponent(TETHER_SERVICE);
Robert Greenwalt2ffe4122014-12-12 12:22:31 -08001414 mContext.startServiceAsUser(startProvIntent, UserHandle.CURRENT);
1415 }
1416 Log.d(TAG, "re-evaluate provisioning");
1417 } else {
1418 Log.d(TAG, "no prov-check needed for new SIM");
1419 }
1420 } catch (Resources.NotFoundException e) {
1421 Log.d(TAG, "no prov-check needed for new SIM");
1422 // not defined, do nothing
1423 }
1424 }
1425 }
1426 }
1427
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001428 class InitialState extends TetherMasterUtilState {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001429 @Override
1430 public boolean processMessage(Message message) {
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001431 maybeLogMessage(this, message.what);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001432 boolean retValue = true;
1433 switch (message.what) {
1434 case CMD_TETHER_MODE_REQUESTED:
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001435 TetherInterfaceStateMachine who = (TetherInterfaceStateMachine)message.obj;
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001436 if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
Christopher Wileyd985dde2016-05-31 10:44:35 -07001437 if (mNotifyList.indexOf(who) < 0) {
1438 mNotifyList.add(who);
Erik Kline6e29bf02016-08-15 16:16:18 +09001439 mIPv6TetheringCoordinator.addActiveDownstream(who);
Christopher Wileyd985dde2016-05-31 10:44:35 -07001440 }
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001441 transitionTo(mTetherModeAliveState);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001442 break;
1443 case CMD_TETHER_MODE_UNREQUESTED:
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001444 who = (TetherInterfaceStateMachine)message.obj;
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001445 if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
Christopher Wileyd985dde2016-05-31 10:44:35 -07001446 mNotifyList.remove(who);
Erik Kline6e29bf02016-08-15 16:16:18 +09001447 mIPv6TetheringCoordinator.removeActiveDownstream(who);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001448 break;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001449 default:
1450 retValue = false;
1451 break;
1452 }
1453 return retValue;
1454 }
1455 }
1456
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001457 class TetherModeAliveState extends TetherMasterUtilState {
Erik Kline6ff17f72015-12-10 20:42:12 +09001458 boolean mTryCell = true;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001459 @Override
1460 public void enter() {
Erik Kline6ff17f72015-12-10 20:42:12 +09001461 // TODO: examine if we should check the return value.
Robert Greenwalt4f74d552011-12-19 16:59:31 -08001462 turnOnMasterTetherSettings(); // may transition us out
Robert Greenwalt2ffe4122014-12-12 12:22:31 -08001463 startListeningForSimChanges();
Erik Kline6ff17f72015-12-10 20:42:12 +09001464 mUpstreamNetworkMonitor.start();
Robert Greenwalt4f74d552011-12-19 16:59:31 -08001465
Erik Kline6ff17f72015-12-10 20:42:12 +09001466 mTryCell = true; // better try something first pass or crazy tests cases will fail
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001467 chooseUpstreamType(mTryCell);
1468 mTryCell = !mTryCell;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001469 }
Erik Klinefa37b2f2016-08-02 18:27:03 +09001470
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001471 @Override
1472 public void exit() {
Robert Greenwaltccf83af12011-06-02 17:30:47 -07001473 turnOffUpstreamMobileConnection();
Erik Kline6ff17f72015-12-10 20:42:12 +09001474 mUpstreamNetworkMonitor.stop();
Robert Greenwalt2ffe4122014-12-12 12:22:31 -08001475 stopListeningForSimChanges();
Robert Greenwaltb37f0c62010-03-25 14:50:34 -07001476 notifyTetheredOfNewUpstreamIface(null);
Erik Klinefa37b2f2016-08-02 18:27:03 +09001477 handleNewUpstreamNetworkState(null);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001478 }
Erik Klinefa37b2f2016-08-02 18:27:03 +09001479
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001480 @Override
1481 public boolean processMessage(Message message) {
Lorenzo Colitticd63d242016-04-10 15:39:53 +09001482 maybeLogMessage(this, message.what);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001483 boolean retValue = true;
1484 switch (message.what) {
Erik Kline6e29bf02016-08-15 16:16:18 +09001485 case CMD_TETHER_MODE_REQUESTED: {
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001486 TetherInterfaceStateMachine who = (TetherInterfaceStateMachine)message.obj;
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001487 if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
Christopher Wileyd985dde2016-05-31 10:44:35 -07001488 if (mNotifyList.indexOf(who) < 0) {
1489 mNotifyList.add(who);
Erik Kline6e29bf02016-08-15 16:16:18 +09001490 mIPv6TetheringCoordinator.addActiveDownstream(who);
Christopher Wileyd985dde2016-05-31 10:44:35 -07001491 }
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001492 who.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED,
Erik Kline6ff17f72015-12-10 20:42:12 +09001493 mCurrentUpstreamIface);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001494 break;
Erik Kline6e29bf02016-08-15 16:16:18 +09001495 }
1496 case CMD_TETHER_MODE_UNREQUESTED: {
1497 TetherInterfaceStateMachine who = (TetherInterfaceStateMachine)message.obj;
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001498 if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
Christopher Wileyd985dde2016-05-31 10:44:35 -07001499 if (mNotifyList.remove(who)) {
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001500 if (DBG) Log.d(TAG, "TetherModeAlive removing notifyee " + who);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001501 if (mNotifyList.isEmpty()) {
Robert Greenwaltd70a3d42010-02-23 18:15:29 -08001502 turnOffMasterTetherSettings(); // transitions appropriately
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001503 } else {
1504 if (DBG) {
1505 Log.d(TAG, "TetherModeAlive still has " + mNotifyList.size() +
1506 " live requests:");
Christopher Wileyd985dde2016-05-31 10:44:35 -07001507 for (TetherInterfaceStateMachine o : mNotifyList) {
1508 Log.d(TAG, " " + o);
1509 }
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001510 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001511 }
Robert Greenwalt68ea9b02012-05-10 16:58:16 -07001512 } else {
1513 Log.e(TAG, "TetherModeAliveState UNREQUESTED has unknown who: " + who);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001514 }
Erik Kline6e29bf02016-08-15 16:16:18 +09001515 mIPv6TetheringCoordinator.removeActiveDownstream(who);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001516 break;
Erik Kline6e29bf02016-08-15 16:16:18 +09001517 }
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001518 case CMD_UPSTREAM_CHANGED:
Robert Greenwalt05d89362011-01-23 16:04:05 -08001519 // need to try DUN immediately if Wifi goes down
Erik Kline6ff17f72015-12-10 20:42:12 +09001520 mTryCell = true;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001521 chooseUpstreamType(mTryCell);
1522 mTryCell = !mTryCell;
1523 break;
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001524 case CMD_RETRY_UPSTREAM:
1525 chooseUpstreamType(mTryCell);
1526 mTryCell = !mTryCell;
1527 break;
Erik Kline00019f42016-06-30 19:31:46 +09001528 case EVENT_UPSTREAM_CALLBACK: {
Erik Kline452b4b72017-01-11 17:34:50 +09001529 final NetworkState ns = (NetworkState) message.obj;
Erik Kline00019f42016-06-30 19:31:46 +09001530
1531 if (ns == null || !pertainsToCurrentUpstream(ns)) {
1532 // TODO: In future, this is where upstream evaluation and selection
1533 // could be handled for notifications which include sufficient data.
1534 // For example, after CONNECTIVITY_ACTION listening is removed, here
1535 // is where we could observe a Wi-Fi network becoming available and
1536 // passing validation.
1537 if (mCurrentUpstreamIface == null) {
1538 // If we have no upstream interface, try to run through upstream
1539 // selection again. If, for example, IPv4 connectivity has shown up
1540 // after IPv6 (e.g., 464xlat became available) we want the chance to
1541 // notice and act accordingly.
1542 chooseUpstreamType(false);
1543 }
1544 break;
1545 }
1546
1547 switch (message.arg1) {
1548 case UpstreamNetworkMonitor.EVENT_ON_AVAILABLE:
1549 // The default network changed, or DUN connected
1550 // before this callback was processed. Updates
1551 // for the current NetworkCapabilities and
1552 // LinkProperties have been requested (default
1553 // request) or are being sent shortly (DUN). Do
1554 // nothing until they arrive; if no updates
1555 // arrive there's nothing to do.
1556 break;
1557 case UpstreamNetworkMonitor.EVENT_ON_CAPABILITIES:
1558 handleNewUpstreamNetworkState(ns);
1559 break;
1560 case UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES:
1561 setDnsForwarders(ns.network, ns.linkProperties);
1562 handleNewUpstreamNetworkState(ns);
1563 break;
1564 case UpstreamNetworkMonitor.EVENT_ON_LOST:
1565 // TODO: Re-evaluate possible upstreams. Currently upstream
1566 // reevaluation is triggered via received CONNECTIVITY_ACTION
1567 // broadcasts that result in being passed a
1568 // TetherMasterSM.CMD_UPSTREAM_CHANGED.
Erik Klinefa37b2f2016-08-02 18:27:03 +09001569 handleNewUpstreamNetworkState(null);
Erik Kline00019f42016-06-30 19:31:46 +09001570 break;
1571 default:
1572 break;
Erik Kline6ff17f72015-12-10 20:42:12 +09001573 }
1574 break;
Erik Kline00019f42016-06-30 19:31:46 +09001575 }
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001576 default:
1577 retValue = false;
1578 break;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001579 }
1580 return retValue;
1581 }
1582 }
1583
Wink Saville64c42ca2011-04-18 14:55:10 -07001584 class ErrorState extends State {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001585 int mErrorNotification;
1586 @Override
1587 public boolean processMessage(Message message) {
1588 boolean retValue = true;
1589 switch (message.what) {
1590 case CMD_TETHER_MODE_REQUESTED:
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001591 TetherInterfaceStateMachine who = (TetherInterfaceStateMachine)message.obj;
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001592 who.sendMessage(mErrorNotification);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001593 break;
1594 default:
1595 retValue = false;
1596 }
1597 return retValue;
1598 }
1599 void notify(int msgType) {
1600 mErrorNotification = msgType;
Christopher Wileyd985dde2016-05-31 10:44:35 -07001601 for (TetherInterfaceStateMachine sm : mNotifyList) {
Robert Greenwalt6a1967c2010-03-17 11:19:57 -07001602 sm.sendMessage(msgType);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001603 }
1604 }
1605
1606 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001607 class SetIpForwardingEnabledErrorState extends ErrorState {
1608 @Override
1609 public void enter() {
1610 Log.e(TAG, "Error in setIpForwardingEnabled");
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001611 notify(TetherInterfaceStateMachine.CMD_IP_FORWARDING_ENABLE_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001612 }
1613 }
1614
1615 class SetIpForwardingDisabledErrorState extends ErrorState {
1616 @Override
1617 public void enter() {
1618 Log.e(TAG, "Error in setIpForwardingDisabled");
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001619 notify(TetherInterfaceStateMachine.CMD_IP_FORWARDING_DISABLE_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001620 }
1621 }
1622
1623 class StartTetheringErrorState extends ErrorState {
1624 @Override
1625 public void enter() {
1626 Log.e(TAG, "Error in startTethering");
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001627 notify(TetherInterfaceStateMachine.CMD_START_TETHERING_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001628 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001629 mNMService.setIpForwardingEnabled(false);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001630 } catch (Exception e) {}
1631 }
1632 }
1633
1634 class StopTetheringErrorState extends ErrorState {
1635 @Override
1636 public void enter() {
1637 Log.e(TAG, "Error in stopTethering");
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001638 notify(TetherInterfaceStateMachine.CMD_STOP_TETHERING_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001639 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001640 mNMService.setIpForwardingEnabled(false);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001641 } catch (Exception e) {}
1642 }
1643 }
1644
1645 class SetDnsForwardersErrorState extends ErrorState {
1646 @Override
1647 public void enter() {
1648 Log.e(TAG, "Error in setDnsForwarders");
Mitchell Wills7040b4e2016-05-23 16:40:10 -07001649 notify(TetherInterfaceStateMachine.CMD_SET_DNS_FORWARDERS_ERROR);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001650 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001651 mNMService.stopTethering();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001652 } catch (Exception e) {}
1653 try {
Chia-chi Yehc9338302011-05-11 16:35:13 -07001654 mNMService.setIpForwardingEnabled(false);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001655 } catch (Exception e) {}
1656 }
1657 }
1658 }
1659
Christopher Wiley499a57a2016-05-16 16:19:07 -07001660 @Override
Lorenzo Colittie3805462015-06-03 11:18:24 +09001661 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Christopher Wiley499a57a2016-05-16 16:19:07 -07001662 // Binder.java closes the resource for us.
1663 @SuppressWarnings("resource")
Lorenzo Colittie3805462015-06-03 11:18:24 +09001664 final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ");
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001665 if (mContext.checkCallingOrSelfPermission(
1666 android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
1667 pw.println("Permission Denial: can't dump ConnectivityService.Tether " +
1668 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1669 Binder.getCallingUid());
1670 return;
1671 }
1672
Lorenzo Colittie3805462015-06-03 11:18:24 +09001673 pw.println("Tethering:");
1674 pw.increaseIndent();
1675 pw.print("mUpstreamIfaceTypes:");
Robert Greenwaltb4453622011-11-03 16:01:40 -07001676 synchronized (mPublicSync) {
Robert Greenwaltb4453622011-11-03 16:01:40 -07001677 for (Integer netType : mUpstreamIfaceTypes) {
Lorenzo Colittie3805462015-06-03 11:18:24 +09001678 pw.print(" " + ConnectivityManager.getNetworkTypeName(netType));
Robert Greenwaltb4453622011-11-03 16:01:40 -07001679 }
Robert Greenwaltb4453622011-11-03 16:01:40 -07001680 pw.println();
Lorenzo Colittie3805462015-06-03 11:18:24 +09001681
Robert Greenwaltb4453622011-11-03 16:01:40 -07001682 pw.println("Tether state:");
Lorenzo Colittie3805462015-06-03 11:18:24 +09001683 pw.increaseIndent();
Christopher Wileyd985dde2016-05-31 10:44:35 -07001684 for (int i = 0; i < mTetherStates.size(); i++) {
1685 final String iface = mTetherStates.keyAt(i);
1686 final TetherState tetherState = mTetherStates.valueAt(i);
1687 pw.print(iface + " - ");
1688
1689 switch (tetherState.mLastState) {
1690 case IControlsTethering.STATE_UNAVAILABLE:
1691 pw.print("UnavailableState");
1692 break;
1693 case IControlsTethering.STATE_AVAILABLE:
1694 pw.print("AvailableState");
1695 break;
1696 case IControlsTethering.STATE_TETHERED:
1697 pw.print("TetheredState");
1698 break;
1699 default:
1700 pw.print("UnknownState");
1701 break;
1702 }
1703 pw.println(" - lastError = " + tetherState.mLastError);
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001704 }
Lorenzo Colittie3805462015-06-03 11:18:24 +09001705 pw.decreaseIndent();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001706 }
Lorenzo Colittie3805462015-06-03 11:18:24 +09001707 pw.decreaseIndent();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001708 }
Christopher Wileye03fb442016-05-18 13:45:20 -07001709
1710 @Override
Christopher Wileyd985dde2016-05-31 10:44:35 -07001711 public void notifyInterfaceStateChange(String iface, TetherInterfaceStateMachine who,
1712 int state, int error) {
1713 synchronized (mPublicSync) {
1714 TetherState tetherState = mTetherStates.get(iface);
1715 if (tetherState != null && tetherState.mStateMachine.equals(who)) {
1716 tetherState.mLastState = state;
1717 tetherState.mLastError = error;
1718 } else {
1719 if (DBG) Log.d(TAG, "got notification from stale iface " + iface);
1720 }
1721 }
1722
1723 if (DBG) {
1724 Log.d(TAG, "iface " + iface + " notified that it was in state " + state +
1725 " with error " + error);
1726 }
1727
Christopher Wileydeebfec2016-09-16 11:14:36 -07001728 try {
1729 // Notify that we're tethering (or not) this interface.
1730 // This is how data saver for instance knows if the user explicitly
1731 // turned on tethering (thus keeping us from being in data saver mode).
1732 mPolicyManager.onTetheringChanged(iface, state == IControlsTethering.STATE_TETHERED);
1733 } catch (RemoteException e) {
1734 // Not really very much we can do here.
1735 }
1736
Christopher Wileyd985dde2016-05-31 10:44:35 -07001737 switch (state) {
1738 case IControlsTethering.STATE_UNAVAILABLE:
1739 case IControlsTethering.STATE_AVAILABLE:
1740 mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED, who);
1741 break;
1742 case IControlsTethering.STATE_TETHERED:
1743 mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED, who);
1744 break;
1745 }
1746 sendTetherStateChangedBroadcast();
1747 }
1748
1749 private void trackNewTetherableInterface(String iface, int interfaceType) {
1750 TetherState tetherState;
1751 tetherState = new TetherState(new TetherInterfaceStateMachine(iface, mLooper,
Lorenzo Colitti14422332016-10-28 17:45:55 +09001752 interfaceType, mNMService, mStatsService, this,
1753 new IPv6TetheringInterfaceServices(iface, mNMService)));
Christopher Wileyd985dde2016-05-31 10:44:35 -07001754 mTetherStates.put(iface, tetherState);
1755 tetherState.mStateMachine.start();
Christopher Wileye03fb442016-05-18 13:45:20 -07001756 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001757}